fixed dealing with collection/lcopy of NULL values.
[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_compare_func: 
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.
81
82
83 <!-- ##### USER_FUNCTION GHashFunc ##### -->
84 <para>
85 Specifies the type of the hash function which is passed to
86 g_hash_table_new() when a #GHashTable is created.
87 </para>
88 <para>
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 
92 #gchar* respectively.
93 </para>
94 <para>
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
100 lookup.
101 </para>
102
103 @key: a key.
104 @Returns: the hash value corresponding to the key.
105
106
107 <!-- ##### FUNCTION g_hash_table_insert ##### -->
108 <para>
109 Inserts a new key and value into a #GHashTable.
110 If the key already exists in the #GHashTable its current value is replaced
111 with the new value.
112 </para>
113 <note>
114 <para>
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
118 new key and value.
119 </para>
120 </note>
121
122 @hash_table: a #GHashTable.
123 @key: a key to insert.
124 @value: the value to associate with the key.
125
126
127 <!-- ##### FUNCTION g_hash_table_size ##### -->
128 <para>
129 Returns the number of key/value pairs in a #GHashTable.
130 </para>
131
132 @hash_table: a #GHashTable.
133 @Returns: the number of key/value pairs in the #GHashTable.
134
135
136 <!-- ##### FUNCTION g_hash_table_lookup ##### -->
137 <para>
138 Looks up a key in the #GHashTable, returning the associated value or NULL
139 if the key is not found.
140 </para>
141
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.
145
146
147 <!-- ##### FUNCTION g_hash_table_lookup_extended ##### -->
148 <para>
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().
153 </para>
154
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.
160
161
162 <!-- ##### FUNCTION g_hash_table_foreach ##### -->
163 <para>
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.
167 </para>
168
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.
172
173
174 <!-- ##### USER_FUNCTION GHFunc ##### -->
175 <para>
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().
179 </para>
180
181 @key: a key.
182 @value: the value corresponding to the key.
183 @user_data: user data passed to g_hash_table_foreach().
184
185
186 <!-- ##### FUNCTION g_hash_table_remove ##### -->
187 <para>
188 Removes a key and its associated value from a #GHashTable.
189 </para>
190 <note>
191 <para>
192 As with g_hash_table_insert(), you should make sure that any dynamically
193 allocated values are freed yourself.
194 </para>
195 </note>
196
197 @hash_table: a #GHashTable.
198 @key: the key to remove.
199
200
201 <!-- ##### FUNCTION g_hash_table_foreach_remove ##### -->
202 <para>
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
205 #GHashTable.
206 </para>
207
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.
212
213
214 <!-- ##### USER_FUNCTION GHRFunc ##### -->
215 <para>
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
220 #GHashTable.
221 </para>
222
223 @key: a key.
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.
227
228
229 <!-- ##### FUNCTION g_hash_table_freeze ##### -->
230 <para>
231 This function is deprecated and will be removed in the next major
232 release of GLib. It does nothing.
233 </para>
234
235 @hash_table: 
236
237
238 <!-- ##### FUNCTION g_hash_table_thaw ##### -->
239 <para>
240 This function is deprecated and will be removed in the next major
241 release of GLib. It does nothing.
242 </para>
243
244 @hash_table: 
245
246
247 <!-- ##### FUNCTION g_hash_table_destroy ##### -->
248 <para>
249 Destroys the #GHashTable.
250 </para>
251 <note>
252 <para>
253 If keys and/or values are dynamically allocated, you should free them
254 first.
255 </para>
256 </note>
257
258 @hash_table: a #GHashTable.
259
260
261 <!-- ##### FUNCTION g_direct_equal ##### -->
262 <para>
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.
266 </para>
267
268 @v: a key.
269 @v2: a key to compare with @v.
270 @Returns: TRUE if the two keys match.
271
272
273 <!-- ##### FUNCTION g_direct_hash ##### -->
274 <para>
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.
278 </para>
279
280 @v: a gpointer key.
281 @Returns: a hash value corresponding to the key.
282
283
284 <!-- ##### FUNCTION g_int_equal ##### -->
285 <para>
286 Compares the two #gint values being pointed to and returns TRUE if they are
287 equal.
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.
290 </para>
291
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.
295
296
297 <!-- ##### FUNCTION g_int_hash ##### -->
298 <para>
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.
302 </para>
303
304 @v: a pointer to a #gint key.
305 @Returns: a hash value corresponding to the key.
306
307
308 <!-- ##### FUNCTION g_str_equal ##### -->
309 <para>
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.
313 </para>
314
315 @v: a key.
316 @v2: a key to compare with @v.
317 @Returns: TRUE if the two keys match.
318
319
320 <!-- ##### FUNCTION g_str_hash ##### -->
321 <para>
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.
325 </para>
326
327 @v: a string key.
328 @Returns: a hash value corresponding to the key.
329
330