Changes due to deprecation update.
[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 <!-- ##### USER_FUNCTION GHashFunc ##### -->
73 <para>
74 Specifies the type of the hash function which is passed to
75 g_hash_table_new() when a #GHashTable is created.
76 </para>
77 <para>
78 The function is passed a key and should return a guint hash value.
79 The functions g_direct_hash(), g_int_hash() and g_str_hash() provide
80 hash functions which can be used when the key is a #gpointer, #gint, and 
81 #gchar* respectively.
82 </para>
83 <para>
84 FIXME: Need more here.
85 The hash values should be evenly distributed over a fairly large range?
86 The modulus is taken with the hash table size (a prime number)
87 to find the 'bucket' to place each key into.
88 The function should also be very fast, since it is called for each key
89 lookup.
90 </para>
91
92 @key: a key.
93 @Returns: the hash value corresponding to the key.
94
95
96 <!-- ##### USER_FUNCTION GEqualFunc ##### -->
97 <para>
98 Specifies the type of a function used to test two values for
99 equality. The function should return TRUE if both values are equal and
100 FALSE otherwise.
101 </para>
102
103 @a: a value.
104 @b: a value to compare with.
105 @Returns: TRUE if @a = @b; FALSE otherwise.
106
107
108 <!-- ##### FUNCTION g_hash_table_insert ##### -->
109 <para>
110
111 </para>
112
113 @hash_table: 
114 @key: 
115 @value: 
116
117
118 <!-- ##### FUNCTION g_hash_table_size ##### -->
119 <para>
120
121 </para>
122
123 @hash_table: 
124 @Returns: 
125
126
127 <!-- ##### FUNCTION g_hash_table_lookup ##### -->
128 <para>
129
130 </para>
131
132 @hash_table: 
133 @key: 
134 @Returns: 
135
136
137 <!-- ##### FUNCTION g_hash_table_lookup_extended ##### -->
138 <para>
139
140 </para>
141
142 @hash_table: 
143 @lookup_key: 
144 @orig_key: 
145 @value: 
146 @Returns: 
147
148
149 <!-- ##### FUNCTION g_hash_table_foreach ##### -->
150 <para>
151
152 </para>
153
154 @hash_table: 
155 @func: 
156 @user_data: 
157
158
159 <!-- ##### USER_FUNCTION GHFunc ##### -->
160 <para>
161 Specifies the type of the function passed to g_hash_table_foreach().
162 It is called with each key/value pair, together with the @user_data parameter
163 which is passed to g_hash_table_foreach().
164 </para>
165
166 @key: a key.
167 @value: the value corresponding to the key.
168 @user_data: user data passed to g_hash_table_foreach().
169
170
171 <!-- ##### FUNCTION g_hash_table_remove ##### -->
172 <para>
173
174 </para>
175
176 @hash_table: 
177 @key: 
178 @Returns: 
179
180
181 <!-- ##### FUNCTION g_hash_table_foreach_remove ##### -->
182 <para>
183
184 </para>
185
186 @hash_table: 
187 @func: 
188 @user_data: 
189 @Returns: 
190
191
192 <!-- ##### USER_FUNCTION GHRFunc ##### -->
193 <para>
194 Specifies the type of the function passed to g_hash_table_foreach_remove().
195 It is called with each key/value pair, together with the @user_data parameter
196 passed to g_hash_table_foreach_remove().
197 It should return TRUE if the key/value pair should be removed from the
198 #GHashTable.
199 </para>
200
201 @key: a key.
202 @value: the value associated with the key.
203 @user_data: user data passed to g_hash_table_remove().
204 @Returns: TRUE if the key/value pair should be removed from the #GHashTable.
205
206
207 <!-- ##### MACRO g_hash_table_freeze ##### -->
208 <para>
209 This function is deprecated and will be removed in the next major
210  release of GLib. It does nothing.
211 </para>
212
213 @hash_table: 
214
215
216 <!-- ##### MACRO g_hash_table_thaw ##### -->
217 <para>
218 This function is deprecated and will be removed in the next major
219  release of GLib. It does nothing.
220 </para>
221
222 @hash_table: 
223
224
225 <!-- ##### FUNCTION g_hash_table_destroy ##### -->
226 <para>
227
228 </para>
229
230 @hash_table: 
231
232
233 <!-- ##### FUNCTION g_direct_equal ##### -->
234 <para>
235 Compares two #gpointer arguments and returns TRUE if they are equal.
236 It can be passed to g_hash_table_new() as the @key_equal_func
237 parameter, when using pointers as keys in a #GHashTable.
238 </para>
239
240 @v: a key.
241 @v2: a key to compare with @v.
242 @Returns: TRUE if the two keys match.
243
244
245 <!-- ##### FUNCTION g_direct_hash ##### -->
246 <para>
247 Converts a gpointer to a hash value.
248 It can be passed to g_hash_table_new() as the @hash_func parameter, when
249 using gpointer values as keys in a #GHashTable.
250 </para>
251
252 @v: a gpointer key.
253 @Returns: a hash value corresponding to the key.
254
255
256 <!-- ##### FUNCTION g_int_equal ##### -->
257 <para>
258 Compares the two #gint values being pointed to and returns TRUE if they are
259 equal.
260 It can be passed to g_hash_table_new() as the @key_equal_func
261 parameter, when using pointers to integers as keys in a #GHashTable.
262 </para>
263
264 @v: a pointer to a #gint key.
265 @v2: a pointer to a #gint key to compare with @v.
266 @Returns: TRUE if the two keys match.
267
268
269 <!-- ##### FUNCTION g_int_hash ##### -->
270 <para>
271 Converts a pointer to a #gint to a hash value.
272 It can be passed to g_hash_table_new() as the @hash_func parameter, when
273 using pointers to gint values as keys in a #GHashTable.
274 </para>
275
276 @v: a pointer to a #gint key.
277 @Returns: a hash value corresponding to the key.
278
279
280 <!-- ##### FUNCTION g_str_equal ##### -->
281 <para>
282 Compares two strings and returns TRUE if they are equal.
283 It can be passed to g_hash_table_new() as the @key_equal_func
284 parameter, when using strings as keys in a #GHashTable.
285 </para>
286
287 @v: a key.
288 @v2: a key to compare with @v.
289 @Returns: TRUE if the two keys match.
290
291
292 <!-- ##### FUNCTION g_str_hash ##### -->
293 <para>
294 Converts a string to a hash value.
295 It can be passed to g_hash_table_new() as the @hash_func parameter, when
296 using strings as keys in a #GHashTable.
297 </para>
298
299 @v: a string key.
300 @Returns: a hash value corresponding to the key.
301
302