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