| 1 |
/* Hash table for sequential list, set, and map data type. |
| 2 |
Copyright (C) 2006, 2009-2026 Free Software Foundation, Inc. |
| 3 |
Written by Bruno Haible <[email protected]>, 2006. |
| 4 |
|
| 5 |
This file is free software: you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU Lesser General Public License as |
| 7 |
published by the Free Software Foundation; either version 2.1 of the |
| 8 |
License, or (at your option) any later version. |
| 9 |
|
| 10 |
This file is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU Lesser General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Lesser General Public License |
| 16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 17 |
|
| 18 |
/* Common code of |
| 19 |
gl_linkedhash_list.c, gl_avltreehash_list.c, gl_rbtreehash_list.c, |
| 20 |
gl_linkedhash_set.c, gl_hash_set.c, |
| 21 |
gl_linkedhash_map.c, gl_hash_map.c. */ |
| 22 |
|
| 23 |
#include "gl_anyhash_primes.h" |
| 24 |
|
| 25 |
/* Resizes the hash table with a new estimated size. */ |
| 26 |
static void |
| 27 |
hash_resize (CONTAINER_T container, size_t estimate) |
| 28 |
{ |
| 29 |
size_t new_size = next_prime (estimate); |
| 30 |
|
| 31 |
if (new_size > container->table_size) |
| 32 |
{ |
| 33 |
gl_hash_entry_t *old_table = container->table; |
| 34 |
|
| 35 |
/* Allocate the new table. */ |
| 36 |
if (size_overflow_p (xtimes (new_size, sizeof (gl_hash_entry_t)))) |
| 37 |
goto fail; |
| 38 |
gl_hash_entry_t *new_table = |
| 39 |
(gl_hash_entry_t *) calloc (new_size, sizeof (gl_hash_entry_t)); |
| 40 |
if (new_table == NULL) |
| 41 |
goto fail; |
| 42 |
|
| 43 |
/* Iterate through the entries of the old table. */ |
| 44 |
for (size_t i = container->table_size; i > 0; ) |
| 45 |
{ |
| 46 |
gl_hash_entry_t node = old_table[--i]; |
| 47 |
|
| 48 |
while (node != NULL) |
| 49 |
{ |
| 50 |
gl_hash_entry_t next = node->hash_next; |
| 51 |
/* Add the entry to the new table. */ |
| 52 |
size_t bucket = node->hashcode % new_size; |
| 53 |
node->hash_next = new_table[bucket]; |
| 54 |
new_table[bucket] = node; |
| 55 |
|
| 56 |
node = next; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
container->table = new_table; |
| 61 |
container->table_size = new_size; |
| 62 |
free (old_table); |
| 63 |
} |
| 64 |
return; |
| 65 |
|
| 66 |
fail: |
| 67 |
/* Just continue without resizing the table. */ |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
/* Resizes the hash table if needed, after CONTAINER_COUNT (container) was |
| 72 |
incremented. */ |
| 73 |
static void |
| 74 |
hash_resize_after_add (CONTAINER_T container) |
| 75 |
{ |
| 76 |
size_t count = CONTAINER_COUNT (container); |
| 77 |
size_t estimate = xsum (count, count / 2); /* 1.5 * count */ |
| 78 |
if (estimate > container->table_size) |
| 79 |
hash_resize (container, estimate); |
| 80 |
} |