25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
27 */ |
27 */ |
28 |
28 |
29 #include "cx/hash_key.h" |
29 #include "cx/hash_key.h" |
|
30 #include "cx/compare.h" |
30 #include <string.h> |
31 #include <string.h> |
31 |
32 |
32 void cx_hash_murmur(CxHashKey *key) { |
33 void cx_hash_murmur(CxHashKey *key) { |
33 const unsigned char *data = key->data; |
34 const unsigned char *data = key->data; |
34 if (data == NULL) { |
35 if (data == NULL) { |
79 h ^= h >> 15; |
80 h ^= h >> 15; |
80 |
81 |
81 key->hash = h; |
82 key->hash = h; |
82 } |
83 } |
83 |
84 |
|
85 |
|
86 uint32_t cx_hash_u32(uint32_t x) { |
|
87 x = ((x >> 16) ^ x) * 0x45d9f3bu; |
|
88 x = ((x >> 16) ^ x) * 0x45d9f3bu; |
|
89 x = (x >> 16) ^ x; |
|
90 return x; |
|
91 } |
|
92 |
|
93 uint64_t cx_hash_u64(uint64_t x) { |
|
94 x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9); |
|
95 x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb); |
|
96 x = x ^ (x >> 31); |
|
97 return x; |
|
98 } |
|
99 |
84 CxHashKey cx_hash_key_str(const char *str) { |
100 CxHashKey cx_hash_key_str(const char *str) { |
85 CxHashKey key; |
101 CxHashKey key; |
86 key.data = str; |
102 key.data = str; |
87 key.len = str == NULL ? 0 : strlen(str); |
103 key.len = str == NULL ? 0 : strlen(str); |
88 cx_hash_murmur(&key); |
104 cx_hash_murmur(&key); |
108 key.data = obj; |
124 key.data = obj; |
109 key.len = len; |
125 key.len = len; |
110 cx_hash_murmur(&key); |
126 cx_hash_murmur(&key); |
111 return key; |
127 return key; |
112 } |
128 } |
|
129 |
|
130 CxHashKey cx_hash_key_u32(uint32_t x) { |
|
131 CxHashKey key; |
|
132 key.data = NULL; |
|
133 key.len = 0; |
|
134 key.hash = cx_hash_u32(x); |
|
135 return key; |
|
136 } |
|
137 |
|
138 CxHashKey cx_hash_key_u64(uint64_t x) { |
|
139 CxHashKey key; |
|
140 key.data = NULL; |
|
141 key.len = 0; |
|
142 key.hash = cx_hash_u64(x); |
|
143 return key; |
|
144 } |
|
145 |
|
146 int cx_hash_key_cmp(const CxHashKey *left, const CxHashKey *right) { |
|
147 int d; |
|
148 d = cx_vcmp_uint64(left->hash, right->hash); |
|
149 if (d != 0) return d; |
|
150 d = cx_vcmp_size(left->len, right->len); |
|
151 if (d != 0) return d; |
|
152 if (left->len == 0) return 0; |
|
153 return memcmp(left->data, right->data, left->len); |
|
154 } |