Blame glib/ghash.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * MT safe
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <string.h>  /* memset */
Packit ae235b
Packit ae235b
#include "ghash.h"
Packit ae235b
Packit ae235b
#include "glib-private.h"
Packit ae235b
#include "gstrfuncs.h"
Packit ae235b
#include "gatomic.h"
Packit ae235b
#include "gtestutils.h"
Packit ae235b
#include "gslice.h"
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:hash_tables
Packit ae235b
 * @title: Hash Tables
Packit ae235b
 * @short_description: associations between keys and values so that
Packit ae235b
 *     given a key the value can be found quickly
Packit ae235b
 *
Packit ae235b
 * A #GHashTable provides associations between keys and values which is
Packit ae235b
 * optimized so that given a key, the associated value can be found
Packit ae235b
 * very quickly.
Packit ae235b
 *
Packit ae235b
 * Note that neither keys nor values are copied when inserted into the
Packit ae235b
 * #GHashTable, so they must exist for the lifetime of the #GHashTable.
Packit ae235b
 * This means that the use of static strings is OK, but temporary
Packit ae235b
 * strings (i.e. those created in buffers and those returned by GTK+
Packit ae235b
 * widgets) should be copied with g_strdup() before being inserted.
Packit ae235b
 *
Packit ae235b
 * If keys or values are dynamically allocated, you must be careful to
Packit ae235b
 * ensure that they are freed when they are removed from the
Packit ae235b
 * #GHashTable, and also when they are overwritten by new insertions
Packit ae235b
 * into the #GHashTable. It is also not advisable to mix static strings
Packit ae235b
 * and dynamically-allocated strings in a #GHashTable, because it then
Packit ae235b
 * becomes difficult to determine whether the string should be freed.
Packit ae235b
 *
Packit ae235b
 * To create a #GHashTable, use g_hash_table_new().
Packit ae235b
 *
Packit ae235b
 * To insert a key and value into a #GHashTable, use
Packit ae235b
 * g_hash_table_insert().
Packit ae235b
 *
Packit ae235b
 * To lookup a value corresponding to a given key, use
Packit ae235b
 * g_hash_table_lookup() and g_hash_table_lookup_extended().
Packit ae235b
 *
Packit ae235b
 * g_hash_table_lookup_extended() can also be used to simply
Packit ae235b
 * check if a key is present in the hash table.
Packit ae235b
 *
Packit ae235b
 * To remove a key and value, use g_hash_table_remove().
Packit ae235b
 *
Packit ae235b
 * To call a function for each key and value pair use
Packit ae235b
 * g_hash_table_foreach() or use a iterator to iterate over the
Packit ae235b
 * key/value pairs in the hash table, see #GHashTableIter.
Packit ae235b
 *
Packit ae235b
 * To destroy a #GHashTable use g_hash_table_destroy().
Packit ae235b
 *
Packit ae235b
 * A common use-case for hash tables is to store information about a
Packit ae235b
 * set of keys, without associating any particular value with each
Packit ae235b
 * key. GHashTable optimizes one way of doing so: If you store only
Packit ae235b
 * key-value pairs where key == value, then GHashTable does not
Packit ae235b
 * allocate memory to store the values, which can be a considerable
Packit ae235b
 * space saving, if your set is large. The functions
Packit ae235b
 * g_hash_table_add() and g_hash_table_contains() are designed to be
Packit ae235b
 * used when using #GHashTable this way.
Packit ae235b
 *
Packit ae235b
 * #GHashTable is not designed to be statically initialised with keys and
Packit ae235b
 * values known at compile time. To build a static hash table, use a tool such
