1 /* Implement simple hashing table with string based keys.
2 Copyright (C) 1994-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, October 1994.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>. */
28 #include <sys/types.h>
36 #include "simple-hash.h"
38 #define obstack_chunk_alloc malloc
39 #define obstack_chunk_free free
42 # define BITSPERBYTE 8
46 # define bcopy(s, d, n) memcpy ((d), (s), (n))
49 #define hashval_t uint32_t
52 #include <programs/xmalloc.h>
54 typedef struct hash_entry
60 struct hash_entry
*next
;
64 /* Prototypes for local functions. */
65 static void insert_entry_2 (hash_table
*htab
, const void *key
, size_t keylen
,
66 unsigned long hval
, size_t idx
, void *data
);
67 static size_t lookup (const hash_table
*htab
, const void *key
, size_t keylen
,
68 unsigned long int hval
);
69 static int is_prime (unsigned long int candidate
);
73 init_hash (htab
, init_size
)
75 unsigned long int init_size
;
77 /* We need the size to be a prime. */
78 init_size
= next_prime (init_size
);
80 /* Initialize the data structure. */
81 htab
->size
= init_size
;
84 htab
->table
= (void *) xcalloc (init_size
+ 1, sizeof (hash_entry
));
85 if (htab
->table
== NULL
)
88 obstack_init (&htab
->mem_pool
);
99 obstack_free (&htab
->mem_pool
, NULL
);
105 insert_entry (htab
, key
, keylen
, data
)
111 unsigned long int hval
= compute_hashval (key
, keylen
);
112 hash_entry
*table
= (hash_entry
*) htab
->table
;
113 size_t idx
= lookup (htab
, key
, keylen
, hval
);
116 /* We don't want to overwrite the old value. */
120 /* An empty bucket has been found. */
121 insert_entry_2 (htab
, obstack_copy (&htab
->mem_pool
, key
, keylen
),
122 keylen
, hval
, idx
, data
);
128 insert_entry_2 (htab
, key
, keylen
, hval
, idx
, data
)
132 unsigned long int hval
;
136 hash_entry
*table
= (hash_entry
*) htab
->table
;
138 table
[idx
].used
= hval
;
139 table
[idx
].key
= key
;
140 table
[idx
].keylen
= keylen
;
141 table
[idx
].data
= data
;
143 /* List the new value in the list. */
144 if ((hash_entry
*) htab
->first
== NULL
)
146 table
[idx
].next
= &table
[idx
];
147 htab
->first
= &table
[idx
];
151 table
[idx
].next
= ((hash_entry
*) htab
->first
)->next
;
152 ((hash_entry
*) htab
->first
)->next
= &table
[idx
];
153 htab
->first
= &table
[idx
];
157 if (100 * htab
->filled
> 75 * htab
->size
)
159 /* Table is filled more than 75%. Resize the table.
160 Experiments have shown that for best performance, this threshold
161 must lie between 40% and 85%. */
162 unsigned long int old_size
= htab
->size
;
164 htab
->size
= next_prime (htab
->size
* 2);
167 htab
->table
= (void *) xcalloc (1 + htab
->size
, sizeof (hash_entry
));
169 for (idx
= 1; idx
<= old_size
; ++idx
)
171 insert_entry_2 (htab
, table
[idx
].key
, table
[idx
].keylen
,
173 lookup (htab
, table
[idx
].key
, table
[idx
].keylen
,
183 find_entry (htab
, key
, keylen
, result
)
184 const hash_table
*htab
;
189 hash_entry
*table
= (hash_entry
*) htab
->table
;
190 size_t idx
= lookup (htab
, key
, keylen
, compute_hashval (key
, keylen
));
192 if (table
[idx
].used
== 0)
195 *result
= table
[idx
].data
;
201 set_entry (htab
, key
, keylen
, newval
)
207 hash_entry
*table
= (hash_entry
*) htab
->table
;
208 size_t idx
= lookup (htab
, key
, keylen
, compute_hashval (key
, keylen
));
210 if (table
[idx
].used
== 0)
213 table
[idx
].data
= newval
;
219 iterate_table (htab
, ptr
, key
, keylen
, data
)
220 const hash_table
*htab
;
228 if (htab
->first
== NULL
)
230 *ptr
= (void *) ((hash_entry
*) htab
->first
)->next
;
234 if (*ptr
== htab
->first
)
236 *ptr
= (void *) (((hash_entry
*) *ptr
)->next
);
239 *key
= ((hash_entry
*) *ptr
)->key
;
240 *keylen
= ((hash_entry
*) *ptr
)->keylen
;
241 *data
= ((hash_entry
*) *ptr
)->data
;
247 [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
248 [Knuth] The Art of Computer Programming, part3 (6.4) */
251 lookup (htab
, key
, keylen
, hval
)
252 const hash_table
*htab
;
255 unsigned long int hval
;
257 unsigned long int hash
;
259 hash_entry
*table
= (hash_entry
*) htab
->table
;
261 /* First hash function: simply take the modul but prevent zero. */
262 hash
= 1 + hval
% htab
->size
;
268 if (table
[idx
].used
== hval
&& table
[idx
].keylen
== keylen
269 && memcmp (table
[idx
].key
, key
, keylen
) == 0)
272 /* Second hash function as suggested in [Knuth]. */
273 hash
= 1 + hval
% (htab
->size
- 2);
278 idx
= htab
->size
+ idx
- hash
;
282 /* If entry is found use it. */
283 if (table
[idx
].used
== hval
&& table
[idx
].keylen
== keylen
284 && memcmp (table
[idx
].key
, key
, keylen
) == 0)
287 while (table
[idx
].used
);
295 unsigned long int seed
;
297 /* Make it definitely odd. */
300 while (!is_prime (seed
))
309 unsigned long int candidate
;
311 /* No even number and none less than 10 will be passed here. */
312 unsigned long int divn
= 3;
313 unsigned long int sq
= divn
* divn
;
315 while (sq
< candidate
&& candidate
% divn
!= 0)
322 return candidate
% divn
!= 0;