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