second try.
[platform/upstream/glib.git] / docs / reference / glib / tmpl / hash_tables.sgml
1 <!-- ##### SECTION Title ##### -->
2 Hash Tables
3
4 <!-- ##### SECTION Short_Description ##### -->
5 associations between keys and values so that given a key the value
6 can be found quickly.
7
8 <!-- ##### SECTION Long_Description ##### -->
9 <para>
10 A #GHashTable provides associations between keys and values which
11 is optimized so that given a key, the associated value can be found
12 very quickly.
13 </para>
14 <para>
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.
20 </para>
21 <para>
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.
28 </para>
29 <para>
30 To create a #GHashTable, use g_hash_table_new().
31 </para>
32 <para>
33 To insert a key and value into a #GHashTable, use g_hash_table_insert().
34 </para>
35 <para>
36 To lookup a value corresponding to a given key, use g_hash_table_lookup()
37 and g_hash_table_lookup_extended().
38 </para>
39 <para>
40 To remove a key and value, use g_hash_table_remove().
41 </para>
42 <para>
43 To call a function for each key and value pair use g_hash_table_foreach().
44 </para>
45 <para>
46 To destroy a #GHashTable use g_hash_table_destroy().
47 </para>
48
49 <!-- ##### SECTION See_Also ##### -->
50 <para>
51
52 </para>
53
54 <!-- ##### STRUCT GHashTable ##### -->
55 <para>
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.
59 </para>
60
61
62 <!-- ##### FUNCTION g_hash_table_new ##### -->
63 <para>
64 Creates a new #GHashTable.
65 </para>
66
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.
79
80
81 <!-- ##### USER_FUNCTION GHashFunc ##### -->
82 <para>
83 Specifies the type of the hash function which is passed to
84 g_hash_table_new() when a #GHashTable is created.
85 </para>
86 <para>
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 
90 #gchar* respectively.
91 </para>
92 <para>
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
98 lookup.
99 </para>
100
101 @key: a key.
102 @Returns: the hash value corresponding to the key.
103
104
105 <!-- ##### USER_FUNCTION GEqualFunc ##### -->
106 <para>
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
109 FALSE otherwise.
110 </para>
111
112 @a: a value.
113 @b: a value to compare with.
114 @Returns: TRUE if @a = @b; FALSE otherwise.
115
116
117 <!-- ##### FUNCTION g_hash_table_insert ##### -->
118 <para>
119 Inserts a new key and value into a #GHashTable.
120 If the key already exists in the #GHashTable its current value is replaced
121 with the new value.
122 </para>
123 <note>
124 <para>
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
128 new key and value.
129 </para>
130 </note>
131
132 @hash_table: a #GHashTable.
133 @key: a key to insert.
134 @value: the value to associate with the key.
135
136
137 <!-- ##### FUNCTION g_hash_table_size ##### -->
138 <para>
139 Returns the number of key/value pairs in a #GHashTable.
140 </para>
141
142 @hash_table: a #GHashTable.
143 @Returns: the number of key/value pairs in the #GHashTable.
144
145
146 <!-- ##### FUNCTION g_hash_table_lookup ##### -->
147 <para>
148 Looks up a key in the #GHashTable, returning the associated value or NULL
149 if the key is not found.
150 </para>
151
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.
155
156
157 <!-- ##### FUNCTION g_hash_table_lookup_extended ##### -->
158 <para>
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().
163 </para>
164
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.
170
171
172 <!-- ##### FUNCTION g_hash_table_foreach ##### -->
173 <para>
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.
177 </para>
178
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.
182
183
184 <!-- ##### USER_FUNCTION GHFunc ##### -->
185 <para>
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().
189 </para>
190
191 @key: a key.
192 @value: the value corresponding to the key.
193 @user_data: user data passed to g_hash_table_foreach().
194
195
196 <!-- ##### FUNCTION g_hash_table_remove ##### -->
197 <para>
198 Removes a key and its associated value from a #GHashTable.
199 </para>
200 <note>
201 <para>
202 As with g_hash_table_insert(), you should make sure that any dynamically
203 allocated values are freed yourself.
204 </para>
205 </note>
206
207 @hash_table: a #GHashTable.
208 @key: the key to remove.
209
210
211 <!-- ##### FUNCTION g_hash_table_foreach_remove ##### -->
212 <para>
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
215 #GHashTable.
216 </para>
217
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.
222
223
224 <!-- ##### USER_FUNCTION GHRFunc ##### -->
225 <para>
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
230 #GHashTable.
231 </para>
232
233 @key: a key.
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.
237
238
239 <!-- ##### FUNCTION g_hash_table_freeze ##### -->
240 <para>
241 This function is deprecated and will be removed in the next major
242 release of GLib. It does nothing.
243 </para>
244
245 @hash_table: 
246
247
248 <!-- ##### FUNCTION g_hash_table_thaw ##### -->
249 <para>
250 This function is deprecated and will be removed in the next major
251 release of GLib. It does nothing.
252 </para>
253
254 @hash_table: 
255
256
257 <!-- ##### FUNCTION g_hash_table_destroy ##### -->
258 <para>
259 Destroys the #GHashTable.
260 </para>
261 <note>
262 <para>
263 If keys and/or values are dynamically allocated, you should free them
264 first.
265 </para>
266 </note>
267
268 @hash_table: a #GHashTable.
269
270
271 <!-- ##### FUNCTION g_direct_equal ##### -->
272 <para>
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.
276 </para>
277
278 @v: a key.
279 @v2: a key to compare with @v.
280 @Returns: TRUE if the two keys match.
281
282
283 <!-- ##### FUNCTION g_direct_hash ##### -->
284 <para>
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.
288 </para>
289
290 @v: a gpointer key.
291 @Returns: a hash value corresponding to the key.
292
293
294 <!-- ##### FUNCTION g_int_equal ##### -->
295 <para>
296 Compares the two #gint values being pointed to and returns TRUE if they are
297 equal.
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.
300 </para>
301
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.
305
306
307 <!-- ##### FUNCTION g_int_hash ##### -->
308 <para>
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.
312 </para>
313
314 @v: a pointer to a #gint key.
315 @Returns: a hash value corresponding to the key.
316
317
318 <!-- ##### FUNCTION g_str_equal ##### -->
319 <para>
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.
323 </para>
324
325 @v: a key.
326 @v2: a key to compare with @v.
327 @Returns: TRUE if the two keys match.
328
329
330 <!-- ##### FUNCTION g_str_hash ##### -->
331 <para>
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.
335 </para>
336
337 @v: a string key.
338 @Returns: a hash value corresponding to the key.
339
340