Changeset 1b20da0 in mainline for kernel/generic/src/adt/hash_table.c
- Timestamp:
- 2018-02-28T17:52:03Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 3061bc1
- Parents:
- df6ded8
- git-author:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-02-28 17:26:03)
- git-committer:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-02-28 17:52:03)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/src/adt/hash_table.c
rdf6ded8 r1b20da0 2 2 * Copyright (c) 2008 Jakub Jermar 3 3 * Copyright (c) 2012 Adam Hraska 4 * 4 * 5 5 * All rights reserved. 6 6 * … … 37 37 /* 38 38 * This is an implementation of a generic resizable chained hash table. 39 * 40 * The table grows to 2*n+1 buckets each time, starting at n == 89, 39 * 40 * The table grows to 2*n+1 buckets each time, starting at n == 89, 41 41 * per Thomas Wang's recommendation: 42 42 * http://www.concentric.net/~Ttwang/tech/hashsize.htm 43 * 43 * 44 44 * This policy produces prime table sizes for the first five resizes 45 * and generally produces table sizes which are either prime or 45 * and generally produces table sizes which are either prime or 46 46 * have fairly large (prime/odd) divisors. Having a prime table size 47 47 * mitigates the use of suboptimal hash functions and distributes … … 79 79 * @param h Hash table structure. Will be initialized by this call. 80 80 * @param init_size Initial desired number of hash table buckets. Pass zero 81 * if you want the default initial size. 81 * if you want the default initial size. 82 82 * @param max_load The table is resized when the average load per bucket 83 83 * exceeds this number. Pass zero if you want the default. … … 86 86 * upon removal. equal() is optional if and only if 87 87 * hash_table_insert_unique() will never be invoked. 88 * All other operations are mandatory. 88 * All other operations are mandatory. 89 89 * 90 90 * @return True on success … … 210 210 * @param h Hash table. 211 211 * @param item Item to be inserted into the hash table. 212 * 213 * @return False if such an item had already been inserted. 212 * 213 * @return False if such an item had already been inserted. 214 214 * @return True if the inserted item was the only item with such a lookup key. 215 215 */ … … 225 225 /* Check for duplicates. */ 226 226 list_foreach(h->bucket[idx], link, ht_link_t, cur_link) { 227 /* 228 * We could filter out items using their hashes first, but 227 /* 228 * We could filter out items using their hashes first, but 229 229 * calling equal() might very well be just as fast. 230 230 */ … … 255 255 256 256 list_foreach(h->bucket[idx], link, ht_link_t, cur_link) { 257 /* 258 * Is this is the item we are looking for? We could have first 259 * checked if the hashes match but op->key_equal() may very well be 257 /* 258 * Is this is the item we are looking for? We could have first 259 * checked if the hashes match but op->key_equal() may very well be 260 260 * just as fast as op->hash(). 261 261 */ … … 278 278 assert(cur); 279 279 ht_link_t *cur_link = member_to_inst(cur, ht_link_t, link); 280 /* 281 * Is this is the item we are looking for? We could have first 282 * checked if the hashes match but op->equal() may very well be 280 /* 281 * Is this is the item we are looking for? We could have first 282 * checked if the hashes match but op->equal() may very well be 283 283 * just as fast as op->hash(). 284 284 */ … … 298 298 * @param key Array of keys that will be compared against items of 299 299 * the hash table. 300 * 300 * 301 301 * @return Returns the number of removed items. 302 302 */ … … 342 342 * 343 343 * @param h Hash table. 344 * @param f Function to be applied. Return false if no more items 344 * @param f Function to be applied. Return false if no more items 345 345 * should be visited. The functor may only delete the supplied 346 * item. It must not delete the successor of the item passed 346 * item. It must not delete the successor of the item passed 347 347 * in the first argument. 348 348 * @param arg Argument to be passed to the function. 349 349 */ 350 350 void hash_table_apply(hash_table_t *h, bool (*f)(ht_link_t *, void *), void *arg) 351 { 351 { 352 352 assert(f); 353 353 assert(h && h->bucket); … … 361 361 list_foreach_safe(h->bucket[idx], cur, next) { 362 362 ht_link_t *cur_link = member_to_inst(cur, ht_link_t, link); 363 /* 364 * The next pointer had already been saved. f() may safely 363 /* 364 * The next pointer had already been saved. f() may safely 365 365 * delete cur (but not next!). 366 366 */ … … 409 409 { 410 410 if (h->item_cnt <= h->full_item_cnt / 4 && HT_MIN_BUCKETS < h->bucket_cnt) { 411 /* 412 * Keep the bucket_cnt odd (possibly also prime). 411 /* 412 * Keep the bucket_cnt odd (possibly also prime). 413 413 * Shrink from 2n + 1 to n. Integer division discards the +1. 414 414 */ … … 430 430 431 431 /** Allocates and rehashes items to a new table. Frees the old table. */ 432 static void resize(hash_table_t *h, size_t new_bucket_cnt) 432 static void resize(hash_table_t *h, size_t new_bucket_cnt) 433 433 { 434 434 assert(h && h->bucket);
Note:
See TracChangeset
for help on using the changeset viewer.