uhash.h 28 KB

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