This source file includes following definitions.
- hash_resize
- hash_resize_after_add
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 #include "gl_anyhash_primes.h"
24
25
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 gl_hash_entry_t *new_table;
36 size_t i;
37
38 if (size_overflow_p (xtimes (new_size, sizeof (gl_hash_entry_t))))
39 goto fail;
40 new_table =
41 (gl_hash_entry_t *) calloc (new_size, sizeof (gl_hash_entry_t));
42 if (new_table == NULL)
43 goto fail;
44
45
46 for (i = container->table_size; i > 0; )
47 {
48 gl_hash_entry_t node = old_table[--i];
49
50 while (node != NULL)
51 {
52 gl_hash_entry_t next = node->hash_next;
53
54 size_t bucket = node->hashcode % new_size;
55 node->hash_next = new_table[bucket];
56 new_table[bucket] = node;
57
58 node = next;
59 }
60 }
61
62 container->table = new_table;
63 container->table_size = new_size;
64 free (old_table);
65 }
66 return;
67
68 fail:
69
70 return;
71 }
72
73
74
75 static void
76 hash_resize_after_add (CONTAINER_T container)
77 {
78 size_t count = CONTAINER_COUNT (container);
79 size_t estimate = xsum (count, count / 2);
80 if (estimate > container->table_size)
81 hash_resize (container, estimate);
82 }