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/.
33 #include <string.h> /* memset */
37 #include "gstrfuncs.h"
39 #include "gtestutils.h"
46 * @short_description: associations between keys and values so that
47 * given a key the value can be found quickly
49 * A #GHashTable provides associations between keys and values which is
50 * optimized so that given a key, the associated value can be found
53 * Note that neither keys nor values are copied when inserted into the
54 * #GHashTable, so they must exist for the lifetime of the #GHashTable.
55 * This means that the use of static strings is OK, but temporary
56 * strings (i.e. those created in buffers and those returned by GTK+
57 * widgets) should be copied with g_strdup() before being inserted.
59 * If keys or values are dynamically allocated, you must be careful to
60 * ensure that they are freed when they are removed from the
61 * #GHashTable, and also when they are overwritten by new insertions
62 * into the #GHashTable. It is also not advisable to mix static strings
63 * and dynamically-allocated strings in a #GHashTable, because it then
64 * becomes difficult to determine whether the string should be freed.
66 * To create a #GHashTable, use g_hash_table_new().
68 * To insert a key and value into a #GHashTable, use
69 * g_hash_table_insert().
71 * To lookup a value corresponding to a given key, use
72 * g_hash_table_lookup() and g_hash_table_lookup_extended().
74 * g_hash_table_lookup_extended() can also be used to simply
75 * check if a key is present in the hash table.
77 * To remove a key and value, use g_hash_table_remove().
79 * To call a function for each key and value pair use
80 * g_hash_table_foreach() or use a iterator to iterate over the
81 * key/value pairs in the hash table, see #GHashTableIter.
83 * To destroy a #GHashTable use g_hash_table_destroy().
86 * <title>Using a GHashTable as a set</title>
88 * A common use-case for hash tables is to store information about
89 * a set of keys, without associating any particular value with each
90 * key. GHashTable optimizes one way of doing so: If you store only
91 * key-value pairs where key == value, then GHashTable does not
92 * allocate memory to store the values, which can be a considerable
93 * space saving, if your set is large.
97 * set_new (GHashFunc hash_func,
98 * GEqualFunc equal_func,
99 * GDestroyNotify destroy)
101 * return g_hash_table_new_full (hash_func, equal_func, destroy, NULL);
105 * set_insert (GHashTable *set,
108 * g_hash_table_insert (set, element, element);
112 * set_contains (GHashTable *set,
115 * return g_hash_table_lookup_extended (set, element, NULL, NULL);
119 * set_remove (GHashTable *set,
122 * return g_hash_table_remove (set, element);
131 * The #GHashTable struct is an opaque data structure to represent a
132 * <link linkend="glib-Hash-Tables">Hash Table</link>. It should only be
133 * accessed via the following functions.
140 * Specifies the type of the hash function which is passed to
141 * g_hash_table_new() when a #GHashTable is created.
143 * The function is passed a key and should return a #guint hash value.
144 * The functions g_direct_hash(), g_int_hash() and g_str_hash() provide
145 * hash functions which can be used when the key is a #gpointer, #gint,
146 * and #gchar* respectively.
148 * <!-- FIXME: Need more here. --> The hash values should be evenly
149 * distributed over a fairly large range? The modulus is taken with the
150 * hash table size (a prime number) to find the 'bucket' to place each
151 * key into. The function should also be very fast, since it is called
152 * for each key lookup.
154 * Returns: the hash value corresponding to the key
160 * @value: the value corresponding to the key
161 * @user_data: user data passed to g_hash_table_foreach()
163 * Specifies the type of the function passed to g_hash_table_foreach().
164 * It is called with each key/value pair, together with the @user_data
165 * parameter which is passed to g_hash_table_foreach().
171 * @value: the value associated with the key
172 * @user_data: user data passed to g_hash_table_remove()
174 * Specifies the type of the function passed to
175 * g_hash_table_foreach_remove(). It is called with each key/value
176 * pair, together with the @user_data parameter passed to
177 * g_hash_table_foreach_remove(). It should return %TRUE if the
178 * key/value pair should be removed from the #GHashTable.
180 * Returns: %TRUE if the key/value pair should be removed from the
187 * @b: a value to compare with
189 * Specifies the type of a function used to test two values for
190 * equality. The function should return %TRUE if both values are equal
191 * and %FALSE otherwise.
193 * Returns: %TRUE if @a = @b; %FALSE otherwise
199 * A GHashTableIter structure represents an iterator that can be used
200 * to iterate over the elements of a #GHashTable. GHashTableIter
201 * structures are typically allocated on the stack and then initialized
202 * with g_hash_table_iter_init().
206 * g_hash_table_freeze:
207 * @hash_table: a #GHashTable
209 * This function is deprecated and will be removed in the next major
210 * release of GLib. It does nothing.
215 * @hash_table: a #GHashTable
217 * This function is deprecated and will be removed in the next major
218 * release of GLib. It does nothing.
221 #define HASH_TABLE_MIN_SHIFT 3 /* 1 << 3 == 8 buckets */
223 #define UNUSED_HASH_VALUE 0
224 #define TOMBSTONE_HASH_VALUE 1
225 #define HASH_IS_UNUSED(h_) ((h_) == UNUSED_HASH_VALUE)
226 #define HASH_IS_TOMBSTONE(h_) ((h_) == TOMBSTONE_HASH_VALUE)
227 #define HASH_IS_REAL(h_) ((h_) >= 2)
235 gint noccupied; /* nnodes + tombstones */
242 GEqualFunc key_equal_func;
244 #ifndef G_DISABLE_ASSERT
246 * Tracks the structure of the hash table, not its contents: is only
247 * incremented when a node is added or removed (is not incremented
248 * when the key or data of a node is modified).
252 GDestroyNotify key_destroy_func;
253 GDestroyNotify value_destroy_func;
258 GHashTable *hash_table;
266 /* Each table size has an associated prime modulo (the first prime
267 * lower than the table size) used to find the initial bucket. Probing
268 * then works modulo 2^n. The prime modulo is necessary to get a
269 * good distribution with poor hash functions.
271 static const gint prime_mod [] =
289 65521, /* For 1 << 16 */
304 2147483647 /* For 1 << 31 */
308 g_hash_table_set_shift (GHashTable *hash_table, gint shift)
313 hash_table->size = 1 << shift;
314 hash_table->mod = prime_mod [shift];
316 for (i = 0; i < shift; i++)
322 hash_table->mask = mask;
326 g_hash_table_find_closest_shift (gint n)
337 g_hash_table_set_shift_from_size (GHashTable *hash_table, gint size)
341 shift = g_hash_table_find_closest_shift (size);
342 shift = MAX (shift, HASH_TABLE_MIN_SHIFT);
344 g_hash_table_set_shift (hash_table, shift);
348 * g_hash_table_lookup_node:
349 * @hash_table: our #GHashTable
350 * @key: the key to lookup against
351 * @hash_return: key hash return location
353 * Performs a lookup in the hash table, preserving extra information
354 * usually needed for insertion.
356 * This function first computes the hash value of the key using the
357 * user's hash function.
359 * If an entry in the table matching @key is found then this function
360 * returns the index of that entry in the table, and if not, the
361 * index of an unused node (empty or tombstone) where the key can be
364 * The computed hash value is returned in the variable pointed to
365 * by @hash_return. This is to save insertions from having to compute
366 * the hash record again for the new record.
368 * Returns: index of the described node
371 g_hash_table_lookup_node (GHashTable *hash_table,
378 guint first_tombstone = 0;
379 gboolean have_tombstone = FALSE;
382 hash_value = hash_table->hash_func (key);
383 if (G_UNLIKELY (!HASH_IS_REAL (hash_value)))
386 *hash_return = hash_value;
388 node_index = hash_value % hash_table->mod;
389 node_hash = hash_table->hashes[node_index];
391 while (!HASH_IS_UNUSED (node_hash))
393 /* We first check if our full hash values
394 * are equal so we can avoid calling the full-blown
395 * key equality function in most cases.
397 if (node_hash == hash_value)
399 gpointer node_key = hash_table->keys[node_index];
401 if (hash_table->key_equal_func)
403 if (hash_table->key_equal_func (node_key, key))
406 else if (node_key == key)
411 else if (HASH_IS_TOMBSTONE (node_hash) && !have_tombstone)
413 first_tombstone = node_index;
414 have_tombstone = TRUE;
419 node_index &= hash_table->mask;
420 node_hash = hash_table->hashes[node_index];
424 return first_tombstone;
430 * g_hash_table_remove_node:
431 * @hash_table: our #GHashTable
432 * @node: pointer to node to remove
433 * @notify: %TRUE if the destroy notify handlers are to be called
435 * Removes a node from the hash table and updates the node count.
436 * The node is replaced by a tombstone. No table resize is performed.
438 * If @notify is %TRUE then the destroy notify functions are called
439 * for the key and value of the hash node.
442 g_hash_table_remove_node (GHashTable *hash_table,
449 key = hash_table->keys[i];
450 value = hash_table->values[i];
452 /* Erect tombstone */
453 hash_table->hashes[i] = TOMBSTONE_HASH_VALUE;
456 hash_table->keys[i] = NULL;
457 hash_table->values[i] = NULL;
459 hash_table->nnodes--;
461 if (notify && hash_table->key_destroy_func)
462 hash_table->key_destroy_func (key);
464 if (notify && hash_table->value_destroy_func)
465 hash_table->value_destroy_func (value);
470 * g_hash_table_remove_all_nodes:
471 * @hash_table: our #GHashTable
472 * @notify: %TRUE if the destroy notify handlers are to be called
474 * Removes all nodes from the table. Since this may be a precursor to
475 * freeing the table entirely, no resize is performed.
477 * If @notify is %TRUE then the destroy notify functions are called
478 * for the key and value of the hash node.
481 g_hash_table_remove_all_nodes (GHashTable *hash_table,
488 hash_table->nnodes = 0;
489 hash_table->noccupied = 0;
492 (hash_table->key_destroy_func == NULL &&
493 hash_table->value_destroy_func == NULL))
495 memset (hash_table->hashes, 0, hash_table->size * sizeof (guint));
496 memset (hash_table->keys, 0, hash_table->size * sizeof (gpointer));
497 memset (hash_table->values, 0, hash_table->size * sizeof (gpointer));
502 for (i = 0; i < hash_table->size; i++)
504 if (HASH_IS_REAL (hash_table->hashes[i]))
506 key = hash_table->keys[i];
507 value = hash_table->values[i];
509 hash_table->hashes[i] = UNUSED_HASH_VALUE;
510 hash_table->keys[i] = NULL;
511 hash_table->values[i] = NULL;
513 if (hash_table->key_destroy_func != NULL)
514 hash_table->key_destroy_func (key);
516 if (hash_table->value_destroy_func != NULL)
517 hash_table->value_destroy_func (value);
519 else if (HASH_IS_TOMBSTONE (hash_table->hashes[i]))
521 hash_table->hashes[i] = UNUSED_HASH_VALUE;
527 * g_hash_table_resize:
528 * @hash_table: our #GHashTable
530 * Resizes the hash table to the optimal size based on the number of
531 * nodes currently held. If you call this function then a resize will
532 * occur, even if one does not need to occur.
533 * Use g_hash_table_maybe_resize() instead.
535 * This function may "resize" the hash table to its current size, with
536 * the side effect of cleaning up tombstones and otherwise optimizing
537 * the probe sequences.
540 g_hash_table_resize (GHashTable *hash_table)
543 gpointer *new_values;
548 old_size = hash_table->size;
549 g_hash_table_set_shift_from_size (hash_table, hash_table->nnodes * 2);
551 new_keys = g_new0 (gpointer, hash_table->size);
552 if (hash_table->keys == hash_table->values)
553 new_values = new_keys;
555 new_values = g_new0 (gpointer, hash_table->size);
556 new_hashes = g_new0 (guint, hash_table->size);
558 for (i = 0; i < old_size; i++)
560 guint node_hash = hash_table->hashes[i];
564 if (!HASH_IS_REAL (node_hash))
567 hash_val = node_hash % hash_table->mod;
569 while (!HASH_IS_UNUSED (new_hashes[hash_val]))
573 hash_val &= hash_table->mask;
576 new_hashes[hash_val] = hash_table->hashes[i];
577 new_keys[hash_val] = hash_table->keys[i];
578 new_values[hash_val] = hash_table->values[i];
581 if (hash_table->keys != hash_table->values)
582 g_free (hash_table->values);
584 g_free (hash_table->keys);
585 g_free (hash_table->hashes);
587 hash_table->keys = new_keys;
588 hash_table->values = new_values;
589 hash_table->hashes = new_hashes;
591 hash_table->noccupied = hash_table->nnodes;
595 * g_hash_table_maybe_resize:
596 * @hash_table: our #GHashTable
598 * Resizes the hash table, if needed.
600 * Essentially, calls g_hash_table_resize() if the table has strayed
601 * too far from its ideal size for its number of nodes.
604 g_hash_table_maybe_resize (GHashTable *hash_table)
606 gint noccupied = hash_table->noccupied;
607 gint size = hash_table->size;
609 if ((size > hash_table->nnodes * 4 && size > 1 << HASH_TABLE_MIN_SHIFT) ||
610 (size <= noccupied + (noccupied / 16)))
611 g_hash_table_resize (hash_table);
616 * @hash_func: a function to create a hash value from a key
617 * @key_equal_func: a function to check two keys for equality
619 * Creates a new #GHashTable with a reference count of 1.
621 * Hash values returned by @hash_func are used to determine where keys
622 * are stored within the #GHashTable data structure. The g_direct_hash(),
623 * g_int_hash(), g_int64_hash(), g_double_hash() and g_str_hash()
624 * functions are provided for some common types of keys.
625 * If @hash_func is %NULL, g_direct_hash() is used.
627 * @key_equal_func is used when looking up keys in the #GHashTable.
628 * The g_direct_equal(), g_int_equal(), g_int64_equal(), g_double_equal()
629 * and g_str_equal() functions are provided for the most common types
630 * of keys. If @key_equal_func is %NULL, keys are compared directly in
631 * a similar fashion to g_direct_equal(), but without the overhead of
634 * Return value: a new #GHashTable
637 g_hash_table_new (GHashFunc hash_func,
638 GEqualFunc key_equal_func)
640 return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
645 * g_hash_table_new_full:
646 * @hash_func: a function to create a hash value from a key
647 * @key_equal_func: a function to check two keys for equality
648 * @key_destroy_func: a function to free the memory allocated for the key
649 * used when removing the entry from the #GHashTable, or %NULL
650 * if you don't want to supply such a function.
651 * @value_destroy_func: a function to free the memory allocated for the
652 * value used when removing the entry from the #GHashTable, or %NULL
653 * if you don't want to supply such a function.
655 * Creates a new #GHashTable like g_hash_table_new() with a reference
656 * count of 1 and allows to specify functions to free the memory
657 * allocated for the key and value that get called when removing the
658 * entry from the #GHashTable.
660 * Return value: a new #GHashTable
663 g_hash_table_new_full (GHashFunc hash_func,
664 GEqualFunc key_equal_func,
665 GDestroyNotify key_destroy_func,
666 GDestroyNotify value_destroy_func)
668 GHashTable *hash_table;
670 hash_table = g_slice_new (GHashTable);
671 g_hash_table_set_shift (hash_table, HASH_TABLE_MIN_SHIFT);
672 hash_table->nnodes = 0;
673 hash_table->noccupied = 0;
674 hash_table->hash_func = hash_func ? hash_func : g_direct_hash;
675 hash_table->key_equal_func = key_equal_func;
676 hash_table->ref_count = 1;
677 #ifndef G_DISABLE_ASSERT
678 hash_table->version = 0;
680 hash_table->key_destroy_func = key_destroy_func;
681 hash_table->value_destroy_func = value_destroy_func;
682 hash_table->keys = g_new0 (gpointer, hash_table->size);
683 hash_table->values = hash_table->keys;
684 hash_table->hashes = g_new0 (guint, hash_table->size);
690 * g_hash_table_iter_init:
691 * @iter: an uninitialized #GHashTableIter
692 * @hash_table: a #GHashTable
694 * Initializes a key/value pair iterator and associates it with
695 * @hash_table. Modifying the hash table after calling this function
696 * invalidates the returned iterator.
698 * GHashTableIter iter;
699 * gpointer key, value;
701 * g_hash_table_iter_init (&iter, hash_table);
702 * while (g_hash_table_iter_next (&iter, &key, &value))
704 * /* do something with key and value */
711 g_hash_table_iter_init (GHashTableIter *iter,
712 GHashTable *hash_table)
714 RealIter *ri = (RealIter *) iter;
716 g_return_if_fail (iter != NULL);
717 g_return_if_fail (hash_table != NULL);
719 ri->hash_table = hash_table;
721 #ifndef G_DISABLE_ASSERT
722 ri->version = hash_table->version;
727 * g_hash_table_iter_next:
728 * @iter: an initialized #GHashTableIter
729 * @key: a location to store the key, or %NULL
730 * @value: a location to store the value, or %NULL
732 * Advances @iter and retrieves the key and/or value that are now
733 * pointed to as a result of this advancement. If %FALSE is returned,
734 * @key and @value are not set, and the iterator becomes invalid.
736 * Return value: %FALSE if the end of the #GHashTable has been reached.
741 g_hash_table_iter_next (GHashTableIter *iter,
745 RealIter *ri = (RealIter *) iter;
748 g_return_val_if_fail (iter != NULL, FALSE);
749 #ifndef G_DISABLE_ASSERT
750 g_return_val_if_fail (ri->version == ri->hash_table->version, FALSE);
752 g_return_val_if_fail (ri->position < ri->hash_table->size, FALSE);
754 position = ri->position;
759 if (position >= ri->hash_table->size)
761 ri->position = position;
765 while (!HASH_IS_REAL (ri->hash_table->hashes[position]));
768 *key = ri->hash_table->keys[position];
770 *value = ri->hash_table->values[position];
772 ri->position = position;
777 * g_hash_table_iter_get_hash_table:
778 * @iter: an initialized #GHashTableIter
780 * Returns the #GHashTable associated with @iter.
782 * Return value: the #GHashTable associated with @iter.
787 g_hash_table_iter_get_hash_table (GHashTableIter *iter)
789 g_return_val_if_fail (iter != NULL, NULL);
791 return ((RealIter *) iter)->hash_table;
795 iter_remove_or_steal (RealIter *ri, gboolean notify)
797 g_return_if_fail (ri != NULL);
798 #ifndef G_DISABLE_ASSERT
799 g_return_if_fail (ri->version == ri->hash_table->version);
801 g_return_if_fail (ri->position >= 0);
802 g_return_if_fail (ri->position < ri->hash_table->size);
804 g_hash_table_remove_node (ri->hash_table, ri->position, notify);
806 #ifndef G_DISABLE_ASSERT
808 ri->hash_table->version++;
813 * g_hash_table_iter_remove:
814 * @iter: an initialized #GHashTableIter
816 * Removes the key/value pair currently pointed to by the iterator
817 * from its associated #GHashTable. Can only be called after
818 * g_hash_table_iter_next() returned %TRUE, and cannot be called
819 * more than once for the same key/value pair.
821 * If the #GHashTable was created using g_hash_table_new_full(),
822 * the key and value are freed using the supplied destroy functions,
823 * otherwise you have to make sure that any dynamically allocated
824 * values are freed yourself.
829 g_hash_table_iter_remove (GHashTableIter *iter)
831 iter_remove_or_steal ((RealIter *) iter, TRUE);
835 * g_hash_table_insert_node:
836 * @hash_table: our #GHashTable
837 * @node_index: pointer to node to insert/replace
838 * @key_hash: key hash
839 * @key: key to replace with
840 * @value: value to replace with
842 * Inserts a value at @node_index in the hash table and updates it.
845 g_hash_table_insert_node (GHashTable *hash_table,
850 gboolean keep_new_key)
856 if (G_UNLIKELY (hash_table->keys == hash_table->values && key != value))
857 hash_table->values = g_memdup (hash_table->keys, sizeof (gpointer) * hash_table->size);
859 old_hash = hash_table->hashes[node_index];
860 old_key = hash_table->keys[node_index];
861 old_value = hash_table->values[node_index];
863 if (HASH_IS_REAL (old_hash))
866 hash_table->keys[node_index] = key;
867 hash_table->values[node_index] = value;
871 hash_table->keys[node_index] = key;
872 hash_table->values[node_index] = value;
873 hash_table->hashes[node_index] = key_hash;
875 hash_table->nnodes++;
877 if (HASH_IS_UNUSED (old_hash))
879 /* We replaced an empty node, and not a tombstone */
880 hash_table->noccupied++;
881 g_hash_table_maybe_resize (hash_table);
884 #ifndef G_DISABLE_ASSERT
885 hash_table->version++;
889 if (HASH_IS_REAL (old_hash))
891 if (hash_table->key_destroy_func)
892 hash_table->key_destroy_func (keep_new_key ? old_key : key);
893 if (hash_table->value_destroy_func)
894 hash_table->value_destroy_func (old_value);
899 * g_hash_table_iter_replace:
900 * @iter: an initialized #GHashTableIter
901 * @value: the value to replace with
903 * Replaces the value currently pointed to by the iterator
904 * from its associated #GHashTable. Can only be called after
905 * g_hash_table_iter_next() returned %TRUE.
907 * If you supplied a @value_destroy_func when creating the
908 * #GHashTable, the old value is freed using that function.
913 g_hash_table_iter_replace (GHashTableIter *iter,
920 ri = (RealIter *) iter;
922 g_return_if_fail (ri != NULL);
923 #ifndef G_DISABLE_ASSERT
924 g_return_if_fail (ri->version == ri->hash_table->version);
926 g_return_if_fail (ri->position >= 0);
927 g_return_if_fail (ri->position < ri->hash_table->size);
929 node_hash = ri->hash_table->hashes[ri->position];
930 key = ri->hash_table->keys[ri->position];
932 g_hash_table_insert_node (ri->hash_table, ri->position, node_hash, key, value, TRUE);
934 #ifndef G_DISABLE_ASSERT
936 ri->hash_table->version++;
941 * g_hash_table_iter_steal:
942 * @iter: an initialized #GHashTableIter
944 * Removes the key/value pair currently pointed to by the
945 * iterator from its associated #GHashTable, without calling
946 * the key and value destroy functions. Can only be called
947 * after g_hash_table_iter_next() returned %TRUE, and cannot
948 * be called more than once for the same key/value pair.
953 g_hash_table_iter_steal (GHashTableIter *iter)
955 iter_remove_or_steal ((RealIter *) iter, FALSE);
961 * @hash_table: a valid #GHashTable
963 * Atomically increments the reference count of @hash_table by one.
964 * This function is MT-safe and may be called from any thread.
966 * Return value: the passed in #GHashTable
971 g_hash_table_ref (GHashTable *hash_table)
973 g_return_val_if_fail (hash_table != NULL, NULL);
975 g_atomic_int_inc (&hash_table->ref_count);
981 * g_hash_table_unref:
982 * @hash_table: a valid #GHashTable
984 * Atomically decrements the reference count of @hash_table by one.
985 * If the reference count drops to 0, all keys and values will be
986 * destroyed, and all memory allocated by the hash table is released.
987 * This function is MT-safe and may be called from any thread.
992 g_hash_table_unref (GHashTable *hash_table)
994 g_return_if_fail (hash_table != NULL);
996 if (g_atomic_int_dec_and_test (&hash_table->ref_count))
998 g_hash_table_remove_all_nodes (hash_table, TRUE);
999 if (hash_table->keys != hash_table->values)
1000 g_free (hash_table->values);
1001 g_free (hash_table->keys);
1002 g_free (hash_table->hashes);
1003 g_slice_free (GHashTable, hash_table);
1008 * g_hash_table_destroy:
1009 * @hash_table: a #GHashTable
1011 * Destroys all keys and values in the #GHashTable and decrements its
1012 * reference count by 1. If keys and/or values are dynamically allocated,
1013 * you should either free them first or create the #GHashTable with destroy
1014 * notifiers using g_hash_table_new_full(). In the latter case the destroy
1015 * functions you supplied will be called on all keys and values during the
1016 * destruction phase.
1019 g_hash_table_destroy (GHashTable *hash_table)
1021 g_return_if_fail (hash_table != NULL);
1023 g_hash_table_remove_all (hash_table);
1024 g_hash_table_unref (hash_table);
1028 * g_hash_table_lookup:
1029 * @hash_table: a #GHashTable
1030 * @key: the key to look up
1032 * Looks up a key in a #GHashTable. Note that this function cannot
1033 * distinguish between a key that is not present and one which is present
1034 * and has the value %NULL. If you need this distinction, use
1035 * g_hash_table_lookup_extended().
1037 * Return value: the associated value, or %NULL if the key is not found
1040 g_hash_table_lookup (GHashTable *hash_table,
1046 g_return_val_if_fail (hash_table != NULL, NULL);
1048 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1050 return HASH_IS_REAL (hash_table->hashes[node_index])
1051 ? hash_table->values[node_index]
1056 * g_hash_table_lookup_extended:
1057 * @hash_table: a #GHashTable
1058 * @lookup_key: the key to look up
1059 * @orig_key: return location for the original key, or %NULL
1060 * @value: return location for the value associated with the key, or %NULL
1062 * Looks up a key in the #GHashTable, returning the original key and the
1063 * associated value and a #gboolean which is %TRUE if the key was found. This
1064 * is useful if you need to free the memory allocated for the original key,
1065 * for example before calling g_hash_table_remove().
1067 * You can actually pass %NULL for @lookup_key to test
1068 * whether the %NULL key exists, provided the hash and equal functions
1069 * of @hash_table are %NULL-safe.
1071 * Return value: %TRUE if the key was found in the #GHashTable
1074 g_hash_table_lookup_extended (GHashTable *hash_table,
1075 gconstpointer lookup_key,
1082 g_return_val_if_fail (hash_table != NULL, FALSE);
1084 node_index = g_hash_table_lookup_node (hash_table, lookup_key, &node_hash);
1086 if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1090 *orig_key = hash_table->keys[node_index];
1093 *value = hash_table->values[node_index];
1099 * g_hash_table_insert_internal:
1100 * @hash_table: our #GHashTable
1101 * @key: the key to insert
1102 * @value: the value to insert
1103 * @keep_new_key: if %TRUE and this key already exists in the table
1104 * then call the destroy notify function on the old key. If %FALSE
1105 * then call the destroy notify function on the new key.
1107 * Implements the common logic for the g_hash_table_insert() and
1108 * g_hash_table_replace() functions.
1110 * Do a lookup of @key. If it is found, replace it with the new
1111 * @value (and perhaps the new @key). If it is not found, create
1115 g_hash_table_insert_internal (GHashTable *hash_table,
1118 gboolean keep_new_key)
1123 g_return_if_fail (hash_table != NULL);
1125 node_index = g_hash_table_lookup_node (hash_table, key, &key_hash);
1127 g_hash_table_insert_node (hash_table, node_index, key_hash, key, value, keep_new_key);
1131 * g_hash_table_insert:
1132 * @hash_table: a #GHashTable
1133 * @key: a key to insert
1134 * @value: the value to associate with the key
1136 * Inserts a new key and value into a #GHashTable.
1138 * If the key already exists in the #GHashTable its current
1139 * value is replaced with the new value. If you supplied a
1140 * @value_destroy_func when creating the #GHashTable, the old
1141 * value is freed using that function. If you supplied a
1142 * @key_destroy_func when creating the #GHashTable, the passed
1143 * key is freed using that function.
1146 g_hash_table_insert (GHashTable *hash_table,
1150 g_hash_table_insert_internal (hash_table, key, value, FALSE);
1154 * g_hash_table_replace:
1155 * @hash_table: a #GHashTable
1156 * @key: a key to insert
1157 * @value: the value to associate with the key
1159 * Inserts a new key and value into a #GHashTable similar to
1160 * g_hash_table_insert(). The difference is that if the key
1161 * already exists in the #GHashTable, it gets replaced by the
1162 * new key. If you supplied a @value_destroy_func when creating
1163 * the #GHashTable, the old value is freed using that function.
1164 * If you supplied a @key_destroy_func when creating the
1165 * #GHashTable, the old key is freed using that function.
1168 g_hash_table_replace (GHashTable *hash_table,
1172 g_hash_table_insert_internal (hash_table, key, value, TRUE);
1176 * g_hash_table_remove_internal:
1177 * @hash_table: our #GHashTable
1178 * @key: the key to remove
1179 * @notify: %TRUE if the destroy notify handlers are to be called
1180 * Return value: %TRUE if a node was found and removed, else %FALSE
1182 * Implements the common logic for the g_hash_table_remove() and
1183 * g_hash_table_steal() functions.
1185 * Do a lookup of @key and remove it if it is found, calling the
1186 * destroy notify handlers only if @notify is %TRUE.
1189 g_hash_table_remove_internal (GHashTable *hash_table,
1196 g_return_val_if_fail (hash_table != NULL, FALSE);
1198 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1200 if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1203 g_hash_table_remove_node (hash_table, node_index, notify);
1204 g_hash_table_maybe_resize (hash_table);
1206 #ifndef G_DISABLE_ASSERT
1207 hash_table->version++;
1214 * g_hash_table_remove:
1215 * @hash_table: a #GHashTable
1216 * @key: the key to remove
1218 * Removes a key and its associated value from a #GHashTable.
1220 * If the #GHashTable was created using g_hash_table_new_full(), the
1221 * key and value are freed using the supplied destroy functions, otherwise
1222 * you have to make sure that any dynamically allocated values are freed
1225 * Returns: %TRUE if the key was found and removed from the #GHashTable
1228 g_hash_table_remove (GHashTable *hash_table,
1231 return g_hash_table_remove_internal (hash_table, key, TRUE);
1235 * g_hash_table_steal:
1236 * @hash_table: a #GHashTable
1237 * @key: the key to remove
1239 * Removes a key and its associated value from a #GHashTable without
1240 * calling the key and value destroy functions.
1242 * Returns: %TRUE if the key was found and removed from the #GHashTable
1245 g_hash_table_steal (GHashTable *hash_table,
1248 return g_hash_table_remove_internal (hash_table, key, FALSE);
1252 * g_hash_table_remove_all:
1253 * @hash_table: a #GHashTable
1255 * Removes all keys and their associated values from a #GHashTable.
1257 * If the #GHashTable was created using g_hash_table_new_full(),
1258 * the keys and values are freed using the supplied destroy functions,
1259 * otherwise you have to make sure that any dynamically allocated
1260 * values are freed yourself.
1265 g_hash_table_remove_all (GHashTable *hash_table)
1267 g_return_if_fail (hash_table != NULL);
1269 #ifndef G_DISABLE_ASSERT
1270 if (hash_table->nnodes != 0)
1271 hash_table->version++;
1274 g_hash_table_remove_all_nodes (hash_table, TRUE);
1275 g_hash_table_maybe_resize (hash_table);
1279 * g_hash_table_steal_all:
1280 * @hash_table: a #GHashTable
1282 * Removes all keys and their associated values from a #GHashTable
1283 * without calling the key and value destroy functions.
1288 g_hash_table_steal_all (GHashTable *hash_table)
1290 g_return_if_fail (hash_table != NULL);
1292 #ifndef G_DISABLE_ASSERT
1293 if (hash_table->nnodes != 0)
1294 hash_table->version++;
1297 g_hash_table_remove_all_nodes (hash_table, FALSE);
1298 g_hash_table_maybe_resize (hash_table);
1302 * g_hash_table_foreach_remove_or_steal:
1303 * @hash_table: a #GHashTable
1304 * @func: the user's callback function
1305 * @user_data: data for @func
1306 * @notify: %TRUE if the destroy notify handlers are to be called
1308 * Implements the common logic for g_hash_table_foreach_remove()
1309 * and g_hash_table_foreach_steal().
1311 * Iterates over every node in the table, calling @func with the key
1312 * and value of the node (and @user_data). If @func returns %TRUE the
1313 * node is removed from the table.
1315 * If @notify is true then the destroy notify handlers will be called
1316 * for each removed node.
1319 g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
1326 #ifndef G_DISABLE_ASSERT
1327 gint version = hash_table->version;
1330 for (i = 0; i < hash_table->size; i++)
1332 guint node_hash = hash_table->hashes[i];
1333 gpointer node_key = hash_table->keys[i];
1334 gpointer node_value = hash_table->values[i];
1336 if (HASH_IS_REAL (node_hash) &&
1337 (* func) (node_key, node_value, user_data))
1339 g_hash_table_remove_node (hash_table, i, notify);
1343 #ifndef G_DISABLE_ASSERT
1344 g_return_val_if_fail (version == hash_table->version, 0);
1348 g_hash_table_maybe_resize (hash_table);
1350 #ifndef G_DISABLE_ASSERT
1352 hash_table->version++;
1359 * g_hash_table_foreach_remove:
1360 * @hash_table: a #GHashTable
1361 * @func: the function to call for each key/value pair
1362 * @user_data: user data to pass to the function
1364 * Calls the given function for each key/value pair in the
1365 * #GHashTable. If the function returns %TRUE, then the key/value
1366 * pair is removed from the #GHashTable. If you supplied key or
1367 * value destroy functions when creating the #GHashTable, they are
1368 * used to free the memory allocated for the removed keys and values.
1370 * See #GHashTableIter for an alternative way to loop over the
1371 * key/value pairs in the hash table.
1373 * Return value: the number of key/value pairs removed
1376 g_hash_table_foreach_remove (GHashTable *hash_table,
1380 g_return_val_if_fail (hash_table != NULL, 0);
1381 g_return_val_if_fail (func != NULL, 0);
1383 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
1387 * g_hash_table_foreach_steal:
1388 * @hash_table: a #GHashTable
1389 * @func: the function to call for each key/value pair
1390 * @user_data: user data to pass to the function
1392 * Calls the given function for each key/value pair in the
1393 * #GHashTable. If the function returns %TRUE, then the key/value
1394 * pair is removed from the #GHashTable, but no key or value
1395 * destroy functions are called.
1397 * See #GHashTableIter for an alternative way to loop over the
1398 * key/value pairs in the hash table.
1400 * Return value: the number of key/value pairs removed.
1403 g_hash_table_foreach_steal (GHashTable *hash_table,
1407 g_return_val_if_fail (hash_table != NULL, 0);
1408 g_return_val_if_fail (func != NULL, 0);
1410 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
1414 * g_hash_table_foreach:
1415 * @hash_table: a #GHashTable
1416 * @func: the function to call for each key/value pair
1417 * @user_data: user data to pass to the function
1419 * Calls the given function for each of the key/value pairs in the
1420 * #GHashTable. The function is passed the key and value of each
1421 * pair, and the given @user_data parameter. The hash table may not
1422 * be modified while iterating over it (you can't add/remove
1423 * items). To remove all items matching a predicate, use
1424 * g_hash_table_foreach_remove().
1426 * See g_hash_table_find() for performance caveats for linear
1427 * order searches in contrast to g_hash_table_lookup().
1430 g_hash_table_foreach (GHashTable *hash_table,
1435 #ifndef G_DISABLE_ASSERT
1436 gint version = hash_table->version;
1439 g_return_if_fail (hash_table != NULL);
1440 g_return_if_fail (func != NULL);
1442 for (i = 0; i < hash_table->size; i++)
1444 guint node_hash = hash_table->hashes[i];
1445 gpointer node_key = hash_table->keys[i];
1446 gpointer node_value = hash_table->values[i];
1448 if (HASH_IS_REAL (node_hash))
1449 (* func) (node_key, node_value, user_data);
1451 #ifndef G_DISABLE_ASSERT
1452 g_return_if_fail (version == hash_table->version);
1458 * g_hash_table_find:
1459 * @hash_table: a #GHashTable
1460 * @predicate: function to test the key/value pairs for a certain property
1461 * @user_data: user data to pass to the function
1463 * Calls the given function for key/value pairs in the #GHashTable
1464 * until @predicate returns %TRUE. The function is passed the key
1465 * and value of each pair, and the given @user_data parameter. The
1466 * hash table may not be modified while iterating over it (you can't
1467 * add/remove items).
1469 * Note, that hash tables are really only optimized for forward
1470 * lookups, i.e. g_hash_table_lookup(). So code that frequently issues
1471 * g_hash_table_find() or g_hash_table_foreach() (e.g. in the order of
1472 * once per every entry in a hash table) should probably be reworked
1473 * to use additional or different data structures for reverse lookups
1474 * (keep in mind that an O(n) find/foreach operation issued for all n
1475 * values in a hash table ends up needing O(n*n) operations).
1477 * Return value: The value of the first key/value pair is returned,
1478 * for which @predicate evaluates to %TRUE. If no pair with the
1479 * requested property is found, %NULL is returned.
1484 g_hash_table_find (GHashTable *hash_table,
1489 #ifndef G_DISABLE_ASSERT
1490 gint version = hash_table->version;
1494 g_return_val_if_fail (hash_table != NULL, NULL);
1495 g_return_val_if_fail (predicate != NULL, NULL);
1499 for (i = 0; i < hash_table->size; i++)
1501 guint node_hash = hash_table->hashes[i];
1502 gpointer node_key = hash_table->keys[i];
1503 gpointer node_value = hash_table->values[i];
1505 if (HASH_IS_REAL (node_hash))
1506 match = predicate (node_key, node_value, user_data);
1508 #ifndef G_DISABLE_ASSERT
1509 g_return_val_if_fail (version == hash_table->version, NULL);
1520 * g_hash_table_size:
1521 * @hash_table: a #GHashTable
1523 * Returns the number of elements contained in the #GHashTable.
1525 * Return value: the number of key/value pairs in the #GHashTable.
1528 g_hash_table_size (GHashTable *hash_table)
1530 g_return_val_if_fail (hash_table != NULL, 0);
1532 return hash_table->nnodes;
1536 * g_hash_table_get_keys:
1537 * @hash_table: a #GHashTable
1539 * Retrieves every key inside @hash_table. The returned data
1540 * is valid until @hash_table is modified.
1542 * Return value: a #GList containing all the keys inside the hash
1543 * table. The content of the list is owned by the hash table and
1544 * should not be modified or freed. Use g_list_free() when done
1550 g_hash_table_get_keys (GHashTable *hash_table)
1555 g_return_val_if_fail (hash_table != NULL, NULL);
1558 for (i = 0; i < hash_table->size; i++)
1560 if (HASH_IS_REAL (hash_table->hashes[i]))
1561 retval = g_list_prepend (retval, hash_table->keys[i]);
1568 * g_hash_table_get_values:
1569 * @hash_table: a #GHashTable
1571 * Retrieves every value inside @hash_table. The returned data
1572 * is valid until @hash_table is modified.
1574 * Return value: a #GList containing all the values inside the hash
1575 * table. The content of the list is owned by the hash table and
1576 * should not be modified or freed. Use g_list_free() when done
1582 g_hash_table_get_values (GHashTable *hash_table)
1587 g_return_val_if_fail (hash_table != NULL, NULL);
1590 for (i = 0; i < hash_table->size; i++)
1592 if (HASH_IS_REAL (hash_table->hashes[i]))
1593 retval = g_list_prepend (retval, hash_table->values[i]);
1605 * @v2: a key to compare with @v1
1607 * Compares two strings for byte-by-byte equality and returns %TRUE
1608 * if they are equal. It can be passed to g_hash_table_new() as the
1609 * @key_equal_func parameter, when using strings as keys in a #GHashTable.
1611 * Note that this function is primarily meant as a hash table comparison
1612 * function. For a general-purpose, %NULL-safe string comparison function,
1615 * Returns: %TRUE if the two keys match
1618 g_str_equal (gconstpointer v1,
1621 const gchar *string1 = v1;
1622 const gchar *string2 = v2;
1624 return strcmp (string1, string2) == 0;
1631 * Converts a string to a hash value.
1633 * This function implements the widely used "djb" hash apparently posted
1634 * by Daniel Bernstein to comp.lang.c some time ago. The 32 bit
1635 * unsigned hash value starts at 5381 and for each byte 'c' in the
1636 * string, is updated: <literal>hash = hash * 33 + c</literal>. This
1637 * function uses the signed value of each byte.
1639 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1640 * when using strings as keys in a #GHashTable.
1642 * Returns: a hash value corresponding to the key
1645 g_str_hash (gconstpointer v)
1647 const signed char *p;
1650 for (p = v; *p != '\0'; p++)
1651 h = (h << 5) + h + *p;
1658 * @v: a #gpointer key
1660 * Converts a gpointer to a hash value.
1661 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1662 * when using pointers as keys in a #GHashTable.
1664 * Returns: a hash value corresponding to the key.
1667 g_direct_hash (gconstpointer v)
1669 return GPOINTER_TO_UINT (v);
1675 * @v2: a key to compare with @v1
1677 * Compares two #gpointer arguments and returns %TRUE if they are equal.
1678 * It can be passed to g_hash_table_new() as the @key_equal_func
1679 * parameter, when using pointers as keys in a #GHashTable.
1681 * Returns: %TRUE if the two keys match.
1684 g_direct_equal (gconstpointer v1,
1692 * @v1: a pointer to a #gint key
1693 * @v2: a pointer to a #gint key to compare with @v1
1695 * Compares the two #gint values being pointed to and returns
1696 * %TRUE if they are equal.
1697 * It can be passed to g_hash_table_new() as the @key_equal_func
1698 * parameter, when using pointers to integers as keys in a #HashTable.
1700 * Returns: %TRUE if the two keys match.
1703 g_int_equal (gconstpointer v1,
1706 return *((const gint*) v1) == *((const gint*) v2);
1711 * @v: a pointer to a #gint key
1713 * Converts a pointer to a #gint to a hash value.
1714 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1715 * when using pointers to integers values as keys in a #GHashTable.
1717 * Returns: a hash value corresponding to the key.
1720 g_int_hash (gconstpointer v)
1722 return *(const gint*) v;
1727 * @v1: a pointer to a #gint64 key
1728 * @v2: a pointer to a #gint64 key to compare with @v1
1730 * Compares the two #gint64 values being pointed to and returns
1731 * %TRUE if they are equal.
1732 * It can be passed to g_hash_table_new() as the @key_equal_func
1733 * parameter, when using pointers to 64-bit integers as keys in a #GHashTable.
1735 * Returns: %TRUE if the two keys match.
1740 g_int64_equal (gconstpointer v1,
1743 return *((const gint64*) v1) == *((const gint64*) v2);
1748 * @v: a pointer to a #gint64 key
1750 * Converts a pointer to a #gint64 to a hash value.
1751 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1752 * when using pointers to 64-bit integers values as keys in a #GHashTable.
1754 * Returns: a hash value corresponding to the key.
1759 g_int64_hash (gconstpointer v)
1761 return (guint) *(const gint64*) v;
1766 * @v1: a pointer to a #gdouble key
1767 * @v2: a pointer to a #gdouble key to compare with @v1
1769 * Compares the two #gdouble values being pointed to and returns
1770 * %TRUE if they are equal.
1771 * It can be passed to g_hash_table_new() as the @key_equal_func
1772 * parameter, when using pointers to doubles as keys in a #GHashTable.
1774 * Returns: %TRUE if the two keys match.
1779 g_double_equal (gconstpointer v1,
1782 return *((const gdouble*) v1) == *((const gdouble*) v2);
1787 * @v: a pointer to a #gdouble key
1789 * Converts a pointer to a #gdouble to a hash value.
1790 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1791 * when using pointers to doubles as keys in a #GHashTable.
1793 * Returns: a hash value corresponding to the key.
1798 g_double_hash (gconstpointer v)
1800 return (guint) *(const gdouble*) v;