| 87 * This function produces no collisions and has a good statistical distribution. |
87 * This function produces no collisions and has a good statistical distribution. |
| 88 * |
88 * |
| 89 * @param x the integer |
89 * @param x the integer |
| 90 * @return the hash |
90 * @return the hash |
| 91 */ |
91 */ |
| 92 CX_EXPORT uint32_t cx_hash_u32(uint32_t x); |
92 CX_INLINE uint32_t cx_hash_u32(uint32_t x) { |
| |
93 x = ((x >> 16) ^ x) * 0x45d9f3bu; |
| |
94 x = ((x >> 16) ^ x) * 0x45d9f3bu; |
| |
95 x = (x >> 16) ^ x; |
| |
96 return x; |
| |
97 } |
| 93 |
98 |
| 94 /** |
99 /** |
| 95 * Mixes up a 64-bit integer to be used as a hash. |
100 * Mixes up a 64-bit integer to be used as a hash. |
| 96 * |
101 * |
| 97 * This function produces no collisions and has a good statistical distribution. |
102 * This function produces no collisions and has a good statistical distribution. |
| 98 * |
103 * |
| 99 * @param x the integer |
104 * @param x the integer |
| 100 * @return the hash |
105 * @return the hash |
| 101 */ |
106 */ |
| 102 CX_EXPORT uint64_t cx_hash_u64(uint64_t x); |
107 CX_INLINE uint64_t cx_hash_u64(uint64_t x){ |
| 103 |
108 x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9); |
| 104 /** |
109 x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb); |
| 105 * Computes a hash key from a 32-bit integer. |
110 x = x ^ (x >> 31); |
| 106 * |
111 return x; |
| 107 * @param x the integer |
112 } |
| 108 * @return the hash key |
|
| 109 */ |
|
| 110 cx_attr_nodiscard |
|
| 111 CX_EXPORT CxHashKey cx_hash_key_u32(uint32_t x); |
|
| 112 |
|
| 113 /** |
|
| 114 * Computes a hash key from a 64-bit integer. |
|
| 115 * |
|
| 116 * @param x the integer |
|
| 117 * @return the hash key |
|
| 118 */ |
|
| 119 cx_attr_nodiscard |
|
| 120 CX_EXPORT CxHashKey cx_hash_key_u64(uint64_t x); |
|
| 121 |
|
| 122 /** |
|
| 123 * Computes a hash key from a string. |
|
| 124 * |
|
| 125 * The string needs to be zero-terminated. |
|
| 126 * |
|
| 127 * @param str the string |
|
| 128 * @return the hash key |
|
| 129 */ |
|
| 130 cx_attr_nodiscard cx_attr_cstr_arg(1) |
|
| 131 CX_EXPORT CxHashKey cx_hash_key_str(const char *str); |
|
| 132 |
|
| 133 /** |
|
| 134 * Computes a hash key from a string. |
|
| 135 * |
|
| 136 * Use this function when the string is represented |
|
| 137 * as an unsigned char array. |
|
| 138 * |
|
| 139 * The string needs to be zero-terminated. |
|
| 140 * |
|
| 141 * @param str the string |
|
| 142 * @return the hash key |
|
| 143 */ |
|
| 144 cx_attr_nodiscard cx_attr_cstr_arg(1) |
|
| 145 CX_EXPORT CxHashKey cx_hash_key_ustr(const unsigned char *str); |
|
| 146 |
|
| 147 /** |
|
| 148 * Computes a hash key from a byte array. |
|
| 149 * |
|
| 150 * @param bytes the array |
|
| 151 * @param len the length |
|
| 152 * @return the hash key |
|
| 153 */ |
|
| 154 cx_attr_nodiscard cx_attr_access_r(1, 2) |
|
| 155 CX_EXPORT CxHashKey cx_hash_key_bytes(const unsigned char *bytes, size_t len); |
|
| 156 |
113 |
| 157 /** |
114 /** |
| 158 * Computes a hash key for an arbitrary object. |
115 * Computes a hash key for an arbitrary object. |
| 159 * |
116 * |
| 160 * The computation uses the in-memory representation that might not be |
117 * The computation uses the in-memory representation that might not be |
| 168 cx_attr_nodiscard |
125 cx_attr_nodiscard |
| 169 cx_attr_access_r(1, 2) |
126 cx_attr_access_r(1, 2) |
| 170 CX_EXPORT CxHashKey cx_hash_key(const void *obj, size_t len); |
127 CX_EXPORT CxHashKey cx_hash_key(const void *obj, size_t len); |
| 171 |
128 |
| 172 /** |
129 /** |
| |
130 * Computes a hash key from a 32-bit integer. |
| |
131 * |
| |
132 * @param x the integer |
| |
133 * @return the hash key |
| |
134 */ |
| |
135 cx_attr_nodiscard |
| |
136 CX_INLINE CxHashKey cx_hash_key_u32(uint32_t x) { |
| |
137 CxHashKey key; |
| |
138 key.data = NULL; |
| |
139 key.len = 0; |
| |
140 key.hash = cx_hash_u32(x); |
| |
141 return key; |
| |
142 } |
| |
143 |
| |
144 /** |
| |
145 * Computes a hash key from a 64-bit integer. |
| |
146 * |
| |
147 * @param x the integer |
| |
148 * @return the hash key |
| |
149 */ |
| |
150 cx_attr_nodiscard |
| |
151 CX_INLINE CxHashKey cx_hash_key_u64(uint64_t x) { |
| |
152 CxHashKey key; |
| |
153 key.data = NULL; |
| |
154 key.len = 0; |
| |
155 key.hash = cx_hash_u64(x); |
| |
156 return key; |
| |
157 } |
| |
158 |
| |
159 /** |
| |
160 * Computes a hash key from a string. |
| |
161 * |
| |
162 * The string needs to be zero-terminated. |
| |
163 * |
| |
164 * @param str the string |
| |
165 * @return the hash key |
| |
166 */ |
| |
167 cx_attr_nodiscard cx_attr_cstr_arg(1) |
| |
168 CX_INLINE CxHashKey cx_hash_key_str(const char *str) { |
| |
169 return cx_hash_key((const void*)str, str == NULL ? 0 : strlen(str)); |
| |
170 } |
| |
171 |
| |
172 /** |
| |
173 * Computes a hash key from a string. |
| |
174 * |
| |
175 * Use this function when the string is represented |
| |
176 * as an unsigned char array. |
| |
177 * |
| |
178 * The string needs to be zero-terminated. |
| |
179 * |
| |
180 * @param str the string |
| |
181 * @return the hash key |
| |
182 */ |
| |
183 cx_attr_nodiscard cx_attr_cstr_arg(1) |
| |
184 CX_INLINE CxHashKey cx_hash_key_ustr(const unsigned char *str) { |
| |
185 return cx_hash_key((const void*)str, str == NULL ? 0 : strlen((const char*)str)); |
| |
186 } |
| |
187 |
| |
188 /** |
| |
189 * Computes a hash key from a byte array. |
| |
190 * |
| |
191 * @param bytes the array |
| |
192 * @param len the length |
| |
193 * @return the hash key |
| |
194 */ |
| |
195 cx_attr_nodiscard cx_attr_access_r(1, 2) |
| |
196 CX_INLINE CxHashKey cx_hash_key_bytes(const unsigned char *bytes, size_t len) { |
| |
197 return cx_hash_key((const void*)bytes, len); |
| |
198 } |
| |
199 |
| |
200 /** |
| 173 * Computes a hash key from a UCX string. |
201 * Computes a hash key from a UCX string. |
| 174 * |
202 * |
| 175 * @param str the string |
203 * @param str the string |
| 176 * @return the hash key |
204 * @return the hash key |
| 177 */ |
205 */ |
| 178 cx_attr_nodiscard |
206 cx_attr_nodiscard |
| 179 CX_EXPORT CxHashKey cx_hash_key_cxstr(cxstring str); |
207 CX_INLINE CxHashKey cx_hash_key_cxstr(cxstring str) { |
| |
208 return cx_hash_key((void*)str.ptr, str.length); |
| |
209 } |
| 180 |
210 |
| 181 /** |
211 /** |
| 182 * Computes a hash key from a UCX string. |
212 * Computes a hash key from a UCX string. |
| 183 * |
213 * |
| 184 * @param str the string |
214 * @param str the string |
| 185 * @return the hash key |
215 * @return the hash key |
| 186 */ |
216 */ |
| 187 cx_attr_nodiscard |
217 cx_attr_nodiscard |
| 188 CX_EXPORT CxHashKey cx_hash_key_mutstr(cxmutstr str); |
218 CX_INLINE CxHashKey cx_hash_key_mutstr(cxmutstr str) { |
| |
219 return cx_hash_key((void*)str.ptr, str.length); |
| |
220 } |
| 189 |
221 |
| 190 /** |
222 /** |
| 191 * The identity function for the CX_HASH_KEY() macro. |
223 * The identity function for the CX_HASH_KEY() macro. |
| 192 * You should never need to use this manually. |
224 * You should never need to use this manually. |
| 193 * |
225 * |