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