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 */
38 #include "gtestutils.h"
44 * @short_description: associations between keys and values so that
45 * given a key the value can be found quickly
47 * A #GHashTable provides associations between keys and values which is
48 * optimized so that given a key, the associated value can be found
51 * Note that neither keys nor values are copied when inserted into the
52 * #GHashTable, so they must exist for the lifetime of the #GHashTable.
53 * This means that the use of static strings is OK, but temporary
54 * strings (i.e. those created in buffers and those returned by GTK+
55 * widgets) should be copied with g_strdup() before being inserted.
57 * If keys or values are dynamically allocated, you must be careful to
58 * ensure that they are freed when they are removed from the
59 * #GHashTable, and also when they are overwritten by new insertions
60 * into the #GHashTable. It is also not advisable to mix static strings
61 * and dynamically-allocated strings in a #GHashTable, because it then
62 * becomes difficult to determine whether the string should be freed.
64 * To create a #GHashTable, use g_hash_table_new().
66 * To insert a key and value into a #GHashTable, use
67 * g_hash_table_insert().
69 * To lookup a value corresponding to a given key, use
70 * g_hash_table_lookup() and g_hash_table_lookup_extended().
72 * To remove a key and value, use g_hash_table_remove().
74 * To call a function for each key and value pair use
75 * g_hash_table_foreach() or use a iterator to iterate over the
76 * key/value pairs in the hash table, see #GHashTableIter.
78 * To destroy a #GHashTable use g_hash_table_destroy().
84 * The #GHashTable struct is an opaque data structure to represent a
85 * <link linkend="glib-Hash-Tables">Hash Table</link>. It should only be
86 * accessed via the following functions.
92 * @Returns: the hash value corresponding to the key.
94 * Specifies the type of the hash function which is passed to
95 * g_hash_table_new() when a #GHashTable is created.
97 * The function is passed a key and should return a #guint hash value.
98 * The functions g_direct_hash(), g_int_hash() and g_str_hash() provide
99 * hash functions which can be used when the key is a #gpointer, #gint,
100 * and #gchar* respectively.
102 * <!-- FIXME: Need more here. --> The hash values should be evenly
103 * distributed over a fairly large range? The modulus is taken with the
104 * hash table size (a prime number) to find the 'bucket' to place each
105 * key into. The function should also be very fast, since it is called
106 * for each key lookup.
112 * @value: the value corresponding to the key.
113 * @user_data: user data passed to g_hash_table_foreach().
115 * Specifies the type of the function passed to g_hash_table_foreach().
116 * It is called with each key/value pair, together with the @user_data
117 * parameter which is passed to g_hash_table_foreach().
123 * @value: the value associated with the key.
124 * @user_data: user data passed to g_hash_table_remove().
125 * @Returns: %TRUE if the key/value pair should be removed from the
128 * Specifies the type of the function passed to
129 * g_hash_table_foreach_remove(). It is called with each key/value
130 * pair, together with the @user_data parameter passed to
131 * g_hash_table_foreach_remove(). It should return %TRUE if the
132 * key/value pair should be removed from the #GHashTable.
138 * @b: a value to compare with.
139 * @Returns: %TRUE if @a = @b; %FALSE otherwise.
141 * Specifies the type of a function used to test two values for
142 * equality. The function should return %TRUE if both values are equal
143 * and %FALSE otherwise.
149 * A GHashTableIter structure represents an iterator that can be used
150 * to iterate over the elements of a #GHashTable. GHashTableIter
151 * structures are typically allocated on the stack and then initialized
152 * with g_hash_table_iter_init().
155 #define HASH_TABLE_MIN_SHIFT 3 /* 1 << 3 == 8 buckets */
157 typedef struct _GHashNode GHashNode;
164 /* If key_hash == 0, node is not in use
165 * If key_hash == 1, node is a tombstone
166 * If key_hash >= 2, node contains data */
176 gint noccupied; /* nnodes + tombstones */
179 GEqualFunc key_equal_func;
180 volatile gint ref_count;
181 #ifndef G_DISABLE_ASSERT
183 * Tracks the structure of the hash table, not its contents: is only
184 * incremented when a node is added or removed (is not incremented
185 * when the key or data of a node is modified).
189 GDestroyNotify key_destroy_func;
190 GDestroyNotify value_destroy_func;
195 GHashTable *hash_table;
203 /* Each table size has an associated prime modulo (the first prime
204 * lower than the table size) used to find the initial bucket. Probing
205 * then works modulo 2^n. The prime modulo is necessary to get a
206 * good distribution with poor hash functions. */
207 static const gint prime_mod [] =
225 65521, /* For 1 << 16 */
240 2147483647 /* For 1 << 31 */
244 g_hash_table_set_shift (GHashTable *hash_table, gint shift)
249 hash_table->size = 1 << shift;
250 hash_table->mod = prime_mod [shift];
252 for (i = 0; i < shift; i++)
258 hash_table->mask = mask;
262 g_hash_table_find_closest_shift (gint n)
273 g_hash_table_set_shift_from_size (GHashTable *hash_table, gint size)
277 shift = g_hash_table_find_closest_shift (size);
278 shift = MAX (shift, HASH_TABLE_MIN_SHIFT);
280 g_hash_table_set_shift (hash_table, shift);
284 * g_hash_table_lookup_node:
285 * @hash_table: our #GHashTable
286 * @key: the key to lookup against
287 * @hash_return: optional key hash return location
288 * Return value: index of the described #GHashNode
290 * Performs a lookup in the hash table. Virtually all hash operations
291 * will use this function internally.
293 * This function first computes the hash value of the key using the
294 * user's hash function.
296 * If an entry in the table matching @key is found then this function
297 * returns the index of that entry in the table, and if not, the
298 * index of an empty node (never a tombstone).
301 g_hash_table_lookup_node (GHashTable *hash_table,
309 /* Empty buckets have hash_value set to 0, and for tombstones, it's 1.
310 * We need to make sure our hash value is not one of these. */
312 hash_value = (* hash_table->hash_func) (key);
313 if (G_UNLIKELY (hash_value <= 1))
316 node_index = hash_value % hash_table->mod;
317 node = &hash_table->nodes [node_index];
319 while (node->key_hash)
321 /* We first check if our full hash values
322 * are equal so we can avoid calling the full-blown
323 * key equality function in most cases.
326 if (node->key_hash == hash_value)
328 if (hash_table->key_equal_func)
330 if (hash_table->key_equal_func (node->key, key))
333 else if (node->key == key)
341 node_index &= hash_table->mask;
342 node = &hash_table->nodes [node_index];
349 * g_hash_table_lookup_node_for_insertion:
350 * @hash_table: our #GHashTable
351 * @key: the key to lookup against
352 * @hash_return: key hash return location
353 * Return value: index of the described #GHashNode
355 * Performs a lookup in the hash table, preserving extra information
356 * usually needed for insertion.
358 * This function first computes the hash value of the key using the
359 * user's hash function.
361 * If an entry in the table matching @key is found then this function
362 * returns the index of that entry in the table, and if not, the
363 * index of an unused node (empty or tombstone) where the key can be
366 * The computed hash value is returned in the variable pointed to
367 * by @hash_return. This is to save insertions from having to compute
368 * the hash record again for the new record.
371 g_hash_table_lookup_node_for_insertion (GHashTable *hash_table,
378 guint first_tombstone;
379 gboolean have_tombstone = FALSE;
382 /* Empty buckets have hash_value set to 0, and for tombstones, it's 1.
383 * We need to make sure our hash value is not one of these. */
385 hash_value = (* hash_table->hash_func) (key);
386 if (G_UNLIKELY (hash_value <= 1))
389 *hash_return = hash_value;
391 node_index = hash_value % hash_table->mod;
392 node = &hash_table->nodes [node_index];
394 while (node->key_hash)
396 /* We first check if our full hash values
397 * are equal so we can avoid calling the full-blown
398 * key equality function in most cases.
401 if (node->key_hash == hash_value)
403 if (hash_table->key_equal_func)
405 if (hash_table->key_equal_func (node->key, key))
408 else if (node->key == key)
413 else if (node->key_hash == 1 && !have_tombstone)
415 first_tombstone = node_index;
416 have_tombstone = TRUE;
421 node_index &= hash_table->mask;
422 node = &hash_table->nodes [node_index];
426 return first_tombstone;
432 * g_hash_table_remove_node:
433 * @hash_table: our #GHashTable
434 * @node: pointer to node to remove
435 * @notify: %TRUE if the destroy notify handlers are to be called
437 * Removes a node from the hash table and updates the node count.
438 * The node is replaced by a tombstone. No table resize is performed.
440 * If @notify is %TRUE then the destroy notify functions are called
441 * for the key and value of the hash node.
444 g_hash_table_remove_node (GHashTable *hash_table,
448 if (notify && hash_table->key_destroy_func)
449 hash_table->key_destroy_func (node->key);
451 if (notify && hash_table->value_destroy_func)
452 hash_table->value_destroy_func (node->value);
454 /* Erect tombstone */
461 hash_table->nnodes--;
465 * g_hash_table_remove_all_nodes:
466 * @hash_table: our #GHashTable
467 * @notify: %TRUE if the destroy notify handlers are to be called
469 * Removes all nodes from the table. Since this may be a precursor to
470 * freeing the table entirely, no resize is performed.
472 * If @notify is %TRUE then the destroy notify functions are called
473 * for the key and value of the hash node.
476 g_hash_table_remove_all_nodes (GHashTable *hash_table,
481 for (i = 0; i < hash_table->size; i++)
483 GHashNode *node = &hash_table->nodes [i];
485 if (node->key_hash > 1)
487 if (notify && hash_table->key_destroy_func)
488 hash_table->key_destroy_func (node->key);
490 if (notify && hash_table->value_destroy_func)
491 hash_table->value_destroy_func (node->value);
495 /* We need to set node->key_hash = 0 for all nodes - might as well be GC
496 * friendly and clear everything */
497 memset (hash_table->nodes, 0, hash_table->size * sizeof (GHashNode));
499 hash_table->nnodes = 0;
500 hash_table->noccupied = 0;
504 * g_hash_table_resize:
505 * @hash_table: our #GHashTable
507 * Resizes the hash table to the optimal size based on the number of
508 * nodes currently held. If you call this function then a resize will
509 * occur, even if one does not need to occur. Use
510 * g_hash_table_maybe_resize() instead.
512 * This function may "resize" the hash table to its current size, with
513 * the side effect of cleaning up tombstones and otherwise optimizing
514 * the probe sequences.
517 g_hash_table_resize (GHashTable *hash_table)
519 GHashNode *new_nodes;
523 old_size = hash_table->size;
524 g_hash_table_set_shift_from_size (hash_table, hash_table->nnodes * 2);
526 new_nodes = g_new0 (GHashNode, hash_table->size);
528 for (i = 0; i < old_size; i++)
530 GHashNode *node = &hash_table->nodes [i];
535 if (node->key_hash <= 1)
538 hash_val = node->key_hash % hash_table->mod;
539 new_node = &new_nodes [hash_val];
541 while (new_node->key_hash)
545 hash_val &= hash_table->mask;
546 new_node = &new_nodes [hash_val];
552 g_free (hash_table->nodes);
553 hash_table->nodes = new_nodes;
554 hash_table->noccupied = hash_table->nnodes;
558 * g_hash_table_maybe_resize:
559 * @hash_table: our #GHashTable
561 * Resizes the hash table, if needed.
563 * Essentially, calls g_hash_table_resize() if the table has strayed
564 * too far from its ideal size for its number of nodes.
567 g_hash_table_maybe_resize (GHashTable *hash_table)
569 gint noccupied = hash_table->noccupied;
570 gint size = hash_table->size;
572 if ((size > hash_table->nnodes * 4 && size > 1 << HASH_TABLE_MIN_SHIFT) ||
573 (size <= noccupied + (noccupied / 16)))
574 g_hash_table_resize (hash_table);
579 * @hash_func: a function to create a hash value from a key.
580 * Hash values are used to determine where keys are stored within the
581 * #GHashTable data structure. The g_direct_hash(), g_int_hash(),
582 * g_int64_hash(), g_double_hash() and g_str_hash() functions are provided
583 * for some common types of keys.
584 * If hash_func is %NULL, g_direct_hash() is used.
585 * @key_equal_func: a function to check two keys for equality. This is
586 * used when looking up keys in the #GHashTable. The g_direct_equal(),
587 * g_int_equal(), g_int64_equal(), g_double_equal() and g_str_equal()
588 * functions are provided for the most common types of keys.
589 * If @key_equal_func is %NULL, keys are compared directly in a similar
590 * fashion to g_direct_equal(), but without the overhead of a function call.
592 * Creates a new #GHashTable with a reference count of 1.
594 * Return value: a new #GHashTable.
597 g_hash_table_new (GHashFunc hash_func,
598 GEqualFunc key_equal_func)
600 return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
605 * g_hash_table_new_full:
606 * @hash_func: a function to create a hash value from a key.
607 * @key_equal_func: a function to check two keys for equality.
608 * @key_destroy_func: a function to free the memory allocated for the key
609 * used when removing the entry from the #GHashTable or %NULL if you
610 * don't want to supply such a function.
611 * @value_destroy_func: a function to free the memory allocated for the
612 * value used when removing the entry from the #GHashTable or %NULL if
613 * you don't want to supply such a function.
615 * Creates a new #GHashTable like g_hash_table_new() with a reference count
616 * of 1 and allows to specify functions to free the memory allocated for the
617 * key and value that get called when removing the entry from the #GHashTable.
619 * Return value: a new #GHashTable.
622 g_hash_table_new_full (GHashFunc hash_func,
623 GEqualFunc key_equal_func,
624 GDestroyNotify key_destroy_func,
625 GDestroyNotify value_destroy_func)
627 GHashTable *hash_table;
629 hash_table = g_slice_new (GHashTable);
630 g_hash_table_set_shift (hash_table, HASH_TABLE_MIN_SHIFT);
631 hash_table->nnodes = 0;
632 hash_table->noccupied = 0;
633 hash_table->hash_func = hash_func ? hash_func : g_direct_hash;
634 hash_table->key_equal_func = key_equal_func;
635 hash_table->ref_count = 1;
636 #ifndef G_DISABLE_ASSERT
637 hash_table->version = 0;
639 hash_table->key_destroy_func = key_destroy_func;
640 hash_table->value_destroy_func = value_destroy_func;
641 hash_table->nodes = g_new0 (GHashNode, hash_table->size);
647 * g_hash_table_iter_init:
648 * @iter: an uninitialized #GHashTableIter.
649 * @hash_table: a #GHashTable.
651 * Initializes a key/value pair iterator and associates it with
652 * @hash_table. Modifying the hash table after calling this function
653 * invalidates the returned iterator.
655 * GHashTableIter iter;
656 * gpointer key, value;
658 * g_hash_table_iter_init (&iter, hash_table);
659 * while (g_hash_table_iter_next (&iter, &key, &value))
661 * /* do something with key and value */
668 g_hash_table_iter_init (GHashTableIter *iter,
669 GHashTable *hash_table)
671 RealIter *ri = (RealIter *) iter;
673 g_return_if_fail (iter != NULL);
674 g_return_if_fail (hash_table != NULL);
676 ri->hash_table = hash_table;
678 #ifndef G_DISABLE_ASSERT
679 ri->version = hash_table->version;
684 * g_hash_table_iter_next:
685 * @iter: an initialized #GHashTableIter.
686 * @key: a location to store the key, or %NULL.
687 * @value: a location to store the value, or %NULL.
689 * Advances @iter and retrieves the key and/or value that are now
690 * pointed to as a result of this advancement. If %FALSE is returned,
691 * @key and @value are not set, and the iterator becomes invalid.
693 * Return value: %FALSE if the end of the #GHashTable has been reached.
698 g_hash_table_iter_next (GHashTableIter *iter,
702 RealIter *ri = (RealIter *) iter;
706 g_return_val_if_fail (iter != NULL, FALSE);
707 #ifndef G_DISABLE_ASSERT
708 g_return_val_if_fail (ri->version == ri->hash_table->version, FALSE);
710 g_return_val_if_fail (ri->position < ri->hash_table->size, FALSE);
712 position = ri->position;
717 if (position >= ri->hash_table->size)
719 ri->position = position;
723 node = &ri->hash_table->nodes [position];
725 while (node->key_hash <= 1);
730 *value = node->value;
732 ri->position = position;
737 * g_hash_table_iter_get_hash_table:
738 * @iter: an initialized #GHashTableIter.
740 * Returns the #GHashTable associated with @iter.
742 * Return value: the #GHashTable associated with @iter.
747 g_hash_table_iter_get_hash_table (GHashTableIter *iter)
749 g_return_val_if_fail (iter != NULL, NULL);
751 return ((RealIter *) iter)->hash_table;
755 iter_remove_or_steal (RealIter *ri, gboolean notify)
757 g_return_if_fail (ri != NULL);
758 #ifndef G_DISABLE_ASSERT
759 g_return_if_fail (ri->version == ri->hash_table->version);
761 g_return_if_fail (ri->position >= 0);
762 g_return_if_fail (ri->position < ri->hash_table->size);
764 g_hash_table_remove_node (ri->hash_table, &ri->hash_table->nodes [ri->position], notify);
766 #ifndef G_DISABLE_ASSERT
768 ri->hash_table->version++;
773 * g_hash_table_iter_remove:
774 * @iter: an initialized #GHashTableIter.
776 * Removes the key/value pair currently pointed to by the iterator
777 * from its associated #GHashTable. Can only be called after
778 * g_hash_table_iter_next() returned %TRUE, and cannot be called more
779 * than once for the same key/value pair.
781 * If the #GHashTable was created using g_hash_table_new_full(), the
782 * key and value are freed using the supplied destroy functions, otherwise
783 * you have to make sure that any dynamically allocated values are freed
789 g_hash_table_iter_remove (GHashTableIter *iter)
791 iter_remove_or_steal ((RealIter *) iter, TRUE);
795 * g_hash_table_iter_steal:
796 * @iter: an initialized #GHashTableIter.
798 * Removes the key/value pair currently pointed to by the iterator
799 * from its associated #GHashTable, without calling the key and value
800 * destroy functions. Can only be called after
801 * g_hash_table_iter_next() returned %TRUE, and cannot be called more
802 * than once for the same key/value pair.
807 g_hash_table_iter_steal (GHashTableIter *iter)
809 iter_remove_or_steal ((RealIter *) iter, FALSE);
815 * @hash_table: a valid #GHashTable.
817 * Atomically increments the reference count of @hash_table by one.
818 * This function is MT-safe and may be called from any thread.
820 * Return value: the passed in #GHashTable.
825 g_hash_table_ref (GHashTable *hash_table)
827 g_return_val_if_fail (hash_table != NULL, NULL);
828 g_return_val_if_fail (hash_table->ref_count > 0, hash_table);
830 g_atomic_int_add (&hash_table->ref_count, 1);
835 * g_hash_table_unref:
836 * @hash_table: a valid #GHashTable.
838 * Atomically decrements the reference count of @hash_table by one.
839 * If the reference count drops to 0, all keys and values will be
840 * destroyed, and all memory allocated by the hash table is released.
841 * This function is MT-safe and may be called from any thread.
846 g_hash_table_unref (GHashTable *hash_table)
848 g_return_if_fail (hash_table != NULL);
849 g_return_if_fail (hash_table->ref_count > 0);
851 if (g_atomic_int_exchange_and_add (&hash_table->ref_count, -1) - 1 == 0)
853 g_hash_table_remove_all_nodes (hash_table, TRUE);
854 g_free (hash_table->nodes);
855 g_slice_free (GHashTable, hash_table);
860 * g_hash_table_destroy:
861 * @hash_table: a #GHashTable.
863 * Destroys all keys and values in the #GHashTable and decrements its
864 * reference count by 1. If keys and/or values are dynamically allocated,
865 * you should either free them first or create the #GHashTable with destroy
866 * notifiers using g_hash_table_new_full(). In the latter case the destroy
867 * functions you supplied will be called on all keys and values during the
871 g_hash_table_destroy (GHashTable *hash_table)
873 g_return_if_fail (hash_table != NULL);
874 g_return_if_fail (hash_table->ref_count > 0);
876 g_hash_table_remove_all (hash_table);
877 g_hash_table_unref (hash_table);
881 * g_hash_table_lookup:
882 * @hash_table: a #GHashTable.
883 * @key: the key to look up.
885 * Looks up a key in a #GHashTable. Note that this function cannot
886 * distinguish between a key that is not present and one which is present
887 * and has the value %NULL. If you need this distinction, use
888 * g_hash_table_lookup_extended().
890 * Return value: the associated value, or %NULL if the key is not found.
893 g_hash_table_lookup (GHashTable *hash_table,
899 g_return_val_if_fail (hash_table != NULL, NULL);
901 node_index = g_hash_table_lookup_node (hash_table, key);
902 node = &hash_table->nodes [node_index];
904 return node->key_hash ? node->value : NULL;
908 * g_hash_table_lookup_extended:
909 * @hash_table: a #GHashTable
910 * @lookup_key: the key to look up
911 * @orig_key: return location for the original key, or %NULL
912 * @value: return location for the value associated with the key, or %NULL
914 * Looks up a key in the #GHashTable, returning the original key and the
915 * associated value and a #gboolean which is %TRUE if the key was found. This
916 * is useful if you need to free the memory allocated for the original key,
917 * for example before calling g_hash_table_remove().
919 * You can actually pass %NULL for @lookup_key to test
920 * whether the %NULL key exists.
922 * Return value: %TRUE if the key was found in the #GHashTable.
925 g_hash_table_lookup_extended (GHashTable *hash_table,
926 gconstpointer lookup_key,
933 g_return_val_if_fail (hash_table != NULL, FALSE);
935 node_index = g_hash_table_lookup_node (hash_table, lookup_key);
936 node = &hash_table->nodes [node_index];
942 *orig_key = node->key;
945 *value = node->value;
951 * g_hash_table_insert_internal:
952 * @hash_table: our #GHashTable
953 * @key: the key to insert
954 * @value: the value to insert
955 * @keep_new_key: if %TRUE and this key already exists in the table
956 * then call the destroy notify function on the old key. If %FALSE
957 * then call the destroy notify function on the new key.
959 * Implements the common logic for the g_hash_table_insert() and
960 * g_hash_table_replace() functions.
962 * Do a lookup of @key. If it is found, replace it with the new
963 * @value (and perhaps the new @key). If it is not found, create a
967 g_hash_table_insert_internal (GHashTable *hash_table,
970 gboolean keep_new_key)
977 g_return_if_fail (hash_table != NULL);
978 g_return_if_fail (hash_table->ref_count > 0);
980 node_index = g_hash_table_lookup_node_for_insertion (hash_table, key, &key_hash);
981 node = &hash_table->nodes [node_index];
983 old_hash = node->key_hash;
989 if (hash_table->key_destroy_func)
990 hash_table->key_destroy_func (node->key);
995 if (hash_table->key_destroy_func)
996 hash_table->key_destroy_func (key);
999 if (hash_table->value_destroy_func)
1000 hash_table->value_destroy_func (node->value);
1002 node->value = value;
1007 node->value = value;
1008 node->key_hash = key_hash;
1010 hash_table->nnodes++;
1014 /* We replaced an empty node, and not a tombstone */
1015 hash_table->noccupied++;
1016 g_hash_table_maybe_resize (hash_table);
1019 #ifndef G_DISABLE_ASSERT
1020 hash_table->version++;
1026 * g_hash_table_insert:
1027 * @hash_table: a #GHashTable.
1028 * @key: a key to insert.
1029 * @value: the value to associate with the key.
1031 * Inserts a new key and value into a #GHashTable.
1033 * If the key already exists in the #GHashTable its current value is replaced
1034 * with the new value. If you supplied a @value_destroy_func when creating the
1035 * #GHashTable, the old value is freed using that function. If you supplied
1036 * a @key_destroy_func when creating the #GHashTable, the passed key is freed
1037 * using that function.
1040 g_hash_table_insert (GHashTable *hash_table,
1044 g_hash_table_insert_internal (hash_table, key, value, FALSE);
1048 * g_hash_table_replace:
1049 * @hash_table: a #GHashTable.
1050 * @key: a key to insert.
1051 * @value: the value to associate with the key.
1053 * Inserts a new key and value into a #GHashTable similar to
1054 * g_hash_table_insert(). The difference is that if the key already exists
1055 * in the #GHashTable, it gets replaced by the new key. If you supplied a
1056 * @value_destroy_func when creating the #GHashTable, the old value is freed
1057 * using that function. If you supplied a @key_destroy_func when creating the
1058 * #GHashTable, the old key is freed using that function.
1061 g_hash_table_replace (GHashTable *hash_table,
1065 g_hash_table_insert_internal (hash_table, key, value, TRUE);
1069 * g_hash_table_remove_internal:
1070 * @hash_table: our #GHashTable
1071 * @key: the key to remove
1072 * @notify: %TRUE if the destroy notify handlers are to be called
1073 * Return value: %TRUE if a node was found and removed, else %FALSE
1075 * Implements the common logic for the g_hash_table_remove() and
1076 * g_hash_table_steal() functions.
1078 * Do a lookup of @key and remove it if it is found, calling the
1079 * destroy notify handlers only if @notify is %TRUE.
1082 g_hash_table_remove_internal (GHashTable *hash_table,
1089 g_return_val_if_fail (hash_table != NULL, FALSE);
1091 node_index = g_hash_table_lookup_node (hash_table, key);
1092 node = &hash_table->nodes [node_index];
1094 /* g_hash_table_lookup_node() never returns a tombstone, so this is safe */
1095 if (!node->key_hash)
1098 g_hash_table_remove_node (hash_table, node, notify);
1099 g_hash_table_maybe_resize (hash_table);
1101 #ifndef G_DISABLE_ASSERT
1102 hash_table->version++;
1109 * g_hash_table_remove:
1110 * @hash_table: a #GHashTable.
1111 * @key: the key to remove.
1113 * Removes a key and its associated value from a #GHashTable.
1115 * If the #GHashTable was created using g_hash_table_new_full(), the
1116 * key and value are freed using the supplied destroy functions, otherwise
1117 * you have to make sure that any dynamically allocated values are freed
1120 * Return value: %TRUE if the key was found and removed from the #GHashTable.
1123 g_hash_table_remove (GHashTable *hash_table,
1126 return g_hash_table_remove_internal (hash_table, key, TRUE);
1130 * g_hash_table_steal:
1131 * @hash_table: a #GHashTable.
1132 * @key: the key to remove.
1134 * Removes a key and its associated value from a #GHashTable without
1135 * calling the key and value destroy functions.
1137 * Return value: %TRUE if the key was found and removed from the #GHashTable.
1140 g_hash_table_steal (GHashTable *hash_table,
1143 return g_hash_table_remove_internal (hash_table, key, FALSE);
1147 * g_hash_table_remove_all:
1148 * @hash_table: a #GHashTable
1150 * Removes all keys and their associated values from a #GHashTable.
1152 * If the #GHashTable was created using g_hash_table_new_full(), the keys
1153 * and values are freed using the supplied destroy functions, otherwise you
1154 * have to make sure that any dynamically allocated values are freed
1160 g_hash_table_remove_all (GHashTable *hash_table)
1162 g_return_if_fail (hash_table != NULL);
1164 #ifndef G_DISABLE_ASSERT
1165 if (hash_table->nnodes != 0)
1166 hash_table->version++;
1169 g_hash_table_remove_all_nodes (hash_table, TRUE);
1170 g_hash_table_maybe_resize (hash_table);
1174 * g_hash_table_steal_all:
1175 * @hash_table: a #GHashTable.
1177 * Removes all keys and their associated values from a #GHashTable
1178 * without calling the key and value destroy functions.
1183 g_hash_table_steal_all (GHashTable *hash_table)
1185 g_return_if_fail (hash_table != NULL);
1187 #ifndef G_DISABLE_ASSERT
1188 if (hash_table->nnodes != 0)
1189 hash_table->version++;
1192 g_hash_table_remove_all_nodes (hash_table, FALSE);
1193 g_hash_table_maybe_resize (hash_table);
1197 * g_hash_table_foreach_remove_or_steal:
1198 * @hash_table: our #GHashTable
1199 * @func: the user's callback function
1200 * @user_data: data for @func
1201 * @notify: %TRUE if the destroy notify handlers are to be called
1203 * Implements the common logic for g_hash_table_foreach_remove() and
1204 * g_hash_table_foreach_steal().
1206 * Iterates over every node in the table, calling @func with the key
1207 * and value of the node (and @user_data). If @func returns %TRUE the
1208 * node is removed from the table.
1210 * If @notify is true then the destroy notify handlers will be called
1211 * for each removed node.
1214 g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
1222 for (i = 0; i < hash_table->size; i++)
1224 GHashNode *node = &hash_table->nodes [i];
1226 if (node->key_hash > 1 && (* func) (node->key, node->value, user_data))
1228 g_hash_table_remove_node (hash_table, node, notify);
1233 g_hash_table_maybe_resize (hash_table);
1235 #ifndef G_DISABLE_ASSERT
1237 hash_table->version++;
1244 * g_hash_table_foreach_remove:
1245 * @hash_table: a #GHashTable.
1246 * @func: the function to call for each key/value pair.
1247 * @user_data: user data to pass to the function.
1249 * Calls the given function for each key/value pair in the #GHashTable.
1250 * If the function returns %TRUE, then the key/value pair is removed from the
1251 * #GHashTable. If you supplied key or value destroy functions when creating
1252 * the #GHashTable, they are used to free the memory allocated for the removed
1255 * See #GHashTableIter for an alternative way to loop over the
1256 * key/value pairs in the hash table.
1258 * Return value: the number of key/value pairs removed.
1261 g_hash_table_foreach_remove (GHashTable *hash_table,
1265 g_return_val_if_fail (hash_table != NULL, 0);
1266 g_return_val_if_fail (func != NULL, 0);
1268 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
1272 * g_hash_table_foreach_steal:
1273 * @hash_table: a #GHashTable.
1274 * @func: the function to call for each key/value pair.
1275 * @user_data: user data to pass to the function.
1277 * Calls the given function for each key/value pair in the #GHashTable.
1278 * If the function returns %TRUE, then the key/value pair is removed from the
1279 * #GHashTable, but no key or value destroy functions are called.
1281 * See #GHashTableIter for an alternative way to loop over the
1282 * key/value pairs in the hash table.
1284 * Return value: the number of key/value pairs removed.
1287 g_hash_table_foreach_steal (GHashTable *hash_table,
1291 g_return_val_if_fail (hash_table != NULL, 0);
1292 g_return_val_if_fail (func != NULL, 0);
1294 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
1298 * g_hash_table_foreach:
1299 * @hash_table: a #GHashTable.
1300 * @func: the function to call for each key/value pair.
1301 * @user_data: user data to pass to the function.
1303 * Calls the given function for each of the key/value pairs in the
1304 * #GHashTable. The function is passed the key and value of each
1305 * pair, and the given @user_data parameter. The hash table may not
1306 * be modified while iterating over it (you can't add/remove
1307 * items). To remove all items matching a predicate, use
1308 * g_hash_table_foreach_remove().
1310 * See g_hash_table_find() for performance caveats for linear
1311 * order searches in contrast to g_hash_table_lookup().
1314 g_hash_table_foreach (GHashTable *hash_table,
1320 g_return_if_fail (hash_table != NULL);
1321 g_return_if_fail (func != NULL);
1323 for (i = 0; i < hash_table->size; i++)
1325 GHashNode *node = &hash_table->nodes [i];
1327 if (node->key_hash > 1)
1328 (* func) (node->key, node->value, user_data);
1333 * g_hash_table_find:
1334 * @hash_table: a #GHashTable.
1335 * @predicate: function to test the key/value pairs for a certain property.
1336 * @user_data: user data to pass to the function.
1338 * Calls the given function for key/value pairs in the #GHashTable until
1339 * @predicate returns %TRUE. The function is passed the key and value of
1340 * each pair, and the given @user_data parameter. The hash table may not
1341 * be modified while iterating over it (you can't add/remove items).
1343 * Note, that hash tables are really only optimized for forward lookups,
1344 * i.e. g_hash_table_lookup().
1345 * So code that frequently issues g_hash_table_find() or
1346 * g_hash_table_foreach() (e.g. in the order of once per every entry in a
1347 * hash table) should probably be reworked to use additional or different
1348 * data structures for reverse lookups (keep in mind that an O(n) find/foreach
1349 * operation issued for all n values in a hash table ends up needing O(n*n)
1352 * Return value: The value of the first key/value pair is returned, for which
1353 * func evaluates to %TRUE. If no pair with the requested property is found,
1354 * %NULL is returned.
1359 g_hash_table_find (GHashTable *hash_table,
1365 g_return_val_if_fail (hash_table != NULL, NULL);
1366 g_return_val_if_fail (predicate != NULL, NULL);
1368 for (i = 0; i < hash_table->size; i++)
1370 GHashNode *node = &hash_table->nodes [i];
1372 if (node->key_hash > 1 && predicate (node->key, node->value, user_data))
1380 * g_hash_table_size:
1381 * @hash_table: a #GHashTable.
1383 * Returns the number of elements contained in the #GHashTable.
1385 * Return value: the number of key/value pairs in the #GHashTable.
1388 g_hash_table_size (GHashTable *hash_table)
1390 g_return_val_if_fail (hash_table != NULL, 0);
1392 return hash_table->nnodes;
1396 * g_hash_table_get_keys:
1397 * @hash_table: a #GHashTable
1399 * Retrieves every key inside @hash_table. The returned data is valid
1400 * until @hash_table is modified.
1402 * Return value: a #GList containing all the keys inside the hash
1403 * table. The content of the list is owned by the hash table and
1404 * should not be modified or freed. Use g_list_free() when done
1410 g_hash_table_get_keys (GHashTable *hash_table)
1415 g_return_val_if_fail (hash_table != NULL, NULL);
1418 for (i = 0; i < hash_table->size; i++)
1420 GHashNode *node = &hash_table->nodes [i];
1422 if (node->key_hash > 1)
1423 retval = g_list_prepend (retval, node->key);
1430 * g_hash_table_get_values:
1431 * @hash_table: a #GHashTable
1433 * Retrieves every value inside @hash_table. The returned data is
1434 * valid until @hash_table is modified.
1436 * Return value: a #GList containing all the values inside the hash
1437 * table. The content of the list is owned by the hash table and
1438 * should not be modified or freed. Use g_list_free() when done
1444 g_hash_table_get_values (GHashTable *hash_table)
1449 g_return_val_if_fail (hash_table != NULL, NULL);
1452 for (i = 0; i < hash_table->size; i++)
1454 GHashNode *node = &hash_table->nodes [i];
1456 if (node->key_hash > 1)
1457 retval = g_list_prepend (retval, node->value);