1 /* Implement simple hashing table with string based keys.
2 Copyright (C) 1994-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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
7 by the Free Software Foundation; version 2 of the License, or
8 (at your option) any later version.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <https://www.gnu.org/licenses/>. */
27 #include <sys/types.h>
35 #include "simple-hash.h"
37 #define obstack_chunk_alloc malloc
38 #define obstack_chunk_free free
41 # define BITSPERBYTE 8
44 #define hashval_t uint32_t
47 #include <programs/xmalloc.h>
49 typedef struct hash_entry
55 struct hash_entry
*next
;
59 /* Prototypes for local functions. */
60 static void insert_entry_2 (hash_table
*htab
, const void *key
, size_t keylen
,
61 unsigned long hval
, size_t idx
, void *data
);
62 static size_t lookup (const hash_table
*htab
, const void *key
, size_t keylen
,
63 unsigned long int hval
);
64 static int is_prime (unsigned long int candidate
);
68 init_hash (hash_table
*htab
, unsigned long int init_size
)
70 /* We need the size to be a prime. */
71 init_size
= next_prime (init_size
);
73 /* Initialize the data structure. */
74 htab
->size
= init_size
;
77 htab
->table
= (void *) xcalloc (init_size
+ 1, sizeof (hash_entry
));
78 if (htab
->table
== NULL
)
81 obstack_init (&htab
->mem_pool
);
88 delete_hash (hash_table
*htab
)
91 obstack_free (&htab
->mem_pool
, NULL
);
97 insert_entry (hash_table
*htab
, const void *key
, size_t keylen
, void *data
)
99 unsigned long int hval
= compute_hashval (key
, keylen
);
100 hash_entry
*table
= (hash_entry
*) htab
->table
;
101 size_t idx
= lookup (htab
, key
, keylen
, hval
);
104 /* We don't want to overwrite the old value. */
108 /* An empty bucket has been found. */
109 insert_entry_2 (htab
, obstack_copy (&htab
->mem_pool
, key
, keylen
),
110 keylen
, hval
, idx
, data
);
116 insert_entry_2 (hash_table
*htab
, const void *key
, size_t keylen
,
117 unsigned long int hval
, size_t idx
, void *data
)
119 hash_entry
*table
= (hash_entry
*) htab
->table
;
121 table
[idx
].used
= hval
;
122 table
[idx
].key
= key
;
123 table
[idx
].keylen
= keylen
;
124 table
[idx
].data
= data
;
126 /* List the new value in the list. */
127 if ((hash_entry
*) htab
->first
== NULL
)
129 table
[idx
].next
= &table
[idx
];
130 htab
->first
= &table
[idx
];
134 table
[idx
].next
= ((hash_entry
*) htab
->first
)->next
;
135 ((hash_entry
*) htab
->first
)->next
= &table
[idx
];
136 htab
->first
= &table
[idx
];
140 if (100 * htab
->filled
> 75 * htab
->size
)
142 /* Table is filled more than 75%. Resize the table.
143 Experiments have shown that for best performance, this threshold
144 must lie between 40% and 85%. */
145 unsigned long int old_size
= htab
->size
;
147 htab
->size
= next_prime (htab
->size
* 2);
150 htab
->table
= (void *) xcalloc (1 + htab
->size
, sizeof (hash_entry
));
152 for (idx
= 1; idx
<= old_size
; ++idx
)
154 insert_entry_2 (htab
, table
[idx
].key
, table
[idx
].keylen
,
156 lookup (htab
, table
[idx
].key
, table
[idx
].keylen
,
166 find_entry (const hash_table
*htab
, const void *key
, size_t keylen
,
169 hash_entry
*table
= (hash_entry
*) htab
->table
;
170 size_t idx
= lookup (htab
, key
, keylen
, compute_hashval (key
, keylen
));
172 if (table
[idx
].used
== 0)
175 *result
= table
[idx
].data
;
181 set_entry (hash_table
*htab
, const void *key
, size_t keylen
, void *newval
)
183 hash_entry
*table
= (hash_entry
*) htab
->table
;
184 size_t idx
= lookup (htab
, key
, keylen
, compute_hashval (key
, keylen
));
186 if (table
[idx
].used
== 0)
189 table
[idx
].data
= newval
;
195 iterate_table (const hash_table
*htab
, void **ptr
, const void **key
,
196 size_t *keylen
, void **data
)
200 if (htab
->first
== NULL
)
202 *ptr
= (void *) ((hash_entry
*) htab
->first
)->next
;
206 if (*ptr
== htab
->first
)
208 *ptr
= (void *) (((hash_entry
*) *ptr
)->next
);
211 *key
= ((hash_entry
*) *ptr
)->key
;
212 *keylen
= ((hash_entry
*) *ptr
)->keylen
;
213 *data
= ((hash_entry
*) *ptr
)->data
;
219 [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
220 [Knuth] The Art of Computer Programming, part3 (6.4) */
223 lookup (const hash_table
*htab
, const void *key
, size_t keylen
,
224 unsigned long int hval
)
226 unsigned long int hash
;
228 hash_entry
*table
= (hash_entry
*) htab
->table
;
230 /* First hash function: simply take the modul but prevent zero. */
231 hash
= 1 + hval
% htab
->size
;
237 if (table
[idx
].used
== hval
&& table
[idx
].keylen
== keylen
238 && memcmp (table
[idx
].key
, key
, keylen
) == 0)
241 /* Second hash function as suggested in [Knuth]. */
242 hash
= 1 + hval
% (htab
->size
- 2);
247 idx
= htab
->size
+ idx
- hash
;
251 /* If entry is found use it. */
252 if (table
[idx
].used
== hval
&& table
[idx
].keylen
== keylen
253 && memcmp (table
[idx
].key
, key
, keylen
) == 0)
256 while (table
[idx
].used
);
263 next_prime (unsigned long int seed
)
265 /* Make it definitely odd. */
268 while (!is_prime (seed
))
276 is_prime (unsigned long int candidate
)
278 /* No even number and none less than 10 will be passed here. */
279 unsigned long int divn
= 3;
280 unsigned long int sq
= divn
* divn
;
282 while (sq
< candidate
&& candidate
% divn
!= 0)
289 return candidate
% divn
!= 0;