Debian Codegraph @

libidu/idu-hash.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* hash.c -- hash table maintenance
2 Copyright (C) 1995-2020 Free Software Foundation, Inc.
3 Written by Greg McGary <[email protected]>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
8 any later version.
9
10 This program 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 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <xalloc.h>
23 #include <error.h>
24
25 #include "idu-hash.h"
26 #include "xnls.h"
27
28 static void hash_rehash (struct hash_table* ht);
29 static unsigned long round_up_2 (unsigned long rough);
30
31 /* Implement double hashing with open addressing. The table size is
32 always a power of two. The secondary (`increment') hash function
33 is forced to return an odd-value, in order to be relatively prime
34 to the table size. This guarantees that the increment can
35 potentially hit every slot in the table during collision
36 resolution. */
37
38 void *hash_deleted_item = &hash_deleted_item;
39
40 /* Force the table size to be a power of two, possibly rounding up the
41 given size. */
42
43 void
44 hash_init (struct hash_table* ht, unsigned long size,
45 hash_func_t hash_1, hash_func_t hash_2, hash_cmp_func_t hash_cmp)
46 {
47 ht->ht_size = round_up_2 (size);
48 ht->ht_empty_slots = ht->ht_size;
49 ht->ht_vec = xcalloc (ht->ht_size, sizeof(struct token *));
50 if (ht->ht_vec == 0)
51 error (EXIT_FAILURE, 0, _("can't allocate %lu bytes for hash table: memory exhausted"),
52 ht->ht_size * sizeof(struct token *));
53 ht->ht_capacity = ht->ht_size * 15 / 16; /* 93.75% loading factor */
54 ht->ht_fill = 0;
55 ht->ht_collisions = 0;
56 ht->ht_lookups = 0;
57 ht->ht_rehashes = 0;
58 ht->ht_hash_1 = hash_1;
59 ht->ht_hash_2 = hash_2;
60 ht->ht_compare = hash_cmp;
61 }
62
•63 static void *
64 idu_hash_insert (struct hash_table* ht, void *item)
65 {
66 void **slot = hash_find_slot (ht, item);
67 void *old_item = slot ? *slot : 0;
68 hash_insert_at (ht, item, slot);
69 return ((HASH_VACANT (old_item)) ? 0 : old_item);
70 }
71
72 /* Load an array of items into `ht'. */
73
74 void
75 hash_load (struct hash_table* ht, void *item_table, unsigned long cardinality, unsigned long size)
76 {
77 char *items = (char *) item_table;
78 while (cardinality--)
79 {
•80 idu_hash_insert (ht, items);
81 items += size;
82 }
83 }
84
85 /* Returns the address of the table slot matching `key'. If `key' is
86 not found, return the address of an empty slot suitable for
87 inserting `key'. The caller is responsible for incrementing
88 ht_fill on insertion. */
89
90 void **
91 hash_find_slot (struct hash_table* ht, void const *key)
92 {
93 void **slot;
94 void **deleted_slot = 0;
95 unsigned int hash_2 = 0;
96 unsigned int hash_1 = (*ht->ht_hash_1) (key);
97
98 ht->ht_lookups++;
99 for (;;)
100 {
101 hash_1 %= ht->ht_size;
102 slot = &ht->ht_vec[hash_1];
103
104 if (*slot == 0)
105 return (deleted_slot ? deleted_slot : slot);
106 if (*slot == hash_deleted_item)
107 {
108 if (deleted_slot == 0)
109 deleted_slot = slot;
110 }
111 else
112 {
113 if (key == *slot)
114 return slot;
115 if ((*ht->ht_compare) (key, *slot) == 0)
116 return slot;
117 ht->ht_collisions++;
118 }
119 if (!hash_2)
120 hash_2 = (*ht->ht_hash_2) (key) | 1;
121 hash_1 += hash_2;
122 }
123 }
124
125 void *
126 hash_find_item (struct hash_table* ht, void const *key)
127 {
128 void **slot = hash_find_slot (ht, key);
129 return ((HASH_VACANT (*slot)) ? 0 : *slot);
130 }
131
132 void *
133 hash_insert_at (struct hash_table* ht, void *item, void const *slot)
134 {
135 void *old_item = *(void **) slot;
136 if (HASH_VACANT (old_item))
137 {
138 ht->ht_fill++;
139 if (old_item == 0)
140 ht->ht_empty_slots--;
141 old_item = item;
142 }
143 *(void const **) slot = item;
144 if (ht->ht_empty_slots < ht->ht_size - ht->ht_capacity)
145 {
146 hash_rehash (ht);
147 return (void *) hash_find_slot (ht, item);
148 }
149 else
150 return (void *) slot;
151 }
152
153 void *
154 hash_delete (struct hash_table* ht, void const *item)
155 {
156 void **slot = hash_find_slot (ht, item);
157 return hash_delete_at (ht, slot);
158 }
159
160 void *
161 hash_delete_at (struct hash_table* ht, void const *slot)
162 {
163 void *item = *(void **) slot;
164 if (!HASH_VACANT (item))
165 {
166 *(void const **) slot = hash_deleted_item;
167 ht->ht_fill--;
168 return item;
169 }
170 else
171 return 0;
172 }
173
•174 #if 0 // unused
175 void
176 hash_free_items (struct hash_table* ht)
177 {
178 void **vec = ht->ht_vec;
179 void **end = &vec[ht->ht_size];
180 for (; vec < end; vec++)
181 {
182 void *item = *vec;
183 if (!HASH_VACANT (item))
184 free (item);
185 *vec = 0;
186 }
187 ht->ht_fill = 0;
188 ht->ht_empty_slots = ht->ht_size;
189 }
190
191 void
192 hash_delete_items (struct hash_table* ht)
193 {
194 void **vec = ht->ht_vec;
195 void **end = &vec[ht->ht_size];
196 for (; vec < end; vec++)
197 *vec = 0;
198 ht->ht_fill = 0;
199 ht->ht_collisions = 0;
200 ht->ht_lookups = 0;
201 ht->ht_rehashes = 0;
202 ht->ht_empty_slots = ht->ht_size;
203 }
204
205 void
206 hash_free (struct hash_table* ht, int free_items)
207 {
208 if (free_items)
209 hash_free_items (ht);
210 else
211 {
212 ht->ht_fill = 0;
213 ht->ht_empty_slots = ht->ht_size;
214 }
215 free (ht->ht_vec);
216 ht->ht_vec = 0;
217 ht->ht_capacity = 0;
218 }
•219 #endif
220
221 void
222 hash_map (struct hash_table *ht, hash_map_func_t map)
223 {
224 void **slot;
225 void **end = &ht->ht_vec[ht->ht_size];
226
227 for (slot = ht->ht_vec; slot < end; slot++)
228 {
229 if (!HASH_VACANT (*slot))
230 (*map) (*slot);
231 }
232 }
233
234 /* Double the size of the hash table in the event of overflow... */
235
236 static void
237 hash_rehash (struct hash_table* ht)
238 {
239 unsigned long old_ht_size = ht->ht_size;
240 void **old_vec = ht->ht_vec;
241 void **ovp;
242
243 if (ht->ht_fill >= ht->ht_capacity)
244 {
245 ht->ht_size *= 2;
246 ht->ht_capacity = ht->ht_size - (ht->ht_size >> 4);
247 }
248 ht->ht_rehashes++;
249 ht->ht_vec = xcalloc (ht->ht_size, sizeof(struct token *));
250
251 for (ovp = old_vec; ovp < &old_vec[old_ht_size]; ovp++)
252 {
253 if (! HASH_VACANT (*ovp))
254 {
255 void **slot = hash_find_slot (ht, *ovp);
256 *slot = *ovp;
257 }
258 }
259 ht->ht_empty_slots = ht->ht_size - ht->ht_fill;
260 free (old_vec);
261 }
262
263 void
264 hash_print_stats (struct hash_table const *ht, FILE *out_FILE)
265 {
266 fprintf (out_FILE, _("Load=%lu/%lu=%.0f%%, "), ht->ht_fill, ht->ht_size,
267 100.0 * (double) ht->ht_fill / (double) ht->ht_size);
268 fprintf (out_FILE, _("Rehash=%u, "), ht->ht_rehashes);
269 fprintf (out_FILE, _("Collisions=%lu/%lu=%.0f%%"), ht->ht_collisions, ht->ht_lookups,
270 (ht->ht_lookups
271 ? (100.0 * (double) ht->ht_collisions / (double) ht->ht_lookups)
272 : 0));
273 }
274
275 /* Dump all items into a NULL-terminated vector. Use the
276 user-supplied vector, or malloc one. */
277
278 void**
279 hash_dump (struct hash_table const *ht, void **vector_0, qsort_cmp_t compare)
280 {
281 void **vector;
282 void **slot;
283 void **end = &ht->ht_vec[ht->ht_size];
284
285 if (vector_0 == 0)
286 vector_0 = xmalloc (sizeof (void *) * (ht->ht_fill + 1));
287 vector = vector_0;
288
289 for (slot = ht->ht_vec; slot < end; slot++)
290 if (!HASH_VACANT (*slot))
291 *vector++ = *slot;
292 *vector = 0;
293
294 if (compare)
295 qsort (vector_0, ht->ht_fill, sizeof (void *), compare);
296 return vector_0;
297 }
298
299 /* Round a given number up to the nearest power of 2. */
300
301 static unsigned long _GL_ATTRIBUTE_CONST
302 round_up_2 (unsigned long rough)
303 {
304 int round;
305
306 round = 1;
307 while (rough)
308 {
309 round <<= 1;
310 rough >>= 1;
311 }
312 return round;
313 }