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;
50 GDestroyNotify value_destroy_func, key_destroy_func;
53 static int prime_tbl[] = {
54 11, 19, 37, 73, 109, 163, 251, 367, 557, 823, 1237,
55 1861, 2777, 4177, 6247, 9371, 14057, 21089, 31627,
56 47431, 71143, 106721, 160073, 240101, 360163,
57 540217, 810343, 1215497, 1823231, 2734867, 4102283,
58 6153409, 9230113, 13845163
66 for (n = 3; n< (int)sqrt (x); n += 2) {
72 // There is only one even prime - 2.
81 for (i = (x & (~1))-1; i< G_MAXINT32; i += 2) {
93 for (i = 0; i < G_N_ELEMENTS (prime_tbl); i++) {
94 if (x <= prime_tbl [i])
97 return calc_prime (x);
101 adjust_threshold (GHashTable *hash)
103 hash->threshold = (int) hash->table_size * 0.75;
104 if (hash->threshold >= hash->table_size)
105 hash->threshold = hash->table_size-1;
106 if (hash->threshold == 0)
111 g_hash_table_new (GHashFunc hash_func, GEqualFunc key_equal_func)
115 if (hash_func == NULL)
116 hash_func = g_direct_hash;
117 if (key_equal_func == NULL)
118 key_equal_func = g_direct_equal;
119 hash = g_new0 (GHashTable, 1);
121 hash->hash_func = hash_func;
122 hash->key_equal_func = key_equal_func;
124 hash->table_size = to_prime (1);
125 hash->table = g_new0 (Slot *, hash->table_size);
126 adjust_threshold (hash);
132 g_hash_table_new_full (GHashFunc hash_func, GEqualFunc key_equal_func,
133 GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func)
135 GHashTable *hash = g_hash_table_new (hash_func, key_equal_func);
139 hash->key_destroy_func = key_destroy_func;
140 hash->value_destroy_func = value_destroy_func;
146 rehash (GHashTable *hash)
152 g_hash_table_insert_replace (GHashTable *hash, gpointer key, gpointer value, gboolean replace)
158 g_return_if_fail (hash != NULL);
160 equal = hash->key_equal_func;
161 if (hash->in_use >= hash->threshold)
164 hashcode = ((*hash->hash_func) (key)) % hash->table_size;
165 for (s = hash->table [hashcode]; s != NULL; s = s->next){
166 if ((*equal) (s->key, key)){
168 if (hash->key_destroy_func != NULL)
169 (*hash->key_destroy_func)(s->key);
172 if (hash->value_destroy_func != NULL)
173 (*hash->value_destroy_func) (s->value);
181 s->next = hash->table [hashcode];
182 hash->table [hashcode] = s;
187 g_hash_table_size (GHashTable *hash)
189 g_return_val_if_fail (hash != NULL, 0);
195 g_hash_table_lookup (GHashTable *hash, gconstpointer key)
197 gpointer orig_key, value;
199 if (g_hash_table_lookup_extended (hash, key, &orig_key, &value))
206 g_hash_table_lookup_extended (GHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value)
212 g_return_val_if_fail (hash != NULL, FALSE);
213 equal = hash->key_equal_func;
215 hashcode = ((*hash->hash_func) (key)) % hash->table_size;
217 for (s = hash->table [hashcode]; s != NULL; s = s->next){
218 if ((*equal)(s->key, key)){
228 g_hash_table_foreach (GHashTable *hash, GHFunc func, gpointer user_data)
232 g_return_if_fail (hash != NULL);
233 g_return_if_fail (func != NULL);
235 for (i = 0; i < hash->table_size; i++){
238 for (s = hash->table [i]; s != NULL; s = s->next)
239 (*func)(s->key, s->value, user_data);
244 g_hash_table_find (GHashTable *hash, GHRFunc predicate, gpointer user_data)
248 g_return_val_if_fail (hash != NULL, NULL);
249 g_return_val_if_fail (predicate != NULL, NULL);
251 for (i = 0; i < hash->table_size; i++){
254 for (s = hash->table [i]; s != NULL; s = s->next)
255 if ((*predicate)(s->key, s->value, user_data))
262 g_hash_table_remove (GHashTable *hash, gconstpointer key)
268 g_return_val_if_fail (hash != NULL, FALSE);
269 equal = hash->key_equal_func;
271 hashcode = ((*hash->hash_func)(key)) % hash->table_size;
273 for (s = hash->table [hashcode]; s != NULL; s = s->next){
274 if ((*equal)(s->key, key)){
275 if (hash->key_destroy_func != NULL)
276 (*hash->key_destroy_func)(s->key);
277 if (hash->value_destroy_func != NULL)
278 (*hash->value_destroy_func)(s->value);
280 hash->table [hashcode] = s->next;
282 last->next = s->next;
293 g_hash_table_foreach_remove (GHashTable *hash, GHRFunc func, gpointer user_data)
298 g_return_val_if_fail (hash != NULL, 0);
299 g_return_val_if_fail (func != NULL, 0);
301 for (i = 0; i < hash->table_size; i++){
305 for (s = hash->table [i]; s != NULL; ){
306 if ((*func)(s->key, s->value, user_data)){
309 if (hash->key_destroy_func != NULL)
310 (*hash->key_destroy_func)(s->key);
311 if (hash->value_destroy_func != NULL)
312 (*hash->value_destroy_func)(s->value);
314 hash->table [i] = s->next;
317 last->next = s->next;
336 g_hash_table_destroy (GHashTable *hash)
340 g_return_if_fail (hash != NULL);
342 for (i = 0; i < hash->table_size; i++){
345 for (s = hash->table [i]; s != NULL; s = next){
348 if (hash->key_destroy_func != NULL)
349 (*hash->key_destroy_func)(s->key);
350 if (hash->value_destroy_func != NULL)
351 (*hash->value_destroy_func)(s->value);
355 g_free (hash->table);
361 g_direct_equal (gconstpointer v1, gconstpointer v2)
367 g_direct_hash (gconstpointer v1)
369 return GPOINTER_TO_UINT (v1);
373 g_int_equal (gconstpointer v1, gconstpointer v2)
375 return GPOINTER_TO_INT (v1) == GPOINTER_TO_INT (v2);
379 g_int_hash (gconstpointer v1)
381 return GPOINTER_TO_UINT(v1);
385 g_str_equal (gconstpointer v1, gconstpointer v2)
387 return strcmp (v1, v2) == 0;
391 g_str_hash (gconstpointer v1)
394 char *p = (char *) v1;
397 hash = (hash << 5) - (hash + *p);