1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
38 #define HASH_TABLE_MIN_SIZE 11
39 #define HASH_TABLE_MAX_SIZE 13845163
42 typedef struct _GHashNode GHashNode;
57 GEqualFunc key_equal_func;
58 GDestroyNotify key_destroy_func;
59 GDestroyNotify value_destroy_func;
62 #define G_HASH_TABLE_RESIZE(hash_table) \
64 if ((hash_table->size >= 3 * hash_table->nnodes && \
65 hash_table->size > HASH_TABLE_MIN_SIZE) || \
66 (3 * hash_table->size <= hash_table->nnodes && \
67 hash_table->size < HASH_TABLE_MAX_SIZE)) \
68 g_hash_table_resize (hash_table); \
71 static void g_hash_table_resize (GHashTable *hash_table);
72 static GHashNode** g_hash_table_lookup_node (GHashTable *hash_table,
74 static GHashNode* g_hash_node_new (gpointer key,
76 static void g_hash_node_destroy (GHashNode *hash_node,
77 GDestroyNotify key_destroy_func,
78 GDestroyNotify value_destroy_func);
79 static void g_hash_nodes_destroy (GHashNode *hash_node,
80 GDestroyNotify key_destroy_func,
81 GDestroyNotify value_destroy_func);
82 static guint g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
88 #ifndef DISABLE_MEM_POOLS
89 G_LOCK_DEFINE_STATIC (g_hash_global);
91 static GMemChunk *node_mem_chunk = NULL;
92 static GHashNode *node_free_list = NULL;
97 * @hash_func: a function to create a hash value from a key.
98 * Hash values are used to determine where keys are stored within the
99 * #GHashTable data structure. The g_direct_hash(), g_int_hash() and
100 * g_str_hash() functions are provided for some common types of keys.
101 * If hash_func is %NULL, g_direct_hash() is used.
102 * @key_equal_func: a function to check two keys for equality. This is
103 * used when looking up keys in the #GHashTable. The g_direct_equal(),
104 * g_int_equal() and g_str_equal() functions are provided for the most
105 * common types of keys. If @key_equal_func is %NULL, keys are compared
106 * directly in a similar fashion to g_direct_equal(), but without the
107 * overhead of a function call.
109 * Creates a new #GHashTable.
111 * Return value: a new #GHashTable.
114 g_hash_table_new (GHashFunc hash_func,
115 GEqualFunc key_equal_func)
117 return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
122 * g_hash_table_new_full:
123 * @hash_func: a function to create a hash value from a key.
124 * @key_equal_func: a function to check two keys for equality.
125 * @key_destroy_func: a function to free the memory allocated for the key
126 * used when removing the entry from the #GHashTable or %NULL if you
127 * don't want to supply such a function.
128 * @value_destroy_func: a function to free the memory allocated for the
129 * value used when removing the entry from the #GHashTable or %NULL if
130 * you don't want to supply such a function.
132 * Creates a new #GHashTable like g_hash_table_new() and allows to specify
133 * functions to free the memory allocated for the key and value that get
134 * called when removing the entry from the #GHashTable.
136 * Return value: a new #GHashTable.
139 g_hash_table_new_full (GHashFunc hash_func,
140 GEqualFunc key_equal_func,
141 GDestroyNotify key_destroy_func,
142 GDestroyNotify value_destroy_func)
144 GHashTable *hash_table;
147 hash_table = g_new (GHashTable, 1);
148 hash_table->size = HASH_TABLE_MIN_SIZE;
149 hash_table->nnodes = 0;
150 hash_table->hash_func = hash_func ? hash_func : g_direct_hash;
151 hash_table->key_equal_func = key_equal_func;
152 hash_table->key_destroy_func = key_destroy_func;
153 hash_table->value_destroy_func = value_destroy_func;
154 hash_table->nodes = g_new (GHashNode*, hash_table->size);
156 for (i = 0; i < hash_table->size; i++)
157 hash_table->nodes[i] = NULL;
163 * g_hash_table_destroy:
164 * @hash_table: a #GHashTable.
166 * Destroys the #GHashTable. If keys and/or values are dynamically
167 * allocated, you should either free them first or create the #GHashTable
168 * using g_hash_table_new_full(). In the latter case the destroy functions
169 * you supplied will be called on all keys and values before destroying
173 g_hash_table_destroy (GHashTable *hash_table)
177 g_return_if_fail (hash_table != NULL);
179 for (i = 0; i < hash_table->size; i++)
180 g_hash_nodes_destroy (hash_table->nodes[i],
181 hash_table->key_destroy_func,
182 hash_table->value_destroy_func);
184 g_free (hash_table->nodes);
188 static inline GHashNode**
189 g_hash_table_lookup_node (GHashTable *hash_table,
194 node = &hash_table->nodes
195 [(* hash_table->hash_func) (key) % hash_table->size];
197 /* Hash table lookup needs to be fast.
198 * We therefore remove the extra conditional of testing
199 * whether to call the key_equal_func or not from
202 if (hash_table->key_equal_func)
203 while (*node && !(*hash_table->key_equal_func) ((*node)->key, key))
204 node = &(*node)->next;
206 while (*node && (*node)->key != key)
207 node = &(*node)->next;
213 * g_hash_table_lookup:
214 * @hash_table: a #GHashTable.
215 * @key: the key to look up.
217 * Looks up a key in a #GHashTable.
219 * Return value: the associated value, or %NULL if the key is not found.
222 g_hash_table_lookup (GHashTable *hash_table,
227 g_return_val_if_fail (hash_table != NULL, NULL);
229 node = *g_hash_table_lookup_node (hash_table, key);
231 return node ? node->value : NULL;
235 * g_hash_table_lookup_extended:
236 * @hash_table: a #GHashTable.
237 * @lookup_key: the key to look up.
238 * @orig_key: returns the original key.
239 * @value: returns the value associated with the key.
241 * Looks up a key in the #GHashTable, returning the original key and the
242 * associated value and a #gboolean which is %TRUE if the key was found. This
243 * is useful if you need to free the memory allocated for the original key,
244 * for example before calling g_hash_table_remove().
246 * Return value: %TRUE if the key was found in the #GHashTable.
249 g_hash_table_lookup_extended (GHashTable *hash_table,
250 gconstpointer lookup_key,
256 g_return_val_if_fail (hash_table != NULL, FALSE);
258 node = *g_hash_table_lookup_node (hash_table, lookup_key);
263 *orig_key = node->key;
265 *value = node->value;
273 * g_hash_table_insert:
274 * @hash_table: a #GHashTable.
275 * @key: a key to insert.
276 * @value: the value to associate with the key.
278 * Inserts a new key and value into a #GHashTable.
280 * If the key already exists in the #GHashTable its current value is replaced
281 * with the new value. If you supplied a @value_destroy_func when creating the
282 * #GHashTable, the old value is freed using that function. If you supplied
283 * a @key_destroy_func when creating the #GHashTable, the passed key is freed
284 * using that function.
287 g_hash_table_insert (GHashTable *hash_table,
293 g_return_if_fail (hash_table != NULL);
295 node = g_hash_table_lookup_node (hash_table, key);
299 /* do not reset node->key in this place, keeping
300 * the old key is the intended behaviour.
301 * g_hash_table_replace() can be used instead.
304 /* free the passed key */
305 if (hash_table->key_destroy_func)
306 hash_table->key_destroy_func (key);
308 if (hash_table->value_destroy_func)
309 hash_table->value_destroy_func ((*node)->value);
311 (*node)->value = value;
315 *node = g_hash_node_new (key, value);
316 hash_table->nnodes++;
317 G_HASH_TABLE_RESIZE (hash_table);
322 * g_hash_table_replace:
323 * @hash_table: a #GHashTable.
324 * @key: a key to insert.
325 * @value: the value to associate with the key.
327 * Inserts a new key and value into a #GHashTable similar to
328 * g_hash_table_insert(). The difference is that if the key already exists
329 * in the #GHashTable, it gets replaced by the new key. If you supplied a
330 * @value_destroy_func when creating the #GHashTable, the old value is freed
331 * using that function. If you supplied a @key_destroy_func when creating the
332 * #GHashTable, the old key is freed using that function.
335 g_hash_table_replace (GHashTable *hash_table,
341 g_return_if_fail (hash_table != NULL);
343 node = g_hash_table_lookup_node (hash_table, key);
347 if (hash_table->key_destroy_func)
348 hash_table->key_destroy_func ((*node)->key);
350 if (hash_table->value_destroy_func)
351 hash_table->value_destroy_func ((*node)->value);
354 (*node)->value = value;
358 *node = g_hash_node_new (key, value);
359 hash_table->nnodes++;
360 G_HASH_TABLE_RESIZE (hash_table);
365 * g_hash_table_remove:
366 * @hash_table: a #GHashTable.
367 * @key: the key to remove.
369 * Removes a key and its associated value from a #GHashTable.
371 * If the #GHashTable was created using g_hash_table_new_full(), the
372 * key and value are freed using the supplied destroy functions, otherwise
373 * you have to make sure that any dynamically allocated values are freed
376 * Return value: %TRUE if the key was found and removed from the #GHashTable.
379 g_hash_table_remove (GHashTable *hash_table,
382 GHashNode **node, *dest;
384 g_return_val_if_fail (hash_table != NULL, FALSE);
386 node = g_hash_table_lookup_node (hash_table, key);
390 (*node) = dest->next;
391 g_hash_node_destroy (dest,
392 hash_table->key_destroy_func,
393 hash_table->value_destroy_func);
394 hash_table->nnodes--;
396 G_HASH_TABLE_RESIZE (hash_table);
405 * g_hash_table_steal:
406 * @hash_table: a #GHashTable.
407 * @key: the key to remove.
409 * Removes a key and its associated value from a #GHashTable without
410 * calling the key and value destroy functions.
412 * Return value: %TRUE if the key was found and removed from the #GHashTable.
415 g_hash_table_steal (GHashTable *hash_table,
418 GHashNode **node, *dest;
420 g_return_val_if_fail (hash_table != NULL, FALSE);
422 node = g_hash_table_lookup_node (hash_table, key);
426 (*node) = dest->next;
427 g_hash_node_destroy (dest, NULL, NULL);
428 hash_table->nnodes--;
430 G_HASH_TABLE_RESIZE (hash_table);
439 * g_hash_table_foreach_remove:
440 * @hash_table: a #GHashTable.
441 * @func: the function to call for each key/value pair.
442 * @user_data: user data to pass to the function.
444 * Calls the given function for each key/value pair in the #GHashTable.
445 * If the function returns %TRUE, then the key/value pair is removed from the
446 * #GHashTable. If you supplied key or value destroy functions when creating
447 * the #GHashTable, they are used to free the memory allocated for the removed
450 * Return value: the number of key/value pairs removed.
453 g_hash_table_foreach_remove (GHashTable *hash_table,
457 g_return_val_if_fail (hash_table != NULL, 0);
458 g_return_val_if_fail (func != NULL, 0);
460 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
464 * g_hash_table_foreach_steal:
465 * @hash_table: a #GHashTable.
466 * @func: the function to call for each key/value pair.
467 * @user_data: user data to pass to the function.
469 * Calls the given function for each key/value pair in the #GHashTable.
470 * If the function returns %TRUE, then the key/value pair is removed from the
471 * #GHashTable, but no key or value destroy functions are called.
473 * Return value: the number of key/value pairs removed.
476 g_hash_table_foreach_steal (GHashTable *hash_table,
480 g_return_val_if_fail (hash_table != NULL, 0);
481 g_return_val_if_fail (func != NULL, 0);
483 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
487 g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
492 GHashNode *node, *prev;
496 for (i = 0; i < hash_table->size; i++)
502 for (node = hash_table->nodes[i]; node; prev = node, node = node->next)
504 if ((* func) (node->key, node->value, user_data))
508 hash_table->nnodes -= 1;
512 prev->next = node->next;
513 g_hash_node_destroy (node,
514 notify ? hash_table->key_destroy_func : NULL,
515 notify ? hash_table->value_destroy_func : NULL);
520 hash_table->nodes[i] = node->next;
521 g_hash_node_destroy (node,
522 notify ? hash_table->key_destroy_func : NULL,
523 notify ? hash_table->value_destroy_func : NULL);
530 G_HASH_TABLE_RESIZE (hash_table);
536 * g_hash_table_foreach:
537 * @hash_table: a #GHashTable.
538 * @func: the function to call for each key/value pair.
539 * @user_data: user data to pass to the function.
541 * Calls the given function for each of the key/value pairs in the
542 * #GHashTable. The function is passed the key and value of each
543 * pair, and the given @user_data parameter. The hash table may not
544 * be modified while iterating over it (you can't add/remove
545 * items). To remove all items matching a predicate, use
546 * g_hash_table_remove().
549 g_hash_table_foreach (GHashTable *hash_table,
556 g_return_if_fail (hash_table != NULL);
557 g_return_if_fail (func != NULL);
559 for (i = 0; i < hash_table->size; i++)
560 for (node = hash_table->nodes[i]; node; node = node->next)
561 (* func) (node->key, node->value, user_data);
566 * @hash_table: a #GHashTable.
568 * Returns the number of elements contained in the #GHashTable.
570 * Return value: the number of key/value pairs in the #GHashTable.
573 g_hash_table_size (GHashTable *hash_table)
575 g_return_val_if_fail (hash_table != NULL, 0);
577 return hash_table->nnodes;
581 g_hash_table_resize (GHashTable *hash_table)
583 GHashNode **new_nodes;
590 new_size = g_spaced_primes_closest (hash_table->nnodes);
591 new_size = CLAMP (new_size, HASH_TABLE_MIN_SIZE, HASH_TABLE_MAX_SIZE);
593 new_nodes = g_new0 (GHashNode*, new_size);
595 for (i = 0; i < hash_table->size; i++)
596 for (node = hash_table->nodes[i]; node; node = next)
600 hash_val = (* hash_table->hash_func) (node->key) % new_size;
602 node->next = new_nodes[hash_val];
603 new_nodes[hash_val] = node;
606 g_free (hash_table->nodes);
607 hash_table->nodes = new_nodes;
608 hash_table->size = new_size;
612 g_hash_node_new (gpointer key,
615 GHashNode *hash_node;
617 #ifdef DISABLE_MEM_POOLS
618 hash_node = g_new (GHashNode, 1);
620 G_LOCK (g_hash_global);
623 hash_node = node_free_list;
624 node_free_list = node_free_list->next;
629 node_mem_chunk = g_mem_chunk_new ("hash node mem chunk",
633 hash_node = g_chunk_new (GHashNode, node_mem_chunk);
635 G_UNLOCK (g_hash_global);
638 hash_node->key = key;
639 hash_node->value = value;
640 hash_node->next = NULL;
646 g_hash_node_destroy (GHashNode *hash_node,
647 GDestroyNotify key_destroy_func,
648 GDestroyNotify value_destroy_func)
650 if (key_destroy_func)
651 key_destroy_func (hash_node->key);
652 if (value_destroy_func)
653 value_destroy_func (hash_node->value);
655 #ifdef ENABLE_GC_FRIENDLY
656 hash_node->key = NULL;
657 hash_node->value = NULL;
658 #endif /* ENABLE_GC_FRIENDLY */
660 #ifdef DISABLE_MEM_POOLS
663 G_LOCK (g_hash_global);
664 hash_node->next = node_free_list;
665 node_free_list = hash_node;
666 G_UNLOCK (g_hash_global);
671 g_hash_nodes_destroy (GHashNode *hash_node,
672 GFreeFunc key_destroy_func,
673 GFreeFunc value_destroy_func)
675 #ifdef DISABLE_MEM_POOLS
678 GHashNode *next = hash_node->next;
680 if (key_destroy_func)
681 key_destroy_func (hash_node->key);
682 if (value_destroy_func)
683 value_destroy_func (hash_node->value);
691 GHashNode *node = hash_node;
695 if (key_destroy_func)
696 key_destroy_func (node->key);
697 if (value_destroy_func)
698 value_destroy_func (node->value);
700 #ifdef ENABLE_GC_FRIENDLY
703 #endif /* ENABLE_GC_FRIENDLY */
708 if (key_destroy_func)
709 key_destroy_func (node->key);
710 if (value_destroy_func)
711 value_destroy_func (node->value);
713 #ifdef ENABLE_GC_FRIENDLY
716 #endif /* ENABLE_GC_FRIENDLY */
718 G_LOCK (g_hash_global);
719 node->next = node_free_list;
720 node_free_list = hash_node;
721 G_UNLOCK (g_hash_global);