[ee7736e] | 1 | /*
|
---|
[739d00a] | 2 | * Copyright (c) 2008 Jakub Jermar
|
---|
[062d900] | 3 | * Copyright (c) 2012 Adam Hraska
|
---|
| 4 | *
|
---|
[ee7736e] | 5 | * All rights reserved.
|
---|
| 6 | *
|
---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
---|
| 8 | * modification, are permitted provided that the following conditions
|
---|
| 9 | * are met:
|
---|
| 10 | *
|
---|
| 11 | * - Redistributions of source code must retain the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
---|
| 13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 14 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 15 | * documentation and/or other materials provided with the distribution.
|
---|
| 16 | * - The name of the author may not be used to endorse or promote products
|
---|
| 17 | * derived from this software without specific prior written permission.
|
---|
| 18 | *
|
---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 29 | */
|
---|
| 30 |
|
---|
[fadd381] | 31 | /** @addtogroup libc
|
---|
[b2951e2] | 32 | * @{
|
---|
| 33 | */
|
---|
| 34 | /** @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[ee7736e] | 37 | /*
|
---|
[062d900] | 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,
|
---|
| 41 | * per Thomas Wang's recommendation:
|
---|
| 42 | * http://www.concentric.net/~Ttwang/tech/hashsize.htm
|
---|
| 43 | *
|
---|
| 44 | * This policy produces prime table sizes for the first five resizes
|
---|
| 45 | * and generally produces table sizes which are either prime or
|
---|
| 46 | * have fairly large (prime/odd) divisors. Having a prime table size
|
---|
| 47 | * mitigates the use of suboptimal hash functions and distributes
|
---|
| 48 | * items over the whole table.
|
---|
[ee7736e] | 49 | */
|
---|
| 50 |
|
---|
[d9c8c81] | 51 | #include <adt/hash_table.h>
|
---|
| 52 | #include <adt/list.h>
|
---|
[ee7736e] | 53 | #include <unistd.h>
|
---|
| 54 | #include <malloc.h>
|
---|
| 55 | #include <assert.h>
|
---|
[19f857a] | 56 | #include <str.h>
|
---|
[ee7736e] | 57 |
|
---|
[062d900] | 58 | /* Optimal initial bucket count. See comment above. */
|
---|
| 59 | #define HT_MIN_BUCKETS 89
|
---|
| 60 | /* The table is resized when the average load per bucket exceeds this number. */
|
---|
| 61 | #define HT_MAX_LOAD 2
|
---|
| 62 |
|
---|
| 63 |
|
---|
[97b199b1] | 64 | static size_t round_up_size(size_t);
|
---|
| 65 | static bool alloc_table(size_t, list_t **);
|
---|
| 66 | static void clear_items(hash_table_t *);
|
---|
| 67 | static void resize(hash_table_t *, size_t);
|
---|
| 68 | static void grow_if_needed(hash_table_t *);
|
---|
| 69 | static void shrink_if_needed(hash_table_t *);
|
---|
[062d900] | 70 |
|
---|
| 71 | /* Dummy do nothing callback to invoke in place of remove_callback == NULL. */
|
---|
| 72 | static void nop_remove_callback(ht_link_t *item)
|
---|
| 73 | {
|
---|
| 74 | /* no-op */
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 |
|
---|
[ee7736e] | 78 | /** Create chained hash table.
|
---|
| 79 | *
|
---|
[e1da7ec] | 80 | * @param h Hash table structure. Will be initialized by this call.
|
---|
[062d900] | 81 | * @param init_size Initial desired number of hash table buckets. Pass zero
|
---|
| 82 | * if you want the default initial size.
|
---|
[220210c8] | 83 | * @param max_load The table is resized when the average load per bucket
|
---|
| 84 | * exceeds this number. Pass zero if you want the default.
|
---|
[062d900] | 85 | * @param op Hash table operations structure. remove_callback()
|
---|
| 86 | * is optional and can be NULL if no action is to be taken
|
---|
| 87 | * upon removal. equal() is optional if and only if
|
---|
| 88 | * hash_table_insert_unique() will never be invoked.
|
---|
| 89 | * All other operations are mandatory.
|
---|
[e1da7ec] | 90 | *
|
---|
| 91 | * @return True on success
|
---|
| 92 | *
|
---|
[ee7736e] | 93 | */
|
---|
[062d900] | 94 | bool hash_table_create(hash_table_t *h, size_t init_size, size_t max_load,
|
---|
| 95 | hash_table_ops_t *op)
|
---|
[ee7736e] | 96 | {
|
---|
[4f34b6a] | 97 | assert(h);
|
---|
[062d900] | 98 | assert(op && op->hash && op->key_hash && op->key_equal);
|
---|
[ee7736e] | 99 |
|
---|
[062d900] | 100 | /* Check for compulsory ops. */
|
---|
| 101 | if (!op || !op->hash || !op->key_hash || !op->key_equal)
|
---|
[4f34b6a] | 102 | return false;
|
---|
[e1da7ec] | 103 |
|
---|
[062d900] | 104 | h->bucket_cnt = round_up_size(init_size);
|
---|
[ee7736e] | 105 |
|
---|
[062d900] | 106 | if (!alloc_table(h->bucket_cnt, &h->bucket))
|
---|
| 107 | return false;
|
---|
[ee7736e] | 108 |
|
---|
[062d900] | 109 | h->max_load = (max_load == 0) ? HT_MAX_LOAD : max_load;
|
---|
| 110 | h->item_cnt = 0;
|
---|
[ee7736e] | 111 | h->op = op;
|
---|
[062d900] | 112 | h->full_item_cnt = h->max_load * h->bucket_cnt;
|
---|
| 113 | h->apply_ongoing = false;
|
---|
| 114 |
|
---|
[97b199b1] | 115 | if (h->op->remove_callback == NULL) {
|
---|
[062d900] | 116 | h->op->remove_callback = nop_remove_callback;
|
---|
| 117 | }
|
---|
[e1da7ec] | 118 |
|
---|
[4f34b6a] | 119 | return true;
|
---|
[ee7736e] | 120 | }
|
---|
| 121 |
|
---|
[062d900] | 122 | /** Destroy a hash table instance.
|
---|
| 123 | *
|
---|
| 124 | * @param h Hash table to be destroyed.
|
---|
| 125 | *
|
---|
| 126 | */
|
---|
| 127 | void hash_table_destroy(hash_table_t *h)
|
---|
| 128 | {
|
---|
| 129 | assert(h && h->bucket);
|
---|
| 130 | assert(!h->apply_ongoing);
|
---|
| 131 |
|
---|
| 132 | clear_items(h);
|
---|
| 133 |
|
---|
| 134 | free(h->bucket);
|
---|
| 135 |
|
---|
| 136 | h->bucket = 0;
|
---|
| 137 | h->bucket_cnt = 0;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /** Returns true if there are no items in the table. */
|
---|
| 141 | bool hash_table_empty(hash_table_t *h)
|
---|
| 142 | {
|
---|
| 143 | assert(h && h->bucket);
|
---|
| 144 | return h->item_cnt == 0;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | /** Returns the number of items in the table. */
|
---|
| 148 | size_t hash_table_size(hash_table_t *h)
|
---|
| 149 | {
|
---|
| 150 | assert(h && h->bucket);
|
---|
| 151 | return h->item_cnt;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[892022a1] | 154 | /** Remove all elements from the hash table
|
---|
| 155 | *
|
---|
| 156 | * @param h Hash table to be cleared
|
---|
| 157 | */
|
---|
| 158 | void hash_table_clear(hash_table_t *h)
|
---|
| 159 | {
|
---|
[062d900] | 160 | assert(h && h->bucket);
|
---|
| 161 | assert(!h->apply_ongoing);
|
---|
| 162 |
|
---|
| 163 | clear_items(h);
|
---|
| 164 |
|
---|
| 165 | /* Shrink the table to its minimum size if possible. */
|
---|
| 166 | if (HT_MIN_BUCKETS < h->bucket_cnt) {
|
---|
| 167 | resize(h, HT_MIN_BUCKETS);
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | /** Unlinks and removes all items but does not resize. */
|
---|
| 172 | static void clear_items(hash_table_t *h)
|
---|
| 173 | {
|
---|
| 174 | if (h->item_cnt == 0)
|
---|
| 175 | return;
|
---|
| 176 |
|
---|
| 177 | for (size_t idx = 0; idx < h->bucket_cnt; ++idx) {
|
---|
| 178 | list_foreach_safe(h->bucket[idx], cur, next) {
|
---|
| 179 | assert(cur);
|
---|
| 180 | ht_link_t *cur_link = member_to_inst(cur, ht_link_t, link);
|
---|
| 181 |
|
---|
[892022a1] | 182 | list_remove(cur);
|
---|
[062d900] | 183 | h->op->remove_callback(cur_link);
|
---|
[892022a1] | 184 | }
|
---|
| 185 | }
|
---|
[062d900] | 186 |
|
---|
| 187 | h->item_cnt = 0;
|
---|
[892022a1] | 188 | }
|
---|
| 189 |
|
---|
[062d900] | 190 | /** Insert item into a hash table.
|
---|
[e1da7ec] | 191 | *
|
---|
[062d900] | 192 | * @param h Hash table.
|
---|
| 193 | * @param key Array of all keys necessary to compute hash index.
|
---|
| 194 | * @param item Item to be inserted into the hash table.
|
---|
[739d00a] | 195 | */
|
---|
[062d900] | 196 | void hash_table_insert(hash_table_t *h, ht_link_t *item)
|
---|
[739d00a] | 197 | {
|
---|
[062d900] | 198 | assert(item);
|
---|
| 199 | assert(h && h->bucket);
|
---|
| 200 | assert(!h->apply_ongoing);
|
---|
[e1da7ec] | 201 |
|
---|
[062d900] | 202 | size_t idx = h->op->hash(item) % h->bucket_cnt;
|
---|
| 203 |
|
---|
| 204 | list_append(&item->link, &h->bucket[idx]);
|
---|
| 205 | ++h->item_cnt;
|
---|
| 206 | grow_if_needed(h);
|
---|
[739d00a] | 207 | }
|
---|
| 208 |
|
---|
[062d900] | 209 |
|
---|
| 210 | /** Insert item into a hash table if not already present.
|
---|
[739d00a] | 211 | *
|
---|
[e1da7ec] | 212 | * @param h Hash table.
|
---|
| 213 | * @param key Array of all keys necessary to compute hash index.
|
---|
| 214 | * @param item Item to be inserted into the hash table.
|
---|
[062d900] | 215 | *
|
---|
| 216 | * @return False if such an item had already been inserted.
|
---|
| 217 | * @return True if the inserted item was the only item with such a lookup key.
|
---|
[ee7736e] | 218 | */
|
---|
[062d900] | 219 | bool hash_table_insert_unique(hash_table_t *h, ht_link_t *item)
|
---|
[ee7736e] | 220 | {
|
---|
[4f34b6a] | 221 | assert(item);
|
---|
[062d900] | 222 | assert(h && h->bucket && h->bucket_cnt);
|
---|
| 223 | assert(h->op && h->op->hash && h->op->equal);
|
---|
| 224 | assert(!h->apply_ongoing);
|
---|
| 225 |
|
---|
| 226 | size_t idx = h->op->hash(item) % h->bucket_cnt;
|
---|
| 227 |
|
---|
| 228 | /* Check for duplicates. */
|
---|
| 229 | list_foreach(h->bucket[idx], cur) {
|
---|
| 230 | /*
|
---|
| 231 | * We could filter out items using their hashes first, but
|
---|
| 232 | * calling equal() might very well be just as fast.
|
---|
| 233 | */
|
---|
| 234 | ht_link_t *cur_link = member_to_inst(cur, ht_link_t, link);
|
---|
| 235 | if (h->op->equal(cur_link, item))
|
---|
| 236 | return false;
|
---|
| 237 | }
|
---|
[e1da7ec] | 238 |
|
---|
[062d900] | 239 | list_append(&item->link, &h->bucket[idx]);
|
---|
| 240 | ++h->item_cnt;
|
---|
| 241 | grow_if_needed(h);
|
---|
[ee7736e] | 242 |
|
---|
[062d900] | 243 | return true;
|
---|
[ee7736e] | 244 | }
|
---|
| 245 |
|
---|
| 246 | /** Search hash table for an item matching keys.
|
---|
| 247 | *
|
---|
[e1da7ec] | 248 | * @param h Hash table.
|
---|
| 249 | * @param key Array of all keys needed to compute hash index.
|
---|
| 250 | *
|
---|
| 251 | * @return Matching item on success, NULL if there is no such item.
|
---|
[ee7736e] | 252 | *
|
---|
| 253 | */
|
---|
[062d900] | 254 | ht_link_t *hash_table_find(const hash_table_t *h, void *key)
|
---|
[ee7736e] | 255 | {
|
---|
[062d900] | 256 | assert(h && h->bucket);
|
---|
[e1da7ec] | 257 |
|
---|
[062d900] | 258 | size_t idx = h->op->key_hash(key) % h->bucket_cnt;
|
---|
| 259 |
|
---|
| 260 | list_foreach(h->bucket[idx], cur) {
|
---|
| 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->key_equal() may very well be
|
---|
| 265 | * just as fast as op->hash().
|
---|
| 266 | */
|
---|
| 267 | if (h->op->key_equal(key, cur_link)) {
|
---|
| 268 | return cur_link;
|
---|
[ee7736e] | 269 | }
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | return NULL;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
[062d900] | 275 | /** Find the next item equal to item. */
|
---|
| 276 | ht_link_t *hash_table_find_next(const hash_table_t *h, ht_link_t *item)
|
---|
| 277 | {
|
---|
| 278 | assert(item);
|
---|
| 279 | assert(h && h->bucket);
|
---|
| 280 |
|
---|
| 281 | /* Traverse the circular list until we reach the starting item again. */
|
---|
| 282 | for (link_t *cur = item->link.next; cur != &item->link; cur = cur->next) {
|
---|
| 283 | assert(cur);
|
---|
| 284 | ht_link_t *cur_link = member_to_inst(cur, ht_link_t, link);
|
---|
| 285 | /*
|
---|
| 286 | * Is this is the item we are looking for? We could have first
|
---|
| 287 | * checked if the hashes match but op->equal() may very well be
|
---|
| 288 | * just as fast as op->hash().
|
---|
| 289 | */
|
---|
| 290 | if (h->op->equal(cur_link, item)) {
|
---|
| 291 | return cur_link;
|
---|
| 292 | }
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | return NULL;
|
---|
| 296 | }
|
---|
| 297 |
|
---|
[ee7736e] | 298 | /** Remove all matching items from hash table.
|
---|
| 299 | *
|
---|
| 300 | * For each removed item, h->remove_callback() is called.
|
---|
| 301 | *
|
---|
[e1da7ec] | 302 | * @param h Hash table.
|
---|
| 303 | * @param key Array of keys that will be compared against items of
|
---|
| 304 | * the hash table.
|
---|
| 305 | * @param keys Number of keys in the 'key' array.
|
---|
[062d900] | 306 | *
|
---|
| 307 | * @return Returns the number of removed items.
|
---|
[ee7736e] | 308 | */
|
---|
[062d900] | 309 | size_t hash_table_remove(hash_table_t *h, void *key)
|
---|
[ee7736e] | 310 | {
|
---|
[062d900] | 311 | assert(h && h->bucket);
|
---|
| 312 | assert(!h->apply_ongoing);
|
---|
[ee7736e] | 313 |
|
---|
[062d900] | 314 | size_t idx = h->op->key_hash(key) % h->bucket_cnt;
|
---|
| 315 |
|
---|
| 316 | size_t removed = 0;
|
---|
| 317 |
|
---|
| 318 | list_foreach_safe(h->bucket[idx], cur, next) {
|
---|
| 319 | ht_link_t *cur_link = member_to_inst(cur, ht_link_t, link);
|
---|
[e1da7ec] | 320 |
|
---|
[062d900] | 321 | if (h->op->key_equal(key, cur_link)) {
|
---|
| 322 | ++removed;
|
---|
[ee7736e] | 323 | list_remove(cur);
|
---|
[062d900] | 324 | h->op->remove_callback(cur_link);
|
---|
[ee7736e] | 325 | }
|
---|
| 326 | }
|
---|
[062d900] | 327 |
|
---|
| 328 | h->item_cnt -= removed;
|
---|
| 329 | shrink_if_needed(h);
|
---|
[ee7736e] | 330 |
|
---|
[062d900] | 331 | return removed;
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | /** Removes an item already present in the table. The item must be in the table.*/
|
---|
| 335 | void hash_table_remove_item(hash_table_t *h, ht_link_t *item)
|
---|
| 336 | {
|
---|
| 337 | assert(item);
|
---|
| 338 | assert(h && h->bucket);
|
---|
| 339 | assert(link_in_use(&item->link));
|
---|
| 340 |
|
---|
| 341 | list_remove(&item->link);
|
---|
| 342 | --h->item_cnt;
|
---|
| 343 | h->op->remove_callback(item);
|
---|
| 344 | shrink_if_needed(h);
|
---|
[ee7736e] | 345 | }
|
---|
[b2951e2] | 346 |
|
---|
[1ab4aca] | 347 | /** Apply function to all items in hash table.
|
---|
[203a090] | 348 | *
|
---|
[e1da7ec] | 349 | * @param h Hash table.
|
---|
[062d900] | 350 | * @param f Function to be applied. Return false if no more items
|
---|
| 351 | * should be visited. The functor may only delete the supplied
|
---|
| 352 | * item. It must not delete the successor of the item passed
|
---|
| 353 | * in the first argument.
|
---|
[e1da7ec] | 354 | * @param arg Argument to be passed to the function.
|
---|
[203a090] | 355 | */
|
---|
[062d900] | 356 | void hash_table_apply(hash_table_t *h, bool (*f)(ht_link_t *, void *), void *arg)
|
---|
[892022a1] | 357 | {
|
---|
[062d900] | 358 | assert(f);
|
---|
| 359 | assert(h && h->bucket);
|
---|
| 360 |
|
---|
| 361 | if (h->item_cnt == 0)
|
---|
| 362 | return;
|
---|
| 363 |
|
---|
| 364 | h->apply_ongoing = true;
|
---|
| 365 |
|
---|
| 366 | for (size_t idx = 0; idx < h->bucket_cnt; ++idx) {
|
---|
| 367 | list_foreach_safe(h->bucket[idx], cur, next) {
|
---|
| 368 | ht_link_t *cur_link = member_to_inst(cur, ht_link_t, link);
|
---|
| 369 | /*
|
---|
| 370 | * The next pointer had already been saved. f() may safely
|
---|
| 371 | * delete cur (but not next!).
|
---|
[892022a1] | 372 | */
|
---|
[062d900] | 373 | if (!f(cur_link, arg))
|
---|
| 374 | return;
|
---|
[203a090] | 375 | }
|
---|
| 376 | }
|
---|
[062d900] | 377 |
|
---|
| 378 | h->apply_ongoing = false;
|
---|
| 379 |
|
---|
| 380 | shrink_if_needed(h);
|
---|
| 381 | grow_if_needed(h);
|
---|
| 382 | }
|
---|
| 383 |
|
---|
| 384 | /** Rounds up size to the nearest suitable table size. */
|
---|
| 385 | static size_t round_up_size(size_t size)
|
---|
| 386 | {
|
---|
| 387 | size_t rounded_size = HT_MIN_BUCKETS;
|
---|
| 388 |
|
---|
| 389 | while (rounded_size < size) {
|
---|
| 390 | rounded_size = 2 * rounded_size + 1;
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 | return rounded_size;
|
---|
[203a090] | 394 | }
|
---|
| 395 |
|
---|
[062d900] | 396 | /** Allocates and initializes the desired number of buckets. True if successful.*/
|
---|
| 397 | static bool alloc_table(size_t bucket_cnt, list_t **pbuckets)
|
---|
| 398 | {
|
---|
| 399 | assert(pbuckets && HT_MIN_BUCKETS <= bucket_cnt);
|
---|
| 400 |
|
---|
| 401 | list_t *buckets = malloc(bucket_cnt * sizeof(list_t));
|
---|
| 402 | if (!buckets)
|
---|
| 403 | return false;
|
---|
| 404 |
|
---|
| 405 | for (size_t i = 0; i < bucket_cnt; i++)
|
---|
| 406 | list_initialize(&buckets[i]);
|
---|
| 407 |
|
---|
| 408 | *pbuckets = buckets;
|
---|
| 409 | return true;
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 |
|
---|
| 413 | /** Shrinks the table if the table is only sparely populated. */
|
---|
| 414 | static inline void shrink_if_needed(hash_table_t *h)
|
---|
| 415 | {
|
---|
| 416 | if (h->item_cnt <= h->full_item_cnt / 4 && HT_MIN_BUCKETS < h->bucket_cnt) {
|
---|
| 417 | /*
|
---|
| 418 | * Keep the bucket_cnt odd (possibly also prime).
|
---|
| 419 | * Shrink from 2n + 1 to n. Integer division discards the +1.
|
---|
| 420 | */
|
---|
| 421 | size_t new_bucket_cnt = h->bucket_cnt / 2;
|
---|
| 422 | resize(h, new_bucket_cnt);
|
---|
| 423 | }
|
---|
| 424 | }
|
---|
| 425 |
|
---|
| 426 | /** Grows the table if table load exceeds the maximum allowed. */
|
---|
| 427 | static inline void grow_if_needed(hash_table_t *h)
|
---|
| 428 | {
|
---|
| 429 | /* Grow the table if the average bucket load exceeds the maximum. */
|
---|
| 430 | if (h->full_item_cnt < h->item_cnt) {
|
---|
| 431 | /* Keep the bucket_cnt odd (possibly also prime). */
|
---|
| 432 | size_t new_bucket_cnt = 2 * h->bucket_cnt + 1;
|
---|
| 433 | resize(h, new_bucket_cnt);
|
---|
| 434 | }
|
---|
| 435 | }
|
---|
| 436 |
|
---|
| 437 | /** Allocates and rehashes items to a new table. Frees the old table. */
|
---|
| 438 | static void resize(hash_table_t *h, size_t new_bucket_cnt)
|
---|
| 439 | {
|
---|
| 440 | assert(h && h->bucket);
|
---|
| 441 | assert(HT_MIN_BUCKETS <= new_bucket_cnt);
|
---|
| 442 |
|
---|
| 443 | /* We are traversing the table and resizing would mess up the buckets. */
|
---|
| 444 | if (h->apply_ongoing)
|
---|
| 445 | return;
|
---|
| 446 |
|
---|
| 447 | list_t *new_buckets;
|
---|
| 448 |
|
---|
| 449 | /* Leave the table as is if we cannot resize. */
|
---|
| 450 | if (!alloc_table(new_bucket_cnt, &new_buckets))
|
---|
| 451 | return;
|
---|
| 452 |
|
---|
| 453 | if (0 < h->item_cnt) {
|
---|
| 454 | /* Rehash all the items to the new table. */
|
---|
| 455 | for (size_t old_idx = 0; old_idx < h->bucket_cnt; ++old_idx) {
|
---|
| 456 | list_foreach_safe(h->bucket[old_idx], cur, next) {
|
---|
| 457 | ht_link_t *cur_link = member_to_inst(cur, ht_link_t, link);
|
---|
| 458 |
|
---|
| 459 | size_t new_idx = h->op->hash(cur_link) % new_bucket_cnt;
|
---|
| 460 | list_remove(cur);
|
---|
| 461 | list_append(cur, &new_buckets[new_idx]);
|
---|
| 462 | }
|
---|
| 463 | }
|
---|
| 464 | }
|
---|
| 465 |
|
---|
| 466 | free(h->bucket);
|
---|
| 467 | h->bucket = new_buckets;
|
---|
| 468 | h->bucket_cnt = new_bucket_cnt;
|
---|
| 469 | h->full_item_cnt = h->max_load * h->bucket_cnt;
|
---|
| 470 | }
|
---|
| 471 |
|
---|
| 472 |
|
---|
[fadd381] | 473 | /** @}
|
---|
[b2951e2] | 474 | */
|
---|