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/.
37 #define HASH_TABLE_MIN_SIZE 11
38 #define HASH_TABLE_MAX_SIZE 13845163
41 typedef struct _GHashNode GHashNode;
57 GEqualFunc key_equal_func;
58 volatile gint ref_count;
59 #ifndef G_DISABLE_ASSERT
61 * Tracks the structure of the hash table, not its contents: is only
62 * incremented when a node is added or removed (is not incremented
63 * when the key or data of a node is modified).
67 GDestroyNotify key_destroy_func;
68 GDestroyNotify value_destroy_func;
73 GHashTable *hash_table;
77 gboolean pre_advanced;
82 * g_hash_table_lookup_node:
83 * @hash_table: our #GHashTable
84 * @key: the key to lookup against
85 * @hash_return: optional key hash return location
86 * Return value: a pointer to the described #GHashNode pointer
88 * Performs a lookup in the hash table. Virtually all hash operations
89 * will use this function internally.
91 * This function first computes the hash value of the key using the
92 * user's hash function.
94 * If an entry in the table matching @key is found then this function
95 * returns a pointer to the pointer to that entry in the table. In
96 * the case that the entry is at the head of a chain, this pointer
97 * will be an item in the nodes[] array. In the case that the entry
98 * is not at the head of a chain, this pointer will be the ->next
99 * pointer on the node that preceeds it.
101 * In the case that no matching entry exists in the table, a pointer
102 * to a %NULL pointer will be returned. To insert a item, this %NULL
103 * pointer should be updated to point to the new #GHashNode.
105 * If @hash_return is a pass-by-reference parameter. If it is
106 * non-%NULL then the computed hash value is returned. This is to
107 * save insertions from having to compute the hash record again for
110 static inline GHashNode **
111 g_hash_table_lookup_node (GHashTable *hash_table,
115 GHashNode **node_ptr, *node;
118 hash_value = (* hash_table->hash_func) (key);
119 node_ptr = &hash_table->nodes[hash_value % hash_table->size];
122 *hash_return = hash_value;
124 /* Hash table lookup needs to be fast.
125 * We therefore remove the extra conditional of testing
126 * whether to call the key_equal_func or not from
129 * Additional optimisation: first check if our full hash
130 * values are equal so we can avoid calling the full-blown
131 * key equality function in most cases.
133 if (hash_table->key_equal_func)
135 while ((node = *node_ptr))
137 if (node->key_hash == hash_value &&
138 hash_table->key_equal_func (node->key, key))
141 node_ptr = &(*node_ptr)->next;
146 while ((node = *node_ptr))
148 if (node->key == key)
151 node_ptr = &(*node_ptr)->next;
159 * g_hash_table_remove_node:
160 * @hash_table: our #GHashTable
161 * @node_ptr_ptr: a pointer to the return value from
162 * g_hash_table_lookup_node()
163 * @notify: %TRUE if the destroy notify handlers are to be called
165 * Removes a node from the hash table and updates the node count. The
166 * node is freed. No table resize is performed.
168 * If @notify is %TRUE then the destroy notify functions are called
169 * for the key and value of the hash node.
171 * @node_ptr_ptr is a pass-by-reference in/out parameter. When the
172 * function is called, it should point to the pointer to the node to
173 * remove. This level of indirection is required so that the pointer
174 * may be updated appropriately once the node has been removed.
176 * Before the function returns, the pointer at @node_ptr_ptr will be
177 * updated to point to the position in the table that contains the
178 * pointer to the "next" node in the chain. This makes this function
179 * convenient to use from functions that iterate over the entire
180 * table. If there is no further item in the chain then the
181 * #GHashNode pointer will be %NULL (ie: **node_ptr_ptr == %NULL).
183 * Since the pointer in the table to the removed node is replaced with
184 * either a pointer to the next node or a %NULL pointer as
185 * appropriate, the pointer at the end of @node_ptr_ptr will never be
186 * modified at all. Stay tuned. :)
189 g_hash_table_remove_node (GHashTable *hash_table,
190 GHashNode ***node_ptr_ptr,
193 GHashNode **node_ptr, *node;
195 node_ptr = *node_ptr_ptr;
198 *node_ptr = node->next;
200 if (notify && hash_table->key_destroy_func)
201 hash_table->key_destroy_func (node->key);
203 if (notify && hash_table->value_destroy_func)
204 hash_table->value_destroy_func (node->value);
206 g_slice_free (GHashNode, node);
208 hash_table->nnodes--;
212 * g_hash_table_remove_all_nodes:
213 * @hash_table: our #GHashTable
214 * @notify: %TRUE if the destroy notify handlers are to be called
216 * Removes all nodes from the table. Since this may be a precursor to
217 * freeing the table entirely, no resize is performed.
219 * If @notify is %TRUE then the destroy notify functions are called
220 * for the key and value of the hash node.
223 g_hash_table_remove_all_nodes (GHashTable *hash_table,
226 GHashNode **node_ptr;
229 for (i = 0; i < hash_table->size; i++)
230 for (node_ptr = &hash_table->nodes[i]; *node_ptr != NULL;)
231 g_hash_table_remove_node (hash_table, &node_ptr, notify);
233 hash_table->nnodes = 0;
237 * g_hash_table_resize:
238 * @hash_table: our #GHashTable
240 * Resizes the hash table to the optimal size based on the number of
241 * nodes currently held. If you call this function then a resize will
242 * occur, even if one does not need to occur. Use
243 * g_hash_table_maybe_resize() instead.
246 g_hash_table_resize (GHashTable *hash_table)
248 GHashNode **new_nodes;
255 new_size = g_spaced_primes_closest (hash_table->nnodes);
256 new_size = CLAMP (new_size, HASH_TABLE_MIN_SIZE, HASH_TABLE_MAX_SIZE);
258 new_nodes = g_new0 (GHashNode*, new_size);
260 for (i = 0; i < hash_table->size; i++)
261 for (node = hash_table->nodes[i]; node; node = next)
265 hash_val = node->key_hash % new_size;
267 node->next = new_nodes[hash_val];
268 new_nodes[hash_val] = node;
271 g_free (hash_table->nodes);
272 hash_table->nodes = new_nodes;
273 hash_table->size = new_size;
277 * g_hash_table_maybe_resize:
278 * @hash_table: our #GHashTable
280 * Resizes the hash table, if needed.
282 * Essentially, calls g_hash_table_resize() if the table has strayed
283 * too far from its ideal size for its number of nodes.
286 g_hash_table_maybe_resize (GHashTable *hash_table)
288 gint nnodes = hash_table->nnodes;
289 gint size = hash_table->size;
291 if ((size >= 3 * nnodes && size > HASH_TABLE_MIN_SIZE) ||
292 (3 * size <= nnodes && size < HASH_TABLE_MAX_SIZE))
293 g_hash_table_resize (hash_table);
298 * @hash_func: a function to create a hash value from a key.
299 * Hash values are used to determine where keys are stored within the
300 * #GHashTable data structure. The g_direct_hash(), g_int_hash() and
301 * g_str_hash() functions are provided for some common types of keys.
302 * If hash_func is %NULL, g_direct_hash() is used.
303 * @key_equal_func: a function to check two keys for equality. This is
304 * used when looking up keys in the #GHashTable. The g_direct_equal(),
305 * g_int_equal() and g_str_equal() functions are provided for the most
306 * common types of keys. If @key_equal_func is %NULL, keys are compared
307 * directly in a similar fashion to g_direct_equal(), but without the
308 * overhead of a function call.
310 * Creates a new #GHashTable with a reference count of 1.
312 * Return value: a new #GHashTable.
315 g_hash_table_new (GHashFunc hash_func,
316 GEqualFunc key_equal_func)
318 return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
323 * g_hash_table_new_full:
324 * @hash_func: a function to create a hash value from a key.
325 * @key_equal_func: a function to check two keys for equality.
326 * @key_destroy_func: a function to free the memory allocated for the key
327 * used when removing the entry from the #GHashTable or %NULL if you
328 * don't want to supply such a function.
329 * @value_destroy_func: a function to free the memory allocated for the
330 * value used when removing the entry from the #GHashTable or %NULL if
331 * you don't want to supply such a function.
333 * Creates a new #GHashTable like g_hash_table_new() with a reference count
334 * of 1 and allows to specify functions to free the memory allocated for the
335 * key and value that get called when removing the entry from the #GHashTable.
337 * Return value: a new #GHashTable.
340 g_hash_table_new_full (GHashFunc hash_func,
341 GEqualFunc key_equal_func,
342 GDestroyNotify key_destroy_func,
343 GDestroyNotify value_destroy_func)
345 GHashTable *hash_table;
347 hash_table = g_slice_new (GHashTable);
348 hash_table->size = HASH_TABLE_MIN_SIZE;
349 hash_table->nnodes = 0;
350 hash_table->hash_func = hash_func ? hash_func : g_direct_hash;
351 hash_table->key_equal_func = key_equal_func;
352 hash_table->ref_count = 1;
353 #ifndef G_DISABLE_ASSERT
354 hash_table->version = 0;
356 hash_table->key_destroy_func = key_destroy_func;
357 hash_table->value_destroy_func = value_destroy_func;
358 hash_table->nodes = g_new0 (GHashNode*, hash_table->size);
364 * g_hash_table_iter_init:
365 * @iter: an uninitialized #GHashTableIter.
366 * @hash_table: a #GHashTable.
368 * Initializes a key/value pair iterator and associates it with
369 * @hash_table. Modifying the hash table after calling this function
370 * invalidates the returned iterator.
372 * <informalexample><programlisting>
373 * GHashTableIter iter;
374 * gpointer key, value;
376 * g_hash_table_iter_init(&iter, hash_table);
377 * while (g_hash_table_iter_next(&iter, &key, &value)) {
378 * /* do something with key and value */
380 * </programlisting></informalexample>
385 g_hash_table_iter_init (GHashTableIter *iter,
386 GHashTable *hash_table)
388 RealIter *ri = (RealIter *) iter;
390 g_return_if_fail (iter != NULL);
391 g_return_if_fail (hash_table != NULL);
393 ri->hash_table = hash_table;
394 ri->prev_node = NULL;
397 ri->pre_advanced = FALSE;
398 #ifndef G_DISABLE_ASSERT
399 ri->version = hash_table->version;
404 * g_hash_table_iter_next:
405 * @iter: an initialized #GHashTableIter.
406 * @key: a location to store the key, or %NULL.
407 * @value: a location to store the value, or %NULL.
409 * Advances @iter and retrieves the key and/or value that are now
410 * pointed to as a result of this advancement. If %FALSE is returned,
411 * @key and @value are not set, and the iterator becomes invalid.
413 * Return value: %FALSE if the end of the #GHashTable has been reached.
418 g_hash_table_iter_next (GHashTableIter *iter,
422 RealIter *ri = (RealIter *) iter;
424 g_return_val_if_fail (iter != NULL, FALSE);
425 g_return_val_if_fail (ri->version == ri->hash_table->version, FALSE);
427 if (ri->pre_advanced)
429 ri->pre_advanced = FALSE;
431 if (ri->node == NULL)
436 if (ri->node != NULL)
438 ri->prev_node = ri->node;
439 ri->node = ri->node->next;
442 while (ri->node == NULL)
445 if (ri->position >= ri->hash_table->size)
448 ri->prev_node = NULL;
449 ri->node = ri->hash_table->nodes[ri->position];
454 *key = ri->node->key;
456 *value = ri->node->value;
462 * g_hash_table_iter_get_hash_table:
463 * @iter: an initialized #GHashTableIter.
465 * Returns the #GHashTable associated with @iter.
467 * Return value: the #GHashTable associated with @iter.
472 g_hash_table_iter_get_hash_table (GHashTableIter *iter)
474 g_return_val_if_fail (iter != NULL, NULL);
476 return ((RealIter *) iter)->hash_table;
480 iter_remove_or_steal (RealIter *ri, gboolean notify)
486 g_return_if_fail (ri != NULL);
487 g_return_if_fail (ri->version == ri->hash_table->version);
488 g_return_if_fail (ri->node != NULL);
490 prev = ri->prev_node;
492 position = ri->position;
494 /* pre-advance the iterator since we will remove the node */
496 ri->node = ri->node->next;
497 /* ri->prev_node is still the correct previous node */
499 while (ri->node == NULL)
502 if (ri->position >= ri->hash_table->size)
505 ri->prev_node = NULL;
506 ri->node = ri->hash_table->nodes[ri->position];
509 ri->pre_advanced = TRUE;
511 /* remove the node */
514 prev->next = node->next;
516 ri->hash_table->nodes[position] = node->next;
520 if (ri->hash_table->key_destroy_func)
521 ri->hash_table->key_destroy_func(node->key);
522 if (ri->hash_table->value_destroy_func)
523 ri->hash_table->value_destroy_func(node->value);
526 g_slice_free (GHashNode, node);
528 ri->hash_table->nnodes--;
532 * g_hash_table_iter_remove():
533 * @iter: an initialized #GHashTableIter.
535 * Removes the key/value pair currently pointed to by the iterator
536 * from its associated #GHashTable. Can only be called after
537 * g_hash_table_iter_next() returned %TRUE, and cannot be called more
538 * than once for the same key/value pair.
540 * If the #GHashTable was created using g_hash_table_new_full(), the
541 * key and value are freed using the supplied destroy functions, otherwise
542 * you have to make sure that any dynamically allocated values are freed
548 g_hash_table_iter_remove (GHashTableIter *iter)
550 iter_remove_or_steal ((RealIter *) iter, TRUE);
554 * g_hash_table_iter_steal():
555 * @iter: an initialized #GHashTableIter.
557 * Removes the key/value pair currently pointed to by the iterator
558 * from its associated #GHashTable, without calling the key and value
559 * destroy functions. Can only be called after
560 * g_hash_table_iter_next() returned %TRUE, and cannot be called more
561 * than once for the same key/value pair.
566 g_hash_table_iter_steal (GHashTableIter *iter)
568 iter_remove_or_steal ((RealIter *) iter, FALSE);
574 * @hash_table: a valid #GHashTable.
576 * Atomically increments the reference count of @hash_table by one.
577 * This function is MT-safe and may be called from any thread.
579 * Return value: the passed in #GHashTable.
584 g_hash_table_ref (GHashTable *hash_table)
586 g_return_val_if_fail (hash_table != NULL, NULL);
587 g_return_val_if_fail (hash_table->ref_count > 0, hash_table);
589 g_atomic_int_add (&hash_table->ref_count, 1);
594 * g_hash_table_unref:
595 * @hash_table: a valid #GHashTable.
597 * Atomically decrements the reference count of @hash_table by one.
598 * If the reference count drops to 0, all keys and values will be
599 * destroyed, and all memory allocated by the hash table is released.
600 * This function is MT-safe and may be called from any thread.
605 g_hash_table_unref (GHashTable *hash_table)
607 g_return_if_fail (hash_table != NULL);
608 g_return_if_fail (hash_table->ref_count > 0);
610 if (g_atomic_int_exchange_and_add (&hash_table->ref_count, -1) - 1 == 0)
612 g_hash_table_remove_all_nodes (hash_table, TRUE);
613 g_free (hash_table->nodes);
614 g_slice_free (GHashTable, hash_table);
619 * g_hash_table_destroy:
620 * @hash_table: a #GHashTable.
622 * Destroys all keys and values in the #GHashTable and decrements its
623 * reference count by 1. If keys and/or values are dynamically allocated,
624 * you should either free them first or create the #GHashTable with destroy
625 * notifiers using g_hash_table_new_full(). In the latter case the destroy
626 * functions you supplied will be called on all keys and values during the
630 g_hash_table_destroy (GHashTable *hash_table)
632 g_return_if_fail (hash_table != NULL);
633 g_return_if_fail (hash_table->ref_count > 0);
635 g_hash_table_remove_all (hash_table);
636 g_hash_table_unref (hash_table);
640 * g_hash_table_lookup:
641 * @hash_table: a #GHashTable.
642 * @key: the key to look up.
644 * Looks up a key in a #GHashTable. Note that this function cannot
645 * distinguish between a key that is not present and one which is present
646 * and has the value %NULL. If you need this distinction, use
647 * g_hash_table_lookup_extended().
649 * Return value: the associated value, or %NULL if the key is not found.
652 g_hash_table_lookup (GHashTable *hash_table,
657 g_return_val_if_fail (hash_table != NULL, NULL);
659 node = *g_hash_table_lookup_node (hash_table, key, NULL);
661 return node ? node->value : NULL;
665 * g_hash_table_lookup_extended:
666 * @hash_table: a #GHashTable.
667 * @lookup_key: the key to look up.
668 * @orig_key: returns the original key.
669 * @value: returns the value associated with the key.
671 * Looks up a key in the #GHashTable, returning the original key and the
672 * associated value and a #gboolean which is %TRUE if the key was found. This
673 * is useful if you need to free the memory allocated for the original key,
674 * for example before calling g_hash_table_remove().
676 * Return value: %TRUE if the key was found in the #GHashTable.
679 g_hash_table_lookup_extended (GHashTable *hash_table,
680 gconstpointer lookup_key,
686 g_return_val_if_fail (hash_table != NULL, FALSE);
688 node = *g_hash_table_lookup_node (hash_table, lookup_key, NULL);
694 *orig_key = node->key;
697 *value = node->value;
703 * g_hash_table_insert_internal:
704 * @hash_table: our #GHashTable
705 * @key: the key to insert
706 * @value: the value to insert
707 * @keep_new_key: if %TRUE and this key already exists in the table
708 * then call the destroy notify function on the old key. If %FALSE
709 * then call the destroy notify function on the new key.
711 * Implements the common logic for the g_hash_table_insert() and
712 * g_hash_table_replace() functions.
714 * Do a lookup of @key. If it is found, replace it with the new
715 * @value (and perhaps the new @key). If it is not found, create a
719 g_hash_table_insert_internal (GHashTable *hash_table,
722 gboolean keep_new_key)
724 GHashNode **node_ptr, *node;
727 g_return_if_fail (hash_table != NULL);
728 g_return_if_fail (hash_table->ref_count > 0);
730 node_ptr = g_hash_table_lookup_node (hash_table, key, &key_hash);
732 if ((node = *node_ptr))
736 if (hash_table->key_destroy_func)
737 hash_table->key_destroy_func (node->key);
742 if (hash_table->key_destroy_func)
743 hash_table->key_destroy_func (key);
746 if (hash_table->value_destroy_func)
747 hash_table->value_destroy_func (node->value);
753 node = g_slice_new (GHashNode);
757 node->key_hash = key_hash;
761 hash_table->nnodes++;
762 g_hash_table_maybe_resize (hash_table);
764 #ifndef G_DISABLE_ASSERT
765 hash_table->version++;
771 * g_hash_table_insert:
772 * @hash_table: a #GHashTable.
773 * @key: a key to insert.
774 * @value: the value to associate with the key.
776 * Inserts a new key and value into a #GHashTable.
778 * If the key already exists in the #GHashTable its current value is replaced
779 * with the new value. If you supplied a @value_destroy_func when creating the
780 * #GHashTable, the old value is freed using that function. If you supplied
781 * a @key_destroy_func when creating the #GHashTable, the passed key is freed
782 * using that function.
785 g_hash_table_insert (GHashTable *hash_table,
789 return g_hash_table_insert_internal (hash_table, key, value, FALSE);
793 * g_hash_table_replace:
794 * @hash_table: a #GHashTable.
795 * @key: a key to insert.
796 * @value: the value to associate with the key.
798 * Inserts a new key and value into a #GHashTable similar to
799 * g_hash_table_insert(). The difference is that if the key already exists
800 * in the #GHashTable, it gets replaced by the new key. If you supplied a
801 * @value_destroy_func when creating the #GHashTable, the old value is freed
802 * using that function. If you supplied a @key_destroy_func when creating the
803 * #GHashTable, the old key is freed using that function.
806 g_hash_table_replace (GHashTable *hash_table,
810 return g_hash_table_insert_internal (hash_table, key, value, TRUE);
814 * g_hash_table_remove_internal:
815 * @hash_table: our #GHashTable
816 * @key: the key to remove
817 * @notify: %TRUE if the destroy notify handlers are to be called
818 * Return value: %TRUE if a node was found and removed, else %FALSE
820 * Implements the common logic for the g_hash_table_remove() and
821 * g_hash_table_steal() functions.
823 * Do a lookup of @key and remove it if it is found, calling the
824 * destroy notify handlers only if @notify is %TRUE.
827 g_hash_table_remove_internal (GHashTable *hash_table,
831 GHashNode **node_ptr;
833 g_return_val_if_fail (hash_table != NULL, FALSE);
835 node_ptr = g_hash_table_lookup_node (hash_table, key, NULL);
836 if (*node_ptr == NULL)
839 g_hash_table_remove_node (hash_table, &node_ptr, notify);
840 g_hash_table_maybe_resize (hash_table);
842 #ifndef G_DISABLE_ASSERT
843 hash_table->version++;
850 * g_hash_table_remove:
851 * @hash_table: a #GHashTable.
852 * @key: the key to remove.
854 * Removes a key and its associated value from a #GHashTable.
856 * If the #GHashTable was created using g_hash_table_new_full(), the
857 * key and value are freed using the supplied destroy functions, otherwise
858 * you have to make sure that any dynamically allocated values are freed
861 * Return value: %TRUE if the key was found and removed from the #GHashTable.
864 g_hash_table_remove (GHashTable *hash_table,
867 return g_hash_table_remove_internal (hash_table, key, TRUE);
871 * g_hash_table_steal:
872 * @hash_table: a #GHashTable.
873 * @key: the key to remove.
875 * Removes a key and its associated value from a #GHashTable without
876 * calling the key and value destroy functions.
878 * Return value: %TRUE if the key was found and removed from the #GHashTable.
881 g_hash_table_steal (GHashTable *hash_table,
884 return g_hash_table_remove_internal (hash_table, key, FALSE);
888 * g_hash_table_remove_all:
889 * @hash_table: a #GHashTable
891 * Removes all keys and their associated values from a #GHashTable.
893 * If the #GHashTable was created using g_hash_table_new_full(), the keys
894 * and values are freed using the supplied destroy functions, otherwise you
895 * have to make sure that any dynamically allocated values are freed
901 g_hash_table_remove_all (GHashTable *hash_table)
903 g_return_if_fail (hash_table != NULL);
905 #ifndef G_DISABLE_ASSERT
906 if (hash_table->nnodes != 0)
907 hash_table->version++;
910 g_hash_table_remove_all_nodes (hash_table, TRUE);
911 g_hash_table_maybe_resize (hash_table);
915 * g_hash_table_steal_all:
916 * @hash_table: a #GHashTable.
918 * Removes all keys and their associated values from a #GHashTable
919 * without calling the key and value destroy functions.
924 g_hash_table_steal_all (GHashTable *hash_table)
926 g_return_if_fail (hash_table != NULL);
928 #ifndef G_DISABLE_ASSERT
929 if (hash_table->nnodes != 0)
930 hash_table->version++;
933 g_hash_table_remove_all_nodes (hash_table, FALSE);
934 g_hash_table_maybe_resize (hash_table);
938 * g_hash_table_foreach_remove_or_steal:
939 * @hash_table: our #GHashTable
940 * @func: the user's callback function
941 * @user_data: data for @func
942 * @notify: %TRUE if the destroy notify handlers are to be called
944 * Implements the common logic for g_hash_table_foreach_remove() and
945 * g_hash_table_foreach_steal().
947 * Iterates over every node in the table, calling @func with the key
948 * and value of the node (and @user_data). If @func returns %TRUE the
949 * node is removed from the table.
951 * If @notify is true then the destroy notify handlers will be called
952 * for each removed node.
955 g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
960 GHashNode *node, **node_ptr;
964 for (i = 0; i < hash_table->size; i++)
965 for (node_ptr = &hash_table->nodes[i]; (node = *node_ptr) != NULL;)
966 if ((* func) (node->key, node->value, user_data))
968 g_hash_table_remove_node (hash_table, &node_ptr, notify);
972 node_ptr = &node->next;
974 g_hash_table_maybe_resize (hash_table);
976 #ifndef G_DISABLE_ASSERT
978 hash_table->version++;
985 * g_hash_table_foreach_remove:
986 * @hash_table: a #GHashTable.
987 * @func: the function to call for each key/value pair.
988 * @user_data: user data to pass to the function.
990 * Calls the given function for each key/value pair in the #GHashTable.
991 * If the function returns %TRUE, then the key/value pair is removed from the
992 * #GHashTable. If you supplied key or value destroy functions when creating
993 * the #GHashTable, they are used to free the memory allocated for the removed
996 * See #GHashTableIterator for an alternative way to loop over the
997 * key/value pairs in the hash table.
999 * Return value: the number of key/value pairs removed.
1002 g_hash_table_foreach_remove (GHashTable *hash_table,
1006 g_return_val_if_fail (hash_table != NULL, 0);
1007 g_return_val_if_fail (func != NULL, 0);
1009 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
1013 * g_hash_table_foreach_steal:
1014 * @hash_table: a #GHashTable.
1015 * @func: the function to call for each key/value pair.
1016 * @user_data: user data to pass to the function.
1018 * Calls the given function for each key/value pair in the #GHashTable.
1019 * If the function returns %TRUE, then the key/value pair is removed from the
1020 * #GHashTable, but no key or value destroy functions are called.
1022 * See #GHashTableIterator for an alternative way to loop over the
1023 * key/value pairs in the hash table.
1025 * Return value: the number of key/value pairs removed.
1028 g_hash_table_foreach_steal (GHashTable *hash_table,
1032 g_return_val_if_fail (hash_table != NULL, 0);
1033 g_return_val_if_fail (func != NULL, 0);
1035 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
1039 * g_hash_table_foreach:
1040 * @hash_table: a #GHashTable.
1041 * @func: the function to call for each key/value pair.
1042 * @user_data: user data to pass to the function.
1044 * Calls the given function for each of the key/value pairs in the
1045 * #GHashTable. The function is passed the key and value of each
1046 * pair, and the given @user_data parameter. The hash table may not
1047 * be modified while iterating over it (you can't add/remove
1048 * items). To remove all items matching a predicate, use
1049 * g_hash_table_foreach_remove().
1051 * See g_hash_table_find() for performance caveats for linear
1052 * order searches in contrast to g_hash_table_lookup().
1055 g_hash_table_foreach (GHashTable *hash_table,
1062 g_return_if_fail (hash_table != NULL);
1063 g_return_if_fail (func != NULL);
1065 for (i = 0; i < hash_table->size; i++)
1066 for (node = hash_table->nodes[i]; node; node = node->next)
1067 (* func) (node->key, node->value, user_data);
1071 * g_hash_table_find:
1072 * @hash_table: a #GHashTable.
1073 * @predicate: function to test the key/value pairs for a certain property.
1074 * @user_data: user data to pass to the function.
1076 * Calls the given function for key/value pairs in the #GHashTable until
1077 * @predicate returns %TRUE. The function is passed the key and value of
1078 * each pair, and the given @user_data parameter. The hash table may not
1079 * be modified while iterating over it (you can't add/remove items).
1081 * Note, that hash tables are really only optimized for forward lookups,
1082 * i.e. g_hash_table_lookup().
1083 * So code that frequently issues g_hash_table_find() or
1084 * g_hash_table_foreach() (e.g. in the order of once per every entry in a
1085 * hash table) should probably be reworked to use additional or different
1086 * data structures for reverse lookups (keep in mind that an O(n) find/foreach
1087 * operation issued for all n values in a hash table ends up needing O(n*n)
1090 * Return value: The value of the first key/value pair is returned, for which
1091 * func evaluates to %TRUE. If no pair with the requested property is found,
1092 * %NULL is returned.
1097 g_hash_table_find (GHashTable *hash_table,
1104 g_return_val_if_fail (hash_table != NULL, NULL);
1105 g_return_val_if_fail (predicate != NULL, NULL);
1107 for (i = 0; i < hash_table->size; i++)
1108 for (node = hash_table->nodes[i]; node; node = node->next)
1109 if (predicate (node->key, node->value, user_data))
1115 * g_hash_table_size:
1116 * @hash_table: a #GHashTable.
1118 * Returns the number of elements contained in the #GHashTable.
1120 * Return value: the number of key/value pairs in the #GHashTable.
1123 g_hash_table_size (GHashTable *hash_table)
1125 g_return_val_if_fail (hash_table != NULL, 0);
1127 return hash_table->nnodes;
1131 * g_hash_table_get_keys:
1132 * @hash_table: a #GHashTable
1134 * Retrieves every key inside @hash_table. The returned data is valid
1135 * until @hash_table is modified.
1137 * Return value: a #GList containing all the keys inside the hash
1138 * table. The content of the list is owned by the hash table and
1139 * should not be modified or freed. Use g_list_free() when done
1145 g_hash_table_get_keys (GHashTable *hash_table)
1151 g_return_val_if_fail (hash_table != NULL, NULL);
1154 for (i = 0; i < hash_table->size; i++)
1155 for (node = hash_table->nodes[i]; node; node = node->next)
1156 retval = g_list_prepend (retval, node->key);
1162 * g_hash_table_get_values:
1163 * @hash_table: a #GHashTable
1165 * Retrieves every value inside @hash_table. The returned data is
1166 * valid until @hash_table is modified.
1168 * Return value: a #GList containing all the values inside the hash
1169 * table. The content of the list is owned by the hash table and
1170 * should not be modified or freed. Use g_list_free() when done
1176 g_hash_table_get_values (GHashTable *hash_table)
1182 g_return_val_if_fail (hash_table != NULL, NULL);
1185 for (i = 0; i < hash_table->size; i++)
1186 for (node = hash_table->nodes[i]; node; node = node->next)
1187 retval = g_list_prepend (retval, node->value);
1192 #define __G_HASH_C__
1193 #include "galiasdef.c"