Line data Source code
1 : // SPDX-License-Identifier: BSD-2-Clause
2 : // Copyright (C) 2010 Andrea Mazzoleni
3 :
4 : #include "tommyhashtbl.h"
5 : #include "tommylist.h"
6 :
7 : #include <assert.h> /* for assert */
8 : #include <string.h> /* for memset */
9 :
10 : /******************************************************************************/
11 : /* hashtable */
12 :
13 4902 : TOMMY_API void tommy_hashtable_init(tommy_hashtable* hashtable, tommy_size_t bucket_max)
14 : {
15 4902 : if (bucket_max < 16)
16 65 : bucket_max = 16;
17 : else
18 4837 : bucket_max = tommy_roundup_pow2(bucket_max);
19 :
20 : /* ensure bucket rounding and allocation size cannot overflow */
21 4902 : assert(bucket_max != 0 && bucket_max <= TOMMY_SIZE_MAX / sizeof(tommy_hashtable_node*));
22 :
23 4902 : hashtable->bucket_max = bucket_max;
24 4902 : hashtable->bucket_mask = hashtable->bucket_max - 1;
25 :
26 : /* let calloc defer memory initialization when supported by the allocator */
27 4902 : hashtable->bucket = tommy_cast(tommy_hashtable_node**, tommy_calloc(hashtable->bucket_max, sizeof(tommy_hashtable_node*)));
28 :
29 4902 : hashtable->count = 0;
30 4902 : }
31 :
32 4902 : TOMMY_API void tommy_hashtable_done(tommy_hashtable* hashtable)
33 : {
34 4902 : tommy_free(hashtable->bucket);
35 4902 : }
36 :
37 15 : TOMMY_API void tommy_hashtable_clear(tommy_hashtable* hashtable)
38 : {
39 15 : memset(hashtable->bucket, 0, hashtable->bucket_max * sizeof(tommy_hashtable_node*));
40 15 : hashtable->count = 0;
41 15 : }
42 :
43 10064990 : TOMMY_API void tommy_hashtable_insert(tommy_hashtable* hashtable, tommy_hashtable_node* node, void* data, tommy_hash_t hash)
44 : {
45 10064990 : tommy_size_t pos = hash & hashtable->bucket_mask;
46 :
47 10064990 : tommy_list_insert_tail(&hashtable->bucket[pos], node, data);
48 :
49 10064990 : node->index = hash;
50 :
51 10064990 : ++hashtable->count;
52 10064990 : }
53 :
54 774 : TOMMY_API void* tommy_hashtable_insert_unique(tommy_hashtable* hashtable, tommy_hashtable_node* node, void* data, tommy_search_func* cmp, const void* cmp_arg, tommy_hash_t hash)
55 : {
56 774 : tommy_hashtable_node** bucket = &hashtable->bucket[hash & hashtable->bucket_mask];
57 774 : tommy_hashtable_node* i = *bucket;
58 :
59 6999 : while (i) {
60 : /* compare keys only for matching full hashes, as the bucket may contain different hashes */
61 6739 : if (i->index == hash && cmp(cmp_arg, i->data) == 0)
62 514 : return i->data;
63 6225 : i = i->next;
64 : }
65 :
66 260 : tommy_list_insert_tail(bucket, node, data);
67 :
68 260 : node->index = hash;
69 :
70 260 : ++hashtable->count;
71 :
72 260 : return data;
73 : }
74 :
75 100 : TOMMY_API void tommy_hashtable_rehash_existing(tommy_hashtable* hashtable, tommy_hashtable_node* node, tommy_hash_t hash)
76 : {
77 100 : if (node->index == hash)
78 10 : return;
79 :
80 : /* unlink using the stored hash, without invoking the resize policy. */
81 90 : tommy_list_remove_existing(&hashtable->bucket[node->index & hashtable->bucket_mask], node);
82 90 : node->index = hash;
83 90 : tommy_list_insert_tail(&hashtable->bucket[hash & hashtable->bucket_mask], node, node->data);
84 : }
85 :
86 7732657 : TOMMY_API void* tommy_hashtable_remove_existing(tommy_hashtable* hashtable, tommy_hashtable_node* node)
87 : {
88 7732657 : tommy_size_t pos = node->index & hashtable->bucket_mask;
89 :
90 7732657 : tommy_list_remove_existing(&hashtable->bucket[pos], node);
91 :
92 7732657 : --hashtable->count;
93 :
94 7732657 : return node->data;
95 : }
96 :
97 4347611 : TOMMY_API void* tommy_hashtable_remove(tommy_hashtable* hashtable, tommy_search_func* cmp, const void* cmp_arg, tommy_hash_t hash)
98 : {
99 4347611 : tommy_size_t pos = hash & hashtable->bucket_mask;
100 4347611 : tommy_hashtable_node* node = hashtable->bucket[pos];
101 :
102 5625638 : while (node) {
103 : /* we first check if the hash matches, as in the same bucket we may have multiples hash values */
104 3605370 : if (node->index == hash && cmp(cmp_arg, node->data) == 0) {
105 2327343 : tommy_list_remove_existing(&hashtable->bucket[pos], node);
106 :
107 2327343 : --hashtable->count;
108 :
109 2327343 : return node->data;
110 : }
111 1278027 : node = node->next;
112 : }
113 :
114 2020268 : return 0;
115 : }
116 :
117 4762 : TOMMY_API void tommy_hashtable_foreach(tommy_hashtable* hashtable, tommy_foreach_func* func)
118 : {
119 4762 : tommy_size_t bucket_max = hashtable->bucket_max;
120 4762 : tommy_hashtable_node** bucket = hashtable->bucket;
121 :
122 5056634 : for (tommy_size_t pos = 0; pos < bucket_max; ++pos) {
123 5051872 : tommy_hashtable_node* node = bucket[pos];
124 :
125 9094230 : while (node) {
126 4042358 : void* data = node->data;
127 4042358 : node = node->next;
128 4042358 : func(data);
129 : }
130 : }
131 4762 : }
132 :
133 70 : TOMMY_API void tommy_hashtable_foreach_arg(tommy_hashtable* hashtable, tommy_foreach_arg_func* func, void* arg)
134 : {
135 70 : tommy_size_t bucket_max = hashtable->bucket_max;
136 70 : tommy_hashtable_node** bucket = hashtable->bucket;
137 :
138 198326 : for (tommy_size_t pos = 0; pos < bucket_max; ++pos) {
139 198256 : tommy_hashtable_node* node = bucket[pos];
140 :
141 498769 : while (node) {
142 300513 : void* data = node->data;
143 300513 : node = node->next;
144 300513 : func(arg, data);
145 : }
146 : }
147 70 : }
148 :
149 4766 : TOMMY_API tommy_size_t tommy_hashtable_memory_usage(const tommy_hashtable* hashtable)
150 : {
151 4766 : return hashtable->bucket_max * (tommy_size_t)sizeof(hashtable->bucket[0])
152 4766 : + tommy_hashtable_count(hashtable) * (tommy_size_t)sizeof(tommy_hashtable_node);
153 : }
154 :
155 16 : TOMMY_API void tommy_hashtable_to_list(tommy_hashtable* hashtable, tommy_list* list)
156 : {
157 : tommy_builder builder;
158 16 : tommy_node* builder_tail = tommy_builder_init(&builder);
159 :
160 : /* clear buckets as they are transferred to avoid a second scan */
161 1296 : for (tommy_size_t pos = 0; pos < hashtable->bucket_max; ++pos) {
162 1280 : tommy_node* head = hashtable->bucket[pos];
163 1280 : if (head) {
164 133 : tommy_node* tail = head->prev;
165 133 : builder_tail = tommy_builder_concat(builder_tail, head, tail);
166 : }
167 1280 : hashtable->bucket[pos] = 0;
168 : }
169 :
170 16 : tommy_list_concat_builder(list, &builder, builder_tail);
171 16 : hashtable->count = 0;
172 16 : }
173 :
174 : /**
175 : * Finds the first or last node starting at an active bucket.
176 : *
177 : * Scans buckets in sequential order until a non-empty bucket is found,
178 : * then returns its head (for forward) or tail (for reverse).
179 : */
180 1983 : tommy_inline tommy_hashtable_node* tommy_imp_hashtable_limit(tommy_hashtable* hashtable, tommy_size_t pos, tommy_bool_t reverse)
181 : {
182 1983 : if (reverse) {
183 7970 : while (pos < hashtable->bucket_max) {
184 7970 : if (hashtable->bucket[pos])
185 1120 : return hashtable->bucket[pos]->prev;
186 6850 : if (!pos)
187 57 : break;
188 6793 : --pos;
189 : }
190 : } else {
191 5785 : while (pos < hashtable->bucket_max) {
192 5696 : if (hashtable->bucket[pos])
193 717 : return hashtable->bucket[pos];
194 4979 : ++pos;
195 : }
196 : }
197 146 : return 0;
198 : }
199 :
200 89 : TOMMY_API tommy_hashtable_node* tommy_hashtable_head(tommy_hashtable* hashtable)
201 : {
202 : /* start forward traversal at the first bucket */
203 89 : return tommy_imp_hashtable_limit(hashtable, 0, 0);
204 : }
205 :
206 89 : TOMMY_API tommy_hashtable_node* tommy_hashtable_tail(tommy_hashtable* hashtable)
207 : {
208 : /* start reverse traversal at the last bucket */
209 89 : return tommy_imp_hashtable_limit(hashtable, hashtable->bucket_max - 1, 1);
210 : }
211 :
212 13176 : TOMMY_API tommy_hashtable_node* tommy_hashtable_next(tommy_hashtable* hashtable, tommy_hashtable_node* node)
213 : {
214 13176 : if (node->next)
215 12459 : return node->next;
216 :
217 : /* bucket exhausted: advance to the next non-empty bucket */
218 717 : tommy_size_t pos = node->index & hashtable->bucket_mask;
219 717 : return tommy_imp_hashtable_limit(hashtable, pos + 1, 0);
220 : }
221 :
222 19952 : TOMMY_API tommy_hashtable_node* tommy_hashtable_prev(tommy_hashtable* hashtable, tommy_hashtable_node* node)
223 : {
224 19952 : tommy_size_t pos = node->index & hashtable->bucket_mask;
225 :
226 : /* if not at the head of the current bucket, use the previous node */
227 19952 : if (node != hashtable->bucket[pos])
228 18784 : return node->prev;
229 :
230 1168 : if (!pos)
231 80 : return 0;
232 :
233 : /* bucket exhausted: advance to the preceding non-empty bucket */
234 1088 : return tommy_imp_hashtable_limit(hashtable, pos - 1, 1);
235 : }
236 :
|