gnetworkmonitornm: Check if network-manager is running
[glib.git] / glib / ghash.c
blob41d05dc6e54e081ef0e23445dae3ef01aa59365c
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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
26 * MT safe
29 #include "config.h"
31 #include <string.h> /* memset */
33 #include "ghash.h"
35 #include "glib-private.h"
36 #include "gstrfuncs.h"
37 #include "gatomic.h"
38 #include "gtestutils.h"
39 #include "gslice.h"
42 /**
43 * SECTION:hash_tables
44 * @title: Hash Tables
45 * @short_description: associations between keys and values so that
46 * given a key the value can be found quickly
48 * A #GHashTable provides associations between keys and values which is
49 * optimized so that given a key, the associated value can be found
50 * very quickly.
52 * Note that neither keys nor values are copied when inserted into the
53 * #GHashTable, so they must exist for the lifetime of the #GHashTable.
54 * This means that the use of static strings is OK, but temporary
55 * strings (i.e. those created in buffers and those returned by GTK+
56 * widgets) should be copied with g_strdup() before being inserted.
58 * If keys or values are dynamically allocated, you must be careful to
59 * ensure that they are freed when they are removed from the
60 * #GHashTable, and also when they are overwritten by new insertions
61 * into the #GHashTable. It is also not advisable to mix static strings
62 * and dynamically-allocated strings in a #GHashTable, because it then
63 * becomes difficult to determine whether the string should be freed.
65 * To create a #GHashTable, use g_hash_table_new().
67 * To insert a key and value into a #GHashTable, use
68 * g_hash_table_insert().
70 * To lookup a value corresponding to a given key, use
71 * g_hash_table_lookup() and g_hash_table_lookup_extended().
73 * g_hash_table_lookup_extended() can also be used to simply
74 * check if a key is present in the hash table.
76 * To remove a key and value, use g_hash_table_remove().
78 * To call a function for each key and value pair use
79 * g_hash_table_foreach() or use a iterator to iterate over the
80 * key/value pairs in the hash table, see #GHashTableIter.
82 * To destroy a #GHashTable use g_hash_table_destroy().
84 * A common use-case for hash tables is to store information about a
85 * set of keys, without associating any particular value with each
86 * key. GHashTable optimizes one way of doing so: If you store only
87 * key-value pairs where key == value, then GHashTable does not
88 * allocate memory to store the values, which can be a considerable
89 * space saving, if your set is large. The functions
90 * g_hash_table_add() and g_hash_table_contains() are designed to be
91 * used when using #GHashTable this way.
94 /**
95 * GHashTable:
97 * The #GHashTable struct is an opaque data structure to represent a
98 * [Hash Table][glib-Hash-Tables]. It should only be accessed via the
99 * following functions.
103 * GHashFunc:
104 * @key: a key
106 * Specifies the type of the hash function which is passed to
107 * g_hash_table_new() when a #GHashTable is created.
109 * The function is passed a key and should return a #guint hash value.
110 * The functions g_direct_hash(), g_int_hash() and g_str_hash() provide
111 * hash functions which can be used when the key is a #gpointer, #gint*,
112 * and #gchar* respectively.
114 * g_direct_hash() is also the appropriate hash function for keys
115 * of the form `GINT_TO_POINTER (n)` (or similar macros).
117 * <!-- FIXME: Need more here. --> A good hash functions should produce
118 * hash values that are evenly distributed over a fairly large range.
119 * The modulus is taken with the hash table size (a prime number) to
120 * find the 'bucket' to place each key into. The function should also
121 * be very fast, since it is called for each key lookup.
123 * Note that the hash functions provided by GLib have these qualities,
124 * but are not particularly robust against manufactured keys that
125 * cause hash collisions. Therefore, you should consider choosing
126 * a more secure hash function when using a GHashTable with keys
127 * that originate in untrusted data (such as HTTP requests).
128 * Using g_str_hash() in that situation might make your application
129 * vulerable to
130 * [Algorithmic Complexity Attacks](https://lwn.net/Articles/474912/).
132 * The key to choosing a good hash is unpredictability. Even
133 * cryptographic hashes are very easy to find collisions for when the
134 * remainder is taken modulo a somewhat predictable prime number. There
135 * must be an element of randomness that an attacker is unable to guess.
137 * Returns: the hash value corresponding to the key
141 * GHFunc:
142 * @key: a key
143 * @value: the value corresponding to the key
144 * @user_data: user data passed to g_hash_table_foreach()
146 * Specifies the type of the function passed to g_hash_table_foreach().
147 * It is called with each key/value pair, together with the @user_data
148 * parameter which is passed to g_hash_table_foreach().
152 * GHRFunc:
153 * @key: a key
154 * @value: the value associated with the key
155 * @user_data: user data passed to g_hash_table_remove()
157 * Specifies the type of the function passed to
158 * g_hash_table_foreach_remove(). It is called with each key/value
159 * pair, together with the @user_data parameter passed to
160 * g_hash_table_foreach_remove(). It should return %TRUE if the
161 * key/value pair should be removed from the #GHashTable.
163 * Returns: %TRUE if the key/value pair should be removed from the
164 * #GHashTable
168 * GEqualFunc:
169 * @a: a value
170 * @b: a value to compare with
172 * Specifies the type of a function used to test two values for
173 * equality. The function should return %TRUE if both values are equal
174 * and %FALSE otherwise.
176 * Returns: %TRUE if @a = @b; %FALSE otherwise
180 * GHashTableIter:
182 * A GHashTableIter structure represents an iterator that can be used
183 * to iterate over the elements of a #GHashTable. GHashTableIter
184 * structures are typically allocated on the stack and then initialized
185 * with g_hash_table_iter_init().
189 * g_hash_table_freeze:
190 * @hash_table: a #GHashTable
192 * This function is deprecated and will be removed in the next major
193 * release of GLib. It does nothing.
197 * g_hash_table_thaw:
198 * @hash_table: a #GHashTable
200 * This function is deprecated and will be removed in the next major
201 * release of GLib. It does nothing.
204 #define HASH_TABLE_MIN_SHIFT 3 /* 1 << 3 == 8 buckets */
206 #define UNUSED_HASH_VALUE 0
207 #define TOMBSTONE_HASH_VALUE 1
208 #define HASH_IS_UNUSED(h_) ((h_) == UNUSED_HASH_VALUE)
209 #define HASH_IS_TOMBSTONE(h_) ((h_) == TOMBSTONE_HASH_VALUE)
210 #define HASH_IS_REAL(h_) ((h_) >= 2)
212 struct _GHashTable
214 gint size;
215 gint mod;
216 guint mask;
217 gint nnodes;
218 gint noccupied; /* nnodes + tombstones */
220 gpointer *keys;
221 guint *hashes;
222 gpointer *values;
224 GHashFunc hash_func;
225 GEqualFunc key_equal_func;
226 gint ref_count;
227 #ifndef G_DISABLE_ASSERT
229 * Tracks the structure of the hash table, not its contents: is only
230 * incremented when a node is added or removed (is not incremented
231 * when the key or data of a node is modified).
233 int version;
234 #endif
235 GDestroyNotify key_destroy_func;
236 GDestroyNotify value_destroy_func;
239 typedef struct
241 GHashTable *hash_table;
242 gpointer dummy1;
243 gpointer dummy2;
244 int position;
245 gboolean dummy3;
246 int version;
247 } RealIter;
249 G_STATIC_ASSERT (sizeof (GHashTableIter) == sizeof (RealIter));
250 G_STATIC_ASSERT (_g_alignof (GHashTableIter) >= _g_alignof (RealIter));
252 /* Each table size has an associated prime modulo (the first prime
253 * lower than the table size) used to find the initial bucket. Probing
254 * then works modulo 2^n. The prime modulo is necessary to get a
255 * good distribution with poor hash functions.
257 static const gint prime_mod [] =
259 1, /* For 1 << 0 */
266 127,
267 251,
268 509,
269 1021,
270 2039,
271 4093,
272 8191,
273 16381,
274 32749,
275 65521, /* For 1 << 16 */
276 131071,
277 262139,
278 524287,
279 1048573,
280 2097143,
281 4194301,
282 8388593,
283 16777213,
284 33554393,
285 67108859,
286 134217689,
287 268435399,
288 536870909,
289 1073741789,
290 2147483647 /* For 1 << 31 */
293 static void
294 g_hash_table_set_shift (GHashTable *hash_table, gint shift)
296 gint i;
297 guint mask = 0;
299 hash_table->size = 1 << shift;
300 hash_table->mod = prime_mod [shift];
302 for (i = 0; i < shift; i++)
304 mask <<= 1;
305 mask |= 1;
308 hash_table->mask = mask;
311 static gint
312 g_hash_table_find_closest_shift (gint n)
314 gint i;
316 for (i = 0; n; i++)
317 n >>= 1;
319 return i;
322 static void
323 g_hash_table_set_shift_from_size (GHashTable *hash_table, gint size)
325 gint shift;
327 shift = g_hash_table_find_closest_shift (size);
328 shift = MAX (shift, HASH_TABLE_MIN_SHIFT);
330 g_hash_table_set_shift (hash_table, shift);
334 * g_hash_table_lookup_node:
335 * @hash_table: our #GHashTable
336 * @key: the key to lookup against
337 * @hash_return: key hash return location
339 * Performs a lookup in the hash table, preserving extra information
340 * usually needed for insertion.
342 * This function first computes the hash value of the key using the
343 * user's hash function.
345 * If an entry in the table matching @key is found then this function
346 * returns the index of that entry in the table, and if not, the
347 * index of an unused node (empty or tombstone) where the key can be
348 * inserted.
350 * The computed hash value is returned in the variable pointed to
351 * by @hash_return. This is to save insertions from having to compute
352 * the hash record again for the new record.
354 * Returns: index of the described node
356 static inline guint
357 g_hash_table_lookup_node (GHashTable *hash_table,
358 gconstpointer key,
359 guint *hash_return)
361 guint node_index;
362 guint node_hash;
363 guint hash_value;
364 guint first_tombstone = 0;
365 gboolean have_tombstone = FALSE;
366 guint step = 0;
368 /* If this happens, then the application is probably doing too much work
369 * from a destroy notifier. The alternative would be to crash any second
370 * (as keys, etc. will be NULL).
371 * Applications need to either use g_hash_table_destroy, or ensure the hash
372 * table is empty prior to removing the last reference using g_hash_table_unref(). */
373 g_assert (hash_table->ref_count > 0);
375 hash_value = hash_table->hash_func (key);
376 if (G_UNLIKELY (!HASH_IS_REAL (hash_value)))
377 hash_value = 2;
379 *hash_return = hash_value;
381 node_index = hash_value % hash_table->mod;
382 node_hash = hash_table->hashes[node_index];
384 while (!HASH_IS_UNUSED (node_hash))
386 /* We first check if our full hash values
387 * are equal so we can avoid calling the full-blown
388 * key equality function in most cases.
390 if (node_hash == hash_value)
392 gpointer node_key = hash_table->keys[node_index];
394 if (hash_table->key_equal_func)
396 if (hash_table->key_equal_func (node_key, key))
397 return node_index;
399 else if (node_key == key)
401 return node_index;
404 else if (HASH_IS_TOMBSTONE (node_hash) && !have_tombstone)
406 first_tombstone = node_index;
407 have_tombstone = TRUE;
410 step++;
411 node_index += step;
412 node_index &= hash_table->mask;
413 node_hash = hash_table->hashes[node_index];
416 if (have_tombstone)
417 return first_tombstone;
419 return node_index;
423 * g_hash_table_remove_node:
424 * @hash_table: our #GHashTable
425 * @node: pointer to node to remove
426 * @notify: %TRUE if the destroy notify handlers are to be called
428 * Removes a node from the hash table and updates the node count.
429 * The node is replaced by a tombstone. No table resize is performed.
431 * If @notify is %TRUE then the destroy notify functions are called
432 * for the key and value of the hash node.
434 static void
435 g_hash_table_remove_node (GHashTable *hash_table,
436 gint i,
437 gboolean notify)
439 gpointer key;
440 gpointer value;
442 key = hash_table->keys[i];
443 value = hash_table->values[i];
445 /* Erect tombstone */
446 hash_table->hashes[i] = TOMBSTONE_HASH_VALUE;
448 /* Be GC friendly */
449 hash_table->keys[i] = NULL;
450 hash_table->values[i] = NULL;
452 hash_table->nnodes--;
454 if (notify && hash_table->key_destroy_func)
455 hash_table->key_destroy_func (key);
457 if (notify && hash_table->value_destroy_func)
458 hash_table->value_destroy_func (value);
463 * g_hash_table_remove_all_nodes:
464 * @hash_table: our #GHashTable
465 * @notify: %TRUE if the destroy notify handlers are to be called
467 * Removes all nodes from the table. Since this may be a precursor to
468 * freeing the table entirely, no resize is performed.
470 * If @notify is %TRUE then the destroy notify functions are called
471 * for the key and value of the hash node.
473 static void
474 g_hash_table_remove_all_nodes (GHashTable *hash_table,
475 gboolean notify,
476 gboolean destruction)
478 int i;
479 gpointer key;
480 gpointer value;
481 gint old_size;
482 gpointer *old_keys;
483 gpointer *old_values;
484 guint *old_hashes;
486 /* If the hash table is already empty, there is nothing to be done. */
487 if (hash_table->nnodes == 0)
488 return;
490 hash_table->nnodes = 0;
491 hash_table->noccupied = 0;
493 if (!notify ||
494 (hash_table->key_destroy_func == NULL &&
495 hash_table->value_destroy_func == NULL))
497 if (!destruction)
499 memset (hash_table->hashes, 0, hash_table->size * sizeof (guint));
500 memset (hash_table->keys, 0, hash_table->size * sizeof (gpointer));
501 memset (hash_table->values, 0, hash_table->size * sizeof (gpointer));
504 return;
507 /* Keep the old storage space around to iterate over it. */
508 old_size = hash_table->size;
509 old_keys = hash_table->keys;
510 old_values = hash_table->values;
511 old_hashes = hash_table->hashes;
513 /* Now create a new storage space; If the table is destroyed we can use the
514 * shortcut of not creating a new storage. This saves the allocation at the
515 * cost of not allowing any recursive access.
516 * However, the application doesn't own any reference anymore, so access
517 * is not allowed. If accesses are done, then either an assert or crash
518 * *will* happen. */
519 g_hash_table_set_shift (hash_table, HASH_TABLE_MIN_SHIFT);
520 if (!destruction)
522 hash_table->keys = g_new0 (gpointer, hash_table->size);
523 hash_table->values = hash_table->keys;
524 hash_table->hashes = g_new0 (guint, hash_table->size);
526 else
528 hash_table->keys = NULL;
529 hash_table->values = NULL;
530 hash_table->hashes = NULL;
533 for (i = 0; i < old_size; i++)
535 if (HASH_IS_REAL (old_hashes[i]))
537 key = old_keys[i];
538 value = old_values[i];
540 old_hashes[i] = UNUSED_HASH_VALUE;
541 old_keys[i] = NULL;
542 old_values[i] = NULL;
544 if (hash_table->key_destroy_func != NULL)
545 hash_table->key_destroy_func (key);
547 if (hash_table->value_destroy_func != NULL)
548 hash_table->value_destroy_func (value);
552 /* Destroy old storage space. */
553 if (old_keys != old_values)
554 g_free (old_values);
556 g_free (old_keys);
557 g_free (old_hashes);
561 * g_hash_table_resize:
562 * @hash_table: our #GHashTable
564 * Resizes the hash table to the optimal size based on the number of
565 * nodes currently held. If you call this function then a resize will
566 * occur, even if one does not need to occur.
567 * Use g_hash_table_maybe_resize() instead.
569 * This function may "resize" the hash table to its current size, with
570 * the side effect of cleaning up tombstones and otherwise optimizing
571 * the probe sequences.
573 static void
574 g_hash_table_resize (GHashTable *hash_table)
576 gpointer *new_keys;
577 gpointer *new_values;
578 guint *new_hashes;
579 gint old_size;
580 gint i;
582 old_size = hash_table->size;
583 g_hash_table_set_shift_from_size (hash_table, hash_table->nnodes * 2);
585 new_keys = g_new0 (gpointer, hash_table->size);
586 if (hash_table->keys == hash_table->values)
587 new_values = new_keys;
588 else
589 new_values = g_new0 (gpointer, hash_table->size);
590 new_hashes = g_new0 (guint, hash_table->size);
592 for (i = 0; i < old_size; i++)
594 guint node_hash = hash_table->hashes[i];
595 guint hash_val;
596 guint step = 0;
598 if (!HASH_IS_REAL (node_hash))
599 continue;
601 hash_val = node_hash % hash_table->mod;
603 while (!HASH_IS_UNUSED (new_hashes[hash_val]))
605 step++;
606 hash_val += step;
607 hash_val &= hash_table->mask;
610 new_hashes[hash_val] = hash_table->hashes[i];
611 new_keys[hash_val] = hash_table->keys[i];
612 new_values[hash_val] = hash_table->values[i];
615 if (hash_table->keys != hash_table->values)
616 g_free (hash_table->values);
618 g_free (hash_table->keys);
619 g_free (hash_table->hashes);
621 hash_table->keys = new_keys;
622 hash_table->values = new_values;
623 hash_table->hashes = new_hashes;
625 hash_table->noccupied = hash_table->nnodes;
629 * g_hash_table_maybe_resize:
630 * @hash_table: our #GHashTable
632 * Resizes the hash table, if needed.
634 * Essentially, calls g_hash_table_resize() if the table has strayed
635 * too far from its ideal size for its number of nodes.
637 static inline void
638 g_hash_table_maybe_resize (GHashTable *hash_table)
640 gint noccupied = hash_table->noccupied;
641 gint size = hash_table->size;
643 if ((size > hash_table->nnodes * 4 && size > 1 << HASH_TABLE_MIN_SHIFT) ||
644 (size <= noccupied + (noccupied / 16)))
645 g_hash_table_resize (hash_table);
649 * g_hash_table_new:
650 * @hash_func: a function to create a hash value from a key
651 * @key_equal_func: a function to check two keys for equality
653 * Creates a new #GHashTable with a reference count of 1.
655 * Hash values returned by @hash_func are used to determine where keys
656 * are stored within the #GHashTable data structure. The g_direct_hash(),
657 * g_int_hash(), g_int64_hash(), g_double_hash() and g_str_hash()
658 * functions are provided for some common types of keys.
659 * If @hash_func is %NULL, g_direct_hash() is used.
661 * @key_equal_func is used when looking up keys in the #GHashTable.
662 * The g_direct_equal(), g_int_equal(), g_int64_equal(), g_double_equal()
663 * and g_str_equal() functions are provided for the most common types
664 * of keys. If @key_equal_func is %NULL, keys are compared directly in
665 * a similar fashion to g_direct_equal(), but without the overhead of
666 * a function call.
668 * Returns: a new #GHashTable
670 GHashTable *
671 g_hash_table_new (GHashFunc hash_func,
672 GEqualFunc key_equal_func)
674 return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
679 * g_hash_table_new_full:
680 * @hash_func: a function to create a hash value from a key
681 * @key_equal_func: a function to check two keys for equality
682 * @key_destroy_func: (allow-none): a function to free the memory allocated for the key
683 * used when removing the entry from the #GHashTable, or %NULL
684 * if you don't want to supply such a function.
685 * @value_destroy_func: (allow-none): a function to free the memory allocated for the
686 * value used when removing the entry from the #GHashTable, or %NULL
687 * if you don't want to supply such a function.
689 * Creates a new #GHashTable like g_hash_table_new() with a reference
690 * count of 1 and allows to specify functions to free the memory
691 * allocated for the key and value that get called when removing the
692 * entry from the #GHashTable.
694 * Since version 2.42 it is permissible for destroy notify functions to
695 * recursively remove further items from the hash table. This is only
696 * permissible if the application still holds a reference to the hash table.
697 * This means that you may need to ensure that the hash table is empty by
698 * calling g_hash_table_remove_all before releasing the last reference using
699 * g_hash_table_unref().
701 * Returns: a new #GHashTable
703 GHashTable *
704 g_hash_table_new_full (GHashFunc hash_func,
705 GEqualFunc key_equal_func,
706 GDestroyNotify key_destroy_func,
707 GDestroyNotify value_destroy_func)
709 GHashTable *hash_table;
711 hash_table = g_slice_new (GHashTable);
712 g_hash_table_set_shift (hash_table, HASH_TABLE_MIN_SHIFT);
713 hash_table->nnodes = 0;
714 hash_table->noccupied = 0;
715 hash_table->hash_func = hash_func ? hash_func : g_direct_hash;
716 hash_table->key_equal_func = key_equal_func;
717 hash_table->ref_count = 1;
718 #ifndef G_DISABLE_ASSERT
719 hash_table->version = 0;
720 #endif
721 hash_table->key_destroy_func = key_destroy_func;
722 hash_table->value_destroy_func = value_destroy_func;
723 hash_table->keys = g_new0 (gpointer, hash_table->size);
724 hash_table->values = hash_table->keys;
725 hash_table->hashes = g_new0 (guint, hash_table->size);
727 return hash_table;
731 * g_hash_table_iter_init:
732 * @iter: an uninitialized #GHashTableIter
733 * @hash_table: a #GHashTable
735 * Initializes a key/value pair iterator and associates it with
736 * @hash_table. Modifying the hash table after calling this function
737 * invalidates the returned iterator.
738 * |[<!-- language="C" -->
739 * GHashTableIter iter;
740 * gpointer key, value;
742 * g_hash_table_iter_init (&iter, hash_table);
743 * while (g_hash_table_iter_next (&iter, &key, &value))
745 * // do something with key and value
747 * ]|
749 * Since: 2.16
751 void
752 g_hash_table_iter_init (GHashTableIter *iter,
753 GHashTable *hash_table)
755 RealIter *ri = (RealIter *) iter;
757 g_return_if_fail (iter != NULL);
758 g_return_if_fail (hash_table != NULL);
760 ri->hash_table = hash_table;
761 ri->position = -1;
762 #ifndef G_DISABLE_ASSERT
763 ri->version = hash_table->version;
764 #endif
768 * g_hash_table_iter_next:
769 * @iter: an initialized #GHashTableIter
770 * @key: (allow-none): a location to store the key, or %NULL
771 * @value: (allow-none): a location to store the value, or %NULL
773 * Advances @iter and retrieves the key and/or value that are now
774 * pointed to as a result of this advancement. If %FALSE is returned,
775 * @key and @value are not set, and the iterator becomes invalid.
777 * Returns: %FALSE if the end of the #GHashTable has been reached.
779 * Since: 2.16
781 gboolean
782 g_hash_table_iter_next (GHashTableIter *iter,
783 gpointer *key,
784 gpointer *value)
786 RealIter *ri = (RealIter *) iter;
787 gint position;
789 g_return_val_if_fail (iter != NULL, FALSE);
790 #ifndef G_DISABLE_ASSERT
791 g_return_val_if_fail (ri->version == ri->hash_table->version, FALSE);
792 #endif
793 g_return_val_if_fail (ri->position < ri->hash_table->size, FALSE);
795 position = ri->position;
799 position++;
800 if (position >= ri->hash_table->size)
802 ri->position = position;
803 return FALSE;
806 while (!HASH_IS_REAL (ri->hash_table->hashes[position]));
808 if (key != NULL)
809 *key = ri->hash_table->keys[position];
810 if (value != NULL)
811 *value = ri->hash_table->values[position];
813 ri->position = position;
814 return TRUE;
818 * g_hash_table_iter_get_hash_table:
819 * @iter: an initialized #GHashTableIter
821 * Returns the #GHashTable associated with @iter.
823 * Returns: the #GHashTable associated with @iter.
825 * Since: 2.16
827 GHashTable *
828 g_hash_table_iter_get_hash_table (GHashTableIter *iter)
830 g_return_val_if_fail (iter != NULL, NULL);
832 return ((RealIter *) iter)->hash_table;
835 static void
836 iter_remove_or_steal (RealIter *ri, gboolean notify)
838 g_return_if_fail (ri != NULL);
839 #ifndef G_DISABLE_ASSERT
840 g_return_if_fail (ri->version == ri->hash_table->version);
841 #endif
842 g_return_if_fail (ri->position >= 0);
843 g_return_if_fail (ri->position < ri->hash_table->size);
845 g_hash_table_remove_node (ri->hash_table, ri->position, notify);
847 #ifndef G_DISABLE_ASSERT
848 ri->version++;
849 ri->hash_table->version++;
850 #endif
854 * g_hash_table_iter_remove:
855 * @iter: an initialized #GHashTableIter
857 * Removes the key/value pair currently pointed to by the iterator
858 * from its associated #GHashTable. Can only be called after
859 * g_hash_table_iter_next() returned %TRUE, and cannot be called
860 * more than once for the same key/value pair.
862 * If the #GHashTable was created using g_hash_table_new_full(),
863 * the key and value are freed using the supplied destroy functions,
864 * otherwise you have to make sure that any dynamically allocated
865 * values are freed yourself.
867 * It is safe to continue iterating the #GHashTable afterward:
868 * |[<!-- language="C" -->
869 * while (g_hash_table_iter_next (&iter, &key, &value))
871 * if (condition)
872 * g_hash_table_iter_remove (&iter);
874 * ]|
876 * Since: 2.16
878 void
879 g_hash_table_iter_remove (GHashTableIter *iter)
881 iter_remove_or_steal ((RealIter *) iter, TRUE);
885 * g_hash_table_insert_node:
886 * @hash_table: our #GHashTable
887 * @node_index: pointer to node to insert/replace
888 * @key_hash: key hash
889 * @key: (allow-none): key to replace with, or %NULL
890 * @value: value to replace with
891 * @keep_new_key: whether to replace the key in the node with @key
892 * @reusing_key: whether @key was taken out of the existing node
894 * Inserts a value at @node_index in the hash table and updates it.
896 * If @key has been taken out of the existing node (ie it is not
897 * passed in via a g_hash_table_insert/replace) call, then @reusing_key
898 * should be %TRUE.
900 * Returns: %TRUE if the key did not exist yet
902 static gboolean
903 g_hash_table_insert_node (GHashTable *hash_table,
904 guint node_index,
905 guint key_hash,
906 gpointer new_key,
907 gpointer new_value,
908 gboolean keep_new_key,
909 gboolean reusing_key)
911 gboolean already_exists;
912 guint old_hash;
913 gpointer key_to_free = NULL;
914 gpointer value_to_free = NULL;
916 old_hash = hash_table->hashes[node_index];
917 already_exists = HASH_IS_REAL (old_hash);
919 /* Proceed in three steps. First, deal with the key because it is the
920 * most complicated. Then consider if we need to split the table in
921 * two (because writing the value will result in the set invariant
922 * becoming broken). Then deal with the value.
924 * There are three cases for the key:
926 * - entry already exists in table, reusing key:
927 * free the just-passed-in new_key and use the existing value
929 * - entry already exists in table, not reusing key:
930 * free the entry in the table, use the new key
932 * - entry not already in table:
933 * use the new key, free nothing
935 * We update the hash at the same time...
937 if (already_exists)
939 /* Note: we must record the old value before writing the new key
940 * because we might change the value in the event that the two
941 * arrays are shared.
943 value_to_free = hash_table->values[node_index];
945 if (keep_new_key)
947 key_to_free = hash_table->keys[node_index];
948 hash_table->keys[node_index] = new_key;
950 else
951 key_to_free = new_key;
953 else
955 hash_table->hashes[node_index] = key_hash;
956 hash_table->keys[node_index] = new_key;
959 /* Step two: check if the value that we are about to write to the
960 * table is the same as the key in the same position. If it's not,
961 * split the table.
963 if (G_UNLIKELY (hash_table->keys == hash_table->values && hash_table->keys[node_index] != new_value))
964 hash_table->values = g_memdup (hash_table->keys, sizeof (gpointer) * hash_table->size);
966 /* Step 3: Actually do the write */
967 hash_table->values[node_index] = new_value;
969 /* Now, the bookkeeping... */
970 if (!already_exists)
972 hash_table->nnodes++;
974 if (HASH_IS_UNUSED (old_hash))
976 /* We replaced an empty node, and not a tombstone */
977 hash_table->noccupied++;
978 g_hash_table_maybe_resize (hash_table);
981 #ifndef G_DISABLE_ASSERT
982 hash_table->version++;
983 #endif
986 if (already_exists)
988 if (hash_table->key_destroy_func && !reusing_key)
989 (* hash_table->key_destroy_func) (key_to_free);
990 if (hash_table->value_destroy_func)
991 (* hash_table->value_destroy_func) (value_to_free);
994 return !already_exists;
998 * g_hash_table_iter_replace:
999 * @iter: an initialized #GHashTableIter
1000 * @value: the value to replace with
1002 * Replaces the value currently pointed to by the iterator
1003 * from its associated #GHashTable. Can only be called after
1004 * g_hash_table_iter_next() returned %TRUE.
1006 * If you supplied a @value_destroy_func when creating the
1007 * #GHashTable, the old value is freed using that function.
1009 * Since: 2.30
1011 void
1012 g_hash_table_iter_replace (GHashTableIter *iter,
1013 gpointer value)
1015 RealIter *ri;
1016 guint node_hash;
1017 gpointer key;
1019 ri = (RealIter *) iter;
1021 g_return_if_fail (ri != NULL);
1022 #ifndef G_DISABLE_ASSERT
1023 g_return_if_fail (ri->version == ri->hash_table->version);
1024 #endif
1025 g_return_if_fail (ri->position >= 0);
1026 g_return_if_fail (ri->position < ri->hash_table->size);
1028 node_hash = ri->hash_table->hashes[ri->position];
1029 key = ri->hash_table->keys[ri->position];
1031 g_hash_table_insert_node (ri->hash_table, ri->position, node_hash, key, value, TRUE, TRUE);
1033 #ifndef G_DISABLE_ASSERT
1034 ri->version++;
1035 ri->hash_table->version++;
1036 #endif
1040 * g_hash_table_iter_steal:
1041 * @iter: an initialized #GHashTableIter
1043 * Removes the key/value pair currently pointed to by the
1044 * iterator from its associated #GHashTable, without calling
1045 * the key and value destroy functions. Can only be called
1046 * after g_hash_table_iter_next() returned %TRUE, and cannot
1047 * be called more than once for the same key/value pair.
1049 * Since: 2.16
1051 void
1052 g_hash_table_iter_steal (GHashTableIter *iter)
1054 iter_remove_or_steal ((RealIter *) iter, FALSE);
1059 * g_hash_table_ref:
1060 * @hash_table: a valid #GHashTable
1062 * Atomically increments the reference count of @hash_table by one.
1063 * This function is MT-safe and may be called from any thread.
1065 * Returns: the passed in #GHashTable
1067 * Since: 2.10
1069 GHashTable *
1070 g_hash_table_ref (GHashTable *hash_table)
1072 g_return_val_if_fail (hash_table != NULL, NULL);
1074 g_atomic_int_inc (&hash_table->ref_count);
1076 return hash_table;
1080 * g_hash_table_unref:
1081 * @hash_table: a valid #GHashTable
1083 * Atomically decrements the reference count of @hash_table by one.
1084 * If the reference count drops to 0, all keys and values will be
1085 * destroyed, and all memory allocated by the hash table is released.
1086 * This function is MT-safe and may be called from any thread.
1088 * Since: 2.10
1090 void
1091 g_hash_table_unref (GHashTable *hash_table)
1093 g_return_if_fail (hash_table != NULL);
1095 if (g_atomic_int_dec_and_test (&hash_table->ref_count))
1097 g_hash_table_remove_all_nodes (hash_table, TRUE, TRUE);
1098 if (hash_table->keys != hash_table->values)
1099 g_free (hash_table->values);
1100 g_free (hash_table->keys);
1101 g_free (hash_table->hashes);
1102 g_slice_free (GHashTable, hash_table);
1107 * g_hash_table_destroy:
1108 * @hash_table: a #GHashTable
1110 * Destroys all keys and values in the #GHashTable and decrements its
1111 * reference count by 1. If keys and/or values are dynamically allocated,
1112 * you should either free them first or create the #GHashTable with destroy
1113 * notifiers using g_hash_table_new_full(). In the latter case the destroy
1114 * functions you supplied will be called on all keys and values during the
1115 * destruction phase.
1117 void
1118 g_hash_table_destroy (GHashTable *hash_table)
1120 g_return_if_fail (hash_table != NULL);
1122 g_hash_table_remove_all (hash_table);
1123 g_hash_table_unref (hash_table);
1127 * g_hash_table_lookup:
1128 * @hash_table: a #GHashTable
1129 * @key: the key to look up
1131 * Looks up a key in a #GHashTable. Note that this function cannot
1132 * distinguish between a key that is not present and one which is present
1133 * and has the value %NULL. If you need this distinction, use
1134 * g_hash_table_lookup_extended().
1136 * Returns: (allow-none): the associated value, or %NULL if the key is not found
1138 gpointer
1139 g_hash_table_lookup (GHashTable *hash_table,
1140 gconstpointer key)
1142 guint node_index;
1143 guint node_hash;
1145 g_return_val_if_fail (hash_table != NULL, NULL);
1147 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1149 return HASH_IS_REAL (hash_table->hashes[node_index])
1150 ? hash_table->values[node_index]
1151 : NULL;
1155 * g_hash_table_lookup_extended:
1156 * @hash_table: a #GHashTable
1157 * @lookup_key: the key to look up
1158 * @orig_key: (allow-none): return location for the original key, or %NULL
1159 * @value: (allow-none): return location for the value associated with the key, or %NULL
1161 * Looks up a key in the #GHashTable, returning the original key and the
1162 * associated value and a #gboolean which is %TRUE if the key was found. This
1163 * is useful if you need to free the memory allocated for the original key,
1164 * for example before calling g_hash_table_remove().
1166 * You can actually pass %NULL for @lookup_key to test
1167 * whether the %NULL key exists, provided the hash and equal functions
1168 * of @hash_table are %NULL-safe.
1170 * Returns: %TRUE if the key was found in the #GHashTable
1172 gboolean
1173 g_hash_table_lookup_extended (GHashTable *hash_table,
1174 gconstpointer lookup_key,
1175 gpointer *orig_key,
1176 gpointer *value)
1178 guint node_index;
1179 guint node_hash;
1181 g_return_val_if_fail (hash_table != NULL, FALSE);
1183 node_index = g_hash_table_lookup_node (hash_table, lookup_key, &node_hash);
1185 if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1186 return FALSE;
1188 if (orig_key)
1189 *orig_key = hash_table->keys[node_index];
1191 if (value)
1192 *value = hash_table->values[node_index];
1194 return TRUE;
1198 * g_hash_table_insert_internal:
1199 * @hash_table: our #GHashTable
1200 * @key: the key to insert
1201 * @value: the value to insert
1202 * @keep_new_key: if %TRUE and this key already exists in the table
1203 * then call the destroy notify function on the old key. If %FALSE
1204 * then call the destroy notify function on the new key.
1206 * Implements the common logic for the g_hash_table_insert() and
1207 * g_hash_table_replace() functions.
1209 * Do a lookup of @key. If it is found, replace it with the new
1210 * @value (and perhaps the new @key). If it is not found, create
1211 * a new node.
1213 * Returns: %TRUE if the key did not exist yet
1215 static gboolean
1216 g_hash_table_insert_internal (GHashTable *hash_table,
1217 gpointer key,
1218 gpointer value,
1219 gboolean keep_new_key)
1221 guint key_hash;
1222 guint node_index;
1224 g_return_val_if_fail (hash_table != NULL, FALSE);
1226 node_index = g_hash_table_lookup_node (hash_table, key, &key_hash);
1228 return g_hash_table_insert_node (hash_table, node_index, key_hash, key, value, keep_new_key, FALSE);
1232 * g_hash_table_insert:
1233 * @hash_table: a #GHashTable
1234 * @key: a key to insert
1235 * @value: the value to associate with the key
1237 * Inserts a new key and value into a #GHashTable.
1239 * If the key already exists in the #GHashTable its current
1240 * value is replaced with the new value. If you supplied a
1241 * @value_destroy_func when creating the #GHashTable, the old
1242 * value is freed using that function. If you supplied a
1243 * @key_destroy_func when creating the #GHashTable, the passed
1244 * key is freed using that function.
1246 * Returns: %TRUE if the key did not exist yet
1248 gboolean
1249 g_hash_table_insert (GHashTable *hash_table,
1250 gpointer key,
1251 gpointer value)
1253 return g_hash_table_insert_internal (hash_table, key, value, FALSE);
1257 * g_hash_table_replace:
1258 * @hash_table: a #GHashTable
1259 * @key: a key to insert
1260 * @value: the value to associate with the key
1262 * Inserts a new key and value into a #GHashTable similar to
1263 * g_hash_table_insert(). The difference is that if the key
1264 * already exists in the #GHashTable, it gets replaced by the
1265 * new key. If you supplied a @value_destroy_func when creating
1266 * the #GHashTable, the old value is freed using that function.
1267 * If you supplied a @key_destroy_func when creating the
1268 * #GHashTable, the old key is freed using that function.
1270 * Returns: %TRUE of the key did not exist yet
1272 gboolean
1273 g_hash_table_replace (GHashTable *hash_table,
1274 gpointer key,
1275 gpointer value)
1277 return g_hash_table_insert_internal (hash_table, key, value, TRUE);
1281 * g_hash_table_add:
1282 * @hash_table: a #GHashTable
1283 * @key: a key to insert
1285 * This is a convenience function for using a #GHashTable as a set. It
1286 * is equivalent to calling g_hash_table_replace() with @key as both the
1287 * key and the value.
1289 * When a hash table only ever contains keys that have themselves as the
1290 * corresponding value it is able to be stored more efficiently. See
1291 * the discussion in the section description.
1293 * Returns: %TRUE if the key did not exist yet
1295 * Since: 2.32
1297 gboolean
1298 g_hash_table_add (GHashTable *hash_table,
1299 gpointer key)
1301 return g_hash_table_insert_internal (hash_table, key, key, TRUE);
1305 * g_hash_table_contains:
1306 * @hash_table: a #GHashTable
1307 * @key: a key to check
1309 * Checks if @key is in @hash_table.
1311 * Returns: %TRUE if @key is in @hash_table, %FALSE otherwise.
1313 * Since: 2.32
1315 gboolean
1316 g_hash_table_contains (GHashTable *hash_table,
1317 gconstpointer key)
1319 guint node_index;
1320 guint node_hash;
1322 g_return_val_if_fail (hash_table != NULL, FALSE);
1324 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1326 return HASH_IS_REAL (hash_table->hashes[node_index]);
1330 * g_hash_table_remove_internal:
1331 * @hash_table: our #GHashTable
1332 * @key: the key to remove
1333 * @notify: %TRUE if the destroy notify handlers are to be called
1334 * Returns: %TRUE if a node was found and removed, else %FALSE
1336 * Implements the common logic for the g_hash_table_remove() and
1337 * g_hash_table_steal() functions.
1339 * Do a lookup of @key and remove it if it is found, calling the
1340 * destroy notify handlers only if @notify is %TRUE.
1342 static gboolean
1343 g_hash_table_remove_internal (GHashTable *hash_table,
1344 gconstpointer key,
1345 gboolean notify)
1347 guint node_index;
1348 guint node_hash;
1350 g_return_val_if_fail (hash_table != NULL, FALSE);
1352 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1354 if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1355 return FALSE;
1357 g_hash_table_remove_node (hash_table, node_index, notify);
1358 g_hash_table_maybe_resize (hash_table);
1360 #ifndef G_DISABLE_ASSERT
1361 hash_table->version++;
1362 #endif
1364 return TRUE;
1368 * g_hash_table_remove:
1369 * @hash_table: a #GHashTable
1370 * @key: the key to remove
1372 * Removes a key and its associated value from a #GHashTable.
1374 * If the #GHashTable was created using g_hash_table_new_full(), the
1375 * key and value are freed using the supplied destroy functions, otherwise
1376 * you have to make sure that any dynamically allocated values are freed
1377 * yourself.
1379 * Returns: %TRUE if the key was found and removed from the #GHashTable
1381 gboolean
1382 g_hash_table_remove (GHashTable *hash_table,
1383 gconstpointer key)
1385 return g_hash_table_remove_internal (hash_table, key, TRUE);
1389 * g_hash_table_steal:
1390 * @hash_table: a #GHashTable
1391 * @key: the key to remove
1393 * Removes a key and its associated value from a #GHashTable without
1394 * calling the key and value destroy functions.
1396 * Returns: %TRUE if the key was found and removed from the #GHashTable
1398 gboolean
1399 g_hash_table_steal (GHashTable *hash_table,
1400 gconstpointer key)
1402 return g_hash_table_remove_internal (hash_table, key, FALSE);
1406 * g_hash_table_remove_all:
1407 * @hash_table: a #GHashTable
1409 * Removes all keys and their associated values from a #GHashTable.
1411 * If the #GHashTable was created using g_hash_table_new_full(),
1412 * the keys and values are freed using the supplied destroy functions,
1413 * otherwise you have to make sure that any dynamically allocated
1414 * values are freed yourself.
1416 * Since: 2.12
1418 void
1419 g_hash_table_remove_all (GHashTable *hash_table)
1421 g_return_if_fail (hash_table != NULL);
1423 #ifndef G_DISABLE_ASSERT
1424 if (hash_table->nnodes != 0)
1425 hash_table->version++;
1426 #endif
1428 g_hash_table_remove_all_nodes (hash_table, TRUE, FALSE);
1429 g_hash_table_maybe_resize (hash_table);
1433 * g_hash_table_steal_all:
1434 * @hash_table: a #GHashTable
1436 * Removes all keys and their associated values from a #GHashTable
1437 * without calling the key and value destroy functions.
1439 * Since: 2.12
1441 void
1442 g_hash_table_steal_all (GHashTable *hash_table)
1444 g_return_if_fail (hash_table != NULL);
1446 #ifndef G_DISABLE_ASSERT
1447 if (hash_table->nnodes != 0)
1448 hash_table->version++;
1449 #endif
1451 g_hash_table_remove_all_nodes (hash_table, FALSE, FALSE);
1452 g_hash_table_maybe_resize (hash_table);
1456 * g_hash_table_foreach_remove_or_steal:
1457 * @hash_table: a #GHashTable
1458 * @func: the user's callback function
1459 * @user_data: data for @func
1460 * @notify: %TRUE if the destroy notify handlers are to be called
1462 * Implements the common logic for g_hash_table_foreach_remove()
1463 * and g_hash_table_foreach_steal().
1465 * Iterates over every node in the table, calling @func with the key
1466 * and value of the node (and @user_data). If @func returns %TRUE the
1467 * node is removed from the table.
1469 * If @notify is true then the destroy notify handlers will be called
1470 * for each removed node.
1472 static guint
1473 g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
1474 GHRFunc func,
1475 gpointer user_data,
1476 gboolean notify)
1478 guint deleted = 0;
1479 gint i;
1480 #ifndef G_DISABLE_ASSERT
1481 gint version = hash_table->version;
1482 #endif
1484 for (i = 0; i < hash_table->size; i++)
1486 guint node_hash = hash_table->hashes[i];
1487 gpointer node_key = hash_table->keys[i];
1488 gpointer node_value = hash_table->values[i];
1490 if (HASH_IS_REAL (node_hash) &&
1491 (* func) (node_key, node_value, user_data))
1493 g_hash_table_remove_node (hash_table, i, notify);
1494 deleted++;
1497 #ifndef G_DISABLE_ASSERT
1498 g_return_val_if_fail (version == hash_table->version, 0);
1499 #endif
1502 g_hash_table_maybe_resize (hash_table);
1504 #ifndef G_DISABLE_ASSERT
1505 if (deleted > 0)
1506 hash_table->version++;
1507 #endif
1509 return deleted;
1513 * g_hash_table_foreach_remove:
1514 * @hash_table: a #GHashTable
1515 * @func: the function to call for each key/value pair
1516 * @user_data: user data to pass to the function
1518 * Calls the given function for each key/value pair in the
1519 * #GHashTable. If the function returns %TRUE, then the key/value
1520 * pair is removed from the #GHashTable. If you supplied key or
1521 * value destroy functions when creating the #GHashTable, they are
1522 * used to free the memory allocated for the removed keys and values.
1524 * See #GHashTableIter for an alternative way to loop over the
1525 * key/value pairs in the hash table.
1527 * Returns: the number of key/value pairs removed
1529 guint
1530 g_hash_table_foreach_remove (GHashTable *hash_table,
1531 GHRFunc func,
1532 gpointer user_data)
1534 g_return_val_if_fail (hash_table != NULL, 0);
1535 g_return_val_if_fail (func != NULL, 0);
1537 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
1541 * g_hash_table_foreach_steal:
1542 * @hash_table: a #GHashTable
1543 * @func: the function to call for each key/value pair
1544 * @user_data: user data to pass to the function
1546 * Calls the given function for each key/value pair in the
1547 * #GHashTable. If the function returns %TRUE, then the key/value
1548 * pair is removed from the #GHashTable, but no key or value
1549 * destroy functions are called.
1551 * See #GHashTableIter for an alternative way to loop over the
1552 * key/value pairs in the hash table.
1554 * Returns: the number of key/value pairs removed.
1556 guint
1557 g_hash_table_foreach_steal (GHashTable *hash_table,
1558 GHRFunc func,
1559 gpointer user_data)
1561 g_return_val_if_fail (hash_table != NULL, 0);
1562 g_return_val_if_fail (func != NULL, 0);
1564 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
1568 * g_hash_table_foreach:
1569 * @hash_table: a #GHashTable
1570 * @func: the function to call for each key/value pair
1571 * @user_data: user data to pass to the function
1573 * Calls the given function for each of the key/value pairs in the
1574 * #GHashTable. The function is passed the key and value of each
1575 * pair, and the given @user_data parameter. The hash table may not
1576 * be modified while iterating over it (you can't add/remove
1577 * items). To remove all items matching a predicate, use
1578 * g_hash_table_foreach_remove().
1580 * See g_hash_table_find() for performance caveats for linear
1581 * order searches in contrast to g_hash_table_lookup().
1583 void
1584 g_hash_table_foreach (GHashTable *hash_table,
1585 GHFunc func,
1586 gpointer user_data)
1588 gint i;
1589 #ifndef G_DISABLE_ASSERT
1590 gint version;
1591 #endif
1593 g_return_if_fail (hash_table != NULL);
1594 g_return_if_fail (func != NULL);
1596 #ifndef G_DISABLE_ASSERT
1597 version = hash_table->version;
1598 #endif
1600 for (i = 0; i < hash_table->size; i++)
1602 guint node_hash = hash_table->hashes[i];
1603 gpointer node_key = hash_table->keys[i];
1604 gpointer node_value = hash_table->values[i];
1606 if (HASH_IS_REAL (node_hash))
1607 (* func) (node_key, node_value, user_data);
1609 #ifndef G_DISABLE_ASSERT
1610 g_return_if_fail (version == hash_table->version);
1611 #endif
1616 * g_hash_table_find:
1617 * @hash_table: a #GHashTable
1618 * @predicate: function to test the key/value pairs for a certain property
1619 * @user_data: user data to pass to the function
1621 * Calls the given function for key/value pairs in the #GHashTable
1622 * until @predicate returns %TRUE. The function is passed the key
1623 * and value of each pair, and the given @user_data parameter. The
1624 * hash table may not be modified while iterating over it (you can't
1625 * add/remove items).
1627 * Note, that hash tables are really only optimized for forward
1628 * lookups, i.e. g_hash_table_lookup(). So code that frequently issues
1629 * g_hash_table_find() or g_hash_table_foreach() (e.g. in the order of
1630 * once per every entry in a hash table) should probably be reworked
1631 * to use additional or different data structures for reverse lookups
1632 * (keep in mind that an O(n) find/foreach operation issued for all n
1633 * values in a hash table ends up needing O(n*n) operations).
1635 * Returns: (allow-none): The value of the first key/value pair is returned,
1636 * for which @predicate evaluates to %TRUE. If no pair with the
1637 * requested property is found, %NULL is returned.
1639 * Since: 2.4
1641 gpointer
1642 g_hash_table_find (GHashTable *hash_table,
1643 GHRFunc predicate,
1644 gpointer user_data)
1646 gint i;
1647 #ifndef G_DISABLE_ASSERT
1648 gint version;
1649 #endif
1650 gboolean match;
1652 g_return_val_if_fail (hash_table != NULL, NULL);
1653 g_return_val_if_fail (predicate != NULL, NULL);
1655 #ifndef G_DISABLE_ASSERT
1656 version = hash_table->version;
1657 #endif
1659 match = FALSE;
1661 for (i = 0; i < hash_table->size; i++)
1663 guint node_hash = hash_table->hashes[i];
1664 gpointer node_key = hash_table->keys[i];
1665 gpointer node_value = hash_table->values[i];
1667 if (HASH_IS_REAL (node_hash))
1668 match = predicate (node_key, node_value, user_data);
1670 #ifndef G_DISABLE_ASSERT
1671 g_return_val_if_fail (version == hash_table->version, NULL);
1672 #endif
1674 if (match)
1675 return node_value;
1678 return NULL;
1682 * g_hash_table_size:
1683 * @hash_table: a #GHashTable
1685 * Returns the number of elements contained in the #GHashTable.
1687 * Returns: the number of key/value pairs in the #GHashTable.
1689 guint
1690 g_hash_table_size (GHashTable *hash_table)
1692 g_return_val_if_fail (hash_table != NULL, 0);
1694 return hash_table->nnodes;
1698 * g_hash_table_get_keys:
1699 * @hash_table: a #GHashTable
1701 * Retrieves every key inside @hash_table. The returned data is valid
1702 * until changes to the hash release those keys.
1704 * Returns: a #GList containing all the keys inside the hash
1705 * table. The content of the list is owned by the hash table and
1706 * should not be modified or freed. Use g_list_free() when done
1707 * using the list.
1709 * Since: 2.14
1711 GList *
1712 g_hash_table_get_keys (GHashTable *hash_table)
1714 gint i;
1715 GList *retval;
1717 g_return_val_if_fail (hash_table != NULL, NULL);
1719 retval = NULL;
1720 for (i = 0; i < hash_table->size; i++)
1722 if (HASH_IS_REAL (hash_table->hashes[i]))
1723 retval = g_list_prepend (retval, hash_table->keys[i]);
1726 return retval;
1730 * g_hash_table_get_keys_as_array:
1731 * @hash_table: a #GHashTable
1732 * @length: (out): the length of the returned array
1734 * Retrieves every key inside @hash_table, as an array.
1736 * The returned array is %NULL-terminated but may contain %NULL as a
1737 * key. Use @length to determine the true length if it's possible that
1738 * %NULL was used as the value for a key.
1740 * Note: in the common case of a string-keyed #GHashTable, the return
1741 * value of this function can be conveniently cast to (const gchar **).
1743 * You should always free the return result with g_free(). In the
1744 * above-mentioned case of a string-keyed hash table, it may be
1745 * appropriate to use g_strfreev() if you call g_hash_table_steal_all()
1746 * first to transfer ownership of the keys.
1748 * Returns: (array length=length) (transfer container): a
1749 * %NULL-terminated array containing each key from the table.
1751 * Since: 2.40
1753 gpointer *
1754 g_hash_table_get_keys_as_array (GHashTable *hash_table,
1755 guint *length)
1757 gpointer *result;
1758 guint i, j = 0;
1760 result = g_new (gpointer, hash_table->nnodes + 1);
1761 for (i = 0; i < hash_table->size; i++)
1763 if (HASH_IS_REAL (hash_table->hashes[i]))
1764 result[j++] = hash_table->keys[i];
1766 g_assert_cmpint (j, ==, hash_table->nnodes);
1767 result[j] = NULL;
1769 if (length)
1770 *length = j;
1772 return result;
1776 * g_hash_table_get_values:
1777 * @hash_table: a #GHashTable
1779 * Retrieves every value inside @hash_table. The returned data
1780 * is valid until @hash_table is modified.
1782 * Returns: a #GList containing all the values inside the hash
1783 * table. The content of the list is owned by the hash table and
1784 * should not be modified or freed. Use g_list_free() when done
1785 * using the list.
1787 * Since: 2.14
1789 GList *
1790 g_hash_table_get_values (GHashTable *hash_table)
1792 gint i;
1793 GList *retval;
1795 g_return_val_if_fail (hash_table != NULL, NULL);
1797 retval = NULL;
1798 for (i = 0; i < hash_table->size; i++)
1800 if (HASH_IS_REAL (hash_table->hashes[i]))
1801 retval = g_list_prepend (retval, hash_table->values[i]);
1804 return retval;
1807 /* Hash functions.
1811 * g_str_equal:
1812 * @v1: a key
1813 * @v2: a key to compare with @v1
1815 * Compares two strings for byte-by-byte equality and returns %TRUE
1816 * if they are equal. It can be passed to g_hash_table_new() as the
1817 * @key_equal_func parameter, when using non-%NULL strings as keys in a
1818 * #GHashTable.
1820 * Note that this function is primarily meant as a hash table comparison
1821 * function. For a general-purpose, %NULL-safe string comparison function,
1822 * see g_strcmp0().
1824 * Returns: %TRUE if the two keys match
1826 gboolean
1827 g_str_equal (gconstpointer v1,
1828 gconstpointer v2)
1830 const gchar *string1 = v1;
1831 const gchar *string2 = v2;
1833 return strcmp (string1, string2) == 0;
1837 * g_str_hash:
1838 * @v: a string key
1840 * Converts a string to a hash value.
1842 * This function implements the widely used "djb" hash apparently
1843 * posted by Daniel Bernstein to comp.lang.c some time ago. The 32
1844 * bit unsigned hash value starts at 5381 and for each byte 'c' in
1845 * the string, is updated: `hash = hash * 33 + c`. This function
1846 * uses the signed value of each byte.
1848 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1849 * when using non-%NULL strings as keys in a #GHashTable.
1851 * Returns: a hash value corresponding to the key
1853 guint
1854 g_str_hash (gconstpointer v)
1856 const signed char *p;
1857 guint32 h = 5381;
1859 for (p = v; *p != '\0'; p++)
1860 h = (h << 5) + h + *p;
1862 return h;
1866 * g_direct_hash:
1867 * @v: (allow-none): a #gpointer key
1869 * Converts a gpointer to a hash value.
1870 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1871 * when using opaque pointers compared by pointer value as keys in a
1872 * #GHashTable.
1874 * This hash function is also appropriate for keys that are integers
1875 * stored in pointers, such as `GINT_TO_POINTER (n)`.
1877 * Returns: a hash value corresponding to the key.
1879 guint
1880 g_direct_hash (gconstpointer v)
1882 return GPOINTER_TO_UINT (v);
1886 * g_direct_equal:
1887 * @v1: (allow-none): a key
1888 * @v2: (allow-none): a key to compare with @v1
1890 * Compares two #gpointer arguments and returns %TRUE if they are equal.
1891 * It can be passed to g_hash_table_new() as the @key_equal_func
1892 * parameter, when using opaque pointers compared by pointer value as
1893 * keys in a #GHashTable.
1895 * This equality function is also appropriate for keys that are integers
1896 * stored in pointers, such as `GINT_TO_POINTER (n)`.
1898 * Returns: %TRUE if the two keys match.
1900 gboolean
1901 g_direct_equal (gconstpointer v1,
1902 gconstpointer v2)
1904 return v1 == v2;
1908 * g_int_equal:
1909 * @v1: a pointer to a #gint key
1910 * @v2: a pointer to a #gint key to compare with @v1
1912 * Compares the two #gint values being pointed to and returns
1913 * %TRUE if they are equal.
1914 * It can be passed to g_hash_table_new() as the @key_equal_func
1915 * parameter, when using non-%NULL pointers to integers as keys in a
1916 * #GHashTable.
1918 * Note that this function acts on pointers to #gint, not on #gint
1919 * directly: if your hash table's keys are of the form
1920 * `GINT_TO_POINTER (n)`, use g_direct_equal() instead.
1922 * Returns: %TRUE if the two keys match.
1924 gboolean
1925 g_int_equal (gconstpointer v1,
1926 gconstpointer v2)
1928 return *((const gint*) v1) == *((const gint*) v2);
1932 * g_int_hash:
1933 * @v: a pointer to a #gint key
1935 * Converts a pointer to a #gint to a hash value.
1936 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1937 * when using non-%NULL pointers to integer values as keys in a #GHashTable.
1939 * Note that this function acts on pointers to #gint, not on #gint
1940 * directly: if your hash table's keys are of the form
1941 * `GINT_TO_POINTER (n)`, use g_direct_hash() instead.
1943 * Returns: a hash value corresponding to the key.
1945 guint
1946 g_int_hash (gconstpointer v)
1948 return *(const gint*) v;
1952 * g_int64_equal:
1953 * @v1: a pointer to a #gint64 key
1954 * @v2: a pointer to a #gint64 key to compare with @v1
1956 * Compares the two #gint64 values being pointed to and returns
1957 * %TRUE if they are equal.
1958 * It can be passed to g_hash_table_new() as the @key_equal_func
1959 * parameter, when using non-%NULL pointers to 64-bit integers as keys in a
1960 * #GHashTable.
1962 * Returns: %TRUE if the two keys match.
1964 * Since: 2.22
1966 gboolean
1967 g_int64_equal (gconstpointer v1,
1968 gconstpointer v2)
1970 return *((const gint64*) v1) == *((const gint64*) v2);
1974 * g_int64_hash:
1975 * @v: a pointer to a #gint64 key
1977 * Converts a pointer to a #gint64 to a hash value.
1979 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1980 * when using non-%NULL pointers to 64-bit integer values as keys in a
1981 * #GHashTable.
1983 * Returns: a hash value corresponding to the key.
1985 * Since: 2.22
1987 guint
1988 g_int64_hash (gconstpointer v)
1990 return (guint) *(const gint64*) v;
1994 * g_double_equal:
1995 * @v1: a pointer to a #gdouble key
1996 * @v2: a pointer to a #gdouble key to compare with @v1
1998 * Compares the two #gdouble values being pointed to and returns
1999 * %TRUE if they are equal.
2000 * It can be passed to g_hash_table_new() as the @key_equal_func
2001 * parameter, when using non-%NULL pointers to doubles as keys in a
2002 * #GHashTable.
2004 * Returns: %TRUE if the two keys match.
2006 * Since: 2.22
2008 gboolean
2009 g_double_equal (gconstpointer v1,
2010 gconstpointer v2)
2012 return *((const gdouble*) v1) == *((const gdouble*) v2);
2016 * g_double_hash:
2017 * @v: a pointer to a #gdouble key
2019 * Converts a pointer to a #gdouble to a hash value.
2020 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2021 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2022 * when using non-%NULL pointers to doubles as keys in a #GHashTable.
2024 * Returns: a hash value corresponding to the key.
2026 * Since: 2.22
2028 guint
2029 g_double_hash (gconstpointer v)
2031 return (guint) *(const gdouble*) v;