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 * g_direct_hash() is also the appropriate hash function for keys
149 * of the form <literal>GINT_TO_POINTER (n)</literal> (or similar macros).
151 * <!-- FIXME: Need more here. --> The hash values should be evenly
152 * distributed over a fairly large range? The modulus is taken with the
153 * hash table size (a prime number) to find the 'bucket' to place each
154 * key into. The function should also be very fast, since it is called
155 * for each key lookup.
157 * Returns: the hash value corresponding to the key
163 * @value: the value corresponding to the key
164 * @user_data: user data passed to g_hash_table_foreach()
166 * Specifies the type of the function passed to g_hash_table_foreach().
167 * It is called with each key/value pair, together with the @user_data
168 * parameter which is passed to g_hash_table_foreach().
174 * @value: the value associated with the key
175 * @user_data: user data passed to g_hash_table_remove()
177 * Specifies the type of the function passed to
178 * g_hash_table_foreach_remove(). It is called with each key/value
179 * pair, together with the @user_data parameter passed to
180 * g_hash_table_foreach_remove(). It should return %TRUE if the
181 * key/value pair should be removed from the #GHashTable.
183 * Returns: %TRUE if the key/value pair should be removed from the
190 * @b: a value to compare with
192 * Specifies the type of a function used to test two values for
193 * equality. The function should return %TRUE if both values are equal
194 * and %FALSE otherwise.
196 * Returns: %TRUE if @a = @b; %FALSE otherwise
202 * A GHashTableIter structure represents an iterator that can be used
203 * to iterate over the elements of a #GHashTable. GHashTableIter
204 * structures are typically allocated on the stack and then initialized
205 * with g_hash_table_iter_init().
209 * g_hash_table_freeze:
210 * @hash_table: a #GHashTable
212 * This function is deprecated and will be removed in the next major
213 * release of GLib. It does nothing.
218 * @hash_table: a #GHashTable
220 * This function is deprecated and will be removed in the next major
221 * release of GLib. It does nothing.
224 #define HASH_TABLE_MIN_SHIFT 3 /* 1 << 3 == 8 buckets */
226 #define UNUSED_HASH_VALUE 0
227 #define TOMBSTONE_HASH_VALUE 1
228 #define HASH_IS_UNUSED(h_) ((h_) == UNUSED_HASH_VALUE)
229 #define HASH_IS_TOMBSTONE(h_) ((h_) == TOMBSTONE_HASH_VALUE)
230 #define HASH_IS_REAL(h_) ((h_) >= 2)
238 gint noccupied; /* nnodes + tombstones */
245 GEqualFunc key_equal_func;
247 #ifndef G_DISABLE_ASSERT
249 * Tracks the structure of the hash table, not its contents: is only
250 * incremented when a node is added or removed (is not incremented
251 * when the key or data of a node is modified).
255 GDestroyNotify key_destroy_func;
256 GDestroyNotify value_destroy_func;
261 GHashTable *hash_table;
269 /* Each table size has an associated prime modulo (the first prime
270 * lower than the table size) used to find the initial bucket. Probing
271 * then works modulo 2^n. The prime modulo is necessary to get a
272 * good distribution with poor hash functions.
274 static const gint prime_mod [] =
292 65521, /* For 1 << 16 */
307 2147483647 /* For 1 << 31 */
311 g_hash_table_set_shift (GHashTable *hash_table, gint shift)
316 hash_table->size = 1 << shift;
317 hash_table->mod = prime_mod [shift];
319 for (i = 0; i < shift; i++)
325 hash_table->mask = mask;
329 g_hash_table_find_closest_shift (gint n)
340 g_hash_table_set_shift_from_size (GHashTable *hash_table, gint size)
344 shift = g_hash_table_find_closest_shift (size);
345 shift = MAX (shift, HASH_TABLE_MIN_SHIFT);
347 g_hash_table_set_shift (hash_table, shift);
351 * g_hash_table_lookup_node:
352 * @hash_table: our #GHashTable
353 * @key: the key to lookup against
354 * @hash_return: key hash return location
356 * Performs a lookup in the hash table, preserving extra information
357 * usually needed for insertion.
359 * This function first computes the hash value of the key using the
360 * user's hash function.
362 * If an entry in the table matching @key is found then this function
363 * returns the index of that entry in the table, and if not, the
364 * index of an unused node (empty or tombstone) where the key can be
367 * The computed hash value is returned in the variable pointed to
368 * by @hash_return. This is to save insertions from having to compute
369 * the hash record again for the new record.
371 * Returns: index of the described node
374 g_hash_table_lookup_node (GHashTable *hash_table,
381 guint first_tombstone = 0;
382 gboolean have_tombstone = FALSE;
385 hash_value = hash_table->hash_func (key);
386 if (G_UNLIKELY (!HASH_IS_REAL (hash_value)))
389 *hash_return = hash_value;
391 node_index = hash_value % hash_table->mod;
392 node_hash = hash_table->hashes[node_index];
394 while (!HASH_IS_UNUSED (node_hash))
396 /* We first check if our full hash values
397 * are equal so we can avoid calling the full-blown
398 * key equality function in most cases.
400 if (node_hash == hash_value)
402 gpointer node_key = hash_table->keys[node_index];
404 if (hash_table->key_equal_func)
406 if (hash_table->key_equal_func (node_key, key))
409 else if (node_key == key)
414 else if (HASH_IS_TOMBSTONE (node_hash) && !have_tombstone)
416 first_tombstone = node_index;
417 have_tombstone = TRUE;
422 node_index &= hash_table->mask;
423 node_hash = hash_table->hashes[node_index];
427 return first_tombstone;
433 * g_hash_table_remove_node:
434 * @hash_table: our #GHashTable
435 * @node: pointer to node to remove
436 * @notify: %TRUE if the destroy notify handlers are to be called
438 * Removes a node from the hash table and updates the node count.
439 * The node is replaced by a tombstone. No table resize is performed.
441 * If @notify is %TRUE then the destroy notify functions are called
442 * for the key and value of the hash node.
445 g_hash_table_remove_node (GHashTable *hash_table,
452 key = hash_table->keys[i];
453 value = hash_table->values[i];
455 /* Erect tombstone */
456 hash_table->hashes[i] = TOMBSTONE_HASH_VALUE;
459 hash_table->keys[i] = NULL;
460 hash_table->values[i] = NULL;
462 hash_table->nnodes--;
464 if (notify && hash_table->key_destroy_func)
465 hash_table->key_destroy_func (key);
467 if (notify && hash_table->value_destroy_func)
468 hash_table->value_destroy_func (value);
473 * g_hash_table_remove_all_nodes:
474 * @hash_table: our #GHashTable
475 * @notify: %TRUE if the destroy notify handlers are to be called
477 * Removes all nodes from the table. Since this may be a precursor to
478 * freeing the table entirely, no resize is performed.
480 * If @notify is %TRUE then the destroy notify functions are called
481 * for the key and value of the hash node.
484 g_hash_table_remove_all_nodes (GHashTable *hash_table,
491 hash_table->nnodes = 0;
492 hash_table->noccupied = 0;
495 (hash_table->key_destroy_func == NULL &&
496 hash_table->value_destroy_func == NULL))
498 memset (hash_table->hashes, 0, hash_table->size * sizeof (guint));
499 memset (hash_table->keys, 0, hash_table->size * sizeof (gpointer));
500 memset (hash_table->values, 0, hash_table->size * sizeof (gpointer));
505 for (i = 0; i < hash_table->size; i++)
507 if (HASH_IS_REAL (hash_table->hashes[i]))
509 key = hash_table->keys[i];
510 value = hash_table->values[i];
512 hash_table->hashes[i] = UNUSED_HASH_VALUE;
513 hash_table->keys[i] = NULL;
514 hash_table->values[i] = NULL;
516 if (hash_table->key_destroy_func != NULL)
517 hash_table->key_destroy_func (key);
519 if (hash_table->value_destroy_func != NULL)
520 hash_table->value_destroy_func (value);
522 else if (HASH_IS_TOMBSTONE (hash_table->hashes[i]))
524 hash_table->hashes[i] = UNUSED_HASH_VALUE;
530 * g_hash_table_resize:
531 * @hash_table: our #GHashTable
533 * Resizes the hash table to the optimal size based on the number of
534 * nodes currently held. If you call this function then a resize will
535 * occur, even if one does not need to occur.
536 * Use g_hash_table_maybe_resize() instead.
538 * This function may "resize" the hash table to its current size, with
539 * the side effect of cleaning up tombstones and otherwise optimizing
540 * the probe sequences.
543 g_hash_table_resize (GHashTable *hash_table)
546 gpointer *new_values;
551 old_size = hash_table->size;
552 g_hash_table_set_shift_from_size (hash_table, hash_table->nnodes * 2);
554 new_keys = g_new0 (gpointer, hash_table->size);
555 if (hash_table->keys == hash_table->values)
556 new_values = new_keys;
558 new_values = g_new0 (gpointer, hash_table->size);
559 new_hashes = g_new0 (guint, hash_table->size);
561 for (i = 0; i < old_size; i++)
563 guint node_hash = hash_table->hashes[i];
567 if (!HASH_IS_REAL (node_hash))
570 hash_val = node_hash % hash_table->mod;
572 while (!HASH_IS_UNUSED (new_hashes[hash_val]))
576 hash_val &= hash_table->mask;
579 new_hashes[hash_val] = hash_table->hashes[i];
580 new_keys[hash_val] = hash_table->keys[i];
581 new_values[hash_val] = hash_table->values[i];
584 if (hash_table->keys != hash_table->values)
585 g_free (hash_table->values);
587 g_free (hash_table->keys);
588 g_free (hash_table->hashes);
590 hash_table->keys = new_keys;
591 hash_table->values = new_values;
592 hash_table->hashes = new_hashes;
594 hash_table->noccupied = hash_table->nnodes;
598 * g_hash_table_maybe_resize:
599 * @hash_table: our #GHashTable
601 * Resizes the hash table, if needed.
603 * Essentially, calls g_hash_table_resize() if the table has strayed
604 * too far from its ideal size for its number of nodes.
607 g_hash_table_maybe_resize (GHashTable *hash_table)
609 gint noccupied = hash_table->noccupied;
610 gint size = hash_table->size;
612 if ((size > hash_table->nnodes * 4 && size > 1 << HASH_TABLE_MIN_SHIFT) ||
613 (size <= noccupied + (noccupied / 16)))
614 g_hash_table_resize (hash_table);
619 * @hash_func: a function to create a hash value from a key
620 * @key_equal_func: a function to check two keys for equality
622 * Creates a new #GHashTable with a reference count of 1.
624 * Hash values returned by @hash_func are used to determine where keys
625 * are stored within the #GHashTable data structure. The g_direct_hash(),
626 * g_int_hash(), g_int64_hash(), g_double_hash() and g_str_hash()
627 * functions are provided for some common types of keys.
628 * If @hash_func is %NULL, g_direct_hash() is used.
630 * @key_equal_func is used when looking up keys in the #GHashTable.
631 * The g_direct_equal(), g_int_equal(), g_int64_equal(), g_double_equal()
632 * and g_str_equal() functions are provided for the most common types
633 * of keys. If @key_equal_func is %NULL, keys are compared directly in
634 * a similar fashion to g_direct_equal(), but without the overhead of
637 * Return value: a new #GHashTable
640 g_hash_table_new (GHashFunc hash_func,
641 GEqualFunc key_equal_func)
643 return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
648 * g_hash_table_new_full:
649 * @hash_func: a function to create a hash value from a key
650 * @key_equal_func: a function to check two keys for equality
651 * @key_destroy_func: a function to free the memory allocated for the key
652 * used when removing the entry from the #GHashTable, or %NULL
653 * if you don't want to supply such a function.
654 * @value_destroy_func: a function to free the memory allocated for the
655 * value used when removing the entry from the #GHashTable, or %NULL
656 * if you don't want to supply such a function.
658 * Creates a new #GHashTable like g_hash_table_new() with a reference
659 * count of 1 and allows to specify functions to free the memory
660 * allocated for the key and value that get called when removing the
661 * entry from the #GHashTable.
663 * Return value: a new #GHashTable
666 g_hash_table_new_full (GHashFunc hash_func,
667 GEqualFunc key_equal_func,
668 GDestroyNotify key_destroy_func,
669 GDestroyNotify value_destroy_func)
671 GHashTable *hash_table;
673 hash_table = g_slice_new (GHashTable);
674 g_hash_table_set_shift (hash_table, HASH_TABLE_MIN_SHIFT);
675 hash_table->nnodes = 0;
676 hash_table->noccupied = 0;
677 hash_table->hash_func = hash_func ? hash_func : g_direct_hash;
678 hash_table->key_equal_func = key_equal_func;
679 hash_table->ref_count = 1;
680 #ifndef G_DISABLE_ASSERT
681 hash_table->version = 0;
683 hash_table->key_destroy_func = key_destroy_func;
684 hash_table->value_destroy_func = value_destroy_func;
685 hash_table->keys = g_new0 (gpointer, hash_table->size);
686 hash_table->values = hash_table->keys;
687 hash_table->hashes = g_new0 (guint, hash_table->size);
693 * g_hash_table_iter_init:
694 * @iter: an uninitialized #GHashTableIter
695 * @hash_table: a #GHashTable
697 * Initializes a key/value pair iterator and associates it with
698 * @hash_table. Modifying the hash table after calling this function
699 * invalidates the returned iterator.
701 * GHashTableIter iter;
702 * gpointer key, value;
704 * g_hash_table_iter_init (&iter, hash_table);
705 * while (g_hash_table_iter_next (&iter, &key, &value))
707 * /* do something with key and value */
714 g_hash_table_iter_init (GHashTableIter *iter,
715 GHashTable *hash_table)
717 RealIter *ri = (RealIter *) iter;
719 g_return_if_fail (iter != NULL);
720 g_return_if_fail (hash_table != NULL);
722 ri->hash_table = hash_table;
724 #ifndef G_DISABLE_ASSERT
725 ri->version = hash_table->version;
730 * g_hash_table_iter_next:
731 * @iter: an initialized #GHashTableIter
732 * @key: a location to store the key, or %NULL
733 * @value: a location to store the value, or %NULL
735 * Advances @iter and retrieves the key and/or value that are now
736 * pointed to as a result of this advancement. If %FALSE is returned,
737 * @key and @value are not set, and the iterator becomes invalid.
739 * Return value: %FALSE if the end of the #GHashTable has been reached.
744 g_hash_table_iter_next (GHashTableIter *iter,
748 RealIter *ri = (RealIter *) iter;
751 g_return_val_if_fail (iter != NULL, FALSE);
752 #ifndef G_DISABLE_ASSERT
753 g_return_val_if_fail (ri->version == ri->hash_table->version, FALSE);
755 g_return_val_if_fail (ri->position < ri->hash_table->size, FALSE);
757 position = ri->position;
762 if (position >= ri->hash_table->size)
764 ri->position = position;
768 while (!HASH_IS_REAL (ri->hash_table->hashes[position]));
771 *key = ri->hash_table->keys[position];
773 *value = ri->hash_table->values[position];
775 ri->position = position;
780 * g_hash_table_iter_get_hash_table:
781 * @iter: an initialized #GHashTableIter
783 * Returns the #GHashTable associated with @iter.
785 * Return value: the #GHashTable associated with @iter.
790 g_hash_table_iter_get_hash_table (GHashTableIter *iter)
792 g_return_val_if_fail (iter != NULL, NULL);
794 return ((RealIter *) iter)->hash_table;
798 iter_remove_or_steal (RealIter *ri, gboolean notify)
800 g_return_if_fail (ri != NULL);
801 #ifndef G_DISABLE_ASSERT
802 g_return_if_fail (ri->version == ri->hash_table->version);
804 g_return_if_fail (ri->position >= 0);
805 g_return_if_fail (ri->position < ri->hash_table->size);
807 g_hash_table_remove_node (ri->hash_table, ri->position, notify);
809 #ifndef G_DISABLE_ASSERT
811 ri->hash_table->version++;
816 * g_hash_table_iter_remove:
817 * @iter: an initialized #GHashTableIter
819 * Removes the key/value pair currently pointed to by the iterator
820 * from its associated #GHashTable. Can only be called after
821 * g_hash_table_iter_next() returned %TRUE, and cannot be called
822 * more than once for the same key/value pair.
824 * If the #GHashTable was created using g_hash_table_new_full(),
825 * the key and value are freed using the supplied destroy functions,
826 * otherwise you have to make sure that any dynamically allocated
827 * values are freed yourself.
832 g_hash_table_iter_remove (GHashTableIter *iter)
834 iter_remove_or_steal ((RealIter *) iter, TRUE);
838 * g_hash_table_insert_node:
839 * @hash_table: our #GHashTable
840 * @node_index: pointer to node to insert/replace
841 * @key_hash: key hash
842 * @key: key to replace with
843 * @value: value to replace with
845 * Inserts a value at @node_index in the hash table and updates it.
848 g_hash_table_insert_node (GHashTable *hash_table,
853 gboolean keep_new_key)
859 if (G_UNLIKELY (hash_table->keys == hash_table->values && key != value))
860 hash_table->values = g_memdup (hash_table->keys, sizeof (gpointer) * hash_table->size);
862 old_hash = hash_table->hashes[node_index];
863 old_key = hash_table->keys[node_index];
864 old_value = hash_table->values[node_index];
866 if (HASH_IS_REAL (old_hash))
869 hash_table->keys[node_index] = key;
870 hash_table->values[node_index] = value;
874 hash_table->keys[node_index] = key;
875 hash_table->values[node_index] = value;
876 hash_table->hashes[node_index] = key_hash;
878 hash_table->nnodes++;
880 if (HASH_IS_UNUSED (old_hash))
882 /* We replaced an empty node, and not a tombstone */
883 hash_table->noccupied++;
884 g_hash_table_maybe_resize (hash_table);
887 #ifndef G_DISABLE_ASSERT
888 hash_table->version++;
892 if (HASH_IS_REAL (old_hash))
894 if (hash_table->key_destroy_func)
895 hash_table->key_destroy_func (keep_new_key ? old_key : key);
896 if (hash_table->value_destroy_func)
897 hash_table->value_destroy_func (old_value);
902 * g_hash_table_iter_replace:
903 * @iter: an initialized #GHashTableIter
904 * @value: the value to replace with
906 * Replaces the value currently pointed to by the iterator
907 * from its associated #GHashTable. Can only be called after
908 * g_hash_table_iter_next() returned %TRUE.
910 * If you supplied a @value_destroy_func when creating the
911 * #GHashTable, the old value is freed using that function.
916 g_hash_table_iter_replace (GHashTableIter *iter,
923 ri = (RealIter *) iter;
925 g_return_if_fail (ri != NULL);
926 #ifndef G_DISABLE_ASSERT
927 g_return_if_fail (ri->version == ri->hash_table->version);
929 g_return_if_fail (ri->position >= 0);
930 g_return_if_fail (ri->position < ri->hash_table->size);
932 node_hash = ri->hash_table->hashes[ri->position];
933 key = ri->hash_table->keys[ri->position];
935 g_hash_table_insert_node (ri->hash_table, ri->position, node_hash, key, value, TRUE);
937 #ifndef G_DISABLE_ASSERT
939 ri->hash_table->version++;
944 * g_hash_table_iter_steal:
945 * @iter: an initialized #GHashTableIter
947 * Removes the key/value pair currently pointed to by the
948 * iterator from its associated #GHashTable, without calling
949 * the key and value destroy functions. Can only be called
950 * after g_hash_table_iter_next() returned %TRUE, and cannot
951 * be called more than once for the same key/value pair.
956 g_hash_table_iter_steal (GHashTableIter *iter)
958 iter_remove_or_steal ((RealIter *) iter, FALSE);
964 * @hash_table: a valid #GHashTable
966 * Atomically increments the reference count of @hash_table by one.
967 * This function is MT-safe and may be called from any thread.
969 * Return value: the passed in #GHashTable
974 g_hash_table_ref (GHashTable *hash_table)
976 g_return_val_if_fail (hash_table != NULL, NULL);
978 g_atomic_int_inc (&hash_table->ref_count);
984 * g_hash_table_unref:
985 * @hash_table: a valid #GHashTable
987 * Atomically decrements the reference count of @hash_table by one.
988 * If the reference count drops to 0, all keys and values will be
989 * destroyed, and all memory allocated by the hash table is released.
990 * This function is MT-safe and may be called from any thread.
995 g_hash_table_unref (GHashTable *hash_table)
997 g_return_if_fail (hash_table != NULL);
999 if (g_atomic_int_dec_and_test (&hash_table->ref_count))
1001 g_hash_table_remove_all_nodes (hash_table, TRUE);
1002 if (hash_table->keys != hash_table->values)
1003 g_free (hash_table->values);
1004 g_free (hash_table->keys);
1005 g_free (hash_table->hashes);
1006 g_slice_free (GHashTable, hash_table);
1011 * g_hash_table_destroy:
1012 * @hash_table: a #GHashTable
1014 * Destroys all keys and values in the #GHashTable and decrements its
1015 * reference count by 1. If keys and/or values are dynamically allocated,
1016 * you should either free them first or create the #GHashTable with destroy
1017 * notifiers using g_hash_table_new_full(). In the latter case the destroy
1018 * functions you supplied will be called on all keys and values during the
1019 * destruction phase.
1022 g_hash_table_destroy (GHashTable *hash_table)
1024 g_return_if_fail (hash_table != NULL);
1026 g_hash_table_remove_all (hash_table);
1027 g_hash_table_unref (hash_table);
1031 * g_hash_table_lookup:
1032 * @hash_table: a #GHashTable
1033 * @key: the key to look up
1035 * Looks up a key in a #GHashTable. Note that this function cannot
1036 * distinguish between a key that is not present and one which is present
1037 * and has the value %NULL. If you need this distinction, use
1038 * g_hash_table_lookup_extended().
1040 * Return value: the associated value, or %NULL if the key is not found
1043 g_hash_table_lookup (GHashTable *hash_table,
1049 g_return_val_if_fail (hash_table != NULL, NULL);
1051 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1053 return HASH_IS_REAL (hash_table->hashes[node_index])
1054 ? hash_table->values[node_index]
1059 * g_hash_table_lookup_extended:
1060 * @hash_table: a #GHashTable
1061 * @lookup_key: the key to look up
1062 * @orig_key: return location for the original key, or %NULL
1063 * @value: return location for the value associated with the key, or %NULL
1065 * Looks up a key in the #GHashTable, returning the original key and the
1066 * associated value and a #gboolean which is %TRUE if the key was found. This
1067 * is useful if you need to free the memory allocated for the original key,
1068 * for example before calling g_hash_table_remove().
1070 * You can actually pass %NULL for @lookup_key to test
1071 * whether the %NULL key exists, provided the hash and equal functions
1072 * of @hash_table are %NULL-safe.
1074 * Return value: %TRUE if the key was found in the #GHashTable
1077 g_hash_table_lookup_extended (GHashTable *hash_table,
1078 gconstpointer lookup_key,
1085 g_return_val_if_fail (hash_table != NULL, FALSE);
1087 node_index = g_hash_table_lookup_node (hash_table, lookup_key, &node_hash);
1089 if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1093 *orig_key = hash_table->keys[node_index];
1096 *value = hash_table->values[node_index];
1102 * g_hash_table_insert_internal:
1103 * @hash_table: our #GHashTable
1104 * @key: the key to insert
1105 * @value: the value to insert
1106 * @keep_new_key: if %TRUE and this key already exists in the table
1107 * then call the destroy notify function on the old key. If %FALSE
1108 * then call the destroy notify function on the new key.
1110 * Implements the common logic for the g_hash_table_insert() and
1111 * g_hash_table_replace() functions.
1113 * Do a lookup of @key. If it is found, replace it with the new
1114 * @value (and perhaps the new @key). If it is not found, create
1118 g_hash_table_insert_internal (GHashTable *hash_table,
1121 gboolean keep_new_key)
1126 g_return_if_fail (hash_table != NULL);
1128 node_index = g_hash_table_lookup_node (hash_table, key, &key_hash);
1130 g_hash_table_insert_node (hash_table, node_index, key_hash, key, value, keep_new_key);
1134 * g_hash_table_insert:
1135 * @hash_table: a #GHashTable
1136 * @key: a key to insert
1137 * @value: the value to associate with the key
1139 * Inserts a new key and value into a #GHashTable.
1141 * If the key already exists in the #GHashTable its current
1142 * value is replaced with the new value. If you supplied a
1143 * @value_destroy_func when creating the #GHashTable, the old
1144 * value is freed using that function. If you supplied a
1145 * @key_destroy_func when creating the #GHashTable, the passed
1146 * key is freed using that function.
1149 g_hash_table_insert (GHashTable *hash_table,
1153 g_hash_table_insert_internal (hash_table, key, value, FALSE);
1157 * g_hash_table_replace:
1158 * @hash_table: a #GHashTable
1159 * @key: a key to insert
1160 * @value: the value to associate with the key
1162 * Inserts a new key and value into a #GHashTable similar to
1163 * g_hash_table_insert(). The difference is that if the key
1164 * already exists in the #GHashTable, it gets replaced by the
1165 * new key. If you supplied a @value_destroy_func when creating
1166 * the #GHashTable, the old value is freed using that function.
1167 * If you supplied a @key_destroy_func when creating the
1168 * #GHashTable, the old key is freed using that function.
1171 g_hash_table_replace (GHashTable *hash_table,
1175 g_hash_table_insert_internal (hash_table, key, value, TRUE);
1179 * g_hash_table_remove_internal:
1180 * @hash_table: our #GHashTable
1181 * @key: the key to remove
1182 * @notify: %TRUE if the destroy notify handlers are to be called
1183 * Return value: %TRUE if a node was found and removed, else %FALSE
1185 * Implements the common logic for the g_hash_table_remove() and
1186 * g_hash_table_steal() functions.
1188 * Do a lookup of @key and remove it if it is found, calling the
1189 * destroy notify handlers only if @notify is %TRUE.
1192 g_hash_table_remove_internal (GHashTable *hash_table,
1199 g_return_val_if_fail (hash_table != NULL, FALSE);
1201 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1203 if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1206 g_hash_table_remove_node (hash_table, node_index, notify);
1207 g_hash_table_maybe_resize (hash_table);
1209 #ifndef G_DISABLE_ASSERT
1210 hash_table->version++;
1217 * g_hash_table_remove:
1218 * @hash_table: a #GHashTable
1219 * @key: the key to remove
1221 * Removes a key and its associated value from a #GHashTable.
1223 * If the #GHashTable was created using g_hash_table_new_full(), the
1224 * key and value are freed using the supplied destroy functions, otherwise
1225 * you have to make sure that any dynamically allocated values are freed
1228 * Returns: %TRUE if the key was found and removed from the #GHashTable
1231 g_hash_table_remove (GHashTable *hash_table,
1234 return g_hash_table_remove_internal (hash_table, key, TRUE);
1238 * g_hash_table_steal:
1239 * @hash_table: a #GHashTable
1240 * @key: the key to remove
1242 * Removes a key and its associated value from a #GHashTable without
1243 * calling the key and value destroy functions.
1245 * Returns: %TRUE if the key was found and removed from the #GHashTable
1248 g_hash_table_steal (GHashTable *hash_table,
1251 return g_hash_table_remove_internal (hash_table, key, FALSE);
1255 * g_hash_table_remove_all:
1256 * @hash_table: a #GHashTable
1258 * Removes all keys and their associated values from a #GHashTable.
1260 * If the #GHashTable was created using g_hash_table_new_full(),
1261 * the keys and values are freed using the supplied destroy functions,
1262 * otherwise you have to make sure that any dynamically allocated
1263 * values are freed yourself.
1268 g_hash_table_remove_all (GHashTable *hash_table)
1270 g_return_if_fail (hash_table != NULL);
1272 #ifndef G_DISABLE_ASSERT
1273 if (hash_table->nnodes != 0)
1274 hash_table->version++;
1277 g_hash_table_remove_all_nodes (hash_table, TRUE);
1278 g_hash_table_maybe_resize (hash_table);
1282 * g_hash_table_steal_all:
1283 * @hash_table: a #GHashTable
1285 * Removes all keys and their associated values from a #GHashTable
1286 * without calling the key and value destroy functions.
1291 g_hash_table_steal_all (GHashTable *hash_table)
1293 g_return_if_fail (hash_table != NULL);
1295 #ifndef G_DISABLE_ASSERT
1296 if (hash_table->nnodes != 0)
1297 hash_table->version++;
1300 g_hash_table_remove_all_nodes (hash_table, FALSE);
1301 g_hash_table_maybe_resize (hash_table);
1305 * g_hash_table_foreach_remove_or_steal:
1306 * @hash_table: a #GHashTable
1307 * @func: the user's callback function
1308 * @user_data: data for @func
1309 * @notify: %TRUE if the destroy notify handlers are to be called
1311 * Implements the common logic for g_hash_table_foreach_remove()
1312 * and g_hash_table_foreach_steal().
1314 * Iterates over every node in the table, calling @func with the key
1315 * and value of the node (and @user_data). If @func returns %TRUE the
1316 * node is removed from the table.
1318 * If @notify is true then the destroy notify handlers will be called
1319 * for each removed node.
1322 g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
1329 #ifndef G_DISABLE_ASSERT
1330 gint version = hash_table->version;
1333 for (i = 0; i < hash_table->size; i++)
1335 guint node_hash = hash_table->hashes[i];
1336 gpointer node_key = hash_table->keys[i];
1337 gpointer node_value = hash_table->values[i];
1339 if (HASH_IS_REAL (node_hash) &&
1340 (* func) (node_key, node_value, user_data))
1342 g_hash_table_remove_node (hash_table, i, notify);
1346 #ifndef G_DISABLE_ASSERT
1347 g_return_val_if_fail (version == hash_table->version, 0);
1351 g_hash_table_maybe_resize (hash_table);
1353 #ifndef G_DISABLE_ASSERT
1355 hash_table->version++;
1362 * g_hash_table_foreach_remove:
1363 * @hash_table: a #GHashTable
1364 * @func: the function to call for each key/value pair
1365 * @user_data: user data to pass to the function
1367 * Calls the given function for each key/value pair in the
1368 * #GHashTable. If the function returns %TRUE, then the key/value
1369 * pair is removed from the #GHashTable. If you supplied key or
1370 * value destroy functions when creating the #GHashTable, they are
1371 * used to free the memory allocated for the removed keys and values.
1373 * See #GHashTableIter for an alternative way to loop over the
1374 * key/value pairs in the hash table.
1376 * Return value: the number of key/value pairs removed
1379 g_hash_table_foreach_remove (GHashTable *hash_table,
1383 g_return_val_if_fail (hash_table != NULL, 0);
1384 g_return_val_if_fail (func != NULL, 0);
1386 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
1390 * g_hash_table_foreach_steal:
1391 * @hash_table: a #GHashTable
1392 * @func: the function to call for each key/value pair
1393 * @user_data: user data to pass to the function
1395 * Calls the given function for each key/value pair in the
1396 * #GHashTable. If the function returns %TRUE, then the key/value
1397 * pair is removed from the #GHashTable, but no key or value
1398 * destroy functions are called.
1400 * See #GHashTableIter for an alternative way to loop over the
1401 * key/value pairs in the hash table.
1403 * Return value: the number of key/value pairs removed.
1406 g_hash_table_foreach_steal (GHashTable *hash_table,
1410 g_return_val_if_fail (hash_table != NULL, 0);
1411 g_return_val_if_fail (func != NULL, 0);
1413 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
1417 * g_hash_table_foreach:
1418 * @hash_table: a #GHashTable
1419 * @func: the function to call for each key/value pair
1420 * @user_data: user data to pass to the function
1422 * Calls the given function for each of the key/value pairs in the
1423 * #GHashTable. The function is passed the key and value of each
1424 * pair, and the given @user_data parameter. The hash table may not
1425 * be modified while iterating over it (you can't add/remove
1426 * items). To remove all items matching a predicate, use
1427 * g_hash_table_foreach_remove().
1429 * See g_hash_table_find() for performance caveats for linear
1430 * order searches in contrast to g_hash_table_lookup().
1433 g_hash_table_foreach (GHashTable *hash_table,
1438 #ifndef G_DISABLE_ASSERT
1439 gint version = hash_table->version;
1442 g_return_if_fail (hash_table != NULL);
1443 g_return_if_fail (func != NULL);
1445 for (i = 0; i < hash_table->size; i++)
1447 guint node_hash = hash_table->hashes[i];
1448 gpointer node_key = hash_table->keys[i];
1449 gpointer node_value = hash_table->values[i];
1451 if (HASH_IS_REAL (node_hash))
1452 (* func) (node_key, node_value, user_data);
1454 #ifndef G_DISABLE_ASSERT
1455 g_return_if_fail (version == hash_table->version);
1461 * g_hash_table_find:
1462 * @hash_table: a #GHashTable
1463 * @predicate: function to test the key/value pairs for a certain property
1464 * @user_data: user data to pass to the function
1466 * Calls the given function for key/value pairs in the #GHashTable
1467 * until @predicate returns %TRUE. The function is passed the key
1468 * and value of each pair, and the given @user_data parameter. The
1469 * hash table may not be modified while iterating over it (you can't
1470 * add/remove items).
1472 * Note, that hash tables are really only optimized for forward
1473 * lookups, i.e. g_hash_table_lookup(). So code that frequently issues
1474 * g_hash_table_find() or g_hash_table_foreach() (e.g. in the order of
1475 * once per every entry in a hash table) should probably be reworked
1476 * to use additional or different data structures for reverse lookups
1477 * (keep in mind that an O(n) find/foreach operation issued for all n
1478 * values in a hash table ends up needing O(n*n) operations).
1480 * Return value: The value of the first key/value pair is returned,
1481 * for which @predicate evaluates to %TRUE. If no pair with the
1482 * requested property is found, %NULL is returned.
1487 g_hash_table_find (GHashTable *hash_table,
1492 #ifndef G_DISABLE_ASSERT
1493 gint version = hash_table->version;
1497 g_return_val_if_fail (hash_table != NULL, NULL);
1498 g_return_val_if_fail (predicate != NULL, NULL);
1502 for (i = 0; i < hash_table->size; i++)
1504 guint node_hash = hash_table->hashes[i];
1505 gpointer node_key = hash_table->keys[i];
1506 gpointer node_value = hash_table->values[i];
1508 if (HASH_IS_REAL (node_hash))
1509 match = predicate (node_key, node_value, user_data);
1511 #ifndef G_DISABLE_ASSERT
1512 g_return_val_if_fail (version == hash_table->version, NULL);
1523 * g_hash_table_size:
1524 * @hash_table: a #GHashTable
1526 * Returns the number of elements contained in the #GHashTable.
1528 * Return value: the number of key/value pairs in the #GHashTable.
1531 g_hash_table_size (GHashTable *hash_table)
1533 g_return_val_if_fail (hash_table != NULL, 0);
1535 return hash_table->nnodes;
1539 * g_hash_table_get_keys:
1540 * @hash_table: a #GHashTable
1542 * Retrieves every key inside @hash_table. The returned data
1543 * is valid until @hash_table is modified.
1545 * Return value: a #GList containing all the keys inside the hash
1546 * table. The content of the list is owned by the hash table and
1547 * should not be modified or freed. Use g_list_free() when done
1553 g_hash_table_get_keys (GHashTable *hash_table)
1558 g_return_val_if_fail (hash_table != NULL, NULL);
1561 for (i = 0; i < hash_table->size; i++)
1563 if (HASH_IS_REAL (hash_table->hashes[i]))
1564 retval = g_list_prepend (retval, hash_table->keys[i]);
1571 * g_hash_table_get_values:
1572 * @hash_table: a #GHashTable
1574 * Retrieves every value inside @hash_table. The returned data
1575 * is valid until @hash_table is modified.
1577 * Return value: a #GList containing all the values inside the hash
1578 * table. The content of the list is owned by the hash table and
1579 * should not be modified or freed. Use g_list_free() when done
1585 g_hash_table_get_values (GHashTable *hash_table)
1590 g_return_val_if_fail (hash_table != NULL, NULL);
1593 for (i = 0; i < hash_table->size; i++)
1595 if (HASH_IS_REAL (hash_table->hashes[i]))
1596 retval = g_list_prepend (retval, hash_table->values[i]);
1608 * @v2: a key to compare with @v1
1610 * Compares two strings for byte-by-byte equality and returns %TRUE
1611 * if they are equal. It can be passed to g_hash_table_new() as the
1612 * @key_equal_func parameter, when using strings as keys in a #GHashTable.
1614 * Note that this function is primarily meant as a hash table comparison
1615 * function. For a general-purpose, %NULL-safe string comparison function,
1618 * Returns: %TRUE if the two keys match
1621 g_str_equal (gconstpointer v1,
1624 const gchar *string1 = v1;
1625 const gchar *string2 = v2;
1627 return strcmp (string1, string2) == 0;
1634 * Converts a string to a hash value.
1636 * This function implements the widely used "djb" hash apparently posted
1637 * by Daniel Bernstein to comp.lang.c some time ago. The 32 bit
1638 * unsigned hash value starts at 5381 and for each byte 'c' in the
1639 * string, is updated: <literal>hash = hash * 33 + c</literal>. This
1640 * function uses the signed value of each byte.
1642 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1643 * when using strings as keys in a #GHashTable.
1645 * Returns: a hash value corresponding to the key
1648 g_str_hash (gconstpointer v)
1650 const signed char *p;
1653 for (p = v; *p != '\0'; p++)
1654 h = (h << 5) + h + *p;
1661 * @v: (allow-none): a #gpointer key
1663 * Converts a gpointer to a hash value.
1664 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1665 * when using opaque pointers compared by pointer value as keys in a
1668 * This hash function is also appropriate for keys that are integers stored
1669 * in pointers, such as <literal>GINT_TO_POINTER (n)</literal>.
1671 * Returns: a hash value corresponding to the key.
1674 g_direct_hash (gconstpointer v)
1676 return GPOINTER_TO_UINT (v);
1681 * @v1: (allow-none): a key
1682 * @v2: (allow-none): a key to compare with @v1
1684 * Compares two #gpointer arguments and returns %TRUE if they are equal.
1685 * It can be passed to g_hash_table_new() as the @key_equal_func
1686 * parameter, when using opaque pointers compared by pointer value as keys
1689 * This equality function is also appropriate for keys that are integers stored
1690 * in pointers, such as <literal>GINT_TO_POINTER (n)</literal>.
1692 * Returns: %TRUE if the two keys match.
1695 g_direct_equal (gconstpointer v1,
1703 * @v1: a pointer to a #gint key
1704 * @v2: a pointer to a #gint key to compare with @v1
1706 * Compares the two #gint values being pointed to and returns
1707 * %TRUE if they are equal.
1708 * It can be passed to g_hash_table_new() as the @key_equal_func
1709 * parameter, when using pointers to integers as keys in a #HashTable.
1711 * Note that this function acts on pointers to #gint, not on #gint directly:
1712 * if your hash table's keys are of the form
1713 * <literal>GINT_TO_POINTER (n)</literal>, use g_direct_equal() instead.
1715 * Returns: %TRUE if the two keys match.
1718 g_int_equal (gconstpointer v1,
1721 return *((const gint*) v1) == *((const gint*) v2);
1726 * @v: a pointer to a #gint key
1728 * Converts a pointer to a #gint to a hash value.
1729 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1730 * when using pointers to integers values as keys in a #GHashTable.
1732 * Note that this function acts on pointers to #gint, not on #gint directly:
1733 * if your hash table's keys are of the form
1734 * <literal>GINT_TO_POINTER (n)</literal>, use g_direct_hash() instead.
1736 * Returns: a hash value corresponding to the key.
1739 g_int_hash (gconstpointer v)
1741 return *(const gint*) v;
1746 * @v1: a pointer to a #gint64 key
1747 * @v2: a pointer to a #gint64 key to compare with @v1
1749 * Compares the two #gint64 values being pointed to and returns
1750 * %TRUE if they are equal.
1751 * It can be passed to g_hash_table_new() as the @key_equal_func
1752 * parameter, when using pointers to 64-bit integers as keys in a #GHashTable.
1754 * Returns: %TRUE if the two keys match.
1759 g_int64_equal (gconstpointer v1,
1762 return *((const gint64*) v1) == *((const gint64*) v2);
1767 * @v: a pointer to a #gint64 key
1769 * Converts a pointer to a #gint64 to a hash value.
1770 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1771 * when using pointers to 64-bit integers values as keys in a #GHashTable.
1773 * Returns: a hash value corresponding to the key.
1778 g_int64_hash (gconstpointer v)
1780 return (guint) *(const gint64*) v;
1785 * @v1: a pointer to a #gdouble key
1786 * @v2: a pointer to a #gdouble key to compare with @v1
1788 * Compares the two #gdouble values being pointed to and returns
1789 * %TRUE if they are equal.
1790 * It can be passed to g_hash_table_new() as the @key_equal_func
1791 * parameter, when using pointers to doubles as keys in a #GHashTable.
1793 * Returns: %TRUE if the two keys match.
1798 g_double_equal (gconstpointer v1,
1801 return *((const gdouble*) v1) == *((const gdouble*) v2);
1806 * @v: a pointer to a #gdouble key
1808 * Converts a pointer to a #gdouble to a hash value.
1809 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1810 * when using pointers to doubles as keys in a #GHashTable.
1812 * Returns: a hash value corresponding to the key.
1817 g_double_hash (gconstpointer v)
1819 return (guint) *(const gdouble*) v;