Synopsis
#include <glib.h>
GHashTable;
GHashTable* g_hash_table_new (GHashFunc hash_func,
GEqualFunc key_equal_func);
GHashTable* g_hash_table_new_full (GHashFunc hash_func,
GEqualFunc key_equal_func,
GDestroyNotify key_destroy_func,
GDestroyNotify value_destroy_func);
guint (*GHashFunc) (gconstpointer key);
gboolean (*GEqualFunc) (gconstpointer a,
gconstpointer b);
void g_hash_table_insert (GHashTable *hash_table,
gpointer key,
gpointer value);
void g_hash_table_replace (GHashTable *hash_table,
gpointer key,
gpointer value);
guint g_hash_table_size (GHashTable *hash_table);
gpointer g_hash_table_lookup (GHashTable *hash_table,
gconstpointer key);
gboolean g_hash_table_lookup_extended (GHashTable *hash_table,
gconstpointer lookup_key,
gpointer *orig_key,
gpointer *value);
void g_hash_table_foreach (GHashTable *hash_table,
GHFunc func,
gpointer user_data);
gpointer g_hash_table_find (GHashTable *hash_table,
GHRFunc predicate,
gpointer user_data);
void (*GHFunc) (gpointer key,
gpointer value,
gpointer user_data);
gboolean g_hash_table_remove (GHashTable *hash_table,
gconstpointer key);
gboolean g_hash_table_steal (GHashTable *hash_table,
gconstpointer key);
guint g_hash_table_foreach_remove (GHashTable *hash_table,
GHRFunc func,
gpointer user_data);
guint g_hash_table_foreach_steal (GHashTable *hash_table,
GHRFunc func,
gpointer user_data);
void g_hash_table_remove_all (GHashTable *hash_table);
void g_hash_table_steal_all (GHashTable *hash_table);
GList* g_hash_table_get_keys (GHashTable *hash_table);
GList* g_hash_table_get_values (GHashTable *hash_table);
gboolean (*GHRFunc) (gpointer key,
gpointer value,
gpointer user_data);
#define g_hash_table_freeze (hash_table)
#define g_hash_table_thaw (hash_table)
void g_hash_table_destroy (GHashTable *hash_table);
GHashTable* g_hash_table_ref (GHashTable *hash_table);
void g_hash_table_unref (GHashTable *hash_table);
GHashTableIter;
void g_hash_table_iter_init (GHashTableIter *iter,
GHashTable *hash_table);
gboolean g_hash_table_iter_next (GHashTableIter *iter,
gpointer *key,
gpointer *value);
GHashTable* g_hash_table_iter_get_hash_table (GHashTableIter *iter);
void g_hash_table_iter_remove (GHashTableIter *iter);
void g_hash_table_iter_steal (GHashTableIter *iter);
gboolean g_direct_equal (gconstpointer v1,
gconstpointer v2);
guint g_direct_hash (gconstpointer v);
gboolean g_int_equal (gconstpointer v1,
gconstpointer v2);
guint g_int_hash (gconstpointer v);
gboolean g_str_equal (gconstpointer v1,
gconstpointer v2);
guint g_str_hash (gconstpointer v);
DescriptionA GHashTable provides associations between keys and values which is optimized so that given a key, the associated value can be found very quickly.
Note that neither keys nor values are copied when inserted into the
GHashTable, so they must exist for the lifetime of the GHashTable.
This means that the use of static strings is OK, but temporary
strings (i.e. those created in buffers and those returned by GTK+ widgets)
should be copied with If keys or values are dynamically allocated, you must be careful to ensure that they are freed when they are removed from the GHashTable, and also when they are overwritten by new insertions into the GHashTable. It is also not advisable to mix static strings and dynamically-allocated strings in a GHashTable, because it then becomes difficult to determine whether the string should be freed.
To create a GHashTable, use
To insert a key and value into a GHashTable, use
To lookup a value corresponding to a given key, use
To remove a key and value, use
To call a function for each key and value pair use
To destroy a GHashTable use DetailsGHashTabletypedef struct _GHashTable GHashTable; The GHashTable struct is an opaque data structure to represent a Hash Table. It should only be accessed via the following functions. g_hash_table_new ()GHashTable* g_hash_table_new (GHashFunc hash_func, GEqualFunc key_equal_func); Creates a new GHashTable with a reference count of 1.
g_hash_table_new_full ()GHashTable* g_hash_table_new_full (GHashFunc hash_func, GEqualFunc key_equal_func, GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func);
Creates a new GHashTable like
GHashFunc ()guint (*GHashFunc) (gconstpointer key);
Specifies the type of the hash function which is passed to
The function is passed a key and should return a guint hash value.
The functions The hash values should be evenly distributed over a fairly large range? The modulus is taken with the hash table size (a prime number) to find the 'bucket' to place each key into. The function should also be very fast, since it is called for each key lookup.
GEqualFunc ()gboolean (*GEqualFunc) (gconstpointer a, gconstpointer b);
Specifies the type of a function used to test two values for
equality. The function should return g_hash_table_insert ()void g_hash_table_insert (GHashTable *hash_table, gpointer key, gpointer value); Inserts a new key and value into a GHashTable.
If the key already exists in the GHashTable its current value is replaced
with the new value. If you supplied a
g_hash_table_replace ()void g_hash_table_replace (GHashTable *hash_table, gpointer key, gpointer value);
Inserts a new key and value into a GHashTable similar to
g_hash_table_size ()guint g_hash_table_size (GHashTable *hash_table); Returns the number of elements contained in the GHashTable.
g_hash_table_lookup ()gpointer g_hash_table_lookup (GHashTable *hash_table, gconstpointer key);
Looks up a key in a GHashTable. Note that this function cannot
distinguish between a key that is not present and one which is present
and has the value
g_hash_table_lookup_extended ()gboolean g_hash_table_lookup_extended (GHashTable *hash_table, gconstpointer lookup_key, gpointer *orig_key, gpointer *value);
Looks up a key in the GHashTable, returning the original key and the
associated value and a gboolean which is
g_hash_table_foreach ()void g_hash_table_foreach (GHashTable *hash_table, GHFunc func, gpointer user_data);
Calls the given function for each of the key/value pairs in the
GHashTable. The function is passed the key and value of each
pair, and the given
See
g_hash_table_find ()gpointer g_hash_table_find (GHashTable *hash_table, GHRFunc predicate, gpointer user_data);
Calls the given function for key/value pairs in the GHashTable until
Note, that hash tables are really only optimized for forward lookups,
i.e.
Since 2.4 GHFunc ()void (*GHFunc) (gpointer key, gpointer value, gpointer user_data);
Specifies the type of the function passed to
g_hash_table_remove ()gboolean g_hash_table_remove (GHashTable *hash_table, gconstpointer key); Removes a key and its associated value from a GHashTable.
If the GHashTable was created using
g_hash_table_steal ()gboolean g_hash_table_steal (GHashTable *hash_table, gconstpointer key); Removes a key and its associated value from a GHashTable without calling the key and value destroy functions.
g_hash_table_foreach_remove ()guint g_hash_table_foreach_remove (GHashTable *hash_table, GHRFunc func, gpointer user_data);
Calls the given function for each key/value pair in the GHashTable.
If the function returns See GHashTableIterator for an alternative way to loop over the key/value pairs in the hash table.
g_hash_table_foreach_steal ()guint g_hash_table_foreach_steal (GHashTable *hash_table, GHRFunc func, gpointer user_data);
Calls the given function for each key/value pair in the GHashTable.
If the function returns See GHashTableIterator for an alternative way to loop over the key/value pairs in the hash table.
g_hash_table_remove_all ()void g_hash_table_remove_all (GHashTable *hash_table); Removes all keys and their associated values from a GHashTable.
If the GHashTable was created using
Since 2.12 g_hash_table_steal_all ()void g_hash_table_steal_all (GHashTable *hash_table); Removes all keys and their associated values from a GHashTable without calling the key and value destroy functions.
Since 2.12 g_hash_table_get_keys ()GList* g_hash_table_get_keys (GHashTable *hash_table);
Retrieves every key inside
Since 2.14 g_hash_table_get_values ()GList* g_hash_table_get_values (GHashTable *hash_table);
Retrieves every value inside
Since 2.14 GHRFunc ()gboolean (*GHRFunc) (gpointer key, gpointer value, gpointer user_data);
Specifies the type of the function passed to
g_hash_table_freeze()#define g_hash_table_freeze(hash_table) Warning
This function is deprecated and will be removed in the next major release of GLib. It does nothing.
g_hash_table_thaw()#define g_hash_table_thaw(hash_table) Warning
This function is deprecated and will be removed in the next major release of GLib. It does nothing.
g_hash_table_destroy ()void g_hash_table_destroy (GHashTable *hash_table);
Destroys all keys and values in the GHashTable and decrements its
reference count by 1. If keys and/or values are dynamically allocated,
you should either free them first or create the GHashTable with destroy
notifiers using
g_hash_table_ref ()GHashTable* g_hash_table_ref (GHashTable *hash_table);
Atomically increments the reference count of
Since 2.10 g_hash_table_unref ()void g_hash_table_unref (GHashTable *hash_table);
Atomically decrements the reference count of
Since 2.10 GHashTableItertypedef struct {
} GHashTableIter;
A GHashTableIter structure represents an iterator that can be
used to iterate over the elements of a GHashTable. GHashTableIter
structures are typically allocated on the stack and then initialized
with g_hash_table_iter_init ()void g_hash_table_iter_init (GHashTableIter *iter, GHashTable *hash_table);
Initializes a key/value pair iterator and associates it with
GHashTableIter iter;
gpointer key, value;
g_hash_table_iter_init (&iter, hash_table);
while (g_hash_table_iter_next (&iter, &key, &value))
{
/* do something with key and value */
}
Since 2.16 g_hash_table_iter_next ()gboolean g_hash_table_iter_next (GHashTableIter *iter, gpointer *key, gpointer *value);
Advances
Since 2.16 g_hash_table_iter_get_hash_table ()GHashTable* g_hash_table_iter_get_hash_table (GHashTableIter *iter);
Returns the GHashTable associated with
Since 2.16 g_hash_table_iter_remove ()void g_hash_table_iter_remove (GHashTableIter *iter);
Removes the key/value pair currently pointed to by the iterator
from its associated GHashTable. Can only be called after
If the GHashTable was created using
Since 2.16 g_hash_table_iter_steal ()void g_hash_table_iter_steal (GHashTableIter *iter);
Removes the key/value pair currently pointed to by the iterator
from its associated GHashTable, without calling the key and value
destroy functions. Can only be called after
Since 2.16 g_direct_equal ()gboolean g_direct_equal (gconstpointer v1, gconstpointer v2);
Compares two gpointer arguments and returns
g_direct_hash ()guint g_direct_hash (gconstpointer v);
Converts a gpointer to a hash value.
It can be passed to
g_int_equal ()gboolean g_int_equal (gconstpointer v1, gconstpointer v2);
Compares the two gint values being pointed to and returns
g_int_hash ()guint g_int_hash (gconstpointer v);
Converts a pointer to a gint to a hash value.
It can be passed to
g_str_equal ()gboolean g_str_equal (gconstpointer v1, gconstpointer v2);
Compares two strings for byte-by-byte equality and returns
g_str_hash ()guint g_str_hash (gconstpointer v);
Converts a string to a hash value.
It can be passed to
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||