Description
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().
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().
To destroy a GHashTable use g_hash_table_destroy().
Details
struct GHashTable
The GHashTable struct is an opaque data structure to represent a
Hash Table.
It should only be accessed via the following functions.
GHashFunc ()
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.
FIXME: Need more here.
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.
g_hash_table_insert ()
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.
Note: If the keys or values use dynamically allocated memory, then you should
first check if the key already exists in the GHashTable. If it does,
you should free the existing key and/or value before inserting the
new key and value.
g_hash_table_size ()
Returns the number of key/value pairs in a GHashTable.
g_hash_table_lookup ()
Looks up a key in the GHashTable, returning the associated value or NULL
if the key is not found.
g_hash_table_lookup_extended ()
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().
g_hash_table_foreach ()
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.
g_hash_table_remove ()
Removes a key and its associated value from a GHashTable.
Note: As with g_hash_table_insert(), you should make sure that any dynamically
allocated values are freed yourself.
g_hash_table_foreach_remove ()
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.
g_hash_table_freeze ()
void g_hash_table_freeze (GHashTable *hash_table); |
Disable resizing of a GHashTable.
This should be used if you need to make a lot of changes to a GHashTable
at once, as it reduces the number of times that the GHashTable is rebuilt.
You should call g_hash_table_thaw() after updating the GHashTable to
enable resizing again.
g_hash_table_thaw ()
Enables resizing of a GHashTable.
g_hash_table_destroy ()
void g_hash_table_destroy (GHashTable *hash_table); |
Destroys the GHashTable.
Note: If keys and/or values are dynamically allocated, you should free them
first.
g_direct_equal ()
Compares two gpointer arguments and returns TRUE if they are equal.
It can be passed to g_hash_table_new() as the key_compare_func
parameter, when using pointers as keys in a GHashTable.
g_direct_hash ()
Converts a gpointer to a hash value.
It can be passed to g_hash_table_new() as the hash_func parameter, when
using gpointer values as keys in a GHashTable.
g_int_equal ()
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_compare_func
parameter, when using pointers to integers as keys in a GHashTable.
g_int_hash ()
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 pointers to gint values as keys in a GHashTable.
g_str_equal ()
Compares two strings and returns TRUE if they are equal.
It can be passed to g_hash_table_new() as the key_compare_func
parameter, when using strings as keys in a GHashTable.
g_str_hash ()
Converts a string to a hash value.
It can be passed to g_hash_table_new() as the hash_func parameter, when
using strings as keys in a GHashTable.