1 /* Implement simple hashing table with string based keys.
2 Copyright (C) 1994-1997, 2000, 2001, 2002 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 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library 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 GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 #include <sys/types.h>
40 #include "simple-hash.h"
42 #define obstack_chunk_alloc malloc
43 #define obstack_chunk_free free
46 # define BITSPERBYTE 8
50 # define bcopy(s, d, n) memcpy ((d), (s), (n))
55 extern void *xmalloc (size_t __n
);
56 extern void *xcalloc (size_t __n
, size_t __m
);
58 typedef struct hash_entry
64 struct hash_entry
*next
;
68 /* Prototypes for local functions. */
69 static void insert_entry_2 (hash_table
*htab
, const void *key
, size_t keylen
,
70 unsigned long hval
, size_t idx
, void *data
);
71 static size_t lookup (const hash_table
*htab
, const void *key
, size_t keylen
,
72 unsigned long int hval
);
73 static int is_prime (unsigned long int candidate
);
77 init_hash (htab
, init_size
)
79 unsigned long int init_size
;
81 /* We need the size to be a prime. */
82 init_size
= next_prime (init_size
);
84 /* Initialize the data structure. */
85 htab
->size
= init_size
;
88 htab
->table
= (void *) xcalloc (init_size
+ 1, sizeof (hash_entry
));
89 if (htab
->table
== NULL
)
92 obstack_init (&htab
->mem_pool
);
103 obstack_free (&htab
->mem_pool
, NULL
);
109 insert_entry (htab
, key
, keylen
, data
)
115 unsigned long int hval
= compute_hashval (key
, keylen
);
116 hash_entry
*table
= (hash_entry
*) htab
->table
;
117 size_t idx
= lookup (htab
, key
, keylen
, hval
);
120 /* We don't want to overwrite the old value. */
124 /* An empty bucket has been found. */
125 insert_entry_2 (htab
, obstack_copy (&htab
->mem_pool
, key
, keylen
),
126 keylen
, hval
, idx
, data
);
132 insert_entry_2 (htab
, key
, keylen
, hval
, idx
, data
)
136 unsigned long int hval
;
140 hash_entry
*table
= (hash_entry
*) htab
->table
;
142 table
[idx
].used
= hval
;
143 table
[idx
].key
= key
;
144 table
[idx
].keylen
= keylen
;
145 table
[idx
].data
= data
;
147 /* List the new value in the list. */
148 if ((hash_entry
*) htab
->first
== NULL
)
150 table
[idx
].next
= &table
[idx
];
151 *(hash_entry
**) &htab
->first
= &table
[idx
];
155 table
[idx
].next
= ((hash_entry
*) htab
->first
)->next
;
156 ((hash_entry
*) htab
->first
)->next
= &table
[idx
];
157 *(hash_entry
**) &htab
->first
= &table
[idx
];
161 if (100 * htab
->filled
> 75 * htab
->size
)
163 /* Table is filled more than 75%. Resize the table.
164 Experiments have shown that for best performance, this threshold
165 must lie between 40% and 85%. */
166 unsigned long int old_size
= htab
->size
;
168 htab
->size
= next_prime (htab
->size
* 2);
171 htab
->table
= (void *) xcalloc (1 + htab
->size
, sizeof (hash_entry
));
173 for (idx
= 1; idx
<= old_size
; ++idx
)
175 insert_entry_2 (htab
, table
[idx
].key
, table
[idx
].keylen
,
177 lookup (htab
, table
[idx
].key
, table
[idx
].keylen
,
187 find_entry (htab
, key
, keylen
, result
)
188 const hash_table
*htab
;
193 hash_entry
*table
= (hash_entry
*) htab
->table
;
194 size_t idx
= lookup (htab
, key
, keylen
, compute_hashval (key
, keylen
));
196 if (table
[idx
].used
== 0)
199 *result
= table
[idx
].data
;
205 set_entry (htab
, key
, keylen
, newval
)
211 hash_entry
*table
= (hash_entry
*) htab
->table
;
212 size_t idx
= lookup (htab
, key
, keylen
, compute_hashval (key
, keylen
));
214 if (table
[idx
].used
== 0)
217 table
[idx
].data
= newval
;
223 iterate_table (htab
, ptr
, key
, keylen
, data
)
224 const hash_table
*htab
;
232 if (htab
->first
== NULL
)
234 *ptr
= (void *) ((hash_entry
*) htab
->first
)->next
;
238 if (*ptr
== htab
->first
)
240 *ptr
= (void *) (((hash_entry
*) *ptr
)->next
);
243 *key
= ((hash_entry
*) *ptr
)->key
;
244 *keylen
= ((hash_entry
*) *ptr
)->keylen
;
245 *data
= ((hash_entry
*) *ptr
)->data
;
251 [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
252 [Knuth] The Art of Computer Programming, part3 (6.4) */
255 lookup (htab
, key
, keylen
, hval
)
256 const hash_table
*htab
;
259 unsigned long int hval
;
261 unsigned long int hash
;
263 hash_entry
*table
= (hash_entry
*) htab
->table
;
265 /* First hash function: simply take the modul but prevent zero. */
266 hash
= 1 + hval
% htab
->size
;
272 if (table
[idx
].used
== hval
&& table
[idx
].keylen
== keylen
273 && memcmp (table
[idx
].key
, key
, keylen
) == 0)
276 /* Second hash function as suggested in [Knuth]. */
277 hash
= 1 + hval
% (htab
->size
- 2);
282 idx
= htab
->size
+ idx
- hash
;
286 /* If entry is found use it. */
287 if (table
[idx
].used
== hval
&& table
[idx
].keylen
== keylen
288 && memcmp (table
[idx
].key
, key
, keylen
) == 0)
291 while (table
[idx
].used
);
299 unsigned long int seed
;
301 /* Make it definitely odd. */
304 while (!is_prime (seed
))
313 unsigned long int candidate
;
315 /* No even number and none less than 10 will be passed here. */
316 unsigned long int divn
= 3;
317 unsigned long int sq
= divn
* divn
;
319 while (sq
< candidate
&& candidate
% divn
!= 0)
326 return candidate
% divn
!= 0;