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 ******************************************************************************
14 #include "unicode/unistr.h"
15 #include "unicode/uobject.h"
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
26 * Hashtable is an INTERNAL CLASS.
28 class U_COMMON_API Hashtable
: public UMemory
{
32 inline void init(UHashFunction
*keyHash
, UKeyComparator
*keyComp
, UValueComparator
*valueComp
, UErrorCode
& status
);
34 inline void initSize(UHashFunction
*keyHash
, UKeyComparator
*keyComp
, UValueComparator
*valueComp
, int32_t size
, UErrorCode
& status
);
38 * Construct a hashtable
39 * @param ignoreKeyCase If true, keys are case insensitive.
40 * @param status Error code
42 Hashtable(UBool ignoreKeyCase
, UErrorCode
& status
);
45 * Construct a hashtable
46 * @param ignoreKeyCase If true, keys are case insensitive.
47 * @param size initial size allocation
48 * @param status Error code
50 Hashtable(UBool ignoreKeyCase
, int32_t size
, UErrorCode
& status
);
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
58 Hashtable(UKeyComparator
*keyComp
, UValueComparator
*valueComp
, UErrorCode
& status
);
61 * Construct a hashtable
62 * @param status Error code
64 Hashtable(UErrorCode
& status
);
67 * Construct a hashtable, _disregarding any error_. Use this constructor
73 * Non-virtual destructor; make this virtual if Hashtable is subclassed
78 UObjectDeleter
*setValueDeleter(UObjectDeleter
*fn
);
80 int32_t count() const;
82 void* put(const UnicodeString
& key
, void* value
, UErrorCode
& status
);
84 int32_t puti(const UnicodeString
& key
, int32_t value
, UErrorCode
& status
);
86 void* get(const UnicodeString
& key
) const;
88 int32_t geti(const UnicodeString
& key
) const;
90 void* remove(const UnicodeString
& key
);
92 int32_t removei(const UnicodeString
& key
);
96 const UHashElement
* find(const UnicodeString
& key
) const;
99 * @param pos - must be UHASH_FIRST on first call, and untouched afterwards.
100 * @see uhash_nextElement
102 const UHashElement
* nextElement(int32_t& pos
) const;
104 UKeyComparator
* setKeyComparator(UKeyComparator
*keyComp
);
106 UValueComparator
* setValueComparator(UValueComparator
* valueComp
);
108 UBool
equals(const Hashtable
& that
) const;
110 Hashtable(const Hashtable
&other
); // forbid copying of this class
111 Hashtable
&operator=(const Hashtable
&other
); // forbid copying of this class
114 /*********************************************************************
116 ********************************************************************/
118 inline void Hashtable::init(UHashFunction
*keyHash
, UKeyComparator
*keyComp
,
119 UValueComparator
*valueComp
, UErrorCode
& status
) {
120 if (U_FAILURE(status
)) {
123 uhash_init(&hashObj
, keyHash
, keyComp
, valueComp
, &status
);
124 if (U_SUCCESS(status
)) {
126 uhash_setKeyDeleter(hash
, uprv_deleteUObject
);
130 inline void Hashtable::initSize(UHashFunction
*keyHash
, UKeyComparator
*keyComp
,
131 UValueComparator
*valueComp
, int32_t size
, UErrorCode
& status
) {
132 if (U_FAILURE(status
)) {
135 uhash_initSize(&hashObj
, keyHash
, keyComp
, valueComp
, size
, &status
);
136 if (U_SUCCESS(status
)) {
138 uhash_setKeyDeleter(hash
, uprv_deleteUObject
);
142 inline Hashtable::Hashtable(UKeyComparator
*keyComp
, UValueComparator
*valueComp
,
143 UErrorCode
& status
) : hash(0) {
144 init( uhash_hashUnicodeString
, keyComp
, valueComp
, status
);
147 inline Hashtable::Hashtable(UBool ignoreKeyCase
, UErrorCode
& status
)
150 init(ignoreKeyCase
? uhash_hashCaselessUnicodeString
151 : uhash_hashUnicodeString
,
152 ignoreKeyCase
? uhash_compareCaselessUnicodeString
153 : uhash_compareUnicodeString
,
158 inline Hashtable::Hashtable(UBool ignoreKeyCase
, int32_t size
, UErrorCode
& status
)
161 initSize(ignoreKeyCase
? uhash_hashCaselessUnicodeString
162 : uhash_hashUnicodeString
,
163 ignoreKeyCase
? uhash_compareCaselessUnicodeString
164 : uhash_compareUnicodeString
,
169 inline Hashtable::Hashtable(UErrorCode
& status
)
172 init(uhash_hashUnicodeString
, uhash_compareUnicodeString
, NULL
, status
);
175 inline Hashtable::Hashtable()
178 UErrorCode status
= U_ZERO_ERROR
;
179 init(uhash_hashUnicodeString
, uhash_compareUnicodeString
, NULL
, status
);
182 inline Hashtable::~Hashtable() {
188 inline UObjectDeleter
*Hashtable::setValueDeleter(UObjectDeleter
*fn
) {
189 return uhash_setValueDeleter(hash
, fn
);
192 inline int32_t Hashtable::count() const {
193 return uhash_count(hash
);
196 inline void* Hashtable::put(const UnicodeString
& key
, void* value
, UErrorCode
& status
) {
197 return uhash_put(hash
, new UnicodeString(key
), value
, &status
);
200 inline int32_t Hashtable::puti(const UnicodeString
& key
, int32_t value
, UErrorCode
& status
) {
201 return uhash_puti(hash
, new UnicodeString(key
), value
, &status
);
204 inline void* Hashtable::get(const UnicodeString
& key
) const {
205 return uhash_get(hash
, &key
);
208 inline int32_t Hashtable::geti(const UnicodeString
& key
) const {
209 return uhash_geti(hash
, &key
);
212 inline void* Hashtable::remove(const UnicodeString
& key
) {
213 return uhash_remove(hash
, &key
);
216 inline int32_t Hashtable::removei(const UnicodeString
& key
) {
217 return uhash_removei(hash
, &key
);
220 inline const UHashElement
* Hashtable::find(const UnicodeString
& key
) const {
221 return uhash_find(hash
, &key
);
224 inline const UHashElement
* Hashtable::nextElement(int32_t& pos
) const {
225 return uhash_nextElement(hash
, &pos
);
228 inline void Hashtable::removeAll(void) {
229 uhash_removeAll(hash
);
232 inline UKeyComparator
* Hashtable::setKeyComparator(UKeyComparator
*keyComp
){
233 return uhash_setKeyComparator(hash
, keyComp
);
236 inline UValueComparator
* Hashtable::setValueComparator(UValueComparator
* valueComp
){
237 return uhash_setValueComparator(hash
, valueComp
);
240 inline UBool
Hashtable::equals(const Hashtable
& that
)const{
241 return uhash_equals(hash
, that
.hash
);