1 /* Implement simple hashing table with string based keys.
2 Copyright (C) 1994-1997,2000,2001,2002,2005 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, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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))
54 extern void *xmalloc (size_t __n
);
55 extern void *xcalloc (size_t __n
, size_t __m
);
57 typedef struct hash_entry
63 struct hash_entry
*next
;
67 /* Prototypes for local functions. */
68 static void insert_entry_2 (hash_table
*htab
, const void *key
, size_t keylen
,
69 unsigned long hval
, size_t idx
, void *data
);
70 static size_t lookup (const hash_table
*htab
, const void *key
, size_t keylen
,
71 unsigned long int hval
);
72 static int is_prime (unsigned long int candidate
);
76 init_hash (htab
, init_size
)
78 unsigned long int init_size
;
80 /* We need the size to be a prime. */
81 init_size
= next_prime (init_size
);
83 /* Initialize the data structure. */
84 htab
->size
= init_size
;
87 htab
->table
= (void *) xcalloc (init_size
+ 1, sizeof (hash_entry
));
88 if (htab
->table
== NULL
)
91 obstack_init (&htab
->mem_pool
);
102 obstack_free (&htab
->mem_pool
, NULL
);
108 insert_entry (htab
, key
, keylen
, data
)
114 unsigned long int hval
= compute_hashval (key
, keylen
);
115 hash_entry
*table
= (hash_entry
*) htab
->table
;
116 size_t idx
= lookup (htab
, key
, keylen
, hval
);
119 /* We don't want to overwrite the old value. */
123 /* An empty bucket has been found. */
124 insert_entry_2 (htab
, obstack_copy (&htab
->mem_pool
, key
, keylen
),
125 keylen
, hval
, idx
, data
);
131 insert_entry_2 (htab
, key
, keylen
, hval
, idx
, data
)
135 unsigned long int hval
;
139 hash_entry
*table
= (hash_entry
*) htab
->table
;
141 table
[idx
].used
= hval
;
142 table
[idx
].key
= key
;
143 table
[idx
].keylen
= keylen
;
144 table
[idx
].data
= data
;
146 /* List the new value in the list. */
147 if ((hash_entry
*) htab
->first
== NULL
)
149 table
[idx
].next
= &table
[idx
];
150 htab
->first
= &table
[idx
];
154 table
[idx
].next
= ((hash_entry
*) htab
->first
)->next
;
155 ((hash_entry
*) htab
->first
)->next
= &table
[idx
];
156 htab
->first
= &table
[idx
];
160 if (100 * htab
->filled
> 75 * htab
->size
)
162 /* Table is filled more than 75%. Resize the table.
163 Experiments have shown that for best performance, this threshold
164 must lie between 40% and 85%. */
165 unsigned long int old_size
= htab
->size
;
167 htab
->size
= next_prime (htab
->size
* 2);
170 htab
->table
= (void *) xcalloc (1 + htab
->size
, sizeof (hash_entry
));
172 for (idx
= 1; idx
<= old_size
; ++idx
)
174 insert_entry_2 (htab
, table
[idx
].key
, table
[idx
].keylen
,
176 lookup (htab
, table
[idx
].key
, table
[idx
].keylen
,
186 find_entry (htab
, key
, keylen
, result
)
187 const hash_table
*htab
;
192 hash_entry
*table
= (hash_entry
*) htab
->table
;
193 size_t idx
= lookup (htab
, key
, keylen
, compute_hashval (key
, keylen
));
195 if (table
[idx
].used
== 0)
198 *result
= table
[idx
].data
;
204 set_entry (htab
, key
, keylen
, newval
)
210 hash_entry
*table
= (hash_entry
*) htab
->table
;
211 size_t idx
= lookup (htab
, key
, keylen
, compute_hashval (key
, keylen
));
213 if (table
[idx
].used
== 0)
216 table
[idx
].data
= newval
;
222 iterate_table (htab
, ptr
, key
, keylen
, data
)
223 const hash_table
*htab
;
231 if (htab
->first
== NULL
)
233 *ptr
= (void *) ((hash_entry
*) htab
->first
)->next
;
237 if (*ptr
== htab
->first
)
239 *ptr
= (void *) (((hash_entry
*) *ptr
)->next
);
242 *key
= ((hash_entry
*) *ptr
)->key
;
243 *keylen
= ((hash_entry
*) *ptr
)->keylen
;
244 *data
= ((hash_entry
*) *ptr
)->data
;
250 [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
251 [Knuth] The Art of Computer Programming, part3 (6.4) */
254 lookup (htab
, key
, keylen
, hval
)
255 const hash_table
*htab
;
258 unsigned long int hval
;
260 unsigned long int hash
;
262 hash_entry
*table
= (hash_entry
*) htab
->table
;
264 /* First hash function: simply take the modul but prevent zero. */
265 hash
= 1 + hval
% htab
->size
;
271 if (table
[idx
].used
== hval
&& table
[idx
].keylen
== keylen
272 && memcmp (table
[idx
].key
, key
, keylen
) == 0)
275 /* Second hash function as suggested in [Knuth]. */
276 hash
= 1 + hval
% (htab
->size
- 2);
281 idx
= htab
->size
+ idx
- hash
;
285 /* If entry is found use it. */
286 if (table
[idx
].used
== hval
&& table
[idx
].keylen
== keylen
287 && memcmp (table
[idx
].key
, key
, keylen
) == 0)
290 while (table
[idx
].used
);
298 unsigned long int seed
;
300 /* Make it definitely odd. */
303 while (!is_prime (seed
))
312 unsigned long int candidate
;
314 /* No even number and none less than 10 will be passed here. */
315 unsigned long int divn
= 3;
316 unsigned long int sq
= divn
* divn
;
318 while (sq
< candidate
&& candidate
% divn
!= 0)
325 return candidate
% divn
!= 0;