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.
96 * The functions g_hash_table_add(), g_hash_table_contains() are both
97 * designed to be used when using #GHashTable as a set.
103 * The #GHashTable struct is an opaque data structure to represent a
104 * <link linkend="glib-Hash-Tables">Hash Table</link>. It should only be
105 * accessed via the following functions.
112 * Specifies the type of the hash function which is passed to
113 * g_hash_table_new() when a #GHashTable is created.
115 * The function is passed a key and should return a #guint hash value.
116 * The functions g_direct_hash(), g_int_hash() and g_str_hash() provide
117 * hash functions which can be used when the key is a #gpointer, #gint*,
118 * and #gchar* respectively.
120 * g_direct_hash() is also the appropriate hash function for keys
121 * of the form <literal>GINT_TO_POINTER (n)</literal> (or similar macros).
123 * <!-- FIXME: Need more here. --> A good hash functions should produce
124 * hash values that are evenly distributed over a fairly large range.
125 * The modulus is taken with the hash table size (a prime number) to
126 * find the 'bucket' to place each key into. The function should also
127 * be very fast, since it is called for each key lookup.
129 * Note that the hash functions provided by GLib have these qualities,
130 * but are not particularly robust against manufactured keys that
131 * cause hash collisions. Therefore, you should consider choosing
132 * a more secure hash function when using a GHashTable with keys
133 * that originate in untrusted data (such as HTTP requests).
134 * Using g_str_hash() in that situation might make your application
135 * vulerable to <ulink url="https://lwn.net/Articles/474912/">Algorithmic Complexity Attacks</ulink>.
137 * The key to choosing a good hash is unpredictability. Even
138 * cryptographic hashes are very easy to find collisions for when the
139 * remainder is taken modulo a somewhat predictable prime number. There
140 * must be an element of randomness that an attacker is unable to guess.
142 * Returns: the hash value corresponding to the key
148 * @value: the value corresponding to the key
149 * @user_data: user data passed to g_hash_table_foreach()
151 * Specifies the type of the function passed to g_hash_table_foreach().
152 * It is called with each key/value pair, together with the @user_data
153 * parameter which is passed to g_hash_table_foreach().
159 * @value: the value associated with the key
160 * @user_data: user data passed to g_hash_table_remove()
162 * Specifies the type of the function passed to
163 * g_hash_table_foreach_remove(). It is called with each key/value
164 * pair, together with the @user_data parameter passed to
165 * g_hash_table_foreach_remove(). It should return %TRUE if the
166 * key/value pair should be removed from the #GHashTable.
168 * Returns: %TRUE if the key/value pair should be removed from the
175 * @b: a value to compare with
177 * Specifies the type of a function used to test two values for
178 * equality. The function should return %TRUE if both values are equal
179 * and %FALSE otherwise.
181 * Returns: %TRUE if @a = @b; %FALSE otherwise
187 * A GHashTableIter structure represents an iterator that can be used
188 * to iterate over the elements of a #GHashTable. GHashTableIter
189 * structures are typically allocated on the stack and then initialized
190 * with g_hash_table_iter_init().
194 * g_hash_table_freeze:
195 * @hash_table: a #GHashTable
197 * This function is deprecated and will be removed in the next major
198 * release of GLib. It does nothing.
203 * @hash_table: a #GHashTable
205 * This function is deprecated and will be removed in the next major
206 * release of GLib. It does nothing.
209 #define HASH_TABLE_MIN_SHIFT 3 /* 1 << 3 == 8 buckets */
211 #define UNUSED_HASH_VALUE 0
212 #define TOMBSTONE_HASH_VALUE 1
213 #define HASH_IS_UNUSED(h_) ((h_) == UNUSED_HASH_VALUE)
214 #define HASH_IS_TOMBSTONE(h_) ((h_) == TOMBSTONE_HASH_VALUE)
215 #define HASH_IS_REAL(h_) ((h_) >= 2)
223 gint noccupied; /* nnodes + tombstones */
230 GEqualFunc key_equal_func;
232 #ifndef G_DISABLE_ASSERT
234 * Tracks the structure of the hash table, not its contents: is only
235 * incremented when a node is added or removed (is not incremented
236 * when the key or data of a node is modified).
240 GDestroyNotify key_destroy_func;
241 GDestroyNotify value_destroy_func;
246 GHashTable *hash_table;
254 /* Each table size has an associated prime modulo (the first prime
255 * lower than the table size) used to find the initial bucket. Probing
256 * then works modulo 2^n. The prime modulo is necessary to get a
257 * good distribution with poor hash functions.
259 static const gint prime_mod [] =
277 65521, /* For 1 << 16 */
292 2147483647 /* For 1 << 31 */
296 g_hash_table_set_shift (GHashTable *hash_table, gint shift)
301 hash_table->size = 1 << shift;
302 hash_table->mod = prime_mod [shift];
304 for (i = 0; i < shift; i++)
310 hash_table->mask = mask;
314 g_hash_table_find_closest_shift (gint n)
325 g_hash_table_set_shift_from_size (GHashTable *hash_table, gint size)
329 shift = g_hash_table_find_closest_shift (size);
330 shift = MAX (shift, HASH_TABLE_MIN_SHIFT);
332 g_hash_table_set_shift (hash_table, shift);
336 * g_hash_table_lookup_node:
337 * @hash_table: our #GHashTable
338 * @key: the key to lookup against
339 * @hash_return: key hash return location
341 * Performs a lookup in the hash table, preserving extra information
342 * usually needed for insertion.
344 * This function first computes the hash value of the key using the
345 * user's hash function.
347 * If an entry in the table matching @key is found then this function
348 * returns the index of that entry in the table, and if not, the
349 * index of an unused node (empty or tombstone) where the key can be
352 * The computed hash value is returned in the variable pointed to
353 * by @hash_return. This is to save insertions from having to compute
354 * the hash record again for the new record.
356 * Returns: index of the described node
359 g_hash_table_lookup_node (GHashTable *hash_table,
366 guint first_tombstone = 0;
367 gboolean have_tombstone = FALSE;
370 hash_value = hash_table->hash_func (key);
371 if (G_UNLIKELY (!HASH_IS_REAL (hash_value)))
374 *hash_return = hash_value;
376 node_index = hash_value % hash_table->mod;
377 node_hash = hash_table->hashes[node_index];
379 while (!HASH_IS_UNUSED (node_hash))
381 /* We first check if our full hash values
382 * are equal so we can avoid calling the full-blown
383 * key equality function in most cases.
385 if (node_hash == hash_value)
387 gpointer node_key = hash_table->keys[node_index];
389 if (hash_table->key_equal_func)
391 if (hash_table->key_equal_func (node_key, key))
394 else if (node_key == key)
399 else if (HASH_IS_TOMBSTONE (node_hash) && !have_tombstone)
401 first_tombstone = node_index;
402 have_tombstone = TRUE;
407 node_index &= hash_table->mask;
408 node_hash = hash_table->hashes[node_index];
412 return first_tombstone;
418 * g_hash_table_remove_node:
419 * @hash_table: our #GHashTable
420 * @node: pointer to node to remove
421 * @notify: %TRUE if the destroy notify handlers are to be called
423 * Removes a node from the hash table and updates the node count.
424 * The node is replaced by a tombstone. No table resize is performed.
426 * If @notify is %TRUE then the destroy notify functions are called
427 * for the key and value of the hash node.
430 g_hash_table_remove_node (GHashTable *hash_table,
437 key = hash_table->keys[i];
438 value = hash_table->values[i];
440 /* Erect tombstone */
441 hash_table->hashes[i] = TOMBSTONE_HASH_VALUE;
444 hash_table->keys[i] = NULL;
445 hash_table->values[i] = NULL;
447 hash_table->nnodes--;
449 if (notify && hash_table->key_destroy_func)
450 hash_table->key_destroy_func (key);
452 if (notify && hash_table->value_destroy_func)
453 hash_table->value_destroy_func (value);
458 * g_hash_table_remove_all_nodes:
459 * @hash_table: our #GHashTable
460 * @notify: %TRUE if the destroy notify handlers are to be called
462 * Removes all nodes from the table. Since this may be a precursor to
463 * freeing the table entirely, no resize is performed.
465 * If @notify is %TRUE then the destroy notify functions are called
466 * for the key and value of the hash node.
469 g_hash_table_remove_all_nodes (GHashTable *hash_table,
476 hash_table->nnodes = 0;
477 hash_table->noccupied = 0;
480 (hash_table->key_destroy_func == NULL &&
481 hash_table->value_destroy_func == NULL))
483 memset (hash_table->hashes, 0, hash_table->size * sizeof (guint));
484 memset (hash_table->keys, 0, hash_table->size * sizeof (gpointer));
485 memset (hash_table->values, 0, hash_table->size * sizeof (gpointer));
490 for (i = 0; i < hash_table->size; i++)
492 if (HASH_IS_REAL (hash_table->hashes[i]))
494 key = hash_table->keys[i];
495 value = hash_table->values[i];
497 hash_table->hashes[i] = UNUSED_HASH_VALUE;
498 hash_table->keys[i] = NULL;
499 hash_table->values[i] = NULL;
501 if (hash_table->key_destroy_func != NULL)
502 hash_table->key_destroy_func (key);
504 if (hash_table->value_destroy_func != NULL)
505 hash_table->value_destroy_func (value);
507 else if (HASH_IS_TOMBSTONE (hash_table->hashes[i]))
509 hash_table->hashes[i] = UNUSED_HASH_VALUE;
515 * g_hash_table_resize:
516 * @hash_table: our #GHashTable
518 * Resizes the hash table to the optimal size based on the number of
519 * nodes currently held. If you call this function then a resize will
520 * occur, even if one does not need to occur.
521 * Use g_hash_table_maybe_resize() instead.
523 * This function may "resize" the hash table to its current size, with
524 * the side effect of cleaning up tombstones and otherwise optimizing
525 * the probe sequences.
528 g_hash_table_resize (GHashTable *hash_table)
531 gpointer *new_values;
536 old_size = hash_table->size;
537 g_hash_table_set_shift_from_size (hash_table, hash_table->nnodes * 2);
539 new_keys = g_new0 (gpointer, hash_table->size);
540 if (hash_table->keys == hash_table->values)
541 new_values = new_keys;
543 new_values = g_new0 (gpointer, hash_table->size);
544 new_hashes = g_new0 (guint, hash_table->size);
546 for (i = 0; i < old_size; i++)
548 guint node_hash = hash_table->hashes[i];
552 if (!HASH_IS_REAL (node_hash))
555 hash_val = node_hash % hash_table->mod;
557 while (!HASH_IS_UNUSED (new_hashes[hash_val]))
561 hash_val &= hash_table->mask;
564 new_hashes[hash_val] = hash_table->hashes[i];
565 new_keys[hash_val] = hash_table->keys[i];
566 new_values[hash_val] = hash_table->values[i];
569 if (hash_table->keys != hash_table->values)
570 g_free (hash_table->values);
572 g_free (hash_table->keys);
573 g_free (hash_table->hashes);
575 hash_table->keys = new_keys;
576 hash_table->values = new_values;
577 hash_table->hashes = new_hashes;
579 hash_table->noccupied = hash_table->nnodes;
583 * g_hash_table_maybe_resize:
584 * @hash_table: our #GHashTable
586 * Resizes the hash table, if needed.
588 * Essentially, calls g_hash_table_resize() if the table has strayed
589 * too far from its ideal size for its number of nodes.
592 g_hash_table_maybe_resize (GHashTable *hash_table)
594 gint noccupied = hash_table->noccupied;
595 gint size = hash_table->size;
597 if ((size > hash_table->nnodes * 4 && size > 1 << HASH_TABLE_MIN_SHIFT) ||
598 (size <= noccupied + (noccupied / 16)))
599 g_hash_table_resize (hash_table);
604 * @hash_func: a function to create a hash value from a key
605 * @key_equal_func: a function to check two keys for equality
607 * Creates a new #GHashTable with a reference count of 1.
609 * Hash values returned by @hash_func are used to determine where keys
610 * are stored within the #GHashTable data structure. The g_direct_hash(),
611 * g_int_hash(), g_int64_hash(), g_double_hash() and g_str_hash()
612 * functions are provided for some common types of keys.
613 * If @hash_func is %NULL, g_direct_hash() is used.
615 * @key_equal_func is used when looking up keys in the #GHashTable.
616 * The g_direct_equal(), g_int_equal(), g_int64_equal(), g_double_equal()
617 * and g_str_equal() functions are provided for the most common types
618 * of keys. If @key_equal_func is %NULL, keys are compared directly in
619 * a similar fashion to g_direct_equal(), but without the overhead of
622 * Return value: a new #GHashTable
625 g_hash_table_new (GHashFunc hash_func,
626 GEqualFunc key_equal_func)
628 return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
633 * g_hash_table_new_full:
634 * @hash_func: a function to create a hash value from a key
635 * @key_equal_func: a function to check two keys for equality
636 * @key_destroy_func: (allow-none): a function to free the memory allocated for the key
637 * used when removing the entry from the #GHashTable, or %NULL
638 * if you don't want to supply such a function.
639 * @value_destroy_func: (allow-none): a function to free the memory allocated for the
640 * value used when removing the entry from the #GHashTable, or %NULL
641 * if you don't want to supply such a function.
643 * Creates a new #GHashTable like g_hash_table_new() with a reference
644 * count of 1 and allows to specify functions to free the memory
645 * allocated for the key and value that get called when removing the
646 * entry from the #GHashTable.
648 * Return value: a new #GHashTable
651 g_hash_table_new_full (GHashFunc hash_func,
652 GEqualFunc key_equal_func,
653 GDestroyNotify key_destroy_func,
654 GDestroyNotify value_destroy_func)
656 GHashTable *hash_table;
658 hash_table = g_slice_new (GHashTable);
659 g_hash_table_set_shift (hash_table, HASH_TABLE_MIN_SHIFT);
660 hash_table->nnodes = 0;
661 hash_table->noccupied = 0;
662 hash_table->hash_func = hash_func ? hash_func : g_direct_hash;
663 hash_table->key_equal_func = key_equal_func;
664 hash_table->ref_count = 1;
665 #ifndef G_DISABLE_ASSERT
666 hash_table->version = 0;
668 hash_table->key_destroy_func = key_destroy_func;
669 hash_table->value_destroy_func = value_destroy_func;
670 hash_table->keys = g_new0 (gpointer, hash_table->size);
671 hash_table->values = hash_table->keys;
672 hash_table->hashes = g_new0 (guint, hash_table->size);
678 * g_hash_table_iter_init:
679 * @iter: an uninitialized #GHashTableIter
680 * @hash_table: a #GHashTable
682 * Initializes a key/value pair iterator and associates it with
683 * @hash_table. Modifying the hash table after calling this function
684 * invalidates the returned iterator.
686 * GHashTableIter iter;
687 * gpointer key, value;
689 * g_hash_table_iter_init (&iter, hash_table);
690 * while (g_hash_table_iter_next (&iter, &key, &value))
692 * /* do something with key and value */
699 g_hash_table_iter_init (GHashTableIter *iter,
700 GHashTable *hash_table)
702 RealIter *ri = (RealIter *) iter;
704 g_return_if_fail (iter != NULL);
705 g_return_if_fail (hash_table != NULL);
707 ri->hash_table = hash_table;
709 #ifndef G_DISABLE_ASSERT
710 ri->version = hash_table->version;
715 * g_hash_table_iter_next:
716 * @iter: an initialized #GHashTableIter
717 * @key: (allow-none): a location to store the key, or %NULL
718 * @value: (allow-none): a location to store the value, or %NULL
720 * Advances @iter and retrieves the key and/or value that are now
721 * pointed to as a result of this advancement. If %FALSE is returned,
722 * @key and @value are not set, and the iterator becomes invalid.
724 * Return value: %FALSE if the end of the #GHashTable has been reached.
729 g_hash_table_iter_next (GHashTableIter *iter,
733 RealIter *ri = (RealIter *) iter;
736 g_return_val_if_fail (iter != NULL, FALSE);
737 #ifndef G_DISABLE_ASSERT
738 g_return_val_if_fail (ri->version == ri->hash_table->version, FALSE);
740 g_return_val_if_fail (ri->position < ri->hash_table->size, FALSE);
742 position = ri->position;
747 if (position >= ri->hash_table->size)
749 ri->position = position;
753 while (!HASH_IS_REAL (ri->hash_table->hashes[position]));
756 *key = ri->hash_table->keys[position];
758 *value = ri->hash_table->values[position];
760 ri->position = position;
765 * g_hash_table_iter_get_hash_table:
766 * @iter: an initialized #GHashTableIter
768 * Returns the #GHashTable associated with @iter.
770 * Return value: the #GHashTable associated with @iter.
775 g_hash_table_iter_get_hash_table (GHashTableIter *iter)
777 g_return_val_if_fail (iter != NULL, NULL);
779 return ((RealIter *) iter)->hash_table;
783 iter_remove_or_steal (RealIter *ri, gboolean notify)
785 g_return_if_fail (ri != NULL);
786 #ifndef G_DISABLE_ASSERT
787 g_return_if_fail (ri->version == ri->hash_table->version);
789 g_return_if_fail (ri->position >= 0);
790 g_return_if_fail (ri->position < ri->hash_table->size);
792 g_hash_table_remove_node (ri->hash_table, ri->position, notify);
794 #ifndef G_DISABLE_ASSERT
796 ri->hash_table->version++;
801 * g_hash_table_iter_remove:
802 * @iter: an initialized #GHashTableIter
804 * Removes the key/value pair currently pointed to by the iterator
805 * from its associated #GHashTable. Can only be called after
806 * g_hash_table_iter_next() returned %TRUE, and cannot be called
807 * more than once for the same key/value pair.
809 * If the #GHashTable was created using g_hash_table_new_full(),
810 * the key and value are freed using the supplied destroy functions,
811 * otherwise you have to make sure that any dynamically allocated
812 * values are freed yourself.
817 g_hash_table_iter_remove (GHashTableIter *iter)
819 iter_remove_or_steal ((RealIter *) iter, TRUE);
823 * g_hash_table_insert_node:
824 * @hash_table: our #GHashTable
825 * @node_index: pointer to node to insert/replace
826 * @key_hash: key hash
827 * @key: (allow-none): key to replace with, or %NULL
828 * @value: value to replace with
829 * @keep_new_key: whether to replace the key in the node with @key
830 * @reusing_key: whether @key was taken out of the existing node
832 * Inserts a value at @node_index in the hash table and updates it.
834 * If @key has been taken out of the existing node (ie it is not
835 * passed in via a g_hash_table_insert/replace) call, then @reusing_key
839 g_hash_table_insert_node (GHashTable *hash_table,
844 gboolean keep_new_key,
845 gboolean reusing_key)
851 if (G_UNLIKELY (hash_table->keys == hash_table->values && key != value))
852 hash_table->values = g_memdup (hash_table->keys, sizeof (gpointer) * hash_table->size);
854 old_hash = hash_table->hashes[node_index];
855 old_key = hash_table->keys[node_index];
856 old_value = hash_table->values[node_index];
858 if (HASH_IS_REAL (old_hash))
861 hash_table->keys[node_index] = key;
862 hash_table->values[node_index] = value;
866 hash_table->keys[node_index] = key;
867 hash_table->values[node_index] = value;
868 hash_table->hashes[node_index] = key_hash;
870 hash_table->nnodes++;
872 if (HASH_IS_UNUSED (old_hash))
874 /* We replaced an empty node, and not a tombstone */
875 hash_table->noccupied++;
876 g_hash_table_maybe_resize (hash_table);
879 #ifndef G_DISABLE_ASSERT
880 hash_table->version++;
884 if (HASH_IS_REAL (old_hash))
886 if (hash_table->key_destroy_func && !reusing_key)
887 hash_table->key_destroy_func (keep_new_key ? old_key : key);
888 if (hash_table->value_destroy_func)
889 hash_table->value_destroy_func (old_value);
894 * g_hash_table_iter_replace:
895 * @iter: an initialized #GHashTableIter
896 * @value: the value to replace with
898 * Replaces the value currently pointed to by the iterator
899 * from its associated #GHashTable. Can only be called after
900 * g_hash_table_iter_next() returned %TRUE.
902 * If you supplied a @value_destroy_func when creating the
903 * #GHashTable, the old value is freed using that function.
908 g_hash_table_iter_replace (GHashTableIter *iter,
915 ri = (RealIter *) iter;
917 g_return_if_fail (ri != NULL);
918 #ifndef G_DISABLE_ASSERT
919 g_return_if_fail (ri->version == ri->hash_table->version);
921 g_return_if_fail (ri->position >= 0);
922 g_return_if_fail (ri->position < ri->hash_table->size);
924 node_hash = ri->hash_table->hashes[ri->position];
925 key = ri->hash_table->keys[ri->position];
927 g_hash_table_insert_node (ri->hash_table, ri->position, node_hash, key, value, TRUE, TRUE);
929 #ifndef G_DISABLE_ASSERT
931 ri->hash_table->version++;
936 * g_hash_table_iter_steal:
937 * @iter: an initialized #GHashTableIter
939 * Removes the key/value pair currently pointed to by the
940 * iterator from its associated #GHashTable, without calling
941 * the key and value destroy functions. Can only be called
942 * after g_hash_table_iter_next() returned %TRUE, and cannot
943 * be called more than once for the same key/value pair.
948 g_hash_table_iter_steal (GHashTableIter *iter)
950 iter_remove_or_steal ((RealIter *) iter, FALSE);
956 * @hash_table: a valid #GHashTable
958 * Atomically increments the reference count of @hash_table by one.
959 * This function is MT-safe and may be called from any thread.
961 * Return value: the passed in #GHashTable
966 g_hash_table_ref (GHashTable *hash_table)
968 g_return_val_if_fail (hash_table != NULL, NULL);
970 g_atomic_int_inc (&hash_table->ref_count);
976 * g_hash_table_unref:
977 * @hash_table: a valid #GHashTable
979 * Atomically decrements the reference count of @hash_table by one.
980 * If the reference count drops to 0, all keys and values will be
981 * destroyed, and all memory allocated by the hash table is released.
982 * This function is MT-safe and may be called from any thread.
987 g_hash_table_unref (GHashTable *hash_table)
989 g_return_if_fail (hash_table != NULL);
991 if (g_atomic_int_dec_and_test (&hash_table->ref_count))
993 g_hash_table_remove_all_nodes (hash_table, TRUE);
994 if (hash_table->keys != hash_table->values)
995 g_free (hash_table->values);
996 g_free (hash_table->keys);
997 g_free (hash_table->hashes);
998 g_slice_free (GHashTable, hash_table);
1003 * g_hash_table_destroy:
1004 * @hash_table: a #GHashTable
1006 * Destroys all keys and values in the #GHashTable and decrements its
1007 * reference count by 1. If keys and/or values are dynamically allocated,
1008 * you should either free them first or create the #GHashTable with destroy
1009 * notifiers using g_hash_table_new_full(). In the latter case the destroy
1010 * functions you supplied will be called on all keys and values during the
1011 * destruction phase.
1014 g_hash_table_destroy (GHashTable *hash_table)
1016 g_return_if_fail (hash_table != NULL);
1018 g_hash_table_remove_all (hash_table);
1019 g_hash_table_unref (hash_table);
1023 * g_hash_table_lookup:
1024 * @hash_table: a #GHashTable
1025 * @key: the key to look up
1027 * Looks up a key in a #GHashTable. Note that this function cannot
1028 * distinguish between a key that is not present and one which is present
1029 * and has the value %NULL. If you need this distinction, use
1030 * g_hash_table_lookup_extended().
1032 * Return value: (allow-none): the associated value, or %NULL if the key is not found
1035 g_hash_table_lookup (GHashTable *hash_table,
1041 g_return_val_if_fail (hash_table != NULL, NULL);
1043 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1045 return HASH_IS_REAL (hash_table->hashes[node_index])
1046 ? hash_table->values[node_index]
1051 * g_hash_table_lookup_extended:
1052 * @hash_table: a #GHashTable
1053 * @lookup_key: the key to look up
1054 * @orig_key: (allow-none): return location for the original key, or %NULL
1055 * @value: (allow-none): return location for the value associated with the key, or %NULL
1057 * Looks up a key in the #GHashTable, returning the original key and the
1058 * associated value and a #gboolean which is %TRUE if the key was found. This
1059 * is useful if you need to free the memory allocated for the original key,
1060 * for example before calling g_hash_table_remove().
1062 * You can actually pass %NULL for @lookup_key to test
1063 * whether the %NULL key exists, provided the hash and equal functions
1064 * of @hash_table are %NULL-safe.
1066 * Return value: %TRUE if the key was found in the #GHashTable
1069 g_hash_table_lookup_extended (GHashTable *hash_table,
1070 gconstpointer lookup_key,
1077 g_return_val_if_fail (hash_table != NULL, FALSE);
1079 node_index = g_hash_table_lookup_node (hash_table, lookup_key, &node_hash);
1081 if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1085 *orig_key = hash_table->keys[node_index];
1088 *value = hash_table->values[node_index];
1094 * g_hash_table_insert_internal:
1095 * @hash_table: our #GHashTable
1096 * @key: the key to insert
1097 * @value: the value to insert
1098 * @keep_new_key: if %TRUE and this key already exists in the table
1099 * then call the destroy notify function on the old key. If %FALSE
1100 * then call the destroy notify function on the new key.
1102 * Implements the common logic for the g_hash_table_insert() and
1103 * g_hash_table_replace() functions.
1105 * Do a lookup of @key. If it is found, replace it with the new
1106 * @value (and perhaps the new @key). If it is not found, create
1110 g_hash_table_insert_internal (GHashTable *hash_table,
1113 gboolean keep_new_key)
1118 g_return_if_fail (hash_table != NULL);
1120 node_index = g_hash_table_lookup_node (hash_table, key, &key_hash);
1122 g_hash_table_insert_node (hash_table, node_index, key_hash, key, value, keep_new_key, FALSE);
1126 * g_hash_table_insert:
1127 * @hash_table: a #GHashTable
1128 * @key: a key to insert
1129 * @value: the value to associate with the key
1131 * Inserts a new key and value into a #GHashTable.
1133 * If the key already exists in the #GHashTable its current
1134 * value is replaced with the new value. If you supplied a
1135 * @value_destroy_func when creating the #GHashTable, the old
1136 * value is freed using that function. If you supplied a
1137 * @key_destroy_func when creating the #GHashTable, the passed
1138 * key is freed using that function.
1141 g_hash_table_insert (GHashTable *hash_table,
1145 g_hash_table_insert_internal (hash_table, key, value, FALSE);
1149 * g_hash_table_replace:
1150 * @hash_table: a #GHashTable
1151 * @key: a key to insert
1152 * @value: the value to associate with the key
1154 * Inserts a new key and value into a #GHashTable similar to
1155 * g_hash_table_insert(). The difference is that if the key
1156 * already exists in the #GHashTable, it gets replaced by the
1157 * new key. If you supplied a @value_destroy_func when creating
1158 * the #GHashTable, the old value is freed using that function.
1159 * If you supplied a @key_destroy_func when creating the
1160 * #GHashTable, the old key is freed using that function.
1163 g_hash_table_replace (GHashTable *hash_table,
1167 g_hash_table_insert_internal (hash_table, key, value, TRUE);
1172 * @hash_table: a #GHashTable
1173 * @key: a key to insert
1175 * This is a convenience function for using a #GHashTable as a set. It
1176 * is equivalent to calling g_hash_table_replace() with @key as both the
1177 * key and the value.
1179 * When a hash table only ever contains keys that have themselves as the
1180 * corresponding value it is able to be stored more efficiently. See
1181 * the discussion in the section description.
1186 g_hash_table_add (GHashTable *hash_table,
1189 g_hash_table_insert_internal (hash_table, key, key, TRUE);
1193 * g_hash_table_contains:
1194 * @hash_table: a #GHashTable
1195 * @key: a key to check
1197 * Checks if @key is in @hash_table.
1202 g_hash_table_contains (GHashTable *hash_table,
1208 g_return_val_if_fail (hash_table != NULL, FALSE);
1210 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1212 return HASH_IS_REAL (hash_table->hashes[node_index]);
1216 * g_hash_table_remove_internal:
1217 * @hash_table: our #GHashTable
1218 * @key: the key to remove
1219 * @notify: %TRUE if the destroy notify handlers are to be called
1220 * Return value: %TRUE if a node was found and removed, else %FALSE
1222 * Implements the common logic for the g_hash_table_remove() and
1223 * g_hash_table_steal() functions.
1225 * Do a lookup of @key and remove it if it is found, calling the
1226 * destroy notify handlers only if @notify is %TRUE.
1229 g_hash_table_remove_internal (GHashTable *hash_table,
1236 g_return_val_if_fail (hash_table != NULL, FALSE);
1238 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1240 if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1243 g_hash_table_remove_node (hash_table, node_index, notify);
1244 g_hash_table_maybe_resize (hash_table);
1246 #ifndef G_DISABLE_ASSERT
1247 hash_table->version++;
1254 * g_hash_table_remove:
1255 * @hash_table: a #GHashTable
1256 * @key: the key to remove
1258 * Removes a key and its associated value from a #GHashTable.
1260 * If the #GHashTable was created using g_hash_table_new_full(), the
1261 * key and value are freed using the supplied destroy functions, otherwise
1262 * you have to make sure that any dynamically allocated values are freed
1265 * Returns: %TRUE if the key was found and removed from the #GHashTable
1268 g_hash_table_remove (GHashTable *hash_table,
1271 return g_hash_table_remove_internal (hash_table, key, TRUE);
1275 * g_hash_table_steal:
1276 * @hash_table: a #GHashTable
1277 * @key: the key to remove
1279 * Removes a key and its associated value from a #GHashTable without
1280 * calling the key and value destroy functions.
1282 * Returns: %TRUE if the key was found and removed from the #GHashTable
1285 g_hash_table_steal (GHashTable *hash_table,
1288 return g_hash_table_remove_internal (hash_table, key, FALSE);
1292 * g_hash_table_remove_all:
1293 * @hash_table: a #GHashTable
1295 * Removes all keys and their associated values from a #GHashTable.
1297 * If the #GHashTable was created using g_hash_table_new_full(),
1298 * the keys and values are freed using the supplied destroy functions,
1299 * otherwise you have to make sure that any dynamically allocated
1300 * values are freed yourself.
1305 g_hash_table_remove_all (GHashTable *hash_table)
1307 g_return_if_fail (hash_table != NULL);
1309 #ifndef G_DISABLE_ASSERT
1310 if (hash_table->nnodes != 0)
1311 hash_table->version++;
1314 g_hash_table_remove_all_nodes (hash_table, TRUE);
1315 g_hash_table_maybe_resize (hash_table);
1319 * g_hash_table_steal_all:
1320 * @hash_table: a #GHashTable
1322 * Removes all keys and their associated values from a #GHashTable
1323 * without calling the key and value destroy functions.
1328 g_hash_table_steal_all (GHashTable *hash_table)
1330 g_return_if_fail (hash_table != NULL);
1332 #ifndef G_DISABLE_ASSERT
1333 if (hash_table->nnodes != 0)
1334 hash_table->version++;
1337 g_hash_table_remove_all_nodes (hash_table, FALSE);
1338 g_hash_table_maybe_resize (hash_table);
1342 * g_hash_table_foreach_remove_or_steal:
1343 * @hash_table: a #GHashTable
1344 * @func: the user's callback function
1345 * @user_data: data for @func
1346 * @notify: %TRUE if the destroy notify handlers are to be called
1348 * Implements the common logic for g_hash_table_foreach_remove()
1349 * and g_hash_table_foreach_steal().
1351 * Iterates over every node in the table, calling @func with the key
1352 * and value of the node (and @user_data). If @func returns %TRUE the
1353 * node is removed from the table.
1355 * If @notify is true then the destroy notify handlers will be called
1356 * for each removed node.
1359 g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
1366 #ifndef G_DISABLE_ASSERT
1367 gint version = hash_table->version;
1370 for (i = 0; i < hash_table->size; i++)
1372 guint node_hash = hash_table->hashes[i];
1373 gpointer node_key = hash_table->keys[i];
1374 gpointer node_value = hash_table->values[i];
1376 if (HASH_IS_REAL (node_hash) &&
1377 (* func) (node_key, node_value, user_data))
1379 g_hash_table_remove_node (hash_table, i, notify);
1383 #ifndef G_DISABLE_ASSERT
1384 g_return_val_if_fail (version == hash_table->version, 0);
1388 g_hash_table_maybe_resize (hash_table);
1390 #ifndef G_DISABLE_ASSERT
1392 hash_table->version++;
1399 * g_hash_table_foreach_remove:
1400 * @hash_table: a #GHashTable
1401 * @func: the function to call for each key/value pair
1402 * @user_data: user data to pass to the function
1404 * Calls the given function for each key/value pair in the
1405 * #GHashTable. If the function returns %TRUE, then the key/value
1406 * pair is removed from the #GHashTable. If you supplied key or
1407 * value destroy functions when creating the #GHashTable, they are
1408 * used to free the memory allocated for the removed keys and values.
1410 * See #GHashTableIter for an alternative way to loop over the
1411 * key/value pairs in the hash table.
1413 * Return value: the number of key/value pairs removed
1416 g_hash_table_foreach_remove (GHashTable *hash_table,
1420 g_return_val_if_fail (hash_table != NULL, 0);
1421 g_return_val_if_fail (func != NULL, 0);
1423 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
1427 * g_hash_table_foreach_steal:
1428 * @hash_table: a #GHashTable
1429 * @func: the function to call for each key/value pair
1430 * @user_data: user data to pass to the function
1432 * Calls the given function for each key/value pair in the
1433 * #GHashTable. If the function returns %TRUE, then the key/value
1434 * pair is removed from the #GHashTable, but no key or value
1435 * destroy functions are called.
1437 * See #GHashTableIter for an alternative way to loop over the
1438 * key/value pairs in the hash table.
1440 * Return value: the number of key/value pairs removed.
1443 g_hash_table_foreach_steal (GHashTable *hash_table,
1447 g_return_val_if_fail (hash_table != NULL, 0);
1448 g_return_val_if_fail (func != NULL, 0);
1450 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
1454 * g_hash_table_foreach:
1455 * @hash_table: a #GHashTable
1456 * @func: the function to call for each key/value pair
1457 * @user_data: user data to pass to the function
1459 * Calls the given function for each of the key/value pairs in the
1460 * #GHashTable. The function is passed the key and value of each
1461 * pair, and the given @user_data parameter. The hash table may not
1462 * be modified while iterating over it (you can't add/remove
1463 * items). To remove all items matching a predicate, use
1464 * g_hash_table_foreach_remove().
1466 * See g_hash_table_find() for performance caveats for linear
1467 * order searches in contrast to g_hash_table_lookup().
1470 g_hash_table_foreach (GHashTable *hash_table,
1475 #ifndef G_DISABLE_ASSERT
1479 g_return_if_fail (hash_table != NULL);
1480 g_return_if_fail (func != NULL);
1482 #ifndef G_DISABLE_ASSERT
1483 version = hash_table->version;
1486 for (i = 0; i < hash_table->size; i++)
1488 guint node_hash = hash_table->hashes[i];
1489 gpointer node_key = hash_table->keys[i];
1490 gpointer node_value = hash_table->values[i];
1492 if (HASH_IS_REAL (node_hash))
1493 (* func) (node_key, node_value, user_data);
1495 #ifndef G_DISABLE_ASSERT
1496 g_return_if_fail (version == hash_table->version);
1502 * g_hash_table_find:
1503 * @hash_table: a #GHashTable
1504 * @predicate: function to test the key/value pairs for a certain property
1505 * @user_data: user data to pass to the function
1507 * Calls the given function for key/value pairs in the #GHashTable
1508 * until @predicate returns %TRUE. The function is passed the key
1509 * and value of each pair, and the given @user_data parameter. The
1510 * hash table may not be modified while iterating over it (you can't
1511 * add/remove items).
1513 * Note, that hash tables are really only optimized for forward
1514 * lookups, i.e. g_hash_table_lookup(). So code that frequently issues
1515 * g_hash_table_find() or g_hash_table_foreach() (e.g. in the order of
1516 * once per every entry in a hash table) should probably be reworked
1517 * to use additional or different data structures for reverse lookups
1518 * (keep in mind that an O(n) find/foreach operation issued for all n
1519 * values in a hash table ends up needing O(n*n) operations).
1521 * Return value: (allow-none): The value of the first key/value pair is returned,
1522 * for which @predicate evaluates to %TRUE. If no pair with the
1523 * requested property is found, %NULL is returned.
1528 g_hash_table_find (GHashTable *hash_table,
1533 #ifndef G_DISABLE_ASSERT
1538 g_return_val_if_fail (hash_table != NULL, NULL);
1539 g_return_val_if_fail (predicate != NULL, NULL);
1541 #ifndef G_DISABLE_ASSERT
1542 version = hash_table->version;
1547 for (i = 0; i < hash_table->size; i++)
1549 guint node_hash = hash_table->hashes[i];
1550 gpointer node_key = hash_table->keys[i];
1551 gpointer node_value = hash_table->values[i];
1553 if (HASH_IS_REAL (node_hash))
1554 match = predicate (node_key, node_value, user_data);
1556 #ifndef G_DISABLE_ASSERT
1557 g_return_val_if_fail (version == hash_table->version, NULL);
1568 * g_hash_table_size:
1569 * @hash_table: a #GHashTable
1571 * Returns the number of elements contained in the #GHashTable.
1573 * Return value: the number of key/value pairs in the #GHashTable.
1576 g_hash_table_size (GHashTable *hash_table)
1578 g_return_val_if_fail (hash_table != NULL, 0);
1580 return hash_table->nnodes;
1584 * g_hash_table_get_keys:
1585 * @hash_table: a #GHashTable
1587 * Retrieves every key inside @hash_table. The returned data
1588 * is valid until @hash_table is modified.
1590 * Return value: a #GList containing all the keys inside the hash
1591 * table. The content of the list is owned by the hash table and
1592 * should not be modified or freed. Use g_list_free() when done
1598 g_hash_table_get_keys (GHashTable *hash_table)
1603 g_return_val_if_fail (hash_table != NULL, NULL);
1606 for (i = 0; i < hash_table->size; i++)
1608 if (HASH_IS_REAL (hash_table->hashes[i]))
1609 retval = g_list_prepend (retval, hash_table->keys[i]);
1616 * g_hash_table_get_values:
1617 * @hash_table: a #GHashTable
1619 * Retrieves every value inside @hash_table. The returned data
1620 * is valid until @hash_table is modified.
1622 * Return value: a #GList containing all the values inside the hash
1623 * table. The content of the list is owned by the hash table and
1624 * should not be modified or freed. Use g_list_free() when done
1630 g_hash_table_get_values (GHashTable *hash_table)
1635 g_return_val_if_fail (hash_table != NULL, NULL);
1638 for (i = 0; i < hash_table->size; i++)
1640 if (HASH_IS_REAL (hash_table->hashes[i]))
1641 retval = g_list_prepend (retval, hash_table->values[i]);
1653 * @v2: a key to compare with @v1
1655 * Compares two strings for byte-by-byte equality and returns %TRUE
1656 * if they are equal. It can be passed to g_hash_table_new() as the
1657 * @key_equal_func parameter, when using non-%NULL strings as keys in a
1660 * Note that this function is primarily meant as a hash table comparison
1661 * function. For a general-purpose, %NULL-safe string comparison function,
1664 * Returns: %TRUE if the two keys match
1667 g_str_equal (gconstpointer v1,
1670 const gchar *string1 = v1;
1671 const gchar *string2 = v2;
1673 return strcmp (string1, string2) == 0;
1680 * Converts a string to a hash value.
1682 * This function implements the widely used "djb" hash apparently posted
1683 * by Daniel Bernstein to comp.lang.c some time ago. The 32 bit
1684 * unsigned hash value starts at 5381 and for each byte 'c' in the
1685 * string, is updated: <literal>hash = hash * 33 + c</literal>. This
1686 * function uses the signed value of each byte.
1688 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1689 * when using non-%NULL strings as keys in a #GHashTable.
1691 * Returns: a hash value corresponding to the key
1694 g_str_hash (gconstpointer v)
1696 const signed char *p;
1699 for (p = v; *p != '\0'; p++)
1700 h = (h << 5) + h + *p;
1707 * @v: (allow-none): a #gpointer key
1709 * Converts a gpointer to a hash value.
1710 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1711 * when using opaque pointers compared by pointer value as keys in a
1714 * This hash function is also appropriate for keys that are integers stored
1715 * in pointers, such as <literal>GINT_TO_POINTER (n)</literal>.
1717 * Returns: a hash value corresponding to the key.
1720 g_direct_hash (gconstpointer v)
1722 return GPOINTER_TO_UINT (v);
1727 * @v1: (allow-none): a key
1728 * @v2: (allow-none): a key to compare with @v1
1730 * Compares two #gpointer arguments and returns %TRUE if they are equal.
1731 * It can be passed to g_hash_table_new() as the @key_equal_func
1732 * parameter, when using opaque pointers compared by pointer value as keys
1735 * This equality function is also appropriate for keys that are integers stored
1736 * in pointers, such as <literal>GINT_TO_POINTER (n)</literal>.
1738 * Returns: %TRUE if the two keys match.
1741 g_direct_equal (gconstpointer v1,
1749 * @v1: a pointer to a #gint key
1750 * @v2: a pointer to a #gint key to compare with @v1
1752 * Compares the two #gint values being pointed to and returns
1753 * %TRUE if they are equal.
1754 * It can be passed to g_hash_table_new() as the @key_equal_func
1755 * parameter, when using non-%NULL pointers to integers as keys in a
1758 * Note that this function acts on pointers to #gint, not on #gint directly:
1759 * if your hash table's keys are of the form
1760 * <literal>GINT_TO_POINTER (n)</literal>, use g_direct_equal() instead.
1762 * Returns: %TRUE if the two keys match.
1765 g_int_equal (gconstpointer v1,
1768 return *((const gint*) v1) == *((const gint*) v2);
1773 * @v: a pointer to a #gint key
1775 * Converts a pointer to a #gint to a hash value.
1776 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1777 * when using non-%NULL pointers to integer values as keys in a #GHashTable.
1779 * Note that this function acts on pointers to #gint, not on #gint directly:
1780 * if your hash table's keys are of the form
1781 * <literal>GINT_TO_POINTER (n)</literal>, use g_direct_hash() instead.
1783 * Returns: a hash value corresponding to the key.
1786 g_int_hash (gconstpointer v)
1788 return *(const gint*) v;
1793 * @v1: a pointer to a #gint64 key
1794 * @v2: a pointer to a #gint64 key to compare with @v1
1796 * Compares the two #gint64 values being pointed to and returns
1797 * %TRUE if they are equal.
1798 * It can be passed to g_hash_table_new() as the @key_equal_func
1799 * parameter, when using non-%NULL pointers to 64-bit integers as keys in a
1802 * Returns: %TRUE if the two keys match.
1807 g_int64_equal (gconstpointer v1,
1810 return *((const gint64*) v1) == *((const gint64*) v2);
1815 * @v: a pointer to a #gint64 key
1817 * Converts a pointer to a #gint64 to a hash value.
1819 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1820 * when using non-%NULL pointers to 64-bit integer values as keys in a
1823 * Returns: a hash value corresponding to the key.
1828 g_int64_hash (gconstpointer v)
1830 return (guint) *(const gint64*) v;
1835 * @v1: a pointer to a #gdouble key
1836 * @v2: a pointer to a #gdouble key to compare with @v1
1838 * Compares the two #gdouble values being pointed to and returns
1839 * %TRUE if they are equal.
1840 * It can be passed to g_hash_table_new() as the @key_equal_func
1841 * parameter, when using non-%NULL pointers to doubles as keys in a
1844 * Returns: %TRUE if the two keys match.
1849 g_double_equal (gconstpointer v1,
1852 return *((const gdouble*) v1) == *((const gdouble*) v2);
1857 * @v: a pointer to a #gdouble key
1859 * Converts a pointer to a #gdouble to a hash value.
1860 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1861 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1862 * when using non-%NULL pointers to doubles as keys in a #GHashTable.
1864 * Returns: a hash value corresponding to the key.
1869 g_double_hash (gconstpointer v)
1871 return (guint) *(const gdouble*) v;