]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
b75a7d8f A |
3 | /* |
4 | ****************************************************************************** | |
0f5d89e8 | 5 | * Copyright (C) 1997-2015, International Business Machines |
b75a7d8f A |
6 | * Corporation and others. All Rights Reserved. |
7 | ****************************************************************************** | |
8 | * Date Name Description | |
9 | * 03/22/00 aliu Adapted from original C++ ICU Hashtable. | |
10 | * 07/06/01 aliu Modified to support int32_t keys on | |
11 | * platforms with sizeof(void*) < 32. | |
12 | ****************************************************************************** | |
13 | */ | |
14 | ||
15 | #ifndef UHASH_H | |
16 | #define UHASH_H | |
17 | ||
18 | #include "unicode/utypes.h" | |
4388f060 A |
19 | #include "cmemory.h" |
20 | #include "uelement.h" | |
b331163b | 21 | #include "unicode/localpointer.h" |
b75a7d8f A |
22 | |
23 | /** | |
24 | * UHashtable stores key-value pairs and does moderately fast lookup | |
25 | * based on keys. It provides a good tradeoff between access time and | |
26 | * storage space. As elements are added to it, it grows to accomodate | |
27 | * them. By default, the table never shrinks, even if all elements | |
28 | * are removed from it. | |
29 | * | |
30 | * Keys and values are stored as void* pointers. These void* pointers | |
31 | * may be actual pointers to strings, objects, or any other structure | |
32 | * in memory, or they may simply be integral values cast to void*. | |
33 | * UHashtable doesn't care and manipulates them via user-supplied | |
34 | * functions. These functions hash keys, compare keys, delete keys, | |
35 | * and delete values. Some function pointers are optional (may be | |
36 | * NULL); others must be supplied. Several prebuilt functions exist | |
37 | * to handle common key types. | |
38 | * | |
39 | * UHashtable ownership of keys and values is flexible, and controlled | |
40 | * by whether or not the key deleter and value deleter functions are | |
41 | * set. If a void* key is actually a pointer to a deletable object, | |
42 | * then UHashtable can be made to delete that object by setting the | |
43 | * key deleter function pointer to a non-NULL value. If this is done, | |
44 | * then keys passed to uhash_put() are owned by the hashtable and will | |
45 | * be deleted by it at some point, either as keys are replaced, or | |
46 | * when uhash_close() is finally called. The same is true of values | |
47 | * and the value deleter function pointer. Keys passed to methods | |
48 | * other than uhash_put() are never owned by the hashtable. | |
49 | * | |
50 | * NULL values are not allowed. uhash_get() returns NULL to indicate | |
51 | * a key that is not in the table, and having a NULL value in the | |
52 | * table would generate an ambiguous result. If a key and a NULL | |
53 | * value is passed to uhash_put(), this has the effect of doing a | |
54 | * uhash_remove() on that key. This keeps uhash_get(), uhash_count(), | |
55 | * and uhash_nextElement() consistent with one another. | |
56 | * | |
57 | * To see everything in a hashtable, use uhash_nextElement() to | |
58 | * iterate through its contents. Each call to this function returns a | |
59 | * UHashElement pointer. A hash element contains a key, value, and | |
60 | * hashcode. During iteration an element may be deleted by calling | |
61 | * uhash_removeElement(); iteration may safely continue thereafter. | |
62 | * The uhash_remove() function may also be safely called in | |
2ca993e8 A |
63 | * mid-iteration. If uhash_put() is called during iteration, |
64 | * the iteration is still guaranteed to terminate reasonably, but | |
65 | * there is no guarantee that every element will be returned or that | |
66 | * some won't be returned more than once. | |
67 | * | |
68 | * Under no circumstances should the UHashElement returned by | |
69 | * uhash_nextElement be modified directly. | |
b75a7d8f A |
70 | * |
71 | * By default, the hashtable grows when necessary, but never shrinks, | |
72 | * even if all items are removed. For most applications this is | |
73 | * optimal. However, in a highly dynamic usage where memory is at a | |
74 | * premium, the table can be set to both grow and shrink by calling | |
75 | * uhash_setResizePolicy() with the policy U_GROW_AND_SHRINK. In a | |
76 | * situation where memory is critical and the client wants a table | |
77 | * that does not grow at all, the constant U_FIXED can be used. | |
78 | */ | |
79 | ||
80 | /******************************************************************** | |
81 | * Data Structures | |
82 | ********************************************************************/ | |
83 | ||
84 | U_CDECL_BEGIN | |
85 | ||
86 | /** | |
4388f060 A |
87 | * A key or value within a UHashtable. |
88 | * The hashing and comparison functions take a pointer to a | |
b75a7d8f | 89 | * UHashTok, but the deleter receives the void* pointer within it. |
b75a7d8f | 90 | */ |
4388f060 | 91 | typedef UElement UHashTok; |
b75a7d8f A |
92 | |
93 | /** | |
94 | * This is a single hash element. | |
95 | */ | |
96 | struct UHashElement { | |
97 | /* Reorder these elements to pack nicely if necessary */ | |
98 | int32_t hashcode; | |
99 | UHashTok value; | |
100 | UHashTok key; | |
101 | }; | |
102 | typedef struct UHashElement UHashElement; | |
103 | ||
104 | /** | |
105 | * A hashing function. | |
106 | * @param key A key stored in a hashtable | |
107 | * @return A NON-NEGATIVE hash code for parm. | |
108 | */ | |
109 | typedef int32_t U_CALLCONV UHashFunction(const UHashTok key); | |
110 | ||
111 | /** | |
4388f060 | 112 | * A key equality (boolean) comparison function. |
b75a7d8f | 113 | */ |
4388f060 A |
114 | typedef UElementsAreEqual UKeyComparator; |
115 | ||
b75a7d8f | 116 | /** |
4388f060 | 117 | * A value equality (boolean) comparison function. |
b75a7d8f | 118 | */ |
4388f060 A |
119 | typedef UElementsAreEqual UValueComparator; |
120 | ||
121 | /* see cmemory.h for UObjectDeleter and uprv_deleteUObject() */ | |
b75a7d8f A |
122 | |
123 | /** | |
124 | * This specifies whether or not, and how, the hastable resizes itself. | |
125 | * See uhash_setResizePolicy(). | |
126 | */ | |
127 | enum UHashResizePolicy { | |
128 | U_GROW, /* Grow on demand, do not shrink */ | |
129 | U_GROW_AND_SHRINK, /* Grow and shrink on demand */ | |
130 | U_FIXED /* Never change size */ | |
131 | }; | |
132 | ||
133 | /** | |
134 | * The UHashtable struct. Clients should treat this as an opaque data | |
135 | * type and manipulate it only through the uhash_... API. | |
136 | */ | |
137 | struct UHashtable { | |
138 | ||
139 | /* Main key-value pair storage array */ | |
140 | ||
141 | UHashElement *elements; | |
142 | ||
73c04bcf A |
143 | /* Function pointers */ |
144 | ||
145 | UHashFunction *keyHasher; /* Computes hash from key. | |
146 | * Never null. */ | |
147 | UKeyComparator *keyComparator; /* Compares keys for equality. | |
148 | * Never null. */ | |
149 | UValueComparator *valueComparator; /* Compares the values for equality */ | |
150 | ||
151 | UObjectDeleter *keyDeleter; /* Deletes keys when required. | |
152 | * If NULL won't do anything */ | |
153 | UObjectDeleter *valueDeleter; /* Deletes values when required. | |
154 | * If NULL won't do anything */ | |
155 | ||
b75a7d8f | 156 | /* Size parameters */ |
0f5d89e8 | 157 | |
b75a7d8f A |
158 | int32_t count; /* The number of key-value pairs in this table. |
159 | * 0 <= count <= length. In practice we | |
160 | * never let count == length (see code). */ | |
161 | int32_t length; /* The physical size of the arrays hashes, keys | |
162 | * and values. Must be prime. */ | |
b75a7d8f A |
163 | |
164 | /* Rehashing thresholds */ | |
0f5d89e8 | 165 | |
b75a7d8f A |
166 | int32_t highWaterMark; /* If count > highWaterMark, rehash */ |
167 | int32_t lowWaterMark; /* If count < lowWaterMark, rehash */ | |
168 | float highWaterRatio; /* 0..1; high water as a fraction of length */ | |
169 | float lowWaterRatio; /* 0..1; low water as a fraction of length */ | |
0f5d89e8 | 170 | |
46f4442e A |
171 | int8_t primeIndex; /* Index into our prime table for length. |
172 | * length == PRIMES[primeIndex] */ | |
73c04bcf | 173 | UBool allocated; /* Was this UHashtable allocated? */ |
b75a7d8f A |
174 | }; |
175 | typedef struct UHashtable UHashtable; | |
176 | ||
177 | U_CDECL_END | |
178 | ||
179 | /******************************************************************** | |
180 | * API | |
181 | ********************************************************************/ | |
182 | ||
183 | /** | |
184 | * Initialize a new UHashtable. | |
185 | * @param keyHash A pointer to the key hashing function. Must not be | |
186 | * NULL. | |
187 | * @param keyComp A pointer to the function that compares keys. Must | |
188 | * not be NULL. | |
189 | * @param status A pointer to an UErrorCode to receive any errors. | |
190 | * @return A pointer to a UHashtable, or 0 if an error occurred. | |
191 | * @see uhash_openSize | |
192 | */ | |
0f5d89e8 | 193 | U_CAPI UHashtable* U_EXPORT2 |
b75a7d8f A |
194 | uhash_open(UHashFunction *keyHash, |
195 | UKeyComparator *keyComp, | |
73c04bcf | 196 | UValueComparator *valueComp, |
b75a7d8f A |
197 | UErrorCode *status); |
198 | ||
199 | /** | |
200 | * Initialize a new UHashtable with a given initial size. | |
201 | * @param keyHash A pointer to the key hashing function. Must not be | |
202 | * NULL. | |
203 | * @param keyComp A pointer to the function that compares keys. Must | |
204 | * not be NULL. | |
205 | * @param size The initial capacity of this hash table. | |
206 | * @param status A pointer to an UErrorCode to receive any errors. | |
207 | * @return A pointer to a UHashtable, or 0 if an error occurred. | |
208 | * @see uhash_open | |
209 | */ | |
0f5d89e8 | 210 | U_CAPI UHashtable* U_EXPORT2 |
b75a7d8f A |
211 | uhash_openSize(UHashFunction *keyHash, |
212 | UKeyComparator *keyComp, | |
73c04bcf | 213 | UValueComparator *valueComp, |
b75a7d8f A |
214 | int32_t size, |
215 | UErrorCode *status); | |
216 | ||
73c04bcf A |
217 | /** |
218 | * Initialize an existing UHashtable. | |
219 | * @param keyHash A pointer to the key hashing function. Must not be | |
220 | * NULL. | |
221 | * @param keyComp A pointer to the function that compares keys. Must | |
222 | * not be NULL. | |
223 | * @param status A pointer to an UErrorCode to receive any errors. | |
224 | * @return A pointer to a UHashtable, or 0 if an error occurred. | |
225 | * @see uhash_openSize | |
226 | */ | |
0f5d89e8 | 227 | U_CAPI UHashtable* U_EXPORT2 |
73c04bcf A |
228 | uhash_init(UHashtable *hash, |
229 | UHashFunction *keyHash, | |
230 | UKeyComparator *keyComp, | |
231 | UValueComparator *valueComp, | |
232 | UErrorCode *status); | |
233 | ||
2ca993e8 A |
234 | /** |
235 | * Initialize an existing UHashtable. | |
236 | * @param keyHash A pointer to the key hashing function. Must not be | |
237 | * NULL. | |
238 | * @param keyComp A pointer to the function that compares keys. Must | |
239 | * not be NULL. | |
240 | * @param size The initial capacity of this hash table. | |
241 | * @param status A pointer to an UErrorCode to receive any errors. | |
242 | * @return A pointer to a UHashtable, or 0 if an error occurred. | |
243 | * @see uhash_openSize | |
244 | */ | |
0f5d89e8 | 245 | U_CAPI UHashtable* U_EXPORT2 |
2ca993e8 A |
246 | uhash_initSize(UHashtable *hash, |
247 | UHashFunction *keyHash, | |
248 | UKeyComparator *keyComp, | |
249 | UValueComparator *valueComp, | |
250 | int32_t size, | |
251 | UErrorCode *status); | |
252 | ||
b75a7d8f A |
253 | /** |
254 | * Close a UHashtable, releasing the memory used. | |
729e4ab9 | 255 | * @param hash The UHashtable to close. If hash is NULL no operation is performed. |
b75a7d8f | 256 | */ |
0f5d89e8 | 257 | U_CAPI void U_EXPORT2 |
b75a7d8f A |
258 | uhash_close(UHashtable *hash); |
259 | ||
260 | ||
261 | ||
262 | /** | |
263 | * Set the function used to hash keys. | |
264 | * @param hash The UHashtable to set | |
265 | * @param fn the function to be used hash keys; must not be NULL | |
266 | * @return the previous key hasher; non-NULL | |
267 | */ | |
0f5d89e8 | 268 | U_CAPI UHashFunction *U_EXPORT2 |
b75a7d8f A |
269 | uhash_setKeyHasher(UHashtable *hash, UHashFunction *fn); |
270 | ||
271 | /** | |
272 | * Set the function used to compare keys. The default comparison is a | |
273 | * void* pointer comparison. | |
274 | * @param hash The UHashtable to set | |
275 | * @param fn the function to be used compare keys; must not be NULL | |
276 | * @return the previous key comparator; non-NULL | |
277 | */ | |
0f5d89e8 | 278 | U_CAPI UKeyComparator *U_EXPORT2 |
b75a7d8f A |
279 | uhash_setKeyComparator(UHashtable *hash, UKeyComparator *fn); |
280 | ||
73c04bcf A |
281 | /** |
282 | * Set the function used to compare values. The default comparison is a | |
283 | * void* pointer comparison. | |
284 | * @param hash The UHashtable to set | |
285 | * @param fn the function to be used compare keys; must not be NULL | |
286 | * @return the previous key comparator; non-NULL | |
287 | */ | |
0f5d89e8 | 288 | U_CAPI UValueComparator *U_EXPORT2 |
73c04bcf A |
289 | uhash_setValueComparator(UHashtable *hash, UValueComparator *fn); |
290 | ||
b75a7d8f A |
291 | /** |
292 | * Set the function used to delete keys. If this function pointer is | |
293 | * NULL, this hashtable does not delete keys. If it is non-NULL, this | |
294 | * hashtable does delete keys. This function should be set once | |
295 | * before any elements are added to the hashtable and should not be | |
296 | * changed thereafter. | |
297 | * @param hash The UHashtable to set | |
298 | * @param fn the function to be used delete keys, or NULL | |
299 | * @return the previous key deleter; may be NULL | |
300 | */ | |
0f5d89e8 | 301 | U_CAPI UObjectDeleter *U_EXPORT2 |
b75a7d8f A |
302 | uhash_setKeyDeleter(UHashtable *hash, UObjectDeleter *fn); |
303 | ||
304 | /** | |
305 | * Set the function used to delete values. If this function pointer | |
306 | * is NULL, this hashtable does not delete values. If it is non-NULL, | |
307 | * this hashtable does delete values. This function should be set | |
308 | * once before any elements are added to the hashtable and should not | |
309 | * be changed thereafter. | |
310 | * @param hash The UHashtable to set | |
311 | * @param fn the function to be used delete values, or NULL | |
312 | * @return the previous value deleter; may be NULL | |
313 | */ | |
0f5d89e8 | 314 | U_CAPI UObjectDeleter *U_EXPORT2 |
b75a7d8f A |
315 | uhash_setValueDeleter(UHashtable *hash, UObjectDeleter *fn); |
316 | ||
317 | /** | |
318 | * Specify whether or not, and how, the hastable resizes itself. | |
319 | * By default, tables grow but do not shrink (policy U_GROW). | |
320 | * See enum UHashResizePolicy. | |
321 | * @param hash The UHashtable to set | |
322 | * @param policy The way the hashtable resizes itself, {U_GROW, U_GROW_AND_SHRINK, U_FIXED} | |
323 | */ | |
0f5d89e8 | 324 | U_CAPI void U_EXPORT2 |
b75a7d8f A |
325 | uhash_setResizePolicy(UHashtable *hash, enum UHashResizePolicy policy); |
326 | ||
327 | /** | |
328 | * Get the number of key-value pairs stored in a UHashtable. | |
329 | * @param hash The UHashtable to query. | |
330 | * @return The number of key-value pairs stored in hash. | |
331 | */ | |
0f5d89e8 | 332 | U_CAPI int32_t U_EXPORT2 |
b75a7d8f A |
333 | uhash_count(const UHashtable *hash); |
334 | ||
335 | /** | |
336 | * Put a (key=pointer, value=pointer) item in a UHashtable. If the | |
337 | * keyDeleter is non-NULL, then the hashtable owns 'key' after this | |
338 | * call. If the valueDeleter is non-NULL, then the hashtable owns | |
339 | * 'value' after this call. Storing a NULL value is the same as | |
340 | * calling uhash_remove(). | |
341 | * @param hash The target UHashtable. | |
342 | * @param key The key to store. | |
343 | * @param value The value to store, may be NULL (see above). | |
344 | * @param status A pointer to an UErrorCode to receive any errors. | |
345 | * @return The previous value, or NULL if none. | |
346 | * @see uhash_get | |
347 | */ | |
0f5d89e8 | 348 | U_CAPI void* U_EXPORT2 |
b75a7d8f A |
349 | uhash_put(UHashtable *hash, |
350 | void *key, | |
351 | void *value, | |
352 | UErrorCode *status); | |
353 | ||
354 | /** | |
355 | * Put a (key=integer, value=pointer) item in a UHashtable. | |
356 | * keyDeleter must be NULL. If the valueDeleter is non-NULL, then the | |
357 | * hashtable owns 'value' after this call. Storing a NULL value is | |
358 | * the same as calling uhash_remove(). | |
359 | * @param hash The target UHashtable. | |
360 | * @param key The integer key to store. | |
361 | * @param value The value to store, may be NULL (see above). | |
362 | * @param status A pointer to an UErrorCode to receive any errors. | |
363 | * @return The previous value, or NULL if none. | |
364 | * @see uhash_get | |
365 | */ | |
0f5d89e8 | 366 | U_CAPI void* U_EXPORT2 |
b75a7d8f A |
367 | uhash_iput(UHashtable *hash, |
368 | int32_t key, | |
369 | void* value, | |
370 | UErrorCode *status); | |
371 | ||
372 | /** | |
373 | * Put a (key=pointer, value=integer) item in a UHashtable. If the | |
374 | * keyDeleter is non-NULL, then the hashtable owns 'key' after this | |
375 | * call. valueDeleter must be NULL. Storing a 0 value is the same as | |
376 | * calling uhash_remove(). | |
377 | * @param hash The target UHashtable. | |
378 | * @param key The key to store. | |
379 | * @param value The integer value to store. | |
380 | * @param status A pointer to an UErrorCode to receive any errors. | |
381 | * @return The previous value, or 0 if none. | |
382 | * @see uhash_get | |
383 | */ | |
0f5d89e8 | 384 | U_CAPI int32_t U_EXPORT2 |
b75a7d8f A |
385 | uhash_puti(UHashtable *hash, |
386 | void* key, | |
387 | int32_t value, | |
388 | UErrorCode *status); | |
389 | ||
374ca955 A |
390 | /** |
391 | * Put a (key=integer, value=integer) item in a UHashtable. If the | |
392 | * keyDeleter is non-NULL, then the hashtable owns 'key' after this | |
393 | * call. valueDeleter must be NULL. Storing a 0 value is the same as | |
394 | * calling uhash_remove(). | |
395 | * @param hash The target UHashtable. | |
396 | * @param key The key to store. | |
397 | * @param value The integer value to store. | |
398 | * @param status A pointer to an UErrorCode to receive any errors. | |
399 | * @return The previous value, or 0 if none. | |
400 | * @see uhash_get | |
401 | */ | |
0f5d89e8 | 402 | U_CAPI int32_t U_EXPORT2 |
374ca955 A |
403 | uhash_iputi(UHashtable *hash, |
404 | int32_t key, | |
405 | int32_t value, | |
406 | UErrorCode *status); | |
407 | ||
b75a7d8f A |
408 | /** |
409 | * Retrieve a pointer value from a UHashtable using a pointer key, | |
410 | * as previously stored by uhash_put(). | |
411 | * @param hash The target UHashtable. | |
412 | * @param key A pointer key stored in a hashtable | |
413 | * @return The requested item, or NULL if not found. | |
414 | */ | |
0f5d89e8 A |
415 | U_CAPI void* U_EXPORT2 |
416 | uhash_get(const UHashtable *hash, | |
b75a7d8f A |
417 | const void *key); |
418 | ||
419 | /** | |
420 | * Retrieve a pointer value from a UHashtable using a integer key, | |
421 | * as previously stored by uhash_iput(). | |
422 | * @param hash The target UHashtable. | |
423 | * @param key An integer key stored in a hashtable | |
424 | * @return The requested item, or NULL if not found. | |
425 | */ | |
0f5d89e8 | 426 | U_CAPI void* U_EXPORT2 |
b75a7d8f A |
427 | uhash_iget(const UHashtable *hash, |
428 | int32_t key); | |
429 | ||
430 | /** | |
431 | * Retrieve an integer value from a UHashtable using a pointer key, | |
432 | * as previously stored by uhash_puti(). | |
433 | * @param hash The target UHashtable. | |
434 | * @param key A pointer key stored in a hashtable | |
435 | * @return The requested item, or 0 if not found. | |
436 | */ | |
0f5d89e8 | 437 | U_CAPI int32_t U_EXPORT2 |
b75a7d8f A |
438 | uhash_geti(const UHashtable *hash, |
439 | const void* key); | |
374ca955 A |
440 | /** |
441 | * Retrieve an integer value from a UHashtable using an integer key, | |
442 | * as previously stored by uhash_iputi(). | |
443 | * @param hash The target UHashtable. | |
444 | * @param key An integer key stored in a hashtable | |
445 | * @return The requested item, or 0 if not found. | |
446 | */ | |
0f5d89e8 | 447 | U_CAPI int32_t U_EXPORT2 |
374ca955 A |
448 | uhash_igeti(const UHashtable *hash, |
449 | int32_t key); | |
b75a7d8f A |
450 | |
451 | /** | |
452 | * Remove an item from a UHashtable stored by uhash_put(). | |
453 | * @param hash The target UHashtable. | |
454 | * @param key A key stored in a hashtable | |
455 | * @return The item removed, or NULL if not found. | |
456 | */ | |
0f5d89e8 | 457 | U_CAPI void* U_EXPORT2 |
b75a7d8f A |
458 | uhash_remove(UHashtable *hash, |
459 | const void *key); | |
460 | ||
461 | /** | |
462 | * Remove an item from a UHashtable stored by uhash_iput(). | |
463 | * @param hash The target UHashtable. | |
464 | * @param key An integer key stored in a hashtable | |
465 | * @return The item removed, or NULL if not found. | |
466 | */ | |
0f5d89e8 | 467 | U_CAPI void* U_EXPORT2 |
b75a7d8f A |
468 | uhash_iremove(UHashtable *hash, |
469 | int32_t key); | |
470 | ||
471 | /** | |
472 | * Remove an item from a UHashtable stored by uhash_puti(). | |
473 | * @param hash The target UHashtable. | |
474 | * @param key An key stored in a hashtable | |
475 | * @return The item removed, or 0 if not found. | |
476 | */ | |
0f5d89e8 | 477 | U_CAPI int32_t U_EXPORT2 |
b75a7d8f A |
478 | uhash_removei(UHashtable *hash, |
479 | const void* key); | |
480 | ||
374ca955 A |
481 | /** |
482 | * Remove an item from a UHashtable stored by uhash_iputi(). | |
483 | * @param hash The target UHashtable. | |
484 | * @param key An integer key stored in a hashtable | |
485 | * @return The item removed, or 0 if not found. | |
486 | */ | |
0f5d89e8 | 487 | U_CAPI int32_t U_EXPORT2 |
374ca955 A |
488 | uhash_iremovei(UHashtable *hash, |
489 | int32_t key); | |
490 | ||
b75a7d8f A |
491 | /** |
492 | * Remove all items from a UHashtable. | |
493 | * @param hash The target UHashtable. | |
494 | */ | |
0f5d89e8 | 495 | U_CAPI void U_EXPORT2 |
b75a7d8f A |
496 | uhash_removeAll(UHashtable *hash); |
497 | ||
498 | /** | |
499 | * Locate an element of a UHashtable. The caller must not modify the | |
500 | * returned object. The primary use of this function is to obtain the | |
501 | * stored key when it may not be identical to the search key. For | |
502 | * example, if the compare function is a case-insensitive string | |
503 | * compare, then the hash key may be desired in order to obtain the | |
504 | * canonical case corresponding to a search key. | |
505 | * @param hash The target UHashtable. | |
506 | * @param key A key stored in a hashtable | |
507 | * @return a hash element, or NULL if the key is not found. | |
508 | */ | |
0f5d89e8 | 509 | U_CAPI const UHashElement* U_EXPORT2 |
b75a7d8f A |
510 | uhash_find(const UHashtable *hash, const void* key); |
511 | ||
b331163b A |
512 | /** |
513 | * \def UHASH_FIRST | |
514 | * Constant for use with uhash_nextElement | |
515 | * @see uhash_nextElement | |
516 | */ | |
517 | #define UHASH_FIRST (-1) | |
518 | ||
b75a7d8f A |
519 | /** |
520 | * Iterate through the elements of a UHashtable. The caller must not | |
521 | * modify the returned object. However, uhash_removeElement() may be | |
522 | * called during iteration to remove an element from the table. | |
523 | * Iteration may safely be resumed afterwards. If uhash_put() is | |
524 | * called during iteration the iteration will then be out of sync and | |
525 | * should be restarted. | |
526 | * @param hash The target UHashtable. | |
b331163b | 527 | * @param pos This should be set to UHASH_FIRST initially, and left untouched |
b75a7d8f A |
528 | * thereafter. |
529 | * @return a hash element, or NULL if no further key-value pairs | |
530 | * exist in the table. | |
531 | */ | |
0f5d89e8 | 532 | U_CAPI const UHashElement* U_EXPORT2 |
b75a7d8f A |
533 | uhash_nextElement(const UHashtable *hash, |
534 | int32_t *pos); | |
535 | ||
536 | /** | |
537 | * Remove an element, returned by uhash_nextElement(), from the table. | |
538 | * Iteration may be safely continued afterwards. | |
539 | * @param hash The hashtable | |
540 | * @param e The element, returned by uhash_nextElement(), to remove. | |
541 | * Must not be NULL. Must not be an empty or deleted element (as long | |
542 | * as this was returned by uhash_nextElement() it will not be empty or | |
543 | * deleted). Note: Although this parameter is const, it will be | |
544 | * modified. | |
545 | * @return the value that was removed. | |
546 | */ | |
0f5d89e8 | 547 | U_CAPI void* U_EXPORT2 |
b75a7d8f A |
548 | uhash_removeElement(UHashtable *hash, const UHashElement* e); |
549 | ||
550 | /******************************************************************** | |
551 | * UHashTok convenience | |
552 | ********************************************************************/ | |
553 | ||
554 | /** | |
555 | * Return a UHashTok for an integer. | |
556 | * @param i The given integer | |
557 | * @return a UHashTok for an integer. | |
558 | */ | |
0f5d89e8 | 559 | /*U_CAPI UHashTok U_EXPORT2 |
73c04bcf | 560 | uhash_toki(int32_t i);*/ |
b75a7d8f A |
561 | |
562 | /** | |
563 | * Return a UHashTok for a pointer. | |
564 | * @param p The given pointer | |
565 | * @return a UHashTok for a pointer. | |
566 | */ | |
0f5d89e8 | 567 | /*U_CAPI UHashTok U_EXPORT2 |
73c04bcf | 568 | uhash_tokp(void* p);*/ |
b75a7d8f A |
569 | |
570 | /******************************************************************** | |
571 | * UChar* and char* Support Functions | |
572 | ********************************************************************/ | |
573 | ||
574 | /** | |
575 | * Generate a hash code for a null-terminated UChar* string. If the | |
576 | * string is not null-terminated do not use this function. Use | |
577 | * together with uhash_compareUChars. | |
578 | * @param key The string (const UChar*) to hash. | |
579 | * @return A hash code for the key. | |
580 | */ | |
0f5d89e8 | 581 | U_CAPI int32_t U_EXPORT2 |
b75a7d8f A |
582 | uhash_hashUChars(const UHashTok key); |
583 | ||
584 | /** | |
585 | * Generate a hash code for a null-terminated char* string. If the | |
586 | * string is not null-terminated do not use this function. Use | |
587 | * together with uhash_compareChars. | |
588 | * @param key The string (const char*) to hash. | |
589 | * @return A hash code for the key. | |
590 | */ | |
0f5d89e8 | 591 | U_CAPI int32_t U_EXPORT2 |
b75a7d8f A |
592 | uhash_hashChars(const UHashTok key); |
593 | ||
b75a7d8f A |
594 | /** |
595 | * Generate a case-insensitive hash code for a null-terminated char* | |
596 | * string. If the string is not null-terminated do not use this | |
597 | * function. Use together with uhash_compareIChars. | |
598 | * @param key The string (const char*) to hash. | |
599 | * @return A hash code for the key. | |
600 | */ | |
601 | U_CAPI int32_t U_EXPORT2 | |
602 | uhash_hashIChars(const UHashTok key); | |
603 | ||
604 | /** | |
605 | * Comparator for null-terminated UChar* strings. Use together with | |
606 | * uhash_hashUChars. | |
607 | * @param key1 The string for comparison | |
608 | * @param key2 The string for comparison | |
609 | * @return true if key1 and key2 are equal, return false otherwise. | |
610 | */ | |
0f5d89e8 | 611 | U_CAPI UBool U_EXPORT2 |
b75a7d8f A |
612 | uhash_compareUChars(const UHashTok key1, const UHashTok key2); |
613 | ||
614 | /** | |
615 | * Comparator for null-terminated char* strings. Use together with | |
616 | * uhash_hashChars. | |
617 | * @param key1 The string for comparison | |
618 | * @param key2 The string for comparison | |
619 | * @return true if key1 and key2 are equal, return false otherwise. | |
620 | */ | |
0f5d89e8 | 621 | U_CAPI UBool U_EXPORT2 |
b75a7d8f A |
622 | uhash_compareChars(const UHashTok key1, const UHashTok key2); |
623 | ||
624 | /** | |
625 | * Case-insensitive comparator for null-terminated char* strings. Use | |
626 | * together with uhash_hashIChars. | |
627 | * @param key1 The string for comparison | |
628 | * @param key2 The string for comparison | |
629 | * @return true if key1 and key2 are equal, return false otherwise. | |
630 | */ | |
0f5d89e8 | 631 | U_CAPI UBool U_EXPORT2 |
b75a7d8f A |
632 | uhash_compareIChars(const UHashTok key1, const UHashTok key2); |
633 | ||
634 | /******************************************************************** | |
635 | * UnicodeString Support Functions | |
636 | ********************************************************************/ | |
637 | ||
638 | /** | |
639 | * Hash function for UnicodeString* keys. | |
640 | * @param key The string (const char*) to hash. | |
641 | * @return A hash code for the key. | |
642 | */ | |
0f5d89e8 | 643 | U_CAPI int32_t U_EXPORT2 |
4388f060 | 644 | uhash_hashUnicodeString(const UElement key); |
b75a7d8f A |
645 | |
646 | /** | |
647 | * Hash function for UnicodeString* keys (case insensitive). | |
648 | * Make sure to use together with uhash_compareCaselessUnicodeString. | |
649 | * @param key The string (const char*) to hash. | |
650 | * @return A hash code for the key. | |
651 | */ | |
0f5d89e8 | 652 | U_CAPI int32_t U_EXPORT2 |
4388f060 | 653 | uhash_hashCaselessUnicodeString(const UElement key); |
b75a7d8f A |
654 | |
655 | /******************************************************************** | |
656 | * int32_t Support Functions | |
657 | ********************************************************************/ | |
658 | ||
659 | /** | |
660 | * Hash function for 32-bit integer keys. | |
661 | * @param key The string (const char*) to hash. | |
662 | * @return A hash code for the key. | |
663 | */ | |
0f5d89e8 | 664 | U_CAPI int32_t U_EXPORT2 |
b75a7d8f A |
665 | uhash_hashLong(const UHashTok key); |
666 | ||
667 | /** | |
668 | * Comparator function for 32-bit integer keys. | |
669 | * @param key1 The integer for comparison | |
670 | * @param Key2 The integer for comparison | |
671 | * @return true if key1 and key2 are equal, return false otherwise | |
672 | */ | |
0f5d89e8 | 673 | U_CAPI UBool U_EXPORT2 |
b75a7d8f A |
674 | uhash_compareLong(const UHashTok key1, const UHashTok key2); |
675 | ||
676 | /******************************************************************** | |
677 | * Other Support Functions | |
678 | ********************************************************************/ | |
679 | ||
680 | /** | |
681 | * Deleter for Hashtable objects. | |
682 | * @param obj The object to be deleted | |
683 | */ | |
0f5d89e8 | 684 | U_CAPI void U_EXPORT2 |
b75a7d8f A |
685 | uhash_deleteHashtable(void *obj); |
686 | ||
4388f060 | 687 | /* Use uprv_free() itself as a deleter for any key or value allocated using uprv_malloc. */ |
b75a7d8f | 688 | |
73c04bcf A |
689 | /** |
690 | * Checks if the given hash tables are equal or not. | |
691 | * @param hash1 | |
692 | * @param hash2 | |
693 | * @return true if the hashtables are equal and false if not. | |
694 | */ | |
0f5d89e8 | 695 | U_CAPI UBool U_EXPORT2 |
73c04bcf A |
696 | uhash_equals(const UHashtable* hash1, const UHashtable* hash2); |
697 | ||
b331163b A |
698 | |
699 | #if U_SHOW_CPLUSPLUS_API | |
700 | ||
701 | U_NAMESPACE_BEGIN | |
702 | ||
703 | /** | |
f3c0d7a5 A |
704 | * \class LocalUHashtablePointer |
705 | * "Smart pointer" class, closes a UHashtable via uhash_close(). | |
b331163b A |
706 | * For most methods see the LocalPointerBase base class. |
707 | * | |
708 | * @see LocalPointerBase | |
709 | * @see LocalPointer | |
710 | * @stable ICU 4.4 | |
711 | */ | |
712 | U_DEFINE_LOCAL_OPEN_POINTER(LocalUHashtablePointer, UHashtable, uhash_close); | |
713 | ||
714 | U_NAMESPACE_END | |
715 | ||
f3c0d7a5 | 716 | #endif // U_SHOW_CPLUSPLUS_API |
b331163b | 717 | |
b75a7d8f | 718 | #endif |