source: mainline/kernel/generic/src/adt/hash_table.c@ cd1e3fc0

Last change on this file since cd1e3fc0 was 9bfa8c8, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Transform a few more headers that only differ in extra empty line

  • Property mode set to 100644
File size: 11.1 KB
Line 
1/*
2 * SPDX-FileCopyrightText: 2008 Jakub Jermar
3 * SPDX-FileCopyrightText: 2012 Adam Hraska
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8/** @addtogroup kernel_generic
9 * @{
10 */
11/** @file
12 */
13
14/*
15 * This is an implementation of a generic resizable chained hash table.
16 *
17 * The table grows to 2*n+1 buckets each time, starting at n == 89,
18 * per Thomas Wang's recommendation:
19 * http://www.concentric.net/~Ttwang/tech/hashsize.htm
20 *
21 * This policy produces prime table sizes for the first five resizes
22 * and generally produces table sizes which are either prime or
23 * have fairly large (prime/odd) divisors. Having a prime table size
24 * mitigates the use of suboptimal hash functions and distributes
25 * items over the whole table.
26 */
27
28#include <adt/hash_table.h>
29#include <adt/list.h>
30#include <assert.h>
31#include <stdlib.h>
32#include <str.h>
33
34/* Optimal initial bucket count. See comment above. */
35#define HT_MIN_BUCKETS 89
36/* The table is resized when the average load per bucket exceeds this number. */
37#define HT_MAX_LOAD 2
38
39static size_t round_up_size(size_t);
40static bool alloc_table(size_t, list_t **);
41static void clear_items(hash_table_t *);
42static void resize(hash_table_t *, size_t);
43static void grow_if_needed(hash_table_t *);
44static void shrink_if_needed(hash_table_t *);
45
46/* Dummy do nothing callback to invoke in place of remove_callback == NULL. */
47static void nop_remove_callback(ht_link_t *item)
48{
49 /* no-op */
50}
51
52/** Create chained hash table.
53 *
54 * @param h Hash table structure. Will be initialized by this call.
55 * @param init_size Initial desired number of hash table buckets. Pass zero
56 * if you want the default initial size.
57 * @param max_load The table is resized when the average load per bucket
58 * exceeds this number. Pass zero if you want the default.
59 * @param op Hash table operations structure. remove_callback()
60 * is optional and can be NULL if no action is to be taken
61 * upon removal. equal() is optional if and only if
62 * hash_table_insert_unique() will never be invoked.
63 * All other operations are mandatory.
64 *
65 * @return True on success
66 *
67 */
68bool hash_table_create(hash_table_t *h, size_t init_size, size_t max_load,
69 hash_table_ops_t *op)
70{
71 assert(h);
72 assert(op && op->hash && op->key_hash && op->key_equal);
73
74 /* Check for compulsory ops. */
75 if (!op || !op->hash || !op->key_hash || !op->key_equal)
76 return false;
77
78 h->bucket_cnt = round_up_size(init_size);
79
80 if (!alloc_table(h->bucket_cnt, &h->bucket))
81 return false;
82
83 h->max_load = (max_load == 0) ? HT_MAX_LOAD : max_load;
84 h->item_cnt = 0;
85 h->op = op;
86 h->full_item_cnt = h->max_load * h->bucket_cnt;
87 h->apply_ongoing = false;
88
89 if (h->op->remove_callback == NULL) {
90 h->op->remove_callback = nop_remove_callback;
91 }
92
93 return true;
94}
95
96/** Destroy a hash table instance.
97 *
98 * @param h Hash table to be destroyed.
99 *
100 */
101void hash_table_destroy(hash_table_t *h)
102{
103 assert(h && h->bucket);
104 assert(!h->apply_ongoing);
105
106 clear_items(h);
107
108 free(h->bucket);
109
110 h->bucket = NULL;
111 h->bucket_cnt = 0;
112}
113
114/** Returns true if there are no items in the table. */
115bool hash_table_empty(hash_table_t *h)
116{
117 assert(h && h->bucket);
118 return h->item_cnt == 0;
119}
120
121/** Returns the number of items in the table. */
122size_t hash_table_size(hash_table_t *h)
123{
124 assert(h && h->bucket);
125 return h->item_cnt;
126}
127
128/** Remove all elements from the hash table
129 *
130 * @param h Hash table to be cleared
131 */
132void hash_table_clear(hash_table_t *h)
133{
134 assert(h && h->bucket);
135 assert(!h->apply_ongoing);
136
137 clear_items(h);
138
139 /* Shrink the table to its minimum size if possible. */
140 if (HT_MIN_BUCKETS < h->bucket_cnt) {
141 resize(h, HT_MIN_BUCKETS);
142 }
143}
144
145/** Unlinks and removes all items but does not resize. */
146static void clear_items(hash_table_t *h)
147{
148 if (h->item_cnt == 0)
149 return;
150
151 for (size_t idx = 0; idx < h->bucket_cnt; ++idx) {
152 list_foreach_safe(h->bucket[idx], cur, next) {
153 assert(cur);
154 ht_link_t *cur_link = member_to_inst(cur, ht_link_t, link);
155
156 list_remove(cur);
157 h->op->remove_callback(cur_link);
158 }
159 }
160
161 h->item_cnt = 0;
162}
163
164/** Insert item into a hash table.
165 *
166 * @param h Hash table.
167 * @param item Item to be inserted into the hash table.
168 */
169void hash_table_insert(hash_table_t *h, ht_link_t *item)
170{
171 assert(item);
172 assert(h && h->bucket);
173 assert(!h->apply_ongoing);
174
175 size_t idx = h->op->hash(item) % h->bucket_cnt;
176
177 list_append(&item->link, &h->bucket[idx]);
178 ++h->item_cnt;
179 grow_if_needed(h);
180}
181
182/** Insert item into a hash table if not already present.
183 *
184 * @param h Hash table.
185 * @param item Item to be inserted into the hash table.
186 *
187 * @return False if such an item had already been inserted.
188 * @return True if the inserted item was the only item with such a lookup key.
189 */
190bool hash_table_insert_unique(hash_table_t *h, ht_link_t *item)
191{
192 assert(item);
193 assert(h && h->bucket && h->bucket_cnt);
194 assert(h->op && h->op->hash && h->op->equal);
195 assert(!h->apply_ongoing);
196
197 size_t idx = h->op->hash(item) % h->bucket_cnt;
198
199 /* Check for duplicates. */
200 list_foreach(h->bucket[idx], link, ht_link_t, cur_link) {
201 /*
202 * We could filter out items using their hashes first, but
203 * calling equal() might very well be just as fast.
204 */
205 if (h->op->equal(cur_link, item))
206 return false;
207 }
208
209 list_append(&item->link, &h->bucket[idx]);
210 ++h->item_cnt;
211 grow_if_needed(h);
212
213 return true;
214}
215
216/** Search hash table for an item matching keys.
217 *
218 * @param h Hash table.
219 * @param key Array of all keys needed to compute hash index.
220 *
221 * @return Matching item on success, NULL if there is no such item.
222 *
223 */
224ht_link_t *hash_table_find(const hash_table_t *h, const void *key)
225{
226 assert(h && h->bucket);
227
228 size_t idx = h->op->key_hash(key) % h->bucket_cnt;
229
230 list_foreach(h->bucket[idx], link, ht_link_t, cur_link) {
231 /*
232 * Is this is the item we are looking for? We could have first
233 * checked if the hashes match but op->key_equal() may very well be
234 * just as fast as op->hash().
235 */
236 if (h->op->key_equal(key, cur_link)) {
237 return cur_link;
238 }
239 }
240
241 return NULL;
242}
243
244/** Find the next item equal to item. */
245ht_link_t *
246hash_table_find_next(const hash_table_t *h, ht_link_t *first, ht_link_t *item)
247{
248 assert(item);
249 assert(h && h->bucket);
250
251 size_t idx = h->op->hash(item) % h->bucket_cnt;
252
253 /* Traverse the circular list until we reach the starting item again. */
254 for (link_t *cur = item->link.next; cur != &first->link;
255 cur = cur->next) {
256 assert(cur);
257
258 if (cur == &h->bucket[idx].head)
259 continue;
260
261 ht_link_t *cur_link = member_to_inst(cur, ht_link_t, link);
262 /*
263 * Is this is the item we are looking for? We could have first
264 * checked if the hashes match but op->equal() may very well be
265 * just as fast as op->hash().
266 */
267 if (h->op->equal(cur_link, item)) {
268 return cur_link;
269 }
270 }
271
272 return NULL;
273}
274
275/** Remove all matching items from hash table.
276 *
277 * For each removed item, h->remove_callback() is called.
278 *
279 * @param h Hash table.
280 * @param key Array of keys that will be compared against items of
281 * the hash table.
282 *
283 * @return Returns the number of removed items.
284 */
285size_t hash_table_remove(hash_table_t *h, const void *key)
286{
287 assert(h && h->bucket);
288 assert(!h->apply_ongoing);
289
290 size_t idx = h->op->key_hash(key) % h->bucket_cnt;
291
292 size_t removed = 0;
293
294 list_foreach_safe(h->bucket[idx], cur, next) {
295 ht_link_t *cur_link = member_to_inst(cur, ht_link_t, link);
296
297 if (h->op->key_equal(key, cur_link)) {
298 ++removed;
299 list_remove(cur);
300 h->op->remove_callback(cur_link);
301 }
302 }
303
304 h->item_cnt -= removed;
305 shrink_if_needed(h);
306
307 return removed;
308}
309
310/** Removes an item already present in the table. The item must be in the table. */
311void hash_table_remove_item(hash_table_t *h, ht_link_t *item)
312{
313 assert(item);
314 assert(h && h->bucket);
315 assert(link_in_use(&item->link));
316
317 list_remove(&item->link);
318 --h->item_cnt;
319 h->op->remove_callback(item);
320 shrink_if_needed(h);
321}
322
323/** Apply function to all items in hash table.
324 *
325 * @param h Hash table.
326 * @param f Function to be applied. Return false if no more items
327 * should be visited. The functor may only delete the supplied
328 * item. It must not delete the successor of the item passed
329 * in the first argument.
330 * @param arg Argument to be passed to the function.
331 */
332void hash_table_apply(hash_table_t *h, bool (*f)(ht_link_t *, void *), void *arg)
333{
334 assert(f);
335 assert(h && h->bucket);
336
337 if (h->item_cnt == 0)
338 return;
339
340 h->apply_ongoing = true;
341
342 for (size_t idx = 0; idx < h->bucket_cnt; ++idx) {
343 list_foreach_safe(h->bucket[idx], cur, next) {
344 ht_link_t *cur_link = member_to_inst(cur, ht_link_t, link);
345 /*
346 * The next pointer had already been saved. f() may safely
347 * delete cur (but not next!).
348 */
349 if (!f(cur_link, arg))
350 goto out;
351 }
352 }
353out:
354 h->apply_ongoing = false;
355
356 shrink_if_needed(h);
357 grow_if_needed(h);
358}
359
360/** Rounds up size to the nearest suitable table size. */
361static size_t round_up_size(size_t size)
362{
363 size_t rounded_size = HT_MIN_BUCKETS;
364
365 while (rounded_size < size) {
366 rounded_size = 2 * rounded_size + 1;
367 }
368
369 return rounded_size;
370}
371
372/** Allocates and initializes the desired number of buckets. True if successful. */
373static bool alloc_table(size_t bucket_cnt, list_t **pbuckets)
374{
375 assert(pbuckets && HT_MIN_BUCKETS <= bucket_cnt);
376
377 list_t *buckets = malloc(bucket_cnt * sizeof(list_t));
378 if (!buckets)
379 return false;
380
381 for (size_t i = 0; i < bucket_cnt; i++)
382 list_initialize(&buckets[i]);
383
384 *pbuckets = buckets;
385 return true;
386}
387
388/** Shrinks the table if the table is only sparely populated. */
389static inline void shrink_if_needed(hash_table_t *h)
390{
391 if (h->item_cnt <= h->full_item_cnt / 4 && HT_MIN_BUCKETS < h->bucket_cnt) {
392 /*
393 * Keep the bucket_cnt odd (possibly also prime).
394 * Shrink from 2n + 1 to n. Integer division discards the +1.
395 */
396 size_t new_bucket_cnt = h->bucket_cnt / 2;
397 resize(h, new_bucket_cnt);
398 }
399}
400
401/** Grows the table if table load exceeds the maximum allowed. */
402static inline void grow_if_needed(hash_table_t *h)
403{
404 /* Grow the table if the average bucket load exceeds the maximum. */
405 if (h->full_item_cnt < h->item_cnt) {
406 /* Keep the bucket_cnt odd (possibly also prime). */
407 size_t new_bucket_cnt = 2 * h->bucket_cnt + 1;
408 resize(h, new_bucket_cnt);
409 }
410}
411
412/** Allocates and rehashes items to a new table. Frees the old table. */
413static void resize(hash_table_t *h, size_t new_bucket_cnt)
414{
415 assert(h && h->bucket);
416 assert(HT_MIN_BUCKETS <= new_bucket_cnt);
417
418 /* We are traversing the table and resizing would mess up the buckets. */
419 if (h->apply_ongoing)
420 return;
421
422 list_t *new_buckets;
423
424 /* Leave the table as is if we cannot resize. */
425 if (!alloc_table(new_bucket_cnt, &new_buckets))
426 return;
427
428 if (0 < h->item_cnt) {
429 /* Rehash all the items to the new table. */
430 for (size_t old_idx = 0; old_idx < h->bucket_cnt; ++old_idx) {
431 list_foreach_safe(h->bucket[old_idx], cur, next) {
432 ht_link_t *cur_link = member_to_inst(cur, ht_link_t, link);
433
434 size_t new_idx = h->op->hash(cur_link) % new_bucket_cnt;
435 list_remove(cur);
436 list_append(cur, &new_buckets[new_idx]);
437 }
438 }
439 }
440
441 free(h->bucket);
442 h->bucket = new_buckets;
443 h->bucket_cnt = new_bucket_cnt;
444 h->full_item_cnt = h->max_load * h->bucket_cnt;
445}
446
447/** @}
448 */
Note: See TracBrowser for help on using the repository browser.