1 /* hash - implement simple hashing table with string based keys.
2 Copyright (C) 1994-1995, 2000-2004 Free Software Foundation, Inc.
3 Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, October 1994.
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 by
7 the Free Software Foundation; either version 2, or (at your option)
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, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
30 #include <sys/types.h>
36 #define obstack_chunk_alloc xmalloc
37 #define obstack_chunk_free free
39 typedef struct hash_entry
45 struct hash_entry
*next
;
49 /* Forward declaration of local functions. */
50 static void insert_entry_2 (hash_table
*htab
,
51 const void *key
, size_t keylen
,
52 unsigned long int hval
, size_t idx
, void *data
);
53 static size_t lookup (hash_table
*htab
,
54 const void *key
, size_t keylen
,
55 unsigned long int hval
);
56 static unsigned long compute_hashval (const void *key
, size_t keylen
);
57 static int is_prime (unsigned long int candidate
);
61 init_hash (hash_table
*htab
, unsigned long int init_size
)
63 /* We need the size to be a prime. */
64 init_size
= next_prime (init_size
);
66 /* Initialize the data structure. */
67 htab
->size
= init_size
;
70 htab
->table
= (void *) xcalloc (init_size
+ 1, sizeof (hash_entry
));
72 obstack_init (&htab
->mem_pool
);
79 delete_hash (hash_table
*htab
)
82 obstack_free (&htab
->mem_pool
, NULL
);
88 insert_entry (hash_table
*htab
, const void *key
, size_t keylen
, void *data
)
90 unsigned long int hval
= compute_hashval (key
, keylen
);
91 hash_entry
*table
= (hash_entry
*) htab
->table
;
92 size_t idx
= lookup (htab
, key
, keylen
, hval
);
95 /* We don't want to overwrite the old value. */
99 /* An empty bucket has been found. */
100 insert_entry_2 (htab
, obstack_copy (&htab
->mem_pool
, key
, keylen
),
101 keylen
, hval
, idx
, data
);
107 insert_entry_2 (hash_table
*htab
,
108 const void *key
, size_t keylen
,
109 unsigned long int hval
, size_t idx
, void *data
)
111 hash_entry
*table
= (hash_entry
*) htab
->table
;
113 table
[idx
].used
= hval
;
114 table
[idx
].key
= key
;
115 table
[idx
].keylen
= keylen
;
116 table
[idx
].data
= data
;
118 /* List the new value in the list. */
119 if ((hash_entry
*) htab
->first
== NULL
)
121 table
[idx
].next
= &table
[idx
];
122 *(hash_entry
**) &htab
->first
= &table
[idx
];
126 table
[idx
].next
= ((hash_entry
*) htab
->first
)->next
;
127 ((hash_entry
*) htab
->first
)->next
= &table
[idx
];
128 *(hash_entry
**) &htab
->first
= &table
[idx
];
132 if (100 * htab
->filled
> 75 * htab
->size
)
134 /* Table is filled more than 75%. Resize the table. */
135 unsigned long int old_size
= htab
->size
;
137 htab
->size
= next_prime (htab
->size
* 2);
140 htab
->table
= (void *) xcalloc (1 + htab
->size
, sizeof (hash_entry
));
142 for (idx
= 1; idx
<= old_size
; ++idx
)
144 insert_entry_2 (htab
, table
[idx
].key
, table
[idx
].keylen
,
146 lookup (htab
, table
[idx
].key
, table
[idx
].keylen
,
156 find_entry (hash_table
*htab
, const void *key
, size_t keylen
, void **result
)
158 hash_entry
*table
= (hash_entry
*) htab
->table
;
159 size_t idx
= lookup (htab
, key
, keylen
, compute_hashval (key
, keylen
));
161 if (table
[idx
].used
== 0)
164 *result
= table
[idx
].data
;
170 iterate_table (hash_table
*htab
, void **ptr
, const void **key
, size_t *keylen
,
175 if (htab
->first
== NULL
)
177 *ptr
= (void *) ((hash_entry
*) htab
->first
)->next
;
181 if (*ptr
== htab
->first
)
183 *ptr
= (void *) (((hash_entry
*) *ptr
)->next
);
186 *key
= ((hash_entry
*) *ptr
)->key
;
187 *keylen
= ((hash_entry
*) *ptr
)->keylen
;
188 *data
= ((hash_entry
*) *ptr
)->data
;
194 [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
195 [Knuth] The Art of Computer Programming, part3 (6.4) */
198 lookup (hash_table
*htab
,
199 const void *key
, size_t keylen
,
200 unsigned long int hval
)
202 unsigned long int hash
;
204 hash_entry
*table
= (hash_entry
*) htab
->table
;
206 /* First hash function: simply take the modul but prevent zero. */
207 hash
= 1 + hval
% htab
->size
;
213 if (table
[idx
].used
== hval
&& table
[idx
].keylen
== keylen
214 && memcmp (table
[idx
].key
, key
, keylen
) == 0)
217 /* Second hash function as suggested in [Knuth]. */
218 hash
= 1 + hval
% (htab
->size
- 2);
223 idx
= htab
->size
+ idx
- hash
;
227 /* If entry is found use it. */
228 if (table
[idx
].used
== hval
&& table
[idx
].keylen
== keylen
229 && memcmp (table
[idx
].key
, key
, keylen
) == 0)
232 while (table
[idx
].used
);
239 compute_hashval (const void *key
, size_t keylen
)
242 unsigned long int hval
;
244 /* Compute the hash value for the given string. The algorithm
245 is taken from [Aho,Sethi,Ullman]. */
250 hval
= (hval
<< 9) | (hval
>> (sizeof (unsigned long) * CHAR_BIT
- 9));
251 hval
+= (unsigned long int) *(((const char *) key
) + cnt
++);
253 return hval
!= 0 ? hval
: ~((unsigned long) 0);
258 next_prime (unsigned long int seed
)
260 /* Make it definitely odd. */
263 while (!is_prime (seed
))
271 is_prime (unsigned long int candidate
)
273 /* No even number and none less than 10 will be passed here. */
274 unsigned long int divn
= 3;
275 unsigned long int sq
= divn
* divn
;
277 while (sq
< candidate
&& candidate
% divn
!= 0)
284 return candidate
% divn
!= 0;