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.
73 @Returns: a new #GHashTable.
74 <!-- # Unused Parameters # -->
75 @key_equal_func: a function to check two keys for equality. This is
76 used when looking up keys in the #GHashTable. The g_direct_equal(),
77 g_int_equal() and g_str_equal() functions are provided for the most
78 common types of keys. If @key_equal_func is NULL, keys are compared
79 directly in a similar fashion to g_direct_equal(), but without the
80 overhead of a function call.
83 <!-- ##### USER_FUNCTION GHashFunc ##### -->
85 Specifies the type of the hash function which is passed to
86 g_hash_table_new() when a #GHashTable is created.
89 The function is passed a key and should return a guint hash value.
90 The functions g_direct_hash(), g_int_hash() and g_str_hash() provide
91 hash functions which can be used when the key is a #gpointer, #gint, and
95 FIXME: Need more here.
96 The hash values should be evenly distributed over a fairly large range?
97 The modulus is taken with the hash table size (a prime number)
98 to find the 'bucket' to place each key into.
99 The function should also be very fast, since it is called for each key
104 @Returns: the hash value corresponding to the key.
107 <!-- ##### FUNCTION g_hash_table_insert ##### -->
109 Inserts a new key and value into a #GHashTable.
110 If the key already exists in the #GHashTable its current value is replaced
115 If the keys or values use dynamically allocated memory, then you should
116 first check if the key already exists in the GHashTable. If it does,
117 you should free the existing key and/or value before inserting the
122 @hash_table: a #GHashTable.
123 @key: a key to insert.
124 @value: the value to associate with the key.
127 <!-- ##### FUNCTION g_hash_table_size ##### -->
129 Returns the number of key/value pairs in a #GHashTable.
132 @hash_table: a #GHashTable.
133 @Returns: the number of key/value pairs in the #GHashTable.
136 <!-- ##### FUNCTION g_hash_table_lookup ##### -->
138 Looks up a key in the #GHashTable, returning the associated value or NULL
139 if the key is not found.
142 @hash_table: a #GHashTable.
143 @key: the key to look up.
144 @Returns: the associated value, or NULL if the key is not found.
147 <!-- ##### FUNCTION g_hash_table_lookup_extended ##### -->
149 Looks up a key in the #GHashTable, returning the original key and the
150 associated value and a gboolean which is TRUE if the key was found.
151 This is useful if you need to free the memory allocated for the
152 original key, for example before calling g_hash_table_remove().
155 @hash_table: a #GHashTable.
156 @lookup_key: the key to look up.
157 @orig_key: returns the original key.
158 @value: returns the value associated with the key.
159 @Returns: TRUE if the key was found in the #GHashTable.
162 <!-- ##### FUNCTION g_hash_table_foreach ##### -->
164 Calls the given function for each of the key/value pairs in the #GHashTable.
165 The function is passed the key and value of each pair, and the given
166 @user_data parameter.
169 @hash_table: a #GHashTable.
170 @func: the function to call for each key/value pair.
171 @user_data: use data to pass to the function.
174 <!-- ##### USER_FUNCTION GHFunc ##### -->
176 Specifies the type of the function passed to g_hash_table_foreach().
177 It is called with each key/value pair, together with the @user_data parameter
178 which is passed to g_hash_table_foreach().
182 @value: the value corresponding to the key.
183 @user_data: user data passed to g_hash_table_foreach().
186 <!-- ##### FUNCTION g_hash_table_remove ##### -->
188 Removes a key and its associated value from a #GHashTable.
192 As with g_hash_table_insert(), you should make sure that any dynamically
193 allocated values are freed yourself.
197 @hash_table: a #GHashTable.
198 @key: the key to remove.
201 <!-- ##### FUNCTION g_hash_table_foreach_remove ##### -->
203 Calls the given function for each key/value pair in the #GHashTable.
204 If the function returns TRUE, then the key/value pair is removed from the
208 @hash_table: a #GHashTable.
209 @func: the function to call for each key/value pair.
210 @user_data: user data to pass to the function.
211 @Returns: the number of key/value paris removed.
214 <!-- ##### USER_FUNCTION GHRFunc ##### -->
216 Specifies the type of the function passed to g_hash_table_foreach_remove().
217 It is called with each key/value pair, together with the @user_data parameter
218 passed to g_hash_table_foreach_remove().
219 It should return TRUE if the key/value pair should be removed from the
224 @value: the value associated with the key.
225 @user_data: user data passed to g_hash_table_remove().
226 @Returns: TRUE if the key/value pair should be removed from the #GHashTable.
229 <!-- ##### FUNCTION g_hash_table_freeze ##### -->
231 This function is deprecated and will be removed in the next major
232 release of GLib. It does nothing.
238 <!-- ##### FUNCTION g_hash_table_thaw ##### -->
240 This function is deprecated and will be removed in the next major
241 release of GLib. It does nothing.
247 <!-- ##### FUNCTION g_hash_table_destroy ##### -->
249 Destroys the #GHashTable.
253 If keys and/or values are dynamically allocated, you should free them
258 @hash_table: a #GHashTable.
261 <!-- ##### FUNCTION g_direct_equal ##### -->
263 Compares two #gpointer arguments and returns TRUE if they are equal.
264 It can be passed to g_hash_table_new() as the @key_equal_func
265 parameter, when using pointers as keys in a #GHashTable.
269 @v2: a key to compare with @v.
270 @Returns: TRUE if the two keys match.
273 <!-- ##### FUNCTION g_direct_hash ##### -->
275 Converts a gpointer to a hash value.
276 It can be passed to g_hash_table_new() as the @hash_func parameter, when
277 using gpointer values as keys in a #GHashTable.
281 @Returns: a hash value corresponding to the key.
284 <!-- ##### FUNCTION g_int_equal ##### -->
286 Compares the two #gint values being pointed to and returns TRUE if they are
288 It can be passed to g_hash_table_new() as the @key_equal_func
289 parameter, when using pointers to integers as keys in a #GHashTable.
292 @v: a pointer to a #gint key.
293 @v2: a pointer to a #gint key to compare with @v.
294 @Returns: TRUE if the two keys match.
297 <!-- ##### FUNCTION g_int_hash ##### -->
299 Converts a pointer to a #gint to a hash value.
300 It can be passed to g_hash_table_new() as the @hash_func parameter, when
301 using pointers to gint values as keys in a #GHashTable.
304 @v: a pointer to a #gint key.
305 @Returns: a hash value corresponding to the key.
308 <!-- ##### FUNCTION g_str_equal ##### -->
310 Compares two strings and returns TRUE if they are equal.
311 It can be passed to g_hash_table_new() as the @key_equal_func
312 parameter, when using strings as keys in a #GHashTable.
316 @v2: a key to compare with @v.
317 @Returns: TRUE if the two keys match.
320 <!-- ##### FUNCTION g_str_hash ##### -->
322 Converts a string to a hash value.
323 It can be passed to g_hash_table_new() as the @hash_func parameter, when
324 using strings as keys in a #GHashTable.
328 @Returns: a hash value corresponding to the key.