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.
32 typedef struct _Slot Slot;
40 static gpointer KEYMARKER_REMOVED = &KEYMARKER_REMOVED;
44 GEqualFunc key_equal_func;
51 GDestroyNotify value_destroy_func, key_destroy_func;
54 static const guint prime_tbl[] = {
55 11, 19, 37, 73, 109, 163, 251, 367, 557, 823, 1237,
56 1861, 2777, 4177, 6247, 9371, 14057, 21089, 31627,
57 47431, 71143, 106721, 160073, 240101, 360163,
58 540217, 810343, 1215497, 1823231, 2734867, 4102283,
59 6153409, 9230113, 13845163
67 for (n = 3; n< (int)sqrt (x); n += 2) {
73 // There is only one even prime - 2.
82 for (i = (x & (~1))-1; i< G_MAXINT32; i += 2) {
90 g_spaced_primes_closest (guint x)
94 for (i = 0; i < G_N_ELEMENTS (prime_tbl); i++) {
95 if (x <= prime_tbl [i])
98 return calc_prime (x);
102 g_hash_table_new (GHashFunc hash_func, GEqualFunc key_equal_func)
106 if (hash_func == NULL)
107 hash_func = g_direct_hash;
108 if (key_equal_func == NULL)
109 key_equal_func = g_direct_equal;
110 hash = g_new0 (GHashTable, 1);
112 hash->hash_func = hash_func;
113 hash->key_equal_func = key_equal_func;
115 hash->table_size = g_spaced_primes_closest (1);
116 hash->table = g_new0 (Slot *, hash->table_size);
117 hash->last_rehash = hash->table_size;
123 g_hash_table_new_full (GHashFunc hash_func, GEqualFunc key_equal_func,
124 GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func)
126 GHashTable *hash = g_hash_table_new (hash_func, key_equal_func);
130 hash->key_destroy_func = key_destroy_func;
131 hash->value_destroy_func = value_destroy_func;
138 dump_hash_table (GHashTable *hash)
142 for (i = 0; i < hash->table_size; i++) {
145 for (s = hash->table [i]; s != NULL; s = s->next){
146 guint hashcode = (*hash->hash_func) (s->key);
147 guint slot = (hashcode) % hash->table_size;
148 printf ("key %p hash %x on slot %d correct slot %d tb size %d\n", s->key, hashcode, i, slot, hash->table_size);
156 sanity_check (GHashTable *hash)
160 for (i = 0; i < hash->table_size; i++) {
163 for (s = hash->table [i]; s != NULL; s = s->next){
164 guint hashcode = (*hash->hash_func) (s->key);
165 guint slot = (hashcode) % hash->table_size;
167 dump_hashcode_func = 1;
168 hashcode = (*hash->hash_func) (s->key);
169 dump_hashcode_func = 0;
170 g_error ("Key %p (bucket %d) on invalid bucket %d (hashcode %x) (tb size %d)", s->key, slot, i, hashcode, hash->table_size);
177 #define sanity_check(HASH) do {}while(0)
182 do_rehash (GHashTable *hash)
187 /* printf ("Resizing diff=%d slots=%d\n", hash->in_use - hash->last_rehash, hash->table_size); */
188 hash->last_rehash = hash->table_size;
189 current_size = hash->table_size;
190 hash->table_size = g_spaced_primes_closest (hash->in_use);
191 /* printf ("New size: %d\n", hash->table_size); */
193 hash->table = g_new0 (Slot *, hash->table_size);
195 for (i = 0; i < current_size; i++){
198 for (s = table [i]; s != NULL; s = next){
199 guint hashcode = ((*hash->hash_func) (s->key)) % hash->table_size;
202 s->next = hash->table [hashcode];
203 hash->table [hashcode] = s;
210 rehash (GHashTable *hash)
212 int diff = ABS (hash->last_rehash - hash->in_use);
214 /* These are the factors to play with to change the rehashing strategy */
215 /* I played with them with a large range, and could not really get */
216 /* something that was too good, maybe the tests are not that great */
217 if (!(diff * 0.75 > hash->table_size * 2))
224 g_hash_table_insert_replace (GHashTable *hash, gpointer key, gpointer value, gboolean replace)
230 g_return_if_fail (hash != NULL);
233 equal = hash->key_equal_func;
234 if (hash->in_use >= hash->threshold)
237 hashcode = ((*hash->hash_func) (key)) % hash->table_size;
238 for (s = hash->table [hashcode]; s != NULL; s = s->next){
239 if ((*equal) (s->key, key)){
241 if (hash->key_destroy_func != NULL)
242 (*hash->key_destroy_func)(s->key);
245 if (hash->value_destroy_func != NULL)
246 (*hash->value_destroy_func) (s->value);
255 s->next = hash->table [hashcode];
256 hash->table [hashcode] = s;
262 g_hash_table_size (GHashTable *hash)
264 g_return_val_if_fail (hash != NULL, 0);
270 g_hash_table_lookup (GHashTable *hash, gconstpointer key)
272 gpointer orig_key, value;
274 if (g_hash_table_lookup_extended (hash, key, &orig_key, &value))
281 g_hash_table_lookup_extended (GHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value)
287 g_return_val_if_fail (hash != NULL, FALSE);
289 equal = hash->key_equal_func;
291 hashcode = ((*hash->hash_func) (key)) % hash->table_size;
293 for (s = hash->table [hashcode]; s != NULL; s = s->next){
294 if ((*equal)(s->key, key)){
304 g_hash_table_foreach (GHashTable *hash, GHFunc func, gpointer user_data)
308 g_return_if_fail (hash != NULL);
309 g_return_if_fail (func != NULL);
311 for (i = 0; i < hash->table_size; i++){
314 for (s = hash->table [i]; s != NULL; s = s->next)
315 (*func)(s->key, s->value, user_data);
320 g_hash_table_find (GHashTable *hash, GHRFunc predicate, gpointer user_data)
324 g_return_val_if_fail (hash != NULL, NULL);
325 g_return_val_if_fail (predicate != NULL, NULL);
327 for (i = 0; i < hash->table_size; i++){
330 for (s = hash->table [i]; s != NULL; s = s->next)
331 if ((*predicate)(s->key, s->value, user_data))
338 g_hash_table_remove_all (GHashTable *hash)
342 g_return_if_fail (hash != NULL);
344 for (i = 0; i < hash->table_size; i++){
347 for (s = hash->table [i]; s != NULL; s = s->next)
348 g_hash_table_remove (hash, s->key);
353 g_hash_table_remove (GHashTable *hash, gconstpointer key)
359 g_return_val_if_fail (hash != NULL, FALSE);
361 equal = hash->key_equal_func;
363 hashcode = ((*hash->hash_func)(key)) % hash->table_size;
365 for (s = hash->table [hashcode]; s != NULL; s = s->next){
366 if ((*equal)(s->key, key)){
367 if (hash->key_destroy_func != NULL)
368 (*hash->key_destroy_func)(s->key);
369 if (hash->value_destroy_func != NULL)
370 (*hash->value_destroy_func)(s->value);
372 hash->table [hashcode] = s->next;
374 last->next = s->next;
387 g_hash_table_foreach_remove (GHashTable *hash, GHRFunc func, gpointer user_data)
392 g_return_val_if_fail (hash != NULL, 0);
393 g_return_val_if_fail (func != NULL, 0);
396 for (i = 0; i < hash->table_size; i++){
400 for (s = hash->table [i]; s != NULL; ){
401 if ((*func)(s->key, s->value, user_data)){
404 if (hash->key_destroy_func != NULL)
405 (*hash->key_destroy_func)(s->key);
406 if (hash->value_destroy_func != NULL)
407 (*hash->value_destroy_func)(s->value);
409 hash->table [i] = s->next;
412 last->next = s->next;
432 g_hash_table_foreach_steal (GHashTable *hash, GHRFunc func, gpointer user_data)
437 g_return_val_if_fail (hash != NULL, 0);
438 g_return_val_if_fail (func != NULL, 0);
441 for (i = 0; i < hash->table_size; i++){
445 for (s = hash->table [i]; s != NULL; ){
446 if ((*func)(s->key, s->value, user_data)){
450 hash->table [i] = s->next;
453 last->next = s->next;
473 g_hash_table_destroy (GHashTable *hash)
477 g_return_if_fail (hash != NULL);
479 for (i = 0; i < hash->table_size; i++){
482 for (s = hash->table [i]; s != NULL; s = next){
485 if (hash->key_destroy_func != NULL)
486 (*hash->key_destroy_func)(s->key);
487 if (hash->value_destroy_func != NULL)
488 (*hash->value_destroy_func)(s->value);
492 g_free (hash->table);
498 g_hash_table_print_stats (GHashTable *table)
500 int i, max_chain_index, chain_size, max_chain_size;
504 max_chain_index = -1;
505 for (i = 0; i < table->table_size; i++) {
507 for (node = table->table [i]; node; node = node->next)
509 if (chain_size > max_chain_size) {
510 max_chain_size = chain_size;
515 printf ("Size: %d Table Size: %d Max Chain Length: %d at %d\n", table->in_use, table->table_size, max_chain_size, max_chain_index);
519 g_direct_equal (gconstpointer v1, gconstpointer v2)
525 g_direct_hash (gconstpointer v1)
527 return GPOINTER_TO_UINT (v1);
531 g_int_equal (gconstpointer v1, gconstpointer v2)
533 return GPOINTER_TO_INT (v1) == GPOINTER_TO_INT (v2);
537 g_int_hash (gconstpointer v1)
539 return GPOINTER_TO_UINT(v1);
543 g_str_equal (gconstpointer v1, gconstpointer v2)
545 return strcmp (v1, v2) == 0;
549 g_str_hash (gconstpointer v1)
552 char *p = (char *) v1;
555 hash = (hash << 5) - (hash + *p);