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