Move all hash functions to ghash.c
[platform/upstream/glib.git] / glib / ghash.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/.
25  */
26
27 /*
28  * MT safe
29  */
30
31 #include "config.h"
32
33 #include <string.h>  /* memset */
34
35 #include "ghash.h"
36
37 #include "gstrfuncs.h"
38 #include "gatomic.h"
39 #include "gtestutils.h"
40 #include "gslice.h"
41
42
43 /**
44  * SECTION:hash_tables
45  * @title: Hash Tables
46  * @short_description: associations between keys and values so that
47  *     given a key the value can be found quickly
48  *
49  * A #GHashTable provides associations between keys and values which is
50  * optimized so that given a key, the associated value can be found
51  * very quickly.
52  *
53  * Note that neither keys nor values are copied when inserted into the
54  * #GHashTable, so they must exist for the lifetime of the #GHashTable.
55  * This means that the use of static strings is OK, but temporary
56  * strings (i.e. those created in buffers and those returned by GTK+
57  * widgets) should be copied with g_strdup() before being inserted.
58  *
59  * If keys or values are dynamically allocated, you must be careful to
60  * ensure that they are freed when they are removed from the
61  * #GHashTable, and also when they are overwritten by new insertions
62  * into the #GHashTable. It is also not advisable to mix static strings
63  * and dynamically-allocated strings in a #GHashTable, because it then
64  * becomes difficult to determine whether the string should be freed.
65  *
66  * To create a #GHashTable, use g_hash_table_new().
67  *
68  * To insert a key and value into a #GHashTable, use
69  * g_hash_table_insert().
70  *
71  * To lookup a value corresponding to a given key, use
72  * g_hash_table_lookup() and g_hash_table_lookup_extended().
73  *
74  * g_hash_table_lookup_extended() can also be used to simply
75  * check if a key is present in the hash table.
76  *
77  * To remove a key and value, use g_hash_table_remove().
78  *
79  * To call a function for each key and value pair use
80  * g_hash_table_foreach() or use a iterator to iterate over the
81  * key/value pairs in the hash table, see #GHashTableIter.
82  *
83  * To destroy a #GHashTable use g_hash_table_destroy().
84  *
85  * <example>
86  * <title>Using a GHashTable as a set</title>
87  * <para>
88  * A common use-case for hash tables is to store information about
89  * a set of keys, without associating any particular value with each
90  * key. GHashTable optimizes one way of doing so: If you store only
91  * key-value pairs where key == value, then GHashTable does not
92  * allocate memory to store the values, which can be a considerable
93  * space saving, if your set is large.
94  * </para>
95  * <programlisting>
96  * GHashTable *
97  * set_new (GHashFunc      hash_func,
98  *          GEqualFunc     equal_func,
99  *          GDestroyNotify destroy)
100  * {
101  *   return g_hash_table_new_full (hash_func, equal_func, destroy, NULL);
102  * }
103  *
104  * void
105  * set_insert (GHashTable *set,
106  *             gpointer    element)
107  * {
108  *   g_hash_table_insert (set, element, element);
109  * }
110  *
111  * gboolean
112  * set_contains (GHashTable *set,
113  *               gpointer    element)
114  * {
115  *   return g_hash_table_lookup_extended (set, element, NULL, NULL);
116  * }
117  *
118  * gboolean
119  * set_remove (GHashTable *set,
120  *             gpointer    element)
121  * {
122  *   return g_hash_table_remove (set, element);
123  * }
124  * </programlisting>
125  * </example>
126  */
127
128 /**
129  * GHashTable:
130  *
131  * The #GHashTable struct is an opaque data structure to represent a
132  * <link linkend="glib-Hash-Tables">Hash Table</link>. It should only be
133  * accessed via the following functions.
134  */
135
136 /**
137  * GHashFunc:
138  * @key: a key
139  *
140  * Specifies the type of the hash function which is passed to
141  * g_hash_table_new() when a #GHashTable is created.
142  *
143  * The function is passed a key and should return a #guint hash value.
144  * The functions g_direct_hash(), g_int_hash() and g_str_hash() provide
145  * hash functions which can be used when the key is a #gpointer, #gint,
146  * and #gchar* respectively.
147  *
148  * <!-- FIXME: Need more here. --> The hash values should be evenly
149  * distributed over a fairly large range? The modulus is taken with the
150  * hash table size (a prime number) to find the 'bucket' to place each
151  * key into. The function should also be very fast, since it is called
152  * for each key lookup.
153  *
154  * Returns: the hash value corresponding to the key
155  */
156
157 /**
158  * GHFunc:
159  * @key: a key
160  * @value: the value corresponding to the key
161  * @user_data: user data passed to g_hash_table_foreach()
162  *
163  * Specifies the type of the function passed to g_hash_table_foreach().
164  * It is called with each key/value pair, together with the @user_data
165  * parameter which is passed to g_hash_table_foreach().
166  */
167
168 /**
169  * GHRFunc:
170  * @key: a key
171  * @value: the value associated with the key
172  * @user_data: user data passed to g_hash_table_remove()
173  *
174  * Specifies the type of the function passed to
175  * g_hash_table_foreach_remove(). It is called with each key/value
176  * pair, together with the @user_data parameter passed to
177  * g_hash_table_foreach_remove(). It should return %TRUE if the
178  * key/value pair should be removed from the #GHashTable.
179  *
180  * Returns: %TRUE if the key/value pair should be removed from the
181  *     #GHashTable
182  */
183
184 /**
185  * GEqualFunc:
186  * @a: a value
187  * @b: a value to compare with
188  *
189  * Specifies the type of a function used to test two values for
190  * equality. The function should return %TRUE if both values are equal
191  * and %FALSE otherwise.
192  *
193  * Returns: %TRUE if @a = @b; %FALSE otherwise
194  */
195
196 /**
197  * GHashTableIter:
198  *
199  * A GHashTableIter structure represents an iterator that can be used
200  * to iterate over the elements of a #GHashTable. GHashTableIter
201  * structures are typically allocated on the stack and then initialized
202  * with g_hash_table_iter_init().
203  */
204
205 /**
206  * g_hash_table_freeze:
207  * @hash_table: a #GHashTable
208  *
209  * This function is deprecated and will be removed in the next major
210  * release of GLib. It does nothing.
211  */
212
213 /**
214  * g_hash_table_thaw:
215  * @hash_table: a #GHashTable
216  *
217  * This function is deprecated and will be removed in the next major
218  * release of GLib. It does nothing.
219  */
220
221 #define HASH_TABLE_MIN_SHIFT 3  /* 1 << 3 == 8 buckets */
222
223 #define UNUSED_HASH_VALUE 0
224 #define TOMBSTONE_HASH_VALUE 1
225 #define HASH_IS_UNUSED(h_) ((h_) == UNUSED_HASH_VALUE)
226 #define HASH_IS_TOMBSTONE(h_) ((h_) == TOMBSTONE_HASH_VALUE)
227 #define HASH_IS_REAL(h_) ((h_) >= 2)
228
229 struct _GHashTable
230 {
231   gint             size;
232   gint             mod;
233   guint            mask;
234   gint             nnodes;
235   gint             noccupied;  /* nnodes + tombstones */
236
237   gpointer        *keys;
238   guint           *hashes;
239   gpointer        *values;
240
241   GHashFunc        hash_func;
242   GEqualFunc       key_equal_func;
243   gint             ref_count;
244 #ifndef G_DISABLE_ASSERT
245   /*
246    * Tracks the structure of the hash table, not its contents: is only
247    * incremented when a node is added or removed (is not incremented
248    * when the key or data of a node is modified).
249    */
250   int              version;
251 #endif
252   GDestroyNotify   key_destroy_func;
253   GDestroyNotify   value_destroy_func;
254 };
255
256 typedef struct
257 {
258   GHashTable  *hash_table;
259   gpointer     dummy1;
260   gpointer     dummy2;
261   int          position;
262   gboolean     dummy3;
263   int          version;
264 } RealIter;
265
266 /* Each table size has an associated prime modulo (the first prime
267  * lower than the table size) used to find the initial bucket. Probing
268  * then works modulo 2^n. The prime modulo is necessary to get a
269  * good distribution with poor hash functions.
270  */
271 static const gint prime_mod [] =
272 {
273   1,          /* For 1 << 0 */
274   2,
275   3,
276   7,
277   13,
278   31,
279   61,
280   127,
281   251,
282   509,
283   1021,
284   2039,
285   4093,
286   8191,
287   16381,
288   32749,
289   65521,      /* For 1 << 16 */
290   131071,
291   262139,
292   524287,
293   1048573,
294   2097143,
295   4194301,
296   8388593,
297   16777213,
298   33554393,
299   67108859,
300   134217689,
301   268435399,
302   536870909,
303   1073741789,
304   2147483647  /* For 1 << 31 */
305 };
306
307 static void
308 g_hash_table_set_shift (GHashTable *hash_table, gint shift)
309 {
310   gint i;
311   guint mask = 0;
312
313   hash_table->size = 1 << shift;
314   hash_table->mod  = prime_mod [shift];
315
316   for (i = 0; i < shift; i++)
317     {
318       mask <<= 1;
319       mask |= 1;
320     }
321
322   hash_table->mask = mask;
323 }
324
325 static gint
326 g_hash_table_find_closest_shift (gint n)
327 {
328   gint i;
329
330   for (i = 0; n; i++)
331     n >>= 1;
332
333   return i;
334 }
335
336 static void
337 g_hash_table_set_shift_from_size (GHashTable *hash_table, gint size)
338 {
339   gint shift;
340
341   shift = g_hash_table_find_closest_shift (size);
342   shift = MAX (shift, HASH_TABLE_MIN_SHIFT);
343
344   g_hash_table_set_shift (hash_table, shift);
345 }
346
347 /*
348  * g_hash_table_lookup_node:
349  * @hash_table: our #GHashTable
350  * @key: the key to lookup against
351  * @hash_return: key hash return location
352  *
353  * Performs a lookup in the hash table, preserving extra information
354  * usually needed for insertion.
355  *
356  * This function first computes the hash value of the key using the
357  * user's hash function.
358  *
359  * If an entry in the table matching @key is found then this function
360  * returns the index of that entry in the table, and if not, the
361  * index of an unused node (empty or tombstone) where the key can be
362  * inserted.
363  *
364  * The computed hash value is returned in the variable pointed to
365  * by @hash_return. This is to save insertions from having to compute
366  * the hash record again for the new record.
367  *
368  * Returns: index of the described node
369  */
370 static inline guint
371 g_hash_table_lookup_node (GHashTable    *hash_table,
372                           gconstpointer  key,
373                           guint         *hash_return)
374 {
375   guint node_index;
376   guint node_hash;
377   guint hash_value;
378   guint first_tombstone = 0;
379   gboolean have_tombstone = FALSE;
380   guint step = 0;
381
382   hash_value = hash_table->hash_func (key);
383   if (G_UNLIKELY (!HASH_IS_REAL (hash_value)))
384     hash_value = 2;
385
386   *hash_return = hash_value;
387
388   node_index = hash_value % hash_table->mod;
389   node_hash = hash_table->hashes[node_index];
390
391   while (!HASH_IS_UNUSED (node_hash))
392     {
393       /* We first check if our full hash values
394        * are equal so we can avoid calling the full-blown
395        * key equality function in most cases.
396        */
397       if (node_hash == hash_value)
398         {
399           gpointer node_key = hash_table->keys[node_index];
400
401           if (hash_table->key_equal_func)
402             {
403               if (hash_table->key_equal_func (node_key, key))
404                 return node_index;
405             }
406           else if (node_key == key)
407             {
408               return node_index;
409             }
410         }
411       else if (HASH_IS_TOMBSTONE (node_hash) && !have_tombstone)
412         {
413           first_tombstone = node_index;
414           have_tombstone = TRUE;
415         }
416
417       step++;
418       node_index += step;
419       node_index &= hash_table->mask;
420       node_hash = hash_table->hashes[node_index];
421     }
422
423   if (have_tombstone)
424     return first_tombstone;
425
426   return node_index;
427 }
428
429 /*
430  * g_hash_table_remove_node:
431  * @hash_table: our #GHashTable
432  * @node: pointer to node to remove
433  * @notify: %TRUE if the destroy notify handlers are to be called
434  *
435  * Removes a node from the hash table and updates the node count.
436  * The node is replaced by a tombstone. No table resize is performed.
437  *
438  * If @notify is %TRUE then the destroy notify functions are called
439  * for the key and value of the hash node.
440  */
441 static void
442 g_hash_table_remove_node (GHashTable   *hash_table,
443                           gint          i,
444                           gboolean      notify)
445 {
446   gpointer key;
447   gpointer value;
448
449   key = hash_table->keys[i];
450   value = hash_table->values[i];
451
452   /* Erect tombstone */
453   hash_table->hashes[i] = TOMBSTONE_HASH_VALUE;
454
455   /* Be GC friendly */
456   hash_table->keys[i] = NULL;
457   hash_table->values[i] = NULL;
458
459   hash_table->nnodes--;
460
461   if (notify && hash_table->key_destroy_func)
462     hash_table->key_destroy_func (key);
463
464   if (notify && hash_table->value_destroy_func)
465     hash_table->value_destroy_func (value);
466
467 }
468
469 /*
470  * g_hash_table_remove_all_nodes:
471  * @hash_table: our #GHashTable
472  * @notify: %TRUE if the destroy notify handlers are to be called
473  *
474  * Removes all nodes from the table.  Since this may be a precursor to
475  * freeing the table entirely, no resize is performed.
476  *
477  * If @notify is %TRUE then the destroy notify functions are called
478  * for the key and value of the hash node.
479  */
480 static void
481 g_hash_table_remove_all_nodes (GHashTable *hash_table,
482                                gboolean    notify)
483 {
484   int i;
485   gpointer key;
486   gpointer value;
487
488   hash_table->nnodes = 0;
489   hash_table->noccupied = 0;
490
491   if (!notify ||
492       (hash_table->key_destroy_func == NULL &&
493        hash_table->value_destroy_func == NULL))
494     {
495       memset (hash_table->hashes, 0, hash_table->size * sizeof (guint));
496       memset (hash_table->keys, 0, hash_table->size * sizeof (gpointer));
497       memset (hash_table->values, 0, hash_table->size * sizeof (gpointer));
498
499       return;
500     }
501
502   for (i = 0; i < hash_table->size; i++)
503     {
504       if (HASH_IS_REAL (hash_table->hashes[i]))
505         {
506           key = hash_table->keys[i];
507           value = hash_table->values[i];
508
509           hash_table->hashes[i] = UNUSED_HASH_VALUE;
510           hash_table->keys[i] = NULL;
511           hash_table->values[i] = NULL;
512
513           if (hash_table->key_destroy_func != NULL)
514             hash_table->key_destroy_func (key);
515
516           if (hash_table->value_destroy_func != NULL)
517             hash_table->value_destroy_func (value);
518         }
519       else if (HASH_IS_TOMBSTONE (hash_table->hashes[i]))
520         {
521           hash_table->hashes[i] = UNUSED_HASH_VALUE;
522         }
523     }
524 }
525
526 /*
527  * g_hash_table_resize:
528  * @hash_table: our #GHashTable
529  *
530  * Resizes the hash table to the optimal size based on the number of
531  * nodes currently held. If you call this function then a resize will
532  * occur, even if one does not need to occur.
533  * Use g_hash_table_maybe_resize() instead.
534  *
535  * This function may "resize" the hash table to its current size, with
536  * the side effect of cleaning up tombstones and otherwise optimizing
537  * the probe sequences.
538  */
539 static void
540 g_hash_table_resize (GHashTable *hash_table)
541 {
542   gpointer *new_keys;
543   gpointer *new_values;
544   guint *new_hashes;
545   gint old_size;
546   gint i;
547
548   old_size = hash_table->size;
549   g_hash_table_set_shift_from_size (hash_table, hash_table->nnodes * 2);
550
551   new_keys = g_new0 (gpointer, hash_table->size);
552   if (hash_table->keys == hash_table->values)
553     new_values = new_keys;
554   else
555     new_values = g_new0 (gpointer, hash_table->size);
556   new_hashes = g_new0 (guint, hash_table->size);
557
558   for (i = 0; i < old_size; i++)
559     {
560       guint node_hash = hash_table->hashes[i];
561       guint hash_val;
562       guint step = 0;
563
564       if (!HASH_IS_REAL (node_hash))
565         continue;
566
567       hash_val = node_hash % hash_table->mod;
568
569       while (!HASH_IS_UNUSED (new_hashes[hash_val]))
570         {
571           step++;
572           hash_val += step;
573           hash_val &= hash_table->mask;
574         }
575
576       new_hashes[hash_val] = hash_table->hashes[i];
577       new_keys[hash_val] = hash_table->keys[i];
578       new_values[hash_val] = hash_table->values[i];
579     }
580
581   if (hash_table->keys != hash_table->values)
582     g_free (hash_table->values);
583
584   g_free (hash_table->keys);
585   g_free (hash_table->hashes);
586
587   hash_table->keys = new_keys;
588   hash_table->values = new_values;
589   hash_table->hashes = new_hashes;
590
591   hash_table->noccupied = hash_table->nnodes;
592 }
593
594 /*
595  * g_hash_table_maybe_resize:
596  * @hash_table: our #GHashTable
597  *
598  * Resizes the hash table, if needed.
599  *
600  * Essentially, calls g_hash_table_resize() if the table has strayed
601  * too far from its ideal size for its number of nodes.
602  */
603 static inline void
604 g_hash_table_maybe_resize (GHashTable *hash_table)
605 {
606   gint noccupied = hash_table->noccupied;
607   gint size = hash_table->size;
608
609   if ((size > hash_table->nnodes * 4 && size > 1 << HASH_TABLE_MIN_SHIFT) ||
610       (size <= noccupied + (noccupied / 16)))
611     g_hash_table_resize (hash_table);
612 }
613
614 /**
615  * g_hash_table_new:
616  * @hash_func: a function to create a hash value from a key
617  * @key_equal_func: a function to check two keys for equality
618  *
619  * Creates a new #GHashTable with a reference count of 1.
620  *
621  * Hash values returned by @hash_func are used to determine where keys
622  * are stored within the #GHashTable data structure. The g_direct_hash(),
623  * g_int_hash(), g_int64_hash(), g_double_hash() and g_str_hash()
624  * functions are provided for some common types of keys.
625  * If @hash_func is %NULL, g_direct_hash() is used.
626  *
627  * @key_equal_func is used when looking up keys in the #GHashTable.
628  * The g_direct_equal(), g_int_equal(), g_int64_equal(), g_double_equal()
629  * and g_str_equal() functions are provided for the most common types
630  * of keys. If @key_equal_func is %NULL, keys are compared directly in
631  * a similar fashion to g_direct_equal(), but without the overhead of
632  * a function call.
633  *
634  * Return value: a new #GHashTable
635  */
636 GHashTable *
637 g_hash_table_new (GHashFunc  hash_func,
638                   GEqualFunc key_equal_func)
639 {
640   return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
641 }
642
643
644 /**
645  * g_hash_table_new_full:
646  * @hash_func: a function to create a hash value from a key
647  * @key_equal_func: a function to check two keys for equality
648  * @key_destroy_func: a function to free the memory allocated for the key
649  *     used when removing the entry from the #GHashTable, or %NULL
650  *     if you don't want to supply such a function.
651  * @value_destroy_func: a function to free the memory allocated for the
652  *     value used when removing the entry from the #GHashTable, or %NULL
653  *     if you don't want to supply such a function.
654  *
655  * Creates a new #GHashTable like g_hash_table_new() with a reference
656  * count of 1 and allows to specify functions to free the memory
657  * allocated for the key and value that get called when removing the
658  * entry from the #GHashTable.
659  *
660  * Return value: a new #GHashTable
661  */
662 GHashTable *
663 g_hash_table_new_full (GHashFunc      hash_func,
664                        GEqualFunc     key_equal_func,
665                        GDestroyNotify key_destroy_func,
666                        GDestroyNotify value_destroy_func)
667 {
668   GHashTable *hash_table;
669
670   hash_table = g_slice_new (GHashTable);
671   g_hash_table_set_shift (hash_table, HASH_TABLE_MIN_SHIFT);
672   hash_table->nnodes             = 0;
673   hash_table->noccupied          = 0;
674   hash_table->hash_func          = hash_func ? hash_func : g_direct_hash;
675   hash_table->key_equal_func     = key_equal_func;
676   hash_table->ref_count          = 1;
677 #ifndef G_DISABLE_ASSERT
678   hash_table->version            = 0;
679 #endif
680   hash_table->key_destroy_func   = key_destroy_func;
681   hash_table->value_destroy_func = value_destroy_func;
682   hash_table->keys               = g_new0 (gpointer, hash_table->size);
683   hash_table->values             = hash_table->keys;
684   hash_table->hashes             = g_new0 (guint, hash_table->size);
685
686   return hash_table;
687 }
688
689 /**
690  * g_hash_table_iter_init:
691  * @iter: an uninitialized #GHashTableIter
692  * @hash_table: a #GHashTable
693  *
694  * Initializes a key/value pair iterator and associates it with
695  * @hash_table. Modifying the hash table after calling this function
696  * invalidates the returned iterator.
697  * |[
698  * GHashTableIter iter;
699  * gpointer key, value;
700  *
701  * g_hash_table_iter_init (&iter, hash_table);
702  * while (g_hash_table_iter_next (&iter, &key, &value))
703  *   {
704  *     /&ast; do something with key and value &ast;/
705  *   }
706  * ]|
707  *
708  * Since: 2.16
709  */
710 void
711 g_hash_table_iter_init (GHashTableIter *iter,
712                         GHashTable     *hash_table)
713 {
714   RealIter *ri = (RealIter *) iter;
715
716   g_return_if_fail (iter != NULL);
717   g_return_if_fail (hash_table != NULL);
718
719   ri->hash_table = hash_table;
720   ri->position = -1;
721 #ifndef G_DISABLE_ASSERT
722   ri->version = hash_table->version;
723 #endif
724 }
725
726 /**
727  * g_hash_table_iter_next:
728  * @iter: an initialized #GHashTableIter
729  * @key: a location to store the key, or %NULL
730  * @value: a location to store the value, or %NULL
731  *
732  * Advances @iter and retrieves the key and/or value that are now
733  * pointed to as a result of this advancement. If %FALSE is returned,
734  * @key and @value are not set, and the iterator becomes invalid.
735  *
736  * Return value: %FALSE if the end of the #GHashTable has been reached.
737  *
738  * Since: 2.16
739  */
740 gboolean
741 g_hash_table_iter_next (GHashTableIter *iter,
742                         gpointer       *key,
743                         gpointer       *value)
744 {
745   RealIter *ri = (RealIter *) iter;
746   gint position;
747
748   g_return_val_if_fail (iter != NULL, FALSE);
749 #ifndef G_DISABLE_ASSERT
750   g_return_val_if_fail (ri->version == ri->hash_table->version, FALSE);
751 #endif
752   g_return_val_if_fail (ri->position < ri->hash_table->size, FALSE);
753
754   position = ri->position;
755
756   do
757     {
758       position++;
759       if (position >= ri->hash_table->size)
760         {
761           ri->position = position;
762           return FALSE;
763         }
764     }
765   while (!HASH_IS_REAL (ri->hash_table->hashes[position]));
766
767   if (key != NULL)
768     *key = ri->hash_table->keys[position];
769   if (value != NULL)
770     *value = ri->hash_table->values[position];
771
772   ri->position = position;
773   return TRUE;
774 }
775
776 /**
777  * g_hash_table_iter_get_hash_table:
778  * @iter: an initialized #GHashTableIter
779  *
780  * Returns the #GHashTable associated with @iter.
781  *
782  * Return value: the #GHashTable associated with @iter.
783  *
784  * Since: 2.16
785  */
786 GHashTable *
787 g_hash_table_iter_get_hash_table (GHashTableIter *iter)
788 {
789   g_return_val_if_fail (iter != NULL, NULL);
790
791   return ((RealIter *) iter)->hash_table;
792 }
793
794 static void
795 iter_remove_or_steal (RealIter *ri, gboolean notify)
796 {
797   g_return_if_fail (ri != NULL);
798 #ifndef G_DISABLE_ASSERT
799   g_return_if_fail (ri->version == ri->hash_table->version);
800 #endif
801   g_return_if_fail (ri->position >= 0);
802   g_return_if_fail (ri->position < ri->hash_table->size);
803
804   g_hash_table_remove_node (ri->hash_table, ri->position, notify);
805
806 #ifndef G_DISABLE_ASSERT
807   ri->version++;
808   ri->hash_table->version++;
809 #endif
810 }
811
812 /**
813  * g_hash_table_iter_remove:
814  * @iter: an initialized #GHashTableIter
815  *
816  * Removes the key/value pair currently pointed to by the iterator
817  * from its associated #GHashTable. Can only be called after
818  * g_hash_table_iter_next() returned %TRUE, and cannot be called
819  * more than once for the same key/value pair.
820  *
821  * If the #GHashTable was created using g_hash_table_new_full(),
822  * the key and value are freed using the supplied destroy functions,
823  * otherwise you have to make sure that any dynamically allocated
824  * values are freed yourself.
825  *
826  * Since: 2.16
827  */
828 void
829 g_hash_table_iter_remove (GHashTableIter *iter)
830 {
831   iter_remove_or_steal ((RealIter *) iter, TRUE);
832 }
833
834 /*
835  * g_hash_table_insert_node:
836  * @hash_table: our #GHashTable
837  * @node_index: pointer to node to insert/replace
838  * @key_hash: key hash
839  * @key: key to replace with
840  * @value: value to replace with
841  *
842  * Inserts a value at @node_index in the hash table and updates it.
843  */
844 static void
845 g_hash_table_insert_node (GHashTable *hash_table,
846                           guint       node_index,
847                           guint       key_hash,
848                           gpointer    key,
849                           gpointer    value,
850                           gboolean    keep_new_key)
851 {
852   guint old_hash;
853   gpointer old_key;
854   gpointer old_value;
855
856   if (G_UNLIKELY (hash_table->keys == hash_table->values && key != value))
857     hash_table->values = g_memdup (hash_table->keys, sizeof (gpointer) * hash_table->size);
858
859   old_hash = hash_table->hashes[node_index];
860   old_key = hash_table->keys[node_index];
861   old_value = hash_table->values[node_index];
862
863   if (HASH_IS_REAL (old_hash))
864     {
865       if (keep_new_key)
866         hash_table->keys[node_index] = key;
867       hash_table->values[node_index] = value;
868     }
869   else
870     {
871       hash_table->keys[node_index] = key;
872       hash_table->values[node_index] = value;
873       hash_table->hashes[node_index] = key_hash;
874
875       hash_table->nnodes++;
876
877       if (HASH_IS_UNUSED (old_hash))
878         {
879           /* We replaced an empty node, and not a tombstone */
880           hash_table->noccupied++;
881           g_hash_table_maybe_resize (hash_table);
882         }
883
884 #ifndef G_DISABLE_ASSERT
885       hash_table->version++;
886 #endif
887     }
888
889   if (HASH_IS_REAL (old_hash))
890     {
891       if (hash_table->key_destroy_func)
892         hash_table->key_destroy_func (keep_new_key ? old_key : key);
893       if (hash_table->value_destroy_func)
894         hash_table->value_destroy_func (old_value);
895     }
896 }
897
898 /**
899  * g_hash_table_iter_replace:
900  * @iter: an initialized #GHashTableIter
901  * @value: the value to replace with
902  *
903  * Replaces the value currently pointed to by the iterator
904  * from its associated #GHashTable. Can only be called after
905  * g_hash_table_iter_next() returned %TRUE.
906  *
907  * If you supplied a @value_destroy_func when creating the
908  * #GHashTable, the old value is freed using that function.
909  *
910  * Since: 2.30
911  */
912 void
913 g_hash_table_iter_replace (GHashTableIter *iter,
914                            gpointer        value)
915 {
916   RealIter *ri;
917   guint node_hash;
918   gpointer key;
919
920   ri = (RealIter *) iter;
921
922   g_return_if_fail (ri != NULL);
923 #ifndef G_DISABLE_ASSERT
924   g_return_if_fail (ri->version == ri->hash_table->version);
925 #endif
926   g_return_if_fail (ri->position >= 0);
927   g_return_if_fail (ri->position < ri->hash_table->size);
928
929   node_hash = ri->hash_table->hashes[ri->position];
930   key = ri->hash_table->keys[ri->position];
931
932   g_hash_table_insert_node (ri->hash_table, ri->position, node_hash, key, value, TRUE);
933
934 #ifndef G_DISABLE_ASSERT
935   ri->version++;
936   ri->hash_table->version++;
937 #endif
938 }
939
940 /**
941  * g_hash_table_iter_steal:
942  * @iter: an initialized #GHashTableIter
943  *
944  * Removes the key/value pair currently pointed to by the
945  * iterator from its associated #GHashTable, without calling
946  * the key and value destroy functions. Can only be called
947  * after g_hash_table_iter_next() returned %TRUE, and cannot
948  * be called more than once for the same key/value pair.
949  *
950  * Since: 2.16
951  */
952 void
953 g_hash_table_iter_steal (GHashTableIter *iter)
954 {
955   iter_remove_or_steal ((RealIter *) iter, FALSE);
956 }
957
958
959 /**
960  * g_hash_table_ref:
961  * @hash_table: a valid #GHashTable
962  *
963  * Atomically increments the reference count of @hash_table by one.
964  * This function is MT-safe and may be called from any thread.
965  *
966  * Return value: the passed in #GHashTable
967  *
968  * Since: 2.10
969  */
970 GHashTable *
971 g_hash_table_ref (GHashTable *hash_table)
972 {
973   g_return_val_if_fail (hash_table != NULL, NULL);
974
975   g_atomic_int_inc (&hash_table->ref_count);
976
977   return hash_table;
978 }
979
980 /**
981  * g_hash_table_unref:
982  * @hash_table: a valid #GHashTable
983  *
984  * Atomically decrements the reference count of @hash_table by one.
985  * If the reference count drops to 0, all keys and values will be
986  * destroyed, and all memory allocated by the hash table is released.
987  * This function is MT-safe and may be called from any thread.
988  *
989  * Since: 2.10
990  */
991 void
992 g_hash_table_unref (GHashTable *hash_table)
993 {
994   g_return_if_fail (hash_table != NULL);
995
996   if (g_atomic_int_dec_and_test (&hash_table->ref_count))
997     {
998       g_hash_table_remove_all_nodes (hash_table, TRUE);
999       if (hash_table->keys != hash_table->values)
1000         g_free (hash_table->values);
1001       g_free (hash_table->keys);
1002       g_free (hash_table->hashes);
1003       g_slice_free (GHashTable, hash_table);
1004     }
1005 }
1006
1007 /**
1008  * g_hash_table_destroy:
1009  * @hash_table: a #GHashTable
1010  *
1011  * Destroys all keys and values in the #GHashTable and decrements its
1012  * reference count by 1. If keys and/or values are dynamically allocated,
1013  * you should either free them first or create the #GHashTable with destroy
1014  * notifiers using g_hash_table_new_full(). In the latter case the destroy
1015  * functions you supplied will be called on all keys and values during the
1016  * destruction phase.
1017  */
1018 void
1019 g_hash_table_destroy (GHashTable *hash_table)
1020 {
1021   g_return_if_fail (hash_table != NULL);
1022
1023   g_hash_table_remove_all (hash_table);
1024   g_hash_table_unref (hash_table);
1025 }
1026
1027 /**
1028  * g_hash_table_lookup:
1029  * @hash_table: a #GHashTable
1030  * @key: the key to look up
1031  *
1032  * Looks up a key in a #GHashTable. Note that this function cannot
1033  * distinguish between a key that is not present and one which is present
1034  * and has the value %NULL. If you need this distinction, use
1035  * g_hash_table_lookup_extended().
1036  *
1037  * Return value: the associated value, or %NULL if the key is not found
1038  */
1039 gpointer
1040 g_hash_table_lookup (GHashTable    *hash_table,
1041                      gconstpointer  key)
1042 {
1043   guint node_index;
1044   guint node_hash;
1045
1046   g_return_val_if_fail (hash_table != NULL, NULL);
1047
1048   node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1049
1050   return HASH_IS_REAL (hash_table->hashes[node_index])
1051     ? hash_table->values[node_index]
1052     : NULL;
1053 }
1054
1055 /**
1056  * g_hash_table_lookup_extended:
1057  * @hash_table: a #GHashTable
1058  * @lookup_key: the key to look up
1059  * @orig_key: return location for the original key, or %NULL
1060  * @value: return location for the value associated with the key, or %NULL
1061  *
1062  * Looks up a key in the #GHashTable, returning the original key and the
1063  * associated value and a #gboolean which is %TRUE if the key was found. This
1064  * is useful if you need to free the memory allocated for the original key,
1065  * for example before calling g_hash_table_remove().
1066  *
1067  * You can actually pass %NULL for @lookup_key to test
1068  * whether the %NULL key exists, provided the hash and equal functions
1069  * of @hash_table are %NULL-safe.
1070  *
1071  * Return value: %TRUE if the key was found in the #GHashTable
1072  */
1073 gboolean
1074 g_hash_table_lookup_extended (GHashTable    *hash_table,
1075                               gconstpointer  lookup_key,
1076                               gpointer      *orig_key,
1077                               gpointer      *value)
1078 {
1079   guint node_index;
1080   guint node_hash;
1081
1082   g_return_val_if_fail (hash_table != NULL, FALSE);
1083
1084   node_index = g_hash_table_lookup_node (hash_table, lookup_key, &node_hash);
1085
1086   if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1087     return FALSE;
1088
1089   if (orig_key)
1090     *orig_key = hash_table->keys[node_index];
1091
1092   if (value)
1093     *value = hash_table->values[node_index];
1094
1095   return TRUE;
1096 }
1097
1098 /*
1099  * g_hash_table_insert_internal:
1100  * @hash_table: our #GHashTable
1101  * @key: the key to insert
1102  * @value: the value to insert
1103  * @keep_new_key: if %TRUE and this key already exists in the table
1104  *   then call the destroy notify function on the old key.  If %FALSE
1105  *   then call the destroy notify function on the new key.
1106  *
1107  * Implements the common logic for the g_hash_table_insert() and
1108  * g_hash_table_replace() functions.
1109  *
1110  * Do a lookup of @key. If it is found, replace it with the new
1111  * @value (and perhaps the new @key). If it is not found, create
1112  * a new node.
1113  */
1114 static void
1115 g_hash_table_insert_internal (GHashTable *hash_table,
1116                               gpointer    key,
1117                               gpointer    value,
1118                               gboolean    keep_new_key)
1119 {
1120   guint key_hash;
1121   guint node_index;
1122
1123   g_return_if_fail (hash_table != NULL);
1124
1125   node_index = g_hash_table_lookup_node (hash_table, key, &key_hash);
1126
1127   g_hash_table_insert_node (hash_table, node_index, key_hash, key, value, keep_new_key);
1128 }
1129
1130 /**
1131  * g_hash_table_insert:
1132  * @hash_table: a #GHashTable
1133  * @key: a key to insert
1134  * @value: the value to associate with the key
1135  *
1136  * Inserts a new key and value into a #GHashTable.
1137  *
1138  * If the key already exists in the #GHashTable its current
1139  * value is replaced with the new value. If you supplied a
1140  * @value_destroy_func when creating the #GHashTable, the old
1141  * value is freed using that function. If you supplied a
1142  * @key_destroy_func when creating the #GHashTable, the passed
1143  * key is freed using that function.
1144  */
1145 void
1146 g_hash_table_insert (GHashTable *hash_table,
1147                      gpointer    key,
1148                      gpointer    value)
1149 {
1150   g_hash_table_insert_internal (hash_table, key, value, FALSE);
1151 }
1152
1153 /**
1154  * g_hash_table_replace:
1155  * @hash_table: a #GHashTable
1156  * @key: a key to insert
1157  * @value: the value to associate with the key
1158  *
1159  * Inserts a new key and value into a #GHashTable similar to
1160  * g_hash_table_insert(). The difference is that if the key
1161  * already exists in the #GHashTable, it gets replaced by the
1162  * new key. If you supplied a @value_destroy_func when creating
1163  * the #GHashTable, the old value is freed using that function.
1164  * If you supplied a @key_destroy_func when creating the
1165  * #GHashTable, the old key is freed using that function.
1166  */
1167 void
1168 g_hash_table_replace (GHashTable *hash_table,
1169                       gpointer    key,
1170                       gpointer    value)
1171 {
1172   g_hash_table_insert_internal (hash_table, key, value, TRUE);
1173 }
1174
1175 /*
1176  * g_hash_table_remove_internal:
1177  * @hash_table: our #GHashTable
1178  * @key: the key to remove
1179  * @notify: %TRUE if the destroy notify handlers are to be called
1180  * Return value: %TRUE if a node was found and removed, else %FALSE
1181  *
1182  * Implements the common logic for the g_hash_table_remove() and
1183  * g_hash_table_steal() functions.
1184  *
1185  * Do a lookup of @key and remove it if it is found, calling the
1186  * destroy notify handlers only if @notify is %TRUE.
1187  */
1188 static gboolean
1189 g_hash_table_remove_internal (GHashTable    *hash_table,
1190                               gconstpointer  key,
1191                               gboolean       notify)
1192 {
1193   guint node_index;
1194   guint node_hash;
1195
1196   g_return_val_if_fail (hash_table != NULL, FALSE);
1197
1198   node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1199
1200   if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1201     return FALSE;
1202
1203   g_hash_table_remove_node (hash_table, node_index, notify);
1204   g_hash_table_maybe_resize (hash_table);
1205
1206 #ifndef G_DISABLE_ASSERT
1207   hash_table->version++;
1208 #endif
1209
1210   return TRUE;
1211 }
1212
1213 /**
1214  * g_hash_table_remove:
1215  * @hash_table: a #GHashTable
1216  * @key: the key to remove
1217  *
1218  * Removes a key and its associated value from a #GHashTable.
1219  *
1220  * If the #GHashTable was created using g_hash_table_new_full(), the
1221  * key and value are freed using the supplied destroy functions, otherwise
1222  * you have to make sure that any dynamically allocated values are freed
1223  * yourself.
1224  *
1225  * Returns: %TRUE if the key was found and removed from the #GHashTable
1226  */
1227 gboolean
1228 g_hash_table_remove (GHashTable    *hash_table,
1229                      gconstpointer  key)
1230 {
1231   return g_hash_table_remove_internal (hash_table, key, TRUE);
1232 }
1233
1234 /**
1235  * g_hash_table_steal:
1236  * @hash_table: a #GHashTable
1237  * @key: the key to remove
1238  *
1239  * Removes a key and its associated value from a #GHashTable without
1240  * calling the key and value destroy functions.
1241  *
1242  * Returns: %TRUE if the key was found and removed from the #GHashTable
1243  */
1244 gboolean
1245 g_hash_table_steal (GHashTable    *hash_table,
1246                     gconstpointer  key)
1247 {
1248   return g_hash_table_remove_internal (hash_table, key, FALSE);
1249 }
1250
1251 /**
1252  * g_hash_table_remove_all:
1253  * @hash_table: a #GHashTable
1254  *
1255  * Removes all keys and their associated values from a #GHashTable.
1256  *
1257  * If the #GHashTable was created using g_hash_table_new_full(),
1258  * the keys and values are freed using the supplied destroy functions,
1259  * otherwise you have to make sure that any dynamically allocated
1260  * values are freed yourself.
1261  *
1262  * Since: 2.12
1263  */
1264 void
1265 g_hash_table_remove_all (GHashTable *hash_table)
1266 {
1267   g_return_if_fail (hash_table != NULL);
1268
1269 #ifndef G_DISABLE_ASSERT
1270   if (hash_table->nnodes != 0)
1271     hash_table->version++;
1272 #endif
1273
1274   g_hash_table_remove_all_nodes (hash_table, TRUE);
1275   g_hash_table_maybe_resize (hash_table);
1276 }
1277
1278 /**
1279  * g_hash_table_steal_all:
1280  * @hash_table: a #GHashTable
1281  *
1282  * Removes all keys and their associated values from a #GHashTable
1283  * without calling the key and value destroy functions.
1284  *
1285  * Since: 2.12
1286  */
1287 void
1288 g_hash_table_steal_all (GHashTable *hash_table)
1289 {
1290   g_return_if_fail (hash_table != NULL);
1291
1292 #ifndef G_DISABLE_ASSERT
1293   if (hash_table->nnodes != 0)
1294     hash_table->version++;
1295 #endif
1296
1297   g_hash_table_remove_all_nodes (hash_table, FALSE);
1298   g_hash_table_maybe_resize (hash_table);
1299 }
1300
1301 /*
1302  * g_hash_table_foreach_remove_or_steal:
1303  * @hash_table: a #GHashTable
1304  * @func: the user's callback function
1305  * @user_data: data for @func
1306  * @notify: %TRUE if the destroy notify handlers are to be called
1307  *
1308  * Implements the common logic for g_hash_table_foreach_remove()
1309  * and g_hash_table_foreach_steal().
1310  *
1311  * Iterates over every node in the table, calling @func with the key
1312  * and value of the node (and @user_data). If @func returns %TRUE the
1313  * node is removed from the table.
1314  *
1315  * If @notify is true then the destroy notify handlers will be called
1316  * for each removed node.
1317  */
1318 static guint
1319 g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
1320                                       GHRFunc     func,
1321                                       gpointer    user_data,
1322                                       gboolean    notify)
1323 {
1324   guint deleted = 0;
1325   gint i;
1326 #ifndef G_DISABLE_ASSERT
1327   gint version = hash_table->version;
1328 #endif
1329
1330   for (i = 0; i < hash_table->size; i++)
1331     {
1332       guint node_hash = hash_table->hashes[i];
1333       gpointer node_key = hash_table->keys[i];
1334       gpointer node_value = hash_table->values[i];
1335
1336       if (HASH_IS_REAL (node_hash) &&
1337           (* func) (node_key, node_value, user_data))
1338         {
1339           g_hash_table_remove_node (hash_table, i, notify);
1340           deleted++;
1341         }
1342
1343 #ifndef G_DISABLE_ASSERT
1344       g_return_val_if_fail (version == hash_table->version, 0);
1345 #endif
1346     }
1347
1348   g_hash_table_maybe_resize (hash_table);
1349
1350 #ifndef G_DISABLE_ASSERT
1351   if (deleted > 0)
1352     hash_table->version++;
1353 #endif
1354
1355   return deleted;
1356 }
1357
1358 /**
1359  * g_hash_table_foreach_remove:
1360  * @hash_table: a #GHashTable
1361  * @func: the function to call for each key/value pair
1362  * @user_data: user data to pass to the function
1363  *
1364  * Calls the given function for each key/value pair in the
1365  * #GHashTable. If the function returns %TRUE, then the key/value
1366  * pair is removed from the #GHashTable. If you supplied key or
1367  * value destroy functions when creating the #GHashTable, they are
1368  * used to free the memory allocated for the removed keys and values.
1369  *
1370  * See #GHashTableIter for an alternative way to loop over the
1371  * key/value pairs in the hash table.
1372  *
1373  * Return value: the number of key/value pairs removed
1374  */
1375 guint
1376 g_hash_table_foreach_remove (GHashTable *hash_table,
1377                              GHRFunc     func,
1378                              gpointer    user_data)
1379 {
1380   g_return_val_if_fail (hash_table != NULL, 0);
1381   g_return_val_if_fail (func != NULL, 0);
1382
1383   return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
1384 }
1385
1386 /**
1387  * g_hash_table_foreach_steal:
1388  * @hash_table: a #GHashTable
1389  * @func: the function to call for each key/value pair
1390  * @user_data: user data to pass to the function
1391  *
1392  * Calls the given function for each key/value pair in the
1393  * #GHashTable. If the function returns %TRUE, then the key/value
1394  * pair is removed from the #GHashTable, but no key or value
1395  * destroy functions are called.
1396  *
1397  * See #GHashTableIter for an alternative way to loop over the
1398  * key/value pairs in the hash table.
1399  *
1400  * Return value: the number of key/value pairs removed.
1401  */
1402 guint
1403 g_hash_table_foreach_steal (GHashTable *hash_table,
1404                             GHRFunc     func,
1405                             gpointer    user_data)
1406 {
1407   g_return_val_if_fail (hash_table != NULL, 0);
1408   g_return_val_if_fail (func != NULL, 0);
1409
1410   return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
1411 }
1412
1413 /**
1414  * g_hash_table_foreach:
1415  * @hash_table: a #GHashTable
1416  * @func: the function to call for each key/value pair
1417  * @user_data: user data to pass to the function
1418  *
1419  * Calls the given function for each of the key/value pairs in the
1420  * #GHashTable.  The function is passed the key and value of each
1421  * pair, and the given @user_data parameter.  The hash table may not
1422  * be modified while iterating over it (you can't add/remove
1423  * items). To remove all items matching a predicate, use
1424  * g_hash_table_foreach_remove().
1425  *
1426  * See g_hash_table_find() for performance caveats for linear
1427  * order searches in contrast to g_hash_table_lookup().
1428  */
1429 void
1430 g_hash_table_foreach (GHashTable *hash_table,
1431                       GHFunc      func,
1432                       gpointer    user_data)
1433 {
1434   gint i;
1435 #ifndef G_DISABLE_ASSERT
1436   gint version = hash_table->version;
1437 #endif
1438
1439   g_return_if_fail (hash_table != NULL);
1440   g_return_if_fail (func != NULL);
1441
1442   for (i = 0; i < hash_table->size; i++)
1443     {
1444       guint node_hash = hash_table->hashes[i];
1445       gpointer node_key = hash_table->keys[i];
1446       gpointer node_value = hash_table->values[i];
1447
1448       if (HASH_IS_REAL (node_hash))
1449         (* func) (node_key, node_value, user_data);
1450
1451 #ifndef G_DISABLE_ASSERT
1452       g_return_if_fail (version == hash_table->version);
1453 #endif
1454     }
1455 }
1456
1457 /**
1458  * g_hash_table_find:
1459  * @hash_table: a #GHashTable
1460  * @predicate: function to test the key/value pairs for a certain property
1461  * @user_data: user data to pass to the function
1462  *
1463  * Calls the given function for key/value pairs in the #GHashTable
1464  * until @predicate returns %TRUE. The function is passed the key
1465  * and value of each pair, and the given @user_data parameter. The
1466  * hash table may not be modified while iterating over it (you can't
1467  * add/remove items).
1468  *
1469  * Note, that hash tables are really only optimized for forward
1470  * lookups, i.e. g_hash_table_lookup(). So code that frequently issues
1471  * g_hash_table_find() or g_hash_table_foreach() (e.g. in the order of
1472  * once per every entry in a hash table) should probably be reworked
1473  * to use additional or different data structures for reverse lookups
1474  * (keep in mind that an O(n) find/foreach operation issued for all n
1475  * values in a hash table ends up needing O(n*n) operations).
1476  *
1477  * Return value: The value of the first key/value pair is returned,
1478  *     for which @predicate evaluates to %TRUE. If no pair with the
1479  *     requested property is found, %NULL is returned.
1480  *
1481  * Since: 2.4
1482  */
1483 gpointer
1484 g_hash_table_find (GHashTable *hash_table,
1485                    GHRFunc     predicate,
1486                    gpointer    user_data)
1487 {
1488   gint i;
1489 #ifndef G_DISABLE_ASSERT
1490   gint version = hash_table->version;
1491 #endif
1492   gboolean match;
1493
1494   g_return_val_if_fail (hash_table != NULL, NULL);
1495   g_return_val_if_fail (predicate != NULL, NULL);
1496
1497   match = FALSE;
1498
1499   for (i = 0; i < hash_table->size; i++)
1500     {
1501       guint node_hash = hash_table->hashes[i];
1502       gpointer node_key = hash_table->keys[i];
1503       gpointer node_value = hash_table->values[i];
1504
1505       if (HASH_IS_REAL (node_hash))
1506         match = predicate (node_key, node_value, user_data);
1507
1508 #ifndef G_DISABLE_ASSERT
1509       g_return_val_if_fail (version == hash_table->version, NULL);
1510 #endif
1511
1512       if (match)
1513         return node_value;
1514     }
1515
1516   return NULL;
1517 }
1518
1519 /**
1520  * g_hash_table_size:
1521  * @hash_table: a #GHashTable
1522  *
1523  * Returns the number of elements contained in the #GHashTable.
1524  *
1525  * Return value: the number of key/value pairs in the #GHashTable.
1526  */
1527 guint
1528 g_hash_table_size (GHashTable *hash_table)
1529 {
1530   g_return_val_if_fail (hash_table != NULL, 0);
1531
1532   return hash_table->nnodes;
1533 }
1534
1535 /**
1536  * g_hash_table_get_keys:
1537  * @hash_table: a #GHashTable
1538  *
1539  * Retrieves every key inside @hash_table. The returned data
1540  * is valid until @hash_table is modified.
1541  *
1542  * Return value: a #GList containing all the keys inside the hash
1543  *     table. The content of the list is owned by the hash table and
1544  *     should not be modified or freed. Use g_list_free() when done
1545  *     using the list.
1546  *
1547  * Since: 2.14
1548  */
1549 GList *
1550 g_hash_table_get_keys (GHashTable *hash_table)
1551 {
1552   gint i;
1553   GList *retval;
1554
1555   g_return_val_if_fail (hash_table != NULL, NULL);
1556
1557   retval = NULL;
1558   for (i = 0; i < hash_table->size; i++)
1559     {
1560       if (HASH_IS_REAL (hash_table->hashes[i]))
1561         retval = g_list_prepend (retval, hash_table->keys[i]);
1562     }
1563
1564   return retval;
1565 }
1566
1567 /**
1568  * g_hash_table_get_values:
1569  * @hash_table: a #GHashTable
1570  *
1571  * Retrieves every value inside @hash_table. The returned data
1572  * is valid until @hash_table is modified.
1573  *
1574  * Return value: a #GList containing all the values inside the hash
1575  *     table. The content of the list is owned by the hash table and
1576  *     should not be modified or freed. Use g_list_free() when done
1577  *     using the list.
1578  *
1579  * Since: 2.14
1580  */
1581 GList *
1582 g_hash_table_get_values (GHashTable *hash_table)
1583 {
1584   gint i;
1585   GList *retval;
1586
1587   g_return_val_if_fail (hash_table != NULL, NULL);
1588
1589   retval = NULL;
1590   for (i = 0; i < hash_table->size; i++)
1591     {
1592       if (HASH_IS_REAL (hash_table->hashes[i]))
1593         retval = g_list_prepend (retval, hash_table->values[i]);
1594     }
1595
1596   return retval;
1597 }
1598
1599 /* Hash functions.
1600  */
1601
1602 /**
1603  * g_str_equal:
1604  * @v1: a key
1605  * @v2: a key to compare with @v1
1606  *
1607  * Compares two strings for byte-by-byte equality and returns %TRUE
1608  * if they are equal. It can be passed to g_hash_table_new() as the
1609  * @key_equal_func parameter, when using strings as keys in a #GHashTable.
1610  *
1611  * Note that this function is primarily meant as a hash table comparison
1612  * function. For a general-purpose, %NULL-safe string comparison function,
1613  * see g_strcmp0().
1614  *
1615  * Returns: %TRUE if the two keys match
1616  */
1617 gboolean
1618 g_str_equal (gconstpointer v1,
1619              gconstpointer v2)
1620 {
1621   const gchar *string1 = v1;
1622   const gchar *string2 = v2;
1623
1624   return strcmp (string1, string2) == 0;
1625 }
1626
1627 /**
1628  * g_str_hash:
1629  * @v: a string key
1630  *
1631  * Converts a string to a hash value.
1632  *
1633  * This function implements the widely used "djb" hash apparently posted
1634  * by Daniel Bernstein to comp.lang.c some time ago.  The 32 bit
1635  * unsigned hash value starts at 5381 and for each byte 'c' in the
1636  * string, is updated: <literal>hash = hash * 33 + c</literal>.  This
1637  * function uses the signed value of each byte.
1638  *
1639  * It can be passed to g_hash_table_new() as the @hash_func parameter,
1640  * when using strings as keys in a #GHashTable.
1641  *
1642  * Returns: a hash value corresponding to the key
1643  */
1644 guint
1645 g_str_hash (gconstpointer v)
1646 {
1647   const signed char *p;
1648   guint32 h = 5381;
1649
1650   for (p = v; *p != '\0'; p++)
1651     h = (h << 5) + h + *p;
1652
1653   return h;
1654 }
1655
1656 /**
1657  * g_direct_hash:
1658  * @v: a #gpointer key
1659  *
1660  * Converts a gpointer to a hash value.
1661  * It can be passed to g_hash_table_new() as the @hash_func parameter, 
1662  * when using pointers as keys in a #GHashTable.
1663  *
1664  * Returns: a hash value corresponding to the key.
1665  */
1666 guint
1667 g_direct_hash (gconstpointer v)
1668 {
1669   return GPOINTER_TO_UINT (v);
1670 }
1671
1672 /**
1673  * g_direct_equal:
1674  * @v1: a key
1675  * @v2: a key to compare with @v1
1676  *
1677  * Compares two #gpointer arguments and returns %TRUE if they are equal.
1678  * It can be passed to g_hash_table_new() as the @key_equal_func
1679  * parameter, when using pointers as keys in a #GHashTable.
1680  *
1681  * Returns: %TRUE if the two keys match.
1682  */
1683 gboolean
1684 g_direct_equal (gconstpointer v1,
1685                 gconstpointer v2)
1686 {
1687   return v1 == v2;
1688 }
1689
1690 /**
1691  * g_int_equal:
1692  * @v1: a pointer to a #gint key
1693  * @v2: a pointer to a #gint key to compare with @v1
1694  *
1695  * Compares the two #gint values being pointed to and returns
1696  * %TRUE if they are equal.
1697  * It can be passed to g_hash_table_new() as the @key_equal_func
1698  * parameter, when using pointers to integers as keys in a #HashTable.
1699  *
1700  * Returns: %TRUE if the two keys match.
1701  */
1702 gboolean
1703 g_int_equal (gconstpointer v1,
1704              gconstpointer v2)
1705 {
1706   return *((const gint*) v1) == *((const gint*) v2);
1707 }
1708
1709 /**
1710  * g_int_hash:
1711  * @v: a pointer to a #gint key
1712  *
1713  * Converts a pointer to a #gint to a hash value.
1714  * It can be passed to g_hash_table_new() as the @hash_func parameter,
1715  * when using pointers to integers values as keys in a #GHashTable.
1716  *
1717  * Returns: a hash value corresponding to the key.
1718  */
1719 guint
1720 g_int_hash (gconstpointer v)
1721 {
1722   return *(const gint*) v;
1723 }
1724
1725 /**
1726  * g_int64_equal:
1727  * @v1: a pointer to a #gint64 key
1728  * @v2: a pointer to a #gint64 key to compare with @v1
1729  *
1730  * Compares the two #gint64 values being pointed to and returns
1731  * %TRUE if they are equal.
1732  * It can be passed to g_hash_table_new() as the @key_equal_func
1733  * parameter, when using pointers to 64-bit integers as keys in a #GHashTable.
1734  *
1735  * Returns: %TRUE if the two keys match.
1736  *
1737  * Since: 2.22
1738  */
1739 gboolean
1740 g_int64_equal (gconstpointer v1,
1741                gconstpointer v2)
1742 {
1743   return *((const gint64*) v1) == *((const gint64*) v2);
1744 }
1745
1746 /**
1747  * g_int64_hash:
1748  * @v: a pointer to a #gint64 key
1749  *
1750  * Converts a pointer to a #gint64 to a hash value.
1751  * It can be passed to g_hash_table_new() as the @hash_func parameter,
1752  * when using pointers to 64-bit integers values as keys in a #GHashTable.
1753  *
1754  * Returns: a hash value corresponding to the key.
1755  *
1756  * Since: 2.22
1757  */
1758 guint
1759 g_int64_hash (gconstpointer v)
1760 {
1761   return (guint) *(const gint64*) v;
1762 }
1763
1764 /**
1765  * g_double_equal:
1766  * @v1: a pointer to a #gdouble key
1767  * @v2: a pointer to a #gdouble key to compare with @v1
1768  *
1769  * Compares the two #gdouble values being pointed to and returns
1770  * %TRUE if they are equal.
1771  * It can be passed to g_hash_table_new() as the @key_equal_func
1772  * parameter, when using pointers to doubles as keys in a #GHashTable.
1773  *
1774  * Returns: %TRUE if the two keys match.
1775  *
1776  * Since: 2.22
1777  */
1778 gboolean
1779 g_double_equal (gconstpointer v1,
1780                 gconstpointer v2)
1781 {
1782   return *((const gdouble*) v1) == *((const gdouble*) v2);
1783 }
1784
1785 /**
1786  * g_double_hash:
1787  * @v: a pointer to a #gdouble key
1788  *
1789  * Converts a pointer to a #gdouble to a hash value.
1790  * It can be passed to g_hash_table_new() as the @hash_func parameter,
1791  * when using pointers to doubles as keys in a #GHashTable.
1792  *
1793  * Returns: a hash value corresponding to the key.
1794  *
1795  * Since: 2.22
1796  */
1797 guint
1798 g_double_hash (gconstpointer v)
1799 {
1800   return (guint) *(const gdouble*) v;
1801 }