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 #define HASH_IS_UNUSED(h_) ((h_) == 0)
158 #define HASH_IS_TOOMBSTONE(h_) ((h_) == 1)
159 #define HASH_IS_REAL(h_) ((h_) >= 2)
161 typedef struct _GHashNode GHashNode;
168 /* If key_hash == 0, node is not in use
169 * If key_hash == 1, node is a tombstone
170 * If key_hash >= 2, node contains data */
180 gint noccupied; /* nnodes + tombstones */
183 GEqualFunc key_equal_func;
184 volatile gint ref_count;
185 #ifndef G_DISABLE_ASSERT
187 * Tracks the structure of the hash table, not its contents: is only
188 * incremented when a node is added or removed (is not incremented
189 * when the key or data of a node is modified).
193 GDestroyNotify key_destroy_func;
194 GDestroyNotify value_destroy_func;
199 GHashTable *hash_table;
207 /* Each table size has an associated prime modulo (the first prime
208 * lower than the table size) used to find the initial bucket. Probing
209 * then works modulo 2^n. The prime modulo is necessary to get a
210 * good distribution with poor hash functions. */
211 static const gint prime_mod [] =
229 65521, /* For 1 << 16 */
244 2147483647 /* For 1 << 31 */
248 g_hash_table_set_shift (GHashTable *hash_table, gint shift)
253 hash_table->size = 1 << shift;
254 hash_table->mod = prime_mod [shift];
256 for (i = 0; i < shift; i++)
262 hash_table->mask = mask;
266 g_hash_table_find_closest_shift (gint n)
277 g_hash_table_set_shift_from_size (GHashTable *hash_table, gint size)
281 shift = g_hash_table_find_closest_shift (size);
282 shift = MAX (shift, HASH_TABLE_MIN_SHIFT);
284 g_hash_table_set_shift (hash_table, shift);
288 * g_hash_table_lookup_node:
289 * @hash_table: our #GHashTable
290 * @key: the key to lookup against (may be %NULL)
291 * @hash_return: optional key hash return location
292 * Return value: index of the described #GHashNode
294 * Performs a lookup in the hash table.
296 * Virtually all hash operations will use this function internally.
298 * This function first computes the hash value of the key using the
299 * user's hash function.
301 * If an entry in the table matching @key is found then this function
302 * returns the index of that entry in the table, and if not, the
303 * index of an empty node (never a tombstone).
306 g_hash_table_lookup_node (GHashTable *hash_table,
314 hash_value = (* hash_table->hash_func) (key);
315 if (G_UNLIKELY (!HASH_IS_REAL (hash_value)))
318 node_index = hash_value % hash_table->mod;
319 node = &hash_table->nodes [node_index];
321 while (!HASH_IS_UNUSED (node->key_hash))
323 /* We first check if our full hash values
324 * are equal so we can avoid calling the full-blown
325 * key equality function in most cases.
328 if (node->key_hash == hash_value)
330 if (hash_table->key_equal_func)
332 if (hash_table->key_equal_func (node->key, key))
335 else if (node->key == key)
343 node_index &= hash_table->mask;
344 node = &hash_table->nodes [node_index];
351 * g_hash_table_lookup_node_for_insertion:
352 * @hash_table: our #GHashTable
353 * @key: the key to lookup against
354 * @hash_return: key hash return location
355 * Return value: index of the described #GHashNode
357 * Performs a lookup in the hash table, preserving extra information
358 * usually needed for insertion.
360 * This function first computes the hash value of the key using the
361 * user's hash function.
363 * If an entry in the table matching @key is found then this function
364 * returns the index of that entry in the table, and if not, the
365 * index of an unused node (empty or tombstone) where the key can be
368 * The computed hash value is returned in the variable pointed to
369 * by @hash_return. This is to save insertions from having to compute
370 * the hash record again for the new record.
373 g_hash_table_lookup_node_for_insertion (GHashTable *hash_table,
380 guint first_tombstone;
381 gboolean have_tombstone = FALSE;
384 hash_value = (* hash_table->hash_func) (key);
385 if (G_UNLIKELY (!HASH_IS_REAL (hash_value)))
388 *hash_return = hash_value;
390 node_index = hash_value % hash_table->mod;
391 node = &hash_table->nodes [node_index];
393 while (!HASH_IS_UNUSED (node->key_hash))
395 /* We first check if our full hash values
396 * are equal so we can avoid calling the full-blown
397 * key equality function in most cases.
400 if (node->key_hash == hash_value)
402 if (hash_table->key_equal_func)
404 if (hash_table->key_equal_func (node->key, key))
407 else if (node->key == key)
412 else if (HASH_IS_TOOMBSTONE (node->key_hash) && !have_tombstone)
414 first_tombstone = node_index;
415 have_tombstone = TRUE;
420 node_index &= hash_table->mask;
421 node = &hash_table->nodes [node_index];
425 return first_tombstone;
431 * g_hash_table_remove_node:
432 * @hash_table: our #GHashTable
433 * @node: pointer to node to remove
434 * @notify: %TRUE if the destroy notify handlers are to be called
436 * Removes a node from the hash table and updates the node count.
437 * The node is replaced by a tombstone. No table resize is performed.
439 * If @notify is %TRUE then the destroy notify functions are called
440 * for the key and value of the hash node.
443 g_hash_table_remove_node (GHashTable *hash_table,
447 if (notify && hash_table->key_destroy_func)
448 hash_table->key_destroy_func (node->key);
450 if (notify && hash_table->value_destroy_func)
451 hash_table->value_destroy_func (node->value);
453 /* Erect tombstone */
460 hash_table->nnodes--;
464 * g_hash_table_remove_all_nodes:
465 * @hash_table: our #GHashTable
466 * @notify: %TRUE if the destroy notify handlers are to be called
468 * Removes all nodes from the table. Since this may be a precursor to
469 * freeing the table entirely, no resize is performed.
471 * If @notify is %TRUE then the destroy notify functions are called
472 * for the key and value of the hash node.
475 g_hash_table_remove_all_nodes (GHashTable *hash_table,
481 (hash_table->key_destroy_func != NULL ||
482 hash_table->value_destroy_func != NULL))
484 for (i = 0; i < hash_table->size; i++)
486 GHashNode *node = &hash_table->nodes [i];
488 if (HASH_IS_REAL (node->key_hash))
490 if (hash_table->key_destroy_func != NULL)
491 hash_table->key_destroy_func (node->key);
493 if (hash_table->value_destroy_func != NULL)
494 hash_table->value_destroy_func (node->value);
499 /* We need to set node->key_hash = 0 for all nodes - might as well be GC
500 * friendly and clear everything
502 memset (hash_table->nodes, 0, hash_table->size * sizeof (GHashNode));
504 hash_table->nnodes = 0;
505 hash_table->noccupied = 0;
509 * g_hash_table_resize:
510 * @hash_table: our #GHashTable
512 * Resizes the hash table to the optimal size based on the number of
513 * nodes currently held. If you call this function then a resize will
514 * occur, even if one does not need to occur. Use
515 * g_hash_table_maybe_resize() instead.
517 * This function may "resize" the hash table to its current size, with
518 * the side effect of cleaning up tombstones and otherwise optimizing
519 * the probe sequences.
522 g_hash_table_resize (GHashTable *hash_table)
524 GHashNode *new_nodes;
528 old_size = hash_table->size;
529 g_hash_table_set_shift_from_size (hash_table, hash_table->nnodes * 2);
531 new_nodes = g_new0 (GHashNode, hash_table->size);
533 for (i = 0; i < old_size; i++)
535 GHashNode *node = &hash_table->nodes [i];
540 if (!HASH_IS_REAL (node->key_hash))
543 hash_val = node->key_hash % hash_table->mod;
544 new_node = &new_nodes [hash_val];
546 while (!HASH_IS_UNUSED (new_node->key_hash))
550 hash_val &= hash_table->mask;
551 new_node = &new_nodes [hash_val];
557 g_free (hash_table->nodes);
558 hash_table->nodes = new_nodes;
559 hash_table->noccupied = hash_table->nnodes;
563 * g_hash_table_maybe_resize:
564 * @hash_table: our #GHashTable
566 * Resizes the hash table, if needed.
568 * Essentially, calls g_hash_table_resize() if the table has strayed
569 * too far from its ideal size for its number of nodes.
572 g_hash_table_maybe_resize (GHashTable *hash_table)
574 gint noccupied = hash_table->noccupied;
575 gint size = hash_table->size;
577 if ((size > hash_table->nnodes * 4 && size > 1 << HASH_TABLE_MIN_SHIFT) ||
578 (size <= noccupied + (noccupied / 16)))
579 g_hash_table_resize (hash_table);
584 * @hash_func: a function to create a hash value from a key.
585 * Hash values are used to determine where keys are stored within the
586 * #GHashTable data structure. The g_direct_hash(), g_int_hash(),
587 * g_int64_hash(), g_double_hash() and g_str_hash() functions are provided
588 * for some common types of keys.
589 * If hash_func is %NULL, g_direct_hash() is used.
590 * @key_equal_func: a function to check two keys for equality. This is
591 * used when looking up keys in the #GHashTable. The g_direct_equal(),
592 * g_int_equal(), g_int64_equal(), g_double_equal() and g_str_equal()
593 * functions are provided for the most common types of keys.
594 * If @key_equal_func is %NULL, keys are compared directly in a similar
595 * fashion to g_direct_equal(), but without the overhead of a function call.
597 * Creates a new #GHashTable with a reference count of 1.
599 * Return value: a new #GHashTable.
602 g_hash_table_new (GHashFunc hash_func,
603 GEqualFunc key_equal_func)
605 return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
610 * g_hash_table_new_full:
611 * @hash_func: a function to create a hash value from a key.
612 * @key_equal_func: a function to check two keys for equality.
613 * @key_destroy_func: a function to free the memory allocated for the key
614 * used when removing the entry from the #GHashTable or %NULL if you
615 * don't want to supply such a function.
616 * @value_destroy_func: a function to free the memory allocated for the
617 * value used when removing the entry from the #GHashTable or %NULL if
618 * you don't want to supply such a function.
620 * Creates a new #GHashTable like g_hash_table_new() with a reference count
621 * of 1 and allows to specify functions to free the memory allocated for the
622 * key and value that get called when removing the entry from the #GHashTable.
624 * Return value: a new #GHashTable.
627 g_hash_table_new_full (GHashFunc hash_func,
628 GEqualFunc key_equal_func,
629 GDestroyNotify key_destroy_func,
630 GDestroyNotify value_destroy_func)
632 GHashTable *hash_table;
634 hash_table = g_slice_new (GHashTable);
635 g_hash_table_set_shift (hash_table, HASH_TABLE_MIN_SHIFT);
636 hash_table->nnodes = 0;
637 hash_table->noccupied = 0;
638 hash_table->hash_func = hash_func ? hash_func : g_direct_hash;
639 hash_table->key_equal_func = key_equal_func;
640 hash_table->ref_count = 1;
641 #ifndef G_DISABLE_ASSERT
642 hash_table->version = 0;
644 hash_table->key_destroy_func = key_destroy_func;
645 hash_table->value_destroy_func = value_destroy_func;
646 hash_table->nodes = g_new0 (GHashNode, hash_table->size);
652 * g_hash_table_iter_init:
653 * @iter: an uninitialized #GHashTableIter.
654 * @hash_table: a #GHashTable.
656 * Initializes a key/value pair iterator and associates it with
657 * @hash_table. Modifying the hash table after calling this function
658 * invalidates the returned iterator.
660 * GHashTableIter iter;
661 * gpointer key, value;
663 * g_hash_table_iter_init (&iter, hash_table);
664 * while (g_hash_table_iter_next (&iter, &key, &value))
666 * /* do something with key and value */
673 g_hash_table_iter_init (GHashTableIter *iter,
674 GHashTable *hash_table)
676 RealIter *ri = (RealIter *) iter;
678 g_return_if_fail (iter != NULL);
679 g_return_if_fail (hash_table != NULL);
681 ri->hash_table = hash_table;
683 #ifndef G_DISABLE_ASSERT
684 ri->version = hash_table->version;
689 * g_hash_table_iter_next:
690 * @iter: an initialized #GHashTableIter.
691 * @key: a location to store the key, or %NULL.
692 * @value: a location to store the value, or %NULL.
694 * Advances @iter and retrieves the key and/or value that are now
695 * pointed to as a result of this advancement. If %FALSE is returned,
696 * @key and @value are not set, and the iterator becomes invalid.
698 * Return value: %FALSE if the end of the #GHashTable has been reached.
703 g_hash_table_iter_next (GHashTableIter *iter,
707 RealIter *ri = (RealIter *) iter;
711 g_return_val_if_fail (iter != NULL, FALSE);
712 #ifndef G_DISABLE_ASSERT
713 g_return_val_if_fail (ri->version == ri->hash_table->version, FALSE);
715 g_return_val_if_fail (ri->position < ri->hash_table->size, FALSE);
717 position = ri->position;
722 if (position >= ri->hash_table->size)
724 ri->position = position;
728 node = &ri->hash_table->nodes [position];
730 while (!HASH_IS_REAL (node->key_hash));
735 *value = node->value;
737 ri->position = position;
742 * g_hash_table_iter_get_hash_table:
743 * @iter: an initialized #GHashTableIter.
745 * Returns the #GHashTable associated with @iter.
747 * Return value: the #GHashTable associated with @iter.
752 g_hash_table_iter_get_hash_table (GHashTableIter *iter)
754 g_return_val_if_fail (iter != NULL, NULL);
756 return ((RealIter *) iter)->hash_table;
760 iter_remove_or_steal (RealIter *ri, gboolean notify)
762 g_return_if_fail (ri != NULL);
763 #ifndef G_DISABLE_ASSERT
764 g_return_if_fail (ri->version == ri->hash_table->version);
766 g_return_if_fail (ri->position >= 0);
767 g_return_if_fail (ri->position < ri->hash_table->size);
769 g_hash_table_remove_node (ri->hash_table, &ri->hash_table->nodes [ri->position], notify);
771 #ifndef G_DISABLE_ASSERT
773 ri->hash_table->version++;
778 * g_hash_table_iter_remove:
779 * @iter: an initialized #GHashTableIter.
781 * Removes the key/value pair currently pointed to by the iterator
782 * from its associated #GHashTable. Can only be called after
783 * g_hash_table_iter_next() returned %TRUE, and cannot be called more
784 * than once for the same key/value pair.
786 * If the #GHashTable was created using g_hash_table_new_full(), the
787 * key and value are freed using the supplied destroy functions, otherwise
788 * you have to make sure that any dynamically allocated values are freed
794 g_hash_table_iter_remove (GHashTableIter *iter)
796 iter_remove_or_steal ((RealIter *) iter, TRUE);
800 * g_hash_table_iter_steal:
801 * @iter: an initialized #GHashTableIter.
803 * Removes the key/value pair currently pointed to by the iterator
804 * from its associated #GHashTable, without calling the key and value
805 * destroy functions. Can only be called after
806 * g_hash_table_iter_next() returned %TRUE, and cannot be called more
807 * than once for the same key/value pair.
812 g_hash_table_iter_steal (GHashTableIter *iter)
814 iter_remove_or_steal ((RealIter *) iter, FALSE);
820 * @hash_table: a valid #GHashTable.
822 * Atomically increments the reference count of @hash_table by one.
823 * This function is MT-safe and may be called from any thread.
825 * Return value: the passed in #GHashTable.
830 g_hash_table_ref (GHashTable *hash_table)
832 g_return_val_if_fail (hash_table != NULL, NULL);
833 g_return_val_if_fail (hash_table->ref_count > 0, hash_table);
835 g_atomic_int_add (&hash_table->ref_count, 1);
840 * g_hash_table_unref:
841 * @hash_table: a valid #GHashTable.
843 * Atomically decrements the reference count of @hash_table by one.
844 * If the reference count drops to 0, all keys and values will be
845 * destroyed, and all memory allocated by the hash table is released.
846 * This function is MT-safe and may be called from any thread.
851 g_hash_table_unref (GHashTable *hash_table)
853 g_return_if_fail (hash_table != NULL);
854 g_return_if_fail (hash_table->ref_count > 0);
856 if (g_atomic_int_exchange_and_add (&hash_table->ref_count, -1) - 1 == 0)
858 g_hash_table_remove_all_nodes (hash_table, TRUE);
859 g_free (hash_table->nodes);
860 g_slice_free (GHashTable, hash_table);
865 * g_hash_table_destroy:
866 * @hash_table: a #GHashTable.
868 * Destroys all keys and values in the #GHashTable and decrements its
869 * reference count by 1. If keys and/or values are dynamically allocated,
870 * you should either free them first or create the #GHashTable with destroy
871 * notifiers using g_hash_table_new_full(). In the latter case the destroy
872 * functions you supplied will be called on all keys and values during the
876 g_hash_table_destroy (GHashTable *hash_table)
878 g_return_if_fail (hash_table != NULL);
879 g_return_if_fail (hash_table->ref_count > 0);
881 g_hash_table_remove_all (hash_table);
882 g_hash_table_unref (hash_table);
886 * g_hash_table_lookup:
887 * @hash_table: a #GHashTable.
888 * @key: the key to look up.
890 * Looks up a key in a #GHashTable. Note that this function cannot
891 * distinguish between a key that is not present and one which is present
892 * and has the value %NULL. If you need this distinction, use
893 * g_hash_table_lookup_extended().
895 * Return value: the associated value, or %NULL if the key is not found.
898 g_hash_table_lookup (GHashTable *hash_table,
904 g_return_val_if_fail (hash_table != NULL, NULL);
906 node_index = g_hash_table_lookup_node (hash_table, key);
907 node = &hash_table->nodes [node_index];
909 return HASH_IS_REAL (node->key_hash) ? node->value : NULL;
913 * g_hash_table_lookup_extended:
914 * @hash_table: a #GHashTable
915 * @lookup_key: the key to look up
916 * @orig_key: return location for the original key, or %NULL
917 * @value: return location for the value associated with the key, or %NULL
919 * Looks up a key in the #GHashTable, returning the original key and the
920 * associated value and a #gboolean which is %TRUE if the key was found. This
921 * is useful if you need to free the memory allocated for the original key,
922 * for example before calling g_hash_table_remove().
924 * You can actually pass %NULL for @lookup_key to test
925 * whether the %NULL key exists, provided the hash and equal functions
926 * of @hash_table are %NULL-safe.
928 * Return value: %TRUE if the key was found in the #GHashTable.
931 g_hash_table_lookup_extended (GHashTable *hash_table,
932 gconstpointer lookup_key,
939 g_return_val_if_fail (hash_table != NULL, FALSE);
941 node_index = g_hash_table_lookup_node (hash_table, lookup_key);
942 node = &hash_table->nodes [node_index];
944 if (!HASH_IS_REAL (node->key_hash))
948 *orig_key = node->key;
951 *value = node->value;
957 * g_hash_table_insert_internal:
958 * @hash_table: our #GHashTable
959 * @key: the key to insert
960 * @value: the value to insert
961 * @keep_new_key: if %TRUE and this key already exists in the table
962 * then call the destroy notify function on the old key. If %FALSE
963 * then call the destroy notify function on the new key.
965 * Implements the common logic for the g_hash_table_insert() and
966 * g_hash_table_replace() functions.
968 * Do a lookup of @key. If it is found, replace it with the new
969 * @value (and perhaps the new @key). If it is not found, create a
973 g_hash_table_insert_internal (GHashTable *hash_table,
976 gboolean keep_new_key)
983 g_return_if_fail (hash_table != NULL);
984 g_return_if_fail (hash_table->ref_count > 0);
986 node_index = g_hash_table_lookup_node_for_insertion (hash_table, key, &key_hash);
987 node = &hash_table->nodes [node_index];
989 old_hash = node->key_hash;
991 if (HASH_IS_REAL (old_hash))
995 if (hash_table->key_destroy_func)
996 hash_table->key_destroy_func (node->key);
1001 if (hash_table->key_destroy_func)
1002 hash_table->key_destroy_func (key);
1005 if (hash_table->value_destroy_func)
1006 hash_table->value_destroy_func (node->value);
1008 node->value = value;
1013 node->value = value;
1014 node->key_hash = key_hash;
1016 hash_table->nnodes++;
1018 if (HASH_IS_UNUSED (old_hash))
1020 /* We replaced an empty node, and not a tombstone */
1021 hash_table->noccupied++;
1022 g_hash_table_maybe_resize (hash_table);
1025 #ifndef G_DISABLE_ASSERT
1026 hash_table->version++;
1032 * g_hash_table_insert:
1033 * @hash_table: a #GHashTable.
1034 * @key: a key to insert.
1035 * @value: the value to associate with the key.
1037 * Inserts a new key and value into a #GHashTable.
1039 * If the key already exists in the #GHashTable its current value is replaced
1040 * with the new value. If you supplied a @value_destroy_func when creating the
1041 * #GHashTable, the old value is freed using that function. If you supplied
1042 * a @key_destroy_func when creating the #GHashTable, the passed key is freed
1043 * using that function.
1046 g_hash_table_insert (GHashTable *hash_table,
1050 g_hash_table_insert_internal (hash_table, key, value, FALSE);
1054 * g_hash_table_replace:
1055 * @hash_table: a #GHashTable.
1056 * @key: a key to insert.
1057 * @value: the value to associate with the key.
1059 * Inserts a new key and value into a #GHashTable similar to
1060 * g_hash_table_insert(). The difference is that if the key already exists
1061 * in the #GHashTable, it gets replaced by the new key. If you supplied a
1062 * @value_destroy_func when creating the #GHashTable, the old value is freed
1063 * using that function. If you supplied a @key_destroy_func when creating the
1064 * #GHashTable, the old key is freed using that function.
1067 g_hash_table_replace (GHashTable *hash_table,
1071 g_hash_table_insert_internal (hash_table, key, value, TRUE);
1075 * g_hash_table_remove_internal:
1076 * @hash_table: our #GHashTable
1077 * @key: the key to remove
1078 * @notify: %TRUE if the destroy notify handlers are to be called
1079 * Return value: %TRUE if a node was found and removed, else %FALSE
1081 * Implements the common logic for the g_hash_table_remove() and
1082 * g_hash_table_steal() functions.
1084 * Do a lookup of @key and remove it if it is found, calling the
1085 * destroy notify handlers only if @notify is %TRUE.
1088 g_hash_table_remove_internal (GHashTable *hash_table,
1095 g_return_val_if_fail (hash_table != NULL, FALSE);
1097 node_index = g_hash_table_lookup_node (hash_table, key);
1098 node = &hash_table->nodes [node_index];
1100 if (!HASH_IS_REAL (node->key_hash))
1103 g_hash_table_remove_node (hash_table, node, notify);
1104 g_hash_table_maybe_resize (hash_table);
1106 #ifndef G_DISABLE_ASSERT
1107 hash_table->version++;
1114 * g_hash_table_remove:
1115 * @hash_table: a #GHashTable.
1116 * @key: the key to remove.
1118 * Removes a key and its associated value from a #GHashTable.
1120 * If the #GHashTable was created using g_hash_table_new_full(), the
1121 * key and value are freed using the supplied destroy functions, otherwise
1122 * you have to make sure that any dynamically allocated values are freed
1125 * Return value: %TRUE if the key was found and removed from the #GHashTable.
1128 g_hash_table_remove (GHashTable *hash_table,
1131 return g_hash_table_remove_internal (hash_table, key, TRUE);
1135 * g_hash_table_steal:
1136 * @hash_table: a #GHashTable.
1137 * @key: the key to remove.
1139 * Removes a key and its associated value from a #GHashTable without
1140 * calling the key and value destroy functions.
1142 * Return value: %TRUE if the key was found and removed from the #GHashTable.
1145 g_hash_table_steal (GHashTable *hash_table,
1148 return g_hash_table_remove_internal (hash_table, key, FALSE);
1152 * g_hash_table_remove_all:
1153 * @hash_table: a #GHashTable
1155 * Removes all keys and their associated values from a #GHashTable.
1157 * If the #GHashTable was created using g_hash_table_new_full(), the keys
1158 * and values are freed using the supplied destroy functions, otherwise you
1159 * have to make sure that any dynamically allocated values are freed
1165 g_hash_table_remove_all (GHashTable *hash_table)
1167 g_return_if_fail (hash_table != NULL);
1169 #ifndef G_DISABLE_ASSERT
1170 if (hash_table->nnodes != 0)
1171 hash_table->version++;
1174 g_hash_table_remove_all_nodes (hash_table, TRUE);
1175 g_hash_table_maybe_resize (hash_table);
1179 * g_hash_table_steal_all:
1180 * @hash_table: a #GHashTable.
1182 * Removes all keys and their associated values from a #GHashTable
1183 * without calling the key and value destroy functions.
1188 g_hash_table_steal_all (GHashTable *hash_table)
1190 g_return_if_fail (hash_table != NULL);
1192 #ifndef G_DISABLE_ASSERT
1193 if (hash_table->nnodes != 0)
1194 hash_table->version++;
1197 g_hash_table_remove_all_nodes (hash_table, FALSE);
1198 g_hash_table_maybe_resize (hash_table);
1202 * g_hash_table_foreach_remove_or_steal:
1203 * @hash_table: our #GHashTable
1204 * @func: the user's callback function
1205 * @user_data: data for @func
1206 * @notify: %TRUE if the destroy notify handlers are to be called
1208 * Implements the common logic for g_hash_table_foreach_remove() and
1209 * g_hash_table_foreach_steal().
1211 * Iterates over every node in the table, calling @func with the key
1212 * and value of the node (and @user_data). If @func returns %TRUE the
1213 * node is removed from the table.
1215 * If @notify is true then the destroy notify handlers will be called
1216 * for each removed node.
1219 g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
1227 for (i = 0; i < hash_table->size; i++)
1229 GHashNode *node = &hash_table->nodes [i];
1231 if (HASH_IS_REAL (node->key_hash) &&
1232 (* func) (node->key, node->value, user_data))
1234 g_hash_table_remove_node (hash_table, node, notify);
1239 g_hash_table_maybe_resize (hash_table);
1241 #ifndef G_DISABLE_ASSERT
1243 hash_table->version++;
1250 * g_hash_table_foreach_remove:
1251 * @hash_table: a #GHashTable.
1252 * @func: the function to call for each key/value pair.
1253 * @user_data: user data to pass to the function.
1255 * Calls the given function for each key/value pair in the #GHashTable.
1256 * If the function returns %TRUE, then the key/value pair is removed from the
1257 * #GHashTable. If you supplied key or value destroy functions when creating
1258 * the #GHashTable, they are used to free the memory allocated for the removed
1261 * See #GHashTableIter for an alternative way to loop over the
1262 * key/value pairs in the hash table.
1264 * Return value: the number of key/value pairs removed.
1267 g_hash_table_foreach_remove (GHashTable *hash_table,
1271 g_return_val_if_fail (hash_table != NULL, 0);
1272 g_return_val_if_fail (func != NULL, 0);
1274 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
1278 * g_hash_table_foreach_steal:
1279 * @hash_table: a #GHashTable.
1280 * @func: the function to call for each key/value pair.
1281 * @user_data: user data to pass to the function.
1283 * Calls the given function for each key/value pair in the #GHashTable.
1284 * If the function returns %TRUE, then the key/value pair is removed from the
1285 * #GHashTable, but no key or value destroy functions are called.
1287 * See #GHashTableIter for an alternative way to loop over the
1288 * key/value pairs in the hash table.
1290 * Return value: the number of key/value pairs removed.
1293 g_hash_table_foreach_steal (GHashTable *hash_table,
1297 g_return_val_if_fail (hash_table != NULL, 0);
1298 g_return_val_if_fail (func != NULL, 0);
1300 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
1304 * g_hash_table_foreach:
1305 * @hash_table: a #GHashTable.
1306 * @func: the function to call for each key/value pair.
1307 * @user_data: user data to pass to the function.
1309 * Calls the given function for each of the key/value pairs in the
1310 * #GHashTable. The function is passed the key and value of each
1311 * pair, and the given @user_data parameter. The hash table may not
1312 * be modified while iterating over it (you can't add/remove
1313 * items). To remove all items matching a predicate, use
1314 * g_hash_table_foreach_remove().
1316 * See g_hash_table_find() for performance caveats for linear
1317 * order searches in contrast to g_hash_table_lookup().
1320 g_hash_table_foreach (GHashTable *hash_table,
1326 g_return_if_fail (hash_table != NULL);
1327 g_return_if_fail (func != NULL);
1329 for (i = 0; i < hash_table->size; i++)
1331 GHashNode *node = &hash_table->nodes [i];
1333 if (HASH_IS_REAL (node->key_hash))
1334 (* func) (node->key, node->value, user_data);
1339 * g_hash_table_find:
1340 * @hash_table: a #GHashTable.
1341 * @predicate: function to test the key/value pairs for a certain property.
1342 * @user_data: user data to pass to the function.
1344 * Calls the given function for key/value pairs in the #GHashTable until
1345 * @predicate returns %TRUE. The function is passed the key and value of
1346 * each pair, and the given @user_data parameter. The hash table may not
1347 * be modified while iterating over it (you can't add/remove items).
1349 * Note, that hash tables are really only optimized for forward lookups,
1350 * i.e. g_hash_table_lookup().
1351 * So code that frequently issues g_hash_table_find() or
1352 * g_hash_table_foreach() (e.g. in the order of once per every entry in a
1353 * hash table) should probably be reworked to use additional or different
1354 * data structures for reverse lookups (keep in mind that an O(n) find/foreach
1355 * operation issued for all n values in a hash table ends up needing O(n*n)
1358 * Return value: The value of the first key/value pair is returned, for which
1359 * func evaluates to %TRUE. If no pair with the requested property is found,
1360 * %NULL is returned.
1365 g_hash_table_find (GHashTable *hash_table,
1371 g_return_val_if_fail (hash_table != NULL, NULL);
1372 g_return_val_if_fail (predicate != NULL, NULL);
1374 for (i = 0; i < hash_table->size; i++)
1376 GHashNode *node = &hash_table->nodes [i];
1378 if (HASH_IS_REAL (node->key_hash) &&
1379 predicate (node->key, node->value, user_data))
1387 * g_hash_table_size:
1388 * @hash_table: a #GHashTable.
1390 * Returns the number of elements contained in the #GHashTable.
1392 * Return value: the number of key/value pairs in the #GHashTable.
1395 g_hash_table_size (GHashTable *hash_table)
1397 g_return_val_if_fail (hash_table != NULL, 0);
1399 return hash_table->nnodes;
1403 * g_hash_table_get_keys:
1404 * @hash_table: a #GHashTable
1406 * Retrieves every key inside @hash_table. The returned data is valid
1407 * until @hash_table is modified.
1409 * Return value: a #GList containing all the keys inside the hash
1410 * table. The content of the list is owned by the hash table and
1411 * should not be modified or freed. Use g_list_free() when done
1417 g_hash_table_get_keys (GHashTable *hash_table)
1422 g_return_val_if_fail (hash_table != NULL, NULL);
1425 for (i = 0; i < hash_table->size; i++)
1427 GHashNode *node = &hash_table->nodes [i];
1429 if (HASH_IS_REAL (node->key_hash))
1430 retval = g_list_prepend (retval, node->key);
1437 * g_hash_table_get_values:
1438 * @hash_table: a #GHashTable
1440 * Retrieves every value inside @hash_table. The returned data is
1441 * valid until @hash_table is modified.
1443 * Return value: a #GList containing all the values inside the hash
1444 * table. The content of the list is owned by the hash table and
1445 * should not be modified or freed. Use g_list_free() when done
1451 g_hash_table_get_values (GHashTable *hash_table)
1456 g_return_val_if_fail (hash_table != NULL, NULL);
1459 for (i = 0; i < hash_table->size; i++)
1461 GHashNode *node = &hash_table->nodes [i];
1463 if (HASH_IS_REAL (node->key_hash))
1464 retval = g_list_prepend (retval, node->value);