]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ****************************************************************************** | |
b331163b | 3 | * Copyright (C) 1997-2014, International Business Machines |
b75a7d8f A |
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" | |
4388f060 | 16 | #include "cmemory.h" |
b75a7d8f A |
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; | |
73c04bcf | 30 | UHashtable hashObj; |
b75a7d8f | 31 | |
73c04bcf | 32 | inline void init(UHashFunction *keyHash, UKeyComparator *keyComp, UValueComparator *valueComp, UErrorCode& status); |
374ca955 | 33 | |
b75a7d8f A |
34 | public: |
35 | /** | |
36 | * Construct a hashtable | |
37 | * @param ignoreKeyCase If true, keys are case insensitive. | |
38 | * @param status Error code | |
39 | */ | |
40 | Hashtable(UBool ignoreKeyCase, UErrorCode& status); | |
41 | ||
73c04bcf A |
42 | /** |
43 | * Construct a hashtable | |
729e4ab9 A |
44 | * @param keyComp Comparator for comparing the keys |
45 | * @param valueComp Comparator for comparing the values | |
73c04bcf A |
46 | * @param status Error code |
47 | */ | |
48 | Hashtable(UKeyComparator *keyComp, UValueComparator *valueComp, UErrorCode& status); | |
49 | ||
374ca955 A |
50 | /** |
51 | * Construct a hashtable | |
52 | * @param status Error code | |
53 | */ | |
54 | Hashtable(UErrorCode& status); | |
55 | ||
b75a7d8f A |
56 | /** |
57 | * Construct a hashtable, _disregarding any error_. Use this constructor | |
58 | * with caution. | |
b75a7d8f | 59 | */ |
374ca955 | 60 | Hashtable(); |
b75a7d8f A |
61 | |
62 | /** | |
63 | * Non-virtual destructor; make this virtual if Hashtable is subclassed | |
64 | * in the future. | |
65 | */ | |
66 | ~Hashtable(); | |
67 | ||
68 | UObjectDeleter *setValueDeleter(UObjectDeleter *fn); | |
69 | ||
70 | int32_t count() const; | |
71 | ||
72 | void* put(const UnicodeString& key, void* value, UErrorCode& status); | |
73 | ||
74 | int32_t puti(const UnicodeString& key, int32_t value, UErrorCode& status); | |
75 | ||
76 | void* get(const UnicodeString& key) const; | |
77 | ||
78 | int32_t geti(const UnicodeString& key) const; | |
79 | ||
80 | void* remove(const UnicodeString& key); | |
81 | ||
82 | int32_t removei(const UnicodeString& key); | |
83 | ||
84 | void removeAll(void); | |
85 | ||
86 | const UHashElement* find(const UnicodeString& key) const; | |
87 | ||
b331163b A |
88 | /** |
89 | * @param pos - must be UHASH_FIRST on first call, and untouched afterwards. | |
90 | * @see uhash_nextElement | |
91 | */ | |
b75a7d8f | 92 | const UHashElement* nextElement(int32_t& pos) const; |
73c04bcf | 93 | |
729e4ab9 | 94 | UKeyComparator* setKeyComparator(UKeyComparator*keyComp); |
73c04bcf | 95 | |
729e4ab9 | 96 | UValueComparator* setValueComparator(UValueComparator* valueComp); |
b75a7d8f | 97 | |
73c04bcf | 98 | UBool equals(const Hashtable& that) const; |
b75a7d8f A |
99 | private: |
100 | Hashtable(const Hashtable &other); // forbid copying of this class | |
101 | Hashtable &operator=(const Hashtable &other); // forbid copying of this class | |
102 | }; | |
103 | ||
104 | /********************************************************************* | |
105 | * Implementation | |
106 | ********************************************************************/ | |
107 | ||
73c04bcf A |
108 | inline void Hashtable::init(UHashFunction *keyHash, UKeyComparator *keyComp, |
109 | UValueComparator *valueComp, UErrorCode& status) { | |
b75a7d8f A |
110 | if (U_FAILURE(status)) { |
111 | return; | |
112 | } | |
73c04bcf | 113 | uhash_init(&hashObj, keyHash, keyComp, valueComp, &status); |
b75a7d8f | 114 | if (U_SUCCESS(status)) { |
73c04bcf | 115 | hash = &hashObj; |
4388f060 | 116 | uhash_setKeyDeleter(hash, uprv_deleteUObject); |
b75a7d8f A |
117 | } |
118 | } | |
119 | ||
73c04bcf A |
120 | inline Hashtable::Hashtable(UKeyComparator *keyComp, UValueComparator *valueComp, |
121 | UErrorCode& status) : hash(0) { | |
122 | init( uhash_hashUnicodeString, keyComp, valueComp, status); | |
123 | } | |
374ca955 A |
124 | inline Hashtable::Hashtable(UBool ignoreKeyCase, UErrorCode& status) |
125 | : hash(0) | |
126 | { | |
127 | init(ignoreKeyCase ? uhash_hashCaselessUnicodeString | |
128 | : uhash_hashUnicodeString, | |
129 | ignoreKeyCase ? uhash_compareCaselessUnicodeString | |
130 | : uhash_compareUnicodeString, | |
73c04bcf | 131 | NULL, |
374ca955 A |
132 | status); |
133 | } | |
134 | ||
135 | inline Hashtable::Hashtable(UErrorCode& status) | |
136 | : hash(0) | |
137 | { | |
73c04bcf | 138 | init(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, status); |
374ca955 A |
139 | } |
140 | ||
141 | inline Hashtable::Hashtable() | |
142 | : hash(0) | |
143 | { | |
b75a7d8f | 144 | UErrorCode status = U_ZERO_ERROR; |
73c04bcf | 145 | init(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, status); |
b75a7d8f A |
146 | } |
147 | ||
148 | inline Hashtable::~Hashtable() { | |
73c04bcf | 149 | if (hash != NULL) { |
b75a7d8f | 150 | uhash_close(hash); |
b75a7d8f A |
151 | } |
152 | } | |
153 | ||
154 | inline UObjectDeleter *Hashtable::setValueDeleter(UObjectDeleter *fn) { | |
155 | return uhash_setValueDeleter(hash, fn); | |
156 | } | |
157 | ||
158 | inline int32_t Hashtable::count() const { | |
159 | return uhash_count(hash); | |
160 | } | |
161 | ||
162 | inline void* Hashtable::put(const UnicodeString& key, void* value, UErrorCode& status) { | |
163 | return uhash_put(hash, new UnicodeString(key), value, &status); | |
164 | } | |
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 | ||
170 | inline void* Hashtable::get(const UnicodeString& key) const { | |
171 | return uhash_get(hash, &key); | |
172 | } | |
173 | ||
174 | inline int32_t Hashtable::geti(const UnicodeString& key) const { | |
175 | return uhash_geti(hash, &key); | |
176 | } | |
177 | ||
178 | inline void* Hashtable::remove(const UnicodeString& key) { | |
179 | return uhash_remove(hash, &key); | |
180 | } | |
181 | ||
182 | inline int32_t Hashtable::removei(const UnicodeString& key) { | |
183 | return uhash_removei(hash, &key); | |
184 | } | |
185 | ||
186 | inline const UHashElement* Hashtable::find(const UnicodeString& key) const { | |
187 | return uhash_find(hash, &key); | |
188 | } | |
189 | ||
190 | inline const UHashElement* Hashtable::nextElement(int32_t& pos) const { | |
191 | return uhash_nextElement(hash, &pos); | |
192 | } | |
193 | ||
194 | inline void Hashtable::removeAll(void) { | |
73c04bcf | 195 | uhash_removeAll(hash); |
b75a7d8f A |
196 | } |
197 | ||
729e4ab9 | 198 | inline UKeyComparator* Hashtable::setKeyComparator(UKeyComparator*keyComp){ |
73c04bcf A |
199 | return uhash_setKeyComparator(hash, keyComp); |
200 | } | |
201 | ||
729e4ab9 | 202 | inline UValueComparator* Hashtable::setValueComparator(UValueComparator* valueComp){ |
73c04bcf A |
203 | return uhash_setValueComparator(hash, valueComp); |
204 | } | |
205 | ||
206 | inline UBool Hashtable::equals(const Hashtable& that)const{ | |
207 | return uhash_equals(hash, that.hash); | |
208 | } | |
b75a7d8f A |
209 | U_NAMESPACE_END |
210 | ||
211 | #endif | |
73c04bcf | 212 |