Coverage Report

Created: 2026-09-13 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/glib/glib/ghash.c
Line
Count
Source
1
/* GLIB - Library of useful routines for C programming
2
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
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
#include "gmacros.h"
37
#include "glib-private.h"
38
#include "gstrfuncs.h"
39
#include "gatomic.h"
40
#include "gtestutils.h"
41
#include "gslice.h"
42
#include "grefcount.h"
43
#include "gvalgrind.h"
44
45
/* The following #pragma is here so we can do this...
46
 *
47
 *   #ifndef USE_SMALL_ARRAYS
48
 *     is_big = TRUE;
49
 *   #endif
50
 *     return is_big ? *(((gpointer *) a) + index) : GUINT_TO_POINTER (*(((guint *) a) + index));
51
 *
52
 * ...instead of this...
53
 *
54
 *   #ifndef USE_SMALL_ARRAYS
55
 *     return *(((gpointer *) a) + index);
56
 *   #else
57
 *     return is_big ? *(((gpointer *) a) + index) : GUINT_TO_POINTER (*(((guint *) a) + index));
58
 *   #endif
59
 *
60
 * ...and still compile successfully when -Werror=duplicated-branches is passed. */
61
62
#if defined(__GNUC__) && __GNUC__ > 6
63
#pragma GCC diagnostic ignored "-Wduplicated-branches"
64
#endif
65
66
/**
67
 * GHashTable:
68
 *
69
 * The #GHashTable struct is an opaque data structure to represent a
70
 * [Hash Table](data-structures.html#hash-tables). It should only be accessed via the
71
 * following functions.
72
 */
73
74
/**
75
 * GHashFunc:
76
 * @key: a key
77
 *
78
 * Specifies the type of the hash function which is passed to
79
 * g_hash_table_new() when a #GHashTable is created.
80
 *
81
 * The function is passed a key and should return a #guint hash value.
82
 * The functions g_direct_hash(), g_int_hash() and g_str_hash() provide
83
 * hash functions which can be used when the key is a #gpointer, #gint*,
84
 * and #gchar* respectively.
85
 *
86
 * g_direct_hash() is also the appropriate hash function for keys
87
 * of the form `GINT_TO_POINTER (n)` (or similar macros).
88
 *
89
 * A good hash functions should produce
90
 * hash values that are evenly distributed over a fairly large range.
91
 * The modulus is taken with the hash table size (a prime number) to
92
 * find the 'bucket' to place each key into. The function should also
93
 * be very fast, since it is called for each key lookup.
94
 *
95
 * Note that the hash functions provided by GLib have these qualities,
96
 * but are not particularly robust against manufactured keys that
97
 * cause hash collisions. Therefore, you should consider choosing
98
 * a more secure hash function when using a GHashTable with keys
99
 * that originate in untrusted data (such as HTTP requests).
100
 * Using g_str_hash() in that situation might make your application
101
 * vulnerable to
102
 * [Algorithmic Complexity Attacks](https://lwn.net/Articles/474912/).
103
 *
104
 * The key to choosing a good hash is unpredictability.  Even
105
 * cryptographic hashes are very easy to find collisions for when the
106
 * remainder is taken modulo a somewhat predictable prime number.  There
107
 * must be an element of randomness that an attacker is unable to guess.
108
 *
109
 * Returns: the hash value corresponding to the key
110
 */
111
112
/**
113
 * GHFunc:
114
 * @key: a key
115
 * @value: the value corresponding to the key
116
 * @user_data: user data passed to g_hash_table_foreach()
117
 *
118
 * Specifies the type of the function passed to g_hash_table_foreach().
119
 * It is called with each key/value pair, together with the @user_data
120
 * parameter which is passed to g_hash_table_foreach().
121
 */
122
123
/**
124
 * GHRFunc:
125
 * @key: a key
126
 * @value: the value associated with the key
127
 * @user_data: user data passed to the calling function
128
 *
129
 * Specifies the type of the function passed to
130
 * [func@GLib.HashTable.find], [func@GLib.HashTable.foreach_remove], and
131
 * [func@GLib.HashTable.foreach_steal].
132
 * 
133
 * The function is called with each key/value pair, together with
134
 * the @user_data parameter passed to the calling function.
135
 * 
136
 * The function should return true if the key/value pair should be
137
 * selected, meaning it has been found or it should be removed from the
138
 * [struct@GLib.HashTable], depending on the calling function.
139
 *
140
 * Returns: true if the key/value pair should be selected, and
141
 *   false otherwise
142
 */
143
144
/**
145
 * GEqualFunc:
146
 * @a: a value
147
 * @b: a value to compare with
148
 *
149
 * Specifies the type of a function used to test two values for
150
 * equality. The function should return %TRUE if both values are equal
151
 * and %FALSE otherwise.
152
 *
153
 * Returns: %TRUE if @a = @b; %FALSE otherwise
154
 */
155
156
/**
157
 * GHashTableIter:
158
 *
159
 * A GHashTableIter structure represents an iterator that can be used
160
 * to iterate over the elements of a #GHashTable. GHashTableIter
161
 * structures are typically allocated on the stack and then initialized
162
 * with g_hash_table_iter_init().
163
 *
164
 * The iteration order of a #GHashTableIter over the keys/values in a hash
165
 * table is not defined.
166
 */
167
168
/**
169
 * g_hash_table_freeze:
170
 * @hash_table: a #GHashTable
171
 *
172
 * This function is deprecated and will be removed in the next major
173
 * release of GLib. It does nothing.
174
 */
175
176
/**
177
 * g_hash_table_thaw:
178
 * @hash_table: a #GHashTable
179
 *
180
 * This function is deprecated and will be removed in the next major
181
 * release of GLib. It does nothing.
182
 */
183
184
625
#define HASH_TABLE_MIN_SHIFT 3  /* 1 << 3 == 8 buckets */
185
186
35.9M
#define UNUSED_HASH_VALUE 0
187
26.2M
#define TOMBSTONE_HASH_VALUE 1
188
35.4M
#define HASH_IS_UNUSED(h_) ((h_) == UNUSED_HASH_VALUE)
189
52.4M
#define HASH_IS_TOMBSTONE(h_) ((h_) == TOMBSTONE_HASH_VALUE)
190
9.97M
#define HASH_IS_REAL(h_) ((h_) >= 2)
191
192
/* If int is smaller than void * on our arch, we start out with
193
 * int-sized keys and values and resize to pointer-sized entries as
194
 * needed. This saves a good amount of memory when the HT is being
195
 * used with e.g. GUINT_TO_POINTER(). */
196
197
2.38k
#define BIG_ENTRY_SIZE (SIZEOF_VOID_P)
198
1.24k
#define SMALL_ENTRY_SIZE (SIZEOF_INT)
199
200
/* NB: The USE_SMALL_ARRAYS code assumes pointers are at most 8 bytes. */
201
#if SMALL_ENTRY_SIZE < BIG_ENTRY_SIZE && BIG_ENTRY_SIZE <= 8
202
# define USE_SMALL_ARRAYS
203
#endif
204
205
struct _GHashTable
206
{
207
  gsize            size;
208
  gint             mod;
209
  guint            mask;
210
  guint            nnodes;
211
  guint            noccupied;  /* nnodes + tombstones */
212
213
  guint            have_big_keys : 1;
214
  guint            have_big_values : 1;
215
216
  gpointer         keys;
217
  guint           *hashes;
218
  gpointer         values;
219
220
  GHashFunc        hash_func;
221
  GEqualFunc       key_equal_func;
222
  gatomicrefcount  ref_count;
223
#ifndef G_DISABLE_ASSERT
224
  /*
225
   * Tracks the structure of the hash table, not its contents: is only
226
   * incremented when a node is added or removed (is not incremented
227
   * when the key or data of a node is modified).
228
   */
229
  int              version;
230
#endif
231
  GDestroyNotify   key_destroy_func;
232
  GDestroyNotify   value_destroy_func;
233
};
234
235
typedef struct
236
{
237
  GHashTable  *hash_table;
238
  gpointer     dummy1;
239
  gpointer     dummy2;
240
  gint         position;
241
  gboolean     dummy3;
242
  gintptr      version;
243
} RealIter;
244
245
G_STATIC_ASSERT (sizeof (GHashTableIter) == sizeof (RealIter));
246
G_STATIC_ASSERT (G_ALIGNOF (GHashTableIter) >= G_ALIGNOF (RealIter));
247
248
/* Each table size has an associated prime modulo (the first prime
249
 * lower than the table size) used to find the initial bucket. Probing
250
 * then works modulo 2^n. The prime modulo is necessary to get a
251
 * good distribution with poor hash functions.
252
 */
253
static const gint prime_mod [] =
254
{
255
  1,          /* For 1 << 0 */
256
  2,
257
  3,
258
  7,
259
  13,
260
  31,
261
  61,
262
  127,
263
  251,
264
  509,
265
  1021,
266
  2039,
267
  4093,
268
  8191,
269
  16381,
270
  32749,
271
  65521,      /* For 1 << 16 */
272
  131071,
273
  262139,
274
  524287,
275
  1048573,
276
  2097143,
277
  4194301,
278
  8388593,
279
  16777213,
280
  33554393,
281
  67108859,
282
  134217689,
283
  268435399,
284
  536870909,
285
  1073741789,
286
  2147483647  /* For 1 << 31 */
287
};
288
289
static void
290
g_hash_table_set_shift (GHashTable *hash_table, gint shift)
291
783
{
292
783
  hash_table->size = 1 << shift;
293
783
  hash_table->mod  = prime_mod [shift];
294
295
  /* hash_table->size is always a power of two, so we can calculate the mask
296
   * by simply subtracting 1 from it. The leading assertion ensures that
297
   * we're really dealing with a power of two. */
298
299
783
  g_assert ((hash_table->size & (hash_table->size - 1)) == 0);
300
783
  hash_table->mask = hash_table->size - 1;
301
783
}
302
303
static gint
304
g_hash_table_find_closest_shift (gint n)
305
552
{
306
552
  gint i;
307
308
2.95k
  for (i = 0; n; i++)
309
2.39k
    n >>= 1;
310
311
552
  return i;
312
552
}
313
314
static void
315
g_hash_table_set_shift_from_size (GHashTable *hash_table, gint size)
316
552
{
317
552
  gint shift;
318
319
552
  shift = g_hash_table_find_closest_shift (size);
320
552
  shift = MAX (shift, HASH_TABLE_MIN_SHIFT);
321
322
552
  g_hash_table_set_shift (hash_table, shift);
323
552
}
324
325
static inline gpointer
326
g_hash_table_realloc_key_or_value_array (gpointer a, guint size, G_GNUC_UNUSED gboolean is_big)
327
1.29k
{
328
1.29k
#ifdef USE_SMALL_ARRAYS
329
1.29k
  return g_realloc (a, size * (is_big ? BIG_ENTRY_SIZE : SMALL_ENTRY_SIZE));
330
#else
331
  return g_renew (gpointer, a, size);
332
#endif
333
1.29k
}
334
335
static inline gpointer
336
g_hash_table_fetch_key_or_value (gpointer a, guint index, gboolean is_big)
337
17.0M
{
338
#ifndef USE_SMALL_ARRAYS
339
  is_big = TRUE;
340
#endif
341
17.0M
  return is_big ? *(((gpointer *) a) + index) : GUINT_TO_POINTER (*(((guint *) a) + index));
342
17.0M
}
343
344
static inline void
345
g_hash_table_assign_key_or_value (gpointer a, guint index, gboolean is_big, gpointer v)
346
1.52M
{
347
#ifndef USE_SMALL_ARRAYS
348
  is_big = TRUE;
349
#endif
350
1.52M
  if (is_big)
351
1.52M
    *(((gpointer *) a) + index) = v;
352
1.11k
  else
353
1.11k
    *(((guint *) a) + index) = GPOINTER_TO_UINT (v);
354
1.52M
}
355
356
static inline gpointer
357
g_hash_table_evict_key_or_value (gpointer a, guint index, gboolean is_big, gpointer v)
358
599k
{
359
#ifndef USE_SMALL_ARRAYS
360
  is_big = TRUE;
361
#endif
362
599k
  if (is_big)
363
598k
    {
364
598k
      gpointer r = *(((gpointer *) a) + index);
365
598k
      *(((gpointer *) a) + index) = v;
366
598k
      return r;
367
598k
    }
368
744
  else
369
744
    {
370
744
      gpointer r = GUINT_TO_POINTER (*(((guint *) a) + index));
371
744
      *(((guint *) a) + index) = GPOINTER_TO_UINT (v);
372
744
      return r;
373
744
    }
374
599k
}
375
376
static inline guint
377
g_hash_table_hash_to_index (GHashTable *hash_table, guint hash)
378
9.44M
{
379
  /* Multiply the hash by a small prime before applying the modulo. This
380
   * prevents the table from becoming densely packed, even with a poor hash
381
   * function. A densely packed table would have poor performance on
382
   * workloads with many failed lookups or a high degree of churn. */
383
9.44M
  return (hash * 11) % hash_table->mod;
384
9.44M
}
385
386
/*
387
 * g_hash_table_lookup_node:
388
 * @hash_table: our #GHashTable
389
 * @key: the key to look up against
390
 * @hash_return: key hash return location
391
 *
392
 * Performs a lookup in the hash table, preserving extra information
393
 * usually needed for insertion.
394
 *
395
 * This function first computes the hash value of the key using the
396
 * user's hash function.
397
 *
398
 * If an entry in the table matching @key is found then this function
399
 * returns the index of that entry in the table, and if not, the
400
 * index of an unused node (empty or tombstone) where the key can be
401
 * inserted.
402
 *
403
 * The computed hash value is returned in the variable pointed to
404
 * by @hash_return. This is to save insertions from having to compute
405
 * the hash record again for the new record.
406
 *
407
 * Returns: index of the described node
408
 */
409
static inline guint
410
g_hash_table_lookup_node (GHashTable    *hash_table,
411
                          gconstpointer  key,
412
                          guint         *hash_return)
