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"
45 * @short_description: associations between keys and values so that
46 * given a key the value can be found quickly
48 * A #GHashTable provides associations between keys and values which is
49 * optimized so that given a key, the associated value can be found
52 * Note that neither keys nor values are copied when inserted into the
53 * #GHashTable, so they must exist for the lifetime of the #GHashTable.
54 * This means that the use of static strings is OK, but temporary
55 * strings (i.e. those created in buffers and those returned by GTK+
56 * widgets) should be copied with g_strdup() before being inserted.
58 * If keys or values are dynamically allocated, you must be careful to
59 * ensure that they are freed when they are removed from the
60 * #GHashTable, and also when they are overwritten by new insertions
61 * into the #GHashTable. It is also not advisable to mix static strings
62 * and dynamically-allocated strings in a #GHashTable, because it then
63 * becomes difficult to determine whether the string should be freed.
65 * To create a #GHashTable, use g_hash_table_new().
67 * To insert a key and value into a #GHashTable, use
68 * g_hash_table_insert().
70 * To lookup a value corresponding to a given key, use
71 * g_hash_table_lookup() and g_hash_table_lookup_extended().
73 * g_hash_table_lookup_extended() can also be used to simply
74 * check if a key is present in the hash table.
76 * To remove a key and value, use g_hash_table_remove().
78 * To call a function for each key and value pair use
79 * g_hash_table_foreach() or use a iterator to iterate over the
80 * key/value pairs in the hash table, see #GHashTableIter.
82 * To destroy a #GHashTable use g_hash_table_destroy().
85 * <title>Using a GHashTable as a set</title>
87 * A common use-case for hash tables is to store information about
88 * a set of keys, without associating any particular value with each
89 * key. GHashTable optimizes one way of doing so: If you store only
90 * key-value pairs where key == value, then GHashTable does not
91 * allocate memory to store the values, which can be a considerable
92 * space saving, if your set is large.
96 * set_new (GHashFunc hash_func,
97 * GEqualFunc equal_func,
98 * GDestroyNotify destroy)
100 * return g_hash_table_new_full (hash_func, equal_func, destroy, NULL);
104 * set_insert (GHashTable *set,
107 * g_hash_table_insert (set, element, element);
111 * set_contains (GHashTable *set,
114 * return g_hash_table_lookup_extended (set, element, NULL, NULL);
118 * set_remove (GHashTable *set,
121 * return g_hash_table_remove (set, element);
130 * The #GHashTable struct is an opaque data structure to represent a
131 * <link linkend="glib-Hash-Tables">Hash Table</link>. It should only be
132 * accessed via the following functions.
138 * @Returns: the hash value corresponding to the key.
140 * Specifies the type of the hash function which is passed to
141 * g_hash_table_new() when a #GHashTable is created.
143 * The function is passed a key and should return a #guint hash value.
144 * The functions g_direct_hash(), g_int_hash() and g_str_hash() provide
145 * hash functions which can be used when the key is a #gpointer, #gint,
146 * and #gchar* respectively.
148 * <!-- FIXME: Need more here. --> The hash values should be evenly
149 * distributed over a fairly large range? The modulus is taken with the
150 * hash table size (a prime number) to find the 'bucket' to place each
151 * key into. The function should also be very fast, since it is called
152 * for each key lookup.
158 * @value: the value corresponding to the key.
159 * @user_data: user data passed to g_hash_table_foreach().
161 * Specifies the type of the function passed to g_hash_table_foreach().
162 * It is called with each key/value pair, together with the @user_data
163 * parameter which is passed to g_hash_table_foreach().
169 * @value: the value associated with the key.
170 * @user_data: user data passed to g_hash_table_remove().
171 * @Returns: %TRUE if the key/value pair should be removed from the
174 * Specifies the type of the function passed to
175 * g_hash_table_foreach_remove(). It is called with each key/value
176 * pair, together with the @user_data parameter passed to
177 * g_hash_table_foreach_remove(). It should return %TRUE if the
178 * key/value pair should be removed from the #GHashTable.
184 * @b: a value to compare with.
185 * @Returns: %TRUE if @a = @b; %FALSE otherwise.
187 * Specifies the type of a function used to test two values for
188 * equality. The function should return %TRUE if both values are equal
189 * and %FALSE otherwise.
195 * A GHashTableIter structure represents an iterator that can be used
196 * to iterate over the elements of a #GHashTable. GHashTableIter
197 * structures are typically allocated on the stack and then initialized
198 * with g_hash_table_iter_init().
201 #define HASH_TABLE_MIN_SHIFT 3 /* 1 << 3 == 8 buckets */
203 #define UNUSED_HASH_VALUE 0
204 #define TOMBSTONE_HASH_VALUE 1
205 #define HASH_IS_UNUSED(h_) ((h_) == UNUSED_HASH_VALUE)
206 #define HASH_IS_TOMBSTONE(h_) ((h_) == TOMBSTONE_HASH_VALUE)
207 #define HASH_IS_REAL(h_) ((h_) >= 2)
215 gint noccupied; /* nnodes + tombstones */
222 GEqualFunc key_equal_func;
224 #ifndef G_DISABLE_ASSERT
226 * Tracks the structure of the hash table, not its contents: is only
227 * incremented when a node is added or removed (is not incremented
228 * when the key or data of a node is modified).
232 GDestroyNotify key_destroy_func;
233 GDestroyNotify value_destroy_func;
238 GHashTable *hash_table;
246 /* Each table size has an associated prime modulo (the first prime
247 * lower than the table size) used to find the initial bucket. Probing
248 * then works modulo 2^n. The prime modulo is necessary to get a
249 * good distribution with poor hash functions. */
250 static const gint prime_mod [] =
268 65521, /* For 1 << 16 */
283 2147483647 /* For 1 << 31 */
287 g_hash_table_set_shift (GHashTable *hash_table, gint shift)
292 hash_table->size = 1 << shift;
293 hash_table->mod = prime_mod [shift];
295 for (i = 0; i < shift; i++)
301 hash_table->mask = mask;
305 g_hash_table_find_closest_shift (gint n)
316 g_hash_table_set_shift_from_size (GHashTable *hash_table, gint size)
320 shift = g_hash_table_find_closest_shift (size);
321 shift = MAX (shift, HASH_TABLE_MIN_SHIFT);
323 g_hash_table_set_shift (hash_table, shift);
327 * g_hash_table_lookup_node:
328 * @hash_table: our #GHashTable
329 * @key: the key to lookup against
330 * @hash_return: key hash return location
331 * Return value: index of the described node
333 * Performs a lookup in the hash table, preserving extra information
334 * usually needed for insertion.
336 * This function first computes the hash value of the key using the
337 * user's hash function.
339 * If an entry in the table matching @key is found then this function
340 * returns the index of that entry in the table, and if not, the
341 * index of an unused node (empty or tombstone) where the key can be
344 * The computed hash value is returned in the variable pointed to
345 * by @hash_return. This is to save insertions from having to compute
346 * the hash record again for the new record.
349 g_hash_table_lookup_node (GHashTable *hash_table,
356 guint first_tombstone = 0;
357 gboolean have_tombstone = FALSE;
360 hash_value = hash_table->hash_func (key);
361 if (G_UNLIKELY (!HASH_IS_REAL (hash_value)))
364 *hash_return = hash_value;
366 node_index = hash_value % hash_table->mod;
367 node_hash = hash_table->hashes[node_index];
369 while (!HASH_IS_UNUSED (node_hash))
371 /* We first check if our full hash values
372 * are equal so we can avoid calling the full-blown
373 * key equality function in most cases.
375 if (node_hash == hash_value)
377 gpointer node_key = hash_table->keys[node_index];
379 if (hash_table->key_equal_func)
381 if (hash_table->key_equal_func (node_key, key))
384 else if (node_key == key)
389 else if (HASH_IS_TOMBSTONE (node_hash) && !have_tombstone)
391 first_tombstone = node_index;
392 have_tombstone = TRUE;
397 node_index &= hash_table->mask;
398 node_hash = hash_table->hashes[node_index];
402 return first_tombstone;
408 * g_hash_table_remove_node:
409 * @hash_table: our #GHashTable
410 * @node: pointer to node to remove
411 * @notify: %TRUE if the destroy notify handlers are to be called
413 * Removes a node from the hash table and updates the node count.
414 * The node is replaced by a tombstone. No table resize is performed.
416 * If @notify is %TRUE then the destroy notify functions are called
417 * for the key and value of the hash node.
420 g_hash_table_remove_node (GHashTable *hash_table,
427 key = hash_table->keys[i];
428 value = hash_table->values[i];
430 /* Erect tombstone */
431 hash_table->hashes[i] = TOMBSTONE_HASH_VALUE;
434 hash_table->keys[i] = NULL;
435 hash_table->values[i] = NULL;
437 hash_table->nnodes--;
439 if (notify && hash_table->key_destroy_func)
440 hash_table->key_destroy_func (key);
442 if (notify && hash_table->value_destroy_func)
443 hash_table->value_destroy_func (value);
448 * g_hash_table_remove_all_nodes:
449 * @hash_table: our #GHashTable
450 * @notify: %TRUE if the destroy notify handlers are to be called
452 * Removes all nodes from the table. Since this may be a precursor to
453 * freeing the table entirely, no resize is performed.
455 * If @notify is %TRUE then the destroy notify functions are called
456 * for the key and value of the hash node.
459 g_hash_table_remove_all_nodes (GHashTable *hash_table,
466 hash_table->nnodes = 0;
467 hash_table->noccupied = 0;
470 (hash_table->key_destroy_func == NULL &&
471 hash_table->value_destroy_func == NULL))
473 memset (hash_table->hashes, 0, hash_table->size * sizeof (guint));
474 memset (hash_table->keys, 0, hash_table->size * sizeof (gpointer));
475 memset (hash_table->values, 0, hash_table->size * sizeof (gpointer));
480 for (i = 0; i < hash_table->size; i++)
482 if (HASH_IS_REAL (hash_table->hashes[i]))
484 key = hash_table->keys[i];
485 value = hash_table->values[i];
487 hash_table->hashes[i] = UNUSED_HASH_VALUE;
488 hash_table->keys[i] = NULL;
489 hash_table->values[i] = NULL;
491 if (hash_table->key_destroy_func != NULL)
492 hash_table->key_destroy_func (key);
494 if (hash_table->value_destroy_func != NULL)
495 hash_table->value_destroy_func (value);
497 else if (HASH_IS_TOMBSTONE (hash_table->hashes[i]))
499 hash_table->hashes[i] = UNUSED_HASH_VALUE;
505 * g_hash_table_resize:
506 * @hash_table: our #GHashTable
508 * Resizes the hash table to the optimal size based on the number of
509 * nodes currently held. If you call this function then a resize will
510 * occur, even if one does not need to occur. Use
511 * g_hash_table_maybe_resize() instead.
513 * This function may "resize" the hash table to its current size, with
514 * the side effect of cleaning up tombstones and otherwise optimizing
515 * the probe sequences.
518 g_hash_table_resize (GHashTable *hash_table)
521 gpointer *new_values;
526 old_size = hash_table->size;
527 g_hash_table_set_shift_from_size (hash_table, hash_table->nnodes * 2);
529 new_keys = g_new0 (gpointer, hash_table->size);
530 if (hash_table->keys == hash_table->values)
531 new_values = new_keys;
533 new_values = g_new0 (gpointer, hash_table->size);
534 new_hashes = g_new0 (guint, hash_table->size);
536 for (i = 0; i < old_size; i++)
538 guint node_hash = hash_table->hashes[i];
542 if (!HASH_IS_REAL (node_hash))
545 hash_val = node_hash % hash_table->mod;
547 while (!HASH_IS_UNUSED (new_hashes[hash_val]))
551 hash_val &= hash_table->mask;
554 new_hashes[hash_val] = hash_table->hashes[i];
555 new_keys[hash_val] = hash_table->keys[i];
556 new_values[hash_val] = hash_table->values[i];
559 if (hash_table->keys != hash_table->values)
560 g_free (hash_table->values);
562 g_free (hash_table->keys);
563 g_free (hash_table->hashes);
565 hash_table->keys = new_keys;
566 hash_table->values = new_values;
567 hash_table->hashes = new_hashes;
569 hash_table->noccupied = hash_table->nnodes;
573 * g_hash_table_maybe_resize:
574 * @hash_table: our #GHashTable
576 * Resizes the hash table, if needed.
578 * Essentially, calls g_hash_table_resize() if the table has strayed
579 * too far from its ideal size for its number of nodes.
582 g_hash_table_maybe_resize (GHashTable *hash_table)
584 gint noccupied = hash_table->noccupied;
585 gint size = hash_table->size;
587 if ((size > hash_table->nnodes * 4 && size > 1 << HASH_TABLE_MIN_SHIFT) ||
588 (size <= noccupied + (noccupied / 16)))
589 g_hash_table_resize (hash_table);
594 * @hash_func: a function to create a hash value from a key.
595 * Hash values are used to determine where keys are stored within the
596 * #GHashTable data structure. The g_direct_hash(), g_int_hash(),
597 * g_int64_hash(), g_double_hash() and g_str_hash() functions are provided
598 * for some common types of keys.
599 * If hash_func is %NULL, g_direct_hash() is used.
600 * @key_equal_func: a function to check two keys for equality. This is
601 * used when looking up keys in the #GHashTable. The g_direct_equal(),
602 * g_int_equal(), g_int64_equal(), g_double_equal() and g_str_equal()
603 * functions are provided for the most common types of keys.
604 * If @key_equal_func is %NULL, keys are compared directly in a similar
605 * fashion to g_direct_equal(), but without the overhead of a function call.
607 * Creates a new #GHashTable with a reference count of 1.
609 * Return value: a new #GHashTable.
612 g_hash_table_new (GHashFunc hash_func,
613 GEqualFunc key_equal_func)
615 return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
620 * g_hash_table_new_full:
621 * @hash_func: a function to create a hash value from a key.
622 * @key_equal_func: a function to check two keys for equality.
623 * @key_destroy_func: a function to free the memory allocated for the key
624 * used when removing the entry from the #GHashTable or %NULL if you
625 * don't want to supply such a function.
626 * @value_destroy_func: a function to free the memory allocated for the
627 * value used when removing the entry from the #GHashTable or %NULL if
628 * you don't want to supply such a function.
630 * Creates a new #GHashTable like g_hash_table_new() with a reference count
631 * of 1 and allows to specify functions to free the memory allocated for the
632 * key and value that get called when removing the entry from the #GHashTable.
634 * Return value: a new #GHashTable.
637 g_hash_table_new_full (GHashFunc hash_func,
638 GEqualFunc key_equal_func,
639 GDestroyNotify key_destroy_func,
640 GDestroyNotify value_destroy_func)
642 GHashTable *hash_table;
644 hash_table = g_slice_new (GHashTable);
645 g_hash_table_set_shift (hash_table, HASH_TABLE_MIN_SHIFT);
646 hash_table->nnodes = 0;
647 hash_table->noccupied = 0;
648 hash_table->hash_func = hash_func ? hash_func : g_direct_hash;
649 hash_table->key_equal_func = key_equal_func;
650 hash_table->ref_count = 1;
651 #ifndef G_DISABLE_ASSERT
652 hash_table->version = 0;
654 hash_table->key_destroy_func = key_destroy_func;
655 hash_table->value_destroy_func = value_destroy_func;
656 hash_table->keys = g_new0 (gpointer, hash_table->size);
657 hash_table->values = hash_table->keys;
658 hash_table->hashes = g_new0 (guint, hash_table->size);
664 * g_hash_table_iter_init:
665 * @iter: an uninitialized #GHashTableIter.
666 * @hash_table: a #GHashTable.
668 * Initializes a key/value pair iterator and associates it with
669 * @hash_table. Modifying the hash table after calling this function
670 * invalidates the returned iterator.
672 * GHashTableIter iter;
673 * gpointer key, value;
675 * g_hash_table_iter_init (&iter, hash_table);
676 * while (g_hash_table_iter_next (&iter, &key, &value))
678 * /* do something with key and value */
685 g_hash_table_iter_init (GHashTableIter *iter,
686 GHashTable *hash_table)
688 RealIter *ri = (RealIter *) iter;
690 g_return_if_fail (iter != NULL);
691 g_return_if_fail (hash_table != NULL);
693 ri->hash_table = hash_table;
695 #ifndef G_DISABLE_ASSERT
696 ri->version = hash_table->version;
701 * g_hash_table_iter_next:
702 * @iter: an initialized #GHashTableIter.
703 * @key: a location to store the key, or %NULL.
704 * @value: a location to store the value, or %NULL.
706 * Advances @iter and retrieves the key and/or value that are now
707 * pointed to as a result of this advancement. If %FALSE is returned,
708 * @key and @value are not set, and the iterator becomes invalid.
710 * Return value: %FALSE if the end of the #GHashTable has been reached.
715 g_hash_table_iter_next (GHashTableIter *iter,
719 RealIter *ri = (RealIter *) iter;
722 g_return_val_if_fail (iter != NULL, FALSE);
723 #ifndef G_DISABLE_ASSERT
724 g_return_val_if_fail (ri->version == ri->hash_table->version, FALSE);
726 g_return_val_if_fail (ri->position < ri->hash_table->size, FALSE);
728 position = ri->position;
733 if (position >= ri->hash_table->size)
735 ri->position = position;
739 while (!HASH_IS_REAL (ri->hash_table->hashes[position]));
742 *key = ri->hash_table->keys[position];
744 *value = ri->hash_table->values[position];
746 ri->position = position;
751 * g_hash_table_iter_get_hash_table:
752 * @iter: an initialized #GHashTableIter.
754 * Returns the #GHashTable associated with @iter.
756 * Return value: the #GHashTable associated with @iter.
761 g_hash_table_iter_get_hash_table (GHashTableIter *iter)
763 g_return_val_if_fail (iter != NULL, NULL);
765 return ((RealIter *) iter)->hash_table;
769 iter_remove_or_steal (RealIter *ri, gboolean notify)
771 g_return_if_fail (ri != NULL);
772 #ifndef G_DISABLE_ASSERT
773 g_return_if_fail (ri->version == ri->hash_table->version);
775 g_return_if_fail (ri->position >= 0);
776 g_return_if_fail (ri->position < ri->hash_table->size);
778 g_hash_table_remove_node (ri->hash_table, ri->position, notify);
780 #ifndef G_DISABLE_ASSERT
782 ri->hash_table->version++;
787 * g_hash_table_iter_remove:
788 * @iter: an initialized #GHashTableIter.
790 * Removes the key/value pair currently pointed to by the iterator
791 * from its associated #GHashTable. Can only be called after
792 * g_hash_table_iter_next() returned %TRUE, and cannot be called more
793 * than once for the same key/value pair.
795 * If the #GHashTable was created using g_hash_table_new_full(), the
796 * key and value are freed using the supplied destroy functions, otherwise
797 * you have to make sure that any dynamically allocated values are freed
803 g_hash_table_iter_remove (GHashTableIter *iter)
805 iter_remove_or_steal ((RealIter *) iter, TRUE);
809 * g_hash_table_insert_node:
810 * @hash_table: our #GHashTable
811 * @node_index: pointer to node to insert/replace
812 * @key_hash: key hash
813 * @key: key to replace with
814 * @value: value to replace with
816 * Inserts a value at @node_index in the hash table and updates it.
819 g_hash_table_insert_node (GHashTable *hash_table,
824 gboolean keep_new_key)
830 if (G_UNLIKELY (hash_table->keys == hash_table->values && key != value))
831 hash_table->values = g_memdup (hash_table->keys, sizeof (gpointer) * hash_table->size);
833 old_hash = hash_table->hashes[node_index];
834 old_key = hash_table->keys[node_index];
835 old_value = hash_table->values[node_index];
837 if (HASH_IS_REAL (old_hash))
840 hash_table->keys[node_index] = key;
841 hash_table->values[node_index] = value;
845 hash_table->keys[node_index] = key;
846 hash_table->values[node_index] = value;
847 hash_table->hashes[node_index] = key_hash;
849 hash_table->nnodes++;
851 if (HASH_IS_UNUSED (old_hash))
853 /* We replaced an empty node, and not a tombstone */
854 hash_table->noccupied++;
855 g_hash_table_maybe_resize (hash_table);
858 #ifndef G_DISABLE_ASSERT
859 hash_table->version++;
863 if (HASH_IS_REAL (old_hash))
865 if (hash_table->key_destroy_func)
866 hash_table->key_destroy_func (keep_new_key ? old_key : key);
867 if (hash_table->value_destroy_func)
868 hash_table->value_destroy_func (old_value);
873 * g_hash_table_iter_replace:
874 * @iter: an initialized #GHashTableIter.
875 * @value: the value to replace with
877 * Replaces the value currently pointed to by the iterator
878 * from its associated #GHashTable. Can only be called after
879 * g_hash_table_iter_next() returned %TRUE.
881 * If you supplied a @value_destroy_func when creating the #GHashTable,
882 * the old value is freed using that function.
887 g_hash_table_iter_replace (GHashTableIter *iter,
894 ri = (RealIter *) iter;
896 g_return_if_fail (ri != NULL);
897 #ifndef G_DISABLE_ASSERT
898 g_return_if_fail (ri->version == ri->hash_table->version);
900 g_return_if_fail (ri->position >= 0);
901 g_return_if_fail (ri->position < ri->hash_table->size);
903 node_hash = ri->hash_table->hashes[ri->position];
904 key = ri->hash_table->keys[ri->position];
906 g_hash_table_insert_node (ri->hash_table, ri->position, node_hash, key, value, TRUE);
908 #ifndef G_DISABLE_ASSERT
910 ri->hash_table->version++;
915 * g_hash_table_iter_steal:
916 * @iter: an initialized #GHashTableIter.
918 * Removes the key/value pair currently pointed to by the iterator
919 * from its associated #GHashTable, without calling the key and value
920 * destroy functions. Can only be called after
921 * g_hash_table_iter_next() returned %TRUE, and cannot be called more
922 * than once for the same key/value pair.
927 g_hash_table_iter_steal (GHashTableIter *iter)
929 iter_remove_or_steal ((RealIter *) iter, FALSE);
935 * @hash_table: a valid #GHashTable.
937 * Atomically increments the reference count of @hash_table by one.
938 * This function is MT-safe and may be called from any thread.
940 * Return value: the passed in #GHashTable.
945 g_hash_table_ref (GHashTable *hash_table)
947 g_return_val_if_fail (hash_table != NULL, NULL);
949 g_atomic_int_inc (&hash_table->ref_count);
955 * g_hash_table_unref:
956 * @hash_table: a valid #GHashTable.
958 * Atomically decrements the reference count of @hash_table by one.
959 * If the reference count drops to 0, all keys and values will be
960 * destroyed, and all memory allocated by the hash table is released.
961 * This function is MT-safe and may be called from any thread.
966 g_hash_table_unref (GHashTable *hash_table)
968 g_return_if_fail (hash_table != NULL);
970 if (g_atomic_int_dec_and_test (&hash_table->ref_count))
972 g_hash_table_remove_all_nodes (hash_table, TRUE);
973 if (hash_table->keys != hash_table->values)
974 g_free (hash_table->values);
975 g_free (hash_table->keys);
976 g_free (hash_table->hashes);
977 g_slice_free (GHashTable, hash_table);
982 * g_hash_table_destroy:
983 * @hash_table: a #GHashTable.
985 * Destroys all keys and values in the #GHashTable and decrements its
986 * reference count by 1. If keys and/or values are dynamically allocated,
987 * you should either free them first or create the #GHashTable with destroy
988 * notifiers using g_hash_table_new_full(). In the latter case the destroy
989 * functions you supplied will be called on all keys and values during the
993 g_hash_table_destroy (GHashTable *hash_table)
995 g_return_if_fail (hash_table != NULL);
997 g_hash_table_remove_all (hash_table);
998 g_hash_table_unref (hash_table);
1002 * g_hash_table_lookup:
1003 * @hash_table: a #GHashTable.
1004 * @key: the key to look up.
1006 * Looks up a key in a #GHashTable. Note that this function cannot
1007 * distinguish between a key that is not present and one which is present
1008 * and has the value %NULL. If you need this distinction, use
1009 * g_hash_table_lookup_extended().
1011 * Return value: the associated value, or %NULL if the key is not found.
1014 g_hash_table_lookup (GHashTable *hash_table,
1020 g_return_val_if_fail (hash_table != NULL, NULL);
1022 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1024 return HASH_IS_REAL (hash_table->hashes[node_index])
1025 ? hash_table->values[node_index]
1030 * g_hash_table_lookup_extended:
1031 * @hash_table: a #GHashTable
1032 * @lookup_key: the key to look up
1033 * @orig_key: return location for the original key, or %NULL
1034 * @value: return location for the value associated with the key, or %NULL
1036 * Looks up a key in the #GHashTable, returning the original key and the
1037 * associated value and a #gboolean which is %TRUE if the key was found. This
1038 * is useful if you need to free the memory allocated for the original key,
1039 * for example before calling g_hash_table_remove().
1041 * You can actually pass %NULL for @lookup_key to test
1042 * whether the %NULL key exists, provided the hash and equal functions
1043 * of @hash_table are %NULL-safe.
1045 * Return value: %TRUE if the key was found in the #GHashTable.
1048 g_hash_table_lookup_extended (GHashTable *hash_table,
1049 gconstpointer lookup_key,
1056 g_return_val_if_fail (hash_table != NULL, FALSE);
1058 node_index = g_hash_table_lookup_node (hash_table, lookup_key, &node_hash);
1060 if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1064 *orig_key = hash_table->keys[node_index];
1067 *value = hash_table->values[node_index];
1073 * g_hash_table_insert_internal:
1074 * @hash_table: our #GHashTable
1075 * @key: the key to insert
1076 * @value: the value to insert
1077 * @keep_new_key: if %TRUE and this key already exists in the table
1078 * then call the destroy notify function on the old key. If %FALSE
1079 * then call the destroy notify function on the new key.
1081 * Implements the common logic for the g_hash_table_insert() and
1082 * g_hash_table_replace() functions.
1084 * Do a lookup of @key. If it is found, replace it with the new
1085 * @value (and perhaps the new @key). If it is not found, create a
1089 g_hash_table_insert_internal (GHashTable *hash_table,
1092 gboolean keep_new_key)
1097 g_return_if_fail (hash_table != NULL);
1099 node_index = g_hash_table_lookup_node (hash_table, key, &key_hash);
1101 g_hash_table_insert_node (hash_table, node_index, key_hash, key, value, keep_new_key);
1105 * g_hash_table_insert:
1106 * @hash_table: a #GHashTable.
1107 * @key: a key to insert.
1108 * @value: the value to associate with the key.
1110 * Inserts a new key and value into a #GHashTable.
1112 * If the key already exists in the #GHashTable its current value is replaced
1113 * with the new value. If you supplied a @value_destroy_func when creating the
1114 * #GHashTable, the old value is freed using that function. If you supplied
1115 * a @key_destroy_func when creating the #GHashTable, the passed key is freed
1116 * using that function.
1119 g_hash_table_insert (GHashTable *hash_table,
1123 g_hash_table_insert_internal (hash_table, key, value, FALSE);
1127 * g_hash_table_replace:
1128 * @hash_table: a #GHashTable.
1129 * @key: a key to insert.
1130 * @value: the value to associate with the key.
1132 * Inserts a new key and value into a #GHashTable similar to
1133 * g_hash_table_insert(). The difference is that if the key already exists
1134 * in the #GHashTable, it gets replaced by the new key. If you supplied a
1135 * @value_destroy_func when creating the #GHashTable, the old value is freed
1136 * using that function. If you supplied a @key_destroy_func when creating the
1137 * #GHashTable, the old key is freed using that function.
1140 g_hash_table_replace (GHashTable *hash_table,
1144 g_hash_table_insert_internal (hash_table, key, value, TRUE);
1148 * g_hash_table_remove_internal:
1149 * @hash_table: our #GHashTable
1150 * @key: the key to remove
1151 * @notify: %TRUE if the destroy notify handlers are to be called
1152 * Return value: %TRUE if a node was found and removed, else %FALSE
1154 * Implements the common logic for the g_hash_table_remove() and
1155 * g_hash_table_steal() functions.
1157 * Do a lookup of @key and remove it if it is found, calling the
1158 * destroy notify handlers only if @notify is %TRUE.
1161 g_hash_table_remove_internal (GHashTable *hash_table,
1168 g_return_val_if_fail (hash_table != NULL, FALSE);
1170 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1172 if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1175 g_hash_table_remove_node (hash_table, node_index, notify);
1176 g_hash_table_maybe_resize (hash_table);
1178 #ifndef G_DISABLE_ASSERT
1179 hash_table->version++;
1186 * g_hash_table_remove:
1187 * @hash_table: a #GHashTable.
1188 * @key: the key to remove.
1190 * Removes a key and its associated value from a #GHashTable.
1192 * If the #GHashTable was created using g_hash_table_new_full(), the
1193 * key and value are freed using the supplied destroy functions, otherwise
1194 * you have to make sure that any dynamically allocated values are freed
1197 * Return value: %TRUE if the key was found and removed from the #GHashTable.
1200 g_hash_table_remove (GHashTable *hash_table,
1203 return g_hash_table_remove_internal (hash_table, key, TRUE);
1207 * g_hash_table_steal:
1208 * @hash_table: a #GHashTable.
1209 * @key: the key to remove.
1211 * Removes a key and its associated value from a #GHashTable without
1212 * calling the key and value destroy functions.
1214 * Return value: %TRUE if the key was found and removed from the #GHashTable.
1217 g_hash_table_steal (GHashTable *hash_table,
1220 return g_hash_table_remove_internal (hash_table, key, FALSE);
1224 * g_hash_table_remove_all:
1225 * @hash_table: a #GHashTable
1227 * Removes all keys and their associated values from a #GHashTable.
1229 * If the #GHashTable was created using g_hash_table_new_full(), the keys
1230 * and values are freed using the supplied destroy functions, otherwise you
1231 * have to make sure that any dynamically allocated values are freed
1237 g_hash_table_remove_all (GHashTable *hash_table)
1239 g_return_if_fail (hash_table != NULL);
1241 #ifndef G_DISABLE_ASSERT
1242 if (hash_table->nnodes != 0)
1243 hash_table->version++;
1246 g_hash_table_remove_all_nodes (hash_table, TRUE);
1247 g_hash_table_maybe_resize (hash_table);
1251 * g_hash_table_steal_all:
1252 * @hash_table: a #GHashTable.
1254 * Removes all keys and their associated values from a #GHashTable
1255 * without calling the key and value destroy functions.
1260 g_hash_table_steal_all (GHashTable *hash_table)
1262 g_return_if_fail (hash_table != NULL);
1264 #ifndef G_DISABLE_ASSERT
1265 if (hash_table->nnodes != 0)
1266 hash_table->version++;
1269 g_hash_table_remove_all_nodes (hash_table, FALSE);
1270 g_hash_table_maybe_resize (hash_table);
1274 * g_hash_table_foreach_remove_or_steal:
1275 * @hash_table: our #GHashTable
1276 * @func: the user's callback function
1277 * @user_data: data for @func
1278 * @notify: %TRUE if the destroy notify handlers are to be called
1280 * Implements the common logic for g_hash_table_foreach_remove() and
1281 * g_hash_table_foreach_steal().
1283 * Iterates over every node in the table, calling @func with the key
1284 * and value of the node (and @user_data). If @func returns %TRUE the
1285 * node is removed from the table.
1287 * If @notify is true then the destroy notify handlers will be called
1288 * for each removed node.
1291 g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
1298 #ifndef G_DISABLE_ASSERT
1299 gint version = hash_table->version;
1302 for (i = 0; i < hash_table->size; i++)
1304 guint node_hash = hash_table->hashes[i];
1305 gpointer node_key = hash_table->keys[i];
1306 gpointer node_value = hash_table->values[i];
1308 if (HASH_IS_REAL (node_hash) &&
1309 (* func) (node_key, node_value, user_data))
1311 g_hash_table_remove_node (hash_table, i, notify);
1315 #ifndef G_DISABLE_ASSERT
1316 g_return_val_if_fail (version == hash_table->version, 0);
1320 g_hash_table_maybe_resize (hash_table);
1322 #ifndef G_DISABLE_ASSERT
1324 hash_table->version++;
1331 * g_hash_table_foreach_remove:
1332 * @hash_table: a #GHashTable.
1333 * @func: the function to call for each key/value pair.
1334 * @user_data: user data to pass to the function.
1336 * Calls the given function for each key/value pair in the #GHashTable.
1337 * If the function returns %TRUE, then the key/value pair is removed from the
1338 * #GHashTable. If you supplied key or value destroy functions when creating
1339 * the #GHashTable, they are used to free the memory allocated for the removed
1342 * See #GHashTableIter for an alternative way to loop over the
1343 * key/value pairs in the hash table.
1345 * Return value: the number of key/value pairs removed.
1348 g_hash_table_foreach_remove (GHashTable *hash_table,
1352 g_return_val_if_fail (hash_table != NULL, 0);
1353 g_return_val_if_fail (func != NULL, 0);
1355 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
1359 * g_hash_table_foreach_steal:
1360 * @hash_table: a #GHashTable.
1361 * @func: the function to call for each key/value pair.
1362 * @user_data: user data to pass to the function.
1364 * Calls the given function for each key/value pair in the #GHashTable.
1365 * If the function returns %TRUE, then the key/value pair is removed from the
1366 * #GHashTable, but no key or value destroy functions are called.
1368 * See #GHashTableIter for an alternative way to loop over the
1369 * key/value pairs in the hash table.
1371 * Return value: the number of key/value pairs removed.
1374 g_hash_table_foreach_steal (GHashTable *hash_table,
1378 g_return_val_if_fail (hash_table != NULL, 0);
1379 g_return_val_if_fail (func != NULL, 0);
1381 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
1385 * g_hash_table_foreach:
1386 * @hash_table: a #GHashTable.
1387 * @func: the function to call for each key/value pair.
1388 * @user_data: user data to pass to the function.
1390 * Calls the given function for each of the key/value pairs in the
1391 * #GHashTable. The function is passed the key and value of each
1392 * pair, and the given @user_data parameter. The hash table may not
1393 * be modified while iterating over it (you can't add/remove
1394 * items). To remove all items matching a predicate, use
1395 * g_hash_table_foreach_remove().
1397 * See g_hash_table_find() for performance caveats for linear
1398 * order searches in contrast to g_hash_table_lookup().
1401 g_hash_table_foreach (GHashTable *hash_table,
1406 #ifndef G_DISABLE_ASSERT
1407 gint version = hash_table->version;
1410 g_return_if_fail (hash_table != NULL);
1411 g_return_if_fail (func != NULL);
1413 for (i = 0; i < hash_table->size; i++)
1415 guint node_hash = hash_table->hashes[i];
1416 gpointer node_key = hash_table->keys[i];
1417 gpointer node_value = hash_table->values[i];
1419 if (HASH_IS_REAL (node_hash))
1420 (* func) (node_key, node_value, user_data);
1422 #ifndef G_DISABLE_ASSERT
1423 g_return_if_fail (version == hash_table->version);
1429 * g_hash_table_find:
1430 * @hash_table: a #GHashTable.
1431 * @predicate: function to test the key/value pairs for a certain property.
1432 * @user_data: user data to pass to the function.
1434 * Calls the given function for key/value pairs in the #GHashTable until
1435 * @predicate returns %TRUE. The function is passed the key and value of
1436 * each pair, and the given @user_data parameter. The hash table may not
1437 * be modified while iterating over it (you can't add/remove items).
1439 * Note, that hash tables are really only optimized for forward lookups,
1440 * i.e. g_hash_table_lookup().
1441 * So code that frequently issues g_hash_table_find() or
1442 * g_hash_table_foreach() (e.g. in the order of once per every entry in a
1443 * hash table) should probably be reworked to use additional or different
1444 * data structures for reverse lookups (keep in mind that an O(n) find/foreach
1445 * operation issued for all n values in a hash table ends up needing O(n*n)
1448 * Return value: The value of the first key/value pair is returned,
1449 * for which @predicate evaluates to %TRUE. If no pair with the
1450 * requested property is found, %NULL is returned.
1455 g_hash_table_find (GHashTable *hash_table,
1460 #ifndef G_DISABLE_ASSERT
1461 gint version = hash_table->version;
1465 g_return_val_if_fail (hash_table != NULL, NULL);
1466 g_return_val_if_fail (predicate != NULL, NULL);
1470 for (i = 0; i < hash_table->size; i++)
1472 guint node_hash = hash_table->hashes[i];
1473 gpointer node_key = hash_table->keys[i];
1474 gpointer node_value = hash_table->values[i];
1476 if (HASH_IS_REAL (node_hash))
1477 match = predicate (node_key, node_value, user_data);
1479 #ifndef G_DISABLE_ASSERT
1480 g_return_val_if_fail (version == hash_table->version, NULL);
1491 * g_hash_table_size:
1492 * @hash_table: a #GHashTable.
1494 * Returns the number of elements contained in the #GHashTable.
1496 * Return value: the number of key/value pairs in the #GHashTable.
1499 g_hash_table_size (GHashTable *hash_table)
1501 g_return_val_if_fail (hash_table != NULL, 0);
1503 return hash_table->nnodes;
1507 * g_hash_table_get_keys:
1508 * @hash_table: a #GHashTable
1510 * Retrieves every key inside @hash_table. The returned data is valid
1511 * until @hash_table is modified.
1513 * Return value: a #GList containing all the keys inside the hash
1514 * table. The content of the list is owned by the hash table and
1515 * should not be modified or freed. Use g_list_free() when done
1521 g_hash_table_get_keys (GHashTable *hash_table)
1526 g_return_val_if_fail (hash_table != NULL, NULL);
1529 for (i = 0; i < hash_table->size; i++)
1531 if (HASH_IS_REAL (hash_table->hashes[i]))
1532 retval = g_list_prepend (retval, hash_table->keys[i]);
1539 * g_hash_table_get_values:
1540 * @hash_table: a #GHashTable
1542 * Retrieves every value inside @hash_table. The returned data is
1543 * valid until @hash_table is modified.
1545 * Return value: a #GList containing all the values inside the hash
1546 * table. The content of the list is owned by the hash table and
1547 * should not be modified or freed. Use g_list_free() when done
1553 g_hash_table_get_values (GHashTable *hash_table)
1558 g_return_val_if_fail (hash_table != NULL, NULL);
1561 for (i = 0; i < hash_table->size; i++)
1563 if (HASH_IS_REAL (hash_table->hashes[i]))
1564 retval = g_list_prepend (retval, hash_table->values[i]);