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