1 <!-- ##### SECTION Title ##### -->
4 <!-- ##### SECTION Short_Description ##### -->
5 associations between keys and values so that given a key the value
8 <!-- ##### SECTION Long_Description ##### -->
10 A #GHashTable provides associations between keys and values which
11 is optimized so that given a key, the associated value can be found
15 Note that neither keys nor values are copied when inserted into the
16 #GHashTable, so they must exist for the lifetime of the #GHashTable.
17 This means that the use of static strings is OK, but temporary
18 strings (i.e. those created in buffers and those returned by GTK widgets)
19 should be copied with g_strdup() before being inserted.
22 If keys or values are dynamically allocated, you must be careful to ensure
23 that they are freed when they are removed from the #GHashTable, and also
24 when they are overwritten by new insertions into the #GHashTable.
25 It is also not advisable to mix static strings and dynamically-allocated
26 strings in a #GHashTable, because it then becomes difficult to determine
27 whether the string should be freed.
30 To create a #GHashTable, use g_hash_table_new().
33 To insert a key and value into a #GHashTable, use g_hash_table_insert().
36 To lookup a value corresponding to a given key, use g_hash_table_lookup()
37 and g_hash_table_lookup_extended().
40 To remove a key and value, use g_hash_table_remove().
43 To call a function for each key and value pair use g_hash_table_foreach().
46 To destroy a #GHashTable use g_hash_table_destroy().
49 <!-- ##### SECTION See_Also ##### -->
54 <!-- ##### STRUCT GHashTable ##### -->
56 The #GHashTable struct is an opaque data structure to represent a
57 <link linkend="glib-Hash-Tables">Hash Table</link>.
58 It should only be accessed via the following functions.
62 <!-- ##### FUNCTION g_hash_table_new ##### -->
72 <!-- ##### FUNCTION g_hash_table_new_full ##### -->
84 <!-- ##### USER_FUNCTION GHashFunc ##### -->
86 Specifies the type of the hash function which is passed to
87 g_hash_table_new() when a #GHashTable is created.
90 The function is passed a key and should return a #guint hash value.
91 The functions g_direct_hash(), g_int_hash() and g_str_hash() provide
92 hash functions which can be used when the key is a #gpointer, #gint, and
96 FIXME: Need more here.
97 The hash values should be evenly distributed over a fairly large range?
98 The modulus is taken with the hash table size (a prime number)
99 to find the 'bucket' to place each key into.
100 The function should also be very fast, since it is called for each key
105 @Returns: the hash value corresponding to the key.
108 <!-- ##### USER_FUNCTION GEqualFunc ##### -->
110 Specifies the type of a function used to test two values for
111 equality. The function should return %TRUE if both values are equal and
116 @b: a value to compare with.
117 @Returns: %TRUE if @a = @b; %FALSE otherwise.
120 <!-- ##### FUNCTION g_hash_table_insert ##### -->
130 <!-- ##### FUNCTION g_hash_table_replace ##### -->
140 <!-- ##### FUNCTION g_hash_table_size ##### -->
149 <!-- ##### FUNCTION g_hash_table_lookup ##### -->
159 <!-- ##### FUNCTION g_hash_table_lookup_extended ##### -->
171 <!-- ##### FUNCTION g_hash_table_foreach ##### -->
181 <!-- ##### USER_FUNCTION GHFunc ##### -->
183 Specifies the type of the function passed to g_hash_table_foreach().
184 It is called with each key/value pair, together with the @user_data parameter
185 which is passed to g_hash_table_foreach().
189 @value: the value corresponding to the key.
190 @user_data: user data passed to g_hash_table_foreach().
193 <!-- ##### FUNCTION g_hash_table_remove ##### -->
203 <!-- ##### FUNCTION g_hash_table_steal ##### -->
213 <!-- ##### FUNCTION g_hash_table_foreach_remove ##### -->
224 <!-- ##### FUNCTION g_hash_table_foreach_steal ##### -->
235 <!-- ##### USER_FUNCTION GHRFunc ##### -->
237 Specifies the type of the function passed to g_hash_table_foreach_remove().
238 It is called with each key/value pair, together with the @user_data parameter
239 passed to g_hash_table_foreach_remove().
240 It should return %TRUE if the key/value pair should be removed from the
245 @value: the value associated with the key.
246 @user_data: user data passed to g_hash_table_remove().
247 @Returns: %TRUE if the key/value pair should be removed from the #GHashTable.
250 <!-- ##### MACRO g_hash_table_freeze ##### -->
252 This function is deprecated and will be removed in the next major
253 release of GLib. It does nothing.
259 <!-- ##### MACRO g_hash_table_thaw ##### -->
261 This function is deprecated and will be removed in the next major
262 release of GLib. It does nothing.
268 <!-- ##### FUNCTION g_hash_table_destroy ##### -->
276 <!-- ##### FUNCTION g_direct_equal ##### -->
278 Compares two #gpointer arguments and returns %TRUE if they are equal.
279 It can be passed to g_hash_table_new() as the @key_equal_func
280 parameter, when using pointers as keys in a #GHashTable.
284 @v2: a key to compare with @v.
285 @Returns: %TRUE if the two keys match.
288 <!-- ##### FUNCTION g_direct_hash ##### -->
290 Converts a gpointer to a hash value.
291 It can be passed to g_hash_table_new() as the @hash_func parameter, when
292 using gpointer values as keys in a #GHashTable.
296 @Returns: a hash value corresponding to the key.
299 <!-- ##### FUNCTION g_int_equal ##### -->
301 Compares the two #gint values being pointed to and returns %TRUE if they are
303 It can be passed to g_hash_table_new() as the @key_equal_func
304 parameter, when using pointers to integers as keys in a #GHashTable.
307 @v: a pointer to a #gint key.
308 @v2: a pointer to a #gint key to compare with @v.
309 @Returns: %TRUE if the two keys match.
312 <!-- ##### FUNCTION g_int_hash ##### -->
314 Converts a pointer to a #gint to a hash value.
315 It can be passed to g_hash_table_new() as the @hash_func parameter, when
316 using pointers to #gint values as keys in a #GHashTable.
319 @v: a pointer to a #gint key.
320 @Returns: a hash value corresponding to the key.
323 <!-- ##### FUNCTION g_str_equal ##### -->
325 Compares two strings and returns %TRUE if they are equal.
326 It can be passed to g_hash_table_new() as the @key_equal_func
327 parameter, when using strings as keys in a #GHashTable.
331 @v2: a key to compare with @v.
332 @Returns: %TRUE if the two keys match.
335 <!-- ##### FUNCTION g_str_hash ##### -->
337 Converts a string to a hash value.
338 It can be passed to g_hash_table_new() as the @hash_func parameter, when
339 using strings as keys in a #GHashTable.
343 @Returns: a hash value corresponding to the key.