413
8.96M
{
414
8.96M
  guint node_index;
415
8.96M
  guint node_hash;
416
8.96M
  guint hash_value;
417
8.96M
  guint first_tombstone = 0;
418
8.96M
  gboolean have_tombstone = FALSE;
419
8.96M
  guint step = 0;
420
421
8.96M
  hash_value = hash_table->hash_func (key);
422
8.96M
  if (G_UNLIKELY (!HASH_IS_REAL (hash_value)))
423
76
    hash_value = 2;
424
425
8.96M
  *hash_return = hash_value;
426
427
8.96M
  node_index = g_hash_table_hash_to_index (hash_table, hash_value);
428
8.96M
  node_hash = hash_table->hashes[node_index];
429
430
35.1M
  while (!HASH_IS_UNUSED (node_hash))
431
34.6M
    {
432
      /* We first check if our full hash values
433
       * are equal so we can avoid calling the full-blown
434
       * key equality function in most cases.
435
       */
436
34.6M
      if (node_hash == hash_value)
437
8.43M
        {
438
8.43M
          gpointer node_key = g_hash_table_fetch_key_or_value (hash_table->keys, node_index, hash_table->have_big_keys);
439
440
8.43M
          if (hash_table->key_equal_func)
441
831k
            {
442
831k
              if (hash_table->key_equal_func (node_key, key))
443
830k
                return node_index;
444
831k
            }
445
7.60M
          else if (node_key == key)
446
7.60M
            {
447
7.60M
              return node_index;
448
7.60M
            }
449
8.43M
        }
450
26.2M
      else if (HASH_IS_TOMBSTONE (node_hash) && !have_tombstone)
451
667
        {
452
667
          first_tombstone = node_index;
453
667
          have_tombstone = TRUE;
454
667
        }
455
456
26.2M
      step++;
457
26.2M
      node_index += step;
458
26.2M
      node_index &= hash_table->mask;
459
26.2M
      node_hash = hash_table->hashes[node_index];
460
26.2M
    }
461
462
526k
  if (have_tombstone)
463
667
    return first_tombstone;
464
465
525k
  return node_index;
466
526k
}
467
468
/*
469
 * g_hash_table_remove_node:
470
 * @hash_table: our #GHashTable
471
 * @node: pointer to node to remove
472
 * @notify: %TRUE if the destroy notify handlers are to be called
473
 *
474
 * Removes a node from the hash table and updates the node count.
475
 * The node is replaced by a tombstone. No table resize is performed.
476
 *
477
 * If @notify is %TRUE then the destroy notify functions are called
478
 * for the key and value of the hash node.
479
 */
480
static void
481
g_hash_table_remove_node (GHashTable   *hash_table,
482
                          gint          i,
483
                          gboolean      notify)
484
450
{
485
450
  gpointer key;
486
450
  gpointer value;
487
488
450
  key = g_hash_table_fetch_key_or_value (hash_table->keys, i, hash_table->have_big_keys);
489
450
  value = g_hash_table_fetch_key_or_value (hash_table->values, i, hash_table->have_big_values);
490
491
  /* Erect tombstone */
492
450
  hash_table->hashes[i] = TOMBSTONE_HASH_VALUE;
493
494
  /* Be GC friendly */
495
450
  g_hash_table_assign_key_or_value (hash_table->keys, i, hash_table->have_big_keys, NULL);
496
450
  g_hash_table_assign_key_or_value (hash_table->values, i, hash_table->have_big_values, NULL);
497
498
450
  g_assert (hash_table->nnodes > 0);
499
450
  hash_table->nnodes--;
500
501
450
  if (notify && hash_table->key_destroy_func)
502
0
    hash_table->key_destroy_func (key);
503
504
450
  if (notify && hash_table->value_destroy_func)
505
0
    hash_table->value_destroy_func (value);
506
507
450
}
508
509
/*
510
 * g_hash_table_setup_storage:
511
 * @hash_table: our #GHashTable
512
 *
513
 * Initialise the hash table size, mask, mod, and arrays.
514
 */
515
static void
516
g_hash_table_setup_storage (GHashTable *hash_table)
517
231
{
518
231
  gboolean small = FALSE;
519
520
  /* We want to use small arrays only if:
521
   *   - we are running on a system where that makes sense (64 bit); and
522
   *   - we are not running under valgrind.
523
   */
524
525
231
#ifdef USE_SMALL_ARRAYS
526
231
  small = TRUE;
527
528
231
# ifdef ENABLE_VALGRIND
529
231
  if (RUNNING_ON_VALGRIND)
530
0
    small = FALSE;
531
231
# endif
532
231
#endif
533
534
231
  g_hash_table_set_shift (hash_table, HASH_TABLE_MIN_SHIFT);
535
536
231
  hash_table->have_big_keys = !small;
537
231
  hash_table->have_big_values = !small;
538
539
231
  hash_table->keys   = g_hash_table_realloc_key_or_value_array (NULL, hash_table->size, hash_table->have_big_keys);
540
231
  hash_table->values = hash_table->keys;
541
231
  hash_table->hashes = g_new0 (guint, hash_table->size);
542
231
}
543
544
/*
545
 * g_hash_table_remove_all_nodes:
546
 * @hash_table: our #GHashTable
547
 * @notify: %TRUE if the destroy notify handlers are to be called
548
 *
549
 * Removes all nodes from the table.
550
 *
551
 * If @notify is %TRUE then the destroy notify functions are called
552
 * for the key and value of the hash node.
553
 *
554
 * Since this may be a precursor to freeing the table entirely, we'd
555
 * ideally perform no resize, and we can indeed avoid that in some
556
 * cases.  However: in the case that we'll be making callbacks to user
557
 * code (via destroy notifies) we need to consider that the user code
558
 * might call back into the table again.  In this case, we setup a new
559
 * set of arrays so that any callers will see an empty (but valid)
560
 * table.
561
 */
562
static void
563
g_hash_table_remove_all_nodes (GHashTable *hash_table,
564
                               gboolean    notify,
565
                               gboolean    destruction)
566
364
{
567
364
  int i;
568
364
  gpointer key;
569
364
  gpointer value;
570
364
  gint old_size;
571
364
  gpointer *old_keys;
572
364
  gpointer *old_values;
573
364
  guint    *old_hashes;
574
364
  gboolean  old_have_big_keys;
575
364
  gboolean  old_have_big_values;
576
577
  /* If the hash table is already empty, there is nothing to be done. */
578
364
  if (hash_table->nnodes == 0)
579
183
    return;
580
581
181
  hash_table->nnodes = 0;
582
181
  hash_table->noccupied = 0;
583
584
  /* Easy case: no callbacks, so we just zero out the arrays */
585
181
  if (!notify ||
586
181
      (hash_table->key_destroy_func == NULL &&
587
181
       hash_table->value_destroy_func == NULL))
588
181
    {
589
181
      if (!destruction)
590
181
        {
591
181
          memset (hash_table->hashes, 0, hash_table->size * sizeof (guint));
592
593
181
#ifdef USE_SMALL_ARRAYS
594
181
          memset (hash_table->keys, 0, hash_table->size * (hash_table->have_big_keys ? BIG_ENTRY_SIZE : SMALL_ENTRY_SIZE));
595
181
          memset (hash_table->values, 0, hash_table->size * (hash_table->have_big_values ? BIG_ENTRY_SIZE : SMALL_ENTRY_SIZE));
596
#else
597
          memset (hash_table->keys, 0, hash_table->size * sizeof (gpointer));
598
          memset (hash_table->values, 0, hash_table->size * sizeof (gpointer));
599
#endif
600
181
        }
601
602
181
      return;
603
181
    }
604
605
  /* Hard case: we need to do user callbacks.  There are two
606
   * possibilities here:
607
   *
608
   *   1) there are no outstanding references on the table and therefore
609
   *   nobody should be calling into it again (destroying == true)
610
   *
611
   *   2) there are outstanding references, and there may be future
612
   *   calls into the table, either after we return, or from the destroy
613
   *   notifies that we're about to do (destroying == false)
614
   *
615
   * We handle both cases by taking the current state of the table into
616
   * local variables and replacing it with something else: in the "no
617
   * outstanding references" cases we replace it with a bunch of
618
   * null/zero values so that any access to the table will fail.  In the
619
   * "may receive future calls" case, we reinitialise the struct to
620
   * appear like a newly-created empty table.
621
   *
622
   * In both cases, we take over the references for the current state,
623
   * freeing them below.
624
   */
625
0
  old_size = hash_table->size;
626
0
  old_have_big_keys = hash_table->have_big_keys;
627
0
  old_have_big_values = hash_table->have_big_values;
628
0
  old_keys   = g_steal_pointer (&hash_table->keys);
629
0
  old_values = g_steal_pointer (&hash_table->values);
630
0
  old_hashes = g_steal_pointer (&hash_table->hashes);
631
632
0
  if (!destruction)
633
    /* Any accesses will see an empty table */
634
0
    g_hash_table_setup_storage (hash_table);
635
0
  else
636
    /* Will cause a quick crash on any attempted access */
637
0
    hash_table->size = hash_table->mod = hash_table->mask = 0;
638
639
  /* Now do the actual destroy notifies */
640
0
  for (i = 0; i < old_size; i++)
641
0
    {
642
0
      if (HASH_IS_REAL (old_hashes[i]))
643
0
        {
644
0
          key = g_hash_table_fetch_key_or_value (old_keys, i, old_have_big_keys);
645
0
          value = g_hash_table_fetch_key_or_value (old_values, i, old_have_big_values);
646
647
0
          old_hashes[i] = UNUSED_HASH_VALUE;
648
649
0
          g_hash_table_assign_key_or_value (old_keys, i, old_have_big_keys, NULL);
650
0
          g_hash_table_assign_key_or_value (old_values, i, old_have_big_values, NULL);
651
652
0
          if (hash_table->key_destroy_func != NULL)
653
0
            hash_table->key_destroy_func (key);
654
655
0
          if (hash_table->value_destroy_func != NULL)
656
0
            hash_table->value_destroy_func (value);
657
0
        }
658
0
    }
659
660
  /* Destroy old storage space. */
661
0
  if (old_keys != old_values)
662
0
    g_free (old_values);
663
664
0
  g_free (old_keys);
665
0
  g_free (old_hashes);
666
0
}
667
668
static void
669
realloc_arrays (GHashTable *hash_table, gboolean is_a_set)
670
552
{
671
552
  hash_table->hashes = g_renew (guint, hash_table->hashes, hash_table->size);
672
552
  hash_table->keys = g_hash_table_realloc_key_or_value_array (hash_table->keys, hash_table->size, hash_table->have_big_keys);
673
674
552
  if (is_a_set)
675
44
    hash_table->values = hash_table->keys;
676
508
  else
677
508
    hash_table->values = g_hash_table_realloc_key_or_value_array (hash_table->values, hash_table->size, hash_table->have_big_values);
678
552
}
679
680
/* When resizing the table in place, we use a temporary bit array to keep
681
 * track of which entries have been assigned a proper location in the new
682
 * table layout.
683
 *
684
 * Each bit corresponds to a bucket. A bit is set if an entry was assigned
685
 * its corresponding location during the resize and thus should not be
686
 * evicted. The array starts out cleared to zero. */
687
688
static inline gboolean
689
get_status_bit (const guint32 *bitmap, guint index)
690
1.02M
{
691
1.02M
  return (bitmap[index / 32] >> (index % 32)) & 1;
692
1.02M
}
693
694
static inline void
695
set_status_bit (guint32 *bitmap, guint index)
696
484k
{
697
484k
  bitmap[index / 32] |= 1U << (index % 32);
698
484k
}
699
700
/* By calling dedicated resize functions for sets and maps, we avoid 2x
701
 * test-and-branch per key in the inner loop. This yields a small
702
 * performance improvement at the cost of a bit of macro gunk. */
