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 * <link linkend="glib-Hash-Tables">Hash Table</link>. It should only be
99 * accessed via the 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 <literal>GINT_TO_POINTER (n)</literal> (or similar macros).
117 * <!-- FIXME: Need more here. --> A good hash functions should produce
118 * hash values that are evenly distributed over a fairly large range.
119 * The modulus is taken with the hash table size (a prime number) to
120 * find the 'bucket' to place each key into. The function should also
121 * be very fast, since it is called for each key lookup.
123 * Note that the hash functions provided by GLib have these qualities,
124 * but are not particularly robust against manufactured keys that
125 * cause hash collisions. Therefore, you should consider choosing
126 * a more secure hash function when using a GHashTable with keys
127 * that originate in untrusted data (such as HTTP requests).
128 * Using g_str_hash() in that situation might make your application
129 * vulerable to <ulink url="https://lwn.net/Articles/474912/">Algorithmic Complexity Attacks</ulink>.
131 * The key to choosing a good hash is unpredictability. Even
132 * cryptographic hashes are very easy to find collisions for when the
133 * remainder is taken modulo a somewhat predictable prime number. There
134 * must be an element of randomness that an attacker is unable to guess.
136 * Returns: the hash value corresponding to the key
142 * @value: the value corresponding to the key
143 * @user_data: user data passed to g_hash_table_foreach()
145 * Specifies the type of the function passed to g_hash_table_foreach().
146 * It is called with each key/value pair, together with the @user_data
147 * parameter which is passed to g_hash_table_foreach().
153 * @value: the value associated with the key
154 * @user_data: user data passed to g_hash_table_remove()
156 * Specifies the type of the function passed to
157 * g_hash_table_foreach_remove(). It is called with each key/value
158 * pair, together with the @user_data parameter passed to
159 * g_hash_table_foreach_remove(). It should return %TRUE if the
160 * key/value pair should be removed from the #GHashTable.
162 * Returns: %TRUE if the key/value pair should be removed from the
169 * @b: a value to compare with
171 * Specifies the type of a function used to test two values for
172 * equality. The function should return %TRUE if both values are equal
173 * and %FALSE otherwise.
175 * Returns: %TRUE if @a = @b; %FALSE otherwise
181 * A GHashTableIter structure represents an iterator that can be used
182 * to iterate over the elements of a #GHashTable. GHashTableIter
183 * structures are typically allocated on the stack and then initialized
184 * with g_hash_table_iter_init().
188 * g_hash_table_freeze:
189 * @hash_table: a #GHashTable
191 * This function is deprecated and will be removed in the next major
192 * release of GLib. It does nothing.
197 * @hash_table: a #GHashTable
199 * This function is deprecated and will be removed in the next major
200 * release of GLib. It does nothing.
203 #define HASH_TABLE_MIN_SHIFT 3 /* 1 << 3 == 8 buckets */
205 #define UNUSED_HASH_VALUE 0
206 #define TOMBSTONE_HASH_VALUE 1
207 #define HASH_IS_UNUSED(h_) ((h_) == UNUSED_HASH_VALUE)
208 #define HASH_IS_TOMBSTONE(h_) ((h_) == TOMBSTONE_HASH_VALUE)
209 #define HASH_IS_REAL(h_) ((h_) >= 2)
217 gint noccupied; /* nnodes + tombstones */
224 GEqualFunc key_equal_func;
226 #ifndef G_DISABLE_ASSERT
228 * Tracks the structure of the hash table, not its contents: is only
229 * incremented when a node is added or removed (is not incremented
230 * when the key or data of a node is modified).
234 GDestroyNotify key_destroy_func;
235 GDestroyNotify value_destroy_func;
240 GHashTable *hash_table;
248 G_STATIC_ASSERT (sizeof (GHashTableIter) == sizeof (RealIter));
249 G_STATIC_ASSERT (_g_alignof (GHashTableIter) >= _g_alignof (RealIter));
251 /* Each table size has an associated prime modulo (the first prime
252 * lower than the table size) used to find the initial bucket. Probing
253 * then works modulo 2^n. The prime modulo is necessary to get a
254 * good distribution with poor hash functions.
256 static const gint prime_mod [] =
274 65521, /* For 1 << 16 */
289 2147483647 /* For 1 << 31 */
293 g_hash_table_set_shift (GHashTable *hash_table, gint shift)
298 hash_table->size = 1 << shift;
299 hash_table->mod = prime_mod [shift];
301 for (i = 0; i < shift; i++)
307 hash_table->mask = mask;
311 g_hash_table_find_closest_shift (gint n)
322 g_hash_table_set_shift_from_size (GHashTable *hash_table, gint size)
326 shift = g_hash_table_find_closest_shift (size);
327 shift = MAX (shift, HASH_TABLE_MIN_SHIFT);
329 g_hash_table_set_shift (hash_table, shift);
333 * g_hash_table_lookup_node:
334 * @hash_table: our #GHashTable
335 * @key: the key to lookup against
336 * @hash_return: key hash return location
338 * Performs a lookup in the hash table, preserving extra information
339 * usually needed for insertion.
341 * This function first computes the hash value of the key using the
342 * user's hash function.
344 * If an entry in the table matching @key is found then this function
345 * returns the index of that entry in the table, and if not, the
346 * index of an unused node (empty or tombstone) where the key can be
349 * The computed hash value is returned in the variable pointed to
350 * by @hash_return. This is to save insertions from having to compute
351 * the hash record again for the new record.
353 * Returns: index of the described node
356 g_hash_table_lookup_node (GHashTable *hash_table,
363 guint first_tombstone = 0;
364 gboolean have_tombstone = FALSE;
367 hash_value = hash_table->hash_func (key);
368 if (G_UNLIKELY (!HASH_IS_REAL (hash_value)))
371 *hash_return = hash_value;
373 node_index = hash_value % hash_table->mod;
374 node_hash = hash_table->hashes[node_index];
376 while (!HASH_IS_UNUSED (node_hash))
378 /* We first check if our full hash values
379 * are equal so we can avoid calling the full-blown
380 * key equality function in most cases.
382 if (node_hash == hash_value)
384 gpointer node_key = hash_table->keys[node_index];
386 if (hash_table->key_equal_func)
388 if (hash_table->key_equal_func (node_key, key))
391 else if (node_key == key)
396 else if (HASH_IS_TOMBSTONE (node_hash) && !have_tombstone)
398 first_tombstone = node_index;
399 have_tombstone = TRUE;
404 node_index &= hash_table->mask;
405 node_hash = hash_table->hashes[node_index];
409 return first_tombstone;
415 * g_hash_table_remove_node:
416 * @hash_table: our #GHashTable
417 * @node: pointer to node to remove
418 * @notify: %TRUE if the destroy notify handlers are to be called
420 * Removes a node from the hash table and updates the node count.
421 * The node is replaced by a tombstone. No table resize is performed.
423 * If @notify is %TRUE then the destroy notify functions are called
424 * for the key and value of the hash node.
427 g_hash_table_remove_node (GHashTable *hash_table,
434 key = hash_table->keys[i];
435 value = hash_table->values[i];
437 /* Erect tombstone */
438 hash_table->hashes[i] = TOMBSTONE_HASH_VALUE;
441 hash_table->keys[i] = NULL;
442 hash_table->values[i] = NULL;
444 hash_table->nnodes--;
446 if (notify && hash_table->key_destroy_func)
447 hash_table->key_destroy_func (key);
449 if (notify && hash_table->value_destroy_func)
450 hash_table->value_destroy_func (value);
455 * g_hash_table_remove_all_nodes:
456 * @hash_table: our #GHashTable
457 * @notify: %TRUE if the destroy notify handlers are to be called
459 * Removes all nodes from the table. Since this may be a precursor to
460 * freeing the table entirely, no resize is performed.
462 * If @notify is %TRUE then the destroy notify functions are called
463 * for the key and value of the hash node.
466 g_hash_table_remove_all_nodes (GHashTable *hash_table,
473 hash_table->nnodes = 0;
474 hash_table->noccupied = 0;
477 (hash_table->key_destroy_func == NULL &&
478 hash_table->value_destroy_func == NULL))
480 memset (hash_table->hashes, 0, hash_table->size * sizeof (guint));
481 memset (hash_table->keys, 0, hash_table->size * sizeof (gpointer));
482 memset (hash_table->values, 0, hash_table->size * sizeof (gpointer));
487 for (i = 0; i < hash_table->size; i++)
489 if (HASH_IS_REAL (hash_table->hashes[i]))
491 key = hash_table->keys[i];
492 value = hash_table->values[i];
494 hash_table->hashes[i] = UNUSED_HASH_VALUE;
495 hash_table->keys[i] = NULL;
496 hash_table->values[i] = NULL;
498 if (hash_table->key_destroy_func != NULL)
499 hash_table->key_destroy_func (key);
501 if (hash_table->value_destroy_func != NULL)
502 hash_table->value_destroy_func (value);
504 else if (HASH_IS_TOMBSTONE (hash_table->hashes[i]))
506 hash_table->hashes[i] = UNUSED_HASH_VALUE;
512 * g_hash_table_resize:
513 * @hash_table: our #GHashTable
515 * Resizes the hash table to the optimal size based on the number of
516 * nodes currently held. If you call this function then a resize will
517 * occur, even if one does not need to occur.
518 * Use g_hash_table_maybe_resize() instead.
520 * This function may "resize" the hash table to its current size, with
521 * the side effect of cleaning up tombstones and otherwise optimizing
522 * the probe sequences.
525 g_hash_table_resize (GHashTable *hash_table)
528 gpointer *new_values;
533 old_size = hash_table->size;
534 g_hash_table_set_shift_from_size (hash_table, hash_table->nnodes * 2);
536 new_keys = g_new0 (gpointer, hash_table->size);
537 if (hash_table->keys == hash_table->values)
538 new_values = new_keys;
540 new_values = g_new0 (gpointer, hash_table->size);
541 new_hashes = g_new0 (guint, hash_table->size);
543 for (i = 0; i < old_size; i++)
545 guint node_hash = hash_table->hashes[i];
549 if (!HASH_IS_REAL (node_hash))
552 hash_val = node_hash % hash_table->mod;
554 while (!HASH_IS_UNUSED (new_hashes[hash_val]))
558 hash_val &= hash_table->mask;
561 new_hashes[hash_val] = hash_table->hashes[i];
562 new_keys[hash_val] = hash_table->keys[i];
563 new_values[hash_val] = hash_table->values[i];
566 if (hash_table->keys != hash_table->values)
567 g_free (hash_table->values);
569 g_free (hash_table->keys);
570 g_free (hash_table->hashes);
572 hash_table->keys = new_keys;
573 hash_table->values = new_values;
574 hash_table->hashes = new_hashes;
576 hash_table->noccupied = hash_table->nnodes;
580 * g_hash_table_maybe_resize:
581 * @hash_table: our #GHashTable
583 * Resizes the hash table, if needed.
585 * Essentially, calls g_hash_table_resize() if the table has strayed
586 * too far from its ideal size for its number of nodes.
589 g_hash_table_maybe_resize (GHashTable *hash_table)
591 gint noccupied = hash_table->noccupied;
592 gint size = hash_table->size;
594 if ((size > hash_table->nnodes * 4 && size > 1 << HASH_TABLE_MIN_SHIFT) ||
595 (size <= noccupied + (noccupied / 16)))
596 g_hash_table_resize (hash_table);
601 * @hash_func: a function to create a hash value from a key
602 * @key_equal_func: a function to check two keys for equality
604 * Creates a new #GHashTable with a reference count of 1.
606 * Hash values returned by @hash_func are used to determine where keys
607 * are stored within the #GHashTable data structure. The g_direct_hash(),
608 * g_int_hash(), g_int64_hash(), g_double_hash() and g_str_hash()
609 * functions are provided for some common types of keys.
610 * If @hash_func is %NULL, g_direct_hash() is used.
612 * @key_equal_func is used when looking up keys in the #GHashTable.
613 * The g_direct_equal(), g_int_equal(), g_int64_equal(), g_double_equal()
614 * and g_str_equal() functions are provided for the most common types
615 * of keys. If @key_equal_func is %NULL, keys are compared directly in
616 * a similar fashion to g_direct_equal(), but without the overhead of
619 * Return value: a new #GHashTable
622 g_hash_table_new (GHashFunc hash_func,
623 GEqualFunc key_equal_func)
625 return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
630 * g_hash_table_new_full:
631 * @hash_func: a function to create a hash value from a key
632 * @key_equal_func: a function to check two keys for equality
633 * @key_destroy_func: (allow-none): a function to free the memory allocated for the key
634 * used when removing the entry from the #GHashTable, or %NULL
635 * if you don't want to supply such a function.
636 * @value_destroy_func: (allow-none): a function to free the memory allocated for the
637 * value used when removing the entry from the #GHashTable, or %NULL
638 * if you don't want to supply such a function.
640 * Creates a new #GHashTable like g_hash_table_new() with a reference
641 * count of 1 and allows to specify functions to free the memory
642 * allocated for the key and value that get called when removing the
643 * entry from the #GHashTable.
645 * Return value: a new #GHashTable
648 g_hash_table_new_full (GHashFunc hash_func,
649 GEqualFunc key_equal_func,
650 GDestroyNotify key_destroy_func,
651 GDestroyNotify value_destroy_func)
653 GHashTable *hash_table;
655 hash_table = g_slice_new (GHashTable);
656 g_hash_table_set_shift (hash_table, HASH_TABLE_MIN_SHIFT);
657 hash_table->nnodes = 0;
658 hash_table->noccupied = 0;
659 hash_table->hash_func = hash_func ? hash_func : g_direct_hash;
660 hash_table->key_equal_func = key_equal_func;
661 hash_table->ref_count = 1;
662 #ifndef G_DISABLE_ASSERT
663 hash_table->version = 0;
665 hash_table->key_destroy_func = key_destroy_func;
666 hash_table->value_destroy_func = value_destroy_func;
667 hash_table->keys = g_new0 (gpointer, hash_table->size);
668 hash_table->values = hash_table->keys;
669 hash_table->hashes = g_new0 (guint, hash_table->size);
675 * g_hash_table_iter_init:
676 * @iter: an uninitialized #GHashTableIter
677 * @hash_table: a #GHashTable
679 * Initializes a key/value pair iterator and associates it with
680 * @hash_table. Modifying the hash table after calling this function
681 * invalidates the returned iterator.
683 * GHashTableIter iter;
684 * gpointer key, value;
686 * g_hash_table_iter_init (&iter, hash_table);
687 * while (g_hash_table_iter_next (&iter, &key, &value))
689 * /* do something with key and value */
696 g_hash_table_iter_init (GHashTableIter *iter,
697 GHashTable *hash_table)
699 RealIter *ri = (RealIter *) iter;
701 g_return_if_fail (iter != NULL);
702 g_return_if_fail (hash_table != NULL);
704 ri->hash_table = hash_table;
706 #ifndef G_DISABLE_ASSERT
707 ri->version = hash_table->version;
712 * g_hash_table_iter_next:
713 * @iter: an initialized #GHashTableIter
714 * @key: (allow-none): a location to store the key, or %NULL
715 * @value: (allow-none): a location to store the value, or %NULL
717 * Advances @iter and retrieves the key and/or value that are now
718 * pointed to as a result of this advancement. If %FALSE is returned,
719 * @key and @value are not set, and the iterator becomes invalid.
721 * Return value: %FALSE if the end of the #GHashTable has been reached.
726 g_hash_table_iter_next (GHashTableIter *iter,
730 RealIter *ri = (RealIter *) iter;
733 g_return_val_if_fail (iter != NULL, FALSE);
734 #ifndef G_DISABLE_ASSERT
735 g_return_val_if_fail (ri->version == ri->hash_table->version, FALSE);
737 g_return_val_if_fail (ri->position < ri->hash_table->size, FALSE);
739 position = ri->position;
744 if (position >= ri->hash_table->size)
746 ri->position = position;
750 while (!HASH_IS_REAL (ri->hash_table->hashes[position]));
753 *key = ri->hash_table->keys[position];
755 *value = ri->hash_table->values[position];
757 ri->position = position;
762 * g_hash_table_iter_get_hash_table:
763 * @iter: an initialized #GHashTableIter
765 * Returns the #GHashTable associated with @iter.
767 * Return value: the #GHashTable associated with @iter.
772 g_hash_table_iter_get_hash_table (GHashTableIter *iter)
774 g_return_val_if_fail (iter != NULL, NULL);
776 return ((RealIter *) iter)->hash_table;
780 iter_remove_or_steal (RealIter *ri, gboolean notify)
782 g_return_if_fail (ri != NULL);
783 #ifndef G_DISABLE_ASSERT
784 g_return_if_fail (ri->version == ri->hash_table->version);
786 g_return_if_fail (ri->position >= 0);
787 g_return_if_fail (ri->position < ri->hash_table->size);
789 g_hash_table_remove_node (ri->hash_table, ri->position, notify);
791 #ifndef G_DISABLE_ASSERT
793 ri->hash_table->version++;
798 * g_hash_table_iter_remove:
799 * @iter: an initialized #GHashTableIter
801 * Removes the key/value pair currently pointed to by the iterator
802 * from its associated #GHashTable. Can only be called after
803 * g_hash_table_iter_next() returned %TRUE, and cannot be called
804 * more than once for the same key/value pair.
806 * If the #GHashTable was created using g_hash_table_new_full(),
807 * the key and value are freed using the supplied destroy functions,
808 * otherwise you have to make sure that any dynamically allocated
809 * values are freed yourself.
814 g_hash_table_iter_remove (GHashTableIter *iter)
816 iter_remove_or_steal ((RealIter *) iter, TRUE);
820 * g_hash_table_insert_node:
821 * @hash_table: our #GHashTable
822 * @node_index: pointer to node to insert/replace
823 * @key_hash: key hash
824 * @key: (allow-none): key to replace with, or %NULL
825 * @value: value to replace with
826 * @keep_new_key: whether to replace the key in the node with @key
827 * @reusing_key: whether @key was taken out of the existing node
829 * Inserts a value at @node_index in the hash table and updates it.
831 * If @key has been taken out of the existing node (ie it is not
832 * passed in via a g_hash_table_insert/replace) call, then @reusing_key
835 * Returns: %TRUE if the key did not exist yet
838 g_hash_table_insert_node (GHashTable *hash_table,
843 gboolean keep_new_key,
844 gboolean reusing_key)
846 gboolean already_exists;
848 gpointer key_to_free = NULL;
849 gpointer value_to_free = NULL;
851 old_hash = hash_table->hashes[node_index];
852 already_exists = HASH_IS_REAL (old_hash);
854 /* Proceed in three steps. First, deal with the key because it is the
855 * most complicated. Then consider if we need to split the table in
856 * two (because writing the value will result in the set invariant
857 * becoming broken). Then deal with the value.
859 * There are three cases for the key:
861 * - entry already exists in table, reusing key:
862 * free the just-passed-in new_key and use the existing value
864 * - entry already exists in table, not reusing key:
865 * free the entry in the table, use the new key
867 * - entry not already in table:
868 * use the new key, free nothing
870 * We update the hash at the same time...
874 /* Note: we must record the old value before writing the new key
875 * because we might change the value in the event that the two
878 value_to_free = hash_table->values[node_index];
882 key_to_free = hash_table->keys[node_index];
883 hash_table->keys[node_index] = new_key;
886 key_to_free = new_key;
890 hash_table->hashes[node_index] = key_hash;
891 hash_table->keys[node_index] = new_key;
894 /* Step two: check if the value that we are about to write to the
895 * table is the same as the key in the same position. If it's not,
898 if (G_UNLIKELY (hash_table->keys == hash_table->values && hash_table->keys[node_index] != new_value))
899 hash_table->values = g_memdup (hash_table->keys, sizeof (gpointer) * hash_table->size);
901 /* Step 3: Actually do the write */
902 hash_table->values[node_index] = new_value;
904 /* Now, the bookkeeping... */
907 hash_table->nnodes++;
909 if (HASH_IS_UNUSED (old_hash))
911 /* We replaced an empty node, and not a tombstone */
912 hash_table->noccupied++;
913 g_hash_table_maybe_resize (hash_table);
916 #ifndef G_DISABLE_ASSERT
917 hash_table->version++;
923 if (hash_table->key_destroy_func && !reusing_key)
924 (* hash_table->key_destroy_func) (key_to_free);
925 if (hash_table->value_destroy_func)
926 (* hash_table->value_destroy_func) (value_to_free);
929 return !already_exists;
933 * g_hash_table_iter_replace:
934 * @iter: an initialized #GHashTableIter
935 * @value: the value to replace with
937 * Replaces the value currently pointed to by the iterator
938 * from its associated #GHashTable. Can only be called after
939 * g_hash_table_iter_next() returned %TRUE.
941 * If you supplied a @value_destroy_func when creating the
942 * #GHashTable, the old value is freed using that function.
947 g_hash_table_iter_replace (GHashTableIter *iter,
954 ri = (RealIter *) iter;
956 g_return_if_fail (ri != NULL);
957 #ifndef G_DISABLE_ASSERT
958 g_return_if_fail (ri->version == ri->hash_table->version);
960 g_return_if_fail (ri->position >= 0);
961 g_return_if_fail (ri->position < ri->hash_table->size);
963 node_hash = ri->hash_table->hashes[ri->position];
964 key = ri->hash_table->keys[ri->position];
966 g_hash_table_insert_node (ri->hash_table, ri->position, node_hash, key, value, TRUE, TRUE);
968 #ifndef G_DISABLE_ASSERT
970 ri->hash_table->version++;
975 * g_hash_table_iter_steal:
976 * @iter: an initialized #GHashTableIter
978 * Removes the key/value pair currently pointed to by the
979 * iterator from its associated #GHashTable, without calling
980 * the key and value destroy functions. Can only be called
981 * after g_hash_table_iter_next() returned %TRUE, and cannot
982 * be called more than once for the same key/value pair.
987 g_hash_table_iter_steal (GHashTableIter *iter)
989 iter_remove_or_steal ((RealIter *) iter, FALSE);
995 * @hash_table: a valid #GHashTable
997 * Atomically increments the reference count of @hash_table by one.
998 * This function is MT-safe and may be called from any thread.
1000 * Return value: the passed in #GHashTable
1005 g_hash_table_ref (GHashTable *hash_table)
1007 g_return_val_if_fail (hash_table != NULL, NULL);
1009 g_atomic_int_inc (&hash_table->ref_count);
1015 * g_hash_table_unref:
1016 * @hash_table: a valid #GHashTable
1018 * Atomically decrements the reference count of @hash_table by one.
1019 * If the reference count drops to 0, all keys and values will be
1020 * destroyed, and all memory allocated by the hash table is released.
1021 * This function is MT-safe and may be called from any thread.
1026 g_hash_table_unref (GHashTable *hash_table)
1028 g_return_if_fail (hash_table != NULL);
1030 if (g_atomic_int_dec_and_test (&hash_table->ref_count))
1032 g_hash_table_remove_all_nodes (hash_table, TRUE);
1033 if (hash_table->keys != hash_table->values)
1034 g_free (hash_table->values);
1035 g_free (hash_table->keys);
1036 g_free (hash_table->hashes);
1037 g_slice_free (GHashTable, hash_table);
1042 * g_hash_table_destroy:
1043 * @hash_table: a #GHashTable
1045 * Destroys all keys and values in the #GHashTable and decrements its
1046 * reference count by 1. If keys and/or values are dynamically allocated,
1047 * you should either free them first or create the #GHashTable with destroy
1048 * notifiers using g_hash_table_new_full(). In the latter case the destroy
1049 * functions you supplied will be called on all keys and values during the
1050 * destruction phase.
1053 g_hash_table_destroy (GHashTable *hash_table)
1055 g_return_if_fail (hash_table != NULL);
1057 g_hash_table_remove_all (hash_table);
1058 g_hash_table_unref (hash_table);
1062 * g_hash_table_lookup:
1063 * @hash_table: a #GHashTable
1064 * @key: the key to look up
1066 * Looks up a key in a #GHashTable. Note that this function cannot
1067 * distinguish between a key that is not present and one which is present
1068 * and has the value %NULL. If you need this distinction, use
1069 * g_hash_table_lookup_extended().
1071 * Return value: (allow-none): the associated value, or %NULL if the key is not found
1074 g_hash_table_lookup (GHashTable *hash_table,
1080 g_return_val_if_fail (hash_table != NULL, NULL);
1082 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1084 return HASH_IS_REAL (hash_table->hashes[node_index])
1085 ? hash_table->values[node_index]
1090 * g_hash_table_lookup_extended:
1091 * @hash_table: a #GHashTable
1092 * @lookup_key: the key to look up
1093 * @orig_key: (allow-none): return location for the original key, or %NULL
1094 * @value: (allow-none): return location for the value associated with the key, or %NULL
1096 * Looks up a key in the #GHashTable, returning the original key and the
1097 * associated value and a #gboolean which is %TRUE if the key was found. This
1098 * is useful if you need to free the memory allocated for the original key,
1099 * for example before calling g_hash_table_remove().
1101 * You can actually pass %NULL for @lookup_key to test
1102 * whether the %NULL key exists, provided the hash and equal functions
1103 * of @hash_table are %NULL-safe.
1105 * Return value: %TRUE if the key was found in the #GHashTable
1108 g_hash_table_lookup_extended (GHashTable *hash_table,
1109 gconstpointer lookup_key,
1116 g_return_val_if_fail (hash_table != NULL, FALSE);
1118 node_index = g_hash_table_lookup_node (hash_table, lookup_key, &node_hash);
1120 if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1124 *orig_key = hash_table->keys[node_index];
1127 *value = hash_table->values[node_index];
1133 * g_hash_table_insert_internal:
1134 * @hash_table: our #GHashTable
1135 * @key: the key to insert
1136 * @value: the value to insert
1137 * @keep_new_key: if %TRUE and this key already exists in the table
1138 * then call the destroy notify function on the old key. If %FALSE
1139 * then call the destroy notify function on the new key.
1141 * Implements the common logic for the g_hash_table_insert() and
1142 * g_hash_table_replace() functions.
1144 * Do a lookup of @key. If it is found, replace it with the new
1145 * @value (and perhaps the new @key). If it is not found, create
1148 * Returns: %TRUE if the key did not exist yet
1151 g_hash_table_insert_internal (GHashTable *hash_table,
1154 gboolean keep_new_key)
1159 g_return_val_if_fail (hash_table != NULL, FALSE);
1161 node_index = g_hash_table_lookup_node (hash_table, key, &key_hash);
1163 return g_hash_table_insert_node (hash_table, node_index, key_hash, key, value, keep_new_key, FALSE);
1167 * g_hash_table_insert:
1168 * @hash_table: a #GHashTable
1169 * @key: a key to insert
1170 * @value: the value to associate with the key
1172 * Inserts a new key and value into a #GHashTable.
1174 * If the key already exists in the #GHashTable its current
1175 * value is replaced with the new value. If you supplied a
1176 * @value_destroy_func when creating the #GHashTable, the old
1177 * value is freed using that function. If you supplied a
1178 * @key_destroy_func when creating the #GHashTable, the passed
1179 * key is freed using that function.
1181 * Returns: %TRUE if the key did not exist yet
1184 g_hash_table_insert (GHashTable *hash_table,
1188 return g_hash_table_insert_internal (hash_table, key, value, FALSE);
1192 * g_hash_table_replace:
1193 * @hash_table: a #GHashTable
1194 * @key: a key to insert
1195 * @value: the value to associate with the key
1197 * Inserts a new key and value into a #GHashTable similar to
1198 * g_hash_table_insert(). The difference is that if the key
1199 * already exists in the #GHashTable, it gets replaced by the
1200 * new key. If you supplied a @value_destroy_func when creating
1201 * the #GHashTable, the old value is freed using that function.
1202 * If you supplied a @key_destroy_func when creating the
1203 * #GHashTable, the old key is freed using that function.
1205 * Returns: %TRUE of the key did not exist yet
1208 g_hash_table_replace (GHashTable *hash_table,
1212 return g_hash_table_insert_internal (hash_table, key, value, TRUE);
1217 * @hash_table: a #GHashTable
1218 * @key: a key to insert
1220 * This is a convenience function for using a #GHashTable as a set. It
1221 * is equivalent to calling g_hash_table_replace() with @key as both the
1222 * key and the value.
1224 * When a hash table only ever contains keys that have themselves as the
1225 * corresponding value it is able to be stored more efficiently. See
1226 * the discussion in the section description.
1228 * Returns: %TRUE if the key did not exist yet
1233 g_hash_table_add (GHashTable *hash_table,
1236 return g_hash_table_insert_internal (hash_table, key, key, TRUE);
1240 * g_hash_table_contains:
1241 * @hash_table: a #GHashTable
1242 * @key: a key to check
1244 * Checks if @key is in @hash_table.
1249 g_hash_table_contains (GHashTable *hash_table,
1255 g_return_val_if_fail (hash_table != NULL, FALSE);
1257 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1259 return HASH_IS_REAL (hash_table->hashes[node_index]);
1263 * g_hash_table_remove_internal:
1264 * @hash_table: our #GHashTable
1265 * @key: the key to remove
1266 * @notify: %TRUE if the destroy notify handlers are to be called
1267 * Return value: %TRUE if a node was found and removed, else %FALSE
1269 * Implements the common logic for the g_hash_table_remove() and
1270 * g_hash_table_steal() functions.
1272 * Do a lookup of @key and remove it if it is found, calling the
1273 * destroy notify handlers only if @notify is %TRUE.
1276 g_hash_table_remove_internal (GHashTable *hash_table,
1283 g_return_val_if_fail (hash_table != NULL, FALSE);
1285 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1287 if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1290 g_hash_table_remove_node (hash_table, node_index, notify);
1291 g_hash_table_maybe_resize (hash_table);
1293 #ifndef G_DISABLE_ASSERT
1294 hash_table->version++;
1301 * g_hash_table_remove:
1302 * @hash_table: a #GHashTable
1303 * @key: the key to remove
1305 * Removes a key and its associated value from a #GHashTable.
1307 * If the #GHashTable was created using g_hash_table_new_full(), the
1308 * key and value are freed using the supplied destroy functions, otherwise
1309 * you have to make sure that any dynamically allocated values are freed
1312 * Returns: %TRUE if the key was found and removed from the #GHashTable
1315 g_hash_table_remove (GHashTable *hash_table,
1318 return g_hash_table_remove_internal (hash_table, key, TRUE);
1322 * g_hash_table_steal:
1323 * @hash_table: a #GHashTable
1324 * @key: the key to remove
1326 * Removes a key and its associated value from a #GHashTable without
1327 * calling the key and value destroy functions.
1329 * Returns: %TRUE if the key was found and removed from the #GHashTable
1332 g_hash_table_steal (GHashTable *hash_table,
1335 return g_hash_table_remove_internal (hash_table, key, FALSE);
1339 * g_hash_table_remove_all:
1340 * @hash_table: a #GHashTable
1342 * Removes all keys and their associated values from a #GHashTable.
1344 * If the #GHashTable was created using g_hash_table_new_full(),
1345 * the keys and values are freed using the supplied destroy functions,
1346 * otherwise you have to make sure that any dynamically allocated
1347 * values are freed yourself.
1352 g_hash_table_remove_all (GHashTable *hash_table)
1354 g_return_if_fail (hash_table != NULL);
1356 #ifndef G_DISABLE_ASSERT
1357 if (hash_table->nnodes != 0)
1358 hash_table->version++;
1361 g_hash_table_remove_all_nodes (hash_table, TRUE);
1362 g_hash_table_maybe_resize (hash_table);
1366 * g_hash_table_steal_all:
1367 * @hash_table: a #GHashTable
1369 * Removes all keys and their associated values from a #GHashTable
1370 * without calling the key and value destroy functions.
1375 g_hash_table_steal_all (GHashTable *hash_table)
1377 g_return_if_fail (hash_table != NULL);
1379 #ifndef G_DISABLE_ASSERT
1380 if (hash_table->nnodes != 0)
1381 hash_table->version++;
1384 g_hash_table_remove_all_nodes (hash_table, FALSE);
1385 g_hash_table_maybe_resize (hash_table);
1389 * g_hash_table_foreach_remove_or_steal:
1390 * @hash_table: a #GHashTable
1391 * @func: the user's callback function
1392 * @user_data: data for @func
1393 * @notify: %TRUE if the destroy notify handlers are to be called
1395 * Implements the common logic for g_hash_table_foreach_remove()
1396 * and g_hash_table_foreach_steal().
1398 * Iterates over every node in the table, calling @func with the key
1399 * and value of the node (and @user_data). If @func returns %TRUE the
1400 * node is removed from the table.
1402 * If @notify is true then the destroy notify handlers will be called
1403 * for each removed node.
1406 g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
1413 #ifndef G_DISABLE_ASSERT
1414 gint version = hash_table->version;
1417 for (i = 0; i < hash_table->size; i++)
1419 guint node_hash = hash_table->hashes[i];
1420 gpointer node_key = hash_table->keys[i];
1421 gpointer node_value = hash_table->values[i];
1423 if (HASH_IS_REAL (node_hash) &&
1424 (* func) (node_key, node_value, user_data))
1426 g_hash_table_remove_node (hash_table, i, notify);
1430 #ifndef G_DISABLE_ASSERT
1431 g_return_val_if_fail (version == hash_table->version, 0);
1435 g_hash_table_maybe_resize (hash_table);
1437 #ifndef G_DISABLE_ASSERT
1439 hash_table->version++;
1446 * g_hash_table_foreach_remove:
1447 * @hash_table: a #GHashTable
1448 * @func: the function to call for each key/value pair
1449 * @user_data: user data to pass to the function
1451 * Calls the given function for each key/value pair in the
1452 * #GHashTable. If the function returns %TRUE, then the key/value
1453 * pair is removed from the #GHashTable. If you supplied key or
1454 * value destroy functions when creating the #GHashTable, they are
1455 * used to free the memory allocated for the removed keys and values.
1457 * See #GHashTableIter for an alternative way to loop over the
1458 * key/value pairs in the hash table.
1460 * Return value: the number of key/value pairs removed
1463 g_hash_table_foreach_remove (GHashTable *hash_table,
1467 g_return_val_if_fail (hash_table != NULL, 0);
1468 g_return_val_if_fail (func != NULL, 0);
1470 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
1474 * g_hash_table_foreach_steal:
1475 * @hash_table: a #GHashTable
1476 * @func: the function to call for each key/value pair
1477 * @user_data: user data to pass to the function
1479 * Calls the given function for each key/value pair in the
1480 * #GHashTable. If the function returns %TRUE, then the key/value
1481 * pair is removed from the #GHashTable, but no key or value
1482 * destroy functions are called.
1484 * See #GHashTableIter for an alternative way to loop over the
1485 * key/value pairs in the hash table.
1487 * Return value: the number of key/value pairs removed.
1490 g_hash_table_foreach_steal (GHashTable *hash_table,
1494 g_return_val_if_fail (hash_table != NULL, 0);
1495 g_return_val_if_fail (func != NULL, 0);
1497 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
1501 * g_hash_table_foreach:
1502 * @hash_table: a #GHashTable
1503 * @func: the function to call for each key/value pair
1504 * @user_data: user data to pass to the function
1506 * Calls the given function for each of the key/value pairs in the
1507 * #GHashTable. The function is passed the key and value of each
1508 * pair, and the given @user_data parameter. The hash table may not
1509 * be modified while iterating over it (you can't add/remove
1510 * items). To remove all items matching a predicate, use
1511 * g_hash_table_foreach_remove().
1513 * See g_hash_table_find() for performance caveats for linear
1514 * order searches in contrast to g_hash_table_lookup().
1517 g_hash_table_foreach (GHashTable *hash_table,
1522 #ifndef G_DISABLE_ASSERT
1526 g_return_if_fail (hash_table != NULL);
1527 g_return_if_fail (func != NULL);
1529 #ifndef G_DISABLE_ASSERT
1530 version = hash_table->version;
1533 for (i = 0; i < hash_table->size; i++)
1535 guint node_hash = hash_table->hashes[i];
1536 gpointer node_key = hash_table->keys[i];
1537 gpointer node_value = hash_table->values[i];
1539 if (HASH_IS_REAL (node_hash))
1540 (* func) (node_key, node_value, user_data);
1542 #ifndef G_DISABLE_ASSERT
1543 g_return_if_fail (version == hash_table->version);
1549 * g_hash_table_find:
1550 * @hash_table: a #GHashTable
1551 * @predicate: function to test the key/value pairs for a certain property
1552 * @user_data: user data to pass to the function
1554 * Calls the given function for key/value pairs in the #GHashTable
1555 * until @predicate returns %TRUE. The function is passed the key
1556 * and value of each pair, and the given @user_data parameter. The
1557 * hash table may not be modified while iterating over it (you can't
1558 * add/remove items).
1560 * Note, that hash tables are really only optimized for forward
1561 * lookups, i.e. g_hash_table_lookup(). So code that frequently issues
1562 * g_hash_table_find() or g_hash_table_foreach() (e.g. in the order of
1563 * once per every entry in a hash table) should probably be reworked
1564 * to use additional or different data structures for reverse lookups
1565 * (keep in mind that an O(n) find/foreach operation issued for all n
1566 * values in a hash table ends up needing O(n*n) operations).
1568 * Return value: (allow-none): The value of the first key/value pair is returned,
1569 * for which @predicate evaluates to %TRUE. If no pair with the
1570 * requested property is found, %NULL is returned.
1575 g_hash_table_find (GHashTable *hash_table,
1580 #ifndef G_DISABLE_ASSERT
1585 g_return_val_if_fail (hash_table != NULL, NULL);
1586 g_return_val_if_fail (predicate != NULL, NULL);
1588 #ifndef G_DISABLE_ASSERT
1589 version = hash_table->version;
1594 for (i = 0; i < hash_table->size; i++)
1596 guint node_hash = hash_table->hashes[i];
1597 gpointer node_key = hash_table->keys[i];
1598 gpointer node_value = hash_table->values[i];
1600 if (HASH_IS_REAL (node_hash))
1601 match = predicate (node_key, node_value, user_data);
1603 #ifndef G_DISABLE_ASSERT
1604 g_return_val_if_fail (version == hash_table->version, NULL);
1615 * g_hash_table_size:
1616 * @hash_table: a #GHashTable
1618 * Returns the number of elements contained in the #GHashTable.
1620 * Return value: the number of key/value pairs in the #GHashTable.
1623 g_hash_table_size (GHashTable *hash_table)
1625 g_return_val_if_fail (hash_table != NULL, 0);
1627 return hash_table->nnodes;
1631 * g_hash_table_get_keys:
1632 * @hash_table: a #GHashTable
1634 * Retrieves every key inside @hash_table. The returned data is valid
1635 * until changes to the hash release those keys.
1637 * Return value: a #GList containing all the keys inside the hash
1638 * table. The content of the list is owned by the hash table and
1639 * should not be modified or freed. Use g_list_free() when done
1645 g_hash_table_get_keys (GHashTable *hash_table)
1650 g_return_val_if_fail (hash_table != NULL, NULL);
1653 for (i = 0; i < hash_table->size; i++)
1655 if (HASH_IS_REAL (hash_table->hashes[i]))
1656 retval = g_list_prepend (retval, hash_table->keys[i]);
1663 * g_hash_table_get_keys_as_array:
1664 * @hash_table: a #GHashTable
1665 * @length: (out): the length of the returned array
1667 * Retrieves every key inside @hash_table, as an array.
1669 * The returned array is %NULL-terminated but may contain %NULL as a
1670 * key. Use @length to determine the true length if it's possible that
1671 * %NULL was used as the value for a key.
1673 * Note: in the common case of a string-keyed #GHashTable, the return
1674 * value of this function can be conveniently cast to (gchar **).
1676 * You should always free the return result with g_free(). In the
1677 * above-mentioned case of a string-keyed hash table, it may be
1678 * appropriate to use g_strfreev() if you call g_hash_table_steal_all()
1679 * first to transfer ownership of the keys.
1681 * Returns: (array length=length) (transfer container): a
1682 * %NULL-terminated array containing each key from the table.
1687 g_hash_table_get_keys_as_array (GHashTable *hash_table,
1693 result = g_new (gpointer, hash_table->nnodes + 1);
1694 for (i = 0; i < hash_table->size; i++)
1696 if (HASH_IS_REAL (hash_table->hashes[i]))
1697 result[j++] = hash_table->keys[i];
1699 g_assert_cmpint (j, ==, hash_table->nnodes);
1709 * g_hash_table_get_values:
1710 * @hash_table: a #GHashTable
1712 * Retrieves every value inside @hash_table. The returned data
1713 * is valid until @hash_table is modified.
1715 * Return value: a #GList containing all the values inside the hash
1716 * table. The content of the list is owned by the hash table and
1717 * should not be modified or freed. Use g_list_free() when done
1723 g_hash_table_get_values (GHashTable *hash_table)
1728 g_return_val_if_fail (hash_table != NULL, NULL);
1731 for (i = 0; i < hash_table->size; i++)
1733 if (HASH_IS_REAL (hash_table->hashes[i]))
1734 retval = g_list_prepend (retval, hash_table->values[i]);
1746 * @v2: a key to compare with @v1
1748 * Compares two strings for byte-by-byte equality and returns %TRUE
1749 * if they are equal. It can be passed to g_hash_table_new() as the
1750 * @key_equal_func parameter, when using non-%NULL strings as keys in a
1753 * Note that this function is primarily meant as a hash table comparison
1754 * function. For a general-purpose, %NULL-safe string comparison function,
1757 * Returns: %TRUE if the two keys match
1760 g_str_equal (gconstpointer v1,
1763 const gchar *string1 = v1;
1764 const gchar *string2 = v2;
1766 return strcmp (string1, string2) == 0;
1773 * Converts a string to a hash value.
1775 * This function implements the widely used "djb" hash apparently posted
1776 * by Daniel Bernstein to comp.lang.c some time ago. The 32 bit
1777 * unsigned hash value starts at 5381 and for each byte 'c' in the
1778 * string, is updated: <literal>hash = hash * 33 + c</literal>. This
1779 * function uses the signed value of each byte.
1781 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1782 * when using non-%NULL strings as keys in a #GHashTable.
1784 * Returns: a hash value corresponding to the key
1787 g_str_hash (gconstpointer v)
1789 const signed char *p;
1792 for (p = v; *p != '\0'; p++)
1793 h = (h << 5) + h + *p;
1800 * @v: (allow-none): a #gpointer key
1802 * Converts a gpointer to a hash value.
1803 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1804 * when using opaque pointers compared by pointer value as keys in a
1807 * This hash function is also appropriate for keys that are integers stored
1808 * in pointers, such as <literal>GINT_TO_POINTER (n)</literal>.
1810 * Returns: a hash value corresponding to the key.
1813 g_direct_hash (gconstpointer v)
1815 return GPOINTER_TO_UINT (v);
1820 * @v1: (allow-none): a key
1821 * @v2: (allow-none): a key to compare with @v1
1823 * Compares two #gpointer arguments and returns %TRUE if they are equal.
1824 * It can be passed to g_hash_table_new() as the @key_equal_func
1825 * parameter, when using opaque pointers compared by pointer value as keys
1828 * This equality function is also appropriate for keys that are integers stored
1829 * in pointers, such as <literal>GINT_TO_POINTER (n)</literal>.
1831 * Returns: %TRUE if the two keys match.
1834 g_direct_equal (gconstpointer v1,
1842 * @v1: a pointer to a #gint key
1843 * @v2: a pointer to a #gint key to compare with @v1
1845 * Compares the two #gint values being pointed to and returns
1846 * %TRUE if they are equal.
1847 * It can be passed to g_hash_table_new() as the @key_equal_func
1848 * parameter, when using non-%NULL pointers to integers as keys in a
1851 * Note that this function acts on pointers to #gint, not on #gint directly:
1852 * if your hash table's keys are of the form
1853 * <literal>GINT_TO_POINTER (n)</literal>, use g_direct_equal() instead.
1855 * Returns: %TRUE if the two keys match.
1858 g_int_equal (gconstpointer v1,
1861 return *((const gint*) v1) == *((const gint*) v2);
1866 * @v: a pointer to a #gint key
1868 * Converts a pointer to a #gint to a hash value.
1869 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1870 * when using non-%NULL pointers to integer values as keys in a #GHashTable.
1872 * Note that this function acts on pointers to #gint, not on #gint directly:
1873 * if your hash table's keys are of the form
1874 * <literal>GINT_TO_POINTER (n)</literal>, use g_direct_hash() instead.
1876 * Returns: a hash value corresponding to the key.
1879 g_int_hash (gconstpointer v)
1881 return *(const gint*) v;
1886 * @v1: a pointer to a #gint64 key
1887 * @v2: a pointer to a #gint64 key to compare with @v1
1889 * Compares the two #gint64 values being pointed to and returns
1890 * %TRUE if they are equal.
1891 * It can be passed to g_hash_table_new() as the @key_equal_func
1892 * parameter, when using non-%NULL pointers to 64-bit integers as keys in a
1895 * Returns: %TRUE if the two keys match.
1900 g_int64_equal (gconstpointer v1,
1903 return *((const gint64*) v1) == *((const gint64*) v2);
1908 * @v: a pointer to a #gint64 key
1910 * Converts a pointer to a #gint64 to a hash value.
1912 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1913 * when using non-%NULL pointers to 64-bit integer values as keys in a
1916 * Returns: a hash value corresponding to the key.
1921 g_int64_hash (gconstpointer v)
1923 return (guint) *(const gint64*) v;
1928 * @v1: a pointer to a #gdouble key
1929 * @v2: a pointer to a #gdouble key to compare with @v1
1931 * Compares the two #gdouble values being pointed to and returns
1932 * %TRUE if they are equal.
1933 * It can be passed to g_hash_table_new() as the @key_equal_func
1934 * parameter, when using non-%NULL pointers to doubles as keys in a
1937 * Returns: %TRUE if the two keys match.
1942 g_double_equal (gconstpointer v1,
1945 return *((const gdouble*) v1) == *((const gdouble*) v2);
1950 * @v: a pointer to a #gdouble key
1952 * Converts a pointer to a #gdouble to a hash value.
1953 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1954 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1955 * when using non-%NULL pointers to doubles as keys in a #GHashTable.
1957 * Returns: a hash value corresponding to the key.
1962 g_double_hash (gconstpointer v)
1964 return (guint) *(const gdouble*) v;