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_add (GHashTable *set,
108 * g_hash_table_replace (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);
127 * As of version 2.32, there is also a g_hash_table_add() function to
128 * add a key to a #GHashTable that is being used as a set.
134 * The #GHashTable struct is an opaque data structure to represent a
135 * <link linkend="glib-Hash-Tables">Hash Table</link>. It should only be
136 * accessed via the following functions.
143 * Specifies the type of the hash function which is passed to
144 * g_hash_table_new() when a #GHashTable is created.
146 * The function is passed a key and should return a #guint hash value.
147 * The functions g_direct_hash(), g_int_hash() and g_str_hash() provide
148 * hash functions which can be used when the key is a #gpointer, #gint*,
149 * and #gchar* respectively.
151 * g_direct_hash() is also the appropriate hash function for keys
152 * of the form <literal>GINT_TO_POINTER (n)</literal> (or similar macros).
154 * <!-- FIXME: Need more here. --> A good hash functions should produce
155 * hash values that are evenly distributed over a fairly large range.
156 * The modulus is taken with the hash table size (a prime number) to
157 * find the 'bucket' to place each key into. The function should also
158 * be very fast, since it is called for each key lookup.
160 * Note that the hash functions provided by GLib have these qualities,
161 * but are not particularly robust against manufactured keys that
162 * cause hash collisions. Therefore, you should consider choosing
163 * a more secure hash function when using a GHashTable with keys
164 * that originate in untrusted data (such as HTTP requests).
165 * Using g_str_hash() in that situation might make your application
166 * vulerable to <ulink url="https://lwn.net/Articles/474912/">Algorithmic Complexity Attacks</ulink>.
168 * The key to choosing a good hash is unpredictability. Even
169 * cryptographic hashes are very easy to find collisions for when the
170 * remainder is taken modulo a somewhat predictable prime number. There
171 * must be an element of randomness that an attacker is unable to guess.
173 * Returns: the hash value corresponding to the key
179 * @value: the value corresponding to the key
180 * @user_data: user data passed to g_hash_table_foreach()
182 * Specifies the type of the function passed to g_hash_table_foreach().
183 * It is called with each key/value pair, together with the @user_data
184 * parameter which is passed to g_hash_table_foreach().
190 * @value: the value associated with the key
191 * @user_data: user data passed to g_hash_table_remove()
193 * Specifies the type of the function passed to
194 * g_hash_table_foreach_remove(). It is called with each key/value
195 * pair, together with the @user_data parameter passed to
196 * g_hash_table_foreach_remove(). It should return %TRUE if the
197 * key/value pair should be removed from the #GHashTable.
199 * Returns: %TRUE if the key/value pair should be removed from the
206 * @b: a value to compare with
208 * Specifies the type of a function used to test two values for
209 * equality. The function should return %TRUE if both values are equal
210 * and %FALSE otherwise.
212 * Returns: %TRUE if @a = @b; %FALSE otherwise
218 * A GHashTableIter structure represents an iterator that can be used
219 * to iterate over the elements of a #GHashTable. GHashTableIter
220 * structures are typically allocated on the stack and then initialized
221 * with g_hash_table_iter_init().
225 * g_hash_table_freeze:
226 * @hash_table: a #GHashTable
228 * This function is deprecated and will be removed in the next major
229 * release of GLib. It does nothing.
234 * @hash_table: a #GHashTable
236 * This function is deprecated and will be removed in the next major
237 * release of GLib. It does nothing.
240 #define HASH_TABLE_MIN_SHIFT 3 /* 1 << 3 == 8 buckets */
242 #define UNUSED_HASH_VALUE 0
243 #define TOMBSTONE_HASH_VALUE 1
244 #define HASH_IS_UNUSED(h_) ((h_) == UNUSED_HASH_VALUE)
245 #define HASH_IS_TOMBSTONE(h_) ((h_) == TOMBSTONE_HASH_VALUE)
246 #define HASH_IS_REAL(h_) ((h_) >= 2)
254 gint noccupied; /* nnodes + tombstones */
261 GEqualFunc key_equal_func;
263 #ifndef G_DISABLE_ASSERT
265 * Tracks the structure of the hash table, not its contents: is only
266 * incremented when a node is added or removed (is not incremented
267 * when the key or data of a node is modified).
271 GDestroyNotify key_destroy_func;
272 GDestroyNotify value_destroy_func;
277 GHashTable *hash_table;
285 /* Each table size has an associated prime modulo (the first prime
286 * lower than the table size) used to find the initial bucket. Probing
287 * then works modulo 2^n. The prime modulo is necessary to get a
288 * good distribution with poor hash functions.
290 static const gint prime_mod [] =
308 65521, /* For 1 << 16 */
323 2147483647 /* For 1 << 31 */
327 g_hash_table_set_shift (GHashTable *hash_table, gint shift)
332 hash_table->size = 1 << shift;
333 hash_table->mod = prime_mod [shift];
335 for (i = 0; i < shift; i++)
341 hash_table->mask = mask;
345 g_hash_table_find_closest_shift (gint n)
356 g_hash_table_set_shift_from_size (GHashTable *hash_table, gint size)
360 shift = g_hash_table_find_closest_shift (size);
361 shift = MAX (shift, HASH_TABLE_MIN_SHIFT);
363 g_hash_table_set_shift (hash_table, shift);
367 * g_hash_table_lookup_node:
368 * @hash_table: our #GHashTable
369 * @key: the key to lookup against
370 * @hash_return: key hash return location
372 * Performs a lookup in the hash table, preserving extra information
373 * usually needed for insertion.
375 * This function first computes the hash value of the key using the
376 * user's hash function.
378 * If an entry in the table matching @key is found then this function
379 * returns the index of that entry in the table, and if not, the
380 * index of an unused node (empty or tombstone) where the key can be
383 * The computed hash value is returned in the variable pointed to
384 * by @hash_return. This is to save insertions from having to compute
385 * the hash record again for the new record.
387 * Returns: index of the described node
390 g_hash_table_lookup_node (GHashTable *hash_table,
397 guint first_tombstone = 0;
398 gboolean have_tombstone = FALSE;
401 hash_value = hash_table->hash_func (key);
402 if (G_UNLIKELY (!HASH_IS_REAL (hash_value)))
405 *hash_return = hash_value;
407 node_index = hash_value % hash_table->mod;
408 node_hash = hash_table->hashes[node_index];
410 while (!HASH_IS_UNUSED (node_hash))
412 /* We first check if our full hash values
413 * are equal so we can avoid calling the full-blown
414 * key equality function in most cases.
416 if (node_hash == hash_value)
418 gpointer node_key = hash_table->keys[node_index];
420 if (hash_table->key_equal_func)
422 if (hash_table->key_equal_func (node_key, key))
425 else if (node_key == key)
430 else if (HASH_IS_TOMBSTONE (node_hash) && !have_tombstone)
432 first_tombstone = node_index;
433 have_tombstone = TRUE;
438 node_index &= hash_table->mask;
439 node_hash = hash_table->hashes[node_index];
443 return first_tombstone;
449 * g_hash_table_remove_node:
450 * @hash_table: our #GHashTable
451 * @node: pointer to node to remove
452 * @notify: %TRUE if the destroy notify handlers are to be called
454 * Removes a node from the hash table and updates the node count.
455 * The node is replaced by a tombstone. No table resize is performed.
457 * If @notify is %TRUE then the destroy notify functions are called
458 * for the key and value of the hash node.
461 g_hash_table_remove_node (GHashTable *hash_table,
468 key = hash_table->keys[i];
469 value = hash_table->values[i];
471 /* Erect tombstone */
472 hash_table->hashes[i] = TOMBSTONE_HASH_VALUE;
475 hash_table->keys[i] = NULL;
476 hash_table->values[i] = NULL;
478 hash_table->nnodes--;
480 if (notify && hash_table->key_destroy_func)
481 hash_table->key_destroy_func (key);
483 if (notify && hash_table->value_destroy_func)
484 hash_table->value_destroy_func (value);
489 * g_hash_table_remove_all_nodes:
490 * @hash_table: our #GHashTable
491 * @notify: %TRUE if the destroy notify handlers are to be called
493 * Removes all nodes from the table. Since this may be a precursor to
494 * freeing the table entirely, no resize is performed.
496 * If @notify is %TRUE then the destroy notify functions are called
497 * for the key and value of the hash node.
500 g_hash_table_remove_all_nodes (GHashTable *hash_table,
507 hash_table->nnodes = 0;
508 hash_table->noccupied = 0;
511 (hash_table->key_destroy_func == NULL &&
512 hash_table->value_destroy_func == NULL))
514 memset (hash_table->hashes, 0, hash_table->size * sizeof (guint));
515 memset (hash_table->keys, 0, hash_table->size * sizeof (gpointer));
516 memset (hash_table->values, 0, hash_table->size * sizeof (gpointer));
521 for (i = 0; i < hash_table->size; i++)
523 if (HASH_IS_REAL (hash_table->hashes[i]))
525 key = hash_table->keys[i];
526 value = hash_table->values[i];
528 hash_table->hashes[i] = UNUSED_HASH_VALUE;
529 hash_table->keys[i] = NULL;
530 hash_table->values[i] = NULL;
532 if (hash_table->key_destroy_func != NULL)
533 hash_table->key_destroy_func (key);
535 if (hash_table->value_destroy_func != NULL)
536 hash_table->value_destroy_func (value);
538 else if (HASH_IS_TOMBSTONE (hash_table->hashes[i]))
540 hash_table->hashes[i] = UNUSED_HASH_VALUE;
546 * g_hash_table_resize:
547 * @hash_table: our #GHashTable
549 * Resizes the hash table to the optimal size based on the number of
550 * nodes currently held. If you call this function then a resize will
551 * occur, even if one does not need to occur.
552 * Use g_hash_table_maybe_resize() instead.
554 * This function may "resize" the hash table to its current size, with
555 * the side effect of cleaning up tombstones and otherwise optimizing
556 * the probe sequences.
559 g_hash_table_resize (GHashTable *hash_table)
562 gpointer *new_values;
567 old_size = hash_table->size;
568 g_hash_table_set_shift_from_size (hash_table, hash_table->nnodes * 2);
570 new_keys = g_new0 (gpointer, hash_table->size);
571 if (hash_table->keys == hash_table->values)
572 new_values = new_keys;
574 new_values = g_new0 (gpointer, hash_table->size);
575 new_hashes = g_new0 (guint, hash_table->size);
577 for (i = 0; i < old_size; i++)
579 guint node_hash = hash_table->hashes[i];
583 if (!HASH_IS_REAL (node_hash))
586 hash_val = node_hash % hash_table->mod;
588 while (!HASH_IS_UNUSED (new_hashes[hash_val]))
592 hash_val &= hash_table->mask;
595 new_hashes[hash_val] = hash_table->hashes[i];
596 new_keys[hash_val] = hash_table->keys[i];
597 new_values[hash_val] = hash_table->values[i];
600 if (hash_table->keys != hash_table->values)
601 g_free (hash_table->values);
603 g_free (hash_table->keys);
604 g_free (hash_table->hashes);
606 hash_table->keys = new_keys;
607 hash_table->values = new_values;
608 hash_table->hashes = new_hashes;
610 hash_table->noccupied = hash_table->nnodes;
614 * g_hash_table_maybe_resize:
615 * @hash_table: our #GHashTable
617 * Resizes the hash table, if needed.
619 * Essentially, calls g_hash_table_resize() if the table has strayed
620 * too far from its ideal size for its number of nodes.
623 g_hash_table_maybe_resize (GHashTable *hash_table)
625 gint noccupied = hash_table->noccupied;
626 gint size = hash_table->size;
628 if ((size > hash_table->nnodes * 4 && size > 1 << HASH_TABLE_MIN_SHIFT) ||
629 (size <= noccupied + (noccupied / 16)))
630 g_hash_table_resize (hash_table);
635 * @hash_func: a function to create a hash value from a key
636 * @key_equal_func: a function to check two keys for equality
638 * Creates a new #GHashTable with a reference count of 1.
640 * Hash values returned by @hash_func are used to determine where keys
641 * are stored within the #GHashTable data structure. The g_direct_hash(),
642 * g_int_hash(), g_int64_hash(), g_double_hash() and g_str_hash()
643 * functions are provided for some common types of keys.
644 * If @hash_func is %NULL, g_direct_hash() is used.
646 * @key_equal_func is used when looking up keys in the #GHashTable.
647 * The g_direct_equal(), g_int_equal(), g_int64_equal(), g_double_equal()
648 * and g_str_equal() functions are provided for the most common types
649 * of keys. If @key_equal_func is %NULL, keys are compared directly in
650 * a similar fashion to g_direct_equal(), but without the overhead of
653 * Return value: a new #GHashTable
656 g_hash_table_new (GHashFunc hash_func,
657 GEqualFunc key_equal_func)
659 return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
664 * g_hash_table_new_full:
665 * @hash_func: a function to create a hash value from a key
666 * @key_equal_func: a function to check two keys for equality
667 * @key_destroy_func: a function to free the memory allocated for the key
668 * used when removing the entry from the #GHashTable, or %NULL
669 * if you don't want to supply such a function.
670 * @value_destroy_func: a function to free the memory allocated for the
671 * value used when removing the entry from the #GHashTable, or %NULL
672 * if you don't want to supply such a function.
674 * Creates a new #GHashTable like g_hash_table_new() with a reference
675 * count of 1 and allows to specify functions to free the memory
676 * allocated for the key and value that get called when removing the
677 * entry from the #GHashTable.
679 * Return value: a new #GHashTable
682 g_hash_table_new_full (GHashFunc hash_func,
683 GEqualFunc key_equal_func,
684 GDestroyNotify key_destroy_func,
685 GDestroyNotify value_destroy_func)
687 GHashTable *hash_table;
689 hash_table = g_slice_new (GHashTable);
690 g_hash_table_set_shift (hash_table, HASH_TABLE_MIN_SHIFT);
691 hash_table->nnodes = 0;
692 hash_table->noccupied = 0;
693 hash_table->hash_func = hash_func ? hash_func : g_direct_hash;
694 hash_table->key_equal_func = key_equal_func;
695 hash_table->ref_count = 1;
696 #ifndef G_DISABLE_ASSERT
697 hash_table->version = 0;
699 hash_table->key_destroy_func = key_destroy_func;
700 hash_table->value_destroy_func = value_destroy_func;
701 hash_table->keys = g_new0 (gpointer, hash_table->size);
702 hash_table->values = hash_table->keys;
703 hash_table->hashes = g_new0 (guint, hash_table->size);
709 * g_hash_table_iter_init:
710 * @iter: an uninitialized #GHashTableIter
711 * @hash_table: a #GHashTable
713 * Initializes a key/value pair iterator and associates it with
714 * @hash_table. Modifying the hash table after calling this function
715 * invalidates the returned iterator.
717 * GHashTableIter iter;
718 * gpointer key, value;
720 * g_hash_table_iter_init (&iter, hash_table);
721 * while (g_hash_table_iter_next (&iter, &key, &value))
723 * /* do something with key and value */
730 g_hash_table_iter_init (GHashTableIter *iter,
731 GHashTable *hash_table)
733 RealIter *ri = (RealIter *) iter;
735 g_return_if_fail (iter != NULL);
736 g_return_if_fail (hash_table != NULL);
738 ri->hash_table = hash_table;
740 #ifndef G_DISABLE_ASSERT
741 ri->version = hash_table->version;
746 * g_hash_table_iter_next:
747 * @iter: an initialized #GHashTableIter
748 * @key: a location to store the key, or %NULL
749 * @value: a location to store the value, or %NULL
751 * Advances @iter and retrieves the key and/or value that are now
752 * pointed to as a result of this advancement. If %FALSE is returned,
753 * @key and @value are not set, and the iterator becomes invalid.
755 * Return value: %FALSE if the end of the #GHashTable has been reached.
760 g_hash_table_iter_next (GHashTableIter *iter,
764 RealIter *ri = (RealIter *) iter;
767 g_return_val_if_fail (iter != NULL, FALSE);
768 #ifndef G_DISABLE_ASSERT
769 g_return_val_if_fail (ri->version == ri->hash_table->version, FALSE);
771 g_return_val_if_fail (ri->position < ri->hash_table->size, FALSE);
773 position = ri->position;
778 if (position >= ri->hash_table->size)
780 ri->position = position;
784 while (!HASH_IS_REAL (ri->hash_table->hashes[position]));
787 *key = ri->hash_table->keys[position];
789 *value = ri->hash_table->values[position];
791 ri->position = position;
796 * g_hash_table_iter_get_hash_table:
797 * @iter: an initialized #GHashTableIter
799 * Returns the #GHashTable associated with @iter.
801 * Return value: the #GHashTable associated with @iter.
806 g_hash_table_iter_get_hash_table (GHashTableIter *iter)
808 g_return_val_if_fail (iter != NULL, NULL);
810 return ((RealIter *) iter)->hash_table;
814 iter_remove_or_steal (RealIter *ri, gboolean notify)
816 g_return_if_fail (ri != NULL);
817 #ifndef G_DISABLE_ASSERT
818 g_return_if_fail (ri->version == ri->hash_table->version);
820 g_return_if_fail (ri->position >= 0);
821 g_return_if_fail (ri->position < ri->hash_table->size);
823 g_hash_table_remove_node (ri->hash_table, ri->position, notify);
825 #ifndef G_DISABLE_ASSERT
827 ri->hash_table->version++;
832 * g_hash_table_iter_remove:
833 * @iter: an initialized #GHashTableIter
835 * Removes the key/value pair currently pointed to by the iterator
836 * from its associated #GHashTable. Can only be called after
837 * g_hash_table_iter_next() returned %TRUE, and cannot be called
838 * more than once for the same key/value pair.
840 * If the #GHashTable was created using g_hash_table_new_full(),
841 * the key and value are freed using the supplied destroy functions,
842 * otherwise you have to make sure that any dynamically allocated
843 * values are freed yourself.
848 g_hash_table_iter_remove (GHashTableIter *iter)
850 iter_remove_or_steal ((RealIter *) iter, TRUE);
854 * g_hash_table_insert_node:
855 * @hash_table: our #GHashTable
856 * @node_index: pointer to node to insert/replace
857 * @key_hash: key hash
858 * @key: key to replace with, or %NULL
859 * @value: value to replace with
860 * @keep_new_key: whether to replace the key in the node with @key
861 * @reusing_key: whether @key was taken out of the existing node
863 * Inserts a value at @node_index in the hash table and updates it.
865 * If @key has been taken out of the existing node (ie it is not
866 * passed in via a g_hash_table_insert/replace) call, then @reusing_key
870 g_hash_table_insert_node (GHashTable *hash_table,
875 gboolean keep_new_key,
876 gboolean reusing_key)
882 if (G_UNLIKELY (hash_table->keys == hash_table->values && key != value))
883 hash_table->values = g_memdup (hash_table->keys, sizeof (gpointer) * hash_table->size);
885 old_hash = hash_table->hashes[node_index];
886 old_key = hash_table->keys[node_index];
887 old_value = hash_table->values[node_index];
889 if (HASH_IS_REAL (old_hash))
892 hash_table->keys[node_index] = key;
893 hash_table->values[node_index] = value;
897 hash_table->keys[node_index] = key;
898 hash_table->values[node_index] = value;
899 hash_table->hashes[node_index] = key_hash;
901 hash_table->nnodes++;
903 if (HASH_IS_UNUSED (old_hash))
905 /* We replaced an empty node, and not a tombstone */
906 hash_table->noccupied++;
907 g_hash_table_maybe_resize (hash_table);
910 #ifndef G_DISABLE_ASSERT
911 hash_table->version++;
915 if (HASH_IS_REAL (old_hash))
917 if (hash_table->key_destroy_func && !reusing_key)
918 hash_table->key_destroy_func (keep_new_key ? old_key : key);
919 if (hash_table->value_destroy_func)
920 hash_table->value_destroy_func (old_value);
925 * g_hash_table_iter_replace:
926 * @iter: an initialized #GHashTableIter
927 * @value: the value to replace with
929 * Replaces the value currently pointed to by the iterator
930 * from its associated #GHashTable. Can only be called after
931 * g_hash_table_iter_next() returned %TRUE.
933 * If you supplied a @value_destroy_func when creating the
934 * #GHashTable, the old value is freed using that function.
939 g_hash_table_iter_replace (GHashTableIter *iter,
946 ri = (RealIter *) iter;
948 g_return_if_fail (ri != NULL);
949 #ifndef G_DISABLE_ASSERT
950 g_return_if_fail (ri->version == ri->hash_table->version);
952 g_return_if_fail (ri->position >= 0);
953 g_return_if_fail (ri->position < ri->hash_table->size);
955 node_hash = ri->hash_table->hashes[ri->position];
956 key = ri->hash_table->keys[ri->position];
958 g_hash_table_insert_node (ri->hash_table, ri->position, node_hash, key, value, TRUE, TRUE);
960 #ifndef G_DISABLE_ASSERT
962 ri->hash_table->version++;
967 * g_hash_table_iter_steal:
968 * @iter: an initialized #GHashTableIter
970 * Removes the key/value pair currently pointed to by the
971 * iterator from its associated #GHashTable, without calling
972 * the key and value destroy functions. Can only be called
973 * after g_hash_table_iter_next() returned %TRUE, and cannot
974 * be called more than once for the same key/value pair.
979 g_hash_table_iter_steal (GHashTableIter *iter)
981 iter_remove_or_steal ((RealIter *) iter, FALSE);
987 * @hash_table: a valid #GHashTable
989 * Atomically increments the reference count of @hash_table by one.
990 * This function is MT-safe and may be called from any thread.
992 * Return value: the passed in #GHashTable
997 g_hash_table_ref (GHashTable *hash_table)
999 g_return_val_if_fail (hash_table != NULL, NULL);
1001 g_atomic_int_inc (&hash_table->ref_count);
1007 * g_hash_table_unref:
1008 * @hash_table: a valid #GHashTable
1010 * Atomically decrements the reference count of @hash_table by one.
1011 * If the reference count drops to 0, all keys and values will be
1012 * destroyed, and all memory allocated by the hash table is released.
1013 * This function is MT-safe and may be called from any thread.
1018 g_hash_table_unref (GHashTable *hash_table)
1020 g_return_if_fail (hash_table != NULL);
1022 if (g_atomic_int_dec_and_test (&hash_table->ref_count))
1024 g_hash_table_remove_all_nodes (hash_table, TRUE);
1025 if (hash_table->keys != hash_table->values)
1026 g_free (hash_table->values);
1027 g_free (hash_table->keys);
1028 g_free (hash_table->hashes);
1029 g_slice_free (GHashTable, hash_table);
1034 * g_hash_table_destroy:
1035 * @hash_table: a #GHashTable
1037 * Destroys all keys and values in the #GHashTable and decrements its
1038 * reference count by 1. If keys and/or values are dynamically allocated,
1039 * you should either free them first or create the #GHashTable with destroy
1040 * notifiers using g_hash_table_new_full(). In the latter case the destroy
1041 * functions you supplied will be called on all keys and values during the
1042 * destruction phase.
1045 g_hash_table_destroy (GHashTable *hash_table)
1047 g_return_if_fail (hash_table != NULL);
1049 g_hash_table_remove_all (hash_table);
1050 g_hash_table_unref (hash_table);
1054 * g_hash_table_lookup:
1055 * @hash_table: a #GHashTable
1056 * @key: the key to look up
1058 * Looks up a key in a #GHashTable. Note that this function cannot
1059 * distinguish between a key that is not present and one which is present
1060 * and has the value %NULL. If you need this distinction, use
1061 * g_hash_table_lookup_extended().
1063 * Return value: (allow-none): the associated value, or %NULL if the key is not found
1066 g_hash_table_lookup (GHashTable *hash_table,
1072 g_return_val_if_fail (hash_table != NULL, NULL);
1074 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1076 return HASH_IS_REAL (hash_table->hashes[node_index])
1077 ? hash_table->values[node_index]
1082 * g_hash_table_lookup_extended:
1083 * @hash_table: a #GHashTable
1084 * @lookup_key: the key to look up
1085 * @orig_key: (allow-none): return location for the original key, or %NULL
1086 * @value: (allow-none): return location for the value associated with the key, or %NULL
1088 * Looks up a key in the #GHashTable, returning the original key and the
1089 * associated value and a #gboolean which is %TRUE if the key was found. This
1090 * is useful if you need to free the memory allocated for the original key,
1091 * for example before calling g_hash_table_remove().
1093 * You can actually pass %NULL for @lookup_key to test
1094 * whether the %NULL key exists, provided the hash and equal functions
1095 * of @hash_table are %NULL-safe.
1097 * Return value: %TRUE if the key was found in the #GHashTable
1100 g_hash_table_lookup_extended (GHashTable *hash_table,
1101 gconstpointer lookup_key,
1108 g_return_val_if_fail (hash_table != NULL, FALSE);
1110 node_index = g_hash_table_lookup_node (hash_table, lookup_key, &node_hash);
1112 if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1116 *orig_key = hash_table->keys[node_index];
1119 *value = hash_table->values[node_index];
1125 * g_hash_table_insert_internal:
1126 * @hash_table: our #GHashTable
1127 * @key: the key to insert
1128 * @value: the value to insert
1129 * @keep_new_key: if %TRUE and this key already exists in the table
1130 * then call the destroy notify function on the old key. If %FALSE
1131 * then call the destroy notify function on the new key.
1133 * Implements the common logic for the g_hash_table_insert() and
1134 * g_hash_table_replace() functions.
1136 * Do a lookup of @key. If it is found, replace it with the new
1137 * @value (and perhaps the new @key). If it is not found, create
1141 g_hash_table_insert_internal (GHashTable *hash_table,
1144 gboolean keep_new_key)
1149 g_return_if_fail (hash_table != NULL);
1151 node_index = g_hash_table_lookup_node (hash_table, key, &key_hash);
1153 g_hash_table_insert_node (hash_table, node_index, key_hash, key, value, keep_new_key, FALSE);
1157 * g_hash_table_insert:
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.
1164 * If the key already exists in the #GHashTable its current
1165 * value is replaced with the new value. If you supplied a
1166 * @value_destroy_func when creating the #GHashTable, the old
1167 * value is freed using that function. If you supplied a
1168 * @key_destroy_func when creating the #GHashTable, the passed
1169 * key is freed using that function.
1172 g_hash_table_insert (GHashTable *hash_table,
1176 g_hash_table_insert_internal (hash_table, key, value, FALSE);
1180 * g_hash_table_replace:
1181 * @hash_table: a #GHashTable
1182 * @key: a key to insert
1183 * @value: the value to associate with the key
1185 * Inserts a new key and value into a #GHashTable similar to
1186 * g_hash_table_insert(). The difference is that if the key
1187 * already exists in the #GHashTable, it gets replaced by the
1188 * new key. If you supplied a @value_destroy_func when creating
1189 * the #GHashTable, the old value is freed using that function.
1190 * If you supplied a @key_destroy_func when creating the
1191 * #GHashTable, the old key is freed using that function.
1194 g_hash_table_replace (GHashTable *hash_table,
1198 g_hash_table_insert_internal (hash_table, key, value, TRUE);
1203 * @hash_table: a #GHashTable
1204 * @key: a key to insert
1206 * This is a convenience function for using a #GHashTable as a set. It
1207 * is equivalent to calling g_hash_table_replace() with @key as both the
1208 * key and the value.
1210 * When a hash table only ever contains keys that have themselves as the
1211 * corresponding value it is able to be stored more efficiently. See
1212 * the discussion in the section description.
1217 g_hash_table_add (GHashTable *hash_table,
1220 g_hash_table_insert_internal (hash_table, key, key, TRUE);
1224 * g_hash_table_contains:
1225 * @hash_table: a #GHashTable
1226 * @key: a key to check
1228 * Checks if @key is in @hash_table.
1233 g_hash_table_contains (GHashTable *hash_table,
1239 g_return_val_if_fail (hash_table != NULL, FALSE);
1241 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1243 return HASH_IS_REAL (hash_table->hashes[node_index]);
1247 * g_hash_table_remove_internal:
1248 * @hash_table: our #GHashTable
1249 * @key: the key to remove
1250 * @notify: %TRUE if the destroy notify handlers are to be called
1251 * Return value: %TRUE if a node was found and removed, else %FALSE
1253 * Implements the common logic for the g_hash_table_remove() and
1254 * g_hash_table_steal() functions.
1256 * Do a lookup of @key and remove it if it is found, calling the
1257 * destroy notify handlers only if @notify is %TRUE.
1260 g_hash_table_remove_internal (GHashTable *hash_table,
1267 g_return_val_if_fail (hash_table != NULL, FALSE);
1269 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1271 if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1274 g_hash_table_remove_node (hash_table, node_index, notify);
1275 g_hash_table_maybe_resize (hash_table);
1277 #ifndef G_DISABLE_ASSERT
1278 hash_table->version++;
1285 * g_hash_table_remove:
1286 * @hash_table: a #GHashTable
1287 * @key: the key to remove
1289 * Removes a key and its associated value from a #GHashTable.
1291 * If the #GHashTable was created using g_hash_table_new_full(), the
1292 * key and value are freed using the supplied destroy functions, otherwise
1293 * you have to make sure that any dynamically allocated values are freed
1296 * Returns: %TRUE if the key was found and removed from the #GHashTable
1299 g_hash_table_remove (GHashTable *hash_table,
1302 return g_hash_table_remove_internal (hash_table, key, TRUE);
1306 * g_hash_table_steal:
1307 * @hash_table: a #GHashTable
1308 * @key: the key to remove
1310 * Removes a key and its associated value from a #GHashTable without
1311 * calling the key and value destroy functions.
1313 * Returns: %TRUE if the key was found and removed from the #GHashTable
1316 g_hash_table_steal (GHashTable *hash_table,
1319 return g_hash_table_remove_internal (hash_table, key, FALSE);
1323 * g_hash_table_remove_all:
1324 * @hash_table: a #GHashTable
1326 * Removes all keys and their associated values from a #GHashTable.
1328 * If the #GHashTable was created using g_hash_table_new_full(),
1329 * the keys and values are freed using the supplied destroy functions,
1330 * otherwise you have to make sure that any dynamically allocated
1331 * values are freed yourself.
1336 g_hash_table_remove_all (GHashTable *hash_table)
1338 g_return_if_fail (hash_table != NULL);
1340 #ifndef G_DISABLE_ASSERT
1341 if (hash_table->nnodes != 0)
1342 hash_table->version++;
1345 g_hash_table_remove_all_nodes (hash_table, TRUE);
1346 g_hash_table_maybe_resize (hash_table);
1350 * g_hash_table_steal_all:
1351 * @hash_table: a #GHashTable
1353 * Removes all keys and their associated values from a #GHashTable
1354 * without calling the key and value destroy functions.
1359 g_hash_table_steal_all (GHashTable *hash_table)
1361 g_return_if_fail (hash_table != NULL);
1363 #ifndef G_DISABLE_ASSERT
1364 if (hash_table->nnodes != 0)
1365 hash_table->version++;
1368 g_hash_table_remove_all_nodes (hash_table, FALSE);
1369 g_hash_table_maybe_resize (hash_table);
1373 * g_hash_table_foreach_remove_or_steal:
1374 * @hash_table: a #GHashTable
1375 * @func: the user's callback function
1376 * @user_data: data for @func
1377 * @notify: %TRUE if the destroy notify handlers are to be called
1379 * Implements the common logic for g_hash_table_foreach_remove()
1380 * and g_hash_table_foreach_steal().
1382 * Iterates over every node in the table, calling @func with the key
1383 * and value of the node (and @user_data). If @func returns %TRUE the
1384 * node is removed from the table.
1386 * If @notify is true then the destroy notify handlers will be called
1387 * for each removed node.
1390 g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
1397 #ifndef G_DISABLE_ASSERT
1398 gint version = hash_table->version;
1401 for (i = 0; i < hash_table->size; i++)
1403 guint node_hash = hash_table->hashes[i];
1404 gpointer node_key = hash_table->keys[i];
1405 gpointer node_value = hash_table->values[i];
1407 if (HASH_IS_REAL (node_hash) &&
1408 (* func) (node_key, node_value, user_data))
1410 g_hash_table_remove_node (hash_table, i, notify);
1414 #ifndef G_DISABLE_ASSERT
1415 g_return_val_if_fail (version == hash_table->version, 0);
1419 g_hash_table_maybe_resize (hash_table);
1421 #ifndef G_DISABLE_ASSERT
1423 hash_table->version++;
1430 * g_hash_table_foreach_remove:
1431 * @hash_table: a #GHashTable
1432 * @func: the function to call for each key/value pair
1433 * @user_data: user data to pass to the function
1435 * Calls the given function for each key/value pair in the
1436 * #GHashTable. If the function returns %TRUE, then the key/value
1437 * pair is removed from the #GHashTable. If you supplied key or
1438 * value destroy functions when creating the #GHashTable, they are
1439 * used to free the memory allocated for the removed keys and values.
1441 * See #GHashTableIter for an alternative way to loop over the
1442 * key/value pairs in the hash table.
1444 * Return value: the number of key/value pairs removed
1447 g_hash_table_foreach_remove (GHashTable *hash_table,
1451 g_return_val_if_fail (hash_table != NULL, 0);
1452 g_return_val_if_fail (func != NULL, 0);
1454 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
1458 * g_hash_table_foreach_steal:
1459 * @hash_table: a #GHashTable
1460 * @func: the function to call for each key/value pair
1461 * @user_data: user data to pass to the function
1463 * Calls the given function for each key/value pair in the
1464 * #GHashTable. If the function returns %TRUE, then the key/value
1465 * pair is removed from the #GHashTable, but no key or value
1466 * destroy functions are called.
1468 * See #GHashTableIter for an alternative way to loop over the
1469 * key/value pairs in the hash table.
1471 * Return value: the number of key/value pairs removed.
1474 g_hash_table_foreach_steal (GHashTable *hash_table,
1478 g_return_val_if_fail (hash_table != NULL, 0);
1479 g_return_val_if_fail (func != NULL, 0);
1481 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
1485 * g_hash_table_foreach:
1486 * @hash_table: a #GHashTable
1487 * @func: the function to call for each key/value pair
1488 * @user_data: user data to pass to the function
1490 * Calls the given function for each of the key/value pairs in the
1491 * #GHashTable. The function is passed the key and value of each
1492 * pair, and the given @user_data parameter. The hash table may not
1493 * be modified while iterating over it (you can't add/remove
1494 * items). To remove all items matching a predicate, use
1495 * g_hash_table_foreach_remove().
1497 * See g_hash_table_find() for performance caveats for linear
1498 * order searches in contrast to g_hash_table_lookup().
1501 g_hash_table_foreach (GHashTable *hash_table,
1506 #ifndef G_DISABLE_ASSERT
1510 g_return_if_fail (hash_table != NULL);
1511 g_return_if_fail (func != NULL);
1513 #ifndef G_DISABLE_ASSERT
1514 version = hash_table->version;
1517 for (i = 0; i < hash_table->size; i++)
1519 guint node_hash = hash_table->hashes[i];
1520 gpointer node_key = hash_table->keys[i];
1521 gpointer node_value = hash_table->values[i];
1523 if (HASH_IS_REAL (node_hash))
1524 (* func) (node_key, node_value, user_data);
1526 #ifndef G_DISABLE_ASSERT
1527 g_return_if_fail (version == hash_table->version);
1533 * g_hash_table_find:
1534 * @hash_table: a #GHashTable
1535 * @predicate: function to test the key/value pairs for a certain property
1536 * @user_data: user data to pass to the function
1538 * Calls the given function for key/value pairs in the #GHashTable
1539 * until @predicate returns %TRUE. The function is passed the key
1540 * and value of each pair, and the given @user_data parameter. The
1541 * hash table may not be modified while iterating over it (you can't
1542 * add/remove items).
1544 * Note, that hash tables are really only optimized for forward
1545 * lookups, i.e. g_hash_table_lookup(). So code that frequently issues
1546 * g_hash_table_find() or g_hash_table_foreach() (e.g. in the order of
1547 * once per every entry in a hash table) should probably be reworked
1548 * to use additional or different data structures for reverse lookups
1549 * (keep in mind that an O(n) find/foreach operation issued for all n
1550 * values in a hash table ends up needing O(n*n) operations).
1552 * Return value: (allow-none): The value of the first key/value pair is returned,
1553 * for which @predicate evaluates to %TRUE. If no pair with the
1554 * requested property is found, %NULL is returned.
1559 g_hash_table_find (GHashTable *hash_table,
1564 #ifndef G_DISABLE_ASSERT
1569 g_return_val_if_fail (hash_table != NULL, NULL);
1570 g_return_val_if_fail (predicate != NULL, NULL);
1572 #ifndef G_DISABLE_ASSERT
1573 version = hash_table->version;
1578 for (i = 0; i < hash_table->size; i++)
1580 guint node_hash = hash_table->hashes[i];
1581 gpointer node_key = hash_table->keys[i];
1582 gpointer node_value = hash_table->values[i];
1584 if (HASH_IS_REAL (node_hash))
1585 match = predicate (node_key, node_value, user_data);
1587 #ifndef G_DISABLE_ASSERT
1588 g_return_val_if_fail (version == hash_table->version, NULL);
1599 * g_hash_table_size:
1600 * @hash_table: a #GHashTable
1602 * Returns the number of elements contained in the #GHashTable.
1604 * Return value: the number of key/value pairs in the #GHashTable.
1607 g_hash_table_size (GHashTable *hash_table)
1609 g_return_val_if_fail (hash_table != NULL, 0);
1611 return hash_table->nnodes;
1615 * g_hash_table_get_keys:
1616 * @hash_table: a #GHashTable
1618 * Retrieves every key inside @hash_table. The returned data
1619 * is valid until @hash_table is modified.
1621 * Return value: a #GList containing all the keys inside the hash
1622 * table. The content of the list is owned by the hash table and
1623 * should not be modified or freed. Use g_list_free() when done
1629 g_hash_table_get_keys (GHashTable *hash_table)
1634 g_return_val_if_fail (hash_table != NULL, NULL);
1637 for (i = 0; i < hash_table->size; i++)
1639 if (HASH_IS_REAL (hash_table->hashes[i]))
1640 retval = g_list_prepend (retval, hash_table->keys[i]);
1647 * g_hash_table_get_values:
1648 * @hash_table: a #GHashTable
1650 * Retrieves every value inside @hash_table. The returned data
1651 * is valid until @hash_table is modified.
1653 * Return value: a #GList containing all the values inside the hash
1654 * table. The content of the list is owned by the hash table and
1655 * should not be modified or freed. Use g_list_free() when done
1661 g_hash_table_get_values (GHashTable *hash_table)
1666 g_return_val_if_fail (hash_table != NULL, NULL);
1669 for (i = 0; i < hash_table->size; i++)
1671 if (HASH_IS_REAL (hash_table->hashes[i]))
1672 retval = g_list_prepend (retval, hash_table->values[i]);
1684 * @v2: a key to compare with @v1
1686 * Compares two strings for byte-by-byte equality and returns %TRUE
1687 * if they are equal. It can be passed to g_hash_table_new() as the
1688 * @key_equal_func parameter, when using non-%NULL strings as keys in a
1691 * Note that this function is primarily meant as a hash table comparison
1692 * function. For a general-purpose, %NULL-safe string comparison function,
1695 * Returns: %TRUE if the two keys match
1698 g_str_equal (gconstpointer v1,
1701 const gchar *string1 = v1;
1702 const gchar *string2 = v2;
1704 return strcmp (string1, string2) == 0;
1711 * Converts a string to a hash value.
1713 * This function implements the widely used "djb" hash apparently posted
1714 * by Daniel Bernstein to comp.lang.c some time ago. The 32 bit
1715 * unsigned hash value starts at 5381 and for each byte 'c' in the
1716 * string, is updated: <literal>hash = hash * 33 + c</literal>. This
1717 * function uses the signed value of each byte.
1719 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1720 * when using non-%NULL strings as keys in a #GHashTable.
1722 * Returns: a hash value corresponding to the key
1725 g_str_hash (gconstpointer v)
1727 const signed char *p;
1730 for (p = v; *p != '\0'; p++)
1731 h = (h << 5) + h + *p;
1738 * @v: (allow-none): a #gpointer key
1740 * Converts a gpointer to a hash value.
1741 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1742 * when using opaque pointers compared by pointer value as keys in a
1745 * This hash function is also appropriate for keys that are integers stored
1746 * in pointers, such as <literal>GINT_TO_POINTER (n)</literal>.
1748 * Returns: a hash value corresponding to the key.
1751 g_direct_hash (gconstpointer v)
1753 return GPOINTER_TO_UINT (v);
1758 * @v1: (allow-none): a key
1759 * @v2: (allow-none): a key to compare with @v1
1761 * Compares two #gpointer arguments and returns %TRUE if they are equal.
1762 * It can be passed to g_hash_table_new() as the @key_equal_func
1763 * parameter, when using opaque pointers compared by pointer value as keys
1766 * This equality function is also appropriate for keys that are integers stored
1767 * in pointers, such as <literal>GINT_TO_POINTER (n)</literal>.
1769 * Returns: %TRUE if the two keys match.
1772 g_direct_equal (gconstpointer v1,
1780 * @v1: a pointer to a #gint key
1781 * @v2: a pointer to a #gint key to compare with @v1
1783 * Compares the two #gint values being pointed to and returns
1784 * %TRUE if they are equal.
1785 * It can be passed to g_hash_table_new() as the @key_equal_func
1786 * parameter, when using non-%NULL pointers to integers as keys in a
1789 * Note that this function acts on pointers to #gint, not on #gint directly:
1790 * if your hash table's keys are of the form
1791 * <literal>GINT_TO_POINTER (n)</literal>, use g_direct_equal() instead.
1793 * Returns: %TRUE if the two keys match.
1796 g_int_equal (gconstpointer v1,
1799 return *((const gint*) v1) == *((const gint*) v2);
1804 * @v: a pointer to a #gint key
1806 * Converts a pointer to a #gint to a hash value.
1807 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1808 * when using non-%NULL pointers to integer values as keys in a #GHashTable.
1810 * Note that this function acts on pointers to #gint, not on #gint directly:
1811 * if your hash table's keys are of the form
1812 * <literal>GINT_TO_POINTER (n)</literal>, use g_direct_hash() instead.
1814 * Returns: a hash value corresponding to the key.
1817 g_int_hash (gconstpointer v)
1819 return *(const gint*) v;
1824 * @v1: a pointer to a #gint64 key
1825 * @v2: a pointer to a #gint64 key to compare with @v1
1827 * Compares the two #gint64 values being pointed to and returns
1828 * %TRUE if they are equal.
1829 * It can be passed to g_hash_table_new() as the @key_equal_func
1830 * parameter, when using non-%NULL pointers to 64-bit integers as keys in a
1833 * Returns: %TRUE if the two keys match.
1838 g_int64_equal (gconstpointer v1,
1841 return *((const gint64*) v1) == *((const gint64*) v2);
1846 * @v: a pointer to a #gint64 key
1848 * Converts a pointer to a #gint64 to a hash value.
1850 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1851 * when using non-%NULL pointers to 64-bit integer values as keys in a
1854 * Returns: a hash value corresponding to the key.
1859 g_int64_hash (gconstpointer v)
1861 return (guint) *(const gint64*) v;
1866 * @v1: a pointer to a #gdouble key
1867 * @v2: a pointer to a #gdouble key to compare with @v1
1869 * Compares the two #gdouble values being pointed to and returns
1870 * %TRUE if they are equal.
1871 * It can be passed to g_hash_table_new() as the @key_equal_func
1872 * parameter, when using non-%NULL pointers to doubles as keys in a
1875 * Returns: %TRUE if the two keys match.
1880 g_double_equal (gconstpointer v1,
1883 return *((const gdouble*) v1) == *((const gdouble*) v2);
1888 * @v: a pointer to a #gdouble key
1890 * Converts a pointer to a #gdouble to a hash value.
1891 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1892 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1893 * when using non-%NULL pointers to doubles as keys in a #GHashTable.
1895 * Returns: a hash value corresponding to the key.
1900 g_double_hash (gconstpointer v)
1902 return (guint) *(const gdouble*) v;