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