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/.
37 #define HASH_TABLE_MIN_SIZE 11
38 #define HASH_TABLE_MAX_SIZE 13845163
41 typedef struct _GHashNode GHashNode;
57 GEqualFunc key_equal_func;
58 volatile gint ref_count;
59 GDestroyNotify key_destroy_func;
60 GDestroyNotify value_destroy_func;
63 #define G_HASH_TABLE_RESIZE(hash_table) \
65 if ((hash_table->size >= 3 * hash_table->nnodes && \
66 hash_table->size > HASH_TABLE_MIN_SIZE) || \
67 (3 * hash_table->size <= hash_table->nnodes && \
68 hash_table->size < HASH_TABLE_MAX_SIZE)) \
69 g_hash_table_resize (hash_table); \
72 static void g_hash_table_resize (GHashTable *hash_table);
73 static GHashNode** g_hash_table_lookup_node (GHashTable *hash_table,
76 static GHashNode* g_hash_node_new (gpointer key,
79 static guint g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
83 static void g_hash_table_remove_all_nodes (GHashTable *hash_table,
89 * @hash_func: a function to create a hash value from a key.
90 * Hash values are used to determine where keys are stored within the
91 * #GHashTable data structure. The g_direct_hash(), g_int_hash() and
92 * g_str_hash() functions are provided for some common types of keys.
93 * If hash_func is %NULL, g_direct_hash() is used.
94 * @key_equal_func: a function to check two keys for equality. This is
95 * used when looking up keys in the #GHashTable. The g_direct_equal(),
96 * g_int_equal() and g_str_equal() functions are provided for the most
97 * common types of keys. If @key_equal_func is %NULL, keys are compared
98 * directly in a similar fashion to g_direct_equal(), but without the
99 * overhead of a function call.
101 * Creates a new #GHashTable with a reference count of 1.
103 * Return value: a new #GHashTable.
106 g_hash_table_new (GHashFunc hash_func,
107 GEqualFunc key_equal_func)
109 return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
114 * g_hash_table_new_full:
115 * @hash_func: a function to create a hash value from a key.
116 * @key_equal_func: a function to check two keys for equality.
117 * @key_destroy_func: a function to free the memory allocated for the key
118 * used when removing the entry from the #GHashTable or %NULL if you
119 * don't want to supply such a function.
120 * @value_destroy_func: a function to free the memory allocated for the
121 * value used when removing the entry from the #GHashTable or %NULL if
122 * you don't want to supply such a function.
124 * Creates a new #GHashTable like g_hash_table_new() with a reference count
125 * of 1 and allows to specify functions to free the memory allocated for the
126 * key and value that get called when removing the entry from the #GHashTable.
128 * Return value: a new #GHashTable.
131 g_hash_table_new_full (GHashFunc hash_func,
132 GEqualFunc key_equal_func,
133 GDestroyNotify key_destroy_func,
134 GDestroyNotify value_destroy_func)
136 GHashTable *hash_table;
138 hash_table = g_slice_new (GHashTable);
139 hash_table->size = HASH_TABLE_MIN_SIZE;
140 hash_table->nnodes = 0;
141 hash_table->hash_func = hash_func ? hash_func : g_direct_hash;
142 hash_table->key_equal_func = key_equal_func;
143 hash_table->ref_count = 1;
144 hash_table->key_destroy_func = key_destroy_func;
145 hash_table->value_destroy_func = value_destroy_func;
146 hash_table->nodes = g_new0 (GHashNode*, hash_table->size);
154 * @hash_table: a valid #GHashTable.
156 * Atomically increments the reference count of @hash_table by one.
157 * This function is MT-safe and may be called from any thread.
159 * Return value: the passed in #GHashTable.
164 g_hash_table_ref (GHashTable *hash_table)
166 g_return_val_if_fail (hash_table != NULL, NULL);
167 g_return_val_if_fail (hash_table->ref_count > 0, hash_table);
169 g_atomic_int_add (&hash_table->ref_count, 1);
174 * g_hash_table_unref:
175 * @hash_table: a valid #GHashTable.
177 * Atomically decrements the reference count of @hash_table by one.
178 * If the reference count drops to 0, all keys and values will be
179 * destroyed, and all memory allocated by the hash table is released.
180 * This function is MT-safe and may be called from any thread.
185 g_hash_table_unref (GHashTable *hash_table)
187 g_return_if_fail (hash_table != NULL);
188 g_return_if_fail (hash_table->ref_count > 0);
190 if (g_atomic_int_exchange_and_add (&hash_table->ref_count, -1) - 1 == 0)
192 g_hash_table_remove_all_nodes (hash_table, FALSE);
193 g_free (hash_table->nodes);
194 g_slice_free (GHashTable, hash_table);
199 * g_hash_table_destroy:
200 * @hash_table: a #GHashTable.
202 * Destroys all keys and values in the #GHashTable and decrements its
203 * reference count by 1. If keys and/or values are dynamically allocated,
204 * you should either free them first or create the #GHashTable with destroy
205 * notifiers using g_hash_table_new_full(). In the latter case the destroy
206 * functions you supplied will be called on all keys and values during the
210 g_hash_table_destroy (GHashTable *hash_table)
212 g_return_if_fail (hash_table != NULL);
213 g_return_if_fail (hash_table->ref_count > 0);
215 g_hash_table_remove_all (hash_table);
216 g_hash_table_unref (hash_table);
219 static inline GHashNode**
220 g_hash_table_lookup_node (GHashTable *hash_table,
227 hash_value = (* hash_table->hash_func) (key);
228 node = &hash_table->nodes[hash_value % hash_table->size];
231 *hash_return = hash_value;
233 /* Hash table lookup needs to be fast.
234 * We therefore remove the extra conditional of testing
235 * whether to call the key_equal_func or not from
238 * Additional optimisation: first check if our full hash
239 * values are equal so we can avoid calling the full-blown
240 * key equality function in most cases.
242 if (hash_table->key_equal_func)
243 while (*node && (((*node)->key_hash != hash_value) ||
244 !(*hash_table->key_equal_func) ((*node)->key, key)))
245 node = &(*node)->next;
247 while (*node && (*node)->key != key)
248 node = &(*node)->next;
254 * g_hash_table_lookup:
255 * @hash_table: a #GHashTable.
256 * @key: the key to look up.
258 * Looks up a key in a #GHashTable. Note that this function cannot
259 * distinguish between a key that is not present and one which is present
260 * and has the value %NULL. If you need this distinction, use
261 * g_hash_table_lookup_extended().
263 * Return value: the associated value, or %NULL if the key is not found.
266 g_hash_table_lookup (GHashTable *hash_table,
271 g_return_val_if_fail (hash_table != NULL, NULL);
273 node = *g_hash_table_lookup_node (hash_table, key, NULL);
275 return node ? node->value : NULL;
279 * g_hash_table_lookup_extended:
280 * @hash_table: a #GHashTable.
281 * @lookup_key: the key to look up.
282 * @orig_key: returns the original key.
283 * @value: returns the value associated with the key.
285 * Looks up a key in the #GHashTable, returning the original key and the
286 * associated value and a #gboolean which is %TRUE if the key was found. This
287 * is useful if you need to free the memory allocated for the original key,
288 * for example before calling g_hash_table_remove().
290 * Return value: %TRUE if the key was found in the #GHashTable.
293 g_hash_table_lookup_extended (GHashTable *hash_table,
294 gconstpointer lookup_key,
300 g_return_val_if_fail (hash_table != NULL, FALSE);
302 node = *g_hash_table_lookup_node (hash_table, lookup_key, NULL);
307 *orig_key = node->key;
309 *value = node->value;
317 g_hash_table_insert_internal (GHashTable *hash_table,
320 gboolean keep_new_key)
325 g_return_if_fail (hash_table != NULL);
326 g_return_if_fail (hash_table->ref_count > 0);
328 node = g_hash_table_lookup_node (hash_table, key, &key_hash);
334 if (hash_table->key_destroy_func)
335 hash_table->key_destroy_func ((*node)->key);
340 if (hash_table->key_destroy_func)
341 hash_table->key_destroy_func (key);
344 if (hash_table->value_destroy_func)
345 hash_table->value_destroy_func ((*node)->value);
347 (*node)->value = value;
351 *node = g_hash_node_new (key, value, key_hash);
352 hash_table->nnodes++;
353 G_HASH_TABLE_RESIZE (hash_table);
358 * g_hash_table_insert:
359 * @hash_table: a #GHashTable.
360 * @key: a key to insert.
361 * @value: the value to associate with the key.
363 * Inserts a new key and value into a #GHashTable.
365 * If the key already exists in the #GHashTable its current value is replaced
366 * with the new value. If you supplied a @value_destroy_func when creating the
367 * #GHashTable, the old value is freed using that function. If you supplied
368 * a @key_destroy_func when creating the #GHashTable, the passed key is freed
369 * using that function.
372 g_hash_table_insert (GHashTable *hash_table,
376 return g_hash_table_insert_internal (hash_table, key, value, FALSE);
380 * g_hash_table_replace:
381 * @hash_table: a #GHashTable.
382 * @key: a key to insert.
383 * @value: the value to associate with the key.
385 * Inserts a new key and value into a #GHashTable similar to
386 * g_hash_table_insert(). The difference is that if the key already exists
387 * in the #GHashTable, it gets replaced by the new key. If you supplied a
388 * @value_destroy_func when creating the #GHashTable, the old value is freed
389 * using that function. If you supplied a @key_destroy_func when creating the
390 * #GHashTable, the old key is freed using that function.
393 g_hash_table_replace (GHashTable *hash_table,
397 return g_hash_table_insert_internal (hash_table, key, value, TRUE);
401 g_hash_table_remove_node (GHashTable *hash_table,
402 GHashNode ***node_ptr_ptr,
405 GHashNode **node_ptr, *node;
407 node_ptr = *node_ptr_ptr;
410 *node_ptr = node->next;
412 if (notify && hash_table->key_destroy_func)
413 hash_table->key_destroy_func (node->key);
415 if (notify && hash_table->value_destroy_func)
416 hash_table->value_destroy_func (node->value);
418 g_slice_free (GHashNode, node);
420 hash_table->nnodes--;
424 g_hash_table_remove_all_nodes (GHashTable *hash_table,
427 GHashNode **node_ptr;
430 for (i = 0; i < hash_table->size; i++)
431 for (node_ptr = &hash_table->nodes[i]; *node_ptr != NULL;)
432 g_hash_table_remove_node (hash_table, &node_ptr, notify);
434 hash_table->nnodes = 0;
438 g_hash_table_remove_internal (GHashTable *hash_table,
442 GHashNode **node_ptr;
444 g_return_val_if_fail (hash_table != NULL, FALSE);
446 node_ptr = g_hash_table_lookup_node (hash_table, key, NULL);
447 if (*node_ptr == NULL)
450 g_hash_table_remove_node (hash_table, &node_ptr, notify);
451 G_HASH_TABLE_RESIZE (hash_table);
457 * g_hash_table_remove:
458 * @hash_table: a #GHashTable.
459 * @key: the key to remove.
461 * Removes a key and its associated value from a #GHashTable.
463 * If the #GHashTable was created using g_hash_table_new_full(), the
464 * key and value are freed using the supplied destroy functions, otherwise
465 * you have to make sure that any dynamically allocated values are freed
468 * Return value: %TRUE if the key was found and removed from the #GHashTable.
471 g_hash_table_remove (GHashTable *hash_table,
474 return g_hash_table_remove_internal (hash_table, key, TRUE);
478 * g_hash_table_remove_all:
479 * @hash_table: a #GHashTable
481 * Removes all keys and their associated values from a #GHashTable.
483 * If the #GHashTable was created using g_hash_table_new_full(), the keys
484 * and values are freed using the supplied destroy functions, otherwise you
485 * have to make sure that any dynamically allocated values are freed
491 g_hash_table_remove_all (GHashTable *hash_table)
493 g_return_if_fail (hash_table != NULL);
495 g_hash_table_remove_all_nodes (hash_table, TRUE);
496 G_HASH_TABLE_RESIZE (hash_table);
500 * g_hash_table_steal:
501 * @hash_table: a #GHashTable.
502 * @key: the key to remove.
504 * Removes a key and its associated value from a #GHashTable without
505 * calling the key and value destroy functions.
507 * Return value: %TRUE if the key was found and removed from the #GHashTable.
510 g_hash_table_steal (GHashTable *hash_table,
513 return g_hash_table_remove_internal (hash_table, key, FALSE);
517 * g_hash_table_steal_all:
518 * @hash_table: a #GHashTable.
520 * Removes all keys and their associated values from a #GHashTable
521 * without calling the key and value destroy functions.
526 g_hash_table_steal_all (GHashTable *hash_table)
528 g_return_if_fail (hash_table != NULL);
530 g_hash_table_remove_all_nodes (hash_table, FALSE);
531 G_HASH_TABLE_RESIZE (hash_table);
535 * g_hash_table_foreach_remove:
536 * @hash_table: a #GHashTable.
537 * @func: the function to call for each key/value pair.
538 * @user_data: user data to pass to the function.
540 * Calls the given function for each key/value pair in the #GHashTable.
541 * If the function returns %TRUE, then the key/value pair is removed from the
542 * #GHashTable. If you supplied key or value destroy functions when creating
543 * the #GHashTable, they are used to free the memory allocated for the removed
546 * Return value: the number of key/value pairs removed.
549 g_hash_table_foreach_remove (GHashTable *hash_table,
553 g_return_val_if_fail (hash_table != NULL, 0);
554 g_return_val_if_fail (func != NULL, 0);
556 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
560 * g_hash_table_foreach_steal:
561 * @hash_table: a #GHashTable.
562 * @func: the function to call for each key/value pair.
563 * @user_data: user data to pass to the function.
565 * Calls the given function for each key/value pair in the #GHashTable.
566 * If the function returns %TRUE, then the key/value pair is removed from the
567 * #GHashTable, but no key or value destroy functions are called.
569 * Return value: the number of key/value pairs removed.
572 g_hash_table_foreach_steal (GHashTable *hash_table,
576 g_return_val_if_fail (hash_table != NULL, 0);
577 g_return_val_if_fail (func != NULL, 0);
579 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
583 g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
588 GHashNode *node, **node_ptr;
592 for (i = 0; i < hash_table->size; i++)
593 for (node_ptr = &hash_table->nodes[i]; (node = *node_ptr) != NULL;)
594 if ((* func) (node->key, node->value, user_data))
596 g_hash_table_remove_node (hash_table, &node_ptr, notify);
600 node_ptr = &node->next;
602 G_HASH_TABLE_RESIZE (hash_table);
608 * g_hash_table_foreach:
609 * @hash_table: a #GHashTable.
610 * @func: the function to call for each key/value pair.
611 * @user_data: user data to pass to the function.
613 * Calls the given function for each of the key/value pairs in the
614 * #GHashTable. The function is passed the key and value of each
615 * pair, and the given @user_data parameter. The hash table may not
616 * be modified while iterating over it (you can't add/remove
617 * items). To remove all items matching a predicate, use
618 * g_hash_table_foreach_remove().
620 * See g_hash_table_find() for performance caveats for linear
621 * order searches in contrast to g_hash_table_lookup().
624 g_hash_table_foreach (GHashTable *hash_table,
631 g_return_if_fail (hash_table != NULL);
632 g_return_if_fail (func != NULL);
634 for (i = 0; i < hash_table->size; i++)
635 for (node = hash_table->nodes[i]; node; node = node->next)
636 (* func) (node->key, node->value, user_data);
641 * @hash_table: a #GHashTable.
642 * @predicate: function to test the key/value pairs for a certain property.
643 * @user_data: user data to pass to the function.
645 * Calls the given function for key/value pairs in the #GHashTable until
646 * @predicate returns %TRUE. The function is passed the key and value of
647 * each pair, and the given @user_data parameter. The hash table may not
648 * be modified while iterating over it (you can't add/remove items).
650 * Note, that hash tables are really only optimized for forward lookups,
651 * i.e. g_hash_table_lookup().
652 * So code that frequently issues g_hash_table_find() or
653 * g_hash_table_foreach() (e.g. in the order of once per every entry in a
654 * hash table) should probably be reworked to use additional or different
655 * data structures for reverse lookups (keep in mind that an O(n) find/foreach
656 * operation issued for all n values in a hash table ends up needing O(n*n)
659 * Return value: The value of the first key/value pair is returned, for which
660 * func evaluates to %TRUE. If no pair with the requested property is found,
666 g_hash_table_find (GHashTable *hash_table,
673 g_return_val_if_fail (hash_table != NULL, NULL);
674 g_return_val_if_fail (predicate != NULL, NULL);
676 for (i = 0; i < hash_table->size; i++)
677 for (node = hash_table->nodes[i]; node; node = node->next)
678 if (predicate (node->key, node->value, user_data))
685 * @hash_table: a #GHashTable.
687 * Returns the number of elements contained in the #GHashTable.
689 * Return value: the number of key/value pairs in the #GHashTable.
692 g_hash_table_size (GHashTable *hash_table)
694 g_return_val_if_fail (hash_table != NULL, 0);
696 return hash_table->nnodes;
700 * g_hash_table_get_keys:
701 * @hash_table: a #GHashTable
703 * Retrieves every key inside @hash_table. The returned data is valid
704 * until @hash_table is modified.
706 * Return value: a #GList containing all the keys inside the hash
707 * table. The content of the list is owned by the hash table and
708 * should not be modified or freed. Use g_list_free() when done
714 g_hash_table_get_keys (GHashTable *hash_table)
720 g_return_val_if_fail (hash_table != NULL, NULL);
723 for (i = 0; i < hash_table->size; i++)
724 for (node = hash_table->nodes[i]; node; node = node->next)
725 retval = g_list_prepend (retval, node->key);
731 * g_hash_table_get_values:
732 * @hash_table: a #GHashTable
734 * Retrieves every value inside @hash_table. The returned data is
735 * valid until @hash_table is modified.
737 * Return value: a #GList containing all the values inside the hash
738 * table. The content of the list is owned by the hash table and
739 * should not be modified or freed. Use g_list_free() when done
745 g_hash_table_get_values (GHashTable *hash_table)
751 g_return_val_if_fail (hash_table != NULL, NULL);
754 for (i = 0; i < hash_table->size; i++)
755 for (node = hash_table->nodes[i]; node; node = node->next)
756 retval = g_list_prepend (retval, node->value);
762 g_hash_table_resize (GHashTable *hash_table)
764 GHashNode **new_nodes;
771 new_size = g_spaced_primes_closest (hash_table->nnodes);
772 new_size = CLAMP (new_size, HASH_TABLE_MIN_SIZE, HASH_TABLE_MAX_SIZE);
774 new_nodes = g_new0 (GHashNode*, new_size);
776 for (i = 0; i < hash_table->size; i++)
777 for (node = hash_table->nodes[i]; node; node = next)
781 hash_val = node->key_hash % new_size;
783 node->next = new_nodes[hash_val];
784 new_nodes[hash_val] = node;
787 g_free (hash_table->nodes);
788 hash_table->nodes = new_nodes;
789 hash_table->size = new_size;
793 g_hash_node_new (gpointer key,
797 GHashNode *hash_node = g_slice_new (GHashNode);
799 hash_node->key = key;
800 hash_node->value = value;
801 hash_node->key_hash = key_hash;
802 hash_node->next = NULL;
808 #include "galiasdef.c"