703
704
#define DEFINE_RESIZE_FUNC(fname) \
705
552
static void fname (GHashTable *hash_table, guint old_size, guint32 *reallocated_buckets_bitmap) \
706
552
{                                                                       \
707
552
  guint i;                                                              \
708
552
                                                                        \
709
522k
  for (i = 0; i < old_size; i++)                                        \
710
521k
    {                                                                   \
711
521k
      guint node_hash = hash_table->hashes[i];                          \
712
521k
      gpointer key, value G_GNUC_UNUSED;                                \
713
521k
                                                                        \
714
521k
      if (!HASH_IS_REAL (node_hash))                                    \
715
521k
        {                                                               \
716
25.6k
          /* Clear tombstones */                                        \
717
25.6k
          hash_table->hashes[i] = UNUSED_HASH_VALUE;                    \
718
25.6k
          continue;                                                     \
719
25.6k
        }                                                               \
720
521k
                                                                        \
721
521k
      /* Skip entries relocated through eviction */                     \
722
521k
      if (get_status_bit (reallocated_buckets_bitmap, i))               \
723
496k
        continue;                                                       \
724
496k
                                                                        \
725
496k
      hash_table->hashes[i] = UNUSED_HASH_VALUE;                        \
726
378k
      EVICT_KEYVAL (hash_table, i, NULL, NULL, key, value);             \
727
378k
                                                                        \
728
378k
      for (;;)                                                          \
729
484k
        {                                                               \
730
484k
          guint hash_val;                                               \
731
484k
          guint replaced_hash;                                          \
732
484k
          guint step = 0;                                               \
733
484k
                                                                        \
734
484k
          hash_val = g_hash_table_hash_to_index (hash_table, node_hash); \
735
484k
                                                                        \
736
533k
          while (get_status_bit (reallocated_buckets_bitmap, hash_val)) \
737
484k
            {                                                           \
738
48.6k
              step++;                                                   \
739
48.6k
              hash_val += step;                                         \
740
48.6k
              hash_val &= hash_table->mask;                             \
741
48.6k
            }                                                           \
742
484k
                                                                        \
743
484k
          set_status_bit (reallocated_buckets_bitmap, hash_val);        \
744
484k
                                                                        \
745
484k
          replaced_hash = hash_table->hashes[hash_val];                 \
746
484k
          hash_table->hashes[hash_val] = node_hash;                     \
747
484k
          if (!HASH_IS_REAL (replaced_hash))                            \
748
484k
            {                                                           \
749
378k
              ASSIGN_KEYVAL (hash_table, hash_val, key, value);         \
750
378k
              break;                                                    \
751
378k
            }                                                           \
752
484k
                                                                        \
753
484k
          node_hash = replaced_hash;                                    \
754
106k
          EVICT_KEYVAL (hash_table, hash_val, key, value, key, value);  \
755
106k
        }                                                               \
756
378k
    }                                                                   \
757
552
}
ghash.c:resize_set
Line
Count
Source
705
44
static void fname (GHashTable *hash_table, guint old_size, guint32 *reallocated_buckets_bitmap) \
706
44
{                                                                       \
707
44
  guint i;                                                              \
708
44
                                                                        \
709
393k
  for (i = 0; i < old_size; i++)                                        \
710
393k
    {                                                                   \
711
393k
      guint node_hash = hash_table->hashes[i];                          \
712
393k
      gpointer key, value G_GNUC_UNUSED;                                \
713
393k
                                                                        \
714
393k
      if (!HASH_IS_REAL (node_hash))                                    \
715
393k
        {                                                               \
716
13.8k
          /* Clear tombstones */                                        \
717
13.8k
          hash_table->hashes[i] = UNUSED_HASH_VALUE;                    \
718
13.8k
          continue;                                                     \
719
13.8k
        }                                                               \
720
393k
                                                                        \
721
393k
      /* Skip entries relocated through eviction */                     \
722
393k
      if (get_status_bit (reallocated_buckets_bitmap, i))               \
723
379k
        continue;                                                       \
724
379k
                                                                        \
725
379k
      hash_table->hashes[i] = UNUSED_HASH_VALUE;                        \
726
297k
      EVICT_KEYVAL (hash_table, i, NULL, NULL, key, value);             \
727
297k
                                                                        \
728
297k
      for (;;)                                                          \
729
369k
        {                                                               \
730
369k
          guint hash_val;                                               \
731
369k
          guint replaced_hash;                                          \
732
369k
          guint step = 0;                                               \
733
369k
                                                                        \
734
369k
          hash_val = g_hash_table_hash_to_index (hash_table, node_hash); \
735
369k
                                                                        \
736
369k
          while (get_status_bit (reallocated_buckets_bitmap, hash_val)) \
737
369k
            {                                                           \
738
70
              step++;                                                   \
739
70
              hash_val += step;                                         \
740
70
              hash_val &= hash_table->mask;                             \
741
70
            }                                                           \
742
369k
                                                                        \
743
369k
          set_status_bit (reallocated_buckets_bitmap, hash_val);        \
744
369k
                                                                        \
745
369k
          replaced_hash = hash_table->hashes[hash_val];                 \
746
369k
          hash_table->hashes[hash_val] = node_hash;                     \
747
369k
          if (!HASH_IS_REAL (replaced_hash))                            \
748
369k
            {                                                           \
749
297k
              ASSIGN_KEYVAL (hash_table, hash_val, key, value);         \
750
297k
              break;                                                    \
751
297k
            }                                                           \
752
369k
                                                                        \
753
369k
          node_hash = replaced_hash;                                    \
754
72.7k
          EVICT_KEYVAL (hash_table, hash_val, key, value, key, value);  \
755
72.7k
        }                                                               \
756
297k
    }                                                                   \
757
44
}
ghash.c:resize_map
Line
Count
Source
705
508
static void fname (GHashTable *hash_table, guint old_size, guint32 *reallocated_buckets_bitmap) \
706
508
{                                                                       \
707
508
  guint i;                                                              \
708
508
                                                                        \
709
129k
  for (i = 0; i < old_size; i++)                                        \
710
128k
    {                                                                   \
711
128k
      guint node_hash = hash_table->hashes[i];                          \
712
128k
      gpointer key, value G_GNUC_UNUSED;                                \
713
128k
                                                                        \
714
128k
      if (!HASH_IS_REAL (node_hash))                                    \
715
128k
        {                                                               \
716
11.8k
          /* Clear tombstones */                                        \
717
11.8k
          hash_table->hashes[i] = UNUSED_HASH_VALUE;                    \
718
11.8k
          continue;                                                     \
719
11.8k
        }                                                               \
720
128k
                                                                        \
721
128k
      /* Skip entries relocated through eviction */                     \
722
128k
      if (get_status_bit (reallocated_buckets_bitmap, i))               \
723
116k
        continue;                                                       \
724
116k
                                                                        \
725
116k
      hash_table->hashes[i] = UNUSED_HASH_VALUE;                        \
726
81.5k
      EVICT_KEYVAL (hash_table, i, NULL, NULL, key, value);             \
727
81.5k
                                                                        \
728
81.5k
      for (;;)                                                          \
729
114k
        {                                                               \
730
114k
          guint hash_val;                                               \
731
114k
          guint replaced_hash;                                          \
732
114k
          guint step = 0;                                               \
733
114k
                                                                        \
734
114k
          hash_val = g_hash_table_hash_to_index (hash_table, node_hash); \
735
114k
                                                                        \
736
163k
          while (get_status_bit (reallocated_buckets_bitmap, hash_val)) \
737
114k
            {                                                           \
738
48.6k
              step++;                                                   \
739
48.6k
              hash_val += step;                                         \
740
48.6k
              hash_val &= hash_table->mask;                             \
741
48.6k
            }                                                           \
742
114k
                                                                        \
743
114k
          set_status_bit (reallocated_buckets_bitmap, hash_val);        \
744
114k
                                                                        \
745
114k
          replaced_hash = hash_table->hashes[hash_val];                 \
746
114k
          hash_table->hashes[hash_val] = node_hash;                     \
747
114k
          if (!HASH_IS_REAL (replaced_hash))                            \
748
114k
            {                                                           \
749
81.5k
              ASSIGN_KEYVAL (hash_table, hash_val, key, value);         \
750
81.5k
              break;                                                    \
751
81.5k
            }                                                           \
752
114k
                                                                        \
753
114k
          node_hash = replaced_hash;                                    \
754
33.3k
          EVICT_KEYVAL (hash_table, hash_val, key, value, key, value);  \
755
33.3k
        }                                                               \
756
81.5k
    }                                                                   \
757
508
}
758
759
81.5k
#define ASSIGN_KEYVAL(ht, index, key, value) G_STMT_START{ \
760
81.5k
    g_hash_table_assign_key_or_value ((ht)->keys, (index), (ht)->have_big_keys, (key)); \
761
81.5k
    g_hash_table_assign_key_or_value ((ht)->values, (index), (ht)->have_big_values, (value)); \
762
81.5k
  }G_STMT_END
763
764
114k
#define EVICT_KEYVAL(ht, index, key, value, outkey, outvalue) G_STMT_START{ \
765
114k
    (outkey) = g_hash_table_evict_key_or_value ((ht)->keys, (index), (ht)->have_big_keys, (key)); \
766
114k
    (outvalue) = g_hash_table_evict_key_or_value ((ht)->values, (index), (ht)->have_big_values, (value)); \
767
114k
  }G_STMT_END
768
769
DEFINE_RESIZE_FUNC (resize_map)
770
771
#undef ASSIGN_KEYVAL
772
#undef EVICT_KEYVAL
773
774
297k
#define ASSIGN_KEYVAL(ht, index, key, value) G_STMT_START{ \
775
297k
    g_hash_table_assign_key_or_value ((ht)->keys, (index), (ht)->have_big_keys, (key)); \
776
297k
  }G_STMT_END
777
778
369k
#define EVICT_KEYVAL(ht, index, key, value, outkey, outvalue) G_STMT_START{ \
779
369k
    (outkey) = g_hash_table_evict_key_or_value ((ht)->keys, (index), (ht)->have_big_keys, (key)); \
780
369k
  }G_STMT_END
781
782
DEFINE_RESIZE_FUNC (resize_set)
783
784
#undef ASSIGN_KEYVAL
785
#undef EVICT_KEYVAL
786
787
/*
788
 * g_hash_table_resize:
789
 * @hash_table: our #GHashTable
790
 *
791
 * Resizes the hash table to the optimal size based on the number of
792
 * nodes currently held. If you call this function then a resize will
793
 * occur, even if one does not need to occur.
794
 * Use g_hash_table_maybe_resize() instead.
795
 *
796
 * This function may "resize" the hash table to its current size, with
797
 * the side effect of cleaning up tombstones and otherwise optimizing
798
 * the probe sequences.
799
 */
800
static void
801
g_hash_table_resize (GHashTable *hash_table)
802
552
{
803
552
  guint32 *reallocated_buckets_bitmap;
804
552
  gsize old_size;
805
552
  gboolean is_a_set;
806
807
552
  old_size = hash_table->size;
808
552
  is_a_set = hash_table->keys == hash_table->values;
809
810
  /* The outer checks in g_hash_table_maybe_resize() will only consider
811
   * cleanup/resize when the load factor goes below .25 (1/4, ignoring
812
   * tombstones) or above .9375 (15/16, including tombstones).
813
   *
814
   * Once this happens, tombstones will always be cleaned out. If our
815
   * load sans tombstones is greater than .75 (1/1.333, see below), we'll
816
   * take this opportunity to grow the table too.
817
   *
818
   * Immediately after growing, the load factor will be in the range
819
   * .375 .. .469. After shrinking, it will be exactly .5. */
820
821
552
  g_hash_table_set_shift_from_size (hash_table, (gint) (hash_table->nnodes * 1.333));
822
823
552
  if (hash_table->size > old_size)
824
412
    {
825
412
      realloc_arrays (hash_table, is_a_set);
826
412
      memset (&hash_table->hashes[old_size], 0, (hash_table->size - old_size) * sizeof (guint));
827
828
412
      reallocated_buckets_bitmap = g_new0 (guint32, (hash_table->size + 31) / 32);
829
412
    }
830
140
  else
831
140
    {
832
140
      reallocated_buckets_bitmap = g_new0 (guint32, (old_size + 31) / 32);
833
140
    }
834
835
552
  if (is_a_set)
836
44
    resize_set (hash_table, old_size, reallocated_buckets_bitmap);
837
508
  else
838
508
    resize_map (hash_table, old_size, reallocated_buckets_bitmap);
839
840
552
  g_free (reallocated_buckets_bitmap);
841
842
552
  if (hash_table->size < old_size)
843
140
    realloc_arrays (hash_table, is_a_set);
844
845
552
  hash_table->noccupied = hash_table->nnodes;
846
552
}
847
848
/*
849
 * g_hash_table_maybe_resize:
850
 * @hash_table: our #GHashTable
851
 *
852
 * Resizes the hash table, if needed.
853
 *
854
 * Essentially, calls g_hash_table_resize() if the table has strayed
855
 * too far from its ideal size for its number of nodes.
856
 */
857
static inline void
858
g_hash_table_maybe_resize (GHashTable *hash_table)
859
312k
{
860
312k
  gsize noccupied = hash_table->noccupied;
861
312k
  gsize size = hash_table->size;
862
863
312k
  if ((size > hash_table->nnodes * 4 && size > 1 << HASH_TABLE_MIN_SHIFT) ||
864
311k
      (size <= noccupied + (noccupied / 16)))
865
552
    g_hash_table_resize (hash_table);
866
312k
}
867
868
#ifdef USE_SMALL_ARRAYS
869
870
static inline gboolean
871
entry_is_big (gpointer v)
872
987
{
873
987
  return (((guintptr) v) >> ((BIG_ENTRY_SIZE - SMALL_ENTRY_SIZE) * 8)) != 0;
874
987
}
875
876
static inline gboolean
877
g_hash_table_maybe_make_big_keys_or_values (gpointer *a_p, gpointer v, gint ht_size)
878
987
{
879
987
  if (entry_is_big (v))
880
407
    {
881
407
      guint *a = (guint *) *a_p;
882
407
      gpointer *a_new;
883
407
      gint i;
884
885
407
      a_new = g_new (gpointer, ht_size);
886
887
3.66k
      for (i = 0; i < ht_size; i++)
888
3.25k
        {
889
3.25k
          a_new[i] = GUINT_TO_POINTER (a[i]);
890
3.25k
        }
891
892
407
      g_free (a);
893
407
      *a_p = a_new;
894
407
      return TRUE;
895
407
    }
896
897
580
  return FALSE;
898
987
}
899
900
#endif
901
902
static inline void
903
g_hash_table_ensure_keyval_fits (GHashTable *hash_table, gpointer key, gpointer value)
904
534k
{
905
534k
  gboolean is_a_set = (hash_table->keys == hash_table->values);
906
907
534k
#ifdef USE_SMALL_ARRAYS
908
909
  /* Convert from set to map? */
910
534k
  if (is_a_set)
911
229k
    {
912
229k
      if (hash_table->have_big_keys)
913
229k
        {
914
229k
          if (key != value)
915
0
            hash_table->values = g_memdup2 (hash_table->keys, sizeof (gpointer) * hash_table->size);
916
          /* Keys and values are both big now, so no need for further checks */
917
229k
          return;
918
229k
        }
919
212
      else
920
212
        {
921
212
          if (key != value)
922
206
            {
923
206
              hash_table->values = g_memdup2 (hash_table->keys, sizeof (guint) * hash_table->size);
924
206
              is_a_set = FALSE;
925
206
            }
926
212
        }
927
229k
    }
928
929
  /* Make keys big? */
930
304k
  if (!hash_table->have_big_keys)
931
215
    {
932
215
      hash_table->have_big_keys = g_hash_table_maybe_make_big_keys_or_values (&hash_table->keys, key, hash_table->size);
933
934
215
      if (is_a_set)
935
6
        {
936
6
          hash_table->values = hash_table->keys;
937
6
          hash_table->have_big_values = hash_table->have_big_keys;
938
6
        }
939
215
    }
940
941
  /* Make values big? */
942
304k
  if (!is_a_set && !hash_table->have_big_values)
943
772
    {
944
772
      hash_table->have_big_values = g_hash_table_maybe_make_big_keys_or_values (&hash_table->values, value, hash_table->size);
945
772
    }
946
947
#else
948
949
  /* Just split if necessary */
950
  if (is_a_set && key != value)
951
    hash_table->values = g_memdup2 (hash_table->keys, sizeof (gpointer) * hash_table->size);
952
953
#endif
954
304k
}
955
956
/**
957
 * g_hash_table_new:
958
 * @hash_func: a function to create a hash value from a key
959
 * @key_equal_func: a function to check two keys for equality
960
 *
961
 * Creates a new #GHashTable with a reference count of 1.
962
 *
963
 * Hash values returned by @hash_func are used to determine where keys
964
 * are stored within the #GHashTable data structure. The g_direct_hash(),
965
 * g_int_hash(), g_int64_hash(), g_double_hash() and g_str_hash()
966
 * functions are provided for some common types of keys.
967
 * If @hash_func is %NULL, g_direct_hash() is used.
968
 *
969
 * @key_equal_func is used when looking up keys in the #GHashTable.
970
 * The g_direct_equal(), g_int_equal(), g_int64_equal(), g_double_equal()
971
 * and g_str_equal() functions are provided for the most common types
972
 * of keys. If @key_equal_func is %NULL, keys are compared directly in
973
 * a similar fashion to g_direct_equal(), but without the overhead of
974
 * a function call. @key_equal_func is called with the key from the hash table
975
 * as its first parameter, and the user-provided key to check against as
976
 * its second.
977
 *
978
 * Returns: (transfer full): a new #GHashTable
979
 */
