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
);
154 #ifdef SANITY_CHECKstatic void
155 sanity_check (GHashTable
*hash
)
159 for (i
= 0; i
< hash
->table_size
; i
++) {
162 for (s
= hash
->table
[i
]; s
!= NULL
; s
= s
->next
){
163 guint hashcode
= (*hash
->hash_func
) (s
->key
);
164 guint slot
= (hashcode
) % hash
->table_size
;
166 dump_hashcode_func
= 1;
167 hashcode
= (*hash
->hash_func
) (s
->key
);
168 dump_hashcode_func
= 0;
169 g_error ("Key %p (bucket %d) on invalid bucket %d (hashcode %x) (tb size %d)", s
->key
, slot
, i
, hashcode
, hash
->table_size
);
176 #define sanity_check(HASH) do {}while(0)
181 do_rehash (GHashTable
*hash
)
186 /* printf ("Resizing diff=%d slots=%d\n", hash->in_use - hash->last_rehash, hash->table_size); */
187 hash
->last_rehash
= hash
->table_size
;
188 current_size
= hash
->table_size
;
189 hash
->table_size
= g_spaced_primes_closest (hash
->in_use
);
190 /* printf ("New size: %d\n", hash->table_size); */
192 hash
->table
= g_new0 (Slot
*, hash
->table_size
);
194 for (i
= 0; i
< current_size
; i
++){
197 for (s
= table
[i
]; s
!= NULL
; s
= next
){
198 guint hashcode
= ((*hash
->hash_func
) (s
->key
)) % hash
->table_size
;
201 s
->next
= hash
->table
[hashcode
];
202 hash
->table
[hashcode
] = s
;
209 rehash (GHashTable
*hash
)
211 int diff
= ABS (hash
->last_rehash
- hash
->in_use
);
213 /* These are the factors to play with to change the rehashing strategy */
214 /* I played with them with a large range, and could not really get */
215 /* something that was too good, maybe the tests are not that great */
216 if (!(diff
* 0.75 > hash
->table_size
* 2))
223 g_hash_table_insert_replace (GHashTable
*hash
, gpointer key
, gpointer value
, gboolean replace
)
229 g_return_if_fail (hash
!= NULL
);
232 equal
= hash
->key_equal_func
;
233 if (hash
->in_use
>= hash
->threshold
)
236 hashcode
= ((*hash
->hash_func
) (key
)) % hash
->table_size
;
237 for (s
= hash
->table
[hashcode
]; s
!= NULL
; s
= s
->next
){
238 if ((*equal
) (s
->key
, key
)){
240 if (hash
->key_destroy_func
!= NULL
)
241 (*hash
->key_destroy_func
)(s
->key
);
244 if (hash
->value_destroy_func
!= NULL
)
245 (*hash
->value_destroy_func
) (s
->value
);
254 s
->next
= hash
->table
[hashcode
];
255 hash
->table
[hashcode
] = s
;
261 g_hash_table_size (GHashTable
*hash
)
263 g_return_val_if_fail (hash
!= NULL
, 0);
269 g_hash_table_lookup (GHashTable
*hash
, gconstpointer key
)
271 gpointer orig_key
, value
;
273 if (g_hash_table_lookup_extended (hash
, key
, &orig_key
, &value
))
280 g_hash_table_lookup_extended (GHashTable
*hash
, gconstpointer key
, gpointer
*orig_key
, gpointer
*value
)
286 g_return_val_if_fail (hash
!= NULL
, FALSE
);
288 equal
= hash
->key_equal_func
;
290 hashcode
= ((*hash
->hash_func
) (key
)) % hash
->table_size
;
292 for (s
= hash
->table
[hashcode
]; s
!= NULL
; s
= s
->next
){
293 if ((*equal
)(s
->key
, key
)){
303 g_hash_table_foreach (GHashTable
*hash
, GHFunc func
, gpointer user_data
)
307 g_return_if_fail (hash
!= NULL
);
308 g_return_if_fail (func
!= NULL
);
310 for (i
= 0; i
< hash
->table_size
; i
++){
313 for (s
= hash
->table
[i
]; s
!= NULL
; s
= s
->next
)
314 (*func
)(s
->key
, s
->value
, user_data
);
319 g_hash_table_find (GHashTable
*hash
, GHRFunc predicate
, gpointer user_data
)
323 g_return_val_if_fail (hash
!= NULL
, NULL
);
324 g_return_val_if_fail (predicate
!= NULL
, NULL
);
326 for (i
= 0; i
< hash
->table_size
; i
++){
329 for (s
= hash
->table
[i
]; s
!= NULL
; s
= s
->next
)
330 if ((*predicate
)(s
->key
, s
->value
, user_data
))
337 g_hash_table_remove (GHashTable
*hash
, gconstpointer key
)
343 g_return_val_if_fail (hash
!= NULL
, FALSE
);
345 equal
= hash
->key_equal_func
;
347 hashcode
= ((*hash
->hash_func
)(key
)) % hash
->table_size
;
349 for (s
= hash
->table
[hashcode
]; s
!= NULL
; s
= s
->next
){
350 if ((*equal
)(s
->key
, key
)){
351 if (hash
->key_destroy_func
!= NULL
)
352 (*hash
->key_destroy_func
)(s
->key
);
353 if (hash
->value_destroy_func
!= NULL
)
354 (*hash
->value_destroy_func
)(s
->value
);
356 hash
->table
[hashcode
] = s
->next
;
358 last
->next
= s
->next
;
371 g_hash_table_foreach_remove (GHashTable
*hash
, GHRFunc func
, gpointer user_data
)
376 g_return_val_if_fail (hash
!= NULL
, 0);
377 g_return_val_if_fail (func
!= NULL
, 0);
380 for (i
= 0; i
< hash
->table_size
; i
++){
384 for (s
= hash
->table
[i
]; s
!= NULL
; ){
385 if ((*func
)(s
->key
, s
->value
, user_data
)){
388 if (hash
->key_destroy_func
!= NULL
)
389 (*hash
->key_destroy_func
)(s
->key
);
390 if (hash
->value_destroy_func
!= NULL
)
391 (*hash
->value_destroy_func
)(s
->value
);
393 hash
->table
[i
] = s
->next
;
396 last
->next
= s
->next
;
416 g_hash_table_foreach_steal (GHashTable
*hash
, GHRFunc func
, gpointer user_data
)
421 g_return_val_if_fail (hash
!= NULL
, 0);
422 g_return_val_if_fail (func
!= NULL
, 0);
425 for (i
= 0; i
< hash
->table_size
; i
++){
429 for (s
= hash
->table
[i
]; s
!= NULL
; ){
430 if ((*func
)(s
->key
, s
->value
, user_data
)){
434 hash
->table
[i
] = s
->next
;
437 last
->next
= s
->next
;
457 g_hash_table_destroy (GHashTable
*hash
)
461 g_return_if_fail (hash
!= NULL
);
463 for (i
= 0; i
< hash
->table_size
; i
++){
466 for (s
= hash
->table
[i
]; s
!= NULL
; s
= next
){
469 if (hash
->key_destroy_func
!= NULL
)
470 (*hash
->key_destroy_func
)(s
->key
);
471 if (hash
->value_destroy_func
!= NULL
)
472 (*hash
->value_destroy_func
)(s
->value
);
476 g_free (hash
->table
);
482 g_direct_equal (gconstpointer v1
, gconstpointer v2
)
488 g_direct_hash (gconstpointer v1
)
490 return GPOINTER_TO_UINT (v1
);
494 g_int_equal (gconstpointer v1
, gconstpointer v2
)
496 return GPOINTER_TO_INT (v1
) == GPOINTER_TO_INT (v2
);
500 g_int_hash (gconstpointer v1
)
502 return GPOINTER_TO_UINT(v1
);
506 g_str_equal (gconstpointer v1
, gconstpointer v2
)
508 return strcmp (v1
, v2
) == 0;
512 g_str_hash (gconstpointer v1
)
515 char *p
= (char *) v1
;
518 hash
= (hash
<< 5) - (hash
+ *p
);