hash.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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-2014, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. ******************************************************************************
  8. * Date Name Description
  9. * 03/28/00 aliu Creation.
  10. ******************************************************************************
  11. */
  12. #ifndef HASH_H
  13. #define HASH_H
  14. #include "unicode/unistr.h"
  15. #include "unicode/uobject.h"
  16. #include "cmemory.h"
  17. #include "uhash.h"
  18. U_NAMESPACE_BEGIN
  19. /**
  20. * Hashtable is a thin C++ wrapper around UHashtable, a general-purpose void*
  21. * hashtable implemented in C. Hashtable is designed to be idiomatic and
  22. * easy-to-use in C++.
  23. *
  24. * Hashtable is an INTERNAL CLASS.
  25. */
  26. class U_COMMON_API Hashtable : public UMemory {
  27. UHashtable* hash;
  28. UHashtable hashObj;
  29. inline void init(UHashFunction *keyHash, UKeyComparator *keyComp, UValueComparator *valueComp, UErrorCode& status);
  30. inline void initSize(UHashFunction *keyHash, UKeyComparator *keyComp, UValueComparator *valueComp, int32_t size, UErrorCode& status);
  31. public:
  32. /**
  33. * Construct a hashtable
  34. * @param ignoreKeyCase If true, keys are case insensitive.
  35. * @param status Error code
  36. */
  37. inline Hashtable(UBool ignoreKeyCase, UErrorCode& status);
  38. /**
  39. * Construct a hashtable
  40. * @param ignoreKeyCase If true, keys are case insensitive.
  41. * @param size initial size allocation
  42. * @param status Error code
  43. */
  44. inline Hashtable(UBool ignoreKeyCase, int32_t size, UErrorCode& status);
  45. /**
  46. * Construct a hashtable
  47. * @param keyComp Comparator for comparing the keys
  48. * @param valueComp Comparator for comparing the values
  49. * @param status Error code
  50. */
  51. inline Hashtable(UKeyComparator *keyComp, UValueComparator *valueComp, UErrorCode& status);
  52. /**
  53. * Construct a hashtable
  54. * @param status Error code
  55. */
  56. inline Hashtable(UErrorCode& status);
  57. /**
  58. * Construct a hashtable, _disregarding any error_. Use this constructor
  59. * with caution.
  60. */
  61. inline Hashtable();
  62. /**
  63. * Non-virtual destructor; make this virtual if Hashtable is subclassed
  64. * in the future.
  65. */
  66. inline ~Hashtable();
  67. inline UObjectDeleter *setValueDeleter(UObjectDeleter *fn);
  68. inline int32_t count() const;
  69. inline void* put(const UnicodeString& key, void* value, UErrorCode& status);
  70. inline int32_t puti(const UnicodeString& key, int32_t value, UErrorCode& status);
  71. inline void* get(const UnicodeString& key) const;
  72. inline int32_t geti(const UnicodeString& key) const;
  73. inline void* remove(const UnicodeString& key);
  74. inline int32_t removei(const UnicodeString& key);
  75. inline void removeAll(void);
  76. inline const UHashElement* find(const UnicodeString& key) const;
  77. /**
  78. * @param pos - must be UHASH_FIRST on first call, and untouched afterwards.
  79. * @see uhash_nextElement
  80. */
  81. inline const UHashElement* nextElement(int32_t& pos) const;
  82. inline UKeyComparator* setKeyComparator(UKeyComparator*keyComp);
  83. inline UValueComparator* setValueComparator(UValueComparator* valueComp);
  84. inline UBool equals(const Hashtable& that) const;
  85. private:
  86. Hashtable(const Hashtable &other); // forbid copying of this class
  87. Hashtable &operator=(const Hashtable &other); // forbid copying of this class
  88. };
  89. /*********************************************************************
  90. * Implementation
  91. ********************************************************************/
  92. inline void Hashtable::init(UHashFunction *keyHash, UKeyComparator *keyComp,
  93. UValueComparator *valueComp, UErrorCode& status) {
  94. if (U_FAILURE(status)) {
  95. return;
  96. }
  97. uhash_init(&hashObj, keyHash, keyComp, valueComp, &status);
  98. if (U_SUCCESS(status)) {
  99. hash = &hashObj;
  100. uhash_setKeyDeleter(hash, uprv_deleteUObject);
  101. }
  102. }
  103. inline void Hashtable::initSize(UHashFunction *keyHash, UKeyComparator *keyComp,
  104. UValueComparator *valueComp, int32_t size, UErrorCode& status) {
  105. if (U_FAILURE(status)) {
  106. return;
  107. }
  108. uhash_initSize(&hashObj, keyHash, keyComp, valueComp, size, &status);
  109. if (U_SUCCESS(status)) {
  110. hash = &hashObj;
  111. uhash_setKeyDeleter(hash, uprv_deleteUObject);
  112. }
  113. }
  114. inline Hashtable::Hashtable(UKeyComparator *keyComp, UValueComparator *valueComp,
  115. UErrorCode& status) : hash(0) {
  116. init( uhash_hashUnicodeString, keyComp, valueComp, status);
  117. }
  118. inline Hashtable::Hashtable(UBool ignoreKeyCase, UErrorCode& status)
  119. : hash(0)
  120. {
  121. init(ignoreKeyCase ? uhash_hashCaselessUnicodeString
  122. : uhash_hashUnicodeString,
  123. ignoreKeyCase ? uhash_compareCaselessUnicodeString
  124. : uhash_compareUnicodeString,
  125. NULL,
  126. status);
  127. }
  128. inline Hashtable::Hashtable(UBool ignoreKeyCase, int32_t size, UErrorCode& status)
  129. : hash(0)
  130. {
  131. initSize(ignoreKeyCase ? uhash_hashCaselessUnicodeString
  132. : uhash_hashUnicodeString,
  133. ignoreKeyCase ? uhash_compareCaselessUnicodeString
  134. : uhash_compareUnicodeString,
  135. NULL, size,
  136. status);
  137. }
  138. inline Hashtable::Hashtable(UErrorCode& status)
  139. : hash(0)
  140. {
  141. init(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, status);
  142. }
  143. inline Hashtable::Hashtable()
  144. : hash(0)
  145. {
  146. UErrorCode status = U_ZERO_ERROR;
  147. init(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, status);
  148. }
  149. inline Hashtable::~Hashtable() {
  150. if (hash != NULL) {
  151. uhash_close(hash);
  152. }
  153. }
  154. inline UObjectDeleter *Hashtable::setValueDeleter(UObjectDeleter *fn) {
  155. return uhash_setValueDeleter(hash, fn);
  156. }
  157. inline int32_t Hashtable::count() const {
  158. return uhash_count(hash);
  159. }
  160. inline void* Hashtable::put(const UnicodeString& key, void* value, UErrorCode& status) {
  161. return uhash_put(hash, new UnicodeString(key), value, &status);
  162. }
  163. inline int32_t Hashtable::puti(const UnicodeString& key, int32_t value, UErrorCode& status) {
  164. return uhash_puti(hash, new UnicodeString(key), value, &status);
  165. }
  166. inline void* Hashtable::get(const UnicodeString& key) const {
  167. return uhash_get(hash, &key);
  168. }
  169. inline int32_t Hashtable::geti(const UnicodeString& key) const {
  170. return uhash_geti(hash, &key);
  171. }
  172. inline void* Hashtable::remove(const UnicodeString& key) {
  173. return uhash_remove(hash, &key);
  174. }
  175. inline int32_t Hashtable::removei(const UnicodeString& key) {
  176. return uhash_removei(hash, &key);
  177. }
  178. inline const UHashElement* Hashtable::find(const UnicodeString& key) const {
  179. return uhash_find(hash, &key);
  180. }
  181. inline const UHashElement* Hashtable::nextElement(int32_t& pos) const {
  182. return uhash_nextElement(hash, &pos);
  183. }
  184. inline void Hashtable::removeAll(void) {
  185. uhash_removeAll(hash);
  186. }
  187. inline UKeyComparator* Hashtable::setKeyComparator(UKeyComparator*keyComp){
  188. return uhash_setKeyComparator(hash, keyComp);
  189. }
  190. inline UValueComparator* Hashtable::setValueComparator(UValueComparator* valueComp){
  191. return uhash_setValueComparator(hash, valueComp);
  192. }
  193. inline UBool Hashtable::equals(const Hashtable& that)const{
  194. return uhash_equals(hash, that.hash);
  195. }
  196. U_NAMESPACE_END
  197. #endif