2 ******************************************************************************
3 * Copyright (C) 1997-2016, 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. 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.
66 * Under no circumstances should the UHashElement returned by
67 * uhash_nextElement be modified directly.
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.
78 /********************************************************************
80 ********************************************************************/
85 * A key or value within a UHashtable.
86 * The hashing and comparison functions take a pointer to a
87 * UHashTok, but the deleter receives the void* pointer within it.
89 typedef UElement UHashTok
;
92 * This is a single hash element.
95 /* Reorder these elements to pack nicely if necessary */
100 typedef struct UHashElement UHashElement
;
103 * A hashing function.
104 * @param key A key stored in a hashtable
105 * @return A NON-NEGATIVE hash code for parm.
107 typedef int32_t U_CALLCONV
UHashFunction(const UHashTok key
);
110 * A key equality (boolean) comparison function.
112 typedef UElementsAreEqual UKeyComparator
;
115 * A value equality (boolean) comparison function.
117 typedef UElementsAreEqual UValueComparator
;
119 /* see cmemory.h for UObjectDeleter and uprv_deleteUObject() */
122 * This specifies whether or not, and how, the hastable resizes itself.
123 * See uhash_setResizePolicy().
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 */
132 * The UHashtable struct. Clients should treat this as an opaque data
133 * type and manipulate it only through the uhash_... API.
137 /* Main key-value pair storage array */
139 UHashElement
*elements
;
141 /* Function pointers */
143 UHashFunction
*keyHasher
; /* Computes hash from key.
145 UKeyComparator
*keyComparator
; /* Compares keys for equality.
147 UValueComparator
*valueComparator
; /* Compares the values for equality */
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 */
154 /* Size parameters */
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. */
162 /* Rehashing thresholds */
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 */
169 int8_t primeIndex
; /* Index into our prime table for length.
170 * length == PRIMES[primeIndex] */
171 UBool allocated
; /* Was this UHashtable allocated? */
173 typedef struct UHashtable UHashtable
;
177 /********************************************************************
179 ********************************************************************/
182 * Initialize a new UHashtable.
183 * @param keyHash A pointer to the key hashing function. Must not be
185 * @param keyComp A pointer to the function that compares keys. Must
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
191 U_CAPI UHashtable
* U_EXPORT2
192 uhash_open(UHashFunction
*keyHash
,
193 UKeyComparator
*keyComp
,
194 UValueComparator
*valueComp
,
198 * Initialize a new UHashtable with a given initial size.
199 * @param keyHash A pointer to the key hashing function. Must not be
201 * @param keyComp A pointer to the function that compares keys. Must
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.
208 U_CAPI UHashtable
* U_EXPORT2
209 uhash_openSize(UHashFunction
*keyHash
,
210 UKeyComparator
*keyComp
,
211 UValueComparator
*valueComp
,
216 * Initialize an existing UHashtable.
217 * @param keyHash A pointer to the key hashing function. Must not be
219 * @param keyComp A pointer to the function that compares keys. Must
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
225 U_CAPI UHashtable
* U_EXPORT2
226 uhash_init(UHashtable
*hash
,
227 UHashFunction
*keyHash
,
228 UKeyComparator
*keyComp
,
229 UValueComparator
*valueComp
,
233 * Initialize an existing UHashtable.
234 * @param keyHash A pointer to the key hashing function. Must not be
236 * @param keyComp A pointer to the function that compares keys. Must
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
243 U_CAPI UHashtable
* U_EXPORT2
244 uhash_initSize(UHashtable
*hash
,
245 UHashFunction
*keyHash
,
246 UKeyComparator
*keyComp
,
247 UValueComparator
*valueComp
,
252 * Close a UHashtable, releasing the memory used.
253 * @param hash The UHashtable to close. If hash is NULL no operation is performed.
255 U_CAPI
void U_EXPORT2
256 uhash_close(UHashtable
*hash
);
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
266 U_CAPI UHashFunction
*U_EXPORT2
267 uhash_setKeyHasher(UHashtable
*hash
, UHashFunction
*fn
);
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
276 U_CAPI UKeyComparator
*U_EXPORT2
277 uhash_setKeyComparator(UHashtable
*hash
, UKeyComparator
*fn
);
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
286 U_CAPI UValueComparator
*U_EXPORT2
287 uhash_setValueComparator(UHashtable
*hash
, UValueComparator
*fn
);
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
299 U_CAPI UObjectDeleter
*U_EXPORT2
300 uhash_setKeyDeleter(UHashtable
*hash
, UObjectDeleter
*fn
);
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
312 U_CAPI UObjectDeleter
*U_EXPORT2
313 uhash_setValueDeleter(UHashtable
*hash
, UObjectDeleter
*fn
);
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}
322 U_CAPI
void U_EXPORT2
323 uhash_setResizePolicy(UHashtable
*hash
, enum UHashResizePolicy policy
);
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.
330 U_CAPI
int32_t U_EXPORT2
331 uhash_count(const UHashtable
*hash
);
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.
346 U_CAPI
void* U_EXPORT2
347 uhash_put(UHashtable
*hash
,
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.
364 U_CAPI
void* U_EXPORT2
365 uhash_iput(UHashtable
*hash
,
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.
382 U_CAPI
int32_t U_EXPORT2
383 uhash_puti(UHashtable
*hash
,
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.
400 U_CAPI
int32_t U_EXPORT2
401 uhash_iputi(UHashtable
*hash
,
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.
413 U_CAPI
void* U_EXPORT2
414 uhash_get(const UHashtable
*hash
,
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.
424 U_CAPI
void* U_EXPORT2
425 uhash_iget(const UHashtable
*hash
,
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.
435 U_CAPI
int32_t U_EXPORT2
436 uhash_geti(const UHashtable
*hash
,
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.
445 U_CAPI
int32_t U_EXPORT2
446 uhash_igeti(const UHashtable
*hash
,
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.
455 U_CAPI
void* U_EXPORT2
456 uhash_remove(UHashtable
*hash
,
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.
465 U_CAPI
void* U_EXPORT2
466 uhash_iremove(UHashtable
*hash
,
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.
475 U_CAPI
int32_t U_EXPORT2
476 uhash_removei(UHashtable
*hash
,
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.
485 U_CAPI
int32_t U_EXPORT2
486 uhash_iremovei(UHashtable
*hash
,
490 * Remove all items from a UHashtable.
491 * @param hash The target UHashtable.
493 U_CAPI
void U_EXPORT2
494 uhash_removeAll(UHashtable
*hash
);
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.
507 U_CAPI
const UHashElement
* U_EXPORT2
508 uhash_find(const UHashtable
*hash
, const void* key
);
512 * Constant for use with uhash_nextElement
513 * @see uhash_nextElement
515 #define UHASH_FIRST (-1)
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.
525 * @param pos This should be set to UHASH_FIRST initially, and left untouched
527 * @return a hash element, or NULL if no further key-value pairs
528 * exist in the table.
530 U_CAPI
const UHashElement
* U_EXPORT2
531 uhash_nextElement(const UHashtable
*hash
,
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
543 * @return the value that was removed.
545 U_CAPI
void* U_EXPORT2
546 uhash_removeElement(UHashtable
*hash
, const UHashElement
* e
);
548 /********************************************************************
549 * UHashTok convenience
550 ********************************************************************/
553 * Return a UHashTok for an integer.
554 * @param i The given integer
555 * @return a UHashTok for an integer.
557 /*U_CAPI UHashTok U_EXPORT2
558 uhash_toki(int32_t i);*/
561 * Return a UHashTok for a pointer.
562 * @param p The given pointer
563 * @return a UHashTok for a pointer.
565 /*U_CAPI UHashTok U_EXPORT2
566 uhash_tokp(void* p);*/
568 /********************************************************************
569 * UChar* and char* Support Functions
570 ********************************************************************/
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.
579 U_CAPI
int32_t U_EXPORT2
580 uhash_hashUChars(const UHashTok key
);
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.
589 U_CAPI
int32_t U_EXPORT2
590 uhash_hashChars(const UHashTok key
);
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.
599 U_CAPI
int32_t U_EXPORT2
600 uhash_hashIChars(const UHashTok key
);
603 * Comparator for null-terminated UChar* strings. Use together with
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.
609 U_CAPI UBool U_EXPORT2
610 uhash_compareUChars(const UHashTok key1
, const UHashTok key2
);
613 * Comparator for null-terminated char* strings. Use together with
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.
619 U_CAPI UBool U_EXPORT2
620 uhash_compareChars(const UHashTok key1
, const UHashTok key2
);
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.
629 U_CAPI UBool U_EXPORT2
630 uhash_compareIChars(const UHashTok key1
, const UHashTok key2
);
632 /********************************************************************
633 * UnicodeString Support Functions
634 ********************************************************************/
637 * Hash function for UnicodeString* keys.
638 * @param key The string (const char*) to hash.
639 * @return A hash code for the key.
641 U_CAPI
int32_t U_EXPORT2
642 uhash_hashUnicodeString(const UElement key
);
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.
650 U_CAPI
int32_t U_EXPORT2
651 uhash_hashCaselessUnicodeString(const UElement key
);
653 /********************************************************************
654 * int32_t Support Functions
655 ********************************************************************/
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.
662 U_CAPI
int32_t U_EXPORT2
663 uhash_hashLong(const UHashTok key
);
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
671 U_CAPI UBool U_EXPORT2
672 uhash_compareLong(const UHashTok key1
, const UHashTok key2
);
674 /********************************************************************
675 * Other Support Functions
676 ********************************************************************/
679 * Deleter for Hashtable objects.
680 * @param obj The object to be deleted
682 U_CAPI
void U_EXPORT2
683 uhash_deleteHashtable(void *obj
);
685 /* Use uprv_free() itself as a deleter for any key or value allocated using uprv_malloc. */
688 * Checks if the given hash tables are equal or not.
691 * @return true if the hashtables are equal and false if not.
693 U_CAPI UBool U_EXPORT2
694 uhash_equals(const UHashtable
* hash1
, const UHashtable
* hash2
);
697 #if U_SHOW_CPLUSPLUS_API
702 * \class LocalUResourceBundlePointer
703 * "Smart pointer" class, closes a UResourceBundle via ures_close().
704 * For most methods see the LocalPointerBase base class.
706 * @see LocalPointerBase
710 U_DEFINE_LOCAL_OPEN_POINTER(LocalUHashtablePointer
, UHashtable
, uhash_close
);