glib/tmpl/arrays.sgml,glib/tmpl/arrays_byte.sgml
[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
65 </para>
66
67 @hash_func: 
68 @key_equal_func: 
69 @Returns: 
70
71
72 <!-- ##### FUNCTION g_hash_table_new_full ##### -->
73 <para>
74
75 </para>
76
77 @hash_func: 
78 @key_equal_func: 
79 @key_destroy_func: 
80 @value_destroy_func: 
81 @Returns: 
82
83
84 <!-- ##### USER_FUNCTION GHashFunc ##### -->
85 <para>
86 Specifies the type of the hash function which is passed to
87 g_hash_table_new() when a #GHashTable is created.
88 </para>
89 <para>
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 
93 #gchar* respectively.
94 </para>
95 <para>
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
101 lookup.
102 </para>
103
104 @key: a key.
105 @Returns: the hash value corresponding to the key.
106
107
108 <!-- ##### USER_FUNCTION GEqualFunc ##### -->
109 <para>
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
112 %FALSE otherwise.
113 </para>
114
115 @a: a value.
116 @b: a value to compare with.
117 @Returns: %TRUE if @a = @b; %FALSE otherwise.
118
119
120 <!-- ##### FUNCTION g_hash_table_insert ##### -->
121 <para>
122
123 </para>
124
125 @hash_table: 
126 @key: 
127 @value: 
128
129
130 <!-- ##### FUNCTION g_hash_table_replace ##### -->
131 <para>
132
133 </para>
134
135 @hash_table: 
136 @key: 
137 @value: 
138
139
140 <!-- ##### FUNCTION g_hash_table_size ##### -->
141 <para>
142
143 </para>
144
145 @hash_table: 
146 @Returns: 
147
148
149 <!-- ##### FUNCTION g_hash_table_lookup ##### -->
150 <para>
151
152 </para>
153
154 @hash_table: 
155 @key: 
156 @Returns: 
157
158
159 <!-- ##### FUNCTION g_hash_table_lookup_extended ##### -->
160 <para>
161
162 </para>
163
164 @hash_table: 
165 @lookup_key: 
166 @orig_key: 
167 @value: 
168 @Returns: 
169
170
171 <!-- ##### FUNCTION g_hash_table_foreach ##### -->
172 <para>
173
174 </para>
175
176 @hash_table: 
177 @func: 
178 @user_data: 
179
180
181 <!-- ##### USER_FUNCTION GHFunc ##### -->
182 <para>
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().
186 </para>
187
188 @key: a key.
189 @value: the value corresponding to the key.
190 @user_data: user data passed to g_hash_table_foreach().
191
192
193 <!-- ##### FUNCTION g_hash_table_remove ##### -->
194 <para>
195
196 </para>
197
198 @hash_table: 
199 @key: 
200 @Returns: 
201
202
203 <!-- ##### FUNCTION g_hash_table_steal ##### -->
204 <para>
205
206 </para>
207
208 @hash_table: 
209 @key: 
210 @Returns: 
211
212
213 <!-- ##### FUNCTION g_hash_table_foreach_remove ##### -->
214 <para>
215
216 </para>
217
218 @hash_table: 
219 @func: 
220 @user_data: 
221 @Returns: 
222
223
224 <!-- ##### FUNCTION g_hash_table_foreach_steal ##### -->
225 <para>
226
227 </para>
228
229 @hash_table: 
230 @func: 
231 @user_data: 
232 @Returns: 
233
234
235 <!-- ##### USER_FUNCTION GHRFunc ##### -->
236 <para>
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
241 #GHashTable.
242 </para>
243
244 @key: a key.
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.
248
249
250 <!-- ##### MACRO g_hash_table_freeze ##### -->
251 <para>
252 This function is deprecated and will be removed in the next major
253  release of GLib. It does nothing.
254 </para>
255
256 @hash_table: 
257
258
259 <!-- ##### MACRO g_hash_table_thaw ##### -->
260 <para>
261 This function is deprecated and will be removed in the next major
262  release of GLib. It does nothing.
263 </para>
264
265 @hash_table: 
266
267
268 <!-- ##### FUNCTION g_hash_table_destroy ##### -->
269 <para>
270
271 </para>
272
273 @hash_table: 
274
275
276 <!-- ##### FUNCTION g_direct_equal ##### -->
277 <para>
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.
281 </para>
282
283 @v: a key.
284 @v2: a key to compare with @v.
285 @Returns: %TRUE if the two keys match.
286
287
288 <!-- ##### FUNCTION g_direct_hash ##### -->
289 <para>
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.
293 </para>
294
295 @v: a gpointer key.
296 @Returns: a hash value corresponding to the key.
297
298
299 <!-- ##### FUNCTION g_int_equal ##### -->
300 <para>
301 Compares the two #gint values being pointed to and returns %TRUE if they are
302 equal.
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.
305 </para>
306
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.
310
311
312 <!-- ##### FUNCTION g_int_hash ##### -->
313 <para>
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.
317 </para>
318
319 @v: a pointer to a #gint key.
320 @Returns: a hash value corresponding to the key.
321
322
323 <!-- ##### FUNCTION g_str_equal ##### -->
324 <para>
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.
328 </para>
329
330 @v: a key.
331 @v2: a key to compare with @v.
332 @Returns: %TRUE if the two keys match.
333
334
335 <!-- ##### FUNCTION g_str_hash ##### -->
336 <para>
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.
340 </para>
341
342 @v: a string key.
343 @Returns: a hash value corresponding to the key.
344
345