1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
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 ******************************************************************************
16 #include "unicode/unistr.h"
17 #include "unicode/uobject.h"
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
28 * Hashtable is an INTERNAL CLASS.
30 class U_COMMON_API Hashtable
: public UMemory
{
34 inline void init(UHashFunction
*keyHash
, UKeyComparator
*keyComp
, UValueComparator
*valueComp
, UErrorCode
& status
);
36 inline void initSize(UHashFunction
*keyHash
, UKeyComparator
*keyComp
, UValueComparator
*valueComp
, int32_t size
, UErrorCode
& status
);
40 * Construct a hashtable
41 * @param ignoreKeyCase If true, keys are case insensitive.
42 * @param status Error code
44 inline Hashtable(UBool ignoreKeyCase
, UErrorCode
& status
);
47 * Construct a hashtable
48 * @param ignoreKeyCase If true, keys are case insensitive.
49 * @param size initial size allocation
50 * @param status Error code
52 inline Hashtable(UBool ignoreKeyCase
, int32_t size
, UErrorCode
& status
);
55 * Construct a hashtable
56 * @param keyComp Comparator for comparing the keys
57 * @param valueComp Comparator for comparing the values
58 * @param status Error code
60 inline Hashtable(UKeyComparator
*keyComp
, UValueComparator
*valueComp
, UErrorCode
& status
);
63 * Construct a hashtable
64 * @param status Error code
66 inline Hashtable(UErrorCode
& status
);
69 * Construct a hashtable, _disregarding any error_. Use this constructor
75 * Non-virtual destructor; make this virtual if Hashtable is subclassed
80 inline UObjectDeleter
*setValueDeleter(UObjectDeleter
*fn
);
82 inline int32_t count() const;
84 inline void* put(const UnicodeString
& key
, void* value
, UErrorCode
& status
);
86 inline int32_t puti(const UnicodeString
& key
, int32_t value
, UErrorCode
& status
);
88 inline void* get(const UnicodeString
& key
) const;
90 inline int32_t geti(const UnicodeString
& key
) const;
92 inline void* remove(const UnicodeString
& key
);
94 inline int32_t removei(const UnicodeString
& key
);
96 inline void removeAll(void);
98 inline const UHashElement
* find(const UnicodeString
& key
) const;
101 * @param pos - must be UHASH_FIRST on first call, and untouched afterwards.
102 * @see uhash_nextElement
104 inline const UHashElement
* nextElement(int32_t& pos
) const;
106 inline UKeyComparator
* setKeyComparator(UKeyComparator
*keyComp
);
108 inline UValueComparator
* setValueComparator(UValueComparator
* valueComp
);
110 inline UBool
equals(const Hashtable
& that
) const;
112 Hashtable(const Hashtable
&other
); // forbid copying of this class
113 Hashtable
&operator=(const Hashtable
&other
); // forbid copying of this class
116 /*********************************************************************
118 ********************************************************************/
120 inline void Hashtable::init(UHashFunction
*keyHash
, UKeyComparator
*keyComp
,
121 UValueComparator
*valueComp
, UErrorCode
& status
) {
122 if (U_FAILURE(status
)) {
125 uhash_init(&hashObj
, keyHash
, keyComp
, valueComp
, &status
);
126 if (U_SUCCESS(status
)) {
128 uhash_setKeyDeleter(hash
, uprv_deleteUObject
);
132 inline void Hashtable::initSize(UHashFunction
*keyHash
, UKeyComparator
*keyComp
,
133 UValueComparator
*valueComp
, int32_t size
, UErrorCode
& status
) {
134 if (U_FAILURE(status
)) {
137 uhash_initSize(&hashObj
, keyHash
, keyComp
, valueComp
, size
, &status
);
138 if (U_SUCCESS(status
)) {
140 uhash_setKeyDeleter(hash
, uprv_deleteUObject
);
144 inline Hashtable::Hashtable(UKeyComparator
*keyComp
, UValueComparator
*valueComp
,
145 UErrorCode
& status
) : hash(0) {
146 init( uhash_hashUnicodeString
, keyComp
, valueComp
, status
);
149 inline Hashtable::Hashtable(UBool ignoreKeyCase
, UErrorCode
& status
)
152 init(ignoreKeyCase
? uhash_hashCaselessUnicodeString
153 : uhash_hashUnicodeString
,
154 ignoreKeyCase
? uhash_compareCaselessUnicodeString
155 : uhash_compareUnicodeString
,
160 inline Hashtable::Hashtable(UBool ignoreKeyCase
, int32_t size
, UErrorCode
& status
)
163 initSize(ignoreKeyCase
? uhash_hashCaselessUnicodeString
164 : uhash_hashUnicodeString
,
165 ignoreKeyCase
? uhash_compareCaselessUnicodeString
166 : uhash_compareUnicodeString
,
171 inline Hashtable::Hashtable(UErrorCode
& status
)
174 init(uhash_hashUnicodeString
, uhash_compareUnicodeString
, NULL
, status
);
177 inline Hashtable::Hashtable()
180 UErrorCode status
= U_ZERO_ERROR
;
181 init(uhash_hashUnicodeString
, uhash_compareUnicodeString
, NULL
, status
);
184 inline Hashtable::~Hashtable() {
190 inline UObjectDeleter
*Hashtable::setValueDeleter(UObjectDeleter
*fn
) {
191 return uhash_setValueDeleter(hash
, fn
);
194 inline int32_t Hashtable::count() const {
195 return uhash_count(hash
);
198 inline void* Hashtable::put(const UnicodeString
& key
, void* value
, UErrorCode
& status
) {
199 return uhash_put(hash
, new UnicodeString(key
), value
, &status
);
202 inline int32_t Hashtable::puti(const UnicodeString
& key
, int32_t value
, UErrorCode
& status
) {
203 return uhash_puti(hash
, new UnicodeString(key
), value
, &status
);
206 inline void* Hashtable::get(const UnicodeString
& key
) const {
207 return uhash_get(hash
, &key
);
210 inline int32_t Hashtable::geti(const UnicodeString
& key
) const {
211 return uhash_geti(hash
, &key
);
214 inline void* Hashtable::remove(const UnicodeString
& key
) {
215 return uhash_remove(hash
, &key
);
218 inline int32_t Hashtable::removei(const UnicodeString
& key
) {
219 return uhash_removei(hash
, &key
);
222 inline const UHashElement
* Hashtable::find(const UnicodeString
& key
) const {
223 return uhash_find(hash
, &key
);
226 inline const UHashElement
* Hashtable::nextElement(int32_t& pos
) const {
227 return uhash_nextElement(hash
, &pos
);
230 inline void Hashtable::removeAll(void) {
231 uhash_removeAll(hash
);
234 inline UKeyComparator
* Hashtable::setKeyComparator(UKeyComparator
*keyComp
){
235 return uhash_setKeyComparator(hash
, keyComp
);
238 inline UValueComparator
* Hashtable::setValueComparator(UValueComparator
* valueComp
){
239 return uhash_setValueComparator(hash
, valueComp
);
242 inline UBool
Hashtable::equals(const Hashtable
& that
)const{
243 return uhash_equals(hash
, that
.hash
);