2 * ghashtable.c: Hashtable implementation
5 * Miguel de Icaza (miguel@novell.com)
7 * (C) 2006 Novell, Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 * In addition to supporting the standard g_hash_table behavior,
31 * this version adds support for providing a malloc/free set of
32 * routines, these by default are malloc and free, but with a GC,
33 * they could be GC_malloc and the free routine could be NULL. If
34 * the value for free is NULL, we set all the fields that we would
35 * have otherwise released to NULL, to assist the garbage collector.
37 * This is designed to support the GC-aware hashtable that Mono uses,
38 * and replaces mono-hash.c. To do this, a new constructor is introduced
39 * that contains the extra parameters.
45 typedef struct _Slot Slot;
53 static gpointer KEYMARKER_REMOVED = &KEYMARKER_REMOVED;
57 GEqualFunc key_equal_func;
64 GDestroyNotify value_destroy_func, key_destroy_func;
69 static int prime_tbl[] = {
70 11, 19, 37, 73, 109, 163, 251, 367, 557, 823, 1237,
71 1861, 2777, 4177, 6247, 9371, 14057, 21089, 31627,
72 47431, 71143, 106721, 160073, 240101, 360163,
73 540217, 810343, 1215497, 1823231, 2734867, 4102283,
74 6153409, 9230113, 13845163
82 for (n = 3; n< (int)sqrt (x); n += 2) {
88 // There is only one even prime - 2.
97 for (i = (x & (~1))-1; i< G_MAXINT32; i += 2) {
109 for (i = 0; i < G_N_ELEMENTS (prime_tbl); i++) {
110 if (x <= prime_tbl [i])
111 return prime_tbl [i];
113 return calc_prime (x);
120 return calloc (1, t);
124 g_hash_table_new (GHashFunc hash_func, GEqualFunc key_equal_func)
126 return g_hash_table_new_full_alloc (hash_func, key_equal_func, NULL, NULL, g_new0, free);
130 g_hash_table_new_alloc (GHashFunc hash_func, GEqualFunc key_equal_func, GMAlloc allocfn, GMFree freefn)
132 return g_hash_table_new_full_alloc (hash_func, key_equal_func, NULL, NULL, allocfn, freefn);
137 g_hash_table_new_full (GHashFunc hash_func, GEqualFunc key_equal_func,
138 GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func)
140 return g_hash_table_new_full_alloc (hash_func, key_equal_func, key_destroy_func, value_destroy_func, g_new0, free);
144 g_hash_table_new_full_alloc (GHashFunc hash_func, GEqualFunc key_equal_func,
145 GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func,
146 GMAlloc allocfn, GMFree freefn)
150 if (hash_func == NULL)
151 hash_func = g_direct_hash;
152 if (key_equal_func == NULL)
153 key_equal_func = g_direct_equal;
155 hash = (*allocfn) (sizeof (GHashTable));
157 hash->hash_func = hash_func;
158 hash->key_equal_func = key_equal_func;
159 hash->key_destroy_func = key_destroy_func;
160 hash->value_destroy_func = value_destroy_func;
161 hash->table_size = to_prime (1);
162 hash->table = (*allocfn) (sizeof (Slot *) * hash->table_size);
163 hash->last_rehash = hash->table_size;
164 hash->freefn = freefn;
165 hash->allocfn = allocfn;
171 do_rehash (GHashTable *hash)
176 /* printf ("Resizing diff=%d slots=%d\n", hash->in_use - hash->last_rehash, hash->table_size); */
177 hash->last_rehash = hash->table_size;
178 current_size = hash->table_size;
179 hash->table_size = to_prime (hash->in_use);
180 /* printf ("New size: %d\n", hash->table_size); */
182 hash->table = (*hash->allocfn)(sizeof (Slot *) * hash->table_size);
184 for (i = 0; i < current_size; i++){
187 for (s = table [i]; s != NULL; s = next){
188 guint hashcode = ((*hash->hash_func) (s->key)) % hash->table_size;
191 s->next = hash->table [hashcode];
192 hash->table [hashcode] = s;
195 (*hash->freefn)(table);
199 rehash (GHashTable *hash)
201 int diff = ABS (hash->last_rehash, hash->in_use);
203 /* These are the factors to play with to change the rehashing strategy */
204 /* I played with them with a large range, and could not really get */
205 /* something that was too good, maybe the tests are not that great */
206 if (!(diff * 0.75 > hash->table_size * 2))
212 g_hash_table_insert_replace (GHashTable *hash, gpointer key, gpointer value, gboolean replace)
218 g_return_if_fail (hash != NULL);
220 equal = hash->key_equal_func;
221 if (hash->in_use >= hash->threshold)
224 hashcode = ((*hash->hash_func) (key)) % hash->table_size;
225 for (s = hash->table [hashcode]; s != NULL; s = s->next){
226 if ((*equal) (s->key, key)){
228 if (hash->key_destroy_func != NULL)
229 (*hash->key_destroy_func)(s->key);
232 if (hash->value_destroy_func != NULL)
233 (*hash->value_destroy_func) (s->value);
238 s = (*hash->allocfn) (sizeof (Slot));
241 s->next = hash->table [hashcode];
242 hash->table [hashcode] = s;
247 g_hash_table_size (GHashTable *hash)
249 g_return_val_if_fail (hash != NULL, 0);
255 g_hash_table_lookup (GHashTable *hash, gconstpointer key)
257 gpointer orig_key, value;
259 if (g_hash_table_lookup_extended (hash, key, &orig_key, &value))
266 g_hash_table_lookup_extended (GHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value)
272 g_return_val_if_fail (hash != NULL, FALSE);
273 equal = hash->key_equal_func;
275 hashcode = ((*hash->hash_func) (key)) % hash->table_size;
277 for (s = hash->table [hashcode]; s != NULL; s = s->next){
278 if ((*equal)(s->key, key)){
288 g_hash_table_foreach (GHashTable *hash, GHFunc func, gpointer user_data)
292 g_return_if_fail (hash != NULL);
293 g_return_if_fail (func != NULL);
295 for (i = 0; i < hash->table_size; i++){
298 for (s = hash->table [i]; s != NULL; s = s->next)
299 (*func)(s->key, s->value, user_data);
304 g_hash_table_find (GHashTable *hash, GHRFunc predicate, gpointer user_data)
308 g_return_val_if_fail (hash != NULL, NULL);
309 g_return_val_if_fail (predicate != NULL, NULL);
311 for (i = 0; i < hash->table_size; i++){
314 for (s = hash->table [i]; s != NULL; s = s->next)
315 if ((*predicate)(s->key, s->value, user_data))
322 g_hash_table_remove (GHashTable *hash, gconstpointer key)
328 g_return_val_if_fail (hash != NULL, FALSE);
329 equal = hash->key_equal_func;
331 hashcode = ((*hash->hash_func)(key)) % hash->table_size;
333 for (s = hash->table [hashcode]; s != NULL; s = s->next){
334 if ((*equal)(s->key, key)){
335 if (hash->key_destroy_func != NULL)
336 (*hash->key_destroy_func)(s->key);
337 if (hash->value_destroy_func != NULL)
338 (*hash->value_destroy_func)(s->value);
340 hash->table [hashcode] = s->next;
342 last->next = s->next;
358 g_hash_table_foreach_remove (GHashTable *hash, GHRFunc func, gpointer user_data)
363 g_return_val_if_fail (hash != NULL, 0);
364 g_return_val_if_fail (func != NULL, 0);
366 for (i = 0; i < hash->table_size; i++){
370 for (s = hash->table [i]; s != NULL; ){
371 if ((*func)(s->key, s->value, user_data)){
374 if (hash->key_destroy_func != NULL)
375 (*hash->key_destroy_func)(s->key);
376 if (hash->value_destroy_func != NULL)
377 (*hash->value_destroy_func)(s->value);
379 hash->table [i] = s->next;
382 last->next = s->next;
407 g_hash_table_destroy (GHashTable *hash)
411 g_return_if_fail (hash != NULL);
413 for (i = 0; i < hash->table_size; i++){
416 for (s = hash->table [i]; s != NULL; s = next){
419 if (hash->key_destroy_func != NULL)
420 (*hash->key_destroy_func)(s->key);
421 if (hash->value_destroy_func != NULL)
422 (*hash->value_destroy_func)(s->value);
432 (*hash->freefn) (hash->table);
433 (*hash->freefn) (hash);
440 g_direct_equal (gconstpointer v1, gconstpointer v2)
446 g_direct_hash (gconstpointer v1)
448 return GPOINTER_TO_UINT (v1);
452 g_int_equal (gconstpointer v1, gconstpointer v2)
454 return GPOINTER_TO_INT (v1) == GPOINTER_TO_INT (v2);
458 g_int_hash (gconstpointer v1)
460 return GPOINTER_TO_UINT(v1);
464 g_str_equal (gconstpointer v1, gconstpointer v2)
466 return strcmp (v1, v2) == 0;
470 g_str_hash (gconstpointer v1)
473 char *p = (char *) v1;
476 hash = (hash << 5) - (hash + *p);