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_lookup (GHashTable
*hash
, gconstpointer key
)
311 gpointer orig_key
, value
;
313 if (g_hash_table_lookup_extended (hash
, key
, &orig_key
, &value
))
320 g_hash_table_lookup_extended (GHashTable
*hash
, gconstpointer key
, gpointer
*orig_key
, gpointer
*value
)
326 g_return_val_if_fail (hash
!= NULL
, FALSE
);
328 equal
= hash
->key_equal_func
;
330 hashcode
= ((*hash
->hash_func
) (key
)) % hash
->table_size
;
332 for (s
= hash
->table
[hashcode
]; s
!= NULL
; s
= s
->next
){
333 if ((*equal
)(s
->key
, key
)){
345 g_hash_table_foreach (GHashTable
*hash
, GHFunc func
, gpointer user_data
)
349 g_return_if_fail (hash
!= NULL
);
350 g_return_if_fail (func
!= NULL
);
352 for (i
= 0; i
< hash
->table_size
; i
++){
355 for (s
= hash
->table
[i
]; s
!= NULL
; s
= s
->next
)
356 (*func
)(s
->key
, s
->value
, user_data
);
361 g_hash_table_find (GHashTable
*hash
, GHRFunc predicate
, gpointer user_data
)
365 g_return_val_if_fail (hash
!= NULL
, NULL
);
366 g_return_val_if_fail (predicate
!= NULL
, NULL
);
368 for (i
= 0; i
< hash
->table_size
; i
++){
371 for (s
= hash
->table
[i
]; s
!= NULL
; s
= s
->next
)
372 if ((*predicate
)(s
->key
, s
->value
, user_data
))
379 g_hash_table_remove_all (GHashTable
*hash
)
383 g_return_if_fail (hash
!= NULL
);
385 for (i
= 0; i
< hash
->table_size
; i
++){
388 while (hash
->table
[i
]) {
390 g_hash_table_remove (hash
, s
->key
);
396 g_hash_table_remove (GHashTable
*hash
, gconstpointer key
)
402 g_return_val_if_fail (hash
!= NULL
, FALSE
);
404 equal
= hash
->key_equal_func
;
406 hashcode
= ((*hash
->hash_func
)(key
)) % hash
->table_size
;
408 for (s
= hash
->table
[hashcode
]; s
!= NULL
; s
= s
->next
){
409 if ((*equal
)(s
->key
, key
)){
410 if (hash
->key_destroy_func
!= NULL
)
411 (*hash
->key_destroy_func
)(s
->key
);
412 if (hash
->value_destroy_func
!= NULL
)
413 (*hash
->value_destroy_func
)(s
->value
);
415 hash
->table
[hashcode
] = s
->next
;
417 last
->next
= s
->next
;
430 g_hash_table_foreach_remove (GHashTable
*hash
, GHRFunc func
, gpointer user_data
)
435 g_return_val_if_fail (hash
!= NULL
, 0);
436 g_return_val_if_fail (func
!= NULL
, 0);
439 for (i
= 0; i
< hash
->table_size
; i
++){
443 for (s
= hash
->table
[i
]; s
!= NULL
; ){
444 if ((*func
)(s
->key
, s
->value
, user_data
)){
447 if (hash
->key_destroy_func
!= NULL
)
448 (*hash
->key_destroy_func
)(s
->key
);
449 if (hash
->value_destroy_func
!= NULL
)
450 (*hash
->value_destroy_func
)(s
->value
);
452 hash
->table
[i
] = s
->next
;
455 last
->next
= s
->next
;
475 g_hash_table_steal (GHashTable
*hash
, gconstpointer key
)
481 g_return_val_if_fail (hash
!= NULL
, FALSE
);
483 equal
= hash
->key_equal_func
;
485 hashcode
= ((*hash
->hash_func
)(key
)) % hash
->table_size
;
487 for (s
= hash
->table
[hashcode
]; s
!= NULL
; s
= s
->next
){
488 if ((*equal
)(s
->key
, key
)) {
490 hash
->table
[hashcode
] = s
->next
;
492 last
->next
= s
->next
;
506 g_hash_table_foreach_steal (GHashTable
*hash
, GHRFunc func
, gpointer user_data
)
511 g_return_val_if_fail (hash
!= NULL
, 0);
512 g_return_val_if_fail (func
!= NULL
, 0);
515 for (i
= 0; i
< hash
->table_size
; i
++){
519 for (s
= hash
->table
[i
]; s
!= NULL
; ){
520 if ((*func
)(s
->key
, s
->value
, user_data
)){
524 hash
->table
[i
] = s
->next
;
527 last
->next
= s
->next
;
547 g_hash_table_destroy (GHashTable
*hash
)
554 for (i
= 0; i
< hash
->table_size
; i
++){
557 for (s
= hash
->table
[i
]; s
!= NULL
; s
= next
){
560 if (hash
->key_destroy_func
!= NULL
)
561 (*hash
->key_destroy_func
)(s
->key
);
562 if (hash
->value_destroy_func
!= NULL
)
563 (*hash
->value_destroy_func
)(s
->value
);
567 g_free (hash
->table
);
573 g_hash_table_print_stats (GHashTable
*table
)
575 int i
, max_chain_index
, chain_size
, max_chain_size
;
579 max_chain_index
= -1;
580 for (i
= 0; i
< table
->table_size
; i
++) {
582 for (node
= table
->table
[i
]; node
; node
= node
->next
)
584 if (chain_size
> max_chain_size
) {
585 max_chain_size
= chain_size
;
590 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
);
594 g_hash_table_iter_init (GHashTableIter
*it
, GHashTable
*hash_table
)
596 Iter
*iter
= (Iter
*)it
;
598 memset (iter
, 0, sizeof (Iter
));
599 iter
->ht
= hash_table
;
600 iter
->slot_index
= -1;
603 gboolean
g_hash_table_iter_next (GHashTableIter
*it
, gpointer
*key
, gpointer
*value
)
605 Iter
*iter
= (Iter
*)it
;
607 GHashTable
*hash
= iter
->ht
;
609 g_assert (iter
->slot_index
!= -2);
610 g_assert (sizeof (Iter
) <= sizeof (GHashTableIter
));
615 if (iter
->slot_index
>= hash
->table_size
) {
616 iter
->slot_index
= -2;
619 if (hash
->table
[iter
->slot_index
])
622 iter
->slot
= hash
->table
[iter
->slot_index
];
626 *key
= iter
->slot
->key
;
628 *value
= iter
->slot
->value
;
629 iter
->slot
= iter
->slot
->next
;
635 g_direct_equal (gconstpointer v1
, gconstpointer v2
)
641 g_direct_hash (gconstpointer v1
)
643 return GPOINTER_TO_UINT (v1
);
647 g_int_equal (gconstpointer v1
, gconstpointer v2
)
649 return *(gint
*)v1
== *(gint
*)v2
;
653 g_int_hash (gconstpointer v1
)
659 g_str_equal (gconstpointer v1
, gconstpointer v2
)
661 return v1
== v2
|| strcmp ((const char*)v1
, (const char*)v2
) == 0;
665 g_str_hash (gconstpointer v1
)
668 char *p
= (char *) v1
;
671 hash
= (hash
<< 5) - (hash
+ *p
);