Branch data Line data Source code
1 : : // Copyright 2011-2025 David Robillard <d@drobilla.net>
2 : : // SPDX-License-Identifier: ISC
3 : :
4 : : #include <zix/hash.h>
5 : :
6 : : #include "qualifiers.h"
7 : :
8 : : #include <zix/allocator.h>
9 : : #include <zix/attributes.h>
10 : : #include <zix/status.h>
11 : :
12 : : #include <assert.h>
13 : : #include <stdbool.h>
14 : :
15 : : typedef struct ZixHashEntry {
16 : : ZixHashCode hash; ///< Non-folded hash value
17 : : ZixHashRecord* value; ///< Pointer to user-owned record
18 : : } ZixHashEntry;
19 : :
20 : : struct ZixHashImpl {
21 : : ZixAllocator* allocator; ///< User allocator
22 : : ZixKeyFunc key_func; ///< User key accessor
23 : : ZixHashFunc hash_func; ///< User hashing function
24 : : ZixKeyEqualFunc equal_func; ///< User equality comparison function
25 : : size_t count; ///< Number of records stored in the table
26 : : size_t mask; ///< Bit mask for fast modulo (n_entries - 1)
27 : : size_t n_entries; ///< Power of two table size
28 : : ZixHashEntry* entries; ///< Pointer to dynamically allocated table
29 : : };
30 : :
31 : : static ZIX_CONSTEXPR size_t min_n_entries = 4U;
32 : : static ZIX_CONSTEXPR size_t tombstone = 0xDEADU;
33 : :
34 : : ZixHash*
35 : 161 : zix_hash_new(ZixAllocator* const allocator,
36 : : const ZixKeyFunc key_func,
37 : : const ZixHashFunc hash_func,
38 : : const ZixKeyEqualFunc equal_func)
39 : : {
40 [ - + ]: 161 : assert(key_func);
41 [ - + ]: 161 : assert(hash_func);
42 [ - + ]: 161 : assert(equal_func);
43 : :
44 : 161 : ZixHash* const hash = (ZixHash*)zix_malloc(allocator, sizeof(ZixHash));
45 [ + + ]: 161 : if (!hash) {
46 : 7 : return NULL;
47 : : }
48 : :
49 : 154 : hash->allocator = allocator;
50 : 154 : hash->key_func = key_func;
51 : 154 : hash->hash_func = hash_func;
52 : 154 : hash->equal_func = equal_func;
53 : 154 : hash->count = 0U;
54 : 154 : hash->n_entries = min_n_entries;
55 : 154 : hash->mask = hash->n_entries - 1U;
56 : :
57 : 154 : hash->entries =
58 : 154 : (ZixHashEntry*)zix_calloc(allocator, hash->n_entries, sizeof(ZixHashEntry));
59 : :
60 [ + + ]: 154 : if (!hash->entries) {
61 : 7 : zix_free(allocator, hash);
62 : 7 : return NULL;
63 : : }
64 : :
65 : 147 : return hash;
66 : : }
67 : :
68 : : void
69 : 162 : zix_hash_free(ZixHash* const hash)
70 : : {
71 [ + + ]: 162 : if (hash) {
72 : 147 : zix_free(hash->allocator, hash->entries);
73 : 147 : zix_free(hash->allocator, hash);
74 : : }
75 : 162 : }
76 : :
77 : : ZIX_NONBLOCKING ZixHashIter
78 : 119 : zix_hash_begin(const ZixHash* const hash)
79 : : {
80 [ - + ]: 119 : assert(hash);
81 [ + + ]: 119 : return hash->entries[0U].value ? 0U : zix_hash_next(hash, 0U);
82 : : }
83 : :
84 : : ZIX_REALTIME ZixHashIter
85 : 6997 : zix_hash_end(const ZixHash* const hash)
86 : : {
87 [ - + ]: 6997 : assert(hash);
88 : 6997 : return hash->n_entries;
89 : : }
90 : :
91 : : ZIX_REALTIME ZixHashRecord*
92 : 3380 : zix_hash_get(const ZixHash* hash, const ZixHashIter i)
93 : : {
94 [ - + ]: 3380 : assert(hash);
95 [ - + ]: 3380 : assert(i < hash->n_entries);
96 : :
97 : 3380 : return hash->entries[i].value;
98 : : }
99 : :
100 : : ZIX_NONBLOCKING ZixHashIter
101 : 3409 : zix_hash_next(const ZixHash* const hash, ZixHashIter i)
102 : : {
103 [ - + ]: 3409 : assert(hash);
104 : : do {
105 : 6760 : ++i;
106 [ + + + + ]: 6760 : } while (i < hash->n_entries && !hash->entries[i].value);
107 : :
108 : 3409 : return i;
109 : : }
110 : :
111 : : ZIX_REALTIME size_t
112 : 7143 : zix_hash_size(const ZixHash* const hash)
113 : : {
114 [ - + ]: 7143 : assert(hash);
115 : 7143 : return hash->count;
116 : : }
117 : :
118 : : static inline size_t
119 : 760090 : fold_hash(const ZixHashCode h_nomod, const size_t mask)
120 : : {
121 : 760090 : return h_nomod & mask;
122 : : }
123 : :
124 : : static inline bool
125 : 1365950 : is_empty(const ZixHashEntry* const entry)
126 : : {
127 [ + + + + ]: 1365950 : return !entry->value && !entry->hash;
128 : : }
129 : :
130 : : static inline bool
131 : 1330487 : is_match(const ZixHash* const hash,
132 : : const ZixHashCode code,
133 : : const size_t entry_index,
134 : : ZixKeyEqualFunc predicate,
135 : : const void* const user_data)
136 : : {
137 : 1330487 : const ZixHashEntry* const entry = &hash->entries[entry_index];
138 : :
139 [ + + + + : 2076532 : return entry->value && entry->hash == code &&
+ + ]
140 : 746045 : predicate(hash->key_func(entry->value), user_data);
141 : : }
142 : :
143 : : static inline size_t
144 : 595355 : next_index(const ZixHash* const hash, const size_t i)
145 : : {
146 [ + + ]: 595355 : return (i == hash->mask) ? 0U : (i + 1U);
147 : : }
148 : :
149 : : static inline ZixHashIter
150 : 749596 : find_entry(const ZixHash* const hash,
151 : : const ZixHashKey* const key,
152 : : const size_t h,
153 : : const ZixHashCode code)
154 : : {
155 : 749596 : size_t i = h;
156 : :
157 [ + + ]: 1333381 : while (!is_empty(&hash->entries[i]) &&
158 [ + + ]: 1315393 : !is_match(hash, code, i, hash->equal_func, key)) {
159 : 583785 : i = next_index(hash, i);
160 : : }
161 : :
162 : 749596 : return i;
163 : : }
164 : :
165 : : static ZixStatus
166 : 757 : rehash(ZixHash* const hash, const size_t old_n_entries)
167 : : {
168 : 757 : ZixHashEntry* const old_entries = hash->entries;
169 : 757 : const size_t new_n_entries = hash->n_entries;
170 : :
171 : : // Allocate a new entries array
172 : 757 : ZixHashEntry* const new_entries = (ZixHashEntry*)zix_calloc(
173 : : hash->allocator, new_n_entries, sizeof(ZixHashEntry));
174 : :
175 [ + + ]: 757 : if (!new_entries) {
176 : 27 : return ZIX_STATUS_NO_MEM;
177 : : }
178 : :
179 : : // Replace the array in the hash first so we can use find_entry() normally
180 : 730 : hash->entries = new_entries;
181 : :
182 : : // Reinsert every element into the new array
183 [ + + ]: 26414 : for (size_t i = 0U; i < old_n_entries; ++i) {
184 : 25684 : const ZixHashEntry* const entry = &old_entries[i];
185 : :
186 [ + + ]: 25684 : if (entry->value) {
187 [ - + ]: 10855 : assert(hash->mask == hash->n_entries - 1U);
188 : 10855 : const size_t new_h = fold_hash(entry->hash, hash->mask);
189 : 10855 : const size_t new_i = find_entry(hash, entry->value, new_h, entry->hash);
190 : :
191 : 10855 : hash->entries[new_i] = *entry;
192 : : }
193 : : }
194 : :
195 : 730 : zix_free(hash->allocator, old_entries);
196 : 730 : return ZIX_STATUS_SUCCESS;
197 : : }
198 : :
199 : : static ZixStatus
200 : 505 : grow(ZixHash* const hash)
201 : : {
202 : 505 : const size_t old_n_entries = hash->n_entries;
203 : 505 : const size_t old_mask = hash->mask;
204 : :
205 : 505 : hash->n_entries <<= 1U;
206 : 505 : hash->mask = hash->n_entries - 1U;
207 : :
208 : 505 : const ZixStatus st = rehash(hash, old_n_entries);
209 [ + + ]: 505 : if (st) {
210 : 18 : hash->n_entries = old_n_entries;
211 : 18 : hash->mask = old_mask;
212 : : }
213 : :
214 : 505 : return st;
215 : : }
216 : :
217 : : static ZixStatus
218 : 384 : shrink(ZixHash* const hash)
219 : : {
220 [ + + ]: 384 : if (hash->n_entries > min_n_entries) {
221 : 252 : const size_t old_n_entries = hash->n_entries;
222 : :
223 : 252 : hash->n_entries >>= 1U;
224 : 252 : hash->mask = hash->n_entries - 1U;
225 : :
226 : 252 : return rehash(hash, old_n_entries);
227 : : }
228 : :
229 : 132 : return ZIX_STATUS_SUCCESS;
230 : : }
231 : :
232 : : ZixHashIter
233 : 10507 : zix_hash_find(const ZixHash* const hash, const ZixHashKey* const key)
234 : : {
235 [ - + ]: 10507 : assert(hash);
236 [ - + ]: 10507 : assert(key);
237 : :
238 : 10507 : const ZixHashCode h_nomod = hash->hash_func(key);
239 : 10507 : const size_t h = fold_hash(h_nomod, hash->mask);
240 : 10507 : const ZixHashIter i = find_entry(hash, key, h, h_nomod);
241 : :
242 [ + + ]: 10507 : return is_empty(&hash->entries[i]) ? hash->n_entries : i;
243 : : }
244 : :
245 : : ZixHashRecord*
246 : 728234 : zix_hash_find_record(const ZixHash* const hash, const ZixHashKey* const key)
247 : : {
248 [ - + ]: 728234 : assert(hash);
249 [ - + ]: 728234 : assert(key);
250 : :
251 : 728234 : const ZixHashCode h_nomod = hash->hash_func(key);
252 : 728234 : const size_t h = fold_hash(h_nomod, hash->mask);
253 : :
254 : 728234 : return hash->entries[find_entry(hash, key, h, h_nomod)].value;
255 : : }
256 : :
257 : : ZixHashInsertPlan
258 : 10494 : zix_hash_plan_insert_prehashed(const ZixHash* const hash,
259 : : const ZixHashCode code,
260 : : const ZixKeyMatchFunc predicate,
261 : : const void* const user_data)
262 : : {
263 [ - + ]: 10494 : assert(hash);
264 [ - + ]: 10494 : assert(predicate);
265 : :
266 : : // Calculate an ideal initial position
267 : 10494 : ZixHashInsertPlan pos = {code, fold_hash(code, hash->mask)};
268 : :
269 : : // Search for a free position starting at the ideal one
270 : 10494 : const size_t start_index = pos.index;
271 : 10494 : size_t first_tombstone = 0;
272 : 10494 : bool found_tombstone = false;
273 [ + + ]: 22062 : while (!is_empty(&hash->entries[pos.index])) {
274 [ + + ]: 15094 : if (is_match(hash, code, pos.index, predicate, user_data)) {
275 : 3524 : return pos;
276 : : }
277 : :
278 [ + + + + ]: 11570 : if (!found_tombstone && !hash->entries[pos.index].value) {
279 [ - + ]: 10 : assert(hash->entries[pos.index].hash == tombstone);
280 : 10 : first_tombstone = pos.index; // Remember the first/best free index
281 : 10 : found_tombstone = true;
282 : : }
283 : :
284 : 11570 : pos.index = next_index(hash, pos.index);
285 [ + + ]: 11570 : if (pos.index == start_index) {
286 : 2 : break; // Rare edge case: entire table is full of entries/tombstones
287 : : }
288 : : }
289 : :
290 : : // If we found a tombstone before an empty slot, place the new element there
291 [ + + ]: 6970 : if (found_tombstone) {
292 : 10 : pos.index = first_tombstone;
293 : : }
294 : :
295 [ - + ]: 6970 : assert(!hash->entries[pos.index].value);
296 : 6970 : return pos;
297 : : }
298 : :
299 : : ZixHashInsertPlan
300 : 10494 : zix_hash_plan_insert(const ZixHash* const hash, const ZixHashKey* const key)
301 : : {
302 [ - + ]: 10494 : assert(hash);
303 [ - + ]: 10494 : assert(key);
304 : :
305 : 10494 : return zix_hash_plan_insert_prehashed(
306 : 10494 : hash, hash->hash_func(key), hash->equal_func, key);
307 : : }
308 : :
309 : : ZIX_REALTIME ZixHashRecord*
310 : 3409 : zix_hash_record_at(const ZixHash* const hash, const ZixHashInsertPlan position)
311 : : {
312 [ - + ]: 3409 : assert(hash);
313 : 3409 : return hash->entries[position.index].value;
314 : : }
315 : :
316 : : ZixStatus
317 : 10494 : zix_hash_insert_at(ZixHash* const hash,
318 : : const ZixHashInsertPlan position,
319 : : ZixHashRecord* const record)
320 : : {
321 [ - + ]: 10494 : assert(hash);
322 [ - + ]: 10494 : assert(record);
323 : :
324 [ + + ]: 10494 : if (hash->entries[position.index].value) {
325 : 3524 : return ZIX_STATUS_EXISTS;
326 : : }
327 : :
328 : : // Set entry to new value
329 : 6970 : ZixHashEntry* const entry = &hash->entries[position.index];
330 : 6970 : const ZixHashEntry orig_entry = *entry;
331 [ - + ]: 6970 : assert(!entry->value);
332 : 6970 : entry->hash = position.code;
333 : 6970 : entry->value = record;
334 : :
335 : : // Update size and rehash if we exceeded the maximum load
336 : 6970 : const size_t max_load = (hash->n_entries / 2U) + (hash->n_entries / 8U);
337 : 6970 : const size_t new_count = hash->count + 1U;
338 [ + + ]: 6970 : if (new_count >= max_load) {
339 : 505 : const ZixStatus st = grow(hash);
340 [ + + ]: 505 : if (st) {
341 : 18 : *entry = orig_entry;
342 : 18 : return st;
343 : : }
344 : : }
345 : :
346 : 6952 : hash->count = new_count;
347 : 6952 : return ZIX_STATUS_SUCCESS;
348 : : }
349 : :
350 : : ZixStatus
351 : 7085 : zix_hash_insert(ZixHash* const hash, ZixHashRecord* const record)
352 : : {
353 [ - + ]: 7085 : assert(hash);
354 [ - + ]: 7085 : assert(record);
355 : :
356 : 7085 : const ZixHashKey* const key = hash->key_func(record);
357 : 7085 : const ZixHashInsertPlan position = zix_hash_plan_insert(hash, key);
358 : :
359 : 7085 : return zix_hash_insert_at(hash, position, record);
360 : : }
361 : :
362 : : ZixStatus
363 : 3511 : zix_hash_erase(ZixHash* const hash,
364 : : const ZixHashIter i,
365 : : ZixHashRecord** const removed)
366 : : {
367 [ - + ]: 3511 : assert(hash);
368 [ - + ]: 3511 : assert(removed);
369 : :
370 : : // Replace entry with a tombstone
371 : 3511 : *removed = hash->entries[i].value;
372 : 3511 : hash->entries[i].hash = tombstone;
373 : 3511 : hash->entries[i].value = NULL;
374 : :
375 : : // Decrease element count and rehash if necessary
376 : 3511 : --hash->count;
377 [ + + ]: 3511 : if (hash->count < hash->n_entries / 4U) {
378 : 384 : return shrink(hash);
379 : : }
380 : :
381 : 3127 : return ZIX_STATUS_SUCCESS;
382 : : }
383 : :
384 : : ZixStatus
385 : 7009 : zix_hash_remove(ZixHash* const hash,
386 : : const ZixHashKey* const key,
387 : : ZixHashRecord** const removed)
388 : : {
389 [ - + ]: 7009 : assert(hash);
390 [ - + ]: 7009 : assert(key);
391 [ - + ]: 7009 : assert(removed);
392 : :
393 : 7009 : const ZixHashIter i = zix_hash_find(hash, key);
394 : :
395 : 7009 : return i == hash->n_entries ? ZIX_STATUS_NOT_FOUND
396 [ + + ]: 7009 : : zix_hash_erase(hash, i, removed);
397 : : }
|