Top |
Hash TablesHash Tables — associations between keys and values so that given a key the value can be found quickly |
A 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 g_strdup()
before being inserted.
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 g_hash_table_new()
.
To insert a key and value into a GHashTable, use
g_hash_table_insert()
.
To lookup a value corresponding to a given key, use
g_hash_table_lookup()
and g_hash_table_lookup_extended()
.
g_hash_table_lookup_extended() can also be used to simply check if a key is present in the hash table.
To remove a key and value, use g_hash_table_remove()
.
To call a function for each key and value pair use
g_hash_table_foreach()
or use a iterator to iterate over the
key/value pairs in the hash table, see GHashTableIter.
To destroy a GHashTable use g_hash_table_destroy()
.
A common use-case for hash tables is to store information about a
set of keys, without associating any particular value with each
key. GHashTable optimizes one way of doing so: If you store only
key-value pairs where key == value, then GHashTable does not
allocate memory to store the values, which can be a considerable
space saving, if your set is large. The functions
g_hash_table_add()
and g_hash_table_contains()
are designed to be
used when using GHashTable this way.
GHashTable is not designed to be statically initialised with keys and values known at compile time. To build a static hash table, use a tool such as gperf.
GHashTable * g_hash_table_new (GHashFunc hash_func
,GEqualFunc key_equal_func
);
Creates a new GHashTable with a reference count of 1.
Hash values returned by hash_func
are used to determine where keys
are stored within the GHashTable data structure. The g_direct_hash()
,
g_int_hash()
, g_int64_hash()
, g_double_hash()
and g_str_hash()
functions are provided for some common types of keys.
If hash_func
is NULL
, g_direct_hash()
is used.
key_equal_func
is used when looking up keys in the GHashTable.
The g_direct_equal()
, g_int_equal()
, g_int64_equal()
, g_double_equal()
and g_str_equal()
functions are provided for the most common types
of keys. If key_equal_func
is NULL
, keys are compared directly in
a similar fashion to g_direct_equal()
, but without the overhead of
a function call. key_equal_func
is called with the key from the hash table
as its first parameter, and the user-provided key to check against as
its second.
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 g_hash_table_new()
with a reference
count of 1 and allows to specify functions to free the memory
allocated for the key and value that get called when removing the
entry from the GHashTable.
Since version 2.42 it is permissible for destroy notify functions to
recursively remove further items from the hash table. This is only
permissible if the application still holds a reference to the hash table.
This means that you may need to ensure that the hash table is empty by
calling g_hash_table_remove_all()
before releasing the last reference using
g_hash_table_unref()
.
hash_func |
a function to create a hash value from a key |
|
key_equal_func |
a function to check two keys for equality |
|
key_destroy_func |
a function to free the memory allocated for the key
used when removing the entry from the GHashTable, or |
[nullable] |
value_destroy_func |
a function to free the memory allocated for the
value used when removing the entry from the GHashTable, or |
[nullable] |
guint
(*GHashFunc) (gconstpointer key
);
Specifies the type of the hash function which is passed to
g_hash_table_new()
when a GHashTable is created.
The function is passed a key and should return a guint hash value.
The functions g_direct_hash()
, g_int_hash()
and g_str_hash()
provide
hash functions which can be used when the key is a gpointer, gint*,
and gchar* respectively.
g_direct_hash() is also the appropriate hash function for keys
of the form GINT_TO_POINTER (n)
(or similar macros).
A good hash functions should produce hash values that are 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.
Note that the hash functions provided by GLib have these qualities,
but are not particularly robust against manufactured keys that
cause hash collisions. Therefore, you should consider choosing
a more secure hash function when using a GHashTable with keys
that originate in untrusted data (such as HTTP requests).
Using g_str_hash()
in that situation might make your application
vulerable to
Algorithmic Complexity Attacks.
The key to choosing a good hash is unpredictability. Even cryptographic hashes are very easy to find collisions for when the remainder is taken modulo a somewhat predictable prime number. There must be an element of randomness that an attacker is unable to guess.
gboolean (*GEqualFunc) (gconstpointer a
,gconstpointer b
);
Specifies the type of a function used to test two values for
equality. The function should return TRUE
if both values are equal
and FALSE
otherwise.
gboolean 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
value_destroy_func
when creating the GHashTable, the old
value is freed using that function. If you supplied a
key_destroy_func
when creating the GHashTable, the passed
key is freed using that function.
Starting from GLib 2.40, this function returns a boolean value to indicate whether the newly added value was already in the hash table or not.
gboolean 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_insert()
. The difference is that if the key
already exists in the GHashTable, it gets replaced by the
new key. If you supplied a value_destroy_func
when creating
the GHashTable, the old value is freed using that function.
If you supplied a key_destroy_func
when creating the
GHashTable, the old key is freed using that function.
Starting from GLib 2.40, this function returns a boolean value to indicate whether the newly added value was already in the hash table or not.
gboolean g_hash_table_add (GHashTable *hash_table
,gpointer key
);
This is a convenience function for using a GHashTable as a set. It
is equivalent to calling g_hash_table_replace()
with key
as both the
key and the value.
When a hash table only ever contains keys that have themselves as the corresponding value it is able to be stored more efficiently. See the discussion in the section description.
Starting from GLib 2.40, this function returns a boolean value to indicate whether the newly added value was already in the hash table or not.
Since: 2.32
gboolean g_hash_table_contains (GHashTable *hash_table
,gconstpointer key
);
Checks if key
is in hash_table
.
Since: 2.32
guint
g_hash_table_size (GHashTable *hash_table
);
Returns the number of elements contained in the GHashTable.
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 NULL
. If you need this distinction, use
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 TRUE
if the key was found. This
is useful if you need to free the memory allocated for the original key,
for example before calling g_hash_table_remove()
.
You can actually pass NULL
for lookup_key
to test
whether the NULL
key exists, provided the hash and equal functions
of hash_table
are NULL
-safe.
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 user_data
parameter. The hash table may not
be modified while iterating over it (you can't add/remove
items). To remove all items matching a predicate, use
g_hash_table_foreach_remove()
.
See g_hash_table_find()
for performance caveats for linear
order searches in contrast to g_hash_table_lookup()
.
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 predicate
returns TRUE
. The function is passed the key
and value of each pair, and the given user_data
parameter. The
hash table may not be modified while iterating over it (you can't
add/remove items).
Note, that hash tables are really only optimized for forward
lookups, i.e. g_hash_table_lookup()
. So code that frequently issues
g_hash_table_find()
or g_hash_table_foreach()
(e.g. in the order of
once per every entry in a hash table) should probably be reworked
to use additional or different data structures for reverse lookups
(keep in mind that an O(n) find/foreach operation issued for all n
values in a hash table ends up needing O(n*n) operations).
hash_table |
||
predicate |
function to test the key/value pairs for a certain property |
|
user_data |
user data to pass to the function |
The value of the first key/value pair is returned,
for which predicate
evaluates to TRUE
. If no pair with the
requested property is found, NULL
is returned.
[nullable]
Since: 2.4
void (*GHFunc) (gpointer key
,gpointer value
,gpointer user_data
);
Specifies the type of the function passed to g_hash_table_foreach()
.
It is called with each key/value pair, together with the user_data
parameter which is passed to g_hash_table_foreach()
.
key |
a key |
|
value |
the value corresponding to the key |
|
user_data |
user data passed to |
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_new_full()
, the
key and value are freed using the supplied destroy functions, otherwise
you have to make sure that any dynamically allocated values are freed
yourself.
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.
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 TRUE
, then the key/value
pair is removed from the GHashTable. If you supplied key or
value destroy functions when creating the GHashTable, they are
used to free the memory allocated for the removed keys and values.
See GHashTableIter for an alternative way to loop over the key/value pairs in the hash table.
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 TRUE
, then the key/value
pair is removed from the GHashTable, but no key or value
destroy functions are called.
See GHashTableIter for an alternative way to loop over the key/value pairs in the hash table.
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 g_hash_table_new_full()
,
the keys and values are freed using the supplied destroy functions,
otherwise you have to make sure that any dynamically allocated
values are freed yourself.
Since: 2.12
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
GList *
g_hash_table_get_keys (GHashTable *hash_table
);
Retrieves every key inside hash_table
. The returned data is valid
until changes to the hash release those keys.
This iterates over every entry in the hash table to build its return value. To iterate over the entries in a GHashTable more efficiently, use a GHashTableIter.
a GList containing all the keys
inside the hash table. The content of the list is owned by the
hash table and should not be modified or freed. Use g_list_free()
when done using the list.
[transfer container]
Since: 2.14
GList *
g_hash_table_get_values (GHashTable *hash_table
);
Retrieves every value inside hash_table
. The returned data
is valid until hash_table
is modified.
This iterates over every entry in the hash table to build its return value. To iterate over the entries in a GHashTable more efficiently, use a GHashTableIter.
a GList containing all the values
inside the hash table. The content of the list is owned by the
hash table and should not be modified or freed. Use g_list_free()
when done using the list.
[transfer container]
Since: 2.14
gpointer * g_hash_table_get_keys_as_array (GHashTable *hash_table
,guint *length
);
Retrieves every key inside hash_table
, as an array.
The returned array is NULL
-terminated but may contain NULL
as a
key. Use length
to determine the true length if it's possible that
NULL
was used as the value for a key.
Note: in the common case of a string-keyed GHashTable, the return value of this function can be conveniently cast to (const gchar **).
This iterates over every entry in the hash table to build its return value. To iterate over the entries in a GHashTable more efficiently, use a GHashTableIter.
You should always free the return result with g_free()
. In the
above-mentioned case of a string-keyed hash table, it may be
appropriate to use g_strfreev()
if you call g_hash_table_steal_all()
first to transfer ownership of the keys.
a
NULL
-terminated array containing each key from the table.
[array length=length][transfer container]
Since: 2.40
gboolean (*GHRFunc) (gpointer key
,gpointer value
,gpointer user_data
);
Specifies the type of the function passed to
g_hash_table_foreach_remove()
. It is called with each key/value
pair, together with the user_data
parameter passed to
g_hash_table_foreach_remove()
. It should return TRUE
if the
key/value pair should be removed from the GHashTable.
key |
a key |
|
value |
the value associated with the key |
|
user_data |
user data passed to |
#define g_hash_table_freeze(hash_table)
g_hash_table_freeze
is deprecated and should not be used in newly-written code.
This function is deprecated and will be removed in the next major release of GLib. It does nothing.
#define g_hash_table_thaw(hash_table)
g_hash_table_thaw
is deprecated and should not be used in newly-written code.
This function is deprecated and will be removed in the next major release of GLib. It does nothing.
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_new_full()
. In the latter case the destroy
functions you supplied will be called on all keys and values during the
destruction phase.
GHashTable *
g_hash_table_ref (GHashTable *hash_table
);
Atomically increments the reference count of hash_table
by one.
This function is MT-safe and may be called from any thread.
Since: 2.10
void
g_hash_table_unref (GHashTable *hash_table
);
Atomically decrements the reference count of hash_table
by one.
If the reference count drops to 0, all keys and values will be
destroyed, and all memory allocated by the hash table is released.
This function is MT-safe and may be called from any thread.
Since: 2.10
void g_hash_table_iter_init (GHashTableIter *iter
,GHashTable *hash_table
);
Initializes a key/value pair iterator and associates it with
hash_table
. Modifying the hash table after calling this function
invalidates the returned iterator.
1 2 3 4 5 6 7 8 |
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
gboolean g_hash_table_iter_next (GHashTableIter *iter
,gpointer *key
,gpointer *value
);
Advances iter
and retrieves the key and/or value that are now
pointed to as a result of this advancement. If FALSE
is returned,
key
and value
are not set, and the iterator becomes invalid.
iter |
an initialized GHashTableIter |
|
key |
a location to store the key. |
[out][optional] |
value |
a location to store the value. |
[out][optional][nullable] |
Since: 2.16
GHashTable *
g_hash_table_iter_get_hash_table (GHashTableIter *iter
);
Returns the GHashTable associated with iter
.
Since: 2.16
void g_hash_table_iter_replace (GHashTableIter *iter
,gpointer value
);
Replaces the value currently pointed to by the iterator
from its associated GHashTable. Can only be called after
g_hash_table_iter_next()
returned TRUE
.
If you supplied a value_destroy_func
when creating the
GHashTable, the old value is freed using that function.
Since: 2.30
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
g_hash_table_iter_next()
returned TRUE
, and cannot be called
more than once for the same key/value pair.
If the GHashTable was created using g_hash_table_new_full()
,
the key and value are freed using the supplied destroy functions,
otherwise you have to make sure that any dynamically allocated
values are freed yourself.
It is safe to continue iterating the GHashTable afterward:
1 2 3 4 5 |
while (g_hash_table_iter_next (&iter, &key, &value)) { if (condition) g_hash_table_iter_remove (&iter); } |
Since: 2.16
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 g_hash_table_iter_next()
returned TRUE
, and cannot
be called more than once for the same key/value pair.
Since: 2.16
gboolean g_direct_equal (gconstpointer v1
,gconstpointer v2
);
Compares two gpointer arguments and returns TRUE
if they are equal.
It can be passed to g_hash_table_new()
as the key_equal_func
parameter, when using opaque pointers compared by pointer value as
keys in a GHashTable.
This equality function is also appropriate for keys that are integers
stored in pointers, such as GINT_TO_POINTER (n)
.
guint
g_direct_hash (gconstpointer v
);
Converts a gpointer to a hash value.
It can be passed to g_hash_table_new()
as the hash_func
parameter,
when using opaque pointers compared by pointer value as keys in a
GHashTable.
This hash function is also appropriate for keys that are integers
stored in pointers, such as GINT_TO_POINTER (n)
.
gboolean g_int_equal (gconstpointer v1
,gconstpointer v2
);
Compares the two gint values being pointed to and returns
TRUE
if they are equal.
It can be passed to g_hash_table_new()
as the key_equal_func
parameter, when using non-NULL
pointers to integers as keys in a
GHashTable.
Note that this function acts on pointers to gint, not on gint
directly: if your hash table's keys are of the form
GINT_TO_POINTER (n)
, use g_direct_equal()
instead.
guint
g_int_hash (gconstpointer v
);
Converts a pointer to a gint to a hash value.
It can be passed to g_hash_table_new()
as the hash_func
parameter,
when using non-NULL
pointers to integer values as keys in a GHashTable.
Note that this function acts on pointers to gint, not on gint
directly: if your hash table's keys are of the form
GINT_TO_POINTER (n)
, use g_direct_hash()
instead.
gboolean g_int64_equal (gconstpointer v1
,gconstpointer v2
);
Compares the two gint64 values being pointed to and returns
TRUE
if they are equal.
It can be passed to g_hash_table_new()
as the key_equal_func
parameter, when using non-NULL
pointers to 64-bit integers as keys in a
GHashTable.
Since: 2.22
guint
g_int64_hash (gconstpointer v
);
Converts a pointer to a gint64 to a hash value.
It can be passed to g_hash_table_new()
as the hash_func
parameter,
when using non-NULL
pointers to 64-bit integer values as keys in a
GHashTable.
Since: 2.22
gboolean g_double_equal (gconstpointer v1
,gconstpointer v2
);
Compares the two gdouble values being pointed to and returns
TRUE
if they are equal.
It can be passed to g_hash_table_new()
as the key_equal_func
parameter, when using non-NULL
pointers to doubles as keys in a
GHashTable.
Since: 2.22
guint
g_double_hash (gconstpointer v
);
Converts a pointer to a gdouble to a hash value.
It can be passed to g_hash_table_new()
as the hash_func
parameter,
It can be passed to g_hash_table_new()
as the hash_func
parameter,
when using non-NULL
pointers to doubles as keys in a GHashTable.
Since: 2.22
gboolean g_str_equal (gconstpointer v1
,gconstpointer v2
);
Compares two strings for byte-by-byte equality and returns TRUE
if they are equal. It can be passed to g_hash_table_new()
as the
key_equal_func
parameter, when using non-NULL
strings as keys in a
GHashTable.
Note that this function is primarily meant as a hash table comparison
function. For a general-purpose, NULL
-safe string comparison function,
see g_strcmp0()
.
guint
g_str_hash (gconstpointer v
);
Converts a string to a hash value.
This function implements the widely used "djb" hash apparently
posted by Daniel Bernstein to comp.lang.c some time ago. The 32
bit unsigned hash value starts at 5381 and for each byte 'c' in
the string, is updated: hash = hash * 33 + c
. This function
uses the signed value of each byte.
It can be passed to g_hash_table_new()
as the hash_func
parameter,
when using non-NULL
strings as keys in a GHashTable.
Note that this function may not be a perfect fit for all use cases. For example, it produces some hash collisions with strings as short as 2.
typedef 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.
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()
.