2 ******************************************************************************
3 * Copyright (C) 1997-2014, International Business Machines
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 ******************************************************************************
16 #include "unicode/utypes.h"
19 #include "unicode/localpointer.h"
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.
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.
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.
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.
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
61 * mid-iteration. However, if uhash_put() is called during iteration
62 * then the iteration will be out of sync. Under no circumstances
63 * should the UHashElement returned by uhash_nextElement be modified
66 * By default, the hashtable grows when necessary, but never shrinks,
67 * even if all items are removed. For most applications this is
68 * optimal. However, in a highly dynamic usage where memory is at a
69 * premium, the table can be set to both grow and shrink by calling
70 * uhash_setResizePolicy() with the policy U_GROW_AND_SHRINK. In a
71 * situation where memory is critical and the client wants a table
72 * that does not grow at all, the constant U_FIXED can be used.
75 /********************************************************************
77 ********************************************************************/
82 * A key or value within a UHashtable.
83 * The hashing and comparison functions take a pointer to a
84 * UHashTok, but the deleter receives the void* pointer within it.
86 typedef UElement UHashTok
;
89 * This is a single hash element.
92 /* Reorder these elements to pack nicely if necessary */
97 typedef struct UHashElement UHashElement
;
100 * A hashing function.
101 * @param key A key stored in a hashtable
102 * @return A NON-NEGATIVE hash code for parm.
104 typedef int32_t U_CALLCONV
UHashFunction(const UHashTok key
);
107 * A key equality (boolean) comparison function.
109 typedef UElementsAreEqual UKeyComparator
;
112 * A value equality (boolean) comparison function.
114 typedef UElementsAreEqual UValueComparator
;
116 /* see cmemory.h for UObjectDeleter and uprv_deleteUObject() */
119 * This specifies whether or not, and how, the hastable resizes itself.
120 * See uhash_setResizePolicy().
122 enum UHashResizePolicy
{
123 U_GROW
, /* Grow on demand, do not shrink */
124 U_GROW_AND_SHRINK
, /* Grow and shrink on demand */
125 U_FIXED
/* Never change size */
129 * The UHashtable struct. Clients should treat this as an opaque data
130 * type and manipulate it only through the uhash_... API.
134 /* Main key-value pair storage array */
136 UHashElement
*elements
;
138 /* Function pointers */
140 UHashFunction
*keyHasher
; /* Computes hash from key.
142 UKeyComparator
*keyComparator
; /* Compares keys for equality.
144 UValueComparator
*valueComparator
; /* Compares the values for equality */
146 UObjectDeleter
*keyDeleter
; /* Deletes keys when required.
147 * If NULL won't do anything */
148 UObjectDeleter
*valueDeleter
; /* Deletes values when required.
149 * If NULL won't do anything */
151 /* Size parameters */
153 int32_t count
; /* The number of key-value pairs in this table.
154 * 0 <= count <= length. In practice we
155 * never let count == length (see code). */
156 int32_t length
; /* The physical size of the arrays hashes, keys
157 * and values. Must be prime. */
159 /* Rehashing thresholds */
161 int32_t highWaterMark
; /* If count > highWaterMark, rehash */
162 int32_t lowWaterMark
; /* If count < lowWaterMark, rehash */
163 float highWaterRatio
; /* 0..1; high water as a fraction of length */
164 float lowWaterRatio
; /* 0..1; low water as a fraction of length */
166 int8_t primeIndex
; /* Index into our prime table for length.
167 * length == PRIMES[primeIndex] */
168 UBool allocated
; /* Was this UHashtable allocated? */
170 typedef struct UHashtable UHashtable
;
174 /********************************************************************
176 ********************************************************************/
179 * Initialize a new UHashtable.
180 * @param keyHash A pointer to the key hashing function. Must not be
182 * @param keyComp A pointer to the function that compares keys. Must
184 * @param status A pointer to an UErrorCode to receive any errors.
185 * @return A pointer to a UHashtable, or 0 if an error occurred.
186 * @see uhash_openSize
188 U_CAPI UHashtable
* U_EXPORT2
189 uhash_open(UHashFunction
*keyHash
,
190 UKeyComparator
*keyComp
,
191 UValueComparator
*valueComp
,
195 * Initialize a new UHashtable with a given initial size.
196 * @param keyHash A pointer to the key hashing function. Must not be
198 * @param keyComp A pointer to the function that compares keys. Must
200 * @param size The initial capacity of this hash table.
201 * @param status A pointer to an UErrorCode to receive any errors.
202 * @return A pointer to a UHashtable, or 0 if an error occurred.
205 U_CAPI UHashtable
* U_EXPORT2
206 uhash_openSize(UHashFunction
*keyHash
,
207 UKeyComparator
*keyComp
,
208 UValueComparator
*valueComp
,
213 * Initialize an existing UHashtable.
214 * @param keyHash A pointer to the key hashing function. Must not be
216 * @param keyComp A pointer to the function that compares keys. Must
218 * @param status A pointer to an UErrorCode to receive any errors.
219 * @return A pointer to a UHashtable, or 0 if an error occurred.
220 * @see uhash_openSize
222 U_CAPI UHashtable
* U_EXPORT2
223 uhash_init(UHashtable
*hash
,
224 UHashFunction
*keyHash
,
225 UKeyComparator
*keyComp
,
226 UValueComparator
*valueComp
,
230 * Close a UHashtable, releasing the memory used.
231 * @param hash The UHashtable to close. If hash is NULL no operation is performed.
233 U_CAPI
void U_EXPORT2
234 uhash_close(UHashtable
*hash
);
239 * Set the function used to hash keys.
240 * @param hash The UHashtable to set
241 * @param fn the function to be used hash keys; must not be NULL
242 * @return the previous key hasher; non-NULL
244 U_CAPI UHashFunction
*U_EXPORT2
245 uhash_setKeyHasher(UHashtable
*hash
, UHashFunction
*fn
);
248 * Set the function used to compare keys. The default comparison is a
249 * void* pointer comparison.
250 * @param hash The UHashtable to set
251 * @param fn the function to be used compare keys; must not be NULL
252 * @return the previous key comparator; non-NULL
254 U_CAPI UKeyComparator
*U_EXPORT2
255 uhash_setKeyComparator(UHashtable
*hash
, UKeyComparator
*fn
);
258 * Set the function used to compare values. The default comparison is a
259 * void* pointer comparison.
260 * @param hash The UHashtable to set
261 * @param fn the function to be used compare keys; must not be NULL
262 * @return the previous key comparator; non-NULL
264 U_CAPI UValueComparator
*U_EXPORT2
265 uhash_setValueComparator(UHashtable
*hash
, UValueComparator
*fn
);
268 * Set the function used to delete keys. If this function pointer is
269 * NULL, this hashtable does not delete keys. If it is non-NULL, this
270 * hashtable does delete keys. This function should be set once
271 * before any elements are added to the hashtable and should not be
272 * changed thereafter.
273 * @param hash The UHashtable to set
274 * @param fn the function to be used delete keys, or NULL
275 * @return the previous key deleter; may be NULL
277 U_CAPI UObjectDeleter
*U_EXPORT2
278 uhash_setKeyDeleter(UHashtable
*hash
, UObjectDeleter
*fn
);
281 * Set the function used to delete values. If this function pointer
282 * is NULL, this hashtable does not delete values. If it is non-NULL,
283 * this hashtable does delete values. This function should be set
284 * once before any elements are added to the hashtable and should not
285 * be changed thereafter.
286 * @param hash The UHashtable to set
287 * @param fn the function to be used delete values, or NULL
288 * @return the previous value deleter; may be NULL
290 U_CAPI UObjectDeleter
*U_EXPORT2
291 uhash_setValueDeleter(UHashtable
*hash
, UObjectDeleter
*fn
);
294 * Specify whether or not, and how, the hastable resizes itself.
295 * By default, tables grow but do not shrink (policy U_GROW).
296 * See enum UHashResizePolicy.
297 * @param hash The UHashtable to set
298 * @param policy The way the hashtable resizes itself, {U_GROW, U_GROW_AND_SHRINK, U_FIXED}
300 U_CAPI
void U_EXPORT2
301 uhash_setResizePolicy(UHashtable
*hash
, enum UHashResizePolicy policy
);
304 * Get the number of key-value pairs stored in a UHashtable.
305 * @param hash The UHashtable to query.
306 * @return The number of key-value pairs stored in hash.
308 U_CAPI
int32_t U_EXPORT2
309 uhash_count(const UHashtable
*hash
);
312 * Put a (key=pointer, value=pointer) item in a UHashtable. If the
313 * keyDeleter is non-NULL, then the hashtable owns 'key' after this
314 * call. If the valueDeleter is non-NULL, then the hashtable owns
315 * 'value' after this call. Storing a NULL value is the same as
316 * calling uhash_remove().
317 * @param hash The target UHashtable.
318 * @param key The key to store.
319 * @param value The value to store, may be NULL (see above).
320 * @param status A pointer to an UErrorCode to receive any errors.
321 * @return The previous value, or NULL if none.
324 U_CAPI
void* U_EXPORT2
325 uhash_put(UHashtable
*hash
,
331 * Put a (key=integer, value=pointer) item in a UHashtable.
332 * keyDeleter must be NULL. If the valueDeleter is non-NULL, then the
333 * hashtable owns 'value' after this call. Storing a NULL value is
334 * the same as calling uhash_remove().
335 * @param hash The target UHashtable.
336 * @param key The integer key to store.
337 * @param value The value to store, may be NULL (see above).
338 * @param status A pointer to an UErrorCode to receive any errors.
339 * @return The previous value, or NULL if none.
342 U_CAPI
void* U_EXPORT2
343 uhash_iput(UHashtable
*hash
,
349 * Put a (key=pointer, value=integer) item in a UHashtable. If the
350 * keyDeleter is non-NULL, then the hashtable owns 'key' after this
351 * call. valueDeleter must be NULL. Storing a 0 value is the same as
352 * calling uhash_remove().
353 * @param hash The target UHashtable.
354 * @param key The key to store.
355 * @param value The integer value to store.
356 * @param status A pointer to an UErrorCode to receive any errors.
357 * @return The previous value, or 0 if none.
360 U_CAPI
int32_t U_EXPORT2
361 uhash_puti(UHashtable
*hash
,
367 * Put a (key=integer, value=integer) item in a UHashtable. If the
368 * keyDeleter is non-NULL, then the hashtable owns 'key' after this
369 * call. valueDeleter must be NULL. Storing a 0 value is the same as
370 * calling uhash_remove().
371 * @param hash The target UHashtable.
372 * @param key The key to store.
373 * @param value The integer value to store.
374 * @param status A pointer to an UErrorCode to receive any errors.
375 * @return The previous value, or 0 if none.
378 U_CAPI
int32_t U_EXPORT2
379 uhash_iputi(UHashtable
*hash
,
385 * Retrieve a pointer value from a UHashtable using a pointer key,
386 * as previously stored by uhash_put().
387 * @param hash The target UHashtable.
388 * @param key A pointer key stored in a hashtable
389 * @return The requested item, or NULL if not found.
391 U_CAPI
void* U_EXPORT2
392 uhash_get(const UHashtable
*hash
,
396 * Retrieve a pointer value from a UHashtable using a integer key,
397 * as previously stored by uhash_iput().
398 * @param hash The target UHashtable.
399 * @param key An integer key stored in a hashtable
400 * @return The requested item, or NULL if not found.
402 U_CAPI
void* U_EXPORT2
403 uhash_iget(const UHashtable
*hash
,
407 * Retrieve an integer value from a UHashtable using a pointer key,
408 * as previously stored by uhash_puti().
409 * @param hash The target UHashtable.
410 * @param key A pointer key stored in a hashtable
411 * @return The requested item, or 0 if not found.
413 U_CAPI
int32_t U_EXPORT2
414 uhash_geti(const UHashtable
*hash
,
417 * Retrieve an integer value from a UHashtable using an integer key,
418 * as previously stored by uhash_iputi().
419 * @param hash The target UHashtable.
420 * @param key An integer key stored in a hashtable
421 * @return The requested item, or 0 if not found.
423 U_CAPI
int32_t U_EXPORT2
424 uhash_igeti(const UHashtable
*hash
,
428 * Remove an item from a UHashtable stored by uhash_put().
429 * @param hash The target UHashtable.
430 * @param key A key stored in a hashtable
431 * @return The item removed, or NULL if not found.
433 U_CAPI
void* U_EXPORT2
434 uhash_remove(UHashtable
*hash
,
438 * Remove an item from a UHashtable stored by uhash_iput().
439 * @param hash The target UHashtable.
440 * @param key An integer key stored in a hashtable
441 * @return The item removed, or NULL if not found.
443 U_CAPI
void* U_EXPORT2
444 uhash_iremove(UHashtable
*hash
,
448 * Remove an item from a UHashtable stored by uhash_puti().
449 * @param hash The target UHashtable.
450 * @param key An key stored in a hashtable
451 * @return The item removed, or 0 if not found.
453 U_CAPI
int32_t U_EXPORT2
454 uhash_removei(UHashtable
*hash
,
458 * Remove an item from a UHashtable stored by uhash_iputi().
459 * @param hash The target UHashtable.
460 * @param key An integer key stored in a hashtable
461 * @return The item removed, or 0 if not found.
463 U_CAPI
int32_t U_EXPORT2
464 uhash_iremovei(UHashtable
*hash
,
468 * Remove all items from a UHashtable.
469 * @param hash The target UHashtable.
471 U_CAPI
void U_EXPORT2
472 uhash_removeAll(UHashtable
*hash
);
475 * Locate an element of a UHashtable. The caller must not modify the
476 * returned object. The primary use of this function is to obtain the
477 * stored key when it may not be identical to the search key. For
478 * example, if the compare function is a case-insensitive string
479 * compare, then the hash key may be desired in order to obtain the
480 * canonical case corresponding to a search key.
481 * @param hash The target UHashtable.
482 * @param key A key stored in a hashtable
483 * @return a hash element, or NULL if the key is not found.
485 U_CAPI
const UHashElement
* U_EXPORT2
486 uhash_find(const UHashtable
*hash
, const void* key
);
490 * Constant for use with uhash_nextElement
491 * @see uhash_nextElement
493 #define UHASH_FIRST (-1)
496 * Iterate through the elements of a UHashtable. The caller must not
497 * modify the returned object. However, uhash_removeElement() may be
498 * called during iteration to remove an element from the table.
499 * Iteration may safely be resumed afterwards. If uhash_put() is
500 * called during iteration the iteration will then be out of sync and
501 * should be restarted.
502 * @param hash The target UHashtable.
503 * @param pos This should be set to UHASH_FIRST initially, and left untouched
505 * @return a hash element, or NULL if no further key-value pairs
506 * exist in the table.
508 U_CAPI
const UHashElement
* U_EXPORT2
509 uhash_nextElement(const UHashtable
*hash
,
513 * Remove an element, returned by uhash_nextElement(), from the table.
514 * Iteration may be safely continued afterwards.
515 * @param hash The hashtable
516 * @param e The element, returned by uhash_nextElement(), to remove.
517 * Must not be NULL. Must not be an empty or deleted element (as long
518 * as this was returned by uhash_nextElement() it will not be empty or
519 * deleted). Note: Although this parameter is const, it will be
521 * @return the value that was removed.
523 U_CAPI
void* U_EXPORT2
524 uhash_removeElement(UHashtable
*hash
, const UHashElement
* e
);
526 /********************************************************************
527 * UHashTok convenience
528 ********************************************************************/
531 * Return a UHashTok for an integer.
532 * @param i The given integer
533 * @return a UHashTok for an integer.
535 /*U_CAPI UHashTok U_EXPORT2
536 uhash_toki(int32_t i);*/
539 * Return a UHashTok for a pointer.
540 * @param p The given pointer
541 * @return a UHashTok for a pointer.
543 /*U_CAPI UHashTok U_EXPORT2
544 uhash_tokp(void* p);*/
546 /********************************************************************
547 * UChar* and char* Support Functions
548 ********************************************************************/
551 * Generate a hash code for a null-terminated UChar* string. If the
552 * string is not null-terminated do not use this function. Use
553 * together with uhash_compareUChars.
554 * @param key The string (const UChar*) to hash.
555 * @return A hash code for the key.
557 U_CAPI
int32_t U_EXPORT2
558 uhash_hashUChars(const UHashTok key
);
561 * Generate a hash code for a null-terminated char* string. If the
562 * string is not null-terminated do not use this function. Use
563 * together with uhash_compareChars.
564 * @param key The string (const char*) to hash.
565 * @return A hash code for the key.
567 U_CAPI
int32_t U_EXPORT2
568 uhash_hashChars(const UHashTok key
);
571 * Generate a case-insensitive hash code for a null-terminated char*
572 * string. If the string is not null-terminated do not use this
573 * function. Use together with uhash_compareIChars.
574 * @param key The string (const char*) to hash.
575 * @return A hash code for the key.
577 U_CAPI
int32_t U_EXPORT2
578 uhash_hashIChars(const UHashTok key
);
581 * Comparator for null-terminated UChar* strings. Use together with
583 * @param key1 The string for comparison
584 * @param key2 The string for comparison
585 * @return true if key1 and key2 are equal, return false otherwise.
587 U_CAPI UBool U_EXPORT2
588 uhash_compareUChars(const UHashTok key1
, const UHashTok key2
);
591 * Comparator for null-terminated char* strings. Use together with
593 * @param key1 The string for comparison
594 * @param key2 The string for comparison
595 * @return true if key1 and key2 are equal, return false otherwise.
597 U_CAPI UBool U_EXPORT2
598 uhash_compareChars(const UHashTok key1
, const UHashTok key2
);
601 * Case-insensitive comparator for null-terminated char* strings. Use
602 * together with uhash_hashIChars.
603 * @param key1 The string for comparison
604 * @param key2 The string for comparison
605 * @return true if key1 and key2 are equal, return false otherwise.
607 U_CAPI UBool U_EXPORT2
608 uhash_compareIChars(const UHashTok key1
, const UHashTok key2
);
610 /********************************************************************
611 * UnicodeString Support Functions
612 ********************************************************************/
615 * Hash function for UnicodeString* keys.
616 * @param key The string (const char*) to hash.
617 * @return A hash code for the key.
619 U_CAPI
int32_t U_EXPORT2
620 uhash_hashUnicodeString(const UElement key
);
623 * Hash function for UnicodeString* keys (case insensitive).
624 * Make sure to use together with uhash_compareCaselessUnicodeString.
625 * @param key The string (const char*) to hash.
626 * @return A hash code for the key.
628 U_CAPI
int32_t U_EXPORT2
629 uhash_hashCaselessUnicodeString(const UElement key
);
631 /********************************************************************
632 * int32_t Support Functions
633 ********************************************************************/
636 * Hash function for 32-bit integer keys.
637 * @param key The string (const char*) to hash.
638 * @return A hash code for the key.
640 U_CAPI
int32_t U_EXPORT2
641 uhash_hashLong(const UHashTok key
);
644 * Comparator function for 32-bit integer keys.
645 * @param key1 The integer for comparison
646 * @param Key2 The integer for comparison
647 * @return true if key1 and key2 are equal, return false otherwise
649 U_CAPI UBool U_EXPORT2
650 uhash_compareLong(const UHashTok key1
, const UHashTok key2
);
652 /********************************************************************
653 * Other Support Functions
654 ********************************************************************/
657 * Deleter for Hashtable objects.
658 * @param obj The object to be deleted
660 U_CAPI
void U_EXPORT2
661 uhash_deleteHashtable(void *obj
);
663 /* Use uprv_free() itself as a deleter for any key or value allocated using uprv_malloc. */
666 * Checks if the given hash tables are equal or not.
669 * @return true if the hashtables are equal and false if not.
671 U_CAPI UBool U_EXPORT2
672 uhash_equals(const UHashtable
* hash1
, const UHashtable
* hash2
);
675 #if U_SHOW_CPLUSPLUS_API
680 * \class LocalUResourceBundlePointer
681 * "Smart pointer" class, closes a UResourceBundle via ures_close().
682 * For most methods see the LocalPointerBase base class.
684 * @see LocalPointerBase
688 U_DEFINE_LOCAL_OPEN_POINTER(LocalUHashtablePointer
, UHashtable
, uhash_close
);