Packit ae235b
 * as [gperf](https://www.gnu.org/software/gperf/).
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GHashTable:
Packit ae235b
 *
Packit ae235b
 * The #GHashTable struct is an opaque data structure to represent a
Packit ae235b
 * [Hash Table][glib-Hash-Tables]. It should only be accessed via the
Packit ae235b
 * following functions.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GHashFunc:
Packit ae235b
 * @key: a key
Packit ae235b
 *
Packit ae235b
 * Specifies the type of the hash function which is passed to
Packit ae235b
 * g_hash_table_new() when a #GHashTable is created.
Packit ae235b
 *
Packit ae235b
 * The function is passed a key and should return a #guint hash value.
Packit ae235b
 * The functions g_direct_hash(), g_int_hash() and g_str_hash() provide
Packit ae235b
 * hash functions which can be used when the key is a #gpointer, #gint*,
Packit ae235b
 * and #gchar* respectively.
Packit ae235b
 *
Packit ae235b
 * g_direct_hash() is also the appropriate hash function for keys
Packit ae235b
 * of the form `GINT_TO_POINTER (n)` (or similar macros).
Packit ae235b
 *
Packit ae235b
 * A good hash functions should produce
Packit ae235b
 * hash values that are evenly distributed over a fairly large range.
Packit ae235b
 * The modulus is taken with the hash table size (a prime number) to
Packit ae235b
 * find the 'bucket' to place each key into. The function should also
Packit ae235b
 * be very fast, since it is called for each key lookup.
Packit ae235b
 *
Packit ae235b
 * Note that the hash functions provided by GLib have these qualities,
Packit ae235b
 * but are not particularly robust against manufactured keys that
Packit ae235b
 * cause hash collisions. Therefore, you should consider choosing
Packit ae235b
 * a more secure hash function when using a GHashTable with keys
Packit ae235b
 * that originate in untrusted data (such as HTTP requests).
Packit ae235b
 * Using g_str_hash() in that situation might make your application
Packit ae235b
 * vulerable to
Packit ae235b
 * [Algorithmic Complexity Attacks](https://lwn.net/Articles/474912/).
Packit ae235b
 *
Packit ae235b
 * The key to choosing a good hash is unpredictability.  Even
Packit ae235b
 * cryptographic hashes are very easy to find collisions for when the
Packit ae235b
 * remainder is taken modulo a somewhat predictable prime number.  There
Packit ae235b
 * must be an element of randomness that an attacker is unable to guess.
Packit ae235b
 *
Packit ae235b
 * Returns: the hash value corresponding to the key
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GHFunc:
Packit ae235b
 * @key: a key
Packit ae235b
 * @value: the value corresponding to the key
Packit ae235b
 * @user_data: user data passed to g_hash_table_foreach()
Packit ae235b
 *
Packit ae235b
 * Specifies the type of the function passed to g_hash_table_foreach().
Packit ae235b
 * It is called with each key/value pair, together with the @user_data
Packit ae235b
 * parameter which is passed to g_hash_table_foreach().
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GHRFunc:
Packit ae235b
 * @key: a key
Packit ae235b
 * @value: the value associated with the key
Packit ae235b
 * @user_data: user data passed to g_hash_table_remove()
Packit ae235b
 *
Packit ae235b
 * Specifies the type of the function passed to
Packit ae235b
 * g_hash_table_foreach_remove(). It is called with each key/value
Packit ae235b
 * pair, together with the @user_data parameter passed to
Packit ae235b
 * g_hash_table_foreach_remove(). It should return %TRUE if the
Packit ae235b
 * key/value pair should be removed from the #GHashTable.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the key/value pair should be removed from the
Packit ae235b
 *     #GHashTable
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GEqualFunc:
Packit ae235b
 * @a: a value
Packit ae235b
 * @b: a value to compare with
Packit ae235b
 *
Packit ae235b
 * Specifies the type of a function used to test two values for
Packit ae235b
 * equality. The function should return %TRUE if both values are equal
Packit ae235b
 * and %FALSE otherwise.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @a = @b; %FALSE otherwise
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GHashTableIter:
Packit ae235b
 *
Packit ae235b
 * A GHashTableIter structure represents an iterator that can be used
Packit ae235b
 * to iterate over the elements of a #GHashTable. GHashTableIter
Packit ae235b
 * structures are typically allocated on the stack and then initialized
Packit ae235b
 * with g_hash_table_iter_init().
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_freeze:
Packit ae235b
 * @hash_table: a #GHashTable
Packit ae235b
 *
Packit ae235b
 * This function is deprecated and will be removed in the next major
Packit ae235b
 * release of GLib. It does nothing.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_thaw:
Packit ae235b
 * @hash_table: a #GHashTable
Packit ae235b
 *
Packit ae235b
 * This function is deprecated and will be removed in the next major
Packit ae235b
 * release of GLib. It does nothing.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#define HASH_TABLE_MIN_SHIFT 3  /* 1 << 3 == 8 buckets */
Packit ae235b
Packit ae235b
#define UNUSED_HASH_VALUE 0
Packit ae235b
#define TOMBSTONE_HASH_VALUE 1
Packit ae235b
#define HASH_IS_UNUSED(h_) ((h_) == UNUSED_HASH_VALUE)
Packit ae235b
#define HASH_IS_TOMBSTONE(h_) ((h_) == TOMBSTONE_HASH_VALUE)
Packit ae235b
#define HASH_IS_REAL(h_) ((h_) >= 2)
Packit ae235b
Packit ae235b
struct _GHashTable
Packit ae235b
{
Packit ae235b
  gint             size;
Packit ae235b
  gint             mod;
Packit ae235b
  guint            mask;
Packit ae235b
  gint             nnodes;
Packit ae235b
  gint             noccupied;  /* nnodes + tombstones */
Packit ae235b
Packit ae235b
  gpointer        *keys;
Packit ae235b
  guint           *hashes;
Packit ae235b
  gpointer        *values;
Packit ae235b
Packit ae235b
  GHashFunc        hash_func;
Packit ae235b
  GEqualFunc       key_equal_func;
Packit ae235b
  gint             ref_count;
Packit ae235b
#ifndef G_DISABLE_ASSERT
Packit ae235b
  /*
Packit ae235b
   * Tracks the structure of the hash table, not its contents: is only
Packit ae235b
   * incremented when a node is added or removed (is not incremented
Packit ae235b
   * when the key or data of a node is modified).
Packit ae235b
   */
Packit ae235b
  int              version;
Packit ae235b
#endif
Packit ae235b
  GDestroyNotify   key_destroy_func;
Packit ae235b
  GDestroyNotify   value_destroy_func;
Packit ae235b
};
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  GHashTable  *hash_table;
Packit ae235b
  gpointer     dummy1;
Packit ae235b
  gpointer     dummy2;
Packit ae235b
  int          position;
Packit ae235b
  gboolean     dummy3;
Packit ae235b
  int          version;
Packit ae235b
} RealIter;
Packit ae235b
Packit ae235b
G_STATIC_ASSERT (sizeof (GHashTableIter) == sizeof (RealIter));
Packit ae235b
G_STATIC_ASSERT (_g_alignof (GHashTableIter) >= _g_alignof (RealIter));
Packit ae235b
Packit ae235b
/* Each table size has an associated prime modulo (the first prime
Packit ae235b
 * lower than the table size) used to find the initial bucket. Probing
Packit ae235b
 * then works modulo 2^n. The prime modulo is necessary to get a
Packit ae235b
 * good distribution with poor hash functions.
Packit ae235b
 */
Packit ae235b
static const gint prime_mod [] =
Packit ae235b
{
Packit ae235b
  1,          /* For 1 << 0 */
Packit ae235b
  2,
Packit ae235b
  3,
Packit ae235b
  7,
Packit ae235b
  13,
Packit ae235b
  31,
Packit ae235b
  61,
Packit ae235b
  127,
Packit ae235b
  251,
Packit ae235b
  509,
Packit ae235b
  1021,
Packit ae235b
  2039,
Packit ae235b
  4093,
Packit ae235b
  8191,
Packit ae235b
  16381,
Packit ae235b
  32749,
Packit ae235b
  65521,      /* For 1 << 16 */
Packit ae235b
  131071,
Packit ae235b
  262139,
Packit ae235b
  524287,
Packit ae235b
  1048573,
Packit ae235b
  2097143,
Packit ae235b
  4194301,
Packit ae235b
  8388593,
Packit ae235b
  16777213,
Packit ae235b
  33554393,
Packit ae235b
  67108859,
Packit ae235b
  134217689,
Packit ae235b
  268435399,
Packit ae235b
  536870909,
Packit ae235b
  1073741789,
Packit ae235b
  2147483647  /* For 1 << 31 */
Packit ae235b
};
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_hash_table_set_shift (GHashTable *hash_table, gint shift)
Packit ae235b
{
Packit ae235b
  gint i;
Packit ae235b
  guint mask = 0;
Packit ae235b
Packit ae235b
  hash_table->size = 1 << shift;
Packit ae235b
  hash_table->mod  = prime_mod [shift];
Packit ae235b
Packit ae235b
  for (i = 0; i < shift; i++)
Packit ae235b
    {
Packit ae235b
      mask <<= 1;
Packit ae235b
      mask |= 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  hash_table->mask = mask;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gint
Packit ae235b
g_hash_table_find_closest_shift (gint n)
Packit ae235b
{
Packit ae235b
  gint i;
Packit ae235b
Packit ae235b
  for (i = 0; n; i++)
Packit ae235b
    n >>= 1;
Packit ae235b
Packit ae235b
  return i;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_hash_table_set_shift_from_size (GHashTable *hash_table, gint size)
Packit ae235b
{
Packit ae235b
  gint shift;
Packit ae235b
Packit ae235b
  shift = g_hash_table_find_closest_shift (size);
Packit ae235b
  shift = MAX (shift, HASH_TABLE_MIN_SHIFT);
Packit ae235b
Packit ae235b
  g_hash_table_set_shift (hash_table, shift);
Packit ae235b
}
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * g_hash_table_lookup_node:
Packit ae235b
 * @hash_table: our #GHashTable
Packit ae235b
 * @key: the key to lookup against
Packit ae235b
 * @hash_return: key hash return location
Packit ae235b
 *
Packit ae235b
 * Performs a lookup in the hash table, preserving extra information
Packit ae235b
 * usually needed for insertion.
Packit ae235b
 *
Packit ae235b
 * This function first computes the hash value of the key using the
Packit ae235b
 * user's hash function.
Packit ae235b
 *
Packit ae235b
 * If an entry in the table matching @key is found then this function
Packit ae235b
 * returns the index of that entry in the table, and if not, the
Packit ae235b
 * index of an unused node (empty or tombstone) where the key can be
Packit ae235b
 * inserted.
Packit ae235b
 *
Packit ae235b
 * The computed hash value is returned in the variable pointed to
Packit ae235b
 * by @hash_return. This is to save insertions from having to compute
Packit ae235b
 * the hash record again for the new record.
Packit ae235b
 *
Packit ae235b
 * Returns: index of the described node
Packit ae235b
 */
Packit ae235b
static inline guint
Packit ae235b
g_hash_table_lookup_node (GHashTable    *hash_table,
Packit ae235b
                          gconstpointer  key,
Packit ae235b
                          guint         *hash_return)
Packit ae235b
{
Packit ae235b
  guint node_index;
Packit ae235b
  guint node_hash;
Packit ae235b
  guint hash_value;
Packit ae235b
  guint first_tombstone = 0;
Packit ae235b
  gboolean have_tombstone = FALSE;
Packit ae235b
  guint step = 0;
Packit ae235b
Packit ae235b
  /* If this happens, then the application is probably doing too much work
Packit ae235b
   * from a destroy notifier. The alternative would be to crash any second
Packit ae235b
   * (as keys, etc. will be NULL).
Packit ae235b
   * Applications need to either use g_hash_table_destroy, or ensure the hash
Packit ae235b
   * table is empty prior to removing the last reference using g_hash_table_unref(). */
Packit ae235b
  g_assert (hash_table->ref_count > 0);
Packit ae235b
Packit ae235b
  hash_value = hash_table->hash_func (key);
Packit ae235b
  if (G_UNLIKELY (!HASH_IS_REAL (hash_value)))
Packit ae235b
    hash_value = 2;
Packit ae235b
Packit ae235b
  *hash_return = hash_value;
Packit ae235b
Packit ae235b
  node_index = hash_value % hash_table->mod;
Packit ae235b
  node_hash = hash_table->hashes[node_index];
Packit ae235b
Packit ae235b
  while (!HASH_IS_UNUSED (node_hash))
Packit ae235b
    {
Packit ae235b
      /* We first check if our full hash values
Packit ae235b
       * are equal so we can avoid calling the full-blown
Packit ae235b
       * key equality function in most cases.
Packit ae235b
       */
Packit ae235b
      if (node_hash == hash_value)
Packit ae235b
        {
Packit ae235b
          gpointer node_key = hash_table->keys[node_index];
Packit ae235b
Packit ae235b
          if (hash_table->key_equal_func)
Packit ae235b
            {
Packit ae235b
              if (hash_table->key_equal_func (node_key, key))
Packit ae235b
                return node_index;
Packit ae235b
            }
Packit ae235b
          else if (node_key == key)
Packit ae235b
            {
Packit ae235b
              return node_index;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      else if (HASH_IS_TOMBSTONE (node_hash) && !have_tombstone)
Packit ae235b
        {
Packit ae235b
          first_tombstone = node_index;
Packit ae235b
          have_tombstone = TRUE;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      step++;
Packit ae235b
      node_index += step;
Packit ae235b
      node_index &= hash_table->mask;
Packit ae235b
      node_hash = hash_table->hashes[node_index];
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (have_tombstone)
Packit ae235b
    return first_tombstone;
Packit ae235b
Packit ae235b
  return node_index;
Packit ae235b
}
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * g_hash_table_remove_node:
Packit ae235b
 * @hash_table: our #GHashTable
Packit ae235b
 * @node: pointer to node to remove
Packit ae235b
 * @notify: %TRUE if the destroy notify handlers are to be called
Packit ae235b
 *
Packit ae235b
 * Removes a node from the hash table and updates the node count.
Packit ae235b
 * The node is replaced by a tombstone. No table resize is performed.
Packit ae235b
 *
Packit ae235b
 * If @notify is %TRUE then the destroy notify functions are called
Packit ae235b
 * for the key and value of the hash node.
Packit ae235b
 */
Packit ae235b
static void
Packit ae235b
g_hash_table_remove_node (GHashTable   *hash_table,
Packit ae235b
                          gint          i,
Packit ae235b
                          gboolean      notify)
Packit ae235b
{
Packit ae235b
  gpointer key;
Packit ae235b
  gpointer value;
Packit ae235b
Packit ae235b
  key = hash_table->keys[i];
Packit ae235b
  value = hash_table->values[i];
Packit ae235b
Packit ae235b
  /* Erect tombstone */
Packit ae235b
  hash_table->hashes[i] = TOMBSTONE_HASH_VALUE;
Packit ae235b
Packit ae235b
  /* Be GC friendly */
Packit ae235b
  hash_table->keys[i] = NULL;
Packit ae235b
  hash_table->values[i] = NULL;
Packit ae235b
Packit ae235b
  hash_table->nnodes--;
Packit ae235b
Packit ae235b
  if (notify && hash_table->key_destroy_func)
Packit ae235b
    hash_table->key_destroy_func (key);
Packit ae235b
Packit ae235b
  if (notify && hash_table->value_destroy_func)
Packit ae235b
    hash_table->value_destroy_func (value);
Packit ae235b
Packit ae235b
}
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * g_hash_table_remove_all_nodes:
Packit ae235b
 * @hash_table: our #GHashTable
Packit ae235b
 * @notify: %TRUE if the destroy notify handlers are to be called
Packit ae235b
 *
Packit ae235b
 * Removes all nodes from the table.  Since this may be a precursor to
Packit ae235b
 * freeing the table entirely, no resize is performed.
Packit ae235b
 *
Packit ae235b
 * If @notify is %TRUE then the destroy notify functions are called
Packit ae235b
 * for the key and value of the hash node.
Packit ae235b
 */
Packit ae235b
static void
Packit ae235b
g_hash_table_remove_all_nodes (GHashTable *hash_table,
Packit ae235b
                               gboolean    notify,
Packit ae235b
                               gboolean    destruction)
Packit ae235b
{
Packit ae235b
  int i;
Packit ae235b
  gpointer key;
Packit ae235b
  gpointer value;
Packit ae235b
  gint old_size;
Packit ae235b
  gpointer *old_keys;
Packit ae235b
  gpointer *old_values;
Packit ae235b
  guint    *old_hashes;
Packit ae235b
Packit ae235b
  /* If the hash table is already empty, there is nothing to be done. */
Packit ae235b
  if (hash_table->nnodes == 0)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  hash_table->nnodes = 0;
Packit ae235b
  hash_table->noccupied = 0;
Packit ae235b
Packit ae235b
  if (!notify ||
Packit ae235b
      (hash_table->key_destroy_func == NULL &&
Packit ae235b
       hash_table->value_destroy_func == NULL))
Packit ae235b
    {
Packit ae235b
      if (!destruction)
Packit ae235b
        {
Packit ae235b
          memset (hash_table->hashes, 0, hash_table->size * sizeof (guint));
Packit ae235b
          memset (hash_table->keys, 0, hash_table->size * sizeof (gpointer));
Packit ae235b
          memset (hash_table->values, 0, hash_table->size * sizeof (gpointer));
Packit ae235b
        }
Packit ae235b
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Keep the old storage space around to iterate over it. */
Packit ae235b
  old_size = hash_table->size;
Packit ae235b
  old_keys   = hash_table->keys;
Packit ae235b
  old_values = hash_table->values;
Packit ae235b
  old_hashes = hash_table->hashes;
Packit ae235b
Packit ae235b
  /* Now create a new storage space; If the table is destroyed we can use the
Packit ae235b
   * shortcut of not creating a new storage. This saves the allocation at the
Packit ae235b
   * cost of not allowing any recursive access.
Packit ae235b
   * However, the application doesn't own any reference anymore, so access
Packit ae235b
   * is not allowed. If accesses are done, then either an assert or crash
Packit ae235b
   * *will* happen. */
Packit ae235b
  g_hash_table_set_shift (hash_table, HASH_TABLE_MIN_SHIFT);
Packit ae235b
  if (!destruction)
Packit ae235b
    {
Packit ae235b
      hash_table->keys   = g_new0 (gpointer, hash_table->size);
Packit ae235b
      hash_table->values = hash_table->keys;
Packit ae235b
      hash_table->hashes = g_new0 (guint, hash_table->size);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      hash_table->keys   = NULL;
Packit ae235b
      hash_table->values = NULL;
Packit ae235b
      hash_table->hashes = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  for (i = 0; i < old_size; i++)
Packit ae235b
    {
Packit ae235b
      if (HASH_IS_REAL (old_hashes[i]))
Packit ae235b
        {
Packit ae235b
          key = old_keys[i];
Packit ae235b
          value = old_values[i];
Packit ae235b
Packit ae235b
          old_hashes[i] = UNUSED_HASH_VALUE;
Packit ae235b
          old_keys[i] = NULL;
Packit ae235b
          old_values[i] = NULL;
Packit ae235b
Packit ae235b
          if (hash_table->key_destroy_func != NULL)
Packit ae235b
            hash_table->key_destroy_func (key);
Packit ae235b
Packit ae235b
          if (hash_table->value_destroy_func != NULL)
Packit ae235b
            hash_table->value_destroy_func (value);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Destroy old storage space. */
Packit ae235b
  if (old_keys != old_values)
Packit ae235b
    g_free (old_values);
Packit ae235b
Packit ae235b
  g_free (old_keys);
Packit ae235b
  g_free (old_hashes);
Packit ae235b
}
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * g_hash_table_resize:
Packit ae235b
 * @hash_table: our #GHashTable
Packit ae235b
 *
Packit ae235b
 * Resizes the hash table to the optimal size based on the number of
Packit ae235b
 * nodes currently held. If you call this function then a resize will
Packit ae235b
 * occur, even if one does not need to occur.
Packit ae235b
 * Use g_hash_table_maybe_resize() instead.
Packit ae235b
 *
Packit ae235b
 * This function may "resize" the hash table to its current size, with
Packit ae235b
 * the side effect of cleaning up tombstones and otherwise optimizing
Packit ae235b
 * the probe sequences.
Packit ae235b
 */
Packit ae235b
static void
Packit ae235b
g_hash_table_resize (GHashTable *hash_table)
Packit ae235b
{
Packit ae235b
  gpointer *new_keys;
Packit ae235b
  gpointer *new_values;
Packit ae235b
  guint *new_hashes;
Packit ae235b
  gint old_size;
Packit ae235b
  gint i;
Packit ae235b
Packit ae235b
  old_size = hash_table->size;
Packit ae235b
  g_hash_table_set_shift_from_size (hash_table, hash_table->nnodes * 2);
Packit ae235b
Packit ae235b
  new_keys = g_new0 (gpointer, hash_table->size);
Packit ae235b
  if (hash_table->keys == hash_table->values)
Packit ae235b
    new_values = new_keys;
Packit ae235b
  else
Packit ae235b
    new_values = g_new0 (gpointer, hash_table->size);
Packit ae235b
  new_hashes = g_new0 (guint, hash_table->size);
Packit ae235b
Packit ae235b
  for (i = 0; i < old_size; i++)
Packit ae235b
    {
Packit ae235b
      guint node_hash = hash_table->hashes[i];
Packit ae235b
      guint hash_val;
Packit ae235b
      guint step = 0;
Packit ae235b
Packit ae235b
      if (!HASH_IS_REAL (node_hash))
Packit ae235b
        continue;
Packit ae235b
Packit ae235b
      hash_val = node_hash % hash_table->mod;
Packit ae235b
Packit ae235b
      while (!HASH_IS_UNUSED (new_hashes[hash_val]))
Packit ae235b
        {
Packit ae235b
          step++;
Packit ae235b
          hash_val += step;
Packit ae235b
          hash_val &= hash_table->mask;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      new_hashes[hash_val] = hash_table->hashes[i];
Packit ae235b
      new_keys[hash_val] = hash_table->keys[i];
Packit ae235b
      new_values[hash_val] = hash_table->values[i];
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (hash_table->keys != hash_table->values)
Packit ae235b
    g_free (hash_table->values);
Packit ae235b
Packit ae235b
  g_free (hash_table->keys);
Packit ae235b
  g_free (hash_table->hashes);
Packit ae235b
Packit ae235b
  hash_table->keys = new_keys;
Packit ae235b
  hash_table->values = new_values;
Packit ae235b
  hash_table->hashes = new_hashes;
Packit ae235b
Packit ae235b
  hash_table->noccupied = hash_table->nnodes;
Packit ae235b
}
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * g_hash_table_maybe_resize:
Packit ae235b
 * @hash_table: our #GHashTable
Packit ae235b
 *
Packit ae235b
 * Resizes the hash table, if needed.
Packit ae235b
 *
Packit ae235b
 * Essentially, calls g_hash_table_resize() if the table has strayed
Packit ae235b
 * too far from its ideal size for its number of nodes.
Packit ae235b
 */
Packit ae235b
static inline void
Packit ae235b
g_hash_table_maybe_resize (GHashTable *hash_table)
Packit ae235b
{
Packit ae235b
  gint noccupied = hash_table->noccupied;
Packit ae235b
  gint size = hash_table->size;
Packit ae235b
Packit ae235b
  if ((size > hash_table->nnodes * 4 && size > 1 << HASH_TABLE_MIN_SHIFT) ||
Packit ae235b
      (size <= noccupied + (noccupied / 16)))
Packit ae235b
    g_hash_table_resize (hash_table);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_new:
Packit ae235b
 * @hash_func: a function to create a hash value from a key
Packit ae235b
 * @key_equal_func: a function to check two keys for equality
Packit ae235b
 *
Packit ae235b
 * Creates a new #GHashTable with a reference count of 1.
Packit ae235b
 *
Packit ae235b
 * Hash values returned by @hash_func are used to determine where keys
Packit ae235b
 * are stored within the #GHashTable data structure. The g_direct_hash(),
Packit ae235b
 * g_int_hash(), g_int64_hash(), g_double_hash() and g_str_hash()
Packit ae235b
 * functions are provided for some common types of keys.
Packit ae235b
 * If @hash_func is %NULL, g_direct_hash() is used.
Packit ae235b
 *
Packit ae235b
 * @key_equal_func is used when looking up keys in the #GHashTable.
Packit ae235b
 * The g_direct_equal(), g_int_equal(), g_int64_equal(), g_double_equal()
Packit ae235b
 * and g_str_equal() functions are provided for the most common types
Packit ae235b
 * of keys. If @key_equal_func is %NULL, keys are compared directly in
Packit ae235b
 * a similar fashion to g_direct_equal(), but without the overhead of
Packit ae235b
 * a function call. @key_equal_func is called with the key from the hash table
Packit ae235b
 * as its first parameter, and the user-provided key to check against as
Packit ae235b
 * its second.
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GHashTable
Packit ae235b
 */
Packit ae235b
GHashTable *
Packit ae235b
g_hash_table_new (GHashFunc  hash_func,
Packit ae235b
                  GEqualFunc key_equal_func)
Packit ae235b
{
Packit ae235b
  return g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_new_full:
Packit ae235b
 * @hash_func: a function to create a hash value from a key
Packit ae235b
 * @key_equal_func: a function to check two keys for equality
Packit ae235b
 * @key_destroy_func: (nullable): a function to free the memory allocated for the key
Packit ae235b
 *     used when removing the entry from the #GHashTable, or %NULL
Packit ae235b
 *     if you don't want to supply such a function.
Packit ae235b
 * @value_destroy_func: (nullable): a function to free the memory allocated for the
Packit ae235b
 *     value used when removing the entry from the #GHashTable, or %NULL
Packit ae235b
 *     if you don't want to supply such a function.
Packit ae235b
 *
Packit ae235b
 * Creates a new #GHashTable like g_hash_table_new() with a reference
Packit ae235b
 * count of 1 and allows to specify functions to free the memory
Packit ae235b
 * allocated for the key and value that get called when removing the
Packit ae235b
 * entry from the #GHashTable.
Packit ae235b
 *
Packit ae235b
 * Since version 2.42 it is permissible for destroy notify functions to
Packit ae235b
 * recursively remove further items from the hash table. This is only
Packit ae235b
 * permissible if the application still holds a reference to the hash table.
Packit ae235b
 * This means that you may need to ensure that the hash table is empty by
Packit ae235b
 * calling g_hash_table_remove_all() before releasing the last reference using
Packit ae235b
 * g_hash_table_unref().
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GHashTable
Packit ae235b
 */
Packit ae235b
GHashTable *
Packit ae235b
g_hash_table_new_full (GHashFunc      hash_func,
Packit ae235b
                       GEqualFunc     key_equal_func,
Packit ae235b
                       GDestroyNotify key_destroy_func,
Packit ae235b
                       GDestroyNotify value_destroy_func)
Packit ae235b
{
Packit ae235b
  GHashTable *hash_table;
Packit ae235b
Packit ae235b
  hash_table = g_slice_new (GHashTable);
Packit ae235b
  g_hash_table_set_shift (hash_table, HASH_TABLE_MIN_SHIFT);
Packit ae235b
  hash_table->nnodes             = 0;
Packit ae235b
  hash_table->noccupied          = 0;
Packit ae235b
  hash_table->hash_func          = hash_func ? hash_func : g_direct_hash;
Packit ae235b
  hash_table->key_equal_func     = key_equal_func;
Packit ae235b
  hash_table->ref_count          = 1;
Packit ae235b
#ifndef G_DISABLE_ASSERT
Packit ae235b
  hash_table->version            = 0;
Packit ae235b
#endif
Packit ae235b
  hash_table->key_destroy_func   = key_destroy_func;
Packit ae235b
  hash_table->value_destroy_func = value_destroy_func;
Packit ae235b
  hash_table->keys               = g_new0 (gpointer, hash_table->size);
Packit ae235b
  hash_table->values             = hash_table->keys;
Packit ae235b
  hash_table->hashes             = g_new0 (guint, hash_table->size);
Packit ae235b
Packit ae235b
  return hash_table;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_iter_init:
Packit ae235b
 * @iter: an uninitialized #GHashTableIter
Packit ae235b
 * @hash_table: a #GHashTable
Packit ae235b
 *
Packit ae235b
 * Initializes a key/value pair iterator and associates it with
Packit ae235b
 * @hash_table. Modifying the hash table after calling this function
Packit ae235b
 * invalidates the returned iterator.
Packit ae235b
 * |[
Packit ae235b
 * GHashTableIter iter;
Packit ae235b
 * gpointer key, value;
Packit ae235b
 *
Packit ae235b
 * g_hash_table_iter_init (&iter, hash_table);
Packit ae235b
 * while (g_hash_table_iter_next (&iter, &key, &value))
Packit ae235b
 *   {
Packit ae235b
 *     // do something with key and value
Packit ae235b
 *   }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_hash_table_iter_init (GHashTableIter *iter,
Packit ae235b
                        GHashTable     *hash_table)
Packit ae235b
{
Packit ae235b
  RealIter *ri = (RealIter *) iter;
Packit ae235b
Packit ae235b
  g_return_if_fail (iter != NULL);
Packit ae235b
  g_return_if_fail (hash_table != NULL);
Packit ae235b
Packit ae235b
  ri->hash_table = hash_table;
Packit ae235b
  ri->position = -1;
Packit ae235b
#ifndef G_DISABLE_ASSERT
Packit ae235b
  ri->version = hash_table->version;
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_iter_next:
Packit ae235b
 * @iter: an initialized #GHashTableIter
Packit ae235b
 * @key: (out) (optional): a location to store the key
Packit ae235b
 * @value: (out) (optional) (nullable): a location to store the value
Packit ae235b
 *
Packit ae235b
 * Advances @iter and retrieves the key and/or value that are now
Packit ae235b
 * pointed to as a result of this advancement. If %FALSE is returned,
Packit ae235b
 * @key and @value are not set, and the iterator becomes invalid.
Packit ae235b
 *
Packit ae235b
 * Returns: %FALSE if the end of the #GHashTable has been reached.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_hash_table_iter_next (GHashTableIter *iter,
Packit ae235b
                        gpointer       *key,
Packit ae235b
                        gpointer       *value)
Packit ae235b
{
Packit ae235b
  RealIter *ri = (RealIter *) iter;
Packit ae235b
  gint position;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (iter != NULL, FALSE);
Packit ae235b
#ifndef G_DISABLE_ASSERT
Packit ae235b
  g_return_val_if_fail (ri->version == ri->hash_table->version, FALSE);
Packit ae235b
#endif
Packit ae235b
  g_return_val_if_fail (ri->position < ri->hash_table->size, FALSE);
Packit ae235b
Packit ae235b
  position = ri->position;
Packit ae235b
Packit ae235b
  do
Packit ae235b
    {
Packit ae235b
      position++;
Packit ae235b
      if (position >= ri->hash_table->size)
Packit ae235b
        {
Packit ae235b
          ri->position = position;
Packit ae235b
          return FALSE;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  while (!HASH_IS_REAL (ri->hash_table->hashes[position]));
Packit ae235b
Packit ae235b
  if (key != NULL)
Packit ae235b
    *key = ri->hash_table->keys[position];
Packit ae235b
  if (value != NULL)
Packit ae235b
    *value = ri->hash_table->values[position];
Packit ae235b
Packit ae235b
  ri->position = position;
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_iter_get_hash_table:
Packit ae235b
 * @iter: an initialized #GHashTableIter
Packit ae235b
 *
Packit ae235b
 * Returns the #GHashTable associated with @iter.
Packit ae235b
 *
Packit ae235b
 * Returns: the #GHashTable associated with @iter.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
GHashTable *
Packit ae235b
g_hash_table_iter_get_hash_table (GHashTableIter *iter)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (iter != NULL, NULL);
Packit ae235b
Packit ae235b
  return ((RealIter *) iter)->hash_table;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
iter_remove_or_steal (RealIter *ri, gboolean notify)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (ri != NULL);
Packit ae235b
#ifndef G_DISABLE_ASSERT
Packit ae235b
  g_return_if_fail (ri->version == ri->hash_table->version);
Packit ae235b
#endif
Packit ae235b
  g_return_if_fail (ri->position >= 0);
Packit ae235b
  g_return_if_fail (ri->position < ri->hash_table->size);
Packit ae235b
Packit ae235b
  g_hash_table_remove_node (ri->hash_table, ri->position, notify);
Packit ae235b
Packit ae235b
#ifndef G_DISABLE_ASSERT
Packit ae235b
  ri->version++;
Packit ae235b
  ri->hash_table->version++;
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_iter_remove:
Packit ae235b
 * @iter: an initialized #GHashTableIter
Packit ae235b
 *
Packit ae235b
 * Removes the key/value pair currently pointed to by the iterator
Packit ae235b
 * from its associated #GHashTable. Can only be called after
Packit ae235b
 * g_hash_table_iter_next() returned %TRUE, and cannot be called
Packit ae235b
 * more than once for the same key/value pair.
Packit ae235b
 *
Packit ae235b
 * If the #GHashTable was created using g_hash_table_new_full(),
Packit ae235b
 * the key and value are freed using the supplied destroy functions,
Packit ae235b
 * otherwise you have to make sure that any dynamically allocated
Packit ae235b
 * values are freed yourself.
Packit ae235b
 *
Packit ae235b
 * It is safe to continue iterating the #GHashTable afterward:
Packit ae235b
 * |[
Packit ae235b
 * while (g_hash_table_iter_next (&iter, &key, &value))
Packit ae235b
 *   {
Packit ae235b
 *     if (condition)
Packit ae235b
 *       g_hash_table_iter_remove (&iter);
Packit ae235b
 *   }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_hash_table_iter_remove (GHashTableIter *iter)
Packit ae235b
{
Packit ae235b
  iter_remove_or_steal ((RealIter *) iter, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * g_hash_table_insert_node:
Packit ae235b
 * @hash_table: our #GHashTable
Packit ae235b
 * @node_index: pointer to node to insert/replace
Packit ae235b
 * @key_hash: key hash
Packit ae235b
 * @key: (nullable): key to replace with, or %NULL
Packit ae235b
 * @value: value to replace with
Packit ae235b
 * @keep_new_key: whether to replace the key in the node with @key
Packit ae235b
 * @reusing_key: whether @key was taken out of the existing node
Packit ae235b
 *
Packit ae235b
 * Inserts a value at @node_index in the hash table and updates it.
Packit ae235b
 *
Packit ae235b
 * If @key has been taken out of the existing node (ie it is not
Packit ae235b
 * passed in via a g_hash_table_insert/replace) call, then @reusing_key
Packit ae235b
 * should be %TRUE.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the key did not exist yet
Packit ae235b
 */
Packit ae235b
static gboolean
Packit ae235b
g_hash_table_insert_node (GHashTable *hash_table,
Packit ae235b
                          guint       node_index,
Packit ae235b
                          guint       key_hash,
Packit ae235b
                          gpointer    new_key,
Packit ae235b
                          gpointer    new_value,
Packit ae235b
                          gboolean    keep_new_key,
Packit ae235b
                          gboolean    reusing_key)
Packit ae235b
{
Packit ae235b
  gboolean already_exists;
Packit ae235b
  guint old_hash;
Packit ae235b
  gpointer key_to_free = NULL;
Packit ae235b
  gpointer value_to_free = NULL;
Packit ae235b
Packit ae235b
  old_hash = hash_table->hashes[node_index];
Packit ae235b
  already_exists = HASH_IS_REAL (old_hash);
Packit ae235b
Packit ae235b
  /* Proceed in three steps.  First, deal with the key because it is the
Packit ae235b
   * most complicated.  Then consider if we need to split the table in
Packit ae235b
   * two (because writing the value will result in the set invariant
Packit ae235b
   * becoming broken).  Then deal with the value.
Packit ae235b
   *
Packit ae235b
   * There are three cases for the key:
Packit ae235b
   *
Packit ae235b
   *  - entry already exists in table, reusing key:
Packit ae235b
   *    free the just-passed-in new_key and use the existing value
Packit ae235b
   *
Packit ae235b
   *  - entry already exists in table, not reusing key:
Packit ae235b
   *    free the entry in the table, use the new key
Packit ae235b
   *
Packit ae235b
   *  - entry not already in table:
Packit ae235b
   *    use the new key, free nothing
Packit ae235b
   *
Packit ae235b
   * We update the hash at the same time...
Packit ae235b
   */
Packit ae235b
  if (already_exists)
Packit ae235b
    {
Packit ae235b
      /* Note: we must record the old value before writing the new key
Packit ae235b
       * because we might change the value in the event that the two
Packit ae235b
       * arrays are shared.
Packit ae235b
       */
Packit ae235b
      value_to_free = hash_table->values[node_index];
Packit ae235b
Packit ae235b
      if (keep_new_key)
Packit ae235b
        {
Packit ae235b
          key_to_free = hash_table->keys[node_index];
Packit ae235b
          hash_table->keys[node_index] = new_key;
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        key_to_free = new_key;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      hash_table->hashes[node_index] = key_hash;
Packit ae235b
      hash_table->keys[node_index] = new_key;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Step two: check if the value that we are about to write to the
Packit ae235b
   * table is the same as the key in the same position.  If it's not,
Packit ae235b
   * split the table.
Packit ae235b
   */
Packit ae235b
  if (G_UNLIKELY (hash_table->keys == hash_table->values && hash_table->keys[node_index] != new_value))
Packit ae235b
    hash_table->values = g_memdup (hash_table->keys, sizeof (gpointer) * hash_table->size);
Packit ae235b
Packit ae235b
  /* Step 3: Actually do the write */
Packit ae235b
  hash_table->values[node_index] = new_value;
Packit ae235b
Packit ae235b
  /* Now, the bookkeeping... */
Packit ae235b
  if (!already_exists)
Packit ae235b
    {
Packit ae235b
      hash_table->nnodes++;
Packit ae235b
Packit ae235b
      if (HASH_IS_UNUSED (old_hash))
Packit ae235b
        {
Packit ae235b
          /* We replaced an empty node, and not a tombstone */
Packit ae235b
          hash_table->noccupied++;
Packit ae235b
          g_hash_table_maybe_resize (hash_table);
Packit ae235b
        }
Packit ae235b
Packit ae235b
#ifndef G_DISABLE_ASSERT
Packit ae235b
      hash_table->version++;
Packit ae235b
#endif
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (already_exists)
Packit ae235b
    {
Packit ae235b
      if (hash_table->key_destroy_func && !reusing_key)
Packit ae235b
        (* hash_table->key_destroy_func) (key_to_free);
Packit ae235b
      if (hash_table->value_destroy_func)
Packit ae235b
        (* hash_table->value_destroy_func) (value_to_free);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return !already_exists;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_iter_replace:
Packit ae235b
 * @iter: an initialized #GHashTableIter
Packit ae235b
 * @value: the value to replace with
Packit ae235b
 *
Packit ae235b
 * Replaces the value currently pointed to by the iterator
Packit ae235b
 * from its associated #GHashTable. Can only be called after
Packit ae235b
 * g_hash_table_iter_next() returned %TRUE.
Packit ae235b
 *
Packit ae235b
 * If you supplied a @value_destroy_func when creating the
Packit ae235b
 * #GHashTable, the old value is freed using that function.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_hash_table_iter_replace (GHashTableIter *iter,
Packit ae235b
                           gpointer        value)
Packit ae235b
{
Packit ae235b
  RealIter *ri;
Packit ae235b
  guint node_hash;
Packit ae235b
  gpointer key;
Packit ae235b
Packit ae235b
  ri = (RealIter *) iter;
Packit ae235b
Packit ae235b
  g_return_if_fail (ri != NULL);
Packit ae235b
#ifndef G_DISABLE_ASSERT
Packit ae235b
  g_return_if_fail (ri->version == ri->hash_table->version);
Packit ae235b
#endif
Packit ae235b
  g_return_if_fail (ri->position >= 0);
Packit ae235b
  g_return_if_fail (ri->position < ri->hash_table->size);
Packit ae235b
Packit ae235b
  node_hash = ri->hash_table->hashes[ri->position];
Packit ae235b
  key = ri->hash_table->keys[ri->position];
Packit ae235b
Packit ae235b
  g_hash_table_insert_node (ri->hash_table, ri->position, node_hash, key, value, TRUE, TRUE);
Packit ae235b
Packit ae235b
#ifndef G_DISABLE_ASSERT
Packit ae235b
  ri->version++;
Packit ae235b
  ri->hash_table->version++;
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_iter_steal:
Packit ae235b
 * @iter: an initialized #GHashTableIter
Packit ae235b
 *
Packit ae235b
 * Removes the key/value pair currently pointed to by the
Packit ae235b
 * iterator from its associated #GHashTable, without calling
Packit ae235b
 * the key and value destroy functions. Can only be called
Packit ae235b
 * after g_hash_table_iter_next() returned %TRUE, and cannot
Packit ae235b
 * be called more than once for the same key/value pair.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_hash_table_iter_steal (GHashTableIter *iter)
Packit ae235b
{
Packit ae235b
  iter_remove_or_steal ((RealIter *) iter, FALSE);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_ref:
Packit ae235b
 * @hash_table: a valid #GHashTable
Packit ae235b
 *
Packit ae235b
 * Atomically increments the reference count of @hash_table by one.
Packit ae235b
 * This function is MT-safe and may be called from any thread.
Packit ae235b
 *
Packit ae235b
 * Returns: the passed in #GHashTable
Packit ae235b
 *
Packit ae235b
 * Since: 2.10
Packit ae235b
 */
Packit ae235b
GHashTable *
Packit ae235b
g_hash_table_ref (GHashTable *hash_table)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (hash_table != NULL, NULL);
Packit ae235b
Packit ae235b
  g_atomic_int_inc (&hash_table->ref_count);
Packit ae235b
Packit ae235b
  return hash_table;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_unref:
Packit ae235b
 * @hash_table: a valid #GHashTable
Packit ae235b
 *
Packit ae235b
 * Atomically decrements the reference count of @hash_table by one.
Packit ae235b
 * If the reference count drops to 0, all keys and values will be
Packit ae235b
 * destroyed, and all memory allocated by the hash table is released.
Packit ae235b
 * This function is MT-safe and may be called from any thread.
Packit ae235b
 *
Packit ae235b
 * Since: 2.10
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_hash_table_unref (GHashTable *hash_table)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (hash_table != NULL);
Packit ae235b
Packit ae235b
  if (g_atomic_int_dec_and_test (&hash_table->ref_count))
Packit ae235b
    {
Packit ae235b
      g_hash_table_remove_all_nodes (hash_table, TRUE, TRUE);
Packit ae235b
      if (hash_table->keys != hash_table->values)
Packit ae235b
        g_free (hash_table->values);
Packit ae235b
      g_free (hash_table->keys);
Packit ae235b
      g_free (hash_table->hashes);
Packit ae235b
      g_slice_free (GHashTable, hash_table);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_destroy:
Packit ae235b
 * @hash_table: a #GHashTable
Packit ae235b
 *
Packit ae235b
 * Destroys all keys and values in the #GHashTable and decrements its
Packit ae235b
 * reference count by 1. If keys and/or values are dynamically allocated,
Packit ae235b
 * you should either free them first or create the #GHashTable with destroy
Packit ae235b
 * notifiers using g_hash_table_new_full(). In the latter case the destroy
Packit ae235b
 * functions you supplied will be called on all keys and values during the
Packit ae235b
 * destruction phase.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_hash_table_destroy (GHashTable *hash_table)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (hash_table != NULL);
Packit ae235b
Packit ae235b
  g_hash_table_remove_all (hash_table);
Packit ae235b
  g_hash_table_unref (hash_table);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_lookup:
Packit ae235b
 * @hash_table: a #GHashTable
Packit ae235b
 * @key: the key to look up
Packit ae235b
 *
Packit ae235b
 * Looks up a key in a #GHashTable. Note that this function cannot
Packit ae235b
 * distinguish between a key that is not present and one which is present
Packit ae235b
 * and has the value %NULL. If you need this distinction, use
Packit ae235b
 * g_hash_table_lookup_extended().
Packit ae235b
 *
Packit ae235b
 * Returns: (nullable): the associated value, or %NULL if the key is not found
Packit ae235b
 */
Packit ae235b
gpointer
Packit ae235b
g_hash_table_lookup (GHashTable    *hash_table,
Packit ae235b
                     gconstpointer  key)
Packit ae235b
{
Packit ae235b
  guint node_index;
Packit ae235b
  guint node_hash;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (hash_table != NULL, NULL);
Packit ae235b
Packit ae235b
  node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
Packit ae235b
Packit ae235b
  return HASH_IS_REAL (hash_table->hashes[node_index])
Packit ae235b
    ? hash_table->values[node_index]
Packit ae235b
    : NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_lookup_extended:
Packit ae235b
 * @hash_table: a #GHashTable
Packit ae235b
 * @lookup_key: the key to look up
Packit ae235b
 * @orig_key: (out) (optional): return location for the original key
Packit ae235b
 * @value: (out) (optional) (nullable): return location for the value associated
Packit ae235b
 * with the key
Packit ae235b
 *
Packit ae235b
 * Looks up a key in the #GHashTable, returning the original key and the
Packit ae235b
 * associated value and a #gboolean which is %TRUE if the key was found. This
Packit ae235b
 * is useful if you need to free the memory allocated for the original key,
Packit ae235b
 * for example before calling g_hash_table_remove().
Packit ae235b
 *
Packit ae235b
 * You can actually pass %NULL for @lookup_key to test
Packit ae235b
 * whether the %NULL key exists, provided the hash and equal functions
Packit ae235b
 * of @hash_table are %NULL-safe.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the key was found in the #GHashTable
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_hash_table_lookup_extended (GHashTable    *hash_table,
Packit ae235b
                              gconstpointer  lookup_key,
Packit ae235b
                              gpointer      *orig_key,
Packit ae235b
                              gpointer      *value)
Packit ae235b
{
Packit ae235b
  guint node_index;
Packit ae235b
  guint node_hash;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (hash_table != NULL, FALSE);
Packit ae235b
Packit ae235b
  node_index = g_hash_table_lookup_node (hash_table, lookup_key, &node_hash);
Packit ae235b
Packit ae235b
  if (!HASH_IS_REAL (hash_table->hashes[node_index]))
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  if (orig_key)
Packit ae235b
    *orig_key = hash_table->keys[node_index];
Packit ae235b
Packit ae235b
  if (value)
Packit ae235b
    *value = hash_table->values[node_index];
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * g_hash_table_insert_internal:
Packit ae235b
 * @hash_table: our #GHashTable
Packit ae235b
 * @key: the key to insert
Packit ae235b
 * @value: the value to insert
Packit ae235b
 * @keep_new_key: if %TRUE and this key already exists in the table
Packit ae235b
 *   then call the destroy notify function on the old key.  If %FALSE
Packit ae235b
 *   then call the destroy notify function on the new key.
Packit ae235b
 *
Packit ae235b
 * Implements the common logic for the g_hash_table_insert() and
Packit ae235b
 * g_hash_table_replace() functions.
Packit ae235b
 *
Packit ae235b
 * Do a lookup of @key. If it is found, replace it with the new
Packit ae235b
 * @value (and perhaps the new @key). If it is not found, create
Packit ae235b
 * a new node.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the key did not exist yet
Packit ae235b
 */
Packit ae235b
static gboolean
Packit ae235b
g_hash_table_insert_internal (GHashTable *hash_table,
Packit ae235b
                              gpointer    key,
Packit ae235b
                              gpointer    value,
Packit ae235b
                              gboolean    keep_new_key)
Packit ae235b
{
Packit ae235b
  guint key_hash;
Packit ae235b
  guint node_index;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (hash_table != NULL, FALSE);
Packit ae235b
Packit ae235b
  node_index = g_hash_table_lookup_node (hash_table, key, &key_hash);
Packit ae235b
Packit ae235b
  return g_hash_table_insert_node (hash_table, node_index, key_hash, key, value, keep_new_key, FALSE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_insert:
Packit ae235b
 * @hash_table: a #GHashTable
Packit ae235b
 * @key: a key to insert
Packit ae235b
 * @value: the value to associate with the key
Packit ae235b
 *
Packit ae235b
 * Inserts a new key and value into a #GHashTable.
Packit ae235b
 *
Packit ae235b
 * If the key already exists in the #GHashTable its current
Packit ae235b
 * value is replaced with the new value. If you supplied a
Packit ae235b
 * @value_destroy_func when creating the #GHashTable, the old
Packit ae235b
 * value is freed using that function. If you supplied a
Packit ae235b
 * @key_destroy_func when creating the #GHashTable, the passed
Packit ae235b
 * key is freed using that function.
Packit ae235b
 *
Packit ae235b
 * Starting from GLib 2.40, this function returns a boolean value to
Packit ae235b
 * indicate whether the newly added value was already in the hash table
Packit ae235b
 * or not.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the key did not exist yet
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_hash_table_insert (GHashTable *hash_table,
Packit ae235b
                     gpointer    key,
Packit ae235b
                     gpointer    value)
Packit ae235b
{
Packit ae235b
  return g_hash_table_insert_internal (hash_table, key, value, FALSE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_replace:
Packit ae235b
 * @hash_table: a #GHashTable
Packit ae235b
 * @key: a key to insert
Packit ae235b
 * @value: the value to associate with the key
Packit ae235b
 *
Packit ae235b
 * Inserts a new key and value into a #GHashTable similar to
Packit ae235b
 * g_hash_table_insert(). The difference is that if the key
Packit ae235b
 * already exists in the #GHashTable, it gets replaced by the
Packit ae235b
 * new key. If you supplied a @value_destroy_func when creating
Packit ae235b
 * the #GHashTable, the old value is freed using that function.
Packit ae235b
 * If you supplied a @key_destroy_func when creating the
Packit ae235b
 * #GHashTable, the old key is freed using that function.
Packit ae235b
 *
Packit ae235b
 * Starting from GLib 2.40, this function returns a boolean value to
Packit ae235b
 * indicate whether the newly added value was already in the hash table
Packit ae235b
 * or not.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the key did not exist yet
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_hash_table_replace (GHashTable *hash_table,
Packit ae235b
                      gpointer    key,
Packit ae235b
                      gpointer    value)
Packit ae235b
{
Packit ae235b
  return g_hash_table_insert_internal (hash_table, key, value, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_add:
Packit ae235b
 * @hash_table: a #GHashTable
Packit ae235b
 * @key: a key to insert
Packit ae235b
 *
Packit ae235b
 * This is a convenience function for using a #GHashTable as a set.  It
Packit ae235b
 * is equivalent to calling g_hash_table_replace() with @key as both the
Packit ae235b
 * key and the value.
Packit ae235b
 *
Packit ae235b
 * When a hash table only ever contains keys that have themselves as the
Packit ae235b
 * corresponding value it is able to be stored more efficiently.  See
Packit ae235b
 * the discussion in the section description.
Packit ae235b
 *
Packit ae235b
 * Starting from GLib 2.40, this function returns a boolean value to
Packit ae235b
 * indicate whether the newly added value was already in the hash table
Packit ae235b
 * or not.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the key did not exist yet
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_hash_table_add (GHashTable *hash_table,
Packit ae235b
                  gpointer    key)
Packit ae235b
{
Packit ae235b
  return g_hash_table_insert_internal (hash_table, key, key, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_contains:
Packit ae235b
 * @hash_table: a #GHashTable
Packit ae235b
 * @key: a key to check
Packit ae235b
 *
Packit ae235b
 * Checks if @key is in @hash_table.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @key is in @hash_table, %FALSE otherwise.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_hash_table_contains (GHashTable    *hash_table,
Packit ae235b
                       gconstpointer  key)
Packit ae235b
{
Packit ae235b
  guint node_index;
Packit ae235b
  guint node_hash;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (hash_table != NULL, FALSE);
Packit ae235b
Packit ae235b
  node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
Packit ae235b
Packit ae235b
  return HASH_IS_REAL (hash_table->hashes[node_index]);
Packit ae235b
}
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * g_hash_table_remove_internal:
Packit ae235b
 * @hash_table: our #GHashTable
Packit ae235b
 * @key: the key to remove
Packit ae235b
 * @notify: %TRUE if the destroy notify handlers are to be called
Packit ae235b
 * Returns: %TRUE if a node was found and removed, else %FALSE
Packit ae235b
 *
Packit ae235b
 * Implements the common logic for the g_hash_table_remove() and
Packit ae235b
 * g_hash_table_steal() functions.
Packit ae235b
 *
Packit ae235b
 * Do a lookup of @key and remove it if it is found, calling the
Packit ae235b
 * destroy notify handlers only if @notify is %TRUE.
Packit ae235b
 */
Packit ae235b
static gboolean
Packit ae235b
g_hash_table_remove_internal (GHashTable    *hash_table,
Packit ae235b
                              gconstpointer  key,
Packit ae235b
                              gboolean       notify)
Packit ae235b
{
Packit ae235b
  guint node_index;
Packit ae235b
  guint node_hash;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (hash_table != NULL, FALSE);
Packit ae235b
Packit ae235b
  node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
Packit ae235b
Packit ae235b
  if (!HASH_IS_REAL (hash_table->hashes[node_index]))
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  g_hash_table_remove_node (hash_table, node_index, notify);
Packit ae235b
  g_hash_table_maybe_resize (hash_table);
Packit ae235b
Packit ae235b
#ifndef G_DISABLE_ASSERT
Packit ae235b
  hash_table->version++;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_remove:
Packit ae235b
 * @hash_table: a #GHashTable
Packit ae235b
 * @key: the key to remove
Packit ae235b
 *
Packit ae235b
 * Removes a key and its associated value from a #GHashTable.
Packit ae235b
 *
Packit ae235b
 * If the #GHashTable was created using g_hash_table_new_full(), the
Packit ae235b
 * key and value are freed using the supplied destroy functions, otherwise
Packit ae235b
 * you have to make sure that any dynamically allocated values are freed
Packit ae235b
 * yourself.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the key was found and removed from the #GHashTable
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_hash_table_remove (GHashTable    *hash_table,
Packit ae235b
                     gconstpointer  key)
Packit ae235b
{
Packit ae235b
  return g_hash_table_remove_internal (hash_table, key, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_steal:
Packit ae235b
 * @hash_table: a #GHashTable
Packit ae235b
 * @key: the key to remove
Packit ae235b
 *
Packit ae235b
 * Removes a key and its associated value from a #GHashTable without
Packit ae235b
 * calling the key and value destroy functions.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the key was found and removed from the #GHashTable
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_hash_table_steal (GHashTable    *hash_table,
Packit ae235b
                    gconstpointer  key)
Packit ae235b
{
Packit ae235b
  return g_hash_table_remove_internal (hash_table, key, FALSE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_remove_all:
Packit ae235b
 * @hash_table: a #GHashTable
Packit ae235b
 *
Packit ae235b
 * Removes all keys and their associated values from a #GHashTable.
Packit ae235b
 *
Packit ae235b
 * If the #GHashTable was created using g_hash_table_new_full(),
Packit ae235b
 * the keys and values are freed using the supplied destroy functions,
Packit ae235b
 * otherwise you have to make sure that any dynamically allocated
Packit ae235b
 * values are freed yourself.
Packit ae235b
 *
Packit ae235b
 * Since: 2.12
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_hash_table_remove_all (GHashTable *hash_table)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (hash_table != NULL);
Packit ae235b
Packit ae235b
#ifndef G_DISABLE_ASSERT
Packit ae235b
  if (hash_table->nnodes != 0)
Packit ae235b
    hash_table->version++;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  g_hash_table_remove_all_nodes (hash_table, TRUE, FALSE);
Packit ae235b
  g_hash_table_maybe_resize (hash_table);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_steal_all:
Packit ae235b
 * @hash_table: a #GHashTable
Packit ae235b
 *
Packit ae235b
 * Removes all keys and their associated values from a #GHashTable
Packit ae235b
 * without calling the key and value destroy functions.
Packit ae235b
 *
Packit ae235b
 * Since: 2.12
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_hash_table_steal_all (GHashTable *hash_table)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (hash_table != NULL);
Packit ae235b
Packit ae235b
#ifndef G_DISABLE_ASSERT
Packit ae235b
  if (hash_table->nnodes != 0)
Packit ae235b
    hash_table->version++;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  g_hash_table_remove_all_nodes (hash_table, FALSE, FALSE);
Packit ae235b
  g_hash_table_maybe_resize (hash_table);
Packit ae235b
}
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * g_hash_table_foreach_remove_or_steal:
Packit ae235b
 * @hash_table: a #GHashTable
Packit ae235b
 * @func: the user's callback function
Packit ae235b
 * @user_data: data for @func
Packit ae235b
 * @notify: %TRUE if the destroy notify handlers are to be called
Packit ae235b
 *
Packit ae235b
 * Implements the common logic for g_hash_table_foreach_remove()
Packit ae235b
 * and g_hash_table_foreach_steal().
Packit ae235b
 *
Packit ae235b
 * Iterates over every node in the table, calling @func with the key
Packit ae235b
 * and value of the node (and @user_data). If @func returns %TRUE the
Packit ae235b
 * node is removed from the table.
Packit ae235b
 *
Packit ae235b
 * If @notify is true then the destroy notify handlers will be called
Packit ae235b
 * for each removed node.
Packit ae235b
 */
Packit ae235b
static guint
Packit ae235b
g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
Packit ae235b
                                      GHRFunc     func,
Packit ae235b
                                      gpointer    user_data,
Packit ae235b
                                      gboolean    notify)
Packit ae235b
{
Packit ae235b
  guint deleted = 0;
Packit ae235b
  gint i;
Packit ae235b
#ifndef G_DISABLE_ASSERT
Packit ae235b
  gint version = hash_table->version;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  for (i = 0; i < hash_table->size; i++)
Packit ae235b
    {
Packit ae235b
      guint node_hash = hash_table->hashes[i];
Packit ae235b
      gpointer node_key = hash_table->keys[i];
Packit ae235b
      gpointer node_value = hash_table->values[i];
Packit ae235b
Packit ae235b
      if (HASH_IS_REAL (node_hash) &&
Packit ae235b
          (* func) (node_key, node_value, user_data))
Packit ae235b
        {
Packit ae235b
          g_hash_table_remove_node (hash_table, i, notify);
Packit ae235b
          deleted++;
Packit ae235b
        }
Packit ae235b
Packit ae235b
#ifndef G_DISABLE_ASSERT
Packit ae235b
      g_return_val_if_fail (version == hash_table->version, 0);
Packit ae235b
#endif
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_hash_table_maybe_resize (hash_table);
Packit ae235b
Packit ae235b
#ifndef G_DISABLE_ASSERT
Packit ae235b
  if (deleted > 0)
Packit ae235b
    hash_table->version++;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return deleted;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_foreach_remove:
Packit ae235b
 * @hash_table: a #GHashTable
Packit ae235b
 * @func: the function to call for each key/value pair
Packit ae235b
 * @user_data: user data to pass to the function
Packit ae235b
 *
Packit ae235b
 * Calls the given function for each key/value pair in the
Packit ae235b
 * #GHashTable. If the function returns %TRUE, then the key/value
Packit ae235b
 * pair is removed from the #GHashTable. If you supplied key or
Packit ae235b
 * value destroy functions when creating the #GHashTable, they are
Packit ae235b
 * used to free the memory allocated for the removed keys and values.
Packit ae235b
 *
Packit ae235b
 * See #GHashTableIter for an alternative way to loop over the
Packit ae235b
 * key/value pairs in the hash table.
Packit ae235b
 *
Packit ae235b
 * Returns: the number of key/value pairs removed
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_hash_table_foreach_remove (GHashTable *hash_table,
Packit ae235b
                             GHRFunc     func,
Packit ae235b
                             gpointer    user_data)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (hash_table != NULL, 0);
Packit ae235b
  g_return_val_if_fail (func != NULL, 0);
Packit ae235b
Packit ae235b
  return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_foreach_steal:
Packit ae235b
 * @hash_table: a #GHashTable
Packit ae235b
 * @func: the function to call for each key/value pair
Packit ae235b
 * @user_data: user data to pass to the function
Packit ae235b
 *
Packit ae235b
 * Calls the given function for each key/value pair in the
Packit ae235b
 * #GHashTable. If the function returns %TRUE, then the key/value
Packit ae235b
 * pair is removed from the #GHashTable, but no key or value
Packit ae235b
 * destroy functions are called.
Packit ae235b
 *
Packit ae235b
 * See #GHashTableIter for an alternative way to loop over the
Packit ae235b
 * key/value pairs in the hash table.
Packit ae235b
 *
Packit ae235b
 * Returns: the number of key/value pairs removed.
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_hash_table_foreach_steal (GHashTable *hash_table,
Packit ae235b
                            GHRFunc     func,
Packit ae235b
                            gpointer    user_data)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (hash_table != NULL, 0);
Packit ae235b
  g_return_val_if_fail (func != NULL, 0);
Packit ae235b
Packit ae235b
  return g_hash_table_foreach_remove_or_steal (hash_table, func, user_data, FALSE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_foreach:
Packit ae235b
 * @hash_table: a #GHashTable
Packit ae235b
 * @func: the function to call for each key/value pair
Packit ae235b
 * @user_data: user data to pass to the function
Packit ae235b
 *
Packit ae235b
 * Calls the given function for each of the key/value pairs in the
Packit ae235b
 * #GHashTable.  The function is passed the key and value of each
Packit ae235b
 * pair, and the given @user_data parameter.  The hash table may not
Packit ae235b
 * be modified while iterating over it (you can't add/remove
Packit ae235b
 * items). To remove all items matching a predicate, use
Packit ae235b
 * g_hash_table_foreach_remove().
Packit ae235b
 *
Packit ae235b
 * See g_hash_table_find() for performance caveats for linear
Packit ae235b
 * order searches in contrast to g_hash_table_lookup().
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_hash_table_foreach (GHashTable *hash_table,
Packit ae235b
                      GHFunc      func,
Packit ae235b
                      gpointer    user_data)
Packit ae235b
{
Packit ae235b
  gint i;
Packit ae235b
#ifndef G_DISABLE_ASSERT
Packit ae235b
  gint version;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  g_return_if_fail (hash_table != NULL);
Packit ae235b
  g_return_if_fail (func != NULL);
Packit ae235b
Packit ae235b
#ifndef G_DISABLE_ASSERT
Packit ae235b
  version = hash_table->version;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  for (i = 0; i < hash_table->size; i++)
Packit ae235b
    {
Packit ae235b
      guint node_hash = hash_table->hashes[i];
Packit ae235b
      gpointer node_key = hash_table->keys[i];
Packit ae235b
      gpointer node_value = hash_table->values[i];
Packit ae235b
Packit ae235b
      if (HASH_IS_REAL (node_hash))
Packit ae235b
        (* func) (node_key, node_value, user_data);
Packit ae235b
Packit ae235b
#ifndef G_DISABLE_ASSERT
Packit ae235b
      g_return_if_fail (version == hash_table->version);
Packit ae235b
#endif
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_find:
Packit ae235b
 * @hash_table: a #GHashTable
Packit ae235b
 * @predicate: function to test the key/value pairs for a certain property
Packit ae235b
 * @user_data: user data to pass to the function
Packit ae235b
 *
Packit ae235b
 * Calls the given function for key/value pairs in the #GHashTable
Packit ae235b
 * until @predicate returns %TRUE. The function is passed the key
Packit ae235b
 * and value of each pair, and the given @user_data parameter. The
Packit ae235b
 * hash table may not be modified while iterating over it (you can't
Packit ae235b
 * add/remove items).
Packit ae235b
 *
Packit ae235b
 * Note, that hash tables are really only optimized for forward
Packit ae235b
 * lookups, i.e. g_hash_table_lookup(). So code that frequently issues
Packit ae235b
 * g_hash_table_find() or g_hash_table_foreach() (e.g. in the order of
Packit ae235b
 * once per every entry in a hash table) should probably be reworked
Packit ae235b
 * to use additional or different data structures for reverse lookups
Packit ae235b
 * (keep in mind that an O(n) find/foreach operation issued for all n
Packit ae235b
 * values in a hash table ends up needing O(n*n) operations).
Packit ae235b
 *
Packit ae235b
 * Returns: (nullable): The value of the first key/value pair is returned,
Packit ae235b
 *     for which @predicate evaluates to %TRUE. If no pair with the
Packit ae235b
 *     requested property is found, %NULL is returned.
Packit ae235b
 *
Packit ae235b
 * Since: 2.4
Packit ae235b
 */
Packit ae235b
gpointer
Packit ae235b
g_hash_table_find (GHashTable *hash_table,
Packit ae235b
                   GHRFunc     predicate,
Packit ae235b
                   gpointer    user_data)
Packit ae235b
{
Packit ae235b
  gint i;
Packit ae235b
#ifndef G_DISABLE_ASSERT
Packit ae235b
  gint version;
Packit ae235b
#endif
Packit ae235b
  gboolean match;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (hash_table != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (predicate != NULL, NULL);
Packit ae235b
Packit ae235b
#ifndef G_DISABLE_ASSERT
Packit ae235b
  version = hash_table->version;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  match = FALSE;
Packit ae235b
Packit ae235b
  for (i = 0; i < hash_table->size; i++)
Packit ae235b
    {
Packit ae235b
      guint node_hash = hash_table->hashes[i];
Packit ae235b
      gpointer node_key = hash_table->keys[i];
Packit ae235b
      gpointer node_value = hash_table->values[i];
Packit ae235b
Packit ae235b
      if (HASH_IS_REAL (node_hash))
Packit ae235b
        match = predicate (node_key, node_value, user_data);
Packit ae235b
Packit ae235b
#ifndef G_DISABLE_ASSERT
Packit ae235b
      g_return_val_if_fail (version == hash_table->version, NULL);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
      if (match)
Packit ae235b
        return node_value;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_size:
Packit ae235b
 * @hash_table: a #GHashTable
Packit ae235b
 *
Packit ae235b
 * Returns the number of elements contained in the #GHashTable.
Packit ae235b
 *
Packit ae235b
 * Returns: the number of key/value pairs in the #GHashTable.
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_hash_table_size (GHashTable *hash_table)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (hash_table != NULL, 0);
Packit ae235b
Packit ae235b
  return hash_table->nnodes;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_get_keys:
Packit ae235b
 * @hash_table: a #GHashTable
Packit ae235b
 *
Packit ae235b
 * Retrieves every key inside @hash_table. The returned data is valid
Packit ae235b
 * until changes to the hash release those keys.
Packit ae235b
 *
Packit ae235b
 * This iterates over every entry in the hash table to build its return value.
Packit ae235b
 * To iterate over the entries in a #GHashTable more efficiently, use a
Packit ae235b
 * #GHashTableIter.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer container): a #GList containing all the keys
Packit ae235b
 *     inside the hash table. The content of the list is owned by the
Packit ae235b
 *     hash table and should not be modified or freed. Use g_list_free()
Packit ae235b
 *     when done using the list.
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
GList *
Packit ae235b
g_hash_table_get_keys (GHashTable *hash_table)
Packit ae235b
{
Packit ae235b
  gint i;
Packit ae235b
  GList *retval;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (hash_table != NULL, NULL);
Packit ae235b
Packit ae235b
  retval = NULL;
Packit ae235b
  for (i = 0; i < hash_table->size; i++)
Packit ae235b
    {
Packit ae235b
      if (HASH_IS_REAL (hash_table->hashes[i]))
Packit ae235b
        retval = g_list_prepend (retval, hash_table->keys[i]);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_get_keys_as_array:
Packit ae235b
 * @hash_table: a #GHashTable
Packit ae235b
 * @length: (out): the length of the returned array
Packit ae235b
 *
Packit ae235b
 * Retrieves every key inside @hash_table, as an array.
Packit ae235b
 *
Packit ae235b
 * The returned array is %NULL-terminated but may contain %NULL as a
Packit ae235b
 * key.  Use @length to determine the true length if it's possible that
Packit ae235b
 * %NULL was used as the value for a key.
Packit ae235b
 *
Packit ae235b
 * Note: in the common case of a string-keyed #GHashTable, the return
Packit ae235b
 * value of this function can be conveniently cast to (const gchar **).
Packit ae235b
 *
Packit ae235b
 * This iterates over every entry in the hash table to build its return value.
Packit ae235b
 * To iterate over the entries in a #GHashTable more efficiently, use a
Packit ae235b
 * #GHashTableIter.
Packit ae235b
 *
Packit ae235b
 * You should always free the return result with g_free().  In the
Packit ae235b
 * above-mentioned case of a string-keyed hash table, it may be
Packit ae235b
 * appropriate to use g_strfreev() if you call g_hash_table_steal_all()
Packit ae235b
 * first to transfer ownership of the keys.
Packit ae235b
 *
Packit ae235b
 * Returns: (array length=length) (transfer container): a
Packit ae235b
 *   %NULL-terminated array containing each key from the table.
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 **/
Packit ae235b
gpointer *
Packit ae235b
g_hash_table_get_keys_as_array (GHashTable *hash_table,
Packit ae235b
                                guint      *length)
Packit ae235b
{
Packit ae235b
  gpointer *result;
Packit ae235b
  guint i, j = 0;
Packit ae235b
Packit ae235b
  result = g_new (gpointer, hash_table->nnodes + 1);
Packit ae235b
  for (i = 0; i < hash_table->size; i++)
Packit ae235b
    {
Packit ae235b
      if (HASH_IS_REAL (hash_table->hashes[i]))
Packit ae235b
        result[j++] = hash_table->keys[i];
Packit ae235b
    }
Packit ae235b
  g_assert_cmpint (j, ==, hash_table->nnodes);
Packit ae235b
  result[j] = NULL;
Packit ae235b
Packit ae235b
  if (length)
Packit ae235b
    *length = j;
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_hash_table_get_values:
Packit ae235b
 * @hash_table: a #GHashTable
Packit ae235b
 *
Packit ae235b
 * Retrieves every value inside @hash_table. The returned data
Packit ae235b
 * is valid until @hash_table is modified.
Packit ae235b
 *
Packit ae235b
 * This iterates over every entry in the hash table to build its return value.
Packit ae235b
 * To iterate over the entries in a #GHashTable more efficiently, use a
Packit ae235b
 * #GHashTableIter.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer container): a #GList containing all the values
Packit ae235b
 *     inside the hash table. The content of the list is owned by the
Packit ae235b
 *     hash table and should not be modified or freed. Use g_list_free()
Packit ae235b
 *     when done using the list.
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
GList *
Packit ae235b
g_hash_table_get_values (GHashTable *hash_table)
Packit ae235b
{
Packit ae235b
  gint i;
Packit ae235b
  GList *retval;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (hash_table != NULL, NULL);
Packit ae235b
Packit ae235b
  retval = NULL;
Packit ae235b
  for (i = 0; i < hash_table->size; i++)
Packit ae235b
    {
Packit ae235b
      if (HASH_IS_REAL (hash_table->hashes[i]))
Packit ae235b
        retval = g_list_prepend (retval, hash_table->values[i]);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Hash functions.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_str_equal:
Packit ae235b
 * @v1: (not nullable): a key
Packit ae235b
 * @v2: (not nullable): a key to compare with @v1
Packit ae235b
 *
Packit ae235b
 * Compares two strings for byte-by-byte equality and returns %TRUE
Packit ae235b
 * if they are equal. It can be passed to g_hash_table_new() as the
Packit ae235b
 * @key_equal_func parameter, when using non-%NULL strings as keys in a
Packit ae235b
 * #GHashTable.
Packit ae235b
 *
Packit ae235b
 * Note that this function is primarily meant as a hash table comparison
Packit ae235b
 * function. For a general-purpose, %NULL-safe string comparison function,
Packit ae235b
 * see g_strcmp0().
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the two keys match
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_str_equal (gconstpointer v1,
Packit ae235b
             gconstpointer v2)
Packit ae235b
{
Packit ae235b
  const gchar *string1 = v1;
Packit ae235b
  const gchar *string2 = v2;
Packit ae235b
Packit ae235b
  return strcmp (string1, string2) == 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_str_hash:
Packit ae235b
 * @v: (not nullable): a string key
Packit ae235b
 *
Packit ae235b
 * Converts a string to a hash value.
Packit ae235b
 *
Packit ae235b
 * This function implements the widely used "djb" hash apparently
Packit ae235b
 * posted by Daniel Bernstein to comp.lang.c some time ago.  The 32
Packit ae235b
 * bit unsigned hash value starts at 5381 and for each byte 'c' in
Packit ae235b
 * the string, is updated: `hash = hash * 33 + c`. This function
Packit ae235b
 * uses the signed value of each byte.
Packit ae235b
 *
Packit ae235b
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
Packit ae235b
 * when using non-%NULL strings as keys in a #GHashTable.
Packit ae235b
 *
Packit ae235b
 * Note that this function may not be a perfect fit for all use cases.
Packit ae235b
 * For example, it produces some hash collisions with strings as short
Packit ae235b
 * as 2.
Packit ae235b
 *
Packit ae235b
 * Returns: a hash value corresponding to the key
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_str_hash (gconstpointer v)
Packit ae235b
{
Packit ae235b
  const signed char *p;
Packit ae235b
  guint32 h = 5381;
Packit ae235b
Packit ae235b
  for (p = v; *p != '\0'; p++)
Packit ae235b
    h = (h << 5) + h + *p;
Packit ae235b
Packit ae235b
  return h;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_direct_hash:
Packit ae235b
 * @v: (nullable): a #gpointer key
Packit ae235b
 *
Packit ae235b
 * Converts a gpointer to a hash value.
Packit ae235b
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
Packit ae235b
 * when using opaque pointers compared by pointer value as keys in a
Packit ae235b
 * #GHashTable.
Packit ae235b
 *
Packit ae235b
 * This hash function is also appropriate for keys that are integers
Packit ae235b
 * stored in pointers, such as `GINT_TO_POINTER (n)`.
Packit ae235b
 *
Packit ae235b
 * Returns: a hash value corresponding to the key.
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_direct_hash (gconstpointer v)
Packit ae235b
{
Packit ae235b
  return GPOINTER_TO_UINT (v);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_direct_equal:
Packit ae235b
 * @v1: (nullable): a key
Packit ae235b
 * @v2: (nullable): a key to compare with @v1
Packit ae235b
 *
Packit ae235b
 * Compares two #gpointer arguments and returns %TRUE if they are equal.
Packit ae235b
 * It can be passed to g_hash_table_new() as the @key_equal_func
Packit ae235b
 * parameter, when using opaque pointers compared by pointer value as
Packit ae235b
 * keys in a #GHashTable.
Packit ae235b
 *
Packit ae235b
 * This equality function is also appropriate for keys that are integers
Packit ae235b
 * stored in pointers, such as `GINT_TO_POINTER (n)`.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the two keys match.
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_direct_equal (gconstpointer v1,
Packit ae235b
                gconstpointer v2)
Packit ae235b
{
Packit ae235b
  return v1 == v2;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_int_equal:
Packit ae235b
 * @v1: (not nullable): a pointer to a #gint key
Packit ae235b
 * @v2: (not nullable): a pointer to a #gint key to compare with @v1
Packit ae235b
 *
Packit ae235b
 * Compares the two #gint values being pointed to and returns
Packit ae235b
 * %TRUE if they are equal.
Packit ae235b
 * It can be passed to g_hash_table_new() as the @key_equal_func
Packit ae235b
 * parameter, when using non-%NULL pointers to integers as keys in a
Packit ae235b
 * #GHashTable.
Packit ae235b
 *
Packit ae235b
 * Note that this function acts on pointers to #gint, not on #gint
Packit ae235b
 * directly: if your hash table's keys are of the form
Packit ae235b
 * `GINT_TO_POINTER (n)`, use g_direct_equal() instead.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the two keys match.
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_int_equal (gconstpointer v1,
Packit ae235b
             gconstpointer v2)
Packit ae235b
{
Packit ae235b
  return *((const gint*) v1) == *((const gint*) v2);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_int_hash:
Packit ae235b
 * @v: (not nullable): a pointer to a #gint key
Packit ae235b
 *
Packit ae235b
 * Converts a pointer to a #gint to a hash value.
Packit ae235b
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
Packit ae235b
 * when using non-%NULL pointers to integer values as keys in a #GHashTable.
Packit ae235b
 *
Packit ae235b
 * Note that this function acts on pointers to #gint, not on #gint
Packit ae235b
 * directly: if your hash table's keys are of the form
Packit ae235b
 * `GINT_TO_POINTER (n)`, use g_direct_hash() instead.
Packit ae235b
 *
Packit ae235b
 * Returns: a hash value corresponding to the key.
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_int_hash (gconstpointer v)
Packit ae235b
{
Packit ae235b
  return *(const gint*) v;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_int64_equal:
Packit ae235b
 * @v1: (not nullable): a pointer to a #gint64 key
Packit ae235b
 * @v2: (not nullable): a pointer to a #gint64 key to compare with @v1
Packit ae235b
 *
Packit ae235b
 * Compares the two #gint64 values being pointed to and returns
Packit ae235b
 * %TRUE if they are equal.
Packit ae235b
 * It can be passed to g_hash_table_new() as the @key_equal_func
Packit ae235b
 * parameter, when using non-%NULL pointers to 64-bit integers as keys in a
Packit ae235b
 * #GHashTable.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the two keys match.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_int64_equal (gconstpointer v1,
Packit ae235b
               gconstpointer v2)
Packit ae235b
{
Packit ae235b
  return *((const gint64*) v1) == *((const gint64*) v2);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_int64_hash:
Packit ae235b
 * @v: (not nullable): a pointer to a #gint64 key
Packit ae235b
 *
Packit ae235b
 * Converts a pointer to a #gint64 to a hash value.
Packit ae235b
 *
Packit ae235b
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
Packit ae235b
 * when using non-%NULL pointers to 64-bit integer values as keys in a
Packit ae235b
 * #GHashTable.
Packit ae235b
 *
Packit ae235b
 * Returns: a hash value corresponding to the key.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_int64_hash (gconstpointer v)
Packit ae235b
{
Packit ae235b
  return (guint) *(const gint64*) v;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_double_equal:
Packit ae235b
 * @v1: (not nullable): a pointer to a #gdouble key
Packit ae235b
 * @v2: (not nullable): a pointer to a #gdouble key to compare with @v1
Packit ae235b
 *
Packit ae235b
 * Compares the two #gdouble values being pointed to and returns
Packit ae235b
 * %TRUE if they are equal.
Packit ae235b
 * It can be passed to g_hash_table_new() as the @key_equal_func
Packit ae235b
 * parameter, when using non-%NULL pointers to doubles as keys in a
Packit ae235b
 * #GHashTable.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the two keys match.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_double_equal (gconstpointer v1,
Packit ae235b
                gconstpointer v2)
Packit ae235b
{
Packit ae235b
  return *((const gdouble*) v1) == *((const gdouble*) v2);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_double_hash:
Packit ae235b
 * @v: (not nullable): a pointer to a #gdouble key
Packit ae235b
 *
Packit ae235b
 * Converts a pointer to a #gdouble to a hash value.
Packit ae235b
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
Packit ae235b
 * It can be passed to g_hash_table_new() as the @hash_func parameter,
Packit ae235b
 * when using non-%NULL pointers to doubles as keys in a #GHashTable.
Packit ae235b
 *
Packit ae235b
 * Returns: a hash value corresponding to the key.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_double_hash (gconstpointer v)
Packit ae235b
{
Packit ae235b
  return (guint) *(const gdouble*) v;
Packit ae235b
}