|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +#include <config.h> |
| 5 | +#include "dn-simdhash.h" |
| 6 | + |
| 7 | +#include "dn-simdhash-utils.h" |
| 8 | + |
| 9 | +typedef unsigned int guint; |
| 10 | +typedef int32_t gboolean; |
| 11 | +typedef void * gpointer; |
| 12 | +typedef const void * gconstpointer; |
| 13 | + |
| 14 | +typedef void (*GDestroyNotify) (gpointer data); |
| 15 | +typedef guint (*GHashFunc) (gconstpointer key); |
| 16 | +typedef gboolean (*GEqualFunc) (gconstpointer a, gconstpointer b); |
| 17 | + |
| 18 | +typedef struct dn_simdhash_ght_data { |
| 19 | + GHashFunc hash_func; |
| 20 | + GEqualFunc key_equal_func; |
| 21 | + GDestroyNotify key_destroy_func; |
| 22 | + GDestroyNotify value_destroy_func; |
| 23 | +} dn_simdhash_ght_data; |
| 24 | + |
| 25 | +static inline uint32_t |
| 26 | +dn_simdhash_ght_hash (dn_simdhash_ght_data data, gconstpointer key) |
| 27 | +{ |
| 28 | + GHashFunc hash_func = data.hash_func; |
| 29 | + if (hash_func) |
| 30 | + return (uint32_t)hash_func(key); |
| 31 | + else |
| 32 | + // FIXME: Seed |
| 33 | + return MurmurHash3_32_ptr(key, 0); |
| 34 | +} |
| 35 | + |
| 36 | +static inline gboolean |
| 37 | +dn_simdhash_ght_equals (dn_simdhash_ght_data data, gconstpointer lhs, gconstpointer rhs) |
| 38 | +{ |
| 39 | + GEqualFunc equal_func = data.key_equal_func; |
| 40 | + if (equal_func) |
| 41 | + return equal_func(lhs, rhs); |
| 42 | + else |
| 43 | + return lhs == rhs; |
| 44 | +} |
| 45 | + |
| 46 | +static inline void |
| 47 | +dn_simdhash_ght_removed (dn_simdhash_ght_data data, gconstpointer key, gpointer value) |
| 48 | +{ |
| 49 | + GDestroyNotify key_destroy_func = data.key_destroy_func, |
| 50 | + value_destroy_func = data.value_destroy_func; |
| 51 | + if (key_destroy_func) |
| 52 | + key_destroy_func((gpointer)key); |
| 53 | + if (value_destroy_func) |
| 54 | + value_destroy_func((gpointer)value); |
| 55 | +} |
| 56 | + |
| 57 | +static inline void |
| 58 | +dn_simdhash_ght_replaced (dn_simdhash_ght_data data, gconstpointer key, gpointer old_value, gpointer new_value) |
| 59 | +{ |
| 60 | + if (old_value == new_value) |
| 61 | + return; |
| 62 | + |
| 63 | + GDestroyNotify value_destroy_func = data.value_destroy_func; |
| 64 | + if (value_destroy_func) |
| 65 | + value_destroy_func((gpointer)old_value); |
| 66 | +} |
| 67 | + |
| 68 | +#define DN_SIMDHASH_T dn_simdhash_ght |
| 69 | +#define DN_SIMDHASH_KEY_T gconstpointer |
| 70 | +#define DN_SIMDHASH_VALUE_T gpointer |
| 71 | +#define DN_SIMDHASH_INSTANCE_DATA_T dn_simdhash_ght_data |
| 72 | +#define DN_SIMDHASH_KEY_HASHER dn_simdhash_ght_hash |
| 73 | +#define DN_SIMDHASH_KEY_EQUALS dn_simdhash_ght_equals |
| 74 | +#define DN_SIMDHASH_ON_REMOVE dn_simdhash_ght_removed |
| 75 | +#define DN_SIMDHASH_ON_REPLACE dn_simdhash_ght_replaced |
| 76 | +#if SIZEOF_VOID_P == 8 |
| 77 | +#define DN_SIMDHASH_BUCKET_CAPACITY 11 |
| 78 | +#else |
| 79 | +#define DN_SIMDHASH_BUCKET_CAPACITY 12 |
| 80 | +#endif |
| 81 | +#define DN_SIMDHASH_NO_DEFAULT_NEW 1 |
| 82 | + |
| 83 | +#include "dn-simdhash-specialization.h" |
| 84 | +#include "dn-simdhash-ght-compatible.h" |
| 85 | + |
| 86 | +dn_simdhash_ght_t * |
| 87 | +dn_simdhash_ght_new ( |
| 88 | + GHashFunc hash_func, GEqualFunc key_equal_func, |
| 89 | + uint32_t capacity, dn_allocator_t *allocator |
| 90 | +) |
| 91 | +{ |
| 92 | + dn_simdhash_ght_t *hash = dn_simdhash_new_internal(&DN_SIMDHASH_T_META, DN_SIMDHASH_T_VTABLE, capacity, allocator); |
| 93 | + dn_simdhash_instance_data(dn_simdhash_ght_data, hash).hash_func = hash_func; |
| 94 | + dn_simdhash_instance_data(dn_simdhash_ght_data, hash).key_equal_func = key_equal_func; |
| 95 | + return hash; |
| 96 | +} |
| 97 | + |
| 98 | +dn_simdhash_ght_t * |
| 99 | +dn_simdhash_ght_new_full ( |
| 100 | + GHashFunc hash_func, GEqualFunc key_equal_func, |
| 101 | + GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func, |
| 102 | + uint32_t capacity, dn_allocator_t *allocator |
| 103 | +) |
| 104 | +{ |
| 105 | + dn_simdhash_ght_t *hash = dn_simdhash_new_internal(&DN_SIMDHASH_T_META, DN_SIMDHASH_T_VTABLE, capacity, allocator); |
| 106 | + dn_simdhash_instance_data(dn_simdhash_ght_data, hash).hash_func = hash_func; |
| 107 | + dn_simdhash_instance_data(dn_simdhash_ght_data, hash).key_equal_func = key_equal_func; |
| 108 | + dn_simdhash_instance_data(dn_simdhash_ght_data, hash).key_destroy_func = key_destroy_func; |
| 109 | + dn_simdhash_instance_data(dn_simdhash_ght_data, hash).value_destroy_func = value_destroy_func; |
| 110 | + return hash; |
| 111 | +} |
0 commit comments