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 ##### -->
64 Creates a new #GHashTable.
67 @hash_func: a function to create a hash value from a key.
68 Hash values are used to determine where keys are stored within the
69 #GHashTable data structure.
70 The g_direct_hash(), g_int_hash() and g_str_hash() functions are provided for
71 some common types of keys. If hash_func is NULL, g_direct_hash() is used.
72 @key_equal_func: a function to check two keys for equality. This is
73 used when looking up keys in the #GHashTable. The g_direct_equal(),
74 g_int_equal() and g_str_equal() functions are provided for the most
75 common types of keys. If @key_equal_func is NULL, keys are compared
76 directly in a similar fashion to g_direct_equal(), but without the
77 overhead of a function call.
78 @Returns: a new #GHashTable.
81 <!-- ##### USER_FUNCTION GHashFunc ##### -->
83 Specifies the type of the hash function which is passed to
84 g_hash_table_new() when a #GHashTable is created.
87 The function is passed a key and should return a guint hash value.
88 The functions g_direct_hash(), g_int_hash() and g_str_hash() provide
89 hash functions which can be used when the key is a #gpointer, #gint, and
93 FIXME: Need more here.
94 The hash values should be evenly distributed over a fairly large range?
95 The modulus is taken with the hash table size (a prime number)
96 to find the 'bucket' to place each key into.
97 The function should also be very fast, since it is called for each key
102 @Returns: the hash value corresponding to the key.
105 <!-- ##### USER_FUNCTION GEqualFunc ##### -->
107 Specifies the type of a function used to test two values for
108 equality. The function should return TRUE if both values are equal and
113 @b: a value to compare with.
114 @Returns: TRUE if @a = @b; FALSE otherwise.
117 <!-- ##### FUNCTION g_hash_table_insert ##### -->
119 Inserts a new key and value into a #GHashTable.
120 If the key already exists in the #GHashTable its current value is replaced
125 If the keys or values use dynamically allocated memory, then you should
126 first check if the key already exists in the GHashTable. If it does,
127 you should free the existing key and/or value before inserting the
132 @hash_table: a #GHashTable.
133 @key: a key to insert.
134 @value: the value to associate with the key.
137 <!-- ##### FUNCTION g_hash_table_size ##### -->
139 Returns the number of key/value pairs in a #GHashTable.
142 @hash_table: a #GHashTable.
143 @Returns: the number of key/value pairs in the #GHashTable.
146 <!-- ##### FUNCTION g_hash_table_lookup ##### -->
148 Looks up a key in the #GHashTable, returning the associated value or NULL
149 if the key is not found.
152 @hash_table: a #GHashTable.
153 @key: the key to look up.
154 @Returns: the associated value, or NULL if the key is not found.
157 <!-- ##### FUNCTION g_hash_table_lookup_extended ##### -->
159 Looks up a key in the #GHashTable, returning the original key and the
160 associated value and a gboolean which is TRUE if the key was found.
161 This is useful if you need to free the memory allocated for the
162 original key, for example before calling g_hash_table_remove().
165 @hash_table: a #GHashTable.
166 @lookup_key: the key to look up.
167 @orig_key: returns the original key.
168 @value: returns the value associated with the key.
169 @Returns: TRUE if the key was found in the #GHashTable.
172 <!-- ##### FUNCTION g_hash_table_foreach ##### -->
174 Calls the given function for each of the key/value pairs in the #GHashTable.
175 The function is passed the key and value of each pair, and the given
176 @user_data parameter.
179 @hash_table: a #GHashTable.
180 @func: the function to call for each key/value pair.
181 @user_data: use data to pass to the function.
184 <!-- ##### USER_FUNCTION GHFunc ##### -->
186 Specifies the type of the function passed to g_hash_table_foreach().
187 It is called with each key/value pair, together with the @user_data parameter
188 which is passed to g_hash_table_foreach().
192 @value: the value corresponding to the key.
193 @user_data: user data passed to g_hash_table_foreach().
196 <!-- ##### FUNCTION g_hash_table_remove ##### -->
198 Removes a key and its associated value from a #GHashTable.
202 As with g_hash_table_insert(), you should make sure that any dynamically
203 allocated values are freed yourself.
207 @hash_table: a #GHashTable.
208 @key: the key to remove.
211 <!-- ##### FUNCTION g_hash_table_foreach_remove ##### -->
213 Calls the given function for each key/value pair in the #GHashTable.
214 If the function returns TRUE, then the key/value pair is removed from the
218 @hash_table: a #GHashTable.
219 @func: the function to call for each key/value pair.
220 @user_data: user data to pass to the function.
221 @Returns: the number of key/value paris removed.
224 <!-- ##### USER_FUNCTION GHRFunc ##### -->
226 Specifies the type of the function passed to g_hash_table_foreach_remove().
227 It is called with each key/value pair, together with the @user_data parameter
228 passed to g_hash_table_foreach_remove().
229 It should return TRUE if the key/value pair should be removed from the
234 @value: the value associated with the key.
235 @user_data: user data passed to g_hash_table_remove().
236 @Returns: TRUE if the key/value pair should be removed from the #GHashTable.
239 <!-- ##### FUNCTION g_hash_table_freeze ##### -->
241 This function is deprecated and will be removed in the next major
242 release of GLib. It does nothing.
248 <!-- ##### FUNCTION g_hash_table_thaw ##### -->
250 This function is deprecated and will be removed in the next major
251 release of GLib. It does nothing.
257 <!-- ##### FUNCTION g_hash_table_destroy ##### -->
259 Destroys the #GHashTable.
263 If keys and/or values are dynamically allocated, you should free them
268 @hash_table: a #GHashTable.
271 <!-- ##### FUNCTION g_direct_equal ##### -->
273 Compares two #gpointer arguments and returns TRUE if they are equal.
274 It can be passed to g_hash_table_new() as the @key_equal_func
275 parameter, when using pointers as keys in a #GHashTable.
279 @v2: a key to compare with @v.
280 @Returns: TRUE if the two keys match.
283 <!-- ##### FUNCTION g_direct_hash ##### -->
285 Converts a gpointer to a hash value.
286 It can be passed to g_hash_table_new() as the @hash_func parameter, when
287 using gpointer values as keys in a #GHashTable.
291 @Returns: a hash value corresponding to the key.
294 <!-- ##### FUNCTION g_int_equal ##### -->
296 Compares the two #gint values being pointed to and returns TRUE if they are
298 It can be passed to g_hash_table_new() as the @key_equal_func
299 parameter, when using pointers to integers as keys in a #GHashTable.
302 @v: a pointer to a #gint key.
303 @v2: a pointer to a #gint key to compare with @v.
304 @Returns: TRUE if the two keys match.
307 <!-- ##### FUNCTION g_int_hash ##### -->
309 Converts a pointer to a #gint to a hash value.
310 It can be passed to g_hash_table_new() as the @hash_func parameter, when
311 using pointers to gint values as keys in a #GHashTable.
314 @v: a pointer to a #gint key.
315 @Returns: a hash value corresponding to the key.
318 <!-- ##### FUNCTION g_str_equal ##### -->
320 Compares two strings and returns TRUE if they are equal.
321 It can be passed to g_hash_table_new() as the @key_equal_func
322 parameter, when using strings as keys in a #GHashTable.
326 @v2: a key to compare with @v.
327 @Returns: TRUE if the two keys match.
330 <!-- ##### FUNCTION g_str_hash ##### -->
332 Converts a string to a hash value.
333 It can be passed to g_hash_table_new() as the @hash_func parameter, when
334 using strings as keys in a #GHashTable.
338 @Returns: a hash value corresponding to the key.