hash.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 int32_t putiAllowZero(const UnicodeString& key, int32_t value, UErrorCode& status);
  72. inline void* get(const UnicodeString& key) const;
  73. inline int32_t geti(const UnicodeString& key) const;
  74. inline int32_t getiAndFound(const UnicodeString& key, UBool &found) const;
  75. inline void* remove(const UnicodeString& key);
  76. inline int32_t removei(const UnicodeString& key);
  77. inline void removeAll();
  78. inline UBool containsKey(const UnicodeString& key) const;
  79. inline const UHashElement* find(const UnicodeString& key) const;
  80. /**
  81. * @param pos - must be UHASH_FIRST on first call, and untouched afterwards.
  82. * @see uhash_nextElement
  83. */
  84. inline const UHashElement* nextElement(int32_t& pos) const;
  85. inline UKeyComparator* setKeyComparator(UKeyComparator*keyComp);
  86. inline UValueComparator* setValueComparator(UValueComparator* valueComp);
  87. inline UBool equals(const Hashtable& that) const;
  88. private:
  89. Hashtable(const Hashtable &other) = delete; // forbid copying of this class
  90. Hashtable &operator=(const Hashtable &other) = delete; // forbid copying of this class
  91. };
  92. /*********************************************************************
  93. * Implementation
  94. ********************************************************************/
  95. inline void Hashtable::init(UHashFunction *keyHash, UKeyComparator *keyComp,
  96. UValueComparator *valueComp, UErrorCode& status) {
  97. if (U_FAILURE(status)) {
  98. return;
  99. }
  100. uhash_init(&hashObj, keyHash, keyComp, valueComp, &status);
  101. if (U_SUCCESS(status)) {
  102. hash = &hashObj;
  103. uhash_setKeyDeleter(hash, uprv_deleteUObject);
  104. }
  105. }
  106. inline void Hashtable::initSize(UHashFunction *keyHash, UKeyComparator *keyComp,
  107. UValueComparator *valueComp, int32_t size, UErrorCode& status) {
  108. if (U_FAILURE(status)) {
  109. return;
  110. }
  111. uhash_initSize(&hashObj, keyHash, keyComp, valueComp, size, &status);
  112. if (U_SUCCESS(status)) {
  113. hash = &hashObj;
  114. uhash_setKeyDeleter(hash, uprv_deleteUObject);
  115. }
  116. }
  117. inline Hashtable::Hashtable(UKeyComparator *keyComp, UValueComparator *valueComp,
  118. UErrorCode& status) : hash(0) {
  119. init( uhash_hashUnicodeString, keyComp, valueComp, status);
  120. }
  121. inline Hashtable::Hashtable(UBool ignoreKeyCase, UErrorCode& status)
  122. : hash(0)
  123. {
  124. init(ignoreKeyCase ? uhash_hashCaselessUnicodeString
  125. : uhash_hashUnicodeString,
  126. ignoreKeyCase ? uhash_compareCaselessUnicodeString
  127. : uhash_compareUnicodeString,
  128. nullptr,
  129. status);
  130. }
  131. inline Hashtable::Hashtable(UBool ignoreKeyCase, int32_t size, UErrorCode& status)
  132. : hash(0)
  133. {
  134. initSize(ignoreKeyCase ? uhash_hashCaselessUnicodeString
  135. : uhash_hashUnicodeString,
  136. ignoreKeyCase ? uhash_compareCaselessUnicodeString
  137. : uhash_compareUnicodeString,
  138. nullptr, size,
  139. status);
  140. }
  141. inline Hashtable::Hashtable(UErrorCode& status)
  142. : hash(0)
  143. {
  144. init(uhash_hashUnicodeString, uhash_compareUnicodeString, nullptr, status);
  145. }
  146. inline Hashtable::Hashtable()
  147. : hash(0)
  148. {
  149. UErrorCode status = U_ZERO_ERROR;
  150. init(uhash_hashUnicodeString, uhash_compareUnicodeString, nullptr, status);
  151. }
  152. inline Hashtable::~Hashtable() {
  153. if (hash != nullptr) {
  154. uhash_close(hash);
  155. }
  156. }
  157. inline UObjectDeleter *Hashtable::setValueDeleter(UObjectDeleter *fn) {
  158. return uhash_setValueDeleter(hash, fn);
  159. }
  160. inline int32_t Hashtable::count() const {
  161. return uhash_count(hash);
  162. }
  163. inline void* Hashtable::put(const UnicodeString& key, void* value, UErrorCode& status) {
  164. return uhash_put(hash, new UnicodeString(key), value, &status);
  165. }
  166. inline int32_t Hashtable::puti(const UnicodeString& key, int32_t value, UErrorCode& status) {
  167. return uhash_puti(hash, new UnicodeString(key), value, &status);
  168. }
  169. inline int32_t Hashtable::putiAllowZero(const UnicodeString& key, int32_t value,
  170. UErrorCode& status) {
  171. return uhash_putiAllowZero(hash, new UnicodeString(key), value, &status);
  172. }
  173. inline void* Hashtable::get(const UnicodeString& key) const {
  174. return uhash_get(hash, &key);
  175. }
  176. inline int32_t Hashtable::geti(const UnicodeString& key) const {
  177. return uhash_geti(hash, &key);
  178. }
  179. inline int32_t Hashtable::getiAndFound(const UnicodeString& key, UBool &found) const {
  180. return uhash_getiAndFound(hash, &key, &found);
  181. }
  182. inline void* Hashtable::remove(const UnicodeString& key) {
  183. return uhash_remove(hash, &key);
  184. }
  185. inline int32_t Hashtable::removei(const UnicodeString& key) {
  186. return uhash_removei(hash, &key);
  187. }
  188. inline UBool Hashtable::containsKey(const UnicodeString& key) const {
  189. return uhash_containsKey(hash, &key);
  190. }
  191. inline const UHashElement* Hashtable::find(const UnicodeString& key) const {
  192. return uhash_find(hash, &key);
  193. }
  194. inline const UHashElement* Hashtable::nextElement(int32_t& pos) const {
  195. return uhash_nextElement(hash, &pos);
  196. }
  197. inline void Hashtable::removeAll() {
  198. uhash_removeAll(hash);
  199. }
  200. inline UKeyComparator* Hashtable::setKeyComparator(UKeyComparator*keyComp){
  201. return uhash_setKeyComparator(hash, keyComp);
  202. }
  203. inline UValueComparator* Hashtable::setValueComparator(UValueComparator* valueComp){
  204. return uhash_setValueComparator(hash, valueComp);
  205. }
  206. inline UBool Hashtable::equals(const Hashtable& that)const{
  207. return uhash_equals(hash, that.hash);
  208. }
  209. U_NAMESPACE_END
  210. #endif