1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
5 * Copyright (C) 1997-2016, International Business Machines
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 ******************************************************************************
18 #include "unicode/utypes.h"
21 #include "unicode/localpointer.h"
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.
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.
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.
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.
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
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.
68 * Under no circumstances should the UHashElement returned by
69 * uhash_nextElement be modified directly.
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.
80 /********************************************************************
82 ********************************************************************/
87 * A key or value within a UHashtable.
88 * The hashing and comparison functions take a pointer to a
89 * UHashTok, but the deleter receives the void* pointer within it.
91 typedef UElement UHashTok
;
94 * This is a single hash element.
97 /* Reorder these elements to pack nicely if necessary */
102 typedef struct UHashElement UHashElement
;
105 * A hashing function.
106 * @param key A key stored in a hashtable
107 * @return A NON-NEGATIVE hash code for parm.
109 typedef int32_t U_CALLCONV
UHashFunction(const UHashTok key
);
112 * A key equality (boolean) comparison function.
114 typedef UElementsAreEqual UKeyComparator
;
117 * A value equality (boolean) comparison function.
119 typedef UElementsAreEqual UValueComparator
;
121 /* see cmemory.h for UObjectDeleter and uprv_deleteUObject() */
124 * This specifies whether or not, and how, the hastable resizes itself.
125 * See uhash_setResizePolicy().
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 */
134 * The UHashtable struct. Clients should treat this as an opaque data
135 * type and manipulate it only through the uhash_... API.
139 /* Main key-value pair storage array */
141 UHashElement
*elements
;
143 /* Function pointers */
145 UHashFunction
*keyHasher
; /* Computes hash from key.
147 UKeyComparator
*keyComparator
; /* Compares keys for equality.
149 UValueComparator
*valueComparator
; /* Compares the values for equality */
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 */
156 /* Size parameters */
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. */
164 /* Rehashing thresholds */
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 */
171 int8_t primeIndex
; /* Index into our prime table for length.
172 * length == PRIMES[primeIndex] */
173 UBool allocated
; /* Was this UHashtable allocated? */
175 typedef struct UHashtable UHashtable
;
179 /********************************************************************
181 ********************************************************************/
184 * Initialize a new UHashtable.
185 * @param keyHash A pointer to the key hashing function. Must not be
187 * @param keyComp A pointer to the function that compares keys. Must
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
193 U_CAPI UHashtable
* U_EXPORT2
194 uhash_open(UHashFunction
*keyHash
,
195 UKeyComparator
*keyComp
,
196 UValueComparator
*valueComp
,
200 * Initialize a new UHashtable with a given initial size.
201 * @param keyHash A pointer to the key hashing function. Must not be
203 * @param keyComp A pointer to the function that compares keys. Must
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.
210 U_CAPI UHashtable
* U_EXPORT2
211 uhash_openSize(UHashFunction
*keyHash
,
212 UKeyComparator
*keyComp
,
213 UValueComparator
*valueComp
,
218 * Initialize an existing UHashtable.
219 * @param keyHash A pointer to the key hashing function. Must not be
221 * @param keyComp A pointer to the function that compares keys. Must
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
227 U_CAPI UHashtable
* U_EXPORT2
228 uhash_init(UHashtable
*hash
,
229 UHashFunction
*keyHash
,
230 UKeyComparator
*keyComp
,
231 UValueComparator
*valueComp
,
235 * Initialize an existing UHashtable.
236 * @param keyHash A pointer to the key hashing function. Must not be
238 * @param keyComp A pointer to the function that compares keys. Must
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
245 U_CAPI UHashtable
* U_EXPORT2
246 uhash_initSize(UHashtable
*hash
,
247 UHashFunction
*keyHash
,
248 UKeyComparator
*keyComp
,
249 UValueComparator
*valueComp
,
254 * Close a UHashtable, releasing the memory used.
255 * @param hash The UHashtable to close. If hash is NULL no operation is performed.
257 U_CAPI
void U_EXPORT2
258 uhash_close(UHashtable
*hash
);
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
268 U_CAPI UHashFunction
*U_EXPORT2
269 uhash_setKeyHasher(UHashtable
*hash
, UHashFunction
*fn
);
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
278 U_CAPI UKeyComparator
*U_EXPORT2
279 uhash_setKeyComparator(UHashtable
*hash
, UKeyComparator
*fn
);
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
288 U_CAPI UValueComparator
*U_EXPORT2
289 uhash_setValueComparator(UHashtable
*hash
, UValueComparator
*fn
);
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
301 U_CAPI UObjectDeleter
*U_EXPORT2
302 uhash_setKeyDeleter(UHashtable
*hash
, UObjectDeleter
*fn
);
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
314 U_CAPI UObjectDeleter
*U_EXPORT2
315 uhash_setValueDeleter(UHashtable
*hash
, UObjectDeleter
*fn
);
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}
324 U_CAPI
void U_EXPORT2
325 uhash_setResizePolicy(UHashtable
*hash
, enum UHashResizePolicy policy
);
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.
332 U_CAPI
int32_t U_EXPORT2
333 uhash_count(const UHashtable
*hash
);
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.
348 U_CAPI
void* U_EXPORT2
349 uhash_put(UHashtable
*hash
,
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.
366 U_CAPI
void* U_EXPORT2
367 uhash_iput(UHashtable
*hash
,
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.
384 U_CAPI
int32_t U_EXPORT2
385 uhash_puti(UHashtable
*hash
,
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.
402 U_CAPI
int32_t U_EXPORT2
403 uhash_iputi(UHashtable
*hash
,
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.
415 U_CAPI
void* U_EXPORT2
416 uhash_get(const UHashtable
*hash
,
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.
426 U_CAPI
void* U_EXPORT2
427 uhash_iget(const UHashtable
*hash
,
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.
437 U_CAPI
int32_t U_EXPORT2
438 uhash_geti(const UHashtable
*hash
,
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.
447 U_CAPI
int32_t U_EXPORT2
448 uhash_igeti(const UHashtable
*hash
,
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.
457 U_CAPI
void* U_EXPORT2
458 uhash_remove(UHashtable
*hash
,
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.
467 U_CAPI
void* U_EXPORT2
468 uhash_iremove(UHashtable
*hash
,
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.
477 U_CAPI
int32_t U_EXPORT2
478 uhash_removei(UHashtable
*hash
,
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.
487 U_CAPI
int32_t U_EXPORT2
488 uhash_iremovei(UHashtable
*hash
,
492 * Remove all items from a UHashtable.
493 * @param hash The target UHashtable.
495 U_CAPI
void U_EXPORT2
496 uhash_removeAll(UHashtable
*hash
);
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.
509 U_CAPI
const UHashElement
* U_EXPORT2
510 uhash_find(const UHashtable
*hash
, const void* key
);
514 * Constant for use with uhash_nextElement
515 * @see uhash_nextElement
517 #define UHASH_FIRST (-1)
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.
527 * @param pos This should be set to UHASH_FIRST initially, and left untouched
529 * @return a hash element, or NULL if no further key-value pairs
530 * exist in the table.
532 U_CAPI
const UHashElement
* U_EXPORT2
533 uhash_nextElement(const UHashtable
*hash
,
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
545 * @return the value that was removed.
547 U_CAPI
void* U_EXPORT2
548 uhash_removeElement(UHashtable
*hash
, const UHashElement
* e
);
550 /********************************************************************
551 * UHashTok convenience
552 ********************************************************************/
555 * Return a UHashTok for an integer.
556 * @param i The given integer
557 * @return a UHashTok for an integer.
559 /*U_CAPI UHashTok U_EXPORT2
560 uhash_toki(int32_t i);*/
563 * Return a UHashTok for a pointer.
564 * @param p The given pointer
565 * @return a UHashTok for a pointer.
567 /*U_CAPI UHashTok U_EXPORT2
568 uhash_tokp(void* p);*/
570 /********************************************************************
571 * UChar* and char* Support Functions
572 ********************************************************************/
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.
581 U_CAPI
int32_t U_EXPORT2
582 uhash_hashUChars(const UHashTok key
);
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.
591 U_CAPI
int32_t U_EXPORT2
592 uhash_hashChars(const UHashTok key
);
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.
601 U_CAPI
int32_t U_EXPORT2
602 uhash_hashIChars(const UHashTok key
);
605 * Comparator for null-terminated UChar* strings. Use together with
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.
611 U_CAPI UBool U_EXPORT2
612 uhash_compareUChars(const UHashTok key1
, const UHashTok key2
);
615 * Comparator for null-terminated char* strings. Use together with
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.
621 U_CAPI UBool U_EXPORT2
622 uhash_compareChars(const UHashTok key1
, const UHashTok key2
);
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.
631 U_CAPI UBool U_EXPORT2
632 uhash_compareIChars(const UHashTok key1
, const UHashTok key2
);
634 /********************************************************************
635 * UnicodeString Support Functions
636 ********************************************************************/
639 * Hash function for UnicodeString* keys.
640 * @param key The string (const char*) to hash.
641 * @return A hash code for the key.
643 U_CAPI
int32_t U_EXPORT2
644 uhash_hashUnicodeString(const UElement key
);
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.
652 U_CAPI
int32_t U_EXPORT2
653 uhash_hashCaselessUnicodeString(const UElement key
);
655 /********************************************************************
656 * int32_t Support Functions
657 ********************************************************************/
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.
664 U_CAPI
int32_t U_EXPORT2
665 uhash_hashLong(const UHashTok key
);
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
673 U_CAPI UBool U_EXPORT2
674 uhash_compareLong(const UHashTok key1
, const UHashTok key2
);
676 /********************************************************************
677 * Other Support Functions
678 ********************************************************************/
681 * Deleter for Hashtable objects.
682 * @param obj The object to be deleted
684 U_CAPI
void U_EXPORT2
685 uhash_deleteHashtable(void *obj
);
687 /* Use uprv_free() itself as a deleter for any key or value allocated using uprv_malloc. */
690 * Checks if the given hash tables are equal or not.
693 * @return true if the hashtables are equal and false if not.
695 U_CAPI UBool U_EXPORT2
696 uhash_equals(const UHashtable
* hash1
, const UHashtable
* hash2
);
699 #if U_SHOW_CPLUSPLUS_API
704 * \class LocalUHashtablePointer
705 * "Smart pointer" class, closes a UHashtable via uhash_close().
706 * For most methods see the LocalPointerBase base class.
708 * @see LocalPointerBase
712 U_DEFINE_LOCAL_OPEN_POINTER(LocalUHashtablePointer
, UHashtable
, uhash_close
);
716 #endif // U_SHOW_CPLUSPLUS_API