1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
33 #include <string.h> /* memset */
37 #include "glib-private.h"
38 #include "gstrfuncs.h"
40 #include "gtestutils.h"
47 * @short_description: associations between keys and values so that
48 * given a key the value can be found quickly
50 * A #GHashTable provides associations between keys and values which is
51 * optimized so that given a key, the associated value can be found
54 * Note that neither keys nor values are copied when inserted into the
55 * #GHashTable, so they must exist for the lifetime of the #GHashTable.
56 * This means that the use of static strings is OK, but temporary
57 * strings (i.e. those created in buffers and those returned by GTK+
58 * widgets) should be copied with g_strdup() before being inserted.
60 * If keys or values are dynamically allocated, you must be careful to
61 * ensure that they are freed when they are removed from the
62 * #GHashTable, and also when they are overwritten by new insertions
63 * into the #GHashTable. It is also not advisable to mix static strings
64 * and dynamically-allocated strings in a #GHashTable, because it then
65 * becomes difficult to determine whether the string should be freed.
67 * To create a #GHashTable, use g_hash_table_new().
69 * To insert a key and value into a #GHashTable, use
70 * g_hash_table_insert().
72 * To lookup a value corresponding to a given key, use
73 * g_hash_table_lookup() and g_hash_table_lookup_extended().
75 * g_hash_table_lookup_extended() can also be used to simply
76 * check if a key is present in the hash table.
78 * To remove a key and value, use g_hash_table_remove().
80 * To call a function for each key and value pair use
81 * g_hash_table_foreach() or use a iterator to iterate over the
82 * key/value pairs in the hash table, see #GHashTableIter.
84 * To destroy a #GHashTable use g_hash_table_destroy().
86 * A common use-case for hash tables is to store information about a
87 * set of keys, without associating any particular value with each
88 * key. GHashTable optimizes one way of doing so: If you store only
89 * key-value pairs where key == value, then GHashTable does not
90 * allocate memory to store the values, which can be a considerable
91 * space saving, if your set is large. The functions
92 * g_hash_table_add() and g_hash_table_contains() are designed to be
93 * used when using #GHashTable this way.
99 * The #GHashTable struct is an opaque data structure to represent a
100 * <link linkend="glib-Hash-Tables">Hash Table</link>. It should only be
101 * accessed via the following functions.
108 * Specifies the type of the hash function which is passed to
109 * g_hash_table_new() when a #GHashTable is created.
111 * The function is passed a key and should return a #guint hash value.
112 * The functions g_direct_hash(), g_int_hash() and g_str_hash() provide
113 * hash functions which can be used when the key is a #gpointer, #gint*,
114 * and #gchar* respectively.
116 * g_direct_hash() is also the appropriate hash function for keys
117 * of the form <literal>GINT_TO_POINTER (n)</literal> (or similar macros).
119 * <!-- FIXME: Need more here. --> A good hash functions should produce
120 * hash values that are evenly distributed over a fairly large range.
121 * The modulus is taken with the hash table size (a prime number) to
122 * find the 'bucket' to place each key into. The function should also
123 * be very fast, since it is called for each key lookup.
125 * Note that the hash functions provided by GLib have these qualities,
126 * but are not particularly robust against manufactured keys that
127 * cause hash collisions. Therefore, you should consider choosing
128 * a more secure hash function when using a GHashTable with keys
129 * that originate in untrusted data (such as HTTP requests).
130 * Using g_str_hash() in that situation might make your application
131 * vulerable to <ulink url="https://lwn.net/Articles/474912/">Algorithmic Complexity Attacks</ulink>.
133 * The key to choosing a good hash is unpredictability. Even
134 * cryptographic hashes are very easy to find collisions for when the
135 * remainder is taken modulo a somewhat predictable prime number. There
136 * must be an element of randomness that an attacker is unable to guess.
138 * Returns: the hash value corresponding to the key
144 * @value: the value corresponding to the key
145 * @user_data: user data passed to g_hash_table_foreach()
147 * Specifies the type of the function passed to g_hash_table_foreach().
148 * It is called with each key/value pair, together with the @user_data
149 * parameter which is passed to g_hash_table_foreach().
155 * @value: the value associated with the key
156 * @user_data: user data passed to g_hash_table_remove()
158 * Specifies the type of the function passed to
159 * g_hash_table_foreach_remove(). It is called with each key/value
160 * pair, together with the @user_data parameter passed to
161 * g_hash_table_foreach_remove(). It should return %TRUE if the
162 * key/value pair should be removed from the #GHashTable.
164 * Returns: %TRUE if the key/value pair should be removed from the
171 * @b: a value to compare with
173 * Specifies the type of a function used to test two values for
174 * equality. The function should return %TRUE if both values are equal
175 * and %FALSE otherwise.
177 * Returns: %TRUE if @a = @b; %FALSE otherwise
183 * A GHashTableIter structure represents an iterator that can be used
184 * to iterate over the elements of a #GHashTable. GHashTableIter
185 * structures are typically allocated on the stack and then initialized
186 * with g_hash_table_iter_init().
190 * g_hash_table_freeze:
191 * @hash_table: a #GHashTable
193 * This function is deprecated and will be removed in the next major
194 * release of GLib. It does nothing.
199 * @hash_table: a #GHashTable
201 * This function is deprecated and will be removed in the next major
202 * release of GLib. It does nothing.
205 #define HASH_TABLE_MIN_SHIFT 3 /* 1 << 3 == 8 buckets */
207 #define UNUSED_HASH_VALUE 0
208 #define TOMBSTONE_HASH_VALUE 1
209 #define HASH_IS_UNUSED(h_) ((h_) == UNUSED_HASH_VALUE)
210 #define HASH_IS_TOMBSTONE(h_) ((h_) == TOMBSTONE_HASH_VALUE)
211 #define HASH_IS_REAL(h_) ((h_) >= 2)
219 gint noccupied; /* nnodes + tombstones */
226 GEqualFunc key_equal_func;
228 #ifndef G_DISABLE_ASSERT
230 * Tracks the structure of the hash table, not its contents: is only
231 * incremented when a node is added or removed (is not incremented
232 * when the key or data of a node is modified).
236 GDestroyNotify key_destroy_func;
237 GDestroyNotify value_destroy_func;
242 GHashTable *hash_table;
250 G_STATIC_ASSERT (sizeof (GHashTableIter) == sizeof (RealIter));
251 G_STATIC_ASSERT (_g_alignof (GHashTableIter) >= _g_alignof (RealIter));
253 /* Each table size has an associated prime modulo (the first prime
254 * lower than the table size) used to find the initial bucket. Probing
255 * then works modulo 2^n. The prime modulo is necessary to get a
256 * good distribution with poor hash functions.
258 static const gint prime_mod [] =
276 65521, /* For 1 << 16 */
291 2147483647 /* For 1 << 31 */
295 g_hash_table_set_shift (GHashTable *hash_table, gint shift)
300 hash_table->size = 1 << shift;
301 hash_table->mod = prime_mod [shift];
303 for (i = 0; i < shift; i++)
309 hash_table->mask = mask;
313 g_hash_table_find_closest_shift (gint n)
324 g_hash_table_set_shift_from_size (GHashTable *hash_table, gint size)
328 shift = g_hash_table_find_closest_shift (size);
329 shift = MAX (shift, HASH_TABLE_MIN_SHIFT);
331 g_hash_table_set_shift (hash_table, shift);
335 * g_hash_table_lookup_node:
336 * @hash_table: our #GHashTable
337 * @key: the key to lookup against
338 * @hash_return: key hash return location
340 * Performs a lookup in the hash table, preserving extra information
341 * usually needed for insertion.
343 * This function first computes the hash value of the key using the
344 * user's hash function.
346 * If an entry in the table matching @key is found then this function
347 * returns the index of that entry in the table, and if not, the
348 * index of an unused node (empty or tombstone) where the key can be
351 * The computed hash value is returned in the variable pointed to
352 * by @hash_return. This is to save insertions from having to compute
353 * the hash record again for the new record.
355 * Returns: index of the described node
358 g_hash_table_lookup_node (GHashTable *hash_table,
365 guint first_tombstone = 0;
366 gboolean have_tombstone = FALSE;
369 hash_value = hash_table->hash_func (key);
370 if (G_UNLIKELY (!HASH_IS_REAL (hash_value)))
373 *hash_return = hash_value;
375 node_index = hash_value % hash_table->mod;
376 node_hash = hash_table->hashes[node_index];
378 while (!HASH_IS_UNUSED (node_hash))
380 /* We first check if our full hash values
381 * are equal so we can avoid calling the full-blown
382 * key equality function in most cases.
384 if (node_hash == hash_value)
386 gpointer node_key = hash_table->keys[node_index];
388 if (hash_table->key_equal_func)
390 if (hash_table->key_equal_func (node_key, key))
393 else if (node_key == key)
398 else if (HASH_IS_TOMBSTONE (node_hash) && !have_tombstone)
400 first_tombstone = node_index;
401 have_tombstone = TRUE;
406 node_index &= hash_table->mask;
407 node_hash = hash_table->hashes[node_index];
411 return first_tombstone;
417 * g_hash_table_remove_node:
418 * @hash_table: our #GHashTable
419 * @node: pointer to node to remove
420 * @notify: %TRUE if the destroy notify handlers are to be called
422 * Removes a node from the hash table and updates the node count.
423 * The node is replaced by a tombstone. No table resize is performed.
425 * If @notify is %TRUE then the destroy notify functions are called
426 * for the key and value of the hash node.
429 g_hash_table_remove_node (GHashTable *hash_table,
436 key = hash_table->keys[i];
437 value = hash_table->values[i];
439 /* Erect tombstone */
440 hash_table->hashes[i] = TOMBSTONE_HASH_VALUE;
443 hash_table->keys[i] = NULL;
444 hash_table->values[i] = NULL;
446 hash_table->nnodes--;
448 if (notify && hash_table->key_destroy_func)
449 hash_table->key_destroy_func (key);
451 if (notify && hash_table->value_destroy_func)
452 hash_table->value_destroy_func (value);
457 * g_hash_table_remove_all_nodes:
458 * @hash_table: our #GHashTable
459 * @notify: %TRUE if the destroy notify handlers are to be called
461 * Removes all nodes from the table. Since this may be a precursor to
462 * freeing the table entirely, no resize is performed.
464 * If @notify is %TRUE then the destroy notify functions are called
465 * for the key and value of the hash node.
468 g_hash_table_remove_all_nodes (GHashTable *hash_table,
475 hash_table->nnodes = 0;
476 hash_table->noccupied = 0;
479 (hash_table->key_destroy_func == NULL &&
480 hash_table->value_destroy_func == NULL))
482 memset (hash_table->hashes, 0, hash_table->size * sizeof (guint));
483 memset (hash_table->keys, 0, hash_table->size * sizeof (gpointer));
484 memset (hash_table->values, 0, hash_table->size * sizeof (gpointer));
489 for (i = 0; i < hash_table->size; i++)
491 if (HASH_IS_REAL (hash_table->hashes[i]))
493 key = hash_table->keys[i];
494 value = hash_table->values[i];
496 hash_table->hashes[i] = UNUSED_HASH_VALUE;
497 hash_table->keys[i] = NULL;
498 hash_table->values[i] = NULL;
500 if (hash_table->key_destroy_func != NULL)
501 hash_table->key_destroy_func (key);
503 if (hash_table->value_destroy_func != NULL)
504 hash_table->value_destroy_func (value);
506 else if (HASH_IS_TOMBSTONE (hash_table->hashes[i]))
508 hash_table->hashes[i] = UNUSED_HASH_VALUE;
514 * g_hash_table_resize:
515 * @hash_table: our #GHashTable
517 * Resizes the hash table to the optimal size based on the number of
518 * nodes currently held. If you call this function then a resize will
519 * occur, even if one does not need to occur.
520 * Use g_hash_table_maybe_resize() instead.
522 * This function may "resize" the hash table to its current size, with
523 * the side effect of cleaning up tombstones and otherwise optimizing
524 * the probe sequences.
527 g_hash_table_resize (GHashTable *hash_table)
530 gpointer *new_values;
535 old_size = hash_table->size;
536 g_hash_table_set_shift_from_size (hash_table, hash_table->nnodes * 2);
538 new_keys = g_new0 (gpointer, hash_table->size);
539 if (hash_table->keys == hash_table->values)
540 new_values = new_keys;
542 new_values = g_new0 (gpointer, hash_table->size);
543 new_hashes = g_new0 (guint, hash_table->size);
545 for (i = 0; i < old_size; i++)
547 guint node_hash = hash_table->hashes[i];
551 if (!HASH_IS_REAL (node_hash))
554 hash_val = node_hash % hash_table->mod;
556 while (!HASH_IS_UNUSED (new_hashes[hash_val]))
560 hash_val &= hash_table->mask;
563 new_hashes[hash_val] = hash_table->hashes[i];
564 new_keys[hash_val] = hash_table->keys[i];
565 new_values[hash_val] = hash_table->values[i];
568 if (hash_table->keys != hash_table->values)
569 g_free (hash_table->values);
571 g_free (hash_table->keys);
572 g_free (hash_table->hashes);
574 hash_table->keys = new_keys;
575 hash_table->values = new_values;
576 hash_table->hashes = new_hashes;
578 hash_table->noccupied = hash_table->nnodes;
582 * g_hash_table_maybe_resize:
583 * @hash_table: our #GHashTable
585 * Resizes the hash table, if needed.
587 * Essentially, calls g_hash_table_resize() if the table has strayed
588 * too far from its ideal size for its number of nodes.
591 g_hash_table_maybe_resize (GHashTable *hash_table)
593 gint noccupied = hash_table->noccupied;
594 gint size = hash_table->size;
596 if ((size > hash_table->nnodes * 4 && size > 1 << HASH_TABLE_MIN_SHIFT) ||
597 (size <= noccupied + (noccupied / 16)))
598 g_hash_table_resize (hash_table);
603 * @hash_func: a function to create a hash value from a key
604 * @key_equal_func: a function to check two keys for equality
606 * Creates a new #GHashTable with a reference count of 1.
608 * Hash values returned by @hash_func are used to determine where keys
609 * are stored within the #GHashTable data structure. The g_direct_hash(),
610 * g_int_hash(), g_int64_hash(), g_double_hash() and g_str_hash()
611 * functions are provided for some common types of keys.
612 * If @hash_func is %NULL, g_direct_hash() is used.
614 * @key_equal_func is used when looking up keys in the #GHashTable.
615 * The g_direct_equal(), g_int_equal(), g_int64_equal(), g_double_equal()
616 * and g_str_equal() functions are provided for the most common types
617 * of keys. If @key_equal_func is %NULL, keys are compared directly in
618 * a similar fashion to g_direct_equal(), but without the overhead of
621 * Return value: a new #GHashTable
624 g_hash_table_new (GHashFunc hash_func,
625 GEqualFunc key_equal_func)
627 return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
632 * g_hash_table_new_full:
633 * @hash_func: a function to create a hash value from a key
634 * @key_equal_func: a function to check two keys for equality
635 * @key_destroy_func: (allow-none): a function to free the memory allocated for the key
636 * used when removing the entry from the #GHashTable, or %NULL
637 * if you don't want to supply such a function.
638 * @value_destroy_func: (allow-none): a function to free the memory allocated for the
639 * value used when removing the entry from the #GHashTable, or %NULL
640 * if you don't want to supply such a function.
642 * Creates a new #GHashTable like g_hash_table_new() with a reference
643 * count of 1 and allows to specify functions to free the memory
644 * allocated for the key and value that get called when removing the
645 * entry from the #GHashTable.
647 * Return value: a new #GHashTable
650 g_hash_table_new_full (GHashFunc hash_func,
651 GEqualFunc key_equal_func,
652 GDestroyNotify key_destroy_func,
653 GDestroyNotify value_destroy_func)
655 GHashTable *hash_table;
657 hash_table = g_slice_new (GHashTable);
658 g_hash_table_set_shift (hash_table, HASH_TABLE_MIN_SHIFT);
659 hash_table->nnodes = 0;
660 hash_table->noccupied = 0;
661 hash_table->hash_func = hash_func ? hash_func : g_direct_hash;
662 hash_table->key_equal_func = key_equal_func;
663 hash_table->ref_count = 1;
664 #ifndef G_DISABLE_ASSERT
665 hash_table->version = 0;
667 hash_table->key_destroy_func = key_destroy_func;
668 hash_table->value_destroy_func = value_destroy_func;
669 hash_table->keys = g_new0 (gpointer, hash_table->size);
670 hash_table->values = hash_table->keys;
671 hash_table->hashes = g_new0 (guint, hash_table->size);
677 * g_hash_table_iter_init:
678 * @iter: an uninitialized #GHashTableIter
679 * @hash_table: a #GHashTable
681 * Initializes a key/value pair iterator and associates it with
682 * @hash_table. Modifying the hash table after calling this function
683 * invalidates the returned iterator.
685 * GHashTableIter iter;
686 * gpointer key, value;
688 * g_hash_table_iter_init (&iter, hash_table);
689 * while (g_hash_table_iter_next (&iter, &key, &value))
691 * /* do something with key and value */
698 g_hash_table_iter_init (GHashTableIter *iter,
699 GHashTable *hash_table)
701 RealIter *ri = (RealIter *) iter;
703 g_return_if_fail (iter != NULL);
704 g_return_if_fail (hash_table != NULL);
706 ri->hash_table = hash_table;
708 #ifndef G_DISABLE_ASSERT
709 ri->version = hash_table->version;
714 * g_hash_table_iter_next:
715 * @iter: an initialized #GHashTableIter
716 * @key: (allow-none): a location to store the key, or %NULL
717 * @value: (allow-none): a location to store the value, or %NULL
719 * Advances @iter and retrieves the key and/or value that are now
720 * pointed to as a result of this advancement. If %FALSE is returned,
721 * @key and @value are not set, and the iterator becomes invalid.
723 * Return value: %FALSE if the end of the #GHashTable has been reached.
728 g_hash_table_iter_next (GHashTableIter *iter,
732 RealIter *ri = (RealIter *) iter;
735 g_return_val_if_fail (iter != NULL, FALSE);
736 #ifndef G_DISABLE_ASSERT
737 g_return_val_if_fail (ri->version == ri->hash_table->version, FALSE);
739 g_return_val_if_fail (ri->position < ri->hash_table->size, FALSE);
741 position = ri->position;
746 if (position >= ri->hash_table->size)
748 ri->position = position;
752 while (!HASH_IS_REAL (ri->hash_table->hashes[position]));
755 *key = ri->hash_table->keys[position];
757 *value = ri->hash_table->values[position];
759 ri->position = position;
764 * g_hash_table_iter_get_hash_table:
765 * @iter: an initialized #GHashTableIter
767 * Returns the #GHashTable associated with @iter.
769 * Return value: the #GHashTable associated with @iter.
774 g_hash_table_iter_get_hash_table (GHashTableIter *iter)
776 g_return_val_if_fail (iter != NULL, NULL);
778 return ((RealIter *) iter)->hash_table;
782 iter_remove_or_steal (RealIter *ri, gboolean notify)
784 g_return_if_fail (ri != NULL);
785 #ifndef G_DISABLE_ASSERT
786 g_return_if_fail (ri->version == ri->hash_table->version);
788 g_return_if_fail (ri->position >= 0);
789 g_return_if_fail (ri->position < ri->hash_table->size);
791 g_hash_table_remove_node (ri->hash_table, ri->position, notify);
793 #ifndef G_DISABLE_ASSERT
795 ri->hash_table->version++;
800 * g_hash_table_iter_remove:
801 * @iter: an initialized #GHashTableIter
803 * Removes the key/value pair currently pointed to by the iterator
804 * from its associated #GHashTable. Can only be called after
805 * g_hash_table_iter_next() returned %TRUE, and cannot be called
806 * more than once for the same key/value pair.
808 * If the #GHashTable was created using g_hash_table_new_full(),
809 * the key and value are freed using the supplied destroy functions,
810 * otherwise you have to make sure that any dynamically allocated
811 * values are freed yourself.
816 g_hash_table_iter_remove (GHashTableIter *iter)
818 iter_remove_or_steal ((RealIter *) iter, TRUE);
822 * g_hash_table_insert_node:
823 * @hash_table: our #GHashTable
824 * @node_index: pointer to node to insert/replace
825 * @key_hash: key hash
826 * @key: (allow-none): key to replace with, or %NULL
827 * @value: value to replace with
828 * @keep_new_key: whether to replace the key in the node with @key
829 * @reusing_key: whether @key was taken out of the existing node
831 * Inserts a value at @node_index in the hash table and updates it.
833 * If @key has been taken out of the existing node (ie it is not
834 * passed in via a g_hash_table_insert/replace) call, then @reusing_key
837 * Returns: %TRUE if the key did not exist yet
840 g_hash_table_insert_node (GHashTable *hash_table,
845 gboolean keep_new_key,
846 gboolean reusing_key)
848 gboolean already_exists;
850 gpointer key_to_free = NULL;
851 gpointer value_to_free = NULL;
853 old_hash = hash_table->hashes[node_index];
854 already_exists = HASH_IS_REAL (old_hash);
856 /* Proceed in three steps. First, deal with the key because it is the
857 * most complicated. Then consider if we need to split the table in
858 * two (because writing the value will result in the set invariant
859 * becoming broken). Then deal with the value.
861 * There are three cases for the key:
863 * - entry already exists in table, reusing key:
864 * free the just-passed-in new_key and use the existing value
866 * - entry already exists in table, not reusing key:
867 * free the entry in the table, use the new key
869 * - entry not already in table:
870 * use the new key, free nothing
872 * We update the hash at the same time...
876 /* Note: we must record the old value before writing the new key
877 * because we might change the value in the event that the two
880 value_to_free = hash_table->values[node_index];
884 key_to_free = hash_table->keys[node_index];
885 hash_table->keys[node_index] = new_key;
888 key_to_free = new_key;
892 hash_table->hashes[node_index] = key_hash;
893 hash_table->keys[node_index] = new_key;
896 /* Step two: check if the value that we are about to write to the
897 * table is the same as the key in the same position. If it's not,
900 if (G_UNLIKELY (hash_table->keys == hash_table->values && hash_table->keys[node_index] != new_value))
901 hash_table->values = g_memdup (hash_table->keys, sizeof (gpointer) * hash_table->size);
903 /* Step 3: Actually do the write */
904 hash_table->values[node_index] = new_value;
906 /* Now, the bookkeeping... */
909 hash_table->nnodes++;
911 if (HASH_IS_UNUSED (old_hash))
913 /* We replaced an empty node, and not a tombstone */
914 hash_table->noccupied++;
915 g_hash_table_maybe_resize (hash_table);
918 #ifndef G_DISABLE_ASSERT
919 hash_table->version++;
925 if (hash_table->key_destroy_func && !reusing_key)
926 (* hash_table->key_destroy_func) (key_to_free);
927 if (hash_table->value_destroy_func)
928 (* hash_table->value_destroy_func) (value_to_free);
931 return !already_exists;
935 * g_hash_table_iter_replace:
936 * @iter: an initialized #GHashTableIter
937 * @value: the value to replace with
939 * Replaces the value currently pointed to by the iterator
940 * from its associated #GHashTable. Can only be called after
941 * g_hash_table_iter_next() returned %TRUE.
943 * If you supplied a @value_destroy_func when creating the
944 * #GHashTable, the old value is freed using that function.
949 g_hash_table_iter_replace (GHashTableIter *iter,
956 ri = (RealIter *) iter;
958 g_return_if_fail (ri != NULL);
959 #ifndef G_DISABLE_ASSERT
960 g_return_if_fail (ri->version == ri->hash_table->version);
962 g_return_if_fail (ri->position >= 0);
963 g_return_if_fail (ri->position < ri->hash_table->size);
965 node_hash = ri->hash_table->hashes[ri->position];
966 key = ri->hash_table->keys[ri->position];
968 g_hash_table_insert_node (ri->hash_table, ri->position, node_hash, key, value, TRUE, TRUE);
970 #ifndef G_DISABLE_ASSERT
972 ri->hash_table->version++;
977 * g_hash_table_iter_steal:
978 * @iter: an initialized #GHashTableIter
980 * Removes the key/value pair currently pointed to by the
981 * iterator from its associated #GHashTable, without calling
982 * the key and value destroy functions. Can only be called
983 * after g_hash_table_iter_next() returned %TRUE, and cannot
984 * be called more than once for the same key/value pair.
989 g_hash_table_iter_steal (GHashTableIter *iter)
991 iter_remove_or_steal ((RealIter *) iter, FALSE);
997 * @hash_table: a valid #GHashTable
999 * Atomically increments the reference count of @hash_table by one.
1000 * This function is MT-safe and may be called from any thread.
1002 * Return value: the passed in #GHashTable
1007 g_hash_table_ref (GHashTable *hash_table)
1009 g_return_val_if_fail (hash_table != NULL, NULL);
1011 g_atomic_int_inc (&hash_table->ref_count);
1017 * g_hash_table_unref:
1018 * @hash_table: a valid #GHashTable
1020 * Atomically decrements the reference count of @hash_table by one.
1021 * If the reference count drops to 0, all keys and values will be
1022 * destroyed, and all memory allocated by the hash table is released.
1023 * This function is MT-safe and may be called from any thread.
1028 g_hash_table_unref (GHashTable *hash_table)
1030 g_return_if_fail (hash_table != NULL);
1032 if (g_atomic_int_dec_and_test (&hash_table->ref_count))
1034 g_hash_table_remove_all_nodes (hash_table, TRUE);
1035 if (hash_table->keys != hash_table->values)
1036 g_free (hash_table->values);
1037 g_free (hash_table->keys);
1038 g_free (hash_table->hashes);
1039 g_slice_free (GHashTable, hash_table);
1044 * g_hash_table_destroy:
1045 * @hash_table: a #GHashTable
1047 * Destroys all keys and values in the #GHashTable and decrements its
1048 * reference count by 1. If keys and/or values are dynamically allocated,
1049 * you should either free them first or create the #GHashTable with destroy
1050 * notifiers using g_hash_table_new_full(). In the latter case the destroy
1051 * functions you supplied will be called on all keys and values during the
1052 * destruction phase.
1055 g_hash_table_destroy (GHashTable *hash_table)
1057 g_return_if_fail (hash_table != NULL);
1059 g_hash_table_remove_all (hash_table);
1060 g_hash_table_unref (hash_table);
1064 * g_hash_table_lookup:
1065 * @hash_table: a #GHashTable
1066 * @key: the key to look up
1068 * Looks up a key in a #GHashTable. Note that this function cannot
1069 * distinguish between a key that is not present and one which is present
1070 * and has the value %NULL. If you need this distinction, use
1071 * g_hash_table_lookup_extended().
1073 * Return value: (allow-none): the associated value, or %NULL if the key is not found
1076 g_hash_table_lookup (GHashTable *hash_table,
1082 g_return_val_if_fail (hash_table != NULL, NULL);
1084 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1086 return HASH_IS_REAL (hash_table->hashes[node_index])
1087 ? hash_table->values[node_index]
1092 * g_hash_table_lookup_extended:
1093 * @hash_table: a #GHashTable
1094 * @lookup_key: the key to look up
1095 * @orig_key: (allow-none): return location for the original key, or %NULL
1096 * @value: (allow-none): return location for the value associated with the key, or %NULL
1098 * Looks up a key in the #GHashTable, returning the original key and the
1099 * associated value and a #gboolean which is %TRUE if the key was found. This
1100 * is useful if you need to free the memory allocated for the original key,
1101 * for example before calling g_hash_table_remove().
1103 * You can actually pass %NULL for @lookup_key to test
1104 * whether the %NULL key exists, provided the hash and equal functions
1105 * of @hash_table are %NULL-safe.
1107 * Return value: %TRUE if the key was found in the #GHashTable
1110 g_hash_table_lookup_extended (GHashTable *hash_table,
1111 gconstpointer lookup_key,
1118 g_return_val_if_fail (hash_table != NULL, FALSE);
1120 node_index = g_hash_table_lookup_node (hash_table, lookup_key, &node_hash);
1122 if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1126 *orig_key = hash_table->keys[node_index];
1129 *value = hash_table->values[node_index];
1135 * g_hash_table_insert_internal:
1136 * @hash_table: our #GHashTable
1137 * @key: the key to insert
1138 * @value: the value to insert
1139 * @keep_new_key: if %TRUE and this key already exists in the table
1140 * then call the destroy notify function on the old key. If %FALSE
1141 * then call the destroy notify function on the new key.
1143 * Implements the common logic for the g_hash_table_insert() and
1144 * g_hash_table_replace() functions.
1146 * Do a lookup of @key. If it is found, replace it with the new
1147 * @value (and perhaps the new @key). If it is not found, create
1150 * Returns: %TRUE if the key did not exist yet
1153 g_hash_table_insert_internal (GHashTable *hash_table,
1156 gboolean keep_new_key)
1161 g_return_val_if_fail (hash_table != NULL, FALSE);
1163 node_index = g_hash_table_lookup_node (hash_table, key, &key_hash);
1165 return g_hash_table_insert_node (hash_table, node_index, key_hash, key, value, keep_new_key, FALSE);
1169 * g_hash_table_insert:
1170 * @hash_table: a #GHashTable
1171 * @key: a key to insert
1172 * @value: the value to associate with the key
1174 * Inserts a new key and value into a #GHashTable.
1176 * If the key already exists in the #GHashTable its current
1177 * value is replaced with the new value. If you supplied a
1178 * @value_destroy_func when creating the #GHashTable, the old
1179 * value is freed using that function. If you supplied a
1180 * @key_destroy_func when creating the #GHashTable, the passed
1181 * key is freed using that function.
1183 * Returns: %TRUE if the key did not exist yet
1186 g_hash_table_insert (GHashTable *hash_table,
1190 return g_hash_table_insert_internal (hash_table, key, value, FALSE);
1194 * g_hash_table_replace:
1195 * @hash_table: a #GHashTable
1196 * @key: a key to insert
1197 * @value: the value to associate with the key
1199 * Inserts a new key and value into a #GHashTable similar to
1200 * g_hash_table_insert(). The difference is that if the key
1201 * already exists in the #GHashTable, it gets replaced by the
1202 * new key. If you supplied a @value_destroy_func when creating
1203 * the #GHashTable, the old value is freed using that function.
1204 * If you supplied a @key_destroy_func when creating the
1205 * #GHashTable, the old key is freed using that function.
1207 * Returns: %TRUE of the key did not exist yet
1210 g_hash_table_replace (GHashTable *hash_table,
1214 return g_hash_table_insert_internal (hash_table, key, value, TRUE);
1219 * @hash_table: a #GHashTable
1220 * @key: a key to insert
1222 * This is a convenience function for using a #GHashTable as a set. It
1223 * is equivalent to calling g_hash_table_replace() with @key as both the
1224 * key and the value.
1226 * When a hash table only ever contains keys that have themselves as the
1227 * corresponding value it is able to be stored more efficiently. See
1228 * the discussion in the section description.
1230 * Returns: %TRUE if the key did not exist yet
1235 g_hash_table_add (GHashTable *hash_table,
1238 return g_hash_table_insert_internal (hash_table, key, key, TRUE);
1242 * g_hash_table_contains:
1243 * @hash_table: a #GHashTable
1244 * @key: a key to check
1246 * Checks if @key is in @hash_table.
1251 g_hash_table_contains (GHashTable *hash_table,
1257 g_return_val_if_fail (hash_table != NULL, FALSE);
1259 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1261 return HASH_IS_REAL (hash_table->hashes[node_index]);
1265 * g_hash_table_remove_internal:
1266 * @hash_table: our #GHashTable
1267 * @key: the key to remove
1268 * @notify: %TRUE if the destroy notify handlers are to be called
1269 * Return value: %TRUE if a node was found and removed, else %FALSE
1271 * Implements the common logic for the g_hash_table_remove() and
1272 * g_hash_table_steal() functions.
1274 * Do a lookup of @key and remove it if it is found, calling the
1275 * destroy notify handlers only if @notify is %TRUE.
1278 g_hash_table_remove_internal (GHashTable *hash_table,
1285 g_return_val_if_fail (hash_table != NULL, FALSE);
1287 node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1289 if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1292 g_hash_table_remove_node (hash_table, node_index, notify);
1293 g_hash_table_maybe_resize (hash_table);
1295 #ifndef G_DISABLE_ASSERT
1296 hash_table->version++;
1303 * g_hash_table_remove:
1304 * @hash_table: a #GHashTable
1305 * @key: the key to remove
1307 * Removes a key and its associated value from a #GHashTable.
1309 * If the #GHashTable was created using g_hash_table_new_full(), the
1310 * key and value are freed using the supplied destroy functions, otherwise
1311 * you have to make sure that any dynamically allocated values are freed
1314 * Returns: %TRUE if the key was found and removed from the #GHashTable
1317 g_hash_table_remove (GHashTable *hash_table,
1320 return g_hash_table_remove_internal (hash_table, key, TRUE);
1324 * g_hash_table_steal:
1325 * @hash_table: a #GHashTable
1326 * @key: the key to remove
1328 * Removes a key and its associated value from a #GHashTable without
1329 * calling the key and value destroy functions.
1331 * Returns: %TRUE if the key was found and removed from the #GHashTable
1334 g_hash_table_steal (GHashTable *hash_table,
1337 return g_hash_table_remove_internal (hash_table, key, FALSE);
1341 * g_hash_table_remove_all:
1342 * @hash_table: a #GHashTable
1344 * Removes all keys and their associated values from a #GHashTable.
1346 * If the #GHashTable was created using g_hash_table_new_full(),
1347 * the keys and values are freed using the supplied destroy functions,
1348 * otherwise you have to make sure that any dynamically allocated
1349 * values are freed yourself.
1354 g_hash_table_remove_all (GHashTable *hash_table)
1356 g_return_if_fail (hash_table != NULL);
1358 #ifndef G_DISABLE_ASSERT
1359 if (hash_table->nnodes != 0)
1360 hash_table->version++;
1363 g_hash_table_remove_all_nodes (hash_table, TRUE);
1364 g_hash_table_maybe_resize (hash_table);
1368 * g_hash_table_steal_all:
1369 * @hash_table: a #GHashTable
1371 * Removes all keys and their associated values from a #GHashTable
1372 * without calling the key and value destroy functions.
1377 g_hash_table_steal_all (GHashTable *hash_table)
1379 g_return_if_fail (hash_table != NULL);
1381 #ifndef G_DISABLE_ASSERT
1382 if (hash_table->nnodes != 0)
1383 hash_table->version++;
1386 g_hash_table_remove_all_nodes (hash_table, FALSE);
1387 g_hash_table_maybe_resize (hash_table);
1391 * g_hash_table_foreach_remove_or_steal:
1392 * @hash_table: a #GHashTable
1393 * @func: the user's callback function
1394 * @user_data: data for @func
1395 * @notify: %TRUE if the destroy notify handlers are to be called
1397 * Implements the common logic for g_hash_table_foreach_remove()
1398 * and g_hash_table_foreach_steal().
1400 * Iterates over every node in the table, calling @func with the key
1401 * and value of the node (and @user_data). If @func returns %TRUE the
1402 * node is removed from the table.
1404 * If @notify is true then the destroy notify handlers will be called
1405 * for each removed node.
1408 g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
1415 #ifndef G_DISABLE_ASSERT
1416 gint version = hash_table->version;
1419 for (i = 0; i < hash_table->size; i++)
1421 guint node_hash = hash_table->hashes[i];
1422 gpointer node_key = hash_table->keys[i];
1423 gpointer node_value = hash_table->values[i];
1425 if (HASH_IS_REAL (node_hash) &&
1426 (* func) (node_key, node_value, user_data))
1428 g_hash_table_remove_node (hash_table, i, notify);
1432 #ifndef G_DISABLE_ASSERT
1433 g_return_val_if_fail (version == hash_table->version, 0);
1437 g_hash_table_maybe_resize (hash_table);
1439 #ifndef G_DISABLE_ASSERT
1441 hash_table->version++;
1448 * g_hash_table_foreach_remove:
1449 * @hash_table: a #GHashTable
1450 * @func: the function to call for each key/value pair
1451 * @user_data: user data to pass to the function
1453 * Calls the given function for each key/value pair in the
1454 * #GHashTable. If the function returns %TRUE, then the key/value
1455 * pair is removed from the #GHashTable. If you supplied key or
1456 * value destroy functions when creating the #GHashTable, they are
1457 * used to free the memory allocated for the removed keys and values.
1459 * See #GHashTableIter for an alternative way to loop over the
1460 * key/value pairs in the hash table.
1462 * Return value: the number of key/value pairs removed
1465 g_hash_table_foreach_remove (GHashTable *hash_table,
1469 g_return_val_if_fail (hash_table != NULL, 0);
1470 g_return_val_if_fail (func != NULL, 0);
1472 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
1476 * g_hash_table_foreach_steal:
1477 * @hash_table: a #GHashTable
1478 * @func: the function to call for each key/value pair
1479 * @user_data: user data to pass to the function
1481 * Calls the given function for each key/value pair in the
1482 * #GHashTable. If the function returns %TRUE, then the key/value
1483 * pair is removed from the #GHashTable, but no key or value
1484 * destroy functions are called.
1486 * See #GHashTableIter for an alternative way to loop over the
1487 * key/value pairs in the hash table.
1489 * Return value: the number of key/value pairs removed.
1492 g_hash_table_foreach_steal (GHashTable *hash_table,
1496 g_return_val_if_fail (hash_table != NULL, 0);
1497 g_return_val_if_fail (func != NULL, 0);
1499 return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
1503 * g_hash_table_foreach:
1504 * @hash_table: a #GHashTable
1505 * @func: the function to call for each key/value pair
1506 * @user_data: user data to pass to the function
1508 * Calls the given function for each of the key/value pairs in the
1509 * #GHashTable. The function is passed the key and value of each
1510 * pair, and the given @user_data parameter. The hash table may not
1511 * be modified while iterating over it (you can't add/remove
1512 * items). To remove all items matching a predicate, use
1513 * g_hash_table_foreach_remove().
1515 * See g_hash_table_find() for performance caveats for linear
1516 * order searches in contrast to g_hash_table_lookup().
1519 g_hash_table_foreach (GHashTable *hash_table,
1524 #ifndef G_DISABLE_ASSERT
1528 g_return_if_fail (hash_table != NULL);
1529 g_return_if_fail (func != NULL);
1531 #ifndef G_DISABLE_ASSERT
1532 version = hash_table->version;
1535 for (i = 0; i < hash_table->size; i++)
1537 guint node_hash = hash_table->hashes[i];
1538 gpointer node_key = hash_table->keys[i];
1539 gpointer node_value = hash_table->values[i];
1541 if (HASH_IS_REAL (node_hash))
1542 (* func) (node_key, node_value, user_data);
1544 #ifndef G_DISABLE_ASSERT
1545 g_return_if_fail (version == hash_table->version);
1551 * g_hash_table_find:
1552 * @hash_table: a #GHashTable
1553 * @predicate: function to test the key/value pairs for a certain property
1554 * @user_data: user data to pass to the function
1556 * Calls the given function for key/value pairs in the #GHashTable
1557 * until @predicate returns %TRUE. The function is passed the key
1558 * and value of each pair, and the given @user_data parameter. The
1559 * hash table may not be modified while iterating over it (you can't
1560 * add/remove items).
1562 * Note, that hash tables are really only optimized for forward
1563 * lookups, i.e. g_hash_table_lookup(). So code that frequently issues
1564 * g_hash_table_find() or g_hash_table_foreach() (e.g. in the order of
1565 * once per every entry in a hash table) should probably be reworked
1566 * to use additional or different data structures for reverse lookups
1567 * (keep in mind that an O(n) find/foreach operation issued for all n
1568 * values in a hash table ends up needing O(n*n) operations).
1570 * Return value: (allow-none): The value of the first key/value pair is returned,
1571 * for which @predicate evaluates to %TRUE. If no pair with the
1572 * requested property is found, %NULL is returned.
1577 g_hash_table_find (GHashTable *hash_table,
1582 #ifndef G_DISABLE_ASSERT
1587 g_return_val_if_fail (hash_table != NULL, NULL);
1588 g_return_val_if_fail (predicate != NULL, NULL);
1590 #ifndef G_DISABLE_ASSERT
1591 version = hash_table->version;
1596 for (i = 0; i < hash_table->size; i++)
1598 guint node_hash = hash_table->hashes[i];
1599 gpointer node_key = hash_table->keys[i];
1600 gpointer node_value = hash_table->values[i];
1602 if (HASH_IS_REAL (node_hash))
1603 match = predicate (node_key, node_value, user_data);
1605 #ifndef G_DISABLE_ASSERT
1606 g_return_val_if_fail (version == hash_table->version, NULL);
1617 * g_hash_table_size:
1618 * @hash_table: a #GHashTable
1620 * Returns the number of elements contained in the #GHashTable.
1622 * Return value: the number of key/value pairs in the #GHashTable.
1625 g_hash_table_size (GHashTable *hash_table)
1627 g_return_val_if_fail (hash_table != NULL, 0);
1629 return hash_table->nnodes;
1633 * g_hash_table_get_keys:
1634 * @hash_table: a #GHashTable
1636 * Retrieves every key inside @hash_table. The returned data is valid
1637 * until changes to the hash release those keys.
1639 * Return value: a #GList containing all the keys inside the hash
1640 * table. The content of the list is owned by the hash table and
1641 * should not be modified or freed. Use g_list_free() when done
1647 g_hash_table_get_keys (GHashTable *hash_table)
1652 g_return_val_if_fail (hash_table != NULL, NULL);
1655 for (i = 0; i < hash_table->size; i++)
1657 if (HASH_IS_REAL (hash_table->hashes[i]))
1658 retval = g_list_prepend (retval, hash_table->keys[i]);
1665 * g_hash_table_get_keys_as_array:
1666 * @hash_table: a #GHashTable
1667 * @length: (out): the length of the returned array
1669 * Retrieves every key inside @hash_table, as an array.
1671 * The returned array is %NULL-terminated but may contain %NULL as a
1672 * key. Use @length to determine the true length if it's possible that
1673 * %NULL was used as the value for a key.
1675 * Note: in the common case of a string-keyed #GHashTable, the return
1676 * value of this function can be conveniently cast to (gchar **).
1678 * You should always free the return result with g_free(). In the
1679 * above-mentioned case of a string-keyed hash table, it may be
1680 * appropriate to use g_strfreev() if you call g_hash_table_steal_all()
1681 * first to transfer ownership of the keys.
1683 * Returns: (array length=length) (transfer container): a
1684 * %NULL-terminated array containing each key from the table.
1689 g_hash_table_get_keys_as_array (GHashTable *hash_table,
1695 result = g_new (gpointer, hash_table->nnodes + 1);
1696 for (i = 0; i < hash_table->size; i++)
1698 if (HASH_IS_REAL (hash_table->hashes[i]))
1699 result[j++] = hash_table->keys[i];
1701 g_assert_cmpint (j, ==, hash_table->nnodes);
1711 * g_hash_table_get_values:
1712 * @hash_table: a #GHashTable
1714 * Retrieves every value inside @hash_table. The returned data
1715 * is valid until @hash_table is modified.
1717 * Return value: a #GList containing all the values inside the hash
1718 * table. The content of the list is owned by the hash table and
1719 * should not be modified or freed. Use g_list_free() when done
1725 g_hash_table_get_values (GHashTable *hash_table)
1730 g_return_val_if_fail (hash_table != NULL, NULL);
1733 for (i = 0; i < hash_table->size; i++)
1735 if (HASH_IS_REAL (hash_table->hashes[i]))
1736 retval = g_list_prepend (retval, hash_table->values[i]);
1748 * @v2: a key to compare with @v1
1750 * Compares two strings for byte-by-byte equality and returns %TRUE
1751 * if they are equal. It can be passed to g_hash_table_new() as the
1752 * @key_equal_func parameter, when using non-%NULL strings as keys in a
1755 * Note that this function is primarily meant as a hash table comparison
1756 * function. For a general-purpose, %NULL-safe string comparison function,
1759 * Returns: %TRUE if the two keys match
1762 g_str_equal (gconstpointer v1,
1765 const gchar *string1 = v1;
1766 const gchar *string2 = v2;
1768 return strcmp (string1, string2) == 0;
1775 * Converts a string to a hash value.
1777 * This function implements the widely used "djb" hash apparently posted
1778 * by Daniel Bernstein to comp.lang.c some time ago. The 32 bit
1779 * unsigned hash value starts at 5381 and for each byte 'c' in the
1780 * string, is updated: <literal>hash = hash * 33 + c</literal>. This
1781 * function uses the signed value of each byte.
1783 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1784 * when using non-%NULL strings as keys in a #GHashTable.
1786 * Returns: a hash value corresponding to the key
1789 g_str_hash (gconstpointer v)
1791 const signed char *p;
1794 for (p = v; *p != '\0'; p++)
1795 h = (h << 5) + h + *p;
1802 * @v: (allow-none): a #gpointer key
1804 * Converts a gpointer to a hash value.
1805 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1806 * when using opaque pointers compared by pointer value as keys in a
1809 * This hash function is also appropriate for keys that are integers stored
1810 * in pointers, such as <literal>GINT_TO_POINTER (n)</literal>.
1812 * Returns: a hash value corresponding to the key.
1815 g_direct_hash (gconstpointer v)
1817 return GPOINTER_TO_UINT (v);
1822 * @v1: (allow-none): a key
1823 * @v2: (allow-none): a key to compare with @v1
1825 * Compares two #gpointer arguments and returns %TRUE if they are equal.
1826 * It can be passed to g_hash_table_new() as the @key_equal_func
1827 * parameter, when using opaque pointers compared by pointer value as keys
1830 * This equality function is also appropriate for keys that are integers stored
1831 * in pointers, such as <literal>GINT_TO_POINTER (n)</literal>.
1833 * Returns: %TRUE if the two keys match.
1836 g_direct_equal (gconstpointer v1,
1844 * @v1: a pointer to a #gint key
1845 * @v2: a pointer to a #gint key to compare with @v1
1847 * Compares the two #gint values being pointed to and returns
1848 * %TRUE if they are equal.
1849 * It can be passed to g_hash_table_new() as the @key_equal_func
1850 * parameter, when using non-%NULL pointers to integers as keys in a
1853 * Note that this function acts on pointers to #gint, not on #gint directly:
1854 * if your hash table's keys are of the form
1855 * <literal>GINT_TO_POINTER (n)</literal>, use g_direct_equal() instead.
1857 * Returns: %TRUE if the two keys match.
1860 g_int_equal (gconstpointer v1,
1863 return *((const gint*) v1) == *((const gint*) v2);
1868 * @v: a pointer to a #gint key
1870 * Converts a pointer to a #gint to a hash value.
1871 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1872 * when using non-%NULL pointers to integer values as keys in a #GHashTable.
1874 * Note that this function acts on pointers to #gint, not on #gint directly:
1875 * if your hash table's keys are of the form
1876 * <literal>GINT_TO_POINTER (n)</literal>, use g_direct_hash() instead.
1878 * Returns: a hash value corresponding to the key.
1881 g_int_hash (gconstpointer v)
1883 return *(const gint*) v;
1888 * @v1: a pointer to a #gint64 key
1889 * @v2: a pointer to a #gint64 key to compare with @v1
1891 * Compares the two #gint64 values being pointed to and returns
1892 * %TRUE if they are equal.
1893 * It can be passed to g_hash_table_new() as the @key_equal_func
1894 * parameter, when using non-%NULL pointers to 64-bit integers as keys in a
1897 * Returns: %TRUE if the two keys match.
1902 g_int64_equal (gconstpointer v1,
1905 return *((const gint64*) v1) == *((const gint64*) v2);
1910 * @v: a pointer to a #gint64 key
1912 * Converts a pointer to a #gint64 to a hash value.
1914 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1915 * when using non-%NULL pointers to 64-bit integer values as keys in a
1918 * Returns: a hash value corresponding to the key.
1923 g_int64_hash (gconstpointer v)
1925 return (guint) *(const gint64*) v;
1930 * @v1: a pointer to a #gdouble key
1931 * @v2: a pointer to a #gdouble key to compare with @v1
1933 * Compares the two #gdouble values being pointed to and returns
1934 * %TRUE if they are equal.
1935 * It can be passed to g_hash_table_new() as the @key_equal_func
1936 * parameter, when using non-%NULL pointers to doubles as keys in a
1939 * Returns: %TRUE if the two keys match.
1944 g_double_equal (gconstpointer v1,
1947 return *((const gdouble*) v1) == *((const gdouble*) v2);
1952 * @v: a pointer to a #gdouble key
1954 * Converts a pointer to a #gdouble to a hash value.
1955 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1956 * It can be passed to g_hash_table_new() as the @hash_func parameter,
1957 * when using non-%NULL pointers to doubles as keys in a #GHashTable.
1959 * Returns: a hash value corresponding to the key.
1964 g_double_hash (gconstpointer v)
1966 return (guint) *(const gdouble*) v;