Add a test for g_markup_parse_context_get_element_stack
[glib.git] / glib / ghash.c
blob14d0fdad5565f51da9628fb38ecfb88d40843e8e
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/.
28 * MT safe
31 #include "config.h"
33 #include <string.h> /* memset */
35 #include "ghash.h"
37 #include "gstrfuncs.h"
38 #include "gatomic.h"
39 #include "gtestutils.h"
40 #include "gslice.h"
43 /**
44 * SECTION:hash_tables
45 * @title: Hash Tables
46 * @short_description: associations between keys and values so that
47 * given a key the value can be found quickly
49 * A #GHashTable provides associations between keys and values which is
50 * optimized so that given a key, the associated value can be found
51 * very quickly.
53 * Note that neither keys nor values are copied when inserted into the
54 * #GHashTable, so they must exist for the lifetime of the #GHashTable.
55 * This means that the use of static strings is OK, but temporary
56 * strings (i.e. those created in buffers and those returned by GTK+
57 * widgets) should be copied with g_strdup() before being inserted.
59 * If keys or values are dynamically allocated, you must be careful to
60 * ensure that they are freed when they are removed from the
61 * #GHashTable, and also when they are overwritten by new insertions
62 * into the #GHashTable. It is also not advisable to mix static strings
63 * and dynamically-allocated strings in a #GHashTable, because it then
64 * becomes difficult to determine whether the string should be freed.
66 * To create a #GHashTable, use g_hash_table_new().
68 * To insert a key and value into a #GHashTable, use
69 * g_hash_table_insert().
71 * To lookup a value corresponding to a given key, use
72 * g_hash_table_lookup() and g_hash_table_lookup_extended().
74 * g_hash_table_lookup_extended() can also be used to simply
75 * check if a key is present in the hash table.
77 * To remove a key and value, use g_hash_table_remove().
79 * To call a function for each key and value pair use
80 * g_hash_table_foreach() or use a iterator to iterate over the
81 * key/value pairs in the hash table, see #GHashTableIter.
83 * To destroy a #GHashTable use g_hash_table_destroy().
85 * A common use-case for hash tables is to store information about a
86 * set of keys, without associating any particular value with each
87 * key. GHashTable optimizes one way of doing so: If you store only
88 * key-value pairs where key == value, then GHashTable does not
89 * allocate memory to store the values, which can be a considerable
90 * space saving, if your set is large. The functions
91 * g_hash_table_add() and g_hash_table_contains() are designed to be
92 * used when using #GHashTable this way.
95 /**
96 * GHashTable:
98 * The #GHashTable struct is an opaque data structure to represent a
99 * <link linkend="glib-Hash-Tables">Hash Table</link>. It should only be
100 * accessed via the following functions.
104 * GHashFunc:
105 * @key: a key
107 * Specifies the type of the hash function which is passed to
108 * g_hash_table_new() when a #GHashTable is created.
110 * The function is passed a key and should return a #guint hash value.
111 * The functions g_direct_hash(), g_int_hash() and g_str_hash() provide
112 * hash functions which can be used when the key is a #gpointer, #gint*,
113 * and #gchar* respectively.
115 * g_direct_hash() is also the appropriate hash function for keys
116 * of the form <literal>GINT_TO_POINTER (n)</literal> (or similar macros).
118 * <!-- FIXME: Need more here. --> A good hash functions should produce
119 * hash values that are evenly distributed over a fairly large range.
120 * The modulus is taken with the hash table size (a prime number) to
121 * find the 'bucket' to place each key into. The function should also
122 * be very fast, since it is called for each key lookup.
124 * Note that the hash functions provided by GLib have these qualities,
125 * but are not particularly robust against manufactured keys that
126 * cause hash collisions. Therefore, you should consider choosing
127 * a more secure hash function when using a GHashTable with keys
128 * that originate in untrusted data (such as HTTP requests).
129 * Using g_str_hash() in that situation might make your application
130 * vulerable to <ulink url="https://lwn.net/Articles/474912/">Algorithmic Complexity Attacks</ulink>.
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 /* Each table size has an associated prime modulo (the first prime
250 * lower than the table size) used to find the initial bucket. Probing
251 * then works modulo 2^n. The prime modulo is necessary to get a
252 * good distribution with poor hash functions.
254 static const gint prime_mod [] =
256 1, /* For 1 << 0 */
263 127,
264 251,
265 509,
266 1021,
267 2039,
268 4093,
269 8191,
270 16381,
271 32749,
272 65521, /* For 1 << 16 */
273 131071,
274 262139,
275 524287,
276 1048573,
277 2097143,
278 4194301,
279 8388593,
280 16777213,
281 33554393,
282 67108859,
283 134217689,
284 268435399,
285 536870909,
286 1073741789,
287 2147483647 /* For 1 << 31 */
290 static void
291 g_hash_table_set_shift (GHashTable *hash_table, gint shift)
293 gint i;
294 guint mask = 0;
296 hash_table->size = 1 << shift;
297 hash_table->mod = prime_mod [shift];
299 for (i = 0; i < shift; i++)
301 mask <<= 1;
302 mask |= 1;
305 hash_table->mask = mask;
308 static gint
309 g_hash_table_find_closest_shift (gint n)
311 gint i;
313 for (i = 0; n; i++)
314 n >>= 1;
316 return i;
319 static void
320 g_hash_table_set_shift_from_size (GHashTable *hash_table, gint size)
322 gint shift;
324 shift = g_hash_table_find_closest_shift (size);
325 shift = MAX (shift, HASH_TABLE_MIN_SHIFT);
327 g_hash_table_set_shift (hash_table, shift);
331 * g_hash_table_lookup_node:
332 * @hash_table: our #GHashTable
333 * @key: the key to lookup against
334 * @hash_return: key hash return location
336 * Performs a lookup in the hash table, preserving extra information
337 * usually needed for insertion.
339 * This function first computes the hash value of the key using the
340 * user's hash function.
342 * If an entry in the table matching @key is found then this function
343 * returns the index of that entry in the table, and if not, the
344 * index of an unused node (empty or tombstone) where the key can be
345 * inserted.
347 * The computed hash value is returned in the variable pointed to
348 * by @hash_return. This is to save insertions from having to compute
349 * the hash record again for the new record.
351 * Returns: index of the described node
353 static inline guint
354 g_hash_table_lookup_node (GHashTable *hash_table,
355 gconstpointer key,
356 guint *hash_return)
358 guint node_index;
359 guint node_hash;
360 guint hash_value;
361 guint first_tombstone = 0;
362 gboolean have_tombstone = FALSE;
363 guint step = 0;
365 hash_value = hash_table->hash_func (key);
366 if (G_UNLIKELY (!HASH_IS_REAL (hash_value)))
367 hash_value = 2;
369 *hash_return = hash_value;
371 node_index = hash_value % hash_table->mod;
372 node_hash = hash_table->hashes[node_index];
374 while (!HASH_IS_UNUSED (node_hash))
376 /* We first check if our full hash values
377 * are equal so we can avoid calling the full-blown
378 * key equality function in most cases.
380 if (node_hash == hash_value)
382 gpointer node_key = hash_table->keys[node_index];
384 if (hash_table->key_equal_func)
386 if (hash_table->key_equal_func (node_key, key))
387 return node_index;
389 else if (node_key == key)
391 return node_index;
394 else if (HASH_IS_TOMBSTONE (node_hash) && !have_tombstone)
396 first_tombstone = node_index;
397 have_tombstone = TRUE;
400 step++;
401 node_index += step;
402 node_index &= hash_table->mask;
403 node_hash = hash_table->hashes[node_index];
406 if (have_tombstone)
407 return first_tombstone;
409 return node_index;
413 * g_hash_table_remove_node:
414 * @hash_table: our #GHashTable
415 * @node: pointer to node to remove
416 * @notify: %TRUE if the destroy notify handlers are to be called
418 * Removes a node from the hash table and updates the node count.
419 * The node is replaced by a tombstone. No table resize is performed.
421 * If @notify is %TRUE then the destroy notify functions are called
422 * for the key and value of the hash node.
424 static void
425 g_hash_table_remove_node (GHashTable *hash_table,
426 gint i,
427 gboolean notify)
429 gpointer key;
430 gpointer value;
432 key = hash_table->keys[i];
433 value = hash_table->values[i];
435 /* Erect tombstone */
436 hash_table->hashes[i] = TOMBSTONE_HASH_VALUE;
438 /* Be GC friendly */
439 hash_table->keys[i] = NULL;
440 hash_table->values[i] = NULL;
442 hash_table->nnodes--;
444 if (notify && hash_table->key_destroy_func)
445 hash_table->key_destroy_func (key);
447 if (notify && hash_table->value_destroy_func)
448 hash_table->value_destroy_func (value);
453 * g_hash_table_remove_all_nodes:
454 * @hash_table: our #GHashTable
455 * @notify: %TRUE if the destroy notify handlers are to be called
457 * Removes all nodes from the table. Since this may be a precursor to
458 * freeing the table entirely, no resize is performed.
460 * If @notify is %TRUE then the destroy notify functions are called
461 * for the key and value of the hash node.
463 static void
464 g_hash_table_remove_all_nodes (GHashTable *hash_table,
465 gboolean notify)
467 int i;
468 gpointer key;
469 gpointer value;
471 hash_table->nnodes = 0;
472 hash_table->noccupied = 0;
474 if (!notify ||
475 (hash_table->key_destroy_func == NULL &&
476 hash_table->value_destroy_func == NULL))
478 memset (hash_table->hashes, 0, hash_table->size * sizeof (guint));
479 memset (hash_table->keys, 0, hash_table->size * sizeof (gpointer));
480 memset (hash_table->values, 0, hash_table->size * sizeof (gpointer));
482 return;
485 for (i = 0; i < hash_table->size; i++)
487 if (HASH_IS_REAL (hash_table->hashes[i]))
489 key = hash_table->keys[i];
490 value = hash_table->values[i];
492 hash_table->hashes[i] = UNUSED_HASH_VALUE;
493 hash_table->keys[i] = NULL;
494 hash_table->values[i] = NULL;
496 if (hash_table->key_destroy_func != NULL)
497 hash_table->key_destroy_func (key);
499 if (hash_table->value_destroy_func != NULL)
500 hash_table->value_destroy_func (value);
502 else if (HASH_IS_TOMBSTONE (hash_table->hashes[i]))
504 hash_table->hashes[i] = UNUSED_HASH_VALUE;
510 * g_hash_table_resize:
511 * @hash_table: our #GHashTable
513 * Resizes the hash table to the optimal size based on the number of
514 * nodes currently held. If you call this function then a resize will
515 * occur, even if one does not need to occur.
516 * Use g_hash_table_maybe_resize() instead.
518 * This function may "resize" the hash table to its current size, with
519 * the side effect of cleaning up tombstones and otherwise optimizing
520 * the probe sequences.
522 static void
523 g_hash_table_resize (GHashTable *hash_table)
525 gpointer *new_keys;
526 gpointer *new_values;
527 guint *new_hashes;
528 gint old_size;
529 gint i;
531 old_size = hash_table->size;
532 g_hash_table_set_shift_from_size (hash_table, hash_table->nnodes * 2);
534 new_keys = g_new0 (gpointer, hash_table->size);
535 if (hash_table->keys == hash_table->values)
536 new_values = new_keys;
537 else
538 new_values = g_new0 (gpointer, hash_table->size);
539 new_hashes = g_new0 (guint, hash_table->size);
541 for (i = 0; i < old_size; i++)
543 guint node_hash = hash_table->hashes[i];
544 guint hash_val;
545 guint step = 0;
547 if (!HASH_IS_REAL (node_hash))
548 continue;
550 hash_val = node_hash % hash_table->mod;
552 while (!HASH_IS_UNUSED (new_hashes[hash_val]))
554 step++;
555 hash_val += step;
556 hash_val &= hash_table->mask;
559 new_hashes[hash_val] = hash_table->hashes[i];
560 new_keys[hash_val] = hash_table->keys[i];
561 new_values[hash_val] = hash_table->values[i];
564 if (hash_table->keys != hash_table->values)
565 g_free (hash_table->values);
567 g_free (hash_table->keys);
568 g_free (hash_table->hashes);
570 hash_table->keys = new_keys;
571 hash_table->values = new_values;
572 hash_table->hashes = new_hashes;
574 hash_table->noccupied = hash_table->nnodes;
578 * g_hash_table_maybe_resize:
579 * @hash_table: our #GHashTable
581 * Resizes the hash table, if needed.
583 * Essentially, calls g_hash_table_resize() if the table has strayed
584 * too far from its ideal size for its number of nodes.
586 static inline void
587 g_hash_table_maybe_resize (GHashTable *hash_table)
589 gint noccupied = hash_table->noccupied;
590 gint size = hash_table->size;
592 if ((size > hash_table->nnodes * 4 && size > 1 << HASH_TABLE_MIN_SHIFT) ||
593 (size <= noccupied + (noccupied / 16)))
594 g_hash_table_resize (hash_table);
598 * g_hash_table_new:
599 * @hash_func: a function to create a hash value from a key
600 * @key_equal_func: a function to check two keys for equality
602 * Creates a new #GHashTable with a reference count of 1.
604 * Hash values returned by @hash_func are used to determine where keys
605 * are stored within the #GHashTable data structure. The g_direct_hash(),
606 * g_int_hash(), g_int64_hash(), g_double_hash() and g_str_hash()
607 * functions are provided for some common types of keys.
608 * If @hash_func is %NULL, g_direct_hash() is used.
610 * @key_equal_func is used when looking up keys in the #GHashTable.
611 * The g_direct_equal(), g_int_equal(), g_int64_equal(), g_double_equal()
612 * and g_str_equal() functions are provided for the most common types
613 * of keys. If @key_equal_func is %NULL, keys are compared directly in
614 * a similar fashion to g_direct_equal(), but without the overhead of
615 * a function call.
617 * Return value: a new #GHashTable
619 GHashTable *
620 g_hash_table_new (GHashFunc hash_func,
621 GEqualFunc key_equal_func)
623 return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
628 * g_hash_table_new_full:
629 * @hash_func: a function to create a hash value from a key
630 * @key_equal_func: a function to check two keys for equality
631 * @key_destroy_func: (allow-none): a function to free the memory allocated for the key
632 * used when removing the entry from the #GHashTable, or %NULL
633 * if you don't want to supply such a function.
634 * @value_destroy_func: (allow-none): a function to free the memory allocated for the
635 * value used when removing the entry from the #GHashTable, or %NULL
636 * if you don't want to supply such a function.
638 * Creates a new #GHashTable like g_hash_table_new() with a reference
639 * count of 1 and allows to specify functions to free the memory
640 * allocated for the key and value that get called when removing the
641 * entry from the #GHashTable.
643 * Return value: a new #GHashTable
645 GHashTable *
646 g_hash_table_new_full (GHashFunc hash_func,
647 GEqualFunc key_equal_func,
648 GDestroyNotify key_destroy_func,
649 GDestroyNotify value_destroy_func)
651 GHashTable *hash_table;
653 hash_table = g_slice_new (GHashTable);
654 g_hash_table_set_shift (hash_table, HASH_TABLE_MIN_SHIFT);
655 hash_table->nnodes = 0;
656 hash_table->noccupied = 0;
657 hash_table->hash_func = hash_func ? hash_func : g_direct_hash;
658 hash_table->key_equal_func = key_equal_func;
659 hash_table->ref_count = 1;
660 #ifndef G_DISABLE_ASSERT
661 hash_table->version = 0;
662 #endif
663 hash_table->key_destroy_func = key_destroy_func;
664 hash_table->value_destroy_func = value_destroy_func;
665 hash_table->keys = g_new0 (gpointer, hash_table->size);
666 hash_table->values = hash_table->keys;
667 hash_table->hashes = g_new0 (guint, hash_table->size);
669 return hash_table;
673 * g_hash_table_iter_init:
674 * @iter: an uninitialized #GHashTableIter
675 * @hash_table: a #GHashTable
677 * Initializes a key/value pair iterator and associates it with
678 * @hash_table. Modifying the hash table after calling this function
679 * invalidates the returned iterator.
680 * |[
681 * GHashTableIter iter;
682 * gpointer key, value;
684 * g_hash_table_iter_init (&iter, hash_table);
685 * while (g_hash_table_iter_next (&iter, &key, &value))
687 * /&ast; do something with key and value &ast;/
689 * ]|
691 * Since: 2.16
693 void
694 g_hash_table_iter_init (GHashTableIter *iter,
695 GHashTable *hash_table)
697 RealIter *ri = (RealIter *) iter;
699 g_return_if_fail (iter != NULL);
700 g_return_if_fail (hash_table != NULL);
702 ri->hash_table = hash_table;
703 ri->position = -1;
704 #ifndef G_DISABLE_ASSERT
705 ri->version = hash_table->version;
706 #endif
710 * g_hash_table_iter_next:
711 * @iter: an initialized #GHashTableIter
712 * @key: (allow-none): a location to store the key, or %NULL
713 * @value: (allow-none): a location to store the value, or %NULL
715 * Advances @iter and retrieves the key and/or value that are now
716 * pointed to as a result of this advancement. If %FALSE is returned,
717 * @key and @value are not set, and the iterator becomes invalid.
719 * Return value: %FALSE if the end of the #GHashTable has been reached.
721 * Since: 2.16
723 gboolean
724 g_hash_table_iter_next (GHashTableIter *iter,
725 gpointer *key,
726 gpointer *value)
728 RealIter *ri = (RealIter *) iter;
729 gint position;
731 g_return_val_if_fail (iter != NULL, FALSE);
732 #ifndef G_DISABLE_ASSERT
733 g_return_val_if_fail (ri->version == ri->hash_table->version, FALSE);
734 #endif
735 g_return_val_if_fail (ri->position < ri->hash_table->size, FALSE);
737 position = ri->position;
741 position++;
742 if (position >= ri->hash_table->size)
744 ri->position = position;
745 return FALSE;
748 while (!HASH_IS_REAL (ri->hash_table->hashes[position]));
750 if (key != NULL)
751 *key = ri->hash_table->keys[position];
752 if (value != NULL)
753 *value = ri->hash_table->values[position];
755 ri->position = position;
756 return TRUE;
760 * g_hash_table_iter_get_hash_table:
761 * @iter: an initialized #GHashTableIter
763 * Returns the #GHashTable associated with @iter.
765 * Return value: the #GHashTable associated with @iter.
767 * Since: 2.16
769 GHashTable *
770 g_hash_table_iter_get_hash_table (GHashTableIter *iter)
772 g_return_val_if_fail (iter != NULL, NULL);
774 return ((RealIter *) iter)->hash_table;
777 static void
778 iter_remove_or_steal (RealIter *ri, gboolean notify)
780 g_return_if_fail (ri != NULL);
781 #ifndef G_DISABLE_ASSERT
782 g_return_if_fail (ri->version == ri->hash_table->version);
783 #endif
784 g_return_if_fail (ri->position >= 0);
785 g_return_if_fail (ri->position < ri->hash_table->size);
787 g_hash_table_remove_node (ri->hash_table, ri->position, notify);
789 #ifndef G_DISABLE_ASSERT
790 ri->version++;
791 ri->hash_table->version++;
792 #endif
796 * g_hash_table_iter_remove:
797 * @iter: an initialized #GHashTableIter
799 * Removes the key/value pair currently pointed to by the iterator
800 * from its associated #GHashTable. Can only be called after
801 * g_hash_table_iter_next() returned %TRUE, and cannot be called
802 * more than once for the same key/value pair.
804 * If the #GHashTable was created using g_hash_table_new_full(),
805 * the key and value are freed using the supplied destroy functions,
806 * otherwise you have to make sure that any dynamically allocated
807 * values are freed yourself.
809 * Since: 2.16
811 void
812 g_hash_table_iter_remove (GHashTableIter *iter)
814 iter_remove_or_steal ((RealIter *) iter, TRUE);
818 * g_hash_table_insert_node:
819 * @hash_table: our #GHashTable
820 * @node_index: pointer to node to insert/replace
821 * @key_hash: key hash
822 * @key: (allow-none): key to replace with, or %NULL
823 * @value: value to replace with
824 * @keep_new_key: whether to replace the key in the node with @key
825 * @reusing_key: whether @key was taken out of the existing node
827 * Inserts a value at @node_index in the hash table and updates it.
829 * If @key has been taken out of the existing node (ie it is not
830 * passed in via a g_hash_table_insert/replace) call, then @reusing_key
831 * should be %TRUE.
833 * Returns: %TRUE if the key did not exist yet
835 static gboolean
836 g_hash_table_insert_node (GHashTable *hash_table,
837 guint node_index,
838 guint key_hash,
839 gpointer new_key,
840 gpointer new_value,
841 gboolean keep_new_key,
842 gboolean reusing_key)
844 gboolean already_exists;
845 guint old_hash;
846 gpointer key_to_free = NULL;
847 gpointer value_to_free = NULL;
849 old_hash = hash_table->hashes[node_index];
850 already_exists = HASH_IS_REAL (old_hash);
852 /* Proceed in three steps. First, deal with the key because it is the
853 * most complicated. Then consider if we need to split the table in
854 * two (because writing the value will result in the set invariant
855 * becoming broken). Then deal with the value.
857 * There are three cases for the key:
859 * - entry already exists in table, reusing key:
860 * free the just-passed-in new_key and use the existing value
862 * - entry already exists in table, not reusing key:
863 * free the entry in the table, use the new key
865 * - entry not already in table:
866 * use the new key, free nothing
868 * We update the hash at the same time...
870 if (already_exists)
872 /* Note: we must record the old value before writing the new key
873 * because we might change the value in the event that the two
874 * arrays are shared.
876 value_to_free = hash_table->values[node_index];
878 if (keep_new_key)
880 key_to_free = hash_table->keys[node_index];
881 hash_table->keys[node_index] = new_key;
883 else
884 key_to_free = new_key;
886 else
888 hash_table->hashes[node_index] = key_hash;
889 hash_table->keys[node_index] = new_key;
892 /* Step two: check if the value that we are about to write to the
893 * table is the same as the key in the same position. If it's not,
894 * split the table.
896 if (G_UNLIKELY (hash_table->keys == hash_table->values && hash_table->keys[node_index] != new_value))
897 hash_table->values = g_memdup (hash_table->keys, sizeof (gpointer) * hash_table->size);
899 /* Step 3: Actually do the write */
900 hash_table->values[node_index] = new_value;
902 /* Now, the bookkeeping... */
903 if (!already_exists)
905 hash_table->nnodes++;
907 if (HASH_IS_UNUSED (old_hash))
909 /* We replaced an empty node, and not a tombstone */
910 hash_table->noccupied++;
911 g_hash_table_maybe_resize (hash_table);
914 #ifndef G_DISABLE_ASSERT
915 hash_table->version++;
916 #endif
919 if (already_exists)
921 if (hash_table->key_destroy_func && !reusing_key)
922 (* hash_table->key_destroy_func) (key_to_free);
923 if (hash_table->value_destroy_func)
924 (* hash_table->value_destroy_func) (value_to_free);
927 return !already_exists;
931 * g_hash_table_iter_replace:
932 * @iter: an initialized #GHashTableIter
933 * @value: the value to replace with
935 * Replaces the value currently pointed to by the iterator
936 * from its associated #GHashTable. Can only be called after
937 * g_hash_table_iter_next() returned %TRUE.
939 * If you supplied a @value_destroy_func when creating the
940 * #GHashTable, the old value is freed using that function.
942 * Since: 2.30
944 void
945 g_hash_table_iter_replace (GHashTableIter *iter,
946 gpointer value)
948 RealIter *ri;
949 guint node_hash;
950 gpointer key;
952 ri = (RealIter *) iter;
954 g_return_if_fail (ri != NULL);
955 #ifndef G_DISABLE_ASSERT
956 g_return_if_fail (ri->version == ri->hash_table->version);
957 #endif
958 g_return_if_fail (ri->position >= 0);
959 g_return_if_fail (ri->position < ri->hash_table->size);
961 node_hash = ri->hash_table->hashes[ri->position];
962 key = ri->hash_table->keys[ri->position];
964 g_hash_table_insert_node (ri->hash_table, ri->position, node_hash, key, value, TRUE, TRUE);
966 #ifndef G_DISABLE_ASSERT
967 ri->version++;
968 ri->hash_table->version++;
969 #endif
973 * g_hash_table_iter_steal:
974 * @iter: an initialized #GHashTableIter
976 * Removes the key/value pair currently pointed to by the
977 * iterator from its associated #GHashTable, without calling
978 * the key and value destroy functions. Can only be called
979 * after g_hash_table_iter_next() returned %TRUE, and cannot
980 * be called more than once for the same key/value pair.
982 * Since: 2.16
984 void
985 g_hash_table_iter_steal (GHashTableIter *iter)
987 iter_remove_or_steal ((RealIter *) iter, FALSE);
992 * g_hash_table_ref:
993 * @hash_table: a valid #GHashTable
995 * Atomically increments the reference count of @hash_table by one.
996 * This function is MT-safe and may be called from any thread.
998 * Return value: the passed in #GHashTable
1000 * Since: 2.10
1002 GHashTable *
1003 g_hash_table_ref (GHashTable *hash_table)
1005 g_return_val_if_fail (hash_table != NULL, NULL);
1007 g_atomic_int_inc (&hash_table->ref_count);
1009 return hash_table;
1013 * g_hash_table_unref:
1014 * @hash_table: a valid #GHashTable
1016 * Atomically decrements the reference count of @hash_table by one.
1017 * If the reference count drops to 0, all keys and values will be
1018 * destroyed, and all memory allocated by the hash table is released.
1019 * This function is MT-safe and may be called from any thread.
1021 * Since: 2.10
1023 void
1024 g_hash_table_unref (GHashTable *hash_table)
1026 g_return_if_fail (hash_table != NULL);
1028 if (g_atomic_int_dec_and_test (&hash_table->ref_count))
1030 g_hash_table_remove_all_nodes (hash_table, TRUE);
1031 if (hash_table->keys != hash_table->values)
1032 g_free (hash_table->values);
1033 g_free (hash_table->keys);
1034 g_free (hash_table->hashes);
1035 g_slice_free (GHashTable, hash_table);
1040 * g_hash_table_destroy:
1041 * @hash_table: a #GHashTable
1043 * Destroys all keys and values in the #GHashTable and decrements its
1044 * reference count by 1. If keys and/or values are dynamically allocated,
1045 * you should either free them first or create the #GHashTable with destroy
1046 * notifiers using g_hash_table_new_full(). In the latter case the destroy
1047 * functions you supplied will be called on all keys and values during the
1048 * destruction phase.
1050 void
1051 g_hash_table_destroy (GHashTable *hash_table)
1053 g_return_if_fail (hash_table != NULL);
1055 g_hash_table_remove_all (hash_table);
1056 g_hash_table_unref (hash_table);
1060 * g_hash_table_lookup:
1061 * @hash_table: a #GHashTable
1062 * @key: the key to look up
1064 * Looks up a key in a #GHashTable. Note that this function cannot
1065 * distinguish between a key that is not present and one which is present
1066 * and has the value %NULL. If you need this distinction, use
1067 * g_hash_table_lookup_extended().
1069 * Return value: (allow-none): the associated value, or %NULL if the key is not found
1071 gpointer
1072 g_hash_table_lookup (GHashTable *hash_table,
1073 gconstpointer key)
1075 guint node_index;
1076 guint node_hash;
1078 g_return_val_if_fail (hash_table != NULL, NULL);
1080 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1082 return HASH_IS_REAL (hash_table->hashes[node_index])
1083 ? hash_table->values[node_index]
1084 : NULL;
1088 * g_hash_table_lookup_extended:
1089 * @hash_table: a #GHashTable
1090 * @lookup_key: the key to look up
1091 * @orig_key: (allow-none): return location for the original key, or %NULL
1092 * @value: (allow-none): return location for the value associated with the key, or %NULL
1094 * Looks up a key in the #GHashTable, returning the original key and the
1095 * associated value and a #gboolean which is %TRUE if the key was found. This
1096 * is useful if you need to free the memory allocated for the original key,
1097 * for example before calling g_hash_table_remove().
1099 * You can actually pass %NULL for @lookup_key to test
1100 * whether the %NULL key exists, provided the hash and equal functions
1101 * of @hash_table are %NULL-safe.
1103 * Return value: %TRUE if the key was found in the #GHashTable
1105 gboolean
1106 g_hash_table_lookup_extended (GHashTable *hash_table,
1107 gconstpointer lookup_key,
1108 gpointer *orig_key,
1109 gpointer *value)
1111 guint node_index;
1112 guint node_hash;
1114 g_return_val_if_fail (hash_table != NULL, FALSE);
1116 node_index = g_hash_table_lookup_node (hash_table, lookup_key, &node_hash);
1118 if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1119 return FALSE;
1121 if (orig_key)
1122 *orig_key = hash_table->keys[node_index];
1124 if (value)
1125 *value = hash_table->values[node_index];
1127 return TRUE;
1131 * g_hash_table_insert_internal:
1132 * @hash_table: our #GHashTable
1133 * @key: the key to insert
1134 * @value: the value to insert
1135 * @keep_new_key: if %TRUE and this key already exists in the table
1136 * then call the destroy notify function on the old key. If %FALSE
1137 * then call the destroy notify function on the new key.
1139 * Implements the common logic for the g_hash_table_insert() and
1140 * g_hash_table_replace() functions.
1142 * Do a lookup of @key. If it is found, replace it with the new
1143 * @value (and perhaps the new @key). If it is not found, create
1144 * a new node.
1146 * Returns: %TRUE if the key did not exist yet
1148 static gboolean
1149 g_hash_table_insert_internal (GHashTable *hash_table,
1150 gpointer key,
1151 gpointer value,
1152 gboolean keep_new_key)
1154 guint key_hash;
1155 guint node_index;
1157 g_return_val_if_fail (hash_table != NULL, FALSE);
1159 node_index = g_hash_table_lookup_node (hash_table, key, &key_hash);
1161 return g_hash_table_insert_node (hash_table, node_index, key_hash, key, value, keep_new_key, FALSE);
1165 * g_hash_table_insert:
1166 * @hash_table: a #GHashTable
1167 * @key: a key to insert
1168 * @value: the value to associate with the key
1170 * Inserts a new key and value into a #GHashTable.
1172 * If the key already exists in the #GHashTable its current
1173 * value is replaced with the new value. If you supplied a
1174 * @value_destroy_func when creating the #GHashTable, the old
1175 * value is freed using that function. If you supplied a
1176 * @key_destroy_func when creating the #GHashTable, the passed
1177 * key is freed using that function.
1179 * Returns: %TRUE if the key did not exist yet
1181 gboolean
1182 g_hash_table_insert (GHashTable *hash_table,
1183 gpointer key,
1184 gpointer value)
1186 return g_hash_table_insert_internal (hash_table, key, value, FALSE);
1190 * g_hash_table_replace:
1191 * @hash_table: a #GHashTable
1192 * @key: a key to insert
1193 * @value: the value to associate with the key
1195 * Inserts a new key and value into a #GHashTable similar to
1196 * g_hash_table_insert(). The difference is that if the key
1197 * already exists in the #GHashTable, it gets replaced by the
1198 * new key. If you supplied a @value_destroy_func when creating
1199 * the #GHashTable, the old value is freed using that function.
1200 * If you supplied a @key_destroy_func when creating the
1201 * #GHashTable, the old key is freed using that function.
1203 * Returns: %TRUE of the key did not exist yet
1205 gboolean
1206 g_hash_table_replace (GHashTable *hash_table,
1207 gpointer key,
1208 gpointer value)
1210 return g_hash_table_insert_internal (hash_table, key, value, TRUE);
1214 * g_hash_table_add:
1215 * @hash_table: a #GHashTable
1216 * @key: a key to insert
1218 * This is a convenience function for using a #GHashTable as a set. It
1219 * is equivalent to calling g_hash_table_replace() with @key as both the
1220 * key and the value.
1222 * When a hash table only ever contains keys that have themselves as the
1223 * corresponding value it is able to be stored more efficiently. See
1224 * the discussion in the section description.
1226 * Returns: %TRUE if the key did not exist yet
1228 * Since: 2.32
1230 gboolean
1231 g_hash_table_add (GHashTable *hash_table,
1232 gpointer key)
1234 return g_hash_table_insert_internal (hash_table, key, key, TRUE);
1238 * g_hash_table_contains:
1239 * @hash_table: a #GHashTable
1240 * @key: a key to check
1242 * Checks if @key is in @hash_table.
1244 * Since: 2.32
1246 gboolean
1247 g_hash_table_contains (GHashTable *hash_table,
1248 gconstpointer key)
1250 guint node_index;
1251 guint node_hash;
1253 g_return_val_if_fail (hash_table != NULL, FALSE);
1255 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1257 return HASH_IS_REAL (hash_table->hashes[node_index]);
1261 * g_hash_table_remove_internal:
1262 * @hash_table: our #GHashTable
1263 * @key: the key to remove
1264 * @notify: %TRUE if the destroy notify handlers are to be called
1265 * Return value: %TRUE if a node was found and removed, else %FALSE
1267 * Implements the common logic for the g_hash_table_remove() and
1268 * g_hash_table_steal() functions.
1270 * Do a lookup of @key and remove it if it is found, calling the
1271 * destroy notify handlers only if @notify is %TRUE.
1273 static gboolean
1274 g_hash_table_remove_internal (GHashTable *hash_table,
1275 gconstpointer key,
1276 gboolean notify)
1278 guint node_index;
1279 guint node_hash;
1281 g_return_val_if_fail (hash_table != NULL, FALSE);
1283 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1285 if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1286 return FALSE;
1288 g_hash_table_remove_node (hash_table, node_index, notify);
1289 g_hash_table_maybe_resize (hash_table);
1291 #ifndef G_DISABLE_ASSERT
1292 hash_table->version++;
1293 #endif
1295 return TRUE;
1299 * g_hash_table_remove:
1300 * @hash_table: a #GHashTable
1301 * @key: the key to remove
1303 * Removes a key and its associated value from a #GHashTable.
1305 * If the #GHashTable was created using g_hash_table_new_full(), the
1306 * key and value are freed using the supplied destroy functions, otherwise
1307 * you have to make sure that any dynamically allocated values are freed
1308 * yourself.
1310 * Returns: %TRUE if the key was found and removed from the #GHashTable
1312 gboolean
1313 g_hash_table_remove (GHashTable *hash_table,
1314 gconstpointer key)
1316 return g_hash_table_remove_internal (hash_table, key, TRUE);
1320 * g_hash_table_steal:
1321 * @hash_table: a #GHashTable
1322 * @key: the key to remove
1324 * Removes a key and its associated value from a #GHashTable without
1325 * calling the key and value destroy functions.
1327 * Returns: %TRUE if the key was found and removed from the #GHashTable
1329 gboolean
1330 g_hash_table_steal (GHashTable *hash_table,
1331 gconstpointer key)
1333 return g_hash_table_remove_internal (hash_table, key, FALSE);
1337 * g_hash_table_remove_all:
1338 * @hash_table: a #GHashTable
1340 * Removes all keys and their associated values from a #GHashTable.
1342 * If the #GHashTable was created using g_hash_table_new_full(),
1343 * the keys and values are freed using the supplied destroy functions,
1344 * otherwise you have to make sure that any dynamically allocated
1345 * values are freed yourself.
1347 * Since: 2.12
1349 void
1350 g_hash_table_remove_all (GHashTable *hash_table)
1352 g_return_if_fail (hash_table != NULL);
1354 #ifndef G_DISABLE_ASSERT
1355 if (hash_table->nnodes != 0)
1356 hash_table->version++;
1357 #endif
1359 g_hash_table_remove_all_nodes (hash_table, TRUE);
1360 g_hash_table_maybe_resize (hash_table);
1364 * g_hash_table_steal_all:
1365 * @hash_table: a #GHashTable
1367 * Removes all keys and their associated values from a #GHashTable
1368 * without calling the key and value destroy functions.
1370 * Since: 2.12
1372 void
1373 g_hash_table_steal_all (GHashTable *hash_table)
1375 g_return_if_fail (hash_table != NULL);
1377 #ifndef G_DISABLE_ASSERT
1378 if (hash_table->nnodes != 0)
1379 hash_table->version++;
1380 #endif
1382 g_hash_table_remove_all_nodes (hash_table, FALSE);
1383 g_hash_table_maybe_resize (hash_table);
1387 * g_hash_table_foreach_remove_or_steal:
1388 * @hash_table: a #GHashTable
1389 * @func: the user's callback function
1390 * @user_data: data for @func
1391 * @notify: %TRUE if the destroy notify handlers are to be called
1393 * Implements the common logic for g_hash_table_foreach_remove()
1394 * and g_hash_table_foreach_steal().
1396 * Iterates over every node in the table, calling @func with the key
1397 * and value of the node (and @user_data). If @func returns %TRUE the
1398 * node is removed from the table.
1400 * If @notify is true then the destroy notify handlers will be called
1401 * for each removed node.
1403 static guint
1404 g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
1405 GHRFunc func,
1406 gpointer user_data,
1407 gboolean notify)
1409 guint deleted = 0;
1410 gint i;
1411 #ifndef G_DISABLE_ASSERT
1412 gint version = hash_table->version;
1413 #endif
1415 for (i = 0; i < hash_table->size; i++)
1417 guint node_hash = hash_table->hashes[i];
1418 gpointer node_key = hash_table->keys[i];
1419 gpointer node_value = hash_table->values[i];
1421 if (HASH_IS_REAL (node_hash) &&
1422 (* func) (node_key, node_value, user_data))
1424 g_hash_table_remove_node (hash_table, i, notify);
1425 deleted++;
1428 #ifndef G_DISABLE_ASSERT
1429 g_return_val_if_fail (version == hash_table->version, 0);
1430 #endif
1433 g_hash_table_maybe_resize (hash_table);
1435 #ifndef G_DISABLE_ASSERT
1436 if (deleted > 0)
1437 hash_table->version++;
1438 #endif
1440 return deleted;
1444 * g_hash_table_foreach_remove:
1445 * @hash_table: a #GHashTable
1446 * @func: the function to call for each key/value pair
1447 * @user_data: user data to pass to the function
1449 * Calls the given function for each key/value pair in the
1450 * #GHashTable. If the function returns %TRUE, then the key/value
1451 * pair is removed from the #GHashTable. If you supplied key or
1452 * value destroy functions when creating the #GHashTable, they are
1453 * used to free the memory allocated for the removed keys and values.
1455 * See #GHashTableIter for an alternative way to loop over the
1456 * key/value pairs in the hash table.
1458 * Return value: the number of key/value pairs removed
1460 guint
1461 g_hash_table_foreach_remove (GHashTable *hash_table,
1462 GHRFunc func,
1463 gpointer user_data)
1465 g_return_val_if_fail (hash_table != NULL, 0);
1466 g_return_val_if_fail (func != NULL, 0);
1468 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
1472 * g_hash_table_foreach_steal:
1473 * @hash_table: a #GHashTable
1474 * @func: the function to call for each key/value pair
1475 * @user_data: user data to pass to the function
1477 * Calls the given function for each key/value pair in the
1478 * #GHashTable. If the function returns %TRUE, then the key/value
1479 * pair is removed from the #GHashTable, but no key or value
1480 * destroy functions are called.
1482 * See #GHashTableIter for an alternative way to loop over the
1483 * key/value pairs in the hash table.
1485 * Return value: the number of key/value pairs removed.
1487 guint
1488 g_hash_table_foreach_steal (GHashTable *hash_table,
1489 GHRFunc func,
1490 gpointer user_data)
1492 g_return_val_if_fail (hash_table != NULL, 0);
1493 g_return_val_if_fail (func != NULL, 0);
1495 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
1499 * g_hash_table_foreach:
1500 * @hash_table: a #GHashTable
1501 * @func: the function to call for each key/value pair
1502 * @user_data: user data to pass to the function
1504 * Calls the given function for each of the key/value pairs in the
1505 * #GHashTable. The function is passed the key and value of each
1506 * pair, and the given @user_data parameter. The hash table may not
1507 * be modified while iterating over it (you can't add/remove
1508 * items). To remove all items matching a predicate, use
1509 * g_hash_table_foreach_remove().
1511 * See g_hash_table_find() for performance caveats for linear
1512 * order searches in contrast to g_hash_table_lookup().
1514 void
1515 g_hash_table_foreach (GHashTable *hash_table,
1516 GHFunc func,
1517 gpointer user_data)
1519 gint i;
1520 #ifndef G_DISABLE_ASSERT
1521 gint version;
1522 #endif
1524 g_return_if_fail (hash_table != NULL);
1525 g_return_if_fail (func != NULL);
1527 #ifndef G_DISABLE_ASSERT
1528 version = hash_table->version;
1529 #endif
1531 for (i = 0; i < hash_table->size; i++)
1533 guint node_hash = hash_table->hashes[i];
1534 gpointer node_key = hash_table->keys[i];
1535 gpointer node_value = hash_table->values[i];
1537 if (HASH_IS_REAL (node_hash))
1538 (* func) (node_key, node_value, user_data);
1540 #ifndef G_DISABLE_ASSERT
1541 g_return_if_fail (version == hash_table->version);
1542 #endif
1547 * g_hash_table_find:
1548 * @hash_table: a #GHashTable
1549 * @predicate: function to test the key/value pairs for a certain property
1550 * @user_data: user data to pass to the function
1552 * Calls the given function for key/value pairs in the #GHashTable
1553 * until @predicate returns %TRUE. The function is passed the key
1554 * and value of each pair, and the given @user_data parameter. The
1555 * hash table may not be modified while iterating over it (you can't
1556 * add/remove items).
1558 * Note, that hash tables are really only optimized for forward
1559 * lookups, i.e. g_hash_table_lookup(). So code that frequently issues
1560 * g_hash_table_find() or g_hash_table_foreach() (e.g. in the order of
1561 * once per every entry in a hash table) should probably be reworked
1562 * to use additional or different data structures for reverse lookups
1563 * (keep in mind that an O(n) find/foreach operation issued for all n
1564 * values in a hash table ends up needing O(n*n) operations).
1566 * Return value: (allow-none): The value of the first key/value pair is returned,
1567 * for which @predicate evaluates to %TRUE. If no pair with the
1568 * requested property is found, %NULL is returned.
1570 * Since: 2.4
1572 gpointer
1573 g_hash_table_find (GHashTable *hash_table,
1574 GHRFunc predicate,
1575 gpointer user_data)
1577 gint i;
1578 #ifndef G_DISABLE_ASSERT
1579 gint version;
1580 #endif
1581 gboolean match;
1583 g_return_val_if_fail (hash_table != NULL, NULL);
1584 g_return_val_if_fail (predicate != NULL, NULL);
1586 #ifndef G_DISABLE_ASSERT
1587 version = hash_table->version;
1588 #endif
1590 match = FALSE;
1592 for (i = 0; i < hash_table->size; i++)
1594 guint node_hash = hash_table->hashes[i];
1595 gpointer node_key = hash_table->keys[i];
1596 gpointer node_value = hash_table->values[i];
1598 if (HASH_IS_REAL (node_hash))
1599 match = predicate (node_key, node_value, user_data);
1601 #ifndef G_DISABLE_ASSERT
1602 g_return_val_if_fail (version == hash_table->version, NULL);
1603 #endif
1605 if (match)
1606 return node_value;
1609 return NULL;
1613 * g_hash_table_size:
1614 * @hash_table: a #GHashTable
1616 * Returns the number of elements contained in the #GHashTable.
1618 * Return value: the number of key/value pairs in the #GHashTable.
1620 guint
1621 g_hash_table_size (GHashTable *hash_table)
1623 g_return_val_if_fail (hash_table != NULL, 0);
1625 return hash_table->nnodes;
1629 * g_hash_table_get_keys:
1630 * @hash_table: a #GHashTable
1632 * Retrieves every key inside @hash_table. The returned data is valid
1633 * until changes to the hash release those keys.
1635 * Return value: a #GList containing all the keys inside the hash
1636 * table. The content of the list is owned by the hash table and
1637 * should not be modified or freed. Use g_list_free() when done
1638 * using the list.
1640 * Since: 2.14
1642 GList *
1643 g_hash_table_get_keys (GHashTable *hash_table)
1645 gint i;
1646 GList *retval;
1648 g_return_val_if_fail (hash_table != NULL, NULL);
1650 retval = NULL;
1651 for (i = 0; i < hash_table->size; i++)
1653 if (HASH_IS_REAL (hash_table->hashes[i]))
1654 retval = g_list_prepend (retval, hash_table->keys[i]);
1657 return retval;
1661 * g_hash_table_get_keys_as_array:
1662 * @hash_table: a #GHashTable
1663 * @length: (out): the length of the returned array
1665 * Retrieves every key inside @hash_table, as an array.
1667 * The returned array is %NULL-terminated but may contain %NULL as a
1668 * key. Use @length to determine the true length if it's possible that
1669 * %NULL was used as the value for a key.
1671 * Note: in the common case of a string-keyed #GHashTable, the return
1672 * value of this function can be conveniently cast to (gchar **).
1674 * You should always free the return result with g_free(). In the
1675 * above-mentioned case of a string-keyed hash table, it may be
1676 * appropriate to use g_strfreev() if you call g_hash_table_steal_all()
1677 * first to transfer ownership of the keys.
1679 * Returns: (array length=length) (transfer container): a
1680 * %NULL-terminated array containing each key from the table.
1682 * Since: 2.40
1684 gpointer *
1685 g_hash_table_get_keys_as_array (GHashTable *hash_table,
1686 guint *length)
1688 gpointer *result;
1689 guint i, j = 0;
1691 result = g_new (gpointer, hash_table->nnodes + 1);
1692 for (i = 0; i < hash_table->size; i++)
1694 if (HASH_IS_REAL (hash_table->hashes[i]))
1695 result[j++] = hash_table->keys[i];
1697 g_assert_cmpint (j, ==, hash_table->nnodes);
1698 result[j] = NULL;
1700 if (length)
1701 *length = j;
1703 return result;
1707 * g_hash_table_get_values:
1708 * @hash_table: a #GHashTable
1710 * Retrieves every value inside @hash_table. The returned data
1711 * is valid until @hash_table is modified.
1713 * Return value: a #GList containing all the values inside the hash
1714 * table. The content of the list is owned by the hash table and
1715 * should not be modified or freed. Use g_list_free() when done
1716 * using the list.
1718 * Since: 2.14
1720 GList *
1721 g_hash_table_get_values (GHashTable *hash_table)
1723 gint i;
1724 GList *retval;
1726 g_return_val_if_fail (hash_table != NULL, NULL);
1728 retval = NULL;
1729 for (i = 0; i < hash_table->size; i++)
1731 if (HASH_IS_REAL (hash_table->hashes[i]))
1732 retval = g_list_prepend (retval, hash_table->values[i]);
1735 return retval;
1738 /* Hash functions.
1742 * g_str_equal:
1743 * @v1: a key
1744 * @v2: a key to compare with @v1
1746 * Compares two strings for byte-by-byte equality and returns %TRUE
1747 * if they are equal. It can be passed to g_hash_table_new() as the
1748 * @key_equal_func parameter, when using non-%NULL strings as keys in a
1749 * #GHashTable.
1751 * Note that this function is primarily meant as a hash table comparison
1752 * function. For a general-purpose, %NULL-safe string comparison function,
1753 * see g_strcmp0().
1755 * Returns: %TRUE if the two keys match
1757 gboolean
1758 g_str_equal (gconstpointer v1,
1759 gconstpointer v2)
1761 const gchar *string1 = v1;
1762 const gchar *string2 = v2;
1764 return strcmp (string1, string2) == 0;
1768 * g_str_hash:
1769 * @v: a string key
1771 * Converts a string to a hash value.
1773 * This function implements the widely used "djb" hash apparently posted
1774 * by Daniel Bernstein to comp.lang.c some time ago. The 32 bit
1775 * unsigned hash value starts at 5381 and for each byte 'c' in the
1776 * string, is updated: <literal>hash = hash * 33 + c</literal>. This
1777 * function uses the signed value of each byte.
1779 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1780 * when using non-%NULL strings as keys in a #GHashTable.
1782 * Returns: a hash value corresponding to the key
1784 guint
1785 g_str_hash (gconstpointer v)
1787 const signed char *p;
1788 guint32 h = 5381;
1790 for (p = v; *p != '\0'; p++)
1791 h = (h << 5) + h + *p;
1793 return h;
1797 * g_direct_hash:
1798 * @v: (allow-none): a #gpointer key
1800 * Converts a gpointer to a hash value.
1801 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1802 * when using opaque pointers compared by pointer value as keys in a
1803 * #GHashTable.
1805 * This hash function is also appropriate for keys that are integers stored
1806 * in pointers, such as <literal>GINT_TO_POINTER (n)</literal>.
1808 * Returns: a hash value corresponding to the key.
1810 guint
1811 g_direct_hash (gconstpointer v)
1813 return GPOINTER_TO_UINT (v);
1817 * g_direct_equal:
1818 * @v1: (allow-none): a key
1819 * @v2: (allow-none): a key to compare with @v1
1821 * Compares two #gpointer arguments and returns %TRUE if they are equal.
1822 * It can be passed to g_hash_table_new() as the @key_equal_func
1823 * parameter, when using opaque pointers compared by pointer value as keys
1824 * in a #GHashTable.
1826 * This equality function is also appropriate for keys that are integers stored
1827 * in pointers, such as <literal>GINT_TO_POINTER (n)</literal>.
1829 * Returns: %TRUE if the two keys match.
1831 gboolean
1832 g_direct_equal (gconstpointer v1,
1833 gconstpointer v2)
1835 return v1 == v2;
1839 * g_int_equal:
1840 * @v1: a pointer to a #gint key
1841 * @v2: a pointer to a #gint key to compare with @v1
1843 * Compares the two #gint values being pointed to and returns
1844 * %TRUE if they are equal.
1845 * It can be passed to g_hash_table_new() as the @key_equal_func
1846 * parameter, when using non-%NULL pointers to integers as keys in a
1847 * #GHashTable.
1849 * Note that this function acts on pointers to #gint, not on #gint directly:
1850 * if your hash table's keys are of the form
1851 * <literal>GINT_TO_POINTER (n)</literal>, use g_direct_equal() instead.
1853 * Returns: %TRUE if the two keys match.
1855 gboolean
1856 g_int_equal (gconstpointer v1,
1857 gconstpointer v2)
1859 return *((const gint*) v1) == *((const gint*) v2);
1863 * g_int_hash:
1864 * @v: a pointer to a #gint key
1866 * Converts a pointer to a #gint to a hash value.
1867 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1868 * when using non-%NULL pointers to integer values as keys in a #GHashTable.
1870 * Note that this function acts on pointers to #gint, not on #gint directly:
1871 * if your hash table's keys are of the form
1872 * <literal>GINT_TO_POINTER (n)</literal>, use g_direct_hash() instead.
1874 * Returns: a hash value corresponding to the key.
1876 guint
1877 g_int_hash (gconstpointer v)
1879 return *(const gint*) v;
1883 * g_int64_equal:
1884 * @v1: a pointer to a #gint64 key
1885 * @v2: a pointer to a #gint64 key to compare with @v1
1887 * Compares the two #gint64 values being pointed to and returns
1888 * %TRUE if they are equal.
1889 * It can be passed to g_hash_table_new() as the @key_equal_func
1890 * parameter, when using non-%NULL pointers to 64-bit integers as keys in a
1891 * #GHashTable.
1893 * Returns: %TRUE if the two keys match.
1895 * Since: 2.22
1897 gboolean
1898 g_int64_equal (gconstpointer v1,
1899 gconstpointer v2)
1901 return *((const gint64*) v1) == *((const gint64*) v2);
1905 * g_int64_hash:
1906 * @v: a pointer to a #gint64 key
1908 * Converts a pointer to a #gint64 to a hash value.
1910 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1911 * when using non-%NULL pointers to 64-bit integer values as keys in a
1912 * #GHashTable.
1914 * Returns: a hash value corresponding to the key.
1916 * Since: 2.22
1918 guint
1919 g_int64_hash (gconstpointer v)
1921 return (guint) *(const gint64*) v;
1925 * g_double_equal:
1926 * @v1: a pointer to a #gdouble key
1927 * @v2: a pointer to a #gdouble key to compare with @v1
1929 * Compares the two #gdouble values being pointed to and returns
1930 * %TRUE if they are equal.
1931 * It can be passed to g_hash_table_new() as the @key_equal_func
1932 * parameter, when using non-%NULL pointers to doubles as keys in a
1933 * #GHashTable.
1935 * Returns: %TRUE if the two keys match.
1937 * Since: 2.22
1939 gboolean
1940 g_double_equal (gconstpointer v1,
1941 gconstpointer v2)
1943 return *((const gdouble*) v1) == *((const gdouble*) v2);
1947 * g_double_hash:
1948 * @v: a pointer to a #gdouble key
1950 * Converts a pointer to a #gdouble to a hash value.
1951 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1952 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1953 * when using non-%NULL pointers to doubles as keys in a #GHashTable.
1955 * Returns: a hash value corresponding to the key.
1957 * Since: 2.22
1959 guint
1960 g_double_hash (gconstpointer v)
1962 return (guint) *(const gdouble*) v;