1 /* Implement simple hashing table with string based keys.
2 Copyright (C) 1994-1997,2000-2002,2005,2012 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/>. */
27 #include <sys/types.h>
39 #include "simple-hash.h"
41 #define obstack_chunk_alloc malloc
42 #define obstack_chunk_free free
45 # define BITSPERBYTE 8
49 # define bcopy(s, d, n) memcpy ((d), (s), (n))
52 #define hashval_t uint32_t
55 extern void *xmalloc (size_t n
)
56 __attribute_malloc__
__attribute_alloc_size (1);
57 extern void *xcalloc (size_t n
, size_t s
)
58 __attribute_malloc__
__attribute_alloc_size (1, 2);
60 typedef struct hash_entry
66 struct hash_entry
*next
;
70 /* Prototypes for local functions. */
71 static void insert_entry_2 (hash_table
*htab
, const void *key
, size_t keylen
,
72 unsigned long hval
, size_t idx
, void *data
);
73 static size_t lookup (const hash_table
*htab
, const void *key
, size_t keylen
,
74 unsigned long int hval
);
75 static int is_prime (unsigned long int candidate
);
79 init_hash (htab
, init_size
)
81 unsigned long int init_size
;
83 /* We need the size to be a prime. */
84 init_size
= next_prime (init_size
);
86 /* Initialize the data structure. */
87 htab
->size
= init_size
;
90 htab
->table
= (void *) xcalloc (init_size
+ 1, sizeof (hash_entry
));
91 if (htab
->table
== NULL
)
94 obstack_init (&htab
->mem_pool
);
105 obstack_free (&htab
->mem_pool
, NULL
);
111 insert_entry (htab
, key
, keylen
, data
)
117 unsigned long int hval
= compute_hashval (key
, keylen
);
118 hash_entry
*table
= (hash_entry
*) htab
->table
;
119 size_t idx
= lookup (htab
, key
, keylen
, hval
);
122 /* We don't want to overwrite the old value. */
126 /* An empty bucket has been found. */
127 insert_entry_2 (htab
, obstack_copy (&htab
->mem_pool
, key
, keylen
),
128 keylen
, hval
, idx
, data
);
134 insert_entry_2 (htab
, key
, keylen
, hval
, idx
, data
)
138 unsigned long int hval
;
142 hash_entry
*table
= (hash_entry
*) htab
->table
;
144 table
[idx
].used
= hval
;
145 table
[idx
].key
= key
;
146 table
[idx
].keylen
= keylen
;
147 table
[idx
].data
= data
;
149 /* List the new value in the list. */
150 if ((hash_entry
*) htab
->first
== NULL
)
152 table
[idx
].next
= &table
[idx
];
153 htab
->first
= &table
[idx
];
157 table
[idx
].next
= ((hash_entry
*) htab
->first
)->next
;
158 ((hash_entry
*) htab
->first
)->next
= &table
[idx
];
159 htab
->first
= &table
[idx
];
163 if (100 * htab
->filled
> 75 * htab
->size
)
165 /* Table is filled more than 75%. Resize the table.
166 Experiments have shown that for best performance, this threshold
167 must lie between 40% and 85%. */
168 unsigned long int old_size
= htab
->size
;
170 htab
->size
= next_prime (htab
->size
* 2);
173 htab
->table
= (void *) xcalloc (1 + htab
->size
, sizeof (hash_entry
));
175 for (idx
= 1; idx
<= old_size
; ++idx
)
177 insert_entry_2 (htab
, table
[idx
].key
, table
[idx
].keylen
,
179 lookup (htab
, table
[idx
].key
, table
[idx
].keylen
,
189 find_entry (htab
, key
, keylen
, result
)
190 const hash_table
*htab
;
195 hash_entry
*table
= (hash_entry
*) htab
->table
;
196 size_t idx
= lookup (htab
, key
, keylen
, compute_hashval (key
, keylen
));
198 if (table
[idx
].used
== 0)
201 *result
= table
[idx
].data
;
207 set_entry (htab
, key
, keylen
, newval
)
213 hash_entry
*table
= (hash_entry
*) htab
->table
;
214 size_t idx
= lookup (htab
, key
, keylen
, compute_hashval (key
, keylen
));
216 if (table
[idx
].used
== 0)
219 table
[idx
].data
= newval
;
225 iterate_table (htab
, ptr
, key
, keylen
, data
)
226 const hash_table
*htab
;
234 if (htab
->first
== NULL
)
236 *ptr
= (void *) ((hash_entry
*) htab
->first
)->next
;
240 if (*ptr
== htab
->first
)
242 *ptr
= (void *) (((hash_entry
*) *ptr
)->next
);
245 *key
= ((hash_entry
*) *ptr
)->key
;
246 *keylen
= ((hash_entry
*) *ptr
)->keylen
;
247 *data
= ((hash_entry
*) *ptr
)->data
;
253 [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
254 [Knuth] The Art of Computer Programming, part3 (6.4) */
257 lookup (htab
, key
, keylen
, hval
)
258 const hash_table
*htab
;
261 unsigned long int hval
;
263 unsigned long int hash
;
265 hash_entry
*table
= (hash_entry
*) htab
->table
;
267 /* First hash function: simply take the modul but prevent zero. */
268 hash
= 1 + hval
% htab
->size
;
274 if (table
[idx
].used
== hval
&& table
[idx
].keylen
== keylen
275 && memcmp (table
[idx
].key
, key
, keylen
) == 0)
278 /* Second hash function as suggested in [Knuth]. */
279 hash
= 1 + hval
% (htab
->size
- 2);
284 idx
= htab
->size
+ idx
- hash
;
288 /* If entry is found use it. */
289 if (table
[idx
].used
== hval
&& table
[idx
].keylen
== keylen
290 && memcmp (table
[idx
].key
, key
, keylen
) == 0)
293 while (table
[idx
].used
);
301 unsigned long int seed
;
303 /* Make it definitely odd. */
306 while (!is_prime (seed
))
315 unsigned long int candidate
;
317 /* No even number and none less than 10 will be passed here. */
318 unsigned long int divn
= 3;
319 unsigned long int sq
= divn
* divn
;
321 while (sq
< candidate
&& candidate
% divn
!= 0)
328 return candidate
% divn
!= 0;