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 * SECTION: hash_tables
40 * @short_description: associations between keys and values so that
41 * given a key the value can be found quickly
43 * A #GHashTable provides associations between keys and values which is
44 * optimized so that given a key, the associated value can be found
47 * Note that neither keys nor values are copied when inserted into the
48 * #GHashTable, so they must exist for the lifetime of the #GHashTable.
49 * This means that the use of static strings is OK, but temporary
50 * strings (i.e. those created in buffers and those returned by GTK+
51 * widgets) should be copied with g_strdup() before being inserted.
53 * If keys or values are dynamically allocated, you must be careful to
54 * ensure that they are freed when they are removed from the
55 * #GHashTable, and also when they are overwritten by new insertions
56 * into the #GHashTable. It is also not advisable to mix static strings
57 * and dynamically-allocated strings in a #GHashTable, because it then
58 * becomes difficult to determine whether the string should be freed.
60 * To create a #GHashTable, use g_hash_table_new().
62 * To insert a key and value into a #GHashTable, use
63 * g_hash_table_insert().
65 * To lookup a value corresponding to a given key, use
66 * g_hash_table_lookup() and g_hash_table_lookup_extended().
68 * To remove a key and value, use g_hash_table_remove().
70 * To call a function for each key and value pair use
71 * g_hash_table_foreach() or use a iterator to iterate over the
72 * key/value pairs in the hash table, see #GHashTableIter.
74 * To destroy a #GHashTable use g_hash_table_destroy().
80 * The #GHashTable struct is an opaque data structure to represent a
81 * <link linkend="glib-Hash-Tables">Hash Table</link>. It should only be
82 * accessed via the following functions.
88 * @Returns: the hash value corresponding to the key.
90 * Specifies the type of the hash function which is passed to
91 * g_hash_table_new() when a #GHashTable is created.
93 * The function is passed a key and should return a #guint hash value.
94 * The functions g_direct_hash(), g_int_hash() and g_str_hash() provide
95 * hash functions which can be used when the key is a #gpointer, #gint,
96 * and #gchar* respectively.
98 * <!-- FIXME: Need more here. --> The hash values should be evenly
99 * distributed over a fairly large range? The modulus is taken with the
100 * hash table size (a prime number) to find the 'bucket' to place each
101 * key into. The function should also be very fast, since it is called
102 * for each key lookup.
108 * @value: the value corresponding to the key.
109 * @user_data: user data passed to g_hash_table_foreach().
111 * Specifies the type of the function passed to g_hash_table_foreach().
112 * It is called with each key/value pair, together with the @user_data
113 * parameter which is passed to g_hash_table_foreach().
119 * @value: the value associated with the key.
120 * @user_data: user data passed to g_hash_table_remove().
121 * @Returns: %TRUE if the key/value pair should be removed from the
124 * Specifies the type of the function passed to
125 * g_hash_table_foreach_remove(). It is called with each key/value
126 * pair, together with the @user_data parameter passed to
127 * g_hash_table_foreach_remove(). It should return %TRUE if the
128 * key/value pair should be removed from the #GHashTable.
134 * @b: a value to compare with.
135 * @Returns: %TRUE if @a = @b; %FALSE otherwise.
137 * Specifies the type of a function used to test two values for
138 * equality. The function should return %TRUE if both values are equal
139 * and %FALSE otherwise.
145 * A GHashTableIter structure represents an iterator that can be used
146 * to iterate over the elements of a #GHashTable. GHashTableIter
147 * structures are typically allocated on the stack and then initialized
148 * with g_hash_table_iter_init().
151 #define HASH_TABLE_MIN_SHIFT 3 /* 1 << 3 == 8 buckets */
153 typedef struct _GHashNode GHashNode;
160 /* If key_hash == 0, node is not in use
161 * If key_hash == 1, node is a tombstone
162 * If key_hash >= 2, node contains data */
172 gint noccupied; /* nnodes + tombstones */
175 GEqualFunc key_equal_func;
176 volatile gint ref_count;
177 #ifndef G_DISABLE_ASSERT
179 * Tracks the structure of the hash table, not its contents: is only
180 * incremented when a node is added or removed (is not incremented
181 * when the key or data of a node is modified).
185 GDestroyNotify key_destroy_func;
186 GDestroyNotify value_destroy_func;
191 GHashTable *hash_table;
199 /* Each table size has an associated prime modulo (the first prime
200 * lower than the table size) used to find the initial bucket. Probing
201 * then works modulo 2^n. The prime modulo is necessary to get a
202 * good distribution with poor hash functions. */
203 static const gint prime_mod [] =
221 65521, /* For 1 << 16 */
236 2147483647 /* For 1 << 31 */
240 g_hash_table_set_shift (GHashTable *hash_table, gint shift)
245 hash_table->size = 1 << shift;
246 hash_table->mod = prime_mod [shift];
248 for (i = 0; i < shift; i++)
254 hash_table->mask = mask;
258 g_hash_table_find_closest_shift (gint n)
269 g_hash_table_set_shift_from_size (GHashTable *hash_table, gint size)
273 shift = g_hash_table_find_closest_shift (size);
274 shift = MAX (shift, HASH_TABLE_MIN_SHIFT);
276 g_hash_table_set_shift (hash_table, shift);
280 * g_hash_table_lookup_node:
281 * @hash_table: our #GHashTable
282 * @key: the key to lookup against
283 * @hash_return: optional key hash return location
284 * Return value: index of the described #GHashNode
286 * Performs a lookup in the hash table. Virtually all hash operations
287 * will use this function internally.
289 * This function first computes the hash value of the key using the
290 * user's hash function.
292 * If an entry in the table matching @key is found then this function
293 * returns the index of that entry in the table, and if not, the
294 * index of an empty node (never a tombstone).
297 g_hash_table_lookup_node (GHashTable *hash_table,
305 /* Empty buckets have hash_value set to 0, and for tombstones, it's 1.
306 * We need to make sure our hash value is not one of these. */
308 hash_value = (* hash_table->hash_func) (key);
309 if (G_UNLIKELY (hash_value <= 1))
312 node_index = hash_value % hash_table->mod;
313 node = &hash_table->nodes [node_index];
315 while (node->key_hash)
317 /* We first check if our full hash values
318 * are equal so we can avoid calling the full-blown
319 * key equality function in most cases.
322 if (node->key_hash == hash_value)
324 if (hash_table->key_equal_func)
326 if (hash_table->key_equal_func (node->key, key))
329 else if (node->key == key)
337 node_index &= hash_table->mask;
338 node = &hash_table->nodes [node_index];
345 * g_hash_table_lookup_node_for_insertion:
346 * @hash_table: our #GHashTable
347 * @key: the key to lookup against
348 * @hash_return: key hash return location
349 * Return value: index of the described #GHashNode
351 * Performs a lookup in the hash table, preserving extra information
352 * usually needed for insertion.
354 * This function first computes the hash value of the key using the
355 * user's hash function.
357 * If an entry in the table matching @key is found then this function
358 * returns the index of that entry in the table, and if not, the
359 * index of an unused node (empty or tombstone) where the key can be
362 * The computed hash value is returned in the variable pointed to
363 * by @hash_return. This is to save insertions from having to compute
364 * the hash record again for the new record.
367 g_hash_table_lookup_node_for_insertion (GHashTable *hash_table,
374 guint first_tombstone;
375 gboolean have_tombstone = FALSE;
378 /* Empty buckets have hash_value set to 0, and for tombstones, it's 1.
379 * We need to make sure our hash value is not one of these. */
381 hash_value = (* hash_table->hash_func) (key);
382 if (G_UNLIKELY (hash_value <= 1))
385 *hash_return = hash_value;
387 node_index = hash_value % hash_table->mod;
388 node = &hash_table->nodes [node_index];
390 while (node->key_hash)
392 /* We first check if our full hash values
393 * are equal so we can avoid calling the full-blown
394 * key equality function in most cases.
397 if (node->key_hash == hash_value)
399 if (hash_table->key_equal_func)
401 if (hash_table->key_equal_func (node->key, key))
404 else if (node->key == key)
409 else if (node->key_hash == 1 && !have_tombstone)
411 first_tombstone = node_index;
412 have_tombstone = TRUE;
417 node_index &= hash_table->mask;
418 node = &hash_table->nodes [node_index];
422 return first_tombstone;
428 * g_hash_table_remove_node:
429 * @hash_table: our #GHashTable
430 * @node: pointer to node to remove
431 * @notify: %TRUE if the destroy notify handlers are to be called
433 * Removes a node from the hash table and updates the node count.
434 * The node is replaced by a tombstone. No table resize is performed.
436 * If @notify is %TRUE then the destroy notify functions are called
437 * for the key and value of the hash node.
440 g_hash_table_remove_node (GHashTable *hash_table,
444 if (notify && hash_table->key_destroy_func)
445 hash_table->key_destroy_func (node->key);
447 if (notify && hash_table->value_destroy_func)
448 hash_table->value_destroy_func (node->value);
450 /* Erect tombstone */
457 hash_table->nnodes--;
461 * g_hash_table_remove_all_nodes:
462 * @hash_table: our #GHashTable
463 * @notify: %TRUE if the destroy notify handlers are to be called
465 * Removes all nodes from the table. Since this may be a precursor to
466 * freeing the table entirely, no resize is performed.
468 * If @notify is %TRUE then the destroy notify functions are called
469 * for the key and value of the hash node.
472 g_hash_table_remove_all_nodes (GHashTable *hash_table,
477 for (i = 0; i < hash_table->size; i++)
479 GHashNode *node = &hash_table->nodes [i];
481 if (node->key_hash > 1)
483 if (notify && hash_table->key_destroy_func)
484 hash_table->key_destroy_func (node->key);
486 if (notify && hash_table->value_destroy_func)
487 hash_table->value_destroy_func (node->value);
491 /* We need to set node->key_hash = 0 for all nodes - might as well be GC
492 * friendly and clear everything */
493 memset (hash_table->nodes, 0, hash_table->size * sizeof (GHashNode));
495 hash_table->nnodes = 0;
496 hash_table->noccupied = 0;
500 * g_hash_table_resize:
501 * @hash_table: our #GHashTable
503 * Resizes the hash table to the optimal size based on the number of
504 * nodes currently held. If you call this function then a resize will
505 * occur, even if one does not need to occur. Use
506 * g_hash_table_maybe_resize() instead.
508 * This function may "resize" the hash table to its current size, with
509 * the side effect of cleaning up tombstones and otherwise optimizing
510 * the probe sequences.
513 g_hash_table_resize (GHashTable *hash_table)
515 GHashNode *new_nodes;
519 old_size = hash_table->size;
520 g_hash_table_set_shift_from_size (hash_table, hash_table->nnodes * 2);
522 new_nodes = g_new0 (GHashNode, hash_table->size);
524 for (i = 0; i < old_size; i++)
526 GHashNode *node = &hash_table->nodes [i];
531 if (node->key_hash <= 1)
534 hash_val = node->key_hash % hash_table->mod;
535 new_node = &new_nodes [hash_val];
537 while (new_node->key_hash)
541 hash_val &= hash_table->mask;
542 new_node = &new_nodes [hash_val];
548 g_free (hash_table->nodes);
549 hash_table->nodes = new_nodes;
550 hash_table->noccupied = hash_table->nnodes;
554 * g_hash_table_maybe_resize:
555 * @hash_table: our #GHashTable
557 * Resizes the hash table, if needed.
559 * Essentially, calls g_hash_table_resize() if the table has strayed
560 * too far from its ideal size for its number of nodes.
563 g_hash_table_maybe_resize (GHashTable *hash_table)
565 gint noccupied = hash_table->noccupied;
566 gint size = hash_table->size;
568 if ((size > hash_table->nnodes * 4 && size > 1 << HASH_TABLE_MIN_SHIFT) ||
569 (size <= noccupied + (noccupied / 16)))
570 g_hash_table_resize (hash_table);
575 * @hash_func: a function to create a hash value from a key.
576 * Hash values are used to determine where keys are stored within the
577 * #GHashTable data structure. The g_direct_hash(), g_int_hash(),
578 * g_int64_hash(), g_double_hash() and g_str_hash() functions are provided
579 * for some common types of keys.
580 * If hash_func is %NULL, g_direct_hash() is used.
581 * @key_equal_func: a function to check two keys for equality. This is
582 * used when looking up keys in the #GHashTable. The g_direct_equal(),
583 * g_int_equal(), g_int64_equal(), g_double_equal() and g_str_equal()
584 * functions are provided for the most common types of keys.
585 * If @key_equal_func is %NULL, keys are compared directly in a similar
586 * fashion to g_direct_equal(), but without the overhead of a function call.
588 * Creates a new #GHashTable with a reference count of 1.
590 * Return value: a new #GHashTable.
593 g_hash_table_new (GHashFunc hash_func,
594 GEqualFunc key_equal_func)
596 return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
601 * g_hash_table_new_full:
602 * @hash_func: a function to create a hash value from a key.
603 * @key_equal_func: a function to check two keys for equality.
604 * @key_destroy_func: a function to free the memory allocated for the key
605 * used when removing the entry from the #GHashTable or %NULL if you
606 * don't want to supply such a function.
607 * @value_destroy_func: a function to free the memory allocated for the
608 * value used when removing the entry from the #GHashTable or %NULL if
609 * you don't want to supply such a function.
611 * Creates a new #GHashTable like g_hash_table_new() with a reference count
612 * of 1 and allows to specify functions to free the memory allocated for the
613 * key and value that get called when removing the entry from the #GHashTable.
615 * Return value: a new #GHashTable.
618 g_hash_table_new_full (GHashFunc hash_func,
619 GEqualFunc key_equal_func,
620 GDestroyNotify key_destroy_func,
621 GDestroyNotify value_destroy_func)
623 GHashTable *hash_table;
625 hash_table = g_slice_new (GHashTable);
626 g_hash_table_set_shift (hash_table, HASH_TABLE_MIN_SHIFT);
627 hash_table->nnodes = 0;
628 hash_table->noccupied = 0;
629 hash_table->hash_func = hash_func ? hash_func : g_direct_hash;
630 hash_table->key_equal_func = key_equal_func;
631 hash_table->ref_count = 1;
632 #ifndef G_DISABLE_ASSERT
633 hash_table->version = 0;
635 hash_table->key_destroy_func = key_destroy_func;
636 hash_table->value_destroy_func = value_destroy_func;
637 hash_table->nodes = g_new0 (GHashNode, hash_table->size);
643 * g_hash_table_iter_init:
644 * @iter: an uninitialized #GHashTableIter.
645 * @hash_table: a #GHashTable.
647 * Initializes a key/value pair iterator and associates it with
648 * @hash_table. Modifying the hash table after calling this function
649 * invalidates the returned iterator.
651 * GHashTableIter iter;
652 * gpointer key, value;
654 * g_hash_table_iter_init (&iter, hash_table);
655 * while (g_hash_table_iter_next (&iter, &key, &value))
657 * /* do something with key and value */
664 g_hash_table_iter_init (GHashTableIter *iter,
665 GHashTable *hash_table)
667 RealIter *ri = (RealIter *) iter;
669 g_return_if_fail (iter != NULL);
670 g_return_if_fail (hash_table != NULL);
672 ri->hash_table = hash_table;
674 #ifndef G_DISABLE_ASSERT
675 ri->version = hash_table->version;
680 * g_hash_table_iter_next:
681 * @iter: an initialized #GHashTableIter.
682 * @key: a location to store the key, or %NULL.
683 * @value: a location to store the value, or %NULL.
685 * Advances @iter and retrieves the key and/or value that are now
686 * pointed to as a result of this advancement. If %FALSE is returned,
687 * @key and @value are not set, and the iterator becomes invalid.
689 * Return value: %FALSE if the end of the #GHashTable has been reached.
694 g_hash_table_iter_next (GHashTableIter *iter,
698 RealIter *ri = (RealIter *) iter;
702 g_return_val_if_fail (iter != NULL, FALSE);
703 #ifndef G_DISABLE_ASSERT
704 g_return_val_if_fail (ri->version == ri->hash_table->version, FALSE);
706 g_return_val_if_fail (ri->position < ri->hash_table->size, FALSE);
708 position = ri->position;
713 if (position >= ri->hash_table->size)
715 ri->position = position;
719 node = &ri->hash_table->nodes [position];
721 while (node->key_hash <= 1);
726 *value = node->value;
728 ri->position = position;
733 * g_hash_table_iter_get_hash_table:
734 * @iter: an initialized #GHashTableIter.
736 * Returns the #GHashTable associated with @iter.
738 * Return value: the #GHashTable associated with @iter.
743 g_hash_table_iter_get_hash_table (GHashTableIter *iter)
745 g_return_val_if_fail (iter != NULL, NULL);
747 return ((RealIter *) iter)->hash_table;
751 iter_remove_or_steal (RealIter *ri, gboolean notify)
753 g_return_if_fail (ri != NULL);
754 #ifndef G_DISABLE_ASSERT
755 g_return_if_fail (ri->version == ri->hash_table->version);
757 g_return_if_fail (ri->position >= 0);
758 g_return_if_fail (ri->position < ri->hash_table->size);
760 g_hash_table_remove_node (ri->hash_table, &ri->hash_table->nodes [ri->position], notify);
762 #ifndef G_DISABLE_ASSERT
764 ri->hash_table->version++;
769 * g_hash_table_iter_remove:
770 * @iter: an initialized #GHashTableIter.
772 * Removes the key/value pair currently pointed to by the iterator
773 * from its associated #GHashTable. Can only be called after
774 * g_hash_table_iter_next() returned %TRUE, and cannot be called more
775 * than once for the same key/value pair.
777 * If the #GHashTable was created using g_hash_table_new_full(), the
778 * key and value are freed using the supplied destroy functions, otherwise
779 * you have to make sure that any dynamically allocated values are freed
785 g_hash_table_iter_remove (GHashTableIter *iter)
787 iter_remove_or_steal ((RealIter *) iter, TRUE);
791 * g_hash_table_iter_steal:
792 * @iter: an initialized #GHashTableIter.
794 * Removes the key/value pair currently pointed to by the iterator
795 * from its associated #GHashTable, without calling the key and value
796 * destroy functions. Can only be called after
797 * g_hash_table_iter_next() returned %TRUE, and cannot be called more
798 * than once for the same key/value pair.
803 g_hash_table_iter_steal (GHashTableIter *iter)
805 iter_remove_or_steal ((RealIter *) iter, FALSE);
811 * @hash_table: a valid #GHashTable.
813 * Atomically increments the reference count of @hash_table by one.
814 * This function is MT-safe and may be called from any thread.
816 * Return value: the passed in #GHashTable.
821 g_hash_table_ref (GHashTable *hash_table)
823 g_return_val_if_fail (hash_table != NULL, NULL);
824 g_return_val_if_fail (hash_table->ref_count > 0, hash_table);
826 g_atomic_int_add (&hash_table->ref_count, 1);
831 * g_hash_table_unref:
832 * @hash_table: a valid #GHashTable.
834 * Atomically decrements the reference count of @hash_table by one.
835 * If the reference count drops to 0, all keys and values will be
836 * destroyed, and all memory allocated by the hash table is released.
837 * This function is MT-safe and may be called from any thread.
842 g_hash_table_unref (GHashTable *hash_table)
844 g_return_if_fail (hash_table != NULL);
845 g_return_if_fail (hash_table->ref_count > 0);
847 if (g_atomic_int_exchange_and_add (&hash_table->ref_count, -1) - 1 == 0)
849 g_hash_table_remove_all_nodes (hash_table, TRUE);
850 g_free (hash_table->nodes);
851 g_slice_free (GHashTable, hash_table);
856 * g_hash_table_destroy:
857 * @hash_table: a #GHashTable.
859 * Destroys all keys and values in the #GHashTable and decrements its
860 * reference count by 1. If keys and/or values are dynamically allocated,
861 * you should either free them first or create the #GHashTable with destroy
862 * notifiers using g_hash_table_new_full(). In the latter case the destroy
863 * functions you supplied will be called on all keys and values during the
867 g_hash_table_destroy (GHashTable *hash_table)
869 g_return_if_fail (hash_table != NULL);
870 g_return_if_fail (hash_table->ref_count > 0);
872 g_hash_table_remove_all (hash_table);
873 g_hash_table_unref (hash_table);
877 * g_hash_table_lookup:
878 * @hash_table: a #GHashTable.
879 * @key: the key to look up.
881 * Looks up a key in a #GHashTable. Note that this function cannot
882 * distinguish between a key that is not present and one which is present
883 * and has the value %NULL. If you need this distinction, use
884 * g_hash_table_lookup_extended().
886 * Return value: the associated value, or %NULL if the key is not found.
889 g_hash_table_lookup (GHashTable *hash_table,
895 g_return_val_if_fail (hash_table != NULL, NULL);
897 node_index = g_hash_table_lookup_node (hash_table, key);
898 node = &hash_table->nodes [node_index];
900 return node->key_hash ? node->value : NULL;
904 * g_hash_table_lookup_extended:
905 * @hash_table: a #GHashTable
906 * @lookup_key: the key to look up
907 * @orig_key: return location for the original key, or %NULL
908 * @value: return location for the value associated with the key, or %NULL
910 * Looks up a key in the #GHashTable, returning the original key and the
911 * associated value and a #gboolean which is %TRUE if the key was found. This
912 * is useful if you need to free the memory allocated for the original key,
913 * for example before calling g_hash_table_remove().
915 * You can actually pass %NULL for @lookup_key to test
916 * whether the %NULL key exists.
918 * Return value: %TRUE if the key was found in the #GHashTable.
921 g_hash_table_lookup_extended (GHashTable *hash_table,
922 gconstpointer lookup_key,
929 g_return_val_if_fail (hash_table != NULL, FALSE);
931 node_index = g_hash_table_lookup_node (hash_table, lookup_key);
932 node = &hash_table->nodes [node_index];
938 *orig_key = node->key;
941 *value = node->value;
947 * g_hash_table_insert_internal:
948 * @hash_table: our #GHashTable
949 * @key: the key to insert
950 * @value: the value to insert
951 * @keep_new_key: if %TRUE and this key already exists in the table
952 * then call the destroy notify function on the old key. If %FALSE
953 * then call the destroy notify function on the new key.
955 * Implements the common logic for the g_hash_table_insert() and
956 * g_hash_table_replace() functions.
958 * Do a lookup of @key. If it is found, replace it with the new
959 * @value (and perhaps the new @key). If it is not found, create a
963 g_hash_table_insert_internal (GHashTable *hash_table,
966 gboolean keep_new_key)
973 g_return_if_fail (hash_table != NULL);
974 g_return_if_fail (hash_table->ref_count > 0);
976 node_index = g_hash_table_lookup_node_for_insertion (hash_table, key, &key_hash);
977 node = &hash_table->nodes [node_index];
979 old_hash = node->key_hash;
985 if (hash_table->key_destroy_func)
986 hash_table->key_destroy_func (node->key);
991 if (hash_table->key_destroy_func)
992 hash_table->key_destroy_func (key);
995 if (hash_table->value_destroy_func)
996 hash_table->value_destroy_func (node->value);
1003 node->value = value;
1004 node->key_hash = key_hash;
1006 hash_table->nnodes++;
1010 /* We replaced an empty node, and not a tombstone */
1011 hash_table->noccupied++;
1012 g_hash_table_maybe_resize (hash_table);
1015 #ifndef G_DISABLE_ASSERT
1016 hash_table->version++;
1022 * g_hash_table_insert:
1023 * @hash_table: a #GHashTable.
1024 * @key: a key to insert.
1025 * @value: the value to associate with the key.
1027 * Inserts a new key and value into a #GHashTable.
1029 * If the key already exists in the #GHashTable its current value is replaced
1030 * with the new value. If you supplied a @value_destroy_func when creating the
1031 * #GHashTable, the old value is freed using that function. If you supplied
1032 * a @key_destroy_func when creating the #GHashTable, the passed key is freed
1033 * using that function.
1036 g_hash_table_insert (GHashTable *hash_table,
1040 g_hash_table_insert_internal (hash_table, key, value, FALSE);
1044 * g_hash_table_replace:
1045 * @hash_table: a #GHashTable.
1046 * @key: a key to insert.
1047 * @value: the value to associate with the key.
1049 * Inserts a new key and value into a #GHashTable similar to
1050 * g_hash_table_insert(). The difference is that if the key already exists
1051 * in the #GHashTable, it gets replaced by the new key. If you supplied a
1052 * @value_destroy_func when creating the #GHashTable, the old value is freed
1053 * using that function. If you supplied a @key_destroy_func when creating the
1054 * #GHashTable, the old key is freed using that function.
1057 g_hash_table_replace (GHashTable *hash_table,
1061 g_hash_table_insert_internal (hash_table, key, value, TRUE);
1065 * g_hash_table_remove_internal:
1066 * @hash_table: our #GHashTable
1067 * @key: the key to remove
1068 * @notify: %TRUE if the destroy notify handlers are to be called
1069 * Return value: %TRUE if a node was found and removed, else %FALSE
1071 * Implements the common logic for the g_hash_table_remove() and
1072 * g_hash_table_steal() functions.
1074 * Do a lookup of @key and remove it if it is found, calling the
1075 * destroy notify handlers only if @notify is %TRUE.
1078 g_hash_table_remove_internal (GHashTable *hash_table,
1085 g_return_val_if_fail (hash_table != NULL, FALSE);
1087 node_index = g_hash_table_lookup_node (hash_table, key);
1088 node = &hash_table->nodes [node_index];
1090 /* g_hash_table_lookup_node() never returns a tombstone, so this is safe */
1091 if (!node->key_hash)
1094 g_hash_table_remove_node (hash_table, node, notify);
1095 g_hash_table_maybe_resize (hash_table);
1097 #ifndef G_DISABLE_ASSERT
1098 hash_table->version++;
1105 * g_hash_table_remove:
1106 * @hash_table: a #GHashTable.
1107 * @key: the key to remove.
1109 * Removes a key and its associated value from a #GHashTable.
1111 * If the #GHashTable was created using g_hash_table_new_full(), the
1112 * key and value are freed using the supplied destroy functions, otherwise
1113 * you have to make sure that any dynamically allocated values are freed
1116 * Return value: %TRUE if the key was found and removed from the #GHashTable.
1119 g_hash_table_remove (GHashTable *hash_table,
1122 return g_hash_table_remove_internal (hash_table, key, TRUE);
1126 * g_hash_table_steal:
1127 * @hash_table: a #GHashTable.
1128 * @key: the key to remove.
1130 * Removes a key and its associated value from a #GHashTable without
1131 * calling the key and value destroy functions.
1133 * Return value: %TRUE if the key was found and removed from the #GHashTable.
1136 g_hash_table_steal (GHashTable *hash_table,
1139 return g_hash_table_remove_internal (hash_table, key, FALSE);
1143 * g_hash_table_remove_all:
1144 * @hash_table: a #GHashTable
1146 * Removes all keys and their associated values from a #GHashTable.
1148 * If the #GHashTable was created using g_hash_table_new_full(), the keys
1149 * and values are freed using the supplied destroy functions, otherwise you
1150 * have to make sure that any dynamically allocated values are freed
1156 g_hash_table_remove_all (GHashTable *hash_table)
1158 g_return_if_fail (hash_table != NULL);
1160 #ifndef G_DISABLE_ASSERT
1161 if (hash_table->nnodes != 0)
1162 hash_table->version++;
1165 g_hash_table_remove_all_nodes (hash_table, TRUE);
1166 g_hash_table_maybe_resize (hash_table);
1170 * g_hash_table_steal_all:
1171 * @hash_table: a #GHashTable.
1173 * Removes all keys and their associated values from a #GHashTable
1174 * without calling the key and value destroy functions.
1179 g_hash_table_steal_all (GHashTable *hash_table)
1181 g_return_if_fail (hash_table != NULL);
1183 #ifndef G_DISABLE_ASSERT
1184 if (hash_table->nnodes != 0)
1185 hash_table->version++;
1188 g_hash_table_remove_all_nodes (hash_table, FALSE);
1189 g_hash_table_maybe_resize (hash_table);
1193 * g_hash_table_foreach_remove_or_steal:
1194 * @hash_table: our #GHashTable
1195 * @func: the user's callback function
1196 * @user_data: data for @func
1197 * @notify: %TRUE if the destroy notify handlers are to be called
1199 * Implements the common logic for g_hash_table_foreach_remove() and
1200 * g_hash_table_foreach_steal().
1202 * Iterates over every node in the table, calling @func with the key
1203 * and value of the node (and @user_data). If @func returns %TRUE the
1204 * node is removed from the table.
1206 * If @notify is true then the destroy notify handlers will be called
1207 * for each removed node.
1210 g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
1218 for (i = 0; i < hash_table->size; i++)
1220 GHashNode *node = &hash_table->nodes [i];
1222 if (node->key_hash > 1 && (* func) (node->key, node->value, user_data))
1224 g_hash_table_remove_node (hash_table, node, notify);
1229 g_hash_table_maybe_resize (hash_table);
1231 #ifndef G_DISABLE_ASSERT
1233 hash_table->version++;
1240 * g_hash_table_foreach_remove:
1241 * @hash_table: a #GHashTable.
1242 * @func: the function to call for each key/value pair.
1243 * @user_data: user data to pass to the function.
1245 * Calls the given function for each key/value pair in the #GHashTable.
1246 * If the function returns %TRUE, then the key/value pair is removed from the
1247 * #GHashTable. If you supplied key or value destroy functions when creating
1248 * the #GHashTable, they are used to free the memory allocated for the removed
1251 * See #GHashTableIter for an alternative way to loop over the
1252 * key/value pairs in the hash table.
1254 * Return value: the number of key/value pairs removed.
1257 g_hash_table_foreach_remove (GHashTable *hash_table,
1261 g_return_val_if_fail (hash_table != NULL, 0);
1262 g_return_val_if_fail (func != NULL, 0);
1264 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
1268 * g_hash_table_foreach_steal:
1269 * @hash_table: a #GHashTable.
1270 * @func: the function to call for each key/value pair.
1271 * @user_data: user data to pass to the function.
1273 * Calls the given function for each key/value pair in the #GHashTable.
1274 * If the function returns %TRUE, then the key/value pair is removed from the
1275 * #GHashTable, but no key or value destroy functions are called.
1277 * See #GHashTableIter for an alternative way to loop over the
1278 * key/value pairs in the hash table.
1280 * Return value: the number of key/value pairs removed.
1283 g_hash_table_foreach_steal (GHashTable *hash_table,
1287 g_return_val_if_fail (hash_table != NULL, 0);
1288 g_return_val_if_fail (func != NULL, 0);
1290 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
1294 * g_hash_table_foreach:
1295 * @hash_table: a #GHashTable.
1296 * @func: the function to call for each key/value pair.
1297 * @user_data: user data to pass to the function.
1299 * Calls the given function for each of the key/value pairs in the
1300 * #GHashTable. The function is passed the key and value of each
1301 * pair, and the given @user_data parameter. The hash table may not
1302 * be modified while iterating over it (you can't add/remove
1303 * items). To remove all items matching a predicate, use
1304 * g_hash_table_foreach_remove().
1306 * See g_hash_table_find() for performance caveats for linear
1307 * order searches in contrast to g_hash_table_lookup().
1310 g_hash_table_foreach (GHashTable *hash_table,
1316 g_return_if_fail (hash_table != NULL);
1317 g_return_if_fail (func != NULL);
1319 for (i = 0; i < hash_table->size; i++)
1321 GHashNode *node = &hash_table->nodes [i];
1323 if (node->key_hash > 1)
1324 (* func) (node->key, node->value, user_data);
1329 * g_hash_table_find:
1330 * @hash_table: a #GHashTable.
1331 * @predicate: function to test the key/value pairs for a certain property.
1332 * @user_data: user data to pass to the function.
1334 * Calls the given function for key/value pairs in the #GHashTable until
1335 * @predicate returns %TRUE. The function is passed the key and value of
1336 * each pair, and the given @user_data parameter. The hash table may not
1337 * be modified while iterating over it (you can't add/remove items).
1339 * Note, that hash tables are really only optimized for forward lookups,
1340 * i.e. g_hash_table_lookup().
1341 * So code that frequently issues g_hash_table_find() or
1342 * g_hash_table_foreach() (e.g. in the order of once per every entry in a
1343 * hash table) should probably be reworked to use additional or different
1344 * data structures for reverse lookups (keep in mind that an O(n) find/foreach
1345 * operation issued for all n values in a hash table ends up needing O(n*n)
1348 * Return value: The value of the first key/value pair is returned, for which
1349 * func evaluates to %TRUE. If no pair with the requested property is found,
1350 * %NULL is returned.
1355 g_hash_table_find (GHashTable *hash_table,
1361 g_return_val_if_fail (hash_table != NULL, NULL);
1362 g_return_val_if_fail (predicate != NULL, NULL);
1364 for (i = 0; i < hash_table->size; i++)
1366 GHashNode *node = &hash_table->nodes [i];
1368 if (node->key_hash > 1 && predicate (node->key, node->value, user_data))
1376 * g_hash_table_size:
1377 * @hash_table: a #GHashTable.
1379 * Returns the number of elements contained in the #GHashTable.
1381 * Return value: the number of key/value pairs in the #GHashTable.
1384 g_hash_table_size (GHashTable *hash_table)
1386 g_return_val_if_fail (hash_table != NULL, 0);
1388 return hash_table->nnodes;
1392 * g_hash_table_get_keys:
1393 * @hash_table: a #GHashTable
1395 * Retrieves every key inside @hash_table. The returned data is valid
1396 * until @hash_table is modified.
1398 * Return value: a #GList containing all the keys inside the hash
1399 * table. The content of the list is owned by the hash table and
1400 * should not be modified or freed. Use g_list_free() when done
1406 g_hash_table_get_keys (GHashTable *hash_table)
1411 g_return_val_if_fail (hash_table != NULL, NULL);
1414 for (i = 0; i < hash_table->size; i++)
1416 GHashNode *node = &hash_table->nodes [i];
1418 if (node->key_hash > 1)
1419 retval = g_list_prepend (retval, node->key);
1426 * g_hash_table_get_values:
1427 * @hash_table: a #GHashTable
1429 * Retrieves every value inside @hash_table. The returned data is
1430 * valid until @hash_table is modified.
1432 * Return value: a #GList containing all the values inside the hash
1433 * table. The content of the list is owned by the hash table and
1434 * should not be modified or freed. Use g_list_free() when done
1440 g_hash_table_get_values (GHashTable *hash_table)
1445 g_return_val_if_fail (hash_table != NULL, NULL);
1448 for (i = 0; i < hash_table->size; i++)
1450 GHashNode *node = &hash_table->nodes [i];
1452 if (node->key_hash > 1)
1453 retval = g_list_prepend (retval, node->value);