Changeset 1b20da0 in mainline for uspace/lib/c/generic/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
-
uspace/lib/c/generic/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 */ … … 299 299 * the hash table. 300 300 * @param keys Number of keys in the 'key' array. 301 * 301 * 302 302 * @return Returns the number of removed items. 303 303 */ … … 343 343 * 344 344 * @param h Hash table. 345 * @param f Function to be applied. Return false if no more items 345 * @param f Function to be applied. Return false if no more items 346 346 * should be visited. The functor may only delete the supplied 347 * item. It must not delete the successor of the item passed 347 * item. It must not delete the successor of the item passed 348 348 * in the first argument. 349 349 * @param arg Argument to be passed to the function. 350 350 */ 351 351 void hash_table_apply(hash_table_t *h, bool (*f)(ht_link_t *, void *), void *arg) 352 { 352 { 353 353 assert(f); 354 354 assert(h && h->bucket); … … 362 362 list_foreach_safe(h->bucket[idx], cur, next) { 363 363 ht_link_t *cur_link = member_to_inst(cur, ht_link_t, link); 364 /* 365 * The next pointer had already been saved. f() may safely 364 /* 365 * The next pointer had already been saved. f() may safely 366 366 * delete cur (but not next!). 367 367 */ … … 410 410 { 411 411 if (h->item_cnt <= h->full_item_cnt / 4 && HT_MIN_BUCKETS < h->bucket_cnt) { 412 /* 413 * Keep the bucket_cnt odd (possibly also prime). 412 /* 413 * Keep the bucket_cnt odd (possibly also prime). 414 414 * Shrink from 2n + 1 to n. Integer division discards the +1. 415 415 */ … … 431 431 432 432 /** Allocates and rehashes items to a new table. Frees the old table. */ 433 static void resize(hash_table_t *h, size_t new_bucket_cnt) 433 static void resize(hash_table_t *h, size_t new_bucket_cnt) 434 434 { 435 435 assert(h && h->bucket);
Note:
See TracChangeset
for help on using the changeset viewer.