src/ucx/hash_key.c

changeset 490
d218607f5a7e
parent 438
22eca559aded
equal deleted inserted replaced
489:921f83a8943f 490:d218607f5a7e
28 28
29 #include "cx/hash_key.h" 29 #include "cx/hash_key.h"
30 #include <string.h> 30 #include <string.h>
31 31
32 void cx_hash_murmur(CxHashKey *key) { 32 void cx_hash_murmur(CxHashKey *key) {
33 unsigned char const *data = key->data.cbytes; 33 unsigned char const *data = key->data;
34 if (data == NULL) { 34 if (data == NULL) {
35 // extension: special value for NULL 35 // extension: special value for NULL
36 key->hash = 1574210520u; 36 key->hash = 1574210520u;
37 return; 37 return;
38 } 38 }
69 case 1: 69 case 1:
70 h ^= (data[i + 0] & 0xFF); 70 h ^= (data[i + 0] & 0xFF);
71 h *= m; 71 h *= m;
72 __attribute__((__fallthrough__)); 72 __attribute__((__fallthrough__));
73 default: // do nothing 73 default: // do nothing
74 ; 74 ;
75 } 75 }
76 76
77 h ^= h >> 13; 77 h ^= h >> 13;
78 h *= m; 78 h *= m;
79 h ^= h >> 15; 79 h ^= h >> 15;
81 key->hash = h; 81 key->hash = h;
82 } 82 }
83 83
84 CxHashKey cx_hash_key_str(char const *str) { 84 CxHashKey cx_hash_key_str(char const *str) {
85 CxHashKey key; 85 CxHashKey key;
86 key.data.cstr = str; 86 key.data = str;
87 key.len = str == NULL ? 0 : strlen(str); 87 key.len = str == NULL ? 0 : strlen(str);
88 cx_hash_murmur(&key); 88 cx_hash_murmur(&key);
89 return key; 89 return key;
90 } 90 }
91 91
92 CxHashKey cx_hash_key_bytes( 92 CxHashKey cx_hash_key_bytes(
93 unsigned char const *bytes, 93 unsigned char const *bytes,
94 size_t len 94 size_t len
95 ) { 95 ) {
96 CxHashKey key; 96 CxHashKey key;
97 key.data.cbytes = bytes; 97 key.data = bytes;
98 key.len = len; 98 key.len = len;
99 cx_hash_murmur(&key); 99 cx_hash_murmur(&key);
100 return key; 100 return key;
101 } 101 }
102 102
103 CxHashKey cx_hash_key( 103 CxHashKey cx_hash_key(
104 void const *obj, 104 void const *obj,
105 size_t len 105 size_t len
106 ) { 106 ) {
107 CxHashKey key; 107 CxHashKey key;
108 key.data.cobj = obj; 108 key.data = obj;
109 key.len = len; 109 key.len = len;
110 cx_hash_murmur(&key); 110 cx_hash_murmur(&key);
111 return key; 111 return key;
112 } 112 }

mercurial