ucx/hash_key.c

changeset 888
af685cc9d623
parent 852
83fdf679df99
equal deleted inserted replaced
877:b60487c3ec36 888:af685cc9d623
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) {
60 } 61 }
61 62
62 switch (len) { 63 switch (len) {
63 case 3: 64 case 3:
64 h ^= (data[i + 2] & 0xFF) << 16; 65 h ^= (data[i + 2] & 0xFF) << 16;
65 __attribute__((__fallthrough__)); 66 cx_attr_fallthrough;
66 case 2: 67 case 2:
67 h ^= (data[i + 1] & 0xFF) << 8; 68 h ^= (data[i + 1] & 0xFF) << 8;
68 __attribute__((__fallthrough__)); 69 cx_attr_fallthrough;
69 case 1: 70 case 1:
70 h ^= (data[i + 0] & 0xFF); 71 h ^= (data[i + 0] & 0xFF);
71 h *= m; 72 h *= m;
72 __attribute__((__fallthrough__)); 73 cx_attr_fallthrough;
73 default: // do nothing 74 default: // do nothing
74 ; 75 ;
75 } 76 }
76 77
77 h ^= h >> 13; 78 h ^= h >> 13;
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);
89 return key; 105 return key;
106 }
107
108 CxHashKey cx_hash_key_ustr(unsigned const char *str) {
109 CxHashKey key;
110 key.data = str;
111 key.len = str == NULL ? 0 : strlen((const char*)str);
112 cx_hash_murmur(&key);
113 return key;
114 }
115
116 CxHashKey cx_hash_key_cxstr(cxstring str) {
117 return cx_hash_key(str.ptr, str.length);
118 }
119
120 CxHashKey cx_hash_key_mutstr(cxmutstr str) {
121 return cx_hash_key(str.ptr, str.length);
90 } 122 }
91 123
92 CxHashKey cx_hash_key_bytes( 124 CxHashKey cx_hash_key_bytes(
93 const unsigned char *bytes, 125 const unsigned char *bytes,
94 size_t len 126 size_t len
108 key.data = obj; 140 key.data = obj;
109 key.len = len; 141 key.len = len;
110 cx_hash_murmur(&key); 142 cx_hash_murmur(&key);
111 return key; 143 return key;
112 } 144 }
145
146 CxHashKey cx_hash_key_u32(uint32_t x) {
147 CxHashKey key;
148 key.data = NULL;
149 key.len = 0;
150 key.hash = cx_hash_u32(x);
151 return key;
152 }
153
154 CxHashKey cx_hash_key_u64(uint64_t x) {
155 CxHashKey key;
156 key.data = NULL;
157 key.len = 0;
158 key.hash = cx_hash_u64(x);
159 return key;
160 }
161
162 int cx_hash_key_cmp(const void *l, const void *r) {
163 const CxHashKey *left = l;
164 const CxHashKey *right = r;
165 int d;
166 d = cx_vcmp_uint64(left->hash, right->hash);
167 if (d != 0) return d;
168 d = cx_vcmp_size(left->len, right->len);
169 if (d != 0) return d;
170 if (left->len == 0) return 0;
171 return memcmp(left->data, right->data, left->len);
172 }

mercurial