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 #include <eglib-remap.h> // Remove the cast macros and restore the rename macros.
34 typedef struct _Slot Slot
;
42 static gpointer KEYMARKER_REMOVED
= &KEYMARKER_REMOVED
;
46 GEqualFunc key_equal_func
;
53 GDestroyNotify value_destroy_func
, key_destroy_func
;
62 static const guint prime_tbl
[] = {
63 11, 19, 37, 73, 109, 163, 251, 367, 557, 823, 1237,
64 1861, 2777, 4177, 6247, 9371, 14057, 21089, 31627,
65 47431, 71143, 106721, 160073, 240101, 360163,
66 540217, 810343, 1215497, 1823231, 2734867, 4102283,
67 6153409, 9230113, 13845163
75 for (n
= 3; n
< (int)sqrt (x
); n
+= 2) {
81 // There is only one even prime - 2.
90 for (i
= (x
& (~1))-1; i
< G_MAXINT32
; i
+= 2) {
98 g_spaced_primes_closest (guint x
)
102 for (i
= 0; i
< G_N_ELEMENTS (prime_tbl
); i
++) {
103 if (x
<= prime_tbl
[i
])
104 return prime_tbl
[i
];
106 return calc_prime (x
);
110 g_hash_table_new (GHashFunc hash_func
, GEqualFunc key_equal_func
)
114 if (hash_func
== NULL
)
115 hash_func
= g_direct_hash
;
116 if (key_equal_func
== NULL
)
117 key_equal_func
= g_direct_equal
;
118 hash
= g_new0 (GHashTable
, 1);
120 hash
->hash_func
= hash_func
;
121 hash
->key_equal_func
= key_equal_func
;
123 hash
->table_size
= g_spaced_primes_closest (1);
124 hash
->table
= g_new0 (Slot
*, hash
->table_size
);
125 hash
->last_rehash
= hash
->table_size
;
131 g_hash_table_new_full (GHashFunc hash_func
, GEqualFunc key_equal_func
,
132 GDestroyNotify key_destroy_func
, GDestroyNotify value_destroy_func
)
134 GHashTable
*hash
= g_hash_table_new (hash_func
, key_equal_func
);
138 hash
->key_destroy_func
= key_destroy_func
;
139 hash
->value_destroy_func
= value_destroy_func
;
146 dump_hash_table (GHashTable
*hash
)
150 for (i
= 0; i
< hash
->table_size
; i
++) {
153 for (s
= hash
->table
[i
]; s
!= NULL
; s
= s
->next
){
154 guint hashcode
= (*hash
->hash_func
) (s
->key
);
155 guint slot
= (hashcode
) % hash
->table_size
;
156 printf ("key %p hash %x on slot %d correct slot %d tb size %d\n", s
->key
, hashcode
, i
, slot
, hash
->table_size
);
164 sanity_check (GHashTable
*hash
)
168 for (i
= 0; i
< hash
->table_size
; i
++) {
171 for (s
= hash
->table
[i
]; s
!= NULL
; s
= s
->next
){
172 guint hashcode
= (*hash
->hash_func
) (s
->key
);
173 guint slot
= (hashcode
) % hash
->table_size
;
175 dump_hashcode_func
= 1;
176 hashcode
= (*hash
->hash_func
) (s
->key
);
177 dump_hashcode_func
= 0;
178 g_error ("Key %p (bucket %d) on invalid bucket %d (hashcode %x) (tb size %d)", s
->key
, slot
, i
, hashcode
, hash
->table_size
);
185 #define sanity_check(HASH) do {}while(0)
190 do_rehash (GHashTable
*hash
)
195 /* printf ("Resizing diff=%d slots=%d\n", hash->in_use - hash->last_rehash, hash->table_size); */
196 hash
->last_rehash
= hash
->table_size
;
197 current_size
= hash
->table_size
;
198 hash
->table_size
= g_spaced_primes_closest (hash
->in_use
);
199 /* printf ("New size: %d\n", hash->table_size); */
201 hash
->table
= g_new0 (Slot
*, hash
->table_size
);
203 for (i
= 0; i
< current_size
; i
++){
206 for (s
= table
[i
]; s
!= NULL
; s
= next
){
207 guint hashcode
= ((*hash
->hash_func
) (s
->key
)) % hash
->table_size
;
210 s
->next
= hash
->table
[hashcode
];
211 hash
->table
[hashcode
] = s
;
218 rehash (GHashTable
*hash
)
220 int diff
= ABS (hash
->last_rehash
- hash
->in_use
);
222 /* These are the factors to play with to change the rehashing strategy */
223 /* I played with them with a large range, and could not really get */
224 /* something that was too good, maybe the tests are not that great */
225 if (!(diff
* 0.75 > hash
->table_size
* 2))
232 g_hash_table_insert_replace (GHashTable
*hash
, gpointer key
, gpointer value
, gboolean replace
)
238 g_return_if_fail (hash
!= NULL
);
241 equal
= hash
->key_equal_func
;
242 if (hash
->in_use
>= hash
->threshold
)
245 hashcode
= ((*hash
->hash_func
) (key
)) % hash
->table_size
;
246 for (s
= hash
->table
[hashcode
]; s
!= NULL
; s
= s
->next
){
247 if ((*equal
) (s
->key
, key
)){
249 if (hash
->key_destroy_func
!= NULL
)
250 (*hash
->key_destroy_func
)(s
->key
);
253 if (hash
->value_destroy_func
!= NULL
)
254 (*hash
->value_destroy_func
) (s
->value
);
263 s
->next
= hash
->table
[hashcode
];
264 hash
->table
[hashcode
] = s
;
270 g_hash_table_get_keys (GHashTable
*hash
)
276 g_hash_table_iter_init (&iter
, hash
);
278 while (g_hash_table_iter_next (&iter
, &key
, NULL
))
279 rv
= g_list_prepend (rv
, key
);
281 return g_list_reverse (rv
);
285 g_hash_table_get_values (GHashTable
*hash
)
291 g_hash_table_iter_init (&iter
, hash
);
293 while (g_hash_table_iter_next (&iter
, NULL
, &value
))
294 rv
= g_list_prepend (rv
, value
);
296 return g_list_reverse (rv
);
301 g_hash_table_size (GHashTable
*hash
)
303 g_return_val_if_fail (hash
!= NULL
, 0);
309 g_hash_table_contains (GHashTable
*hash
, gconstpointer key
)
311 g_return_val_if_fail (key
!= NULL
, FALSE
);
313 return g_hash_table_lookup_extended (hash
, key
, NULL
, NULL
);
317 g_hash_table_lookup (GHashTable
*hash
, gconstpointer key
)
319 gpointer orig_key
, value
;
321 if (g_hash_table_lookup_extended (hash
, key
, &orig_key
, &value
))
328 g_hash_table_lookup_extended (GHashTable
*hash
, gconstpointer key
, gpointer
*orig_key
, gpointer
*value
)
334 g_return_val_if_fail (hash
!= NULL
, FALSE
);
336 equal
= hash
->key_equal_func
;
338 hashcode
= ((*hash
->hash_func
) (key
)) % hash
->table_size
;
340 for (s
= hash
->table
[hashcode
]; s
!= NULL
; s
= s
->next
){
341 if ((*equal
)(s
->key
, key
)){
353 g_hash_table_foreach (GHashTable
*hash
, GHFunc func
, gpointer user_data
)
357 g_return_if_fail (hash
!= NULL
);
358 g_return_if_fail (func
!= NULL
);
360 for (i
= 0; i
< hash
->table_size
; i
++){
363 for (s
= hash
->table
[i
]; s
!= NULL
; s
= s
->next
)
364 (*func
)(s
->key
, s
->value
, user_data
);
369 g_hash_table_find (GHashTable
*hash
, GHRFunc predicate
, gpointer user_data
)
373 g_return_val_if_fail (hash
!= NULL
, NULL
);
374 g_return_val_if_fail (predicate
!= NULL
, NULL
);
376 for (i
= 0; i
< hash
->table_size
; i
++){
379 for (s
= hash
->table
[i
]; s
!= NULL
; s
= s
->next
)
380 if ((*predicate
)(s
->key
, s
->value
, user_data
))
387 g_hash_table_remove_all (GHashTable
*hash
)
391 g_return_if_fail (hash
!= NULL
);
393 for (i
= 0; i
< hash
->table_size
; i
++){
396 while (hash
->table
[i
]) {
398 g_hash_table_remove (hash
, s
->key
);
404 g_hash_table_remove (GHashTable
*hash
, gconstpointer key
)
410 g_return_val_if_fail (hash
!= NULL
, FALSE
);
412 equal
= hash
->key_equal_func
;
414 hashcode
= ((*hash
->hash_func
)(key
)) % hash
->table_size
;
416 for (s
= hash
->table
[hashcode
]; s
!= NULL
; s
= s
->next
){
417 if ((*equal
)(s
->key
, key
)){
418 if (hash
->key_destroy_func
!= NULL
)
419 (*hash
->key_destroy_func
)(s
->key
);
420 if (hash
->value_destroy_func
!= NULL
)
421 (*hash
->value_destroy_func
)(s
->value
);
423 hash
->table
[hashcode
] = s
->next
;
425 last
->next
= s
->next
;
438 g_hash_table_foreach_remove (GHashTable
*hash
, GHRFunc func
, gpointer user_data
)
443 g_return_val_if_fail (hash
!= NULL
, 0);
444 g_return_val_if_fail (func
!= NULL
, 0);
447 for (i
= 0; i
< hash
->table_size
; i
++){
451 for (s
= hash
->table
[i
]; s
!= NULL
; ){
452 if ((*func
)(s
->key
, s
->value
, user_data
)){
455 if (hash
->key_destroy_func
!= NULL
)
456 (*hash
->key_destroy_func
)(s
->key
);
457 if (hash
->value_destroy_func
!= NULL
)
458 (*hash
->value_destroy_func
)(s
->value
);
460 hash
->table
[i
] = s
->next
;
463 last
->next
= s
->next
;
483 g_hash_table_steal (GHashTable
*hash
, gconstpointer key
)
489 g_return_val_if_fail (hash
!= NULL
, FALSE
);
491 equal
= hash
->key_equal_func
;
493 hashcode
= ((*hash
->hash_func
)(key
)) % hash
->table_size
;
495 for (s
= hash
->table
[hashcode
]; s
!= NULL
; s
= s
->next
){
496 if ((*equal
)(s
->key
, key
)) {
498 hash
->table
[hashcode
] = s
->next
;
500 last
->next
= s
->next
;
514 g_hash_table_foreach_steal (GHashTable
*hash
, GHRFunc func
, gpointer user_data
)
519 g_return_val_if_fail (hash
!= NULL
, 0);
520 g_return_val_if_fail (func
!= NULL
, 0);
523 for (i
= 0; i
< hash
->table_size
; i
++){
527 for (s
= hash
->table
[i
]; s
!= NULL
; ){
528 if ((*func
)(s
->key
, s
->value
, user_data
)){
532 hash
->table
[i
] = s
->next
;
535 last
->next
= s
->next
;
555 g_hash_table_destroy (GHashTable
*hash
)
562 for (i
= 0; i
< hash
->table_size
; i
++){
565 for (s
= hash
->table
[i
]; s
!= NULL
; s
= next
){
568 if (hash
->key_destroy_func
!= NULL
)
569 (*hash
->key_destroy_func
)(s
->key
);
570 if (hash
->value_destroy_func
!= NULL
)
571 (*hash
->value_destroy_func
)(s
->value
);
575 g_free (hash
->table
);
581 g_hash_table_print_stats (GHashTable
*table
)
583 int i
, max_chain_index
, chain_size
, max_chain_size
;
587 max_chain_index
= -1;
588 for (i
= 0; i
< table
->table_size
; i
++) {
590 for (node
= table
->table
[i
]; node
; node
= node
->next
)
592 if (chain_size
> max_chain_size
) {
593 max_chain_size
= chain_size
;
598 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
);
602 g_hash_table_iter_init (GHashTableIter
*it
, GHashTable
*hash_table
)
604 Iter
*iter
= (Iter
*)it
;
606 memset (iter
, 0, sizeof (Iter
));
607 iter
->ht
= hash_table
;
608 iter
->slot_index
= -1;
611 gboolean
g_hash_table_iter_next (GHashTableIter
*it
, gpointer
*key
, gpointer
*value
)
613 Iter
*iter
= (Iter
*)it
;
615 GHashTable
*hash
= iter
->ht
;
617 g_assert (iter
->slot_index
!= -2);
618 g_assert (sizeof (Iter
) <= sizeof (GHashTableIter
));
623 if (iter
->slot_index
>= hash
->table_size
) {
624 iter
->slot_index
= -2;
627 if (hash
->table
[iter
->slot_index
])
630 iter
->slot
= hash
->table
[iter
->slot_index
];
634 *key
= iter
->slot
->key
;
636 *value
= iter
->slot
->value
;
637 iter
->slot
= iter
->slot
->next
;
643 g_direct_equal (gconstpointer v1
, gconstpointer v2
)
649 g_direct_hash (gconstpointer v1
)
651 return GPOINTER_TO_UINT (v1
);
655 g_int_equal (gconstpointer v1
, gconstpointer v2
)
657 return *(gint
*)v1
== *(gint
*)v2
;
661 g_int_hash (gconstpointer v1
)
667 g_str_equal (gconstpointer v1
, gconstpointer v2
)
669 return v1
== v2
|| strcmp ((const char*)v1
, (const char*)v2
) == 0;
673 g_str_hash (gconstpointer v1
)
676 unsigned char *p
= (unsigned char *) v1
;
679 hash
= (hash
<< 5) - (hash
+ *p
);