980
GHashTable *
981
g_hash_table_new (GHashFunc  hash_func,
982
                  GEqualFunc key_equal_func)
983
224
{
984
224
  return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
985
224
}
986
987
988
/**
989
 * g_hash_table_new_full:
990
 * @hash_func: a function to create a hash value from a key
991
 * @key_equal_func: a function to check two keys for equality
992
 * @key_destroy_func: (nullable): a function to free the memory allocated for the key
993
 *     used when removing the entry from the #GHashTable, or %NULL
994
 *     if you don't want to supply such a function.
995
 * @value_destroy_func: (nullable): a function to free the memory allocated for the
996
 *     value used when removing the entry from the #GHashTable, or %NULL
997
 *     if you don't want to supply such a function.
998
 *
999
 * Creates a new #GHashTable like g_hash_table_new() with a reference
1000
 * count of 1 and allows to specify functions to free the memory
1001
 * allocated for the key and value that get called when removing the
1002
 * entry from the #GHashTable.
1003
 *
1004
 * Since version 2.42 it is permissible for destroy notify functions to
1005
 * recursively remove further items from the hash table. This is only
1006
 * permissible if the application still holds a reference to the hash table.
1007
 * This means that you may need to ensure that the hash table is empty by
1008
 * calling g_hash_table_remove_all() before releasing the last reference using
1009
 * g_hash_table_unref().
1010
 *
1011
 * Returns: (transfer full): a new #GHashTable
1012
 */
1013
GHashTable *
1014
g_hash_table_new_full (GHashFunc      hash_func,
1015
                       GEqualFunc     key_equal_func,
1016
                       GDestroyNotify key_destroy_func,
1017
                       GDestroyNotify value_destroy_func)
1018
231
{
1019
231
  GHashTable *hash_table;
1020
1021
231
  hash_table = g_slice_new (GHashTable);
1022
231
  g_atomic_ref_count_init (&hash_table->ref_count);
1023
231
  hash_table->nnodes             = 0;
1024
231
  hash_table->noccupied          = 0;
1025
231
  hash_table->hash_func          = hash_func ? hash_func : g_direct_hash;
1026
231
  hash_table->key_equal_func     = key_equal_func;
1027
231
#ifndef G_DISABLE_ASSERT
1028
231
  hash_table->version            = 0;
1029
231
#endif
1030
231
  hash_table->key_destroy_func   = key_destroy_func;
1031
231
  hash_table->value_destroy_func = value_destroy_func;
1032
1033
231
  g_hash_table_setup_storage (hash_table);
1034
1035
231
  return hash_table;
1036
231
}
1037
1038
/**
1039
 * g_hash_table_new_similar:
1040
 * @other_hash_table: (not nullable) (transfer none): Another #GHashTable
1041
 *
1042
 * Creates a new #GHashTable like g_hash_table_new_full() with a reference
1043
 * count of 1.
1044
 *
1045
 * It inherits the hash function, the key equal function, the key destroy function,
1046
 * as well as the value destroy function, from @other_hash_table.
1047
 *
1048
 * The returned hash table will be empty; it will not contain the keys
1049
 * or values from @other_hash_table.
1050
 *
1051
 * Returns: (transfer full) (not nullable): a new #GHashTable
1052
 * Since: 2.72
1053
 */
1054
GHashTable *
1055
g_hash_table_new_similar (GHashTable *other_hash_table)
1056
0
{
1057
0
  g_return_val_if_fail (other_hash_table, NULL);
1058
1059
0
  return g_hash_table_new_full (other_hash_table->hash_func,
1060
0
                                other_hash_table->key_equal_func,
1061
0
                                other_hash_table->key_destroy_func,
1062
0
                                other_hash_table->value_destroy_func);
1063
0
}
1064
1065
/**
1066
 * g_hash_table_iter_init:
1067
 * @iter: an uninitialized #GHashTableIter
1068
 * @hash_table: a #GHashTable
1069
 *
1070
 * Initializes a key/value pair iterator and associates it with
1071
 * @hash_table. Modifying the hash table after calling this function
1072
 * invalidates the returned iterator.
1073
 *
1074
 * The iteration order of a #GHashTableIter over the keys/values in a hash
1075
 * table is not defined.
1076
 *
1077
 * |[<!-- language="C" -->
1078
 * GHashTableIter iter;
1079
 * gpointer key, value;
1080
 *
1081
 * g_hash_table_iter_init (&iter, hash_table);
1082
 * while (g_hash_table_iter_next (&iter, &key, &value))
1083
 *   {
1084
 *     // do something with key and value
1085
 *   }
1086
 * ]|
1087
 *
1088
 * Since: 2.16
1089
 */
1090
void
1091
g_hash_table_iter_init (GHashTableIter *iter,
1092
                        GHashTable     *hash_table)
1093
0
{
1094
0
  RealIter *ri = (RealIter *) iter;
1095
1096
0
  g_return_if_fail (iter != NULL);
1097
0
  g_return_if_fail (hash_table != NULL);
1098
1099
0
  ri->hash_table = hash_table;
1100
0
  ri->position = -1;
1101
0
#ifndef G_DISABLE_ASSERT
1102
0
  ri->version = hash_table->version;
1103
0
#endif
1104
0
}
1105
1106
/**
1107
 * g_hash_table_iter_next:
1108
 * @iter: an initialized #GHashTableIter
1109
 * @key: (out) (optional) (nullable): a location to store the key
1110
 * @value: (out) (optional) (nullable): a location to store the value
1111
 *
1112
 * Advances @iter and retrieves the key and/or value that are now
1113
 * pointed to as a result of this advancement. If %FALSE is returned,
1114
 * @key and @value are not set, and the iterator becomes invalid.
1115
 *
1116
 * Returns: %FALSE if the end of the #GHashTable has been reached.
1117
 *
1118
 * Since: 2.16
1119
 */
1120
gboolean
1121
g_hash_table_iter_next (GHashTableIter *iter,
1122
                        gpointer       *key,
1123
                        gpointer       *value)
1124
0
{
1125
0
  RealIter *ri = (RealIter *) iter;
1126
0
  gint position;
1127
1128
0
  g_return_val_if_fail (iter != NULL, FALSE);
1129
0
#ifndef G_DISABLE_ASSERT
1130
0
  g_return_val_if_fail (ri->version == ri->hash_table->version, FALSE);
1131
0
#endif
1132
0
  g_return_val_if_fail (ri->position < (gssize) ri->hash_table->size, FALSE);
1133
1134
0
  position = ri->position;
1135
1136
0
  do
1137
0
    {
1138
0
      position++;
1139
0
      if (position >= (gssize) ri->hash_table->size)
1140
0
        {
1141
0
          ri->position = position;
1142
0
          return FALSE;
1143
0
        }
1144
0
    }
1145
0
  while (!HASH_IS_REAL (ri->hash_table->hashes[position]));
1146
1147
0
  if (key != NULL)
1148
0
    *key = g_hash_table_fetch_key_or_value (ri->hash_table->keys, position, ri->hash_table->have_big_keys);
1149
0
  if (value != NULL)
1150
0
    *value = g_hash_table_fetch_key_or_value (ri->hash_table->values, position, ri->hash_table->have_big_values);
1151
1152
0
  ri->position = position;
1153
0
  return TRUE;
1154
0
}
1155
1156
/**
1157
 * g_hash_table_iter_get_hash_table:
1158
 * @iter: an initialized #GHashTableIter
1159
 *
1160
 * Returns the #GHashTable associated with @iter.
1161
 *
1162
 * Returns: (transfer none): the #GHashTable associated with @iter.
1163
 *
1164
 * Since: 2.16
1165
 */
1166
GHashTable *
1167
g_hash_table_iter_get_hash_table (GHashTableIter *iter)
1168
0
{
1169
0
  g_return_val_if_fail (iter != NULL, NULL);
1170
1171
0
  return ((RealIter *) iter)->hash_table;
1172
0
}
1173
1174
static void
1175
iter_remove_or_steal (RealIter *ri, gboolean notify)
1176
0
{
1177
0
  g_return_if_fail (ri != NULL);
1178
0
#ifndef G_DISABLE_ASSERT
1179
0
  g_return_if_fail (ri->version == ri->hash_table->version);
1180
0
#endif
1181
0
  g_return_if_fail (ri->position >= 0);
1182
0
  g_return_if_fail ((gsize) ri->position < ri->hash_table->size);
1183
1184
0
  g_hash_table_remove_node (ri->hash_table, ri->position, notify);
1185
1186
0
#ifndef G_DISABLE_ASSERT
1187
0
  ri->version++;
1188
0
  ri->hash_table->version++;
1189
0
#endif
1190
0
}
1191
1192
/**
1193
 * g_hash_table_iter_remove:
1194
 * @iter: an initialized #GHashTableIter
1195
 *
1196
 * Removes the key/value pair currently pointed to by the iterator
1197
 * from its associated #GHashTable. Can only be called after
1198
 * g_hash_table_iter_next() returned %TRUE, and cannot be called
1199
 * more than once for the same key/value pair.
1200
 *
1201
 * If the #GHashTable was created using g_hash_table_new_full(),
1202
 * the key and value are freed using the supplied destroy functions,
1203
 * otherwise you have to make sure that any dynamically allocated
1204
 * values are freed yourself.
1205
 *
1206
 * It is safe to continue iterating the #GHashTable afterward:
1207
 * |[<!-- language="C" -->
1208
 * while (g_hash_table_iter_next (&iter, &key, &value))
1209
 *   {
1210
 *     if (condition)
1211
 *       g_hash_table_iter_remove (&iter);
1212
 *   }
1213
 * ]|
1214
 *
1215
 * Since: 2.16
1216
 */
1217
void
1218
g_hash_table_iter_remove (GHashTableIter *iter)
1219
0
{
1220
0
  iter_remove_or_steal ((RealIter *) iter, TRUE);
1221
0
}
1222
1223
/*
1224
 * g_hash_table_insert_node:
1225
 * @hash_table: our #GHashTable
1226
 * @node_index: pointer to node to insert/replace
1227
 * @key_hash: key hash
1228
 * @key: (nullable): key to replace with, or %NULL
1229
 * @value: value to replace with
1230
 * @keep_new_key: whether to replace the key in the node with @key
1231
 * @reusing_key: whether @key was taken out of the existing node
1232
 *
1233
 * Inserts a value at @node_index in the hash table and updates it.
1234
 *
1235
 * If @key has been taken out of the existing node (ie it is not
1236
 * passed in via a g_hash_table_insert/replace) call, then @reusing_key
1237
 * should be %TRUE.
1238
 *
1239
 * Returns: %TRUE if the key did not exist yet
1240
 */
1241
static gboolean
1242
g_hash_table_insert_node (GHashTable *hash_table,
1243
                          guint       node_index,
1244
                          guint       key_hash,
1245
                          gpointer    new_key,
1246
                          gpointer    new_value,
1247
                          gboolean    keep_new_key,
1248
                          gboolean    reusing_key)
