• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ******************************************************************************
3 *   Copyright (C) 1997-2006, 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 "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;
29     UHashtable hashObj;
30 
31     inline void init(UHashFunction *keyHash, UKeyComparator *keyComp, UValueComparator *valueComp, UErrorCode& status);
32 
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 
41     /**
42      * Construct a hashtable
43      * @param keyComp Compartor for comparing the keys
44      * @param valueComp Compartor for comparing the values
45      * @param status Error code
46     */
47     Hashtable(UKeyComparator *keyComp, UValueComparator *valueComp, UErrorCode& status);
48 
49     /**
50      * Construct a hashtable
51      * @param status Error code
52     */
53     Hashtable(UErrorCode& status);
54 
55     /**
56      * Construct a hashtable, _disregarding any error_.  Use this constructor
57      * with caution.
58      */
59     Hashtable();
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;
88 
89     UKeyComparator* setKeyCompartor(UKeyComparator*keyComp);
90 
91     UValueComparator* setValueCompartor(UValueComparator* valueComp);
92 
93     UBool equals(const Hashtable& that) const;
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 
init(UHashFunction * keyHash,UKeyComparator * keyComp,UValueComparator * valueComp,UErrorCode & status)103 inline void Hashtable::init(UHashFunction *keyHash, UKeyComparator *keyComp,
104                             UValueComparator *valueComp, UErrorCode& status) {
105     if (U_FAILURE(status)) {
106         return;
107     }
108     uhash_init(&hashObj, keyHash, keyComp, valueComp, &status);
109     if (U_SUCCESS(status)) {
110         hash = &hashObj;
111         uhash_setKeyDeleter(hash, uhash_deleteUnicodeString);
112     }
113 }
114 
Hashtable(UKeyComparator * keyComp,UValueComparator * valueComp,UErrorCode & status)115 inline Hashtable::Hashtable(UKeyComparator *keyComp, UValueComparator *valueComp,
116                  UErrorCode& status) : hash(0) {
117     init( uhash_hashUnicodeString, keyComp, valueComp, status);
118 }
Hashtable(UBool ignoreKeyCase,UErrorCode & status)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,
126             NULL,
127             status);
128 }
129 
Hashtable(UErrorCode & status)130 inline Hashtable::Hashtable(UErrorCode& status)
131  : hash(0)
132 {
133     init(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, status);
134 }
135 
Hashtable()136 inline Hashtable::Hashtable()
137  : hash(0)
138 {
139     UErrorCode status = U_ZERO_ERROR;
140     init(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, status);
141 }
142 
~Hashtable()143 inline Hashtable::~Hashtable() {
144     if (hash != NULL) {
145         uhash_close(hash);
146     }
147 }
148 
setValueDeleter(UObjectDeleter * fn)149 inline UObjectDeleter *Hashtable::setValueDeleter(UObjectDeleter *fn) {
150     return uhash_setValueDeleter(hash, fn);
151 }
152 
count()153 inline int32_t Hashtable::count() const {
154     return uhash_count(hash);
155 }
156 
put(const UnicodeString & key,void * value,UErrorCode & status)157 inline void* Hashtable::put(const UnicodeString& key, void* value, UErrorCode& status) {
158     return uhash_put(hash, new UnicodeString(key), value, &status);
159 }
160 
puti(const UnicodeString & key,int32_t value,UErrorCode & status)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 
get(const UnicodeString & key)165 inline void* Hashtable::get(const UnicodeString& key) const {
166     return uhash_get(hash, &key);
167 }
168 
geti(const UnicodeString & key)169 inline int32_t Hashtable::geti(const UnicodeString& key) const {
170     return uhash_geti(hash, &key);
171 }
172 
remove(const UnicodeString & key)173 inline void* Hashtable::remove(const UnicodeString& key) {
174     return uhash_remove(hash, &key);
175 }
176 
removei(const UnicodeString & key)177 inline int32_t Hashtable::removei(const UnicodeString& key) {
178     return uhash_removei(hash, &key);
179 }
180 
find(const UnicodeString & key)181 inline const UHashElement* Hashtable::find(const UnicodeString& key) const {
182     return uhash_find(hash, &key);
183 }
184 
nextElement(int32_t & pos)185 inline const UHashElement* Hashtable::nextElement(int32_t& pos) const {
186     return uhash_nextElement(hash, &pos);
187 }
188 
removeAll(void)189 inline void Hashtable::removeAll(void) {
190     uhash_removeAll(hash);
191 }
192 
setKeyCompartor(UKeyComparator * keyComp)193 inline UKeyComparator* Hashtable::setKeyCompartor(UKeyComparator*keyComp){
194     return uhash_setKeyComparator(hash, keyComp);
195 }
196 
setValueCompartor(UValueComparator * valueComp)197 inline UValueComparator* Hashtable::setValueCompartor(UValueComparator* valueComp){
198     return uhash_setValueComparator(hash, valueComp);
199 }
200 
equals(const Hashtable & that)201 inline UBool Hashtable::equals(const Hashtable& that)const{
202    return uhash_equals(hash, that.hash);
203 }
204 U_NAMESPACE_END
205 
206 #endif
207 
208