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/.
31 #include <string.h> /* memset */
35 #include "glib-private.h"
36 #include "gstrfuncs.h"
38 #include "gtestutils.h"
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
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.
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.
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
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
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().
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
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
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.
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)
218 gint noccupied; /* nnodes + tombstones */
225 GEqualFunc key_equal_func;
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).
235 GDestroyNotify key_destroy_func;
236 GDestroyNotify value_destroy_func;
241 GHashTable *hash_table;
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 [] =
275 65521, /* For 1 << 16 */
290 2147483647 /* For 1 << 31 */
294 g_hash_table_set_shift (GHashTable *hash_table, gint shift)
299 hash_table->size = 1 << shift;
300 hash_table->mod = prime_mod [shift];
302 for (i = 0; i < shift; i++)
308 hash_table->mask = mask;
312 g_hash_table_find_closest_shift (gint n)
323 g_hash_table_set_shift_from_size (GHashTable *hash_table, gint size)
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
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
357 g_hash_table_lookup_node (GHashTable *hash_table,
364 guint first_tombstone = 0;
365 gboolean have_tombstone = FALSE;
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)))
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))
399 else if (node_key == key)
404 else if (HASH_IS_TOMBSTONE (node_hash) && !have_tombstone)
406 first_tombstone = node_index;
407 have_tombstone = TRUE;
412 node_index &= hash_table->mask;
413 node_hash = hash_table->hashes[node_index];
417 return first_tombstone;
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.
435 g_hash_table_remove_node (GHashTable *hash_table,
442 key = hash_table->keys[i];
443 value = hash_table->values[i];
445 /* Erect tombstone */
446 hash_table->hashes[i] = TOMBSTONE_HASH_VALUE;
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.
474 g_hash_table_remove_all_nodes (GHashTable *hash_table,
476 gboolean destruction)
483 gpointer *old_values;
486 /* If the hash table is already empty, there is nothing to be done. */
487 if (hash_table->nnodes == 0)
490 hash_table->nnodes = 0;
491 hash_table->noccupied = 0;
494 (hash_table->key_destroy_func == NULL &&
495 hash_table->value_destroy_func == NULL))
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));
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
519 g_hash_table_set_shift (hash_table, HASH_TABLE_MIN_SHIFT);
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);
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]))
538 value = old_values[i];
540 old_hashes[i] = UNUSED_HASH_VALUE;
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)
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.
574 g_hash_table_resize (GHashTable *hash_table)
577 gpointer *new_values;
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;
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];
598 if (!HASH_IS_REAL (node_hash))
601 hash_val = node_hash % hash_table->mod;
603 while (!HASH_IS_UNUSED (new_hashes[hash_val]))
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.
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);
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
668 * Returns: a new #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
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;
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);
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
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;
762 #ifndef G_DISABLE_ASSERT
763 ri->version = hash_table->version;
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.
782 g_hash_table_iter_next (GHashTableIter *iter,
786 RealIter *ri = (RealIter *) iter;
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);
793 g_return_val_if_fail (ri->position < ri->hash_table->size, FALSE);
795 position = ri->position;
800 if (position >= ri->hash_table->size)
802 ri->position = position;
806 while (!HASH_IS_REAL (ri->hash_table->hashes[position]));
809 *key = ri->hash_table->keys[position];
811 *value = ri->hash_table->values[position];
813 ri->position = position;
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.
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;
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);
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
849 ri->hash_table->version++;
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))
872 * g_hash_table_iter_remove (&iter);
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
900 * Returns: %TRUE if the key did not exist yet
903 g_hash_table_insert_node (GHashTable *hash_table,
908 gboolean keep_new_key,
909 gboolean reusing_key)
911 gboolean already_exists;
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...
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
943 value_to_free = hash_table->values[node_index];
947 key_to_free = hash_table->keys[node_index];
948 hash_table->keys[node_index] = new_key;
951 key_to_free = new_key;
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,
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... */
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++;
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.
1012 g_hash_table_iter_replace (GHashTableIter *iter,
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);
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
1035 ri->hash_table->version++;
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.
1052 g_hash_table_iter_steal (GHashTableIter *iter)
1054 iter_remove_or_steal ((RealIter *) iter, FALSE);
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
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);
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.
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.
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
1139 g_hash_table_lookup (GHashTable *hash_table,
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]
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
1173 g_hash_table_lookup_extended (GHashTable *hash_table,
1174 gconstpointer lookup_key,
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]))
1189 *orig_key = hash_table->keys[node_index];
1192 *value = hash_table->values[node_index];
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
1213 * Returns: %TRUE if the key did not exist yet
1216 g_hash_table_insert_internal (GHashTable *hash_table,
1219 gboolean keep_new_key)
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
1249 g_hash_table_insert (GHashTable *hash_table,
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
1273 g_hash_table_replace (GHashTable *hash_table,
1277 return g_hash_table_insert_internal (hash_table, key, value, TRUE);
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
1298 g_hash_table_add (GHashTable *hash_table,
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.
1314 g_hash_table_contains (GHashTable *hash_table,
1320 g_return_val_if_fail (hash_table != NULL, FALSE);
1322 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1324 return HASH_IS_REAL (hash_table->hashes[node_index]);
1328 * g_hash_table_remove_internal:
1329 * @hash_table: our #GHashTable
1330 * @key: the key to remove
1331 * @notify: %TRUE if the destroy notify handlers are to be called
1332 * Returns: %TRUE if a node was found and removed, else %FALSE
1334 * Implements the common logic for the g_hash_table_remove() and
1335 * g_hash_table_steal() functions.
1337 * Do a lookup of @key and remove it if it is found, calling the
1338 * destroy notify handlers only if @notify is %TRUE.
1341 g_hash_table_remove_internal (GHashTable *hash_table,
1348 g_return_val_if_fail (hash_table != NULL, FALSE);
1350 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1352 if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1355 g_hash_table_remove_node (hash_table, node_index, notify);
1356 g_hash_table_maybe_resize (hash_table);
1358 #ifndef G_DISABLE_ASSERT
1359 hash_table->version++;
1366 * g_hash_table_remove:
1367 * @hash_table: a #GHashTable
1368 * @key: the key to remove
1370 * Removes a key and its associated value from a #GHashTable.
1372 * If the #GHashTable was created using g_hash_table_new_full(), the
1373 * key and value are freed using the supplied destroy functions, otherwise
1374 * you have to make sure that any dynamically allocated values are freed
1377 * Returns: %TRUE if the key was found and removed from the #GHashTable
1380 g_hash_table_remove (GHashTable *hash_table,
1383 return g_hash_table_remove_internal (hash_table, key, TRUE);
1387 * g_hash_table_steal:
1388 * @hash_table: a #GHashTable
1389 * @key: the key to remove
1391 * Removes a key and its associated value from a #GHashTable without
1392 * calling the key and value destroy functions.
1394 * Returns: %TRUE if the key was found and removed from the #GHashTable
1397 g_hash_table_steal (GHashTable *hash_table,
1400 return g_hash_table_remove_internal (hash_table, key, FALSE);
1404 * g_hash_table_remove_all:
1405 * @hash_table: a #GHashTable
1407 * Removes all keys and their associated values from a #GHashTable.
1409 * If the #GHashTable was created using g_hash_table_new_full(),
1410 * the keys and values are freed using the supplied destroy functions,
1411 * otherwise you have to make sure that any dynamically allocated
1412 * values are freed yourself.
1417 g_hash_table_remove_all (GHashTable *hash_table)
1419 g_return_if_fail (hash_table != NULL);
1421 #ifndef G_DISABLE_ASSERT
1422 if (hash_table->nnodes != 0)
1423 hash_table->version++;
1426 g_hash_table_remove_all_nodes (hash_table, TRUE, FALSE);
1427 g_hash_table_maybe_resize (hash_table);
1431 * g_hash_table_steal_all:
1432 * @hash_table: a #GHashTable
1434 * Removes all keys and their associated values from a #GHashTable
1435 * without calling the key and value destroy functions.
1440 g_hash_table_steal_all (GHashTable *hash_table)
1442 g_return_if_fail (hash_table != NULL);
1444 #ifndef G_DISABLE_ASSERT
1445 if (hash_table->nnodes != 0)
1446 hash_table->version++;
1449 g_hash_table_remove_all_nodes (hash_table, FALSE, FALSE);
1450 g_hash_table_maybe_resize (hash_table);
1454 * g_hash_table_foreach_remove_or_steal:
1455 * @hash_table: a #GHashTable
1456 * @func: the user's callback function
1457 * @user_data: data for @func
1458 * @notify: %TRUE if the destroy notify handlers are to be called
1460 * Implements the common logic for g_hash_table_foreach_remove()
1461 * and g_hash_table_foreach_steal().
1463 * Iterates over every node in the table, calling @func with the key
1464 * and value of the node (and @user_data). If @func returns %TRUE the
1465 * node is removed from the table.
1467 * If @notify is true then the destroy notify handlers will be called
1468 * for each removed node.
1471 g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
1478 #ifndef G_DISABLE_ASSERT
1479 gint version = hash_table->version;
1482 for (i = 0; i < hash_table->size; i++)
1484 guint node_hash = hash_table->hashes[i];
1485 gpointer node_key = hash_table->keys[i];
1486 gpointer node_value = hash_table->values[i];
1488 if (HASH_IS_REAL (node_hash) &&
1489 (* func) (node_key, node_value, user_data))
1491 g_hash_table_remove_node (hash_table, i, notify);
1495 #ifndef G_DISABLE_ASSERT
1496 g_return_val_if_fail (version == hash_table->version, 0);
1500 g_hash_table_maybe_resize (hash_table);
1502 #ifndef G_DISABLE_ASSERT
1504 hash_table->version++;
1511 * g_hash_table_foreach_remove:
1512 * @hash_table: a #GHashTable
1513 * @func: the function to call for each key/value pair
1514 * @user_data: user data to pass to the function
1516 * Calls the given function for each key/value pair in the
1517 * #GHashTable. If the function returns %TRUE, then the key/value
1518 * pair is removed from the #GHashTable. If you supplied key or
1519 * value destroy functions when creating the #GHashTable, they are
1520 * used to free the memory allocated for the removed keys and values.
1522 * See #GHashTableIter for an alternative way to loop over the
1523 * key/value pairs in the hash table.
1525 * Returns: the number of key/value pairs removed
1528 g_hash_table_foreach_remove (GHashTable *hash_table,
1532 g_return_val_if_fail (hash_table != NULL, 0);
1533 g_return_val_if_fail (func != NULL, 0);
1535 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
1539 * g_hash_table_foreach_steal:
1540 * @hash_table: a #GHashTable
1541 * @func: the function to call for each key/value pair
1542 * @user_data: user data to pass to the function
1544 * Calls the given function for each key/value pair in the
1545 * #GHashTable. If the function returns %TRUE, then the key/value
1546 * pair is removed from the #GHashTable, but no key or value
1547 * destroy functions are called.
1549 * See #GHashTableIter for an alternative way to loop over the
1550 * key/value pairs in the hash table.
1552 * Returns: the number of key/value pairs removed.
1555 g_hash_table_foreach_steal (GHashTable *hash_table,
1559 g_return_val_if_fail (hash_table != NULL, 0);
1560 g_return_val_if_fail (func != NULL, 0);
1562 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
1566 * g_hash_table_foreach:
1567 * @hash_table: a #GHashTable
1568 * @func: the function to call for each key/value pair
1569 * @user_data: user data to pass to the function
1571 * Calls the given function for each of the key/value pairs in the
1572 * #GHashTable. The function is passed the key and value of each
1573 * pair, and the given @user_data parameter. The hash table may not
1574 * be modified while iterating over it (you can't add/remove
1575 * items). To remove all items matching a predicate, use
1576 * g_hash_table_foreach_remove().
1578 * See g_hash_table_find() for performance caveats for linear
1579 * order searches in contrast to g_hash_table_lookup().
1582 g_hash_table_foreach (GHashTable *hash_table,
1587 #ifndef G_DISABLE_ASSERT
1591 g_return_if_fail (hash_table != NULL);
1592 g_return_if_fail (func != NULL);
1594 #ifndef G_DISABLE_ASSERT
1595 version = hash_table->version;
1598 for (i = 0; i < hash_table->size; i++)
1600 guint node_hash = hash_table->hashes[i];
1601 gpointer node_key = hash_table->keys[i];
1602 gpointer node_value = hash_table->values[i];
1604 if (HASH_IS_REAL (node_hash))
1605 (* func) (node_key, node_value, user_data);
1607 #ifndef G_DISABLE_ASSERT
1608 g_return_if_fail (version == hash_table->version);
1614 * g_hash_table_find:
1615 * @hash_table: a #GHashTable
1616 * @predicate: function to test the key/value pairs for a certain property
1617 * @user_data: user data to pass to the function
1619 * Calls the given function for key/value pairs in the #GHashTable
1620 * until @predicate returns %TRUE. The function is passed the key
1621 * and value of each pair, and the given @user_data parameter. The
1622 * hash table may not be modified while iterating over it (you can't
1623 * add/remove items).
1625 * Note, that hash tables are really only optimized for forward
1626 * lookups, i.e. g_hash_table_lookup(). So code that frequently issues
1627 * g_hash_table_find() or g_hash_table_foreach() (e.g. in the order of
1628 * once per every entry in a hash table) should probably be reworked
1629 * to use additional or different data structures for reverse lookups
1630 * (keep in mind that an O(n) find/foreach operation issued for all n
1631 * values in a hash table ends up needing O(n*n) operations).
1633 * Returns: (allow-none): The value of the first key/value pair is returned,
1634 * for which @predicate evaluates to %TRUE. If no pair with the
1635 * requested property is found, %NULL is returned.
1640 g_hash_table_find (GHashTable *hash_table,
1645 #ifndef G_DISABLE_ASSERT
1650 g_return_val_if_fail (hash_table != NULL, NULL);
1651 g_return_val_if_fail (predicate != NULL, NULL);
1653 #ifndef G_DISABLE_ASSERT
1654 version = hash_table->version;
1659 for (i = 0; i < hash_table->size; i++)
1661 guint node_hash = hash_table->hashes[i];
1662 gpointer node_key = hash_table->keys[i];
1663 gpointer node_value = hash_table->values[i];
1665 if (HASH_IS_REAL (node_hash))
1666 match = predicate (node_key, node_value, user_data);
1668 #ifndef G_DISABLE_ASSERT
1669 g_return_val_if_fail (version == hash_table->version, NULL);
1680 * g_hash_table_size:
1681 * @hash_table: a #GHashTable
1683 * Returns the number of elements contained in the #GHashTable.
1685 * Returns: the number of key/value pairs in the #GHashTable.
1688 g_hash_table_size (GHashTable *hash_table)
1690 g_return_val_if_fail (hash_table != NULL, 0);
1692 return hash_table->nnodes;
1696 * g_hash_table_get_keys:
1697 * @hash_table: a #GHashTable
1699 * Retrieves every key inside @hash_table. The returned data is valid
1700 * until changes to the hash release those keys.
1702 * Returns: a #GList containing all the keys inside the hash
1703 * table. The content of the list is owned by the hash table and
1704 * should not be modified or freed. Use g_list_free() when done
1710 g_hash_table_get_keys (GHashTable *hash_table)
1715 g_return_val_if_fail (hash_table != NULL, NULL);
1718 for (i = 0; i < hash_table->size; i++)
1720 if (HASH_IS_REAL (hash_table->hashes[i]))
1721 retval = g_list_prepend (retval, hash_table->keys[i]);
1728 * g_hash_table_get_keys_as_array:
1729 * @hash_table: a #GHashTable
1730 * @length: (out): the length of the returned array
1732 * Retrieves every key inside @hash_table, as an array.
1734 * The returned array is %NULL-terminated but may contain %NULL as a
1735 * key. Use @length to determine the true length if it's possible that
1736 * %NULL was used as the value for a key.
1738 * Note: in the common case of a string-keyed #GHashTable, the return
1739 * value of this function can be conveniently cast to (gchar **).
1741 * You should always free the return result with g_free(). In the
1742 * above-mentioned case of a string-keyed hash table, it may be
1743 * appropriate to use g_strfreev() if you call g_hash_table_steal_all()
1744 * first to transfer ownership of the keys.
1746 * Returns: (array length=length) (transfer container): a
1747 * %NULL-terminated array containing each key from the table.
1752 g_hash_table_get_keys_as_array (GHashTable *hash_table,
1758 result = g_new (gpointer, hash_table->nnodes + 1);
1759 for (i = 0; i < hash_table->size; i++)
1761 if (HASH_IS_REAL (hash_table->hashes[i]))
1762 result[j++] = hash_table->keys[i];
1764 g_assert_cmpint (j, ==, hash_table->nnodes);
1774 * g_hash_table_get_values:
1775 * @hash_table: a #GHashTable
1777 * Retrieves every value inside @hash_table. The returned data
1778 * is valid until @hash_table is modified.
1780 * Returns: a #GList containing all the values inside the hash
1781 * table. The content of the list is owned by the hash table and
1782 * should not be modified or freed. Use g_list_free() when done
1788 g_hash_table_get_values (GHashTable *hash_table)
1793 g_return_val_if_fail (hash_table != NULL, NULL);
1796 for (i = 0; i < hash_table->size; i++)
1798 if (HASH_IS_REAL (hash_table->hashes[i]))
1799 retval = g_list_prepend (retval, hash_table->values[i]);
1811 * @v2: a key to compare with @v1
1813 * Compares two strings for byte-by-byte equality and returns %TRUE
1814 * if they are equal. It can be passed to g_hash_table_new() as the
1815 * @key_equal_func parameter, when using non-%NULL strings as keys in a
1818 * Note that this function is primarily meant as a hash table comparison
1819 * function. For a general-purpose, %NULL-safe string comparison function,
1822 * Returns: %TRUE if the two keys match
1825 g_str_equal (gconstpointer v1,
1828 const gchar *string1 = v1;
1829 const gchar *string2 = v2;
1831 return strcmp (string1, string2) == 0;
1838 * Converts a string to a hash value.
1840 * This function implements the widely used "djb" hash apparently
1841 * posted by Daniel Bernstein to comp.lang.c some time ago. The 32
1842 * bit unsigned hash value starts at 5381 and for each byte 'c' in
1843 * the string, is updated: `hash = hash * 33 + c`. This function
1844 * uses the signed value of each byte.
1846 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1847 * when using non-%NULL strings as keys in a #GHashTable.
1849 * Returns: a hash value corresponding to the key
1852 g_str_hash (gconstpointer v)
1854 const signed char *p;
1857 for (p = v; *p != '\0'; p++)
1858 h = (h << 5) + h + *p;
1865 * @v: (allow-none): a #gpointer key
1867 * Converts a gpointer to a hash value.
1868 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1869 * when using opaque pointers compared by pointer value as keys in a
1872 * This hash function is also appropriate for keys that are integers
1873 * stored in pointers, such as `GINT_TO_POINTER (n)`.
1875 * Returns: a hash value corresponding to the key.
1878 g_direct_hash (gconstpointer v)
1880 return GPOINTER_TO_UINT (v);
1885 * @v1: (allow-none): a key
1886 * @v2: (allow-none): a key to compare with @v1
1888 * Compares two #gpointer arguments and returns %TRUE if they are equal.
1889 * It can be passed to g_hash_table_new() as the @key_equal_func
1890 * parameter, when using opaque pointers compared by pointer value as
1891 * keys in a #GHashTable.
1893 * This equality function is also appropriate for keys that are integers
1894 * stored in pointers, such as `GINT_TO_POINTER (n)`.
1896 * Returns: %TRUE if the two keys match.
1899 g_direct_equal (gconstpointer v1,
1907 * @v1: a pointer to a #gint key
1908 * @v2: a pointer to a #gint key to compare with @v1
1910 * Compares the two #gint values being pointed to and returns
1911 * %TRUE if they are equal.
1912 * It can be passed to g_hash_table_new() as the @key_equal_func
1913 * parameter, when using non-%NULL pointers to integers as keys in a
1916 * Note that this function acts on pointers to #gint, not on #gint
1917 * directly: if your hash table's keys are of the form
1918 * `GINT_TO_POINTER (n)`, use g_direct_equal() instead.
1920 * Returns: %TRUE if the two keys match.
1923 g_int_equal (gconstpointer v1,
1926 return *((const gint*) v1) == *((const gint*) v2);
1931 * @v: a pointer to a #gint key
1933 * Converts a pointer to a #gint to a hash value.
1934 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1935 * when using non-%NULL pointers to integer values as keys in a #GHashTable.
1937 * Note that this function acts on pointers to #gint, not on #gint
1938 * directly: if your hash table's keys are of the form
1939 * `GINT_TO_POINTER (n)`, use g_direct_hash() instead.
1941 * Returns: a hash value corresponding to the key.
1944 g_int_hash (gconstpointer v)
1946 return *(const gint*) v;
1951 * @v1: a pointer to a #gint64 key
1952 * @v2: a pointer to a #gint64 key to compare with @v1
1954 * Compares the two #gint64 values being pointed to and returns
1955 * %TRUE if they are equal.
1956 * It can be passed to g_hash_table_new() as the @key_equal_func
1957 * parameter, when using non-%NULL pointers to 64-bit integers as keys in a
1960 * Returns: %TRUE if the two keys match.
1965 g_int64_equal (gconstpointer v1,
1968 return *((const gint64*) v1) == *((const gint64*) v2);
1973 * @v: a pointer to a #gint64 key
1975 * Converts a pointer to a #gint64 to a hash value.
1977 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1978 * when using non-%NULL pointers to 64-bit integer values as keys in a
1981 * Returns: a hash value corresponding to the key.
1986 g_int64_hash (gconstpointer v)
1988 return (guint) *(const gint64*) v;
1993 * @v1: a pointer to a #gdouble key
1994 * @v2: a pointer to a #gdouble key to compare with @v1
1996 * Compares the two #gdouble values being pointed to and returns
1997 * %TRUE if they are equal.
1998 * It can be passed to g_hash_table_new() as the @key_equal_func
1999 * parameter, when using non-%NULL pointers to doubles as keys in a
2002 * Returns: %TRUE if the two keys match.
2007 g_double_equal (gconstpointer v1,
2010 return *((const gdouble*) v1) == *((const gdouble*) v2);
2015 * @v: a pointer to a #gdouble key
2017 * Converts a pointer to a #gdouble to a hash value.
2018 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2019 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2020 * when using non-%NULL pointers to doubles as keys in a #GHashTable.
2022 * Returns: a hash value corresponding to the key.
2027 g_double_hash (gconstpointer v)
2029 return (guint) *(const gdouble*) v;