1249
534k
{
1250
534k
  gboolean already_exists;
1251
534k
  guint old_hash;
1252
534k
  gpointer key_to_free = NULL;
1253
534k
  gpointer key_to_keep = NULL;
1254
534k
  gpointer value_to_free = NULL;
1255
1256
534k
  old_hash = hash_table->hashes[node_index];
1257
534k
  already_exists = HASH_IS_REAL (old_hash);
1258
1259
  /* Proceed in three steps.  First, deal with the key because it is the
1260
   * most complicated.  Then consider if we need to split the table in
1261
   * two (because writing the value will result in the set invariant
1262
   * becoming broken).  Then deal with the value.
1263
   *
1264
   * There are three cases for the key:
1265
   *
1266
   *  - entry already exists in table, reusing key:
1267
   *    free the just-passed-in new_key and use the existing value
1268
   *
1269
   *  - entry already exists in table, not reusing key:
1270
   *    free the entry in the table, use the new key
1271
   *
1272
   *  - entry not already in table:
1273
   *    use the new key, free nothing
1274
   *
1275
   * We update the hash at the same time...
1276
   */
1277
534k
  if (already_exists)
1278
222k
    {
1279
      /* Note: we must record the old value before writing the new key
1280
       * because we might change the value in the event that the two
1281
       * arrays are shared.
1282
       */
1283
222k
      value_to_free = g_hash_table_fetch_key_or_value (hash_table->values, node_index, hash_table->have_big_values);
1284
1285
222k
      if (keep_new_key)
1286
0
        {
1287
0
          key_to_free = g_hash_table_fetch_key_or_value (hash_table->keys, node_index, hash_table->have_big_keys);
1288
0
          key_to_keep = new_key;
1289
0
        }
1290
222k
      else
1291
222k
        {
1292
222k
          key_to_free = new_key;
1293
222k
          key_to_keep = g_hash_table_fetch_key_or_value (hash_table->keys, node_index, hash_table->have_big_keys);
1294
222k
        }
1295
222k
    }
1296
311k
  else
1297
311k
    {
1298
311k
      hash_table->hashes[node_index] = key_hash;
1299
311k
      key_to_keep = new_key;
1300
311k
    }
1301
1302
  /* Resize key/value arrays and split table as necessary */
1303
534k
  g_hash_table_ensure_keyval_fits (hash_table, key_to_keep, new_value);
1304
534k
  g_hash_table_assign_key_or_value (hash_table->keys, node_index, hash_table->have_big_keys, key_to_keep);
1305
1306
  /* Step 3: Actually do the write */
1307
534k
  g_hash_table_assign_key_or_value (hash_table->values, node_index, hash_table->have_big_values, new_value);
1308
1309
  /* Now, the bookkeeping... */
1310
534k
  if (!already_exists)
1311
311k
    {
1312
311k
      hash_table->nnodes++;
1313
1314
311k
      if (HASH_IS_UNUSED (old_hash))
1315
311k
        {
1316
          /* We replaced an empty node, and not a tombstone */
1317
311k
          hash_table->noccupied++;
1318
311k
          g_hash_table_maybe_resize (hash_table);
1319
311k
        }
1320
1321
311k
#ifndef G_DISABLE_ASSERT
1322
311k
      hash_table->version++;
1323
311k
#endif
1324
311k
    }
1325
1326
534k
  if (already_exists)
1327
222k
    {
1328
222k
      if (hash_table->key_destroy_func && !reusing_key)
1329
0
        (* hash_table->key_destroy_func) (key_to_free);
1330
222k
      if (hash_table->value_destroy_func)
1331
0
        (* hash_table->value_destroy_func) (value_to_free);
1332
222k
    }
1333
1334
534k
  return !already_exists;
1335
534k
}
1336
1337
/**
1338
 * g_hash_table_iter_replace:
1339
 * @iter: an initialized #GHashTableIter
1340
 * @value: the value to replace with
1341
 *
1342
 * Replaces the value currently pointed to by the iterator
1343
 * from its associated #GHashTable. Can only be called after
1344
 * g_hash_table_iter_next() returned %TRUE.
1345
 *
1346
 * If you supplied a @value_destroy_func when creating the
1347
 * #GHashTable, the old value is freed using that function.
1348
 *
1349
 * Since: 2.30
1350
 */
1351
void
1352
g_hash_table_iter_replace (GHashTableIter *iter,
1353
                           gpointer        value)
1354
0
{
1355
0
  RealIter *ri;
1356
0
  guint node_hash;
1357
0
  gpointer key;
1358
1359
0
  ri = (RealIter *) iter;
1360
1361
0
  g_return_if_fail (ri != NULL);
1362
0
#ifndef G_DISABLE_ASSERT
1363
0
  g_return_if_fail (ri->version == ri->hash_table->version);
1364
0
#endif
1365
0
  g_return_if_fail (ri->position >= 0);
1366
0
  g_return_if_fail ((gsize) ri->position < ri->hash_table->size);
1367
1368
0
  node_hash = ri->hash_table->hashes[ri->position];
1369
1370
0
  key = g_hash_table_fetch_key_or_value (ri->hash_table->keys, ri->position, ri->hash_table->have_big_keys);
1371
1372
0
  g_hash_table_insert_node (ri->hash_table, ri->position, node_hash, key, value, TRUE, TRUE);
1373
1374
0
#ifndef G_DISABLE_ASSERT
1375
0
  ri->version++;
1376
0
  ri->hash_table->version++;
1377
0
#endif
1378
0
}
1379
1380
/**
1381
 * g_hash_table_iter_steal:
1382
 * @iter: an initialized #GHashTableIter
1383
 *
1384
 * Removes the key/value pair currently pointed to by the
1385
 * iterator from its associated #GHashTable, without calling
1386
 * the key and value destroy functions. Can only be called
1387
 * after g_hash_table_iter_next() returned %TRUE, and cannot
1388
 * be called more than once for the same key/value pair.
1389
 *
1390
 * Since: 2.16
1391
 */
1392
void
1393
g_hash_table_iter_steal (GHashTableIter *iter)
1394
0
{
1395
0
  iter_remove_or_steal ((RealIter *) iter, FALSE);
1396
0
}
1397
1398
1399
/**
1400
 * g_hash_table_ref:
1401
 * @hash_table: a valid #GHashTable
1402
 *
1403
 * Atomically increments the reference count of @hash_table by one.
1404
 * This function is MT-safe and may be called from any thread.
1405
 *
1406
 * Returns: (transfer full): the passed in #GHashTable
1407
 *
1408
 * Since: 2.10
1409
 */
1410
GHashTable *
1411
g_hash_table_ref (GHashTable *hash_table)
1412
0
{
1413
0
  g_return_val_if_fail (hash_table != NULL, NULL);
1414
1415
0
  g_atomic_ref_count_inc (&hash_table->ref_count);
1416
1417
0
  return hash_table;
1418
0
}
1419
1420
/**
1421
 * g_hash_table_unref:
1422
 * @hash_table: (transfer full): a valid #GHashTable
1423
 *
1424
 * Atomically decrements the reference count of @hash_table by one.
1425
 * If the reference count drops to 0, all keys and values will be
1426
 * destroyed, and all memory allocated by the hash table is released.
1427
 * This function is MT-safe and may be called from any thread.
1428
 *
1429
 * Since: 2.10
1430
 */
1431
void
1432
g_hash_table_unref (GHashTable *hash_table)
1433
182
{
1434
182
  g_return_if_fail (hash_table != NULL);
1435
1436
182
  if (g_atomic_ref_count_dec (&hash_table->ref_count))
1437
182
    {
1438
182
      g_hash_table_remove_all_nodes (hash_table, TRUE, TRUE);
1439
182
      if (hash_table->keys != hash_table->values)
1440
181
        g_free (hash_table->values);
1441
182
      g_free (hash_table->keys);
1442
182
      g_free (hash_table->hashes);
1443
182
      g_slice_free (GHashTable, hash_table);
1444
182
    }
1445
182
}
1446
1447
/**
1448
 * g_hash_table_destroy:
1449
 * @hash_table: a #GHashTable
1450
 *
1451
 * Destroys all keys and values in the #GHashTable and decrements its
1452
 * reference count by 1. If keys and/or values are dynamically allocated,
1453
 * you should either free them first or create the #GHashTable with destroy
1454
 * notifiers using g_hash_table_new_full(). In the latter case the destroy
1455
 * functions you supplied will be called on all keys and values during the
1456
 * destruction phase.
1457
 */
1458
void
1459
g_hash_table_destroy (GHashTable *hash_table)
1460
182
{
1461
182
  g_return_if_fail (hash_table != NULL);
1462
1463
182
  g_hash_table_remove_all (hash_table);
1464
182
  g_hash_table_unref (hash_table);
1465
182
}
1466
1467
/**
1468
 * g_hash_table_lookup:
1469
 * @hash_table: a #GHashTable
1470
 * @key: the key to look up
1471
 *
1472
 * Looks up a key in a #GHashTable. Note that this function cannot
1473
 * distinguish between a key that is not present and one which is present
1474
 * and has the value %NULL. If you need this distinction, use
1475
 * g_hash_table_lookup_extended().
1476
 *
1477
 * Returns: (nullable): the associated value, or %NULL if the key is not found
1478
 */
1479
gpointer
1480
g_hash_table_lookup (GHashTable    *hash_table,
1481
                     gconstpointer  key)
1482
8.42M
{
1483
8.42M
  guint node_index;
1484
8.42M
  guint node_hash;
1485
1486
8.42M
  g_return_val_if_fail (hash_table != NULL, NULL);
1487
1488
8.42M
  node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1489
1490
8.42M
  return HASH_IS_REAL (hash_table->hashes[node_index])
1491
8.42M
    ? g_hash_table_fetch_key_or_value (hash_table->values, node_index, hash_table->have_big_values)
1492
8.42M
    : NULL;
1493
8.42M
}
1494
1495
/**
1496
 * g_hash_table_lookup_extended:
1497
 * @hash_table: a #GHashTable
1498
 * @lookup_key: the key to look up
1499
 * @orig_key: (out) (optional): return location for the original key
1500
 * @value: (out) (optional) (nullable): return location for the value associated
1501
 * with the key
1502
 *
1503
 * Looks up a key in the #GHashTable, returning the original key and the
1504
 * associated value and a #gboolean which is %TRUE if the key was found. This
1505
 * is useful if you need to free the memory allocated for the original key,
1506
 * for example before calling g_hash_table_remove().
1507
 *
1508
 * You can actually pass %NULL for @lookup_key to test
1509
 * whether the %NULL key exists, provided the hash and equal functions
1510
 * of @hash_table are %NULL-safe.
1511
 *
1512
 * Returns: %TRUE if the key was found in the #GHashTable
1513
 */
1514
gboolean
1515
g_hash_table_lookup_extended (GHashTable    *hash_table,
1516
                              gconstpointer  lookup_key,
1517
                              gpointer      *orig_key,
1518
                              gpointer      *value)
1519
0
{
1520
0
  guint node_index;
1521
0
  guint node_hash;
1522
1523
0
  g_return_val_if_fail (hash_table != NULL, FALSE);
1524
1525
0
  node_index = g_hash_table_lookup_node (hash_table, lookup_key, &node_hash);
1526
1527
0
  if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1528
0
    {
1529
0
      if (orig_key != NULL)
1530
0
        *orig_key = NULL;
1531
0
      if (value != NULL)
1532
0
        *value = NULL;
1533
1534
0
      return FALSE;
1535
0
    }
1536
1537
0
  if (orig_key)
1538
0
    *orig_key = g_hash_table_fetch_key_or_value (hash_table->keys, node_index, hash_table->have_big_keys);
1539
1540
0
  if (value)
1541
0
    *value = g_hash_table_fetch_key_or_value (hash_table->values, node_index, hash_table->have_big_values);
1542
1543
0
  return TRUE;
1544
0
}
1545
1546
/*
1547
 * g_hash_table_insert_internal:
1548
 * @hash_table: our #GHashTable
1549
 * @key: the key to insert
1550
 * @value: the value to insert
1551
 * @keep_new_key: if %TRUE and this key already exists in the table
1552
 *   then call the destroy notify function on the old key.  If %FALSE
1553
 *   then call the destroy notify function on the new key.
1554
 *
1555
 * Implements the common logic for the g_hash_table_insert() and
1556
 * g_hash_table_replace() functions.
1557
 *
1558
 * Do a lookup of @key. If it is found, replace it with the new
1559
 * @value (and perhaps the new @key). If it is not found, create
1560
 * a new node.
1561
 *
1562
 * Returns: %TRUE if the key did not exist yet
1563
 */
1564
static gboolean
1565
g_hash_table_insert_internal (GHashTable *hash_table,
1566
                              gpointer    key,
1567
                              gpointer    value,
1568
                              gboolean    keep_new_key)
1569
534k
{
1570
534k
  guint key_hash;
1571
534k
  guint node_index;
1572
1573
534k
  g_return_val_if_fail (hash_table != NULL, FALSE);
1574
1575
534k
  node_index = g_hash_table_lookup_node (hash_table, key, &key_hash);
1576
1577
534k
  return g_hash_table_insert_node (hash_table, node_index, key_hash, key, value, keep_new_key, FALSE);
1578
534k
}
1579
1580
/**
1581
 * g_hash_table_insert:
1582
 * @hash_table: a #GHashTable
1583
 * @key: a key to insert
1584
 * @value: the value to associate with the key
1585
 *
1586
 * Inserts a new key and value into a #GHashTable.
1587
 *
1588
 * If the key already exists in the #GHashTable its current
1589
 * value is replaced with the new value. If you supplied a
1590
 * @value_destroy_func when creating the #GHashTable, the old
1591
 * value is freed using that function. If you supplied a
1592
 * @key_destroy_func when creating the #GHashTable, the passed
1593
 * key is freed using that function.
1594
 *
1595
 * Starting from GLib 2.40, this function returns a boolean value to
1596
 * indicate whether the newly added value was already in the hash table
1597
 * or not.
1598
 *
1599
 * Returns: %TRUE if the key did not exist yet
1600
 */
1601
gboolean
1602
g_hash_table_insert (GHashTable *hash_table,
1603
                     gpointer    key,
1604
                     gpointer    value)
1605
304k
{
1606
304k
  return g_hash_table_insert_internal (hash_table, key, value, FALSE);
1607
304k
}
1608
1609
/**
1610
 * g_hash_table_replace:
1611
 * @hash_table: a #GHashTable
1612
 * @key: a key to insert
1613
 * @value: the value to associate with the key
1614
 *
1615
 * Inserts a new key and value into a #GHashTable similar to
1616
 * g_hash_table_insert(). The difference is that if the key
1617
 * already exists in the #GHashTable, it gets replaced by the
1618
 * new key. If you supplied a @value_destroy_func when creating
1619
 * the #GHashTable, the old value is freed using that function.
1620
 * If you supplied a @key_destroy_func when creating the
1621
 * #GHashTable, the old key is freed using that function.
1622
 *
1623
 * Starting from GLib 2.40, this function returns a boolean value to
1624
 * indicate whether the newly added value was already in the hash table
1625
 * or not.
1626
 *
1627
 * Returns: %TRUE if the key did not exist yet
1628
 */
