uhash.h 24 KB

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