1629
gboolean
1630
g_hash_table_replace (GHashTable *hash_table,
1631
                      gpointer    key,
1632
                      gpointer    value)
1633
0
{
1634
0
  return g_hash_table_insert_internal (hash_table, key, value, TRUE);
1635
0
}
1636
1637
/**
1638
 * g_hash_table_add:
1639
 * @hash_table: a #GHashTable
1640
 * @key: (transfer full): a key to insert
1641
 *
1642
 * This is a convenience function for using a #GHashTable as a set.  It
1643
 * is equivalent to calling g_hash_table_replace() with @key as both the
1644
 * key and the value.
1645
 *
1646
 * In particular, this means that if @key already exists in the hash table, then
1647
 * the old copy of @key in the hash table is freed and @key replaces it in the
1648
 * table.
1649
 *
1650
 * When a hash table only ever contains keys that have themselves as the
1651
 * corresponding value it is able to be stored more efficiently.  See
1652
 * the discussion in the section description.
1653
 *
1654
 * Starting from GLib 2.40, this function returns a boolean value to
1655
 * indicate whether the newly added value was already in the hash table
1656
 * or not.
1657
 *
1658
 * Returns: %TRUE if the key did not exist yet
1659
 *
1660
 * Since: 2.32
1661
 */
1662
gboolean
1663
g_hash_table_add (GHashTable *hash_table,
1664
                  gpointer    key)
1665
229k
{
1666
229k
  return g_hash_table_insert_internal (hash_table, key, key, TRUE);
1667
229k
}
1668
1669
/**
1670
 * g_hash_table_contains:
1671
 * @hash_table: a #GHashTable
1672
 * @key: a key to check
1673
 *
1674
 * Checks if @key is in @hash_table.
1675
 *
1676
 * Returns: %TRUE if @key is in @hash_table, %FALSE otherwise.
1677
 *
1678
 * Since: 2.32
1679
 **/
1680
gboolean
1681
g_hash_table_contains (GHashTable    *hash_table,
1682
                       gconstpointer  key)
1683
0
{
1684
0
  guint node_index;
1685
0
  guint node_hash;
1686
1687
0
  g_return_val_if_fail (hash_table != NULL, FALSE);
1688
1689
0
  node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1690
1691
0
  return HASH_IS_REAL (hash_table->hashes[node_index]);
1692
0
}
1693
1694
/*
1695
 * g_hash_table_remove_internal:
1696
 * @hash_table: our #GHashTable
1697
 * @key: the key to remove
1698
 * @notify: %TRUE if the destroy notify handlers are to be called
1699
 * Returns: %TRUE if a node was found and removed, else %FALSE
1700
 *
1701
 * Implements the common logic for the g_hash_table_remove() and
1702
 * g_hash_table_steal() functions.
1703
 *
1704
 * Do a lookup of @key and remove it if it is found, calling the
1705
 * destroy notify handlers only if @notify is %TRUE.
1706
 */
1707
static gboolean
1708
g_hash_table_remove_internal (GHashTable    *hash_table,
1709
                              gconstpointer  key,
1710
                              gboolean       notify)
1711
450
{
1712
450
  guint node_index;
1713
450
  guint node_hash;
1714
1715
450
  g_return_val_if_fail (hash_table != NULL, FALSE);
1716
1717
450
  node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
1718
1719
450
  if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1720
0
    return FALSE;
1721
1722
450
  g_hash_table_remove_node (hash_table, node_index, notify);
1723
450
  g_hash_table_maybe_resize (hash_table);
1724
1725
450
#ifndef G_DISABLE_ASSERT
1726
450
  hash_table->version++;
1727
450
#endif
1728
1729
450
  return TRUE;
1730
450
}
1731
1732
/**
1733
 * g_hash_table_remove:
1734
 * @hash_table: a #GHashTable
1735
 * @key: the key to remove
1736
 *
1737
 * Removes a key and its associated value from a #GHashTable.
1738
 *
1739
 * If the #GHashTable was created using g_hash_table_new_full(), the
1740
 * key and value are freed using the supplied destroy functions, otherwise
1741
 * you have to make sure that any dynamically allocated values are freed
1742
 * yourself.
1743
 *
1744
 * Returns: %TRUE if the key was found and removed from the #GHashTable
1745
 */
1746
gboolean
1747
g_hash_table_remove (GHashTable    *hash_table,
1748
                     gconstpointer  key)
1749
450
{
1750
450
  return g_hash_table_remove_internal (hash_table, key, TRUE);
1751
450
}
1752
1753
/**
1754
 * g_hash_table_steal:
1755
 * @hash_table: a #GHashTable
1756
 * @key: the key to remove
1757
 *
1758
 * Removes a key and its associated value from a #GHashTable without
1759
 * calling the key and value destroy functions.
1760
 *
1761
 * Returns: %TRUE if the key was found and removed from the #GHashTable
1762
 */
1763
gboolean
1764
g_hash_table_steal (GHashTable    *hash_table,
1765
                    gconstpointer  key)
1766
0
{
1767
0
  return g_hash_table_remove_internal (hash_table, key, FALSE);
1768
0
}
1769
1770
/**
1771
 * g_hash_table_steal_extended:
1772
 * @hash_table: a #GHashTable
1773
 * @lookup_key: the key to look up
1774
 * @stolen_key: (out) (optional) (transfer full): return location for the
1775
 *    original key
1776
 * @stolen_value: (out) (optional) (nullable) (transfer full): return location
1777
 *    for the value associated with the key
1778
 *
1779
 * Looks up a key in the #GHashTable, stealing the original key and the
1780
 * associated value and returning %TRUE if the key was found. If the key was
1781
 * not found, %FALSE is returned.
1782
 *
1783
 * If found, the stolen key and value are removed from the hash table without
1784
 * calling the key and value destroy functions, and ownership is transferred to
1785
 * the caller of this method, as with g_hash_table_steal(). That is the case
1786
 * regardless whether @stolen_key or @stolen_value output parameters are
1787
 * requested.
1788
 *
1789
 * You can pass %NULL for @lookup_key, provided the hash and equal functions
1790
 * of @hash_table are %NULL-safe.
1791
 *
1792
 * The dictionary implementation optimizes for having all values identical to
1793
 * their keys, for example by using g_hash_table_add(). Before 2.82, when
1794
 * stealing both the key and the value from such a dictionary, the value was
1795
 * %NULL. Since 2.82, the returned value and key will be the same.
1796
 *
1797
 * Returns: %TRUE if the key was found in the #GHashTable
1798
 * Since: 2.58
1799
 */
1800
gboolean
1801
g_hash_table_steal_extended (GHashTable    *hash_table,
1802
                             gconstpointer  lookup_key,
1803
                             gpointer      *stolen_key,
1804
                             gpointer      *stolen_value)
1805
0
{
1806
0
  guint node_index;
1807
0
  guint node_hash;
1808
1809
0
  g_return_val_if_fail (hash_table != NULL, FALSE);
1810
1811
0
  node_index = g_hash_table_lookup_node (hash_table, lookup_key, &node_hash);
1812
1813
0
  if (!HASH_IS_REAL (hash_table->hashes[node_index]))
1814
0
    {
1815
0
      if (stolen_key != NULL)
1816
0
        *stolen_key = NULL;
1817
0
      if (stolen_value != NULL)
1818
0
        *stolen_value = NULL;
1819
0
      return FALSE;
1820
0
    }
1821
1822
0
  if (stolen_key != NULL)
1823
0
  {
1824
0
    *stolen_key = g_hash_table_fetch_key_or_value (hash_table->keys, node_index, hash_table->have_big_keys);
1825
0
    g_hash_table_assign_key_or_value (hash_table->keys, node_index, hash_table->have_big_keys, NULL);
1826
0
  }
1827
1828
0
  if (stolen_value != NULL)
1829
0
    {
1830
0
      if (stolen_key && hash_table->keys == hash_table->values)
1831
0
        *stolen_value = *stolen_key;
1832
0
      else
1833
0
        {
1834
0
          *stolen_value = g_hash_table_fetch_key_or_value (hash_table->values, node_index, hash_table->have_big_values);
1835
0
          g_hash_table_assign_key_or_value (hash_table->values, node_index, hash_table->have_big_values, NULL);
1836
0
        }
1837
0
    }
1838
1839
0
  g_hash_table_remove_node (hash_table, node_index, FALSE);
1840
0
  g_hash_table_maybe_resize (hash_table);
1841
1842
0
#ifndef G_DISABLE_ASSERT
1843
0
  hash_table->version++;
1844
0
#endif
1845
1846
0
  return TRUE;
1847
0
}
1848
1849
/**
1850
 * g_hash_table_remove_all:
1851
 * @hash_table: a #GHashTable
1852
 *
1853
 * Removes all keys and their associated values from a #GHashTable.
1854
 *
1855
 * If the #GHashTable was created using g_hash_table_new_full(),
1856
 * the keys and values are freed using the supplied destroy functions,
1857
 * otherwise you have to make sure that any dynamically allocated
1858
 * values are freed yourself.
1859
 *
1860
 * Since: 2.12
1861
 */
1862
void
1863
g_hash_table_remove_all (GHashTable *hash_table)
1864
182
{
1865
182
  g_return_if_fail (hash_table != NULL);
1866
1867
182
#ifndef G_DISABLE_ASSERT
1868
182
  if (hash_table->nnodes != 0)
1869
181
    hash_table->version++;
1870
182
#endif
1871
1872
182
  g_hash_table_remove_all_nodes (hash_table, TRUE, FALSE);
1873
182
  g_hash_table_maybe_resize (hash_table);
1874
182
}
1875
1876
/**
1877
 * g_hash_table_steal_all:
1878
 * @hash_table: a #GHashTable
1879
 *
1880
 * Removes all keys and their associated values from a #GHashTable
1881
 * without calling the key and value destroy functions.
1882
 *
1883
 * Since: 2.12
1884
 */
1885
void
1886
g_hash_table_steal_all (GHashTable *hash_table)
1887
0
{
1888
0
  g_return_if_fail (hash_table != NULL);
1889
1890
0
#ifndef G_DISABLE_ASSERT
1891
0
  if (hash_table->nnodes != 0)
1892
0
    hash_table->version++;
1893
0
#endif
1894
1895
0
  g_hash_table_remove_all_nodes (hash_table, FALSE, FALSE);
1896
0
  g_hash_table_maybe_resize (hash_table);
1897
0
}
1898
1899
/**
1900
 * g_hash_table_steal_all_keys: (skip)
1901
 * @hash_table: a #GHashTable
1902
 *
1903
 * Removes all keys and their associated values from a #GHashTable
1904
 * without calling the key destroy functions, returning the keys
1905
 * as a #GPtrArray with the free func set to the @hash_table key
1906
 * destroy function.
1907
 *
1908
 * Returns: (transfer container): a #GPtrArray containing each key of
1909
 * the table. Unref with with g_ptr_array_unref() when done.
1910
 *
1911
 * Since: 2.76
1912
 */
1913
GPtrArray *
1914
g_hash_table_steal_all_keys (GHashTable *hash_table)
1915
0
{
1916
0
  GPtrArray *array;
1917
0
  GDestroyNotify key_destroy_func;
1918
1919
0
  g_return_val_if_fail (hash_table != NULL, NULL);
1920
1921
0
  array = g_hash_table_get_keys_as_ptr_array (hash_table);
1922
1923
  /* Ignore the key destroy notify calls during removal, and use it for the
1924
   * array elements instead, but restore it after the hash table has been
1925
   * cleared, so that newly added keys will continue using it.
1926
   */
1927
0
  key_destroy_func = g_steal_pointer (&hash_table->key_destroy_func);
1928
0
  g_ptr_array_set_free_func (array, key_destroy_func);
1929
1930
0
  g_hash_table_remove_all (hash_table);
1931
0
  hash_table->key_destroy_func = g_steal_pointer (&key_destroy_func);
1932
1933
0
  return array;
1934
0
}
1935
1936
/**
1937
 * g_hash_table_steal_all_values: (skip)
1938
 * @hash_table: a #GHashTable
1939
 *
1940
 * Removes all keys and their associated values from a #GHashTable
1941
 * without calling the value destroy functions, returning the values
1942
 * as a #GPtrArray with the free func set to the @hash_table value
1943
 * destroy function.
1944
 *
1945
 * Returns: (transfer container): a #GPtrArray containing each value of
1946
 * the table. Unref with with g_ptr_array_unref() when done.
1947
 *
1948
 * Since: 2.76
1949
 */
1950
GPtrArray *
1951
g_hash_table_steal_all_values (GHashTable *hash_table)
1952
0
{
1953
0
  GPtrArray *array;
1954
0
  GDestroyNotify value_destroy_func;
1955
1956
0
  g_return_val_if_fail (hash_table != NULL, NULL);
1957
1958
0
  array = g_hash_table_get_values_as_ptr_array (hash_table);
1959
1960
  /* Ignore the value destroy notify calls during removal, and use it for the
1961
   * array elements instead, but restore it after the hash table has been
1962
   * cleared, so that newly added values will continue using it.
1963
   */
1964
0
  value_destroy_func = g_steal_pointer (&hash_table->value_destroy_func);
1965
0
  g_ptr_array_set_free_func (array, value_destroy_func);
1966
1967
0
  g_hash_table_remove_all (hash_table);
1968
0
  hash_table->value_destroy_func = g_steal_pointer (&value_destroy_func);
1969
1970
0
  return array;
1971
0
}
1972
1973
/*
1974
 * g_hash_table_foreach_remove_or_steal:
1975
 * @hash_table: a #GHashTable
1976
 * @func: the user's callback function
1977
 * @user_data: data for @func
1978
 * @notify: %TRUE if the destroy notify handlers are to be called
1979
 *
1980
 * Implements the common logic for g_hash_table_foreach_remove()
1981
 * and g_hash_table_foreach_steal().
1982
 *
1983
 * Iterates over every node in the table, calling @func with the key
1984
 * and value of the node (and @user_data). If @func returns %TRUE the
1985
 * node is removed from the table.
1986
 *
1987
 * If @notify is true then the destroy notify handlers will be called
1988
 * for each removed node.
1989
 */
1990
static guint
1991
g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
1992
                                      GHRFunc     func,
1993
                                      gpointer    user_data,
1994
                                      gboolean    notify)
1995
0
{
1996
0
  guint deleted = 0;
1997
0
  gsize i;
1998
0
#ifndef G_DISABLE_ASSERT
1999
0
  gint version = hash_table->version;
2000
0
#endif
2001
2002
0
  for (i = 0; i < hash_table->size; i++)
2003
0
    {
2004
0
      guint node_hash = hash_table->hashes[i];
2005
0
      gpointer node_key = g_hash_table_fetch_key_or_value (hash_table->keys, i, hash_table->have_big_keys);
2006
0
      gpointer node_value = g_hash_table_fetch_key_or_value (hash_table->values, i, hash_table->have_big_values);
2007
2008
0
      if (HASH_IS_REAL (node_hash) &&
2009
0
          (* func) (node_key, node_value, user_data))
2010
0
        {
2011
0
          g_hash_table_remove_node (hash_table, i, notify);
2012
0
          deleted++;
2013
0
        }
2014
2015
0
#ifndef G_DISABLE_ASSERT
2016
0
      g_return_val_if_fail (version == hash_table->version, 0);
2017
0
#endif
2018
0
    }
2019
2020
0
  g_hash_table_maybe_resize (hash_table);
2021
2022
0
#ifndef G_DISABLE_ASSERT
2023
0
  if (deleted > 0)
2024
0
    hash_table->version++;
2025
0
#endif
2026
2027
0
  return deleted;
2028
0
}
2029
2030
/**
2031
 * g_hash_table_foreach_remove:
2032
 * @hash_table: a #GHashTable
2033
 * @func: (scope call): the function to call for each key/value pair
2034
 * @user_data: user data to pass to the function
2035
 *
2036
 * Calls the given function for each key/value pair in the
2037
 * #GHashTable. If the function returns %TRUE, then the key/value
2038
 * pair is removed from the #GHashTable. If you supplied key or
2039
 * value destroy functions when creating the #GHashTable, they are
2040
 * used to free the memory allocated for the removed keys and values.
2041
 *
2042
 * See #GHashTableIter for an alternative way to loop over the
2043
 * key/value pairs in the hash table.
2044
 *
2045
 * Returns: the number of key/value pairs removed
2046
 */
2047
guint
2048
g_hash_table_foreach_remove (GHashTable *hash_table,
2049
                             GHRFunc     func,
2050
                             gpointer    user_data)
2051
0
{
2052
0
  g_return_val_if_fail (hash_table != NULL, 0);
2053
0
  g_return_val_if_fail (func != NULL, 0);
2054
2055
0
  return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
2056
0
}
2057
2058
/**
2059
 * g_hash_table_foreach_steal:
2060
 * @hash_table: a #GHashTable
2061
 * @func: (scope call): the function to call for each key/value pair
2062
 * @user_data: user data to pass to the function
2063
 *
2064
 * Calls the given function for each key/value pair in the
2065
 * #GHashTable. If the function returns %TRUE, then the key/value
2066
 * pair is removed from the #GHashTable, but no key or value
2067
 * destroy functions are called.
2068
 *
2069
 * See #GHashTableIter for an alternative way to loop over the
2070
 * key/value pairs in the hash table.
2071
 *
2072
 * Returns: the number of key/value pairs removed.
2073
 */
2074
guint
2075
g_hash_table_foreach_steal (GHashTable *hash_table,
2076
                            GHRFunc     func,
2077
                            gpointer    user_data)
2078
0
{
2079
0
  g_return_val_if_fail (hash_table != NULL, 0);
2080
0
  g_return_val_if_fail (func != NULL, 0);
2081
2082
0
  return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
2083
0
}
2084
2085
/**
2086
 * g_hash_table_foreach:
2087
 * @hash_table: a #GHashTable
2088
 * @func: (scope call): the function to call for each key/value pair
2089
 * @user_data: user data to pass to the function
2090
 *
2091
 * Calls the given function for each of the key/value pairs in the
2092
 * #GHashTable.  The function is passed the key and value of each
2093
 * pair, and the given @user_data parameter.  The hash table may not
2094
 * be modified while iterating over it (you can't add/remove
2095
 * items). To remove all items matching a predicate, use
2096
 * g_hash_table_foreach_remove().
2097
 *
2098
 * The order in which g_hash_table_foreach() iterates over the keys/values in
2099
 * the hash table is not defined.
2100
 *
2101
 * See g_hash_table_find() for performance caveats for linear
2102
 * order searches in contrast to g_hash_table_lookup().
2103
 */
2104
void
2105
g_hash_table_foreach (GHashTable *hash_table,
2106
                      GHFunc      func,
2107
                      gpointer    user_data)
2108
9
{
2109
9
  gsize i;
2110
9
#ifndef G_DISABLE_ASSERT
2111
9
  gint version;
2112
9
#endif
2113
2114
9
  g_return_if_fail (hash_table != NULL);
2115
9
  g_return_if_fail (func != NULL);
2116
2117
9
#ifndef G_DISABLE_ASSERT
2118
9
  version = hash_table->version;
2119
9
#endif
2120
2121
129
  for (i = 0; i < hash_table->size; i++)
2122
120
    {
2123
120
      guint node_hash = hash_table->hashes[i];
2124
120
      gpointer node_key = g_hash_table_fetch_key_or_value (hash_table->keys, i, hash_table->have_big_keys);
2125
120
      gpointer node_value = g_hash_table_fetch_key_or_value (hash_table->values, i, hash_table->have_big_values);
2126
2127
120
      if (HASH_IS_REAL (node_hash))
2128
54
        (* func) (node_key, node_value, user_data);
2129
2130
120
#ifndef G_DISABLE_ASSERT
2131
120
      g_return_if_fail (version == hash_table->version);
2132
120
#endif
2133
120
    }
2134
9
}
2135
2136
/**
2137
 * g_hash_table_find:
2138
 * @hash_table: a #GHashTable
2139
 * @predicate: (scope call): function to test the key/value pairs for a certain property
2140
 * @user_data: user data to pass to the function
2141
 *
2142
 * Calls the given function for key/value pairs in the #GHashTable
2143
 * until @predicate returns %TRUE. The function is passed the key
2144
 * and value of each pair, and the given @user_data parameter. The
2145
 * hash table may not be modified while iterating over it (you can't
2146
 * add/remove items).
2147
 *
2148
 * Note, that hash tables are really only optimized for forward
2149
 * lookups, i.e. g_hash_table_lookup(). So code that frequently issues
2150
 * g_hash_table_find() or g_hash_table_foreach() (e.g. in the order of
2151
 * once per every entry in a hash table) should probably be reworked
2152
 * to use additional or different data structures for reverse lookups
2153
 * (keep in mind that an O(n) find/foreach operation issued for all n
2154
 * values in a hash table ends up needing O(n*n) operations).
2155
 *
2156
 * Returns: (nullable): The value of the first key/value pair is returned,
2157
 *     for which @predicate evaluates to %TRUE. If no pair with the
2158
 *     requested property is found, %NULL is returned.
2159
 *
2160
 * Since: 2.4
2161
 */
2162
gpointer
2163
g_hash_table_find (GHashTable *hash_table,
2164
                   GHRFunc     predicate,
2165
                   gpointer    user_data)
2166
0
{
2167
0
  gsize i;
2168
0
#ifndef G_DISABLE_ASSERT
2169
0
  gint version;
2170
0
#endif
2171
0
  gboolean match;
2172
2173
0
  g_return_val_if_fail (hash_table != NULL, NULL);
2174
0
  g_return_val_if_fail (predicate != NULL, NULL);
2175
2176
0
#ifndef G_DISABLE_ASSERT
2177
0
  version = hash_table->version;
2178
0
#endif
2179
2180
0
  match = FALSE;
2181
2182
0
  for (i = 0; i < hash_table->size; i++)
2183
0
    {
2184
0
      guint node_hash = hash_table->hashes[i];
2185
0
      gpointer node_key = g_hash_table_fetch_key_or_value (hash_table->keys, i, hash_table->have_big_keys);
2186
0
      gpointer node_value = g_hash_table_fetch_key_or_value (hash_table->values, i, hash_table->have_big_values);
2187
2188
0
      if (HASH_IS_REAL (node_hash))
2189
0
        match = predicate (node_key, node_value, user_data);
2190
2191
0
#ifndef G_DISABLE_ASSERT
2192
0
      g_return_val_if_fail (version == hash_table->version, NULL);
2193
0
#endif
2194
2195
0
      if (match)
2196
0
        return node_value;
2197
0
    }
2198
2199
0
  return NULL;
2200
0
}
2201
2202
/**
2203
 * g_hash_table_size:
2204
 * @hash_table: a #GHashTable
2205
 *
2206
 * Returns the number of elements contained in the #GHashTable.
2207
 *
2208
 * Returns: the number of key/value pairs in the #GHashTable.
2209
 */
2210
guint
2211
g_hash_table_size (GHashTable *hash_table)
2212
0
{
2213
0
  g_return_val_if_fail (hash_table != NULL, 0);
2214
2215
0
  return hash_table->nnodes;
2216
0
}
2217
2218
/**
2219
 * g_hash_table_get_keys:
2220
 * @hash_table: a #GHashTable
2221
 *
2222
 * Retrieves every key inside @hash_table. The returned data is valid
2223
 * until changes to the hash release those keys.
2224
 *
2225
 * This iterates over every entry in the hash table to build its return value.
2226
 * To iterate over the entries in a #GHashTable more efficiently, use a
2227
 * #GHashTableIter.
2228
 *
2229
 * Returns: (transfer container): a #GList containing all the keys
2230
 *     inside the hash table. The content of the list is owned by the
2231
 *     hash table and should not be modified or freed. Use g_list_free()
2232
 *     when done using the list.
2233
 *
2234
 * Since: 2.14
2235
 */
2236
GList *
2237
g_hash_table_get_keys (GHashTable *hash_table)
2238
0
{
2239
0
  gsize i;
2240
0
  GList *retval;
2241
2242
0
  g_return_val_if_fail (hash_table != NULL, NULL);
2243
2244
0
  retval = NULL;
2245
0
  for (i = 0; i < hash_table->size; i++)
2246
0
    {
2247
0
      if (HASH_IS_REAL (hash_table->hashes[i]))
2248
0
        retval = g_list_prepend (retval, g_hash_table_fetch_key_or_value (hash_table->keys, i, hash_table->have_big_keys));
2249
0
    }
2250
2251
0
  return retval;
2252
0
}
2253
2254
/**
2255
 * g_hash_table_get_keys_as_array:
2256
 * @hash_table: a #GHashTable
2257
 * @length: (out) (optional): the length of the returned array
2258
 *
2259
 * Retrieves every key inside @hash_table, as an array.
2260
 *
2261
 * The returned array is %NULL-terminated but may contain %NULL as a
2262
 * key.  Use @length to determine the true length if it's possible that
2263
 * %NULL was used as the value for a key.
2264
 *
2265
 * Note: in the common case of a string-keyed #GHashTable, the return
2266
 * value of this function can be conveniently cast to (const gchar **).
2267
 *
2268
 * This iterates over every entry in the hash table to build its return value.
2269
 * To iterate over the entries in a #GHashTable more efficiently, use a
2270
 * #GHashTableIter.
2271
 *
2272
 * You should always free the return result with g_free().  In the
2273
 * above-mentioned case of a string-keyed hash table, it may be
2274
 * appropriate to use g_strfreev() if you call g_hash_table_steal_all()
2275
 * first to transfer ownership of the keys.
2276
 *
2277
 * Returns: (array length=length) (transfer container): a
2278
 *   %NULL-terminated array containing each key from the table.
2279
 *
2280
 * Since: 2.40
2281
 **/
2282
gpointer *
2283
g_hash_table_get_keys_as_array (GHashTable *hash_table,
2284
                                guint      *length)
2285
0
{
2286
0
  gpointer *result;
2287
0
  gsize i, j = 0;
2288
2289
0
  result = g_new (gpointer, hash_table->nnodes + 1);
2290
0
  for (i = 0; i < hash_table->size; i++)
2291
0
    {
2292
0
      if (HASH_IS_REAL (hash_table->hashes[i]))
2293
0
        result[j++] = g_hash_table_fetch_key_or_value (hash_table->keys, i, hash_table->have_big_keys);
2294
0
    }
2295
0
  g_assert (j == hash_table->nnodes);
2296
0
  result[j] = NULL;
2297
2298
0
  if (length)
2299
0
    *length = j;
2300
2301
0
  return result;
2302
0
}
2303
2304
/**
2305
 * g_hash_table_get_keys_as_ptr_array: (skip)
2306
 * @hash_table: a #GHashTable
2307
 *
2308
 * Retrieves every key inside @hash_table, as a #GPtrArray.
2309
 * The returned data is valid until changes to the hash release those keys.
2310
 *
2311
 * This iterates over every entry in the hash table to build its return value.
2312
 * To iterate over the entries in a #GHashTable more efficiently, use a
2313
 * #GHashTableIter.
2314
 *
2315
 * You should always unref the returned array with g_ptr_array_unref().
2316
 *
2317
 * Returns: (transfer container): a #GPtrArray containing each key from
2318
 * the table. Unref with with g_ptr_array_unref() when done.
2319
 *
2320
 * Since: 2.76
2321
 **/
2322
GPtrArray *
2323
g_hash_table_get_keys_as_ptr_array (GHashTable *hash_table)
2324
0
{
2325
0
  GPtrArray *array;
2326
2327
0
  g_return_val_if_fail (hash_table != NULL, NULL);
2328
2329
0
  array = g_ptr_array_sized_new (hash_table->size);
2330
0
  for (gsize i = 0; i < hash_table->size; ++i)
2331
0
    {
2332
0
      if (HASH_IS_REAL (hash_table->hashes[i]))
2333
0
        {
2334
0
          g_ptr_array_add (array, g_hash_table_fetch_key_or_value (
2335
0
            hash_table->keys, i, hash_table->have_big_keys));
2336
0
        }
2337
0
    }
2338
0
  g_assert (array->len == hash_table->nnodes);
2339
2340
0
  return array;
2341
0
}
2342
2343
/**
2344
 * g_hash_table_get_values:
2345
 * @hash_table: a #GHashTable
2346
 *
2347
 * Retrieves every value inside @hash_table. The returned data
2348
 * is valid until @hash_table is modified.
2349
 *
2350
 * This iterates over every entry in the hash table to build its return value.
2351
 * To iterate over the entries in a #GHashTable more efficiently, use a
2352
 * #GHashTableIter.
2353
 *
2354
 * Returns: (transfer container): a #GList containing all the values
2355
 *     inside the hash table. The content of the list is owned by the
2356
 *     hash table and should not be modified or freed. Use g_list_free()
2357
 *     when done using the list.
2358
 *
2359
 * Since: 2.14
2360
 */
2361
GList *
2362
g_hash_table_get_values (GHashTable *hash_table)
2363
0
{
2364
0
  gsize i;
2365
0
  GList *retval;
2366
2367
0
  g_return_val_if_fail (hash_table != NULL, NULL);
2368
2369
0
  retval = NULL;
2370
0
  for (i = 0; i < hash_table->size; i++)
2371
0
    {
2372
0
      if (HASH_IS_REAL (hash_table->hashes[i]))
2373
0
        retval = g_list_prepend (retval, g_hash_table_fetch_key_or_value (hash_table->values, i, hash_table->have_big_values));
2374
0
    }
2375
2376
0
  return retval;
2377
0
}
2378
2379
/**
2380
 * g_hash_table_get_values_as_ptr_array: (skip)
2381
 * @hash_table: a #GHashTable
2382
 *
2383
 * Retrieves every value inside @hash_table, as a #GPtrArray.
2384
 * The returned data is valid until changes to the hash release those values.
2385
 *
2386
 * This iterates over every entry in the hash table to build its return value.
2387
 * To iterate over the entries in a #GHashTable more efficiently, use a
2388
 * #GHashTableIter.
2389
 *
2390
 * You should always unref the returned array with g_ptr_array_unref().
2391
 *
2392
 * Returns: (transfer container): a #GPtrArray containing each value from
2393
 * the table. Unref with with g_ptr_array_unref() when done.
2394
 *
2395
 * Since: 2.76
2396
 **/
2397
GPtrArray *
2398
g_hash_table_get_values_as_ptr_array (GHashTable *hash_table)
2399
0
{
2400
0
  GPtrArray *array;
2401
2402
0
  g_return_val_if_fail (hash_table != NULL, NULL);
2403
2404
0
  array = g_ptr_array_sized_new (hash_table->size);
2405
0
  for (gsize i = 0; i < hash_table->size; ++i)
2406
0
    {
2407
0
      if (HASH_IS_REAL (hash_table->hashes[i]))
2408
0
        {
2409
0
          g_ptr_array_add (array, g_hash_table_fetch_key_or_value (
2410
0
            hash_table->values, i, hash_table->have_big_values));
2411
0
        }
2412
0
    }
2413
0
  g_assert (array->len == hash_table->nnodes);
2414
2415
0
  return array;
2416
0
}
2417
2418
/* Hash functions.
2419
 */
2420
2421
/**
2422
 * g_str_equal:
2423
 * @v1: (not nullable): a key
2424
 * @v2: (not nullable): a key to compare with @v1
2425
 *
2426
 * Compares two strings for byte-by-byte equality and returns %TRUE
2427
 * if they are equal. It can be passed to g_hash_table_new() as the
2428
 * @key_equal_func parameter, when using non-%NULL strings as keys in a
2429
 * #GHashTable.
2430
 *
2431
 * This function is typically used for hash table comparisons, but can be used
2432
 * for general purpose comparisons of non-%NULL strings. For a %NULL-safe string
2433
 * comparison function, see g_strcmp0().
2434
 *
2435
 * Returns: %TRUE if the two keys match
2436
 */
2437
gboolean
2438
(g_str_equal) (gconstpointer v1,
2439
               gconstpointer v2)
2440
549k
{
2441
549k
  const gchar *string1 = v1;
2442
549k
  const gchar *string2 = v2;
2443
2444
549k
  return strcmp (string1, string2) == 0;
2445
549k
}
2446
2447
/**
2448
 * g_str_hash:
2449
 * @v: (not nullable): a string key
2450
 *
2451
 * Converts a string to a hash value.
2452
 *
2453
 * This function implements the widely used "djb" hash apparently
2454
 * posted by Daniel Bernstein to comp.lang.c some time ago.  The 32
2455
 * bit unsigned hash value starts at 5381 and for each byte 'c' in
2456
 * the string, is updated: `hash = hash * 33 + c`. This function
2457
 * uses the signed value of each byte.
2458
 *
2459
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2460
 * when using non-%NULL strings as keys in a #GHashTable.
2461
 *
2462
 * Note that this function may not be a perfect fit for all use cases.
2463
 * For example, it produces some hash collisions with strings as short
2464
 * as 2.
2465
 *
2466
 * Returns: a hash value corresponding to the key
2467
 */
2468
guint
2469
g_str_hash (gconstpointer v)
2470
557k
{
2471
557k
  const signed char *p;
2472
557k
  guint32 h = 5381;
2473
2474
8.18M
  for (p = v; *p != '\0'; p++)
2475
7.62M
    h = (h << 5) + h + *p;
2476
2477
557k
  return h;
2478
557k
}
2479
2480
/**
2481
 * g_direct_hash:
2482
 * @v: (nullable): a #gpointer key
2483
 *
2484
 * Converts a gpointer to a hash value.
2485
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2486
 * when using opaque pointers compared by pointer value as keys in a
2487
 * #GHashTable.
2488
 *
2489
 * This hash function is also appropriate for keys that are integers
2490
 * stored in pointers, such as `GINT_TO_POINTER (n)`.
2491
 *
2492
 * Returns: a hash value corresponding to the key.
2493
 */
2494
guint
2495
g_direct_hash (gconstpointer v)
2496
7.89M
{
2497
7.89M
  return GPOINTER_TO_UINT (v);
2498
7.89M
}
2499
2500
/**
2501
 * g_direct_equal:
2502
 * @v1: (nullable): a key
2503
 * @v2: (nullable): a key to compare with @v1
2504
 *
2505
 * Compares two #gpointer arguments and returns %TRUE if they are equal.
2506
 * It can be passed to g_hash_table_new() as the @key_equal_func
2507
 * parameter, when using opaque pointers compared by pointer value as
2508
 * keys in a #GHashTable.
2509
 *
2510
 * This equality function is also appropriate for keys that are integers
2511
 * stored in pointers, such as `GINT_TO_POINTER (n)`.
2512
 *
2513
 * Returns: %TRUE if the two keys match.
2514
 */
2515
gboolean
2516
g_direct_equal (gconstpointer v1,
2517
                gconstpointer v2)
2518
0
{
2519
0
  return v1 == v2;
2520
0
}
2521
2522
/**
2523
 * g_int_equal:
2524
 * @v1: (not nullable): a pointer to a #gint key
2525
 * @v2: (not nullable): a pointer to a #gint key to compare with @v1
2526
 *
2527
 * Compares the two #gint values being pointed to and returns
2528
 * %TRUE if they are equal.
2529
 * It can be passed to g_hash_table_new() as the @key_equal_func
2530
 * parameter, when using non-%NULL pointers to integers as keys in a
2531
 * #GHashTable.
2532
 *
2533
 * Note that this function acts on pointers to #gint, not on #gint
2534
 * directly: if your hash table's keys are of the form
2535
 * `GINT_TO_POINTER (n)`, use g_direct_equal() instead.
2536
 *
2537
 * Returns: %TRUE if the two keys match.
2538
 */
2539
gboolean
2540
g_int_equal (gconstpointer v1,
2541
             gconstpointer v2)
2542
0
{
2543
0
  return *((const gint*) v1) == *((const gint*) v2);
2544
0
}
2545
2546
/**
2547
 * g_int_hash:
2548
 * @v: (not nullable): a pointer to a #gint key
2549
 *
2550
 * Converts a pointer to a #gint to a hash value.
2551
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2552
 * when using non-%NULL pointers to integer values as keys in a #GHashTable.
2553
 *
2554
 * Note that this function acts on pointers to #gint, not on #gint
2555
 * directly: if your hash table's keys are of the form
2556
 * `GINT_TO_POINTER (n)`, use g_direct_hash() instead.
2557
 *
2558
 * Returns: a hash value corresponding to the key.
2559
 */
2560
guint
2561
g_int_hash (gconstpointer v)
2562
0
{
2563
0
  return *(const gint*) v;
2564
0
}
2565
2566
/**
2567
 * g_uint_equal:
2568
 * @v1: (not nullable): a pointer to a #guint key
2569
 * @v2: (not nullable): a pointer to a #guint key to compare with @v1
2570
 *
2571
 * Compares the two #guint values being pointed to and returns
2572
 * %TRUE if they are equal.
2573
 * It can be passed to g_hash_table_new() as the @key_equal_func
2574
 * parameter, when using non-%NULL pointers to integers as keys in a
2575
 * #GHashTable.
2576
 *
2577
 * Note that this function acts on pointers to #guint, not on #guint
2578
 * directly: if your hash table's keys are of the form
2579
 * `GUINT_TO_POINTER (n)`, use g_direct_equal() instead.
2580
 *
2581
 * Returns: %TRUE if the two keys match.
2582
 */
2583
gboolean
2584
g_uint_equal (gconstpointer v1,
2585
              gconstpointer v2)
2586
0
{
2587
0
  return *((const guint *) v1) == *((const guint *) v2);
2588
0
}
2589
2590
/**
2591
 * g_uint_hash:
2592
 * @v: (not nullable): a pointer to a #guint key
2593
 *
2594
 * Converts a pointer to a #guint to a hash value.
2595
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2596
 * when using non-%NULL pointers to integer values as keys in a #GHashTable.
2597
 *
2598
 * Note that this function acts on pointers to #guint, not on #guint
2599
 * directly: if your hash table's keys are of the form
2600
 * `GUINT_TO_POINTER (n)`, use g_direct_hash() instead.
2601
 *
2602
 * Returns: a hash value corresponding to the key.
2603
 */
2604
guint
2605
g_uint_hash (gconstpointer v)
2606
0
{
2607
0
  return *(const guint *) v;
2608
0
}
2609
2610
/**
2611
 * g_int64_equal:
2612
 * @v1: (not nullable): a pointer to a #gint64 key
2613
 * @v2: (not nullable): a pointer to a #gint64 key to compare with @v1
2614
 *
2615
 * Compares the two #gint64 values being pointed to and returns
2616
 * %TRUE if they are equal.
2617
 * It can be passed to g_hash_table_new() as the @key_equal_func
2618
 * parameter, when using non-%NULL pointers to 64-bit integers as keys in a
2619
 * #GHashTable.
2620
 *
2621
 * Returns: %TRUE if the two keys match.
2622
 *
2623
 * Since: 2.22
2624
 */
2625
gboolean
2626
g_int64_equal (gconstpointer v1,
2627
               gconstpointer v2)
2628
0
{
2629
0
  return *((const gint64*) v1) == *((const gint64*) v2);
2630
0
}
2631
2632
/**
2633
 * g_int64_hash:
2634
 * @v: (not nullable): a pointer to a #gint64 key
2635
 *
2636
 * Converts a pointer to a #gint64 to a hash value.
2637
 *
2638
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2639
 * when using non-%NULL pointers to 64-bit integer values as keys in a
2640
 * #GHashTable.
2641
 *
2642
 * Returns: a hash value corresponding to the key.
2643
 *
2644
 * Since: 2.22
2645
 */
2646
guint
2647
g_int64_hash (gconstpointer v)
2648
0
{
2649
0
  const guint64 *bits = v;
2650
2651
0
  return (guint) ((*bits >> 32) ^ (*bits & 0xffffffffU));
2652
0
}
2653
2654
/**
2655
 * g_double_equal:
2656
 * @v1: (not nullable): a pointer to a #gdouble key
2657
 * @v2: (not nullable): a pointer to a #gdouble key to compare with @v1
2658
 *
2659
 * Compares the two #gdouble values being pointed to and returns
2660
 * %TRUE if they are equal.
2661
 * It can be passed to g_hash_table_new() as the @key_equal_func
2662
 * parameter, when using non-%NULL pointers to doubles as keys in a
2663
 * #GHashTable.
2664
 *
2665
 * Returns: %TRUE if the two keys match.
2666
 *
2667
 * Since: 2.22
2668
 */
2669
gboolean
2670
g_double_equal (gconstpointer v1,
2671
                gconstpointer v2)
2672
0
{
2673
0
  return *((const gdouble*) v1) == *((const gdouble*) v2);
2674
0
}
2675
2676
/**
2677
 * g_double_hash:
2678
 * @v: (not nullable): a pointer to a #gdouble key
2679
 *
2680
 * Converts a pointer to a #gdouble to a hash value.
2681
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2682
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2683
 * when using non-%NULL pointers to doubles as keys in a #GHashTable.
2684
 *
2685
 * Returns: a hash value corresponding to the key.
2686
 *
2687
 * Since: 2.22
2688
 */
2689
guint
2690
g_double_hash (gconstpointer v)
2691
0
{
2692
  /* Same as g_int64_hash() */
2693
0
  const guint64 *bits = v;
2694
2695
0
  return (guint) ((*bits >> 32) ^ (*bits & 0xffffffffU));
2696
0
}