2 ******************************************************************************
3 * Copyright (C) 1997-2010, 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"
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
25 * Hashtable is an INTERNAL CLASS.
27 class U_COMMON_API Hashtable
: public UMemory
{
31 inline void init(UHashFunction
*keyHash
, UKeyComparator
*keyComp
, UValueComparator
*valueComp
, UErrorCode
& status
);
35 * Construct a hashtable
36 * @param ignoreKeyCase If true, keys are case insensitive.
37 * @param status Error code
39 Hashtable(UBool ignoreKeyCase
, UErrorCode
& status
);
42 * Construct a hashtable
43 * @param keyComp Comparator for comparing the keys
44 * @param valueComp Comparator for comparing the values
45 * @param status Error code
47 Hashtable(UKeyComparator
*keyComp
, UValueComparator
*valueComp
, UErrorCode
& status
);
50 * Construct a hashtable
51 * @param status Error code
53 Hashtable(UErrorCode
& status
);
56 * Construct a hashtable, _disregarding any error_. Use this constructor
62 * Non-virtual destructor; make this virtual if Hashtable is subclassed
67 UObjectDeleter
*setValueDeleter(UObjectDeleter
*fn
);
69 int32_t count() const;
71 void* put(const UnicodeString
& key
, void* value
, UErrorCode
& status
);
73 int32_t puti(const UnicodeString
& key
, int32_t value
, UErrorCode
& status
);
75 void* get(const UnicodeString
& key
) const;
77 int32_t geti(const UnicodeString
& key
) const;
79 void* remove(const UnicodeString
& key
);
81 int32_t removei(const UnicodeString
& key
);
85 const UHashElement
* find(const UnicodeString
& key
) const;
87 const UHashElement
* nextElement(int32_t& pos
) const;
89 UKeyComparator
* setKeyComparator(UKeyComparator
*keyComp
);
91 UValueComparator
* setValueComparator(UValueComparator
* valueComp
);
93 UBool
equals(const Hashtable
& that
) const;
95 Hashtable(const Hashtable
&other
); // forbid copying of this class
96 Hashtable
&operator=(const Hashtable
&other
); // forbid copying of this class
99 /*********************************************************************
101 ********************************************************************/
103 inline void Hashtable::init(UHashFunction
*keyHash
, UKeyComparator
*keyComp
,
104 UValueComparator
*valueComp
, UErrorCode
& status
) {
105 if (U_FAILURE(status
)) {
108 uhash_init(&hashObj
, keyHash
, keyComp
, valueComp
, &status
);
109 if (U_SUCCESS(status
)) {
111 uhash_setKeyDeleter(hash
, uhash_deleteUnicodeString
);
115 inline Hashtable::Hashtable(UKeyComparator
*keyComp
, UValueComparator
*valueComp
,
116 UErrorCode
& status
) : hash(0) {
117 init( uhash_hashUnicodeString
, keyComp
, valueComp
, status
);
119 inline Hashtable::Hashtable(UBool ignoreKeyCase
, UErrorCode
& status
)
122 init(ignoreKeyCase
? uhash_hashCaselessUnicodeString
123 : uhash_hashUnicodeString
,
124 ignoreKeyCase
? uhash_compareCaselessUnicodeString
125 : uhash_compareUnicodeString
,
130 inline Hashtable::Hashtable(UErrorCode
& status
)
133 init(uhash_hashUnicodeString
, uhash_compareUnicodeString
, NULL
, status
);
136 inline Hashtable::Hashtable()
139 UErrorCode status
= U_ZERO_ERROR
;
140 init(uhash_hashUnicodeString
, uhash_compareUnicodeString
, NULL
, status
);
143 inline Hashtable::~Hashtable() {
149 inline UObjectDeleter
*Hashtable::setValueDeleter(UObjectDeleter
*fn
) {
150 return uhash_setValueDeleter(hash
, fn
);
153 inline int32_t Hashtable::count() const {
154 return uhash_count(hash
);
157 inline void* Hashtable::put(const UnicodeString
& key
, void* value
, UErrorCode
& status
) {
158 return uhash_put(hash
, new UnicodeString(key
), value
, &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
);
165 inline void* Hashtable::get(const UnicodeString
& key
) const {
166 return uhash_get(hash
, &key
);
169 inline int32_t Hashtable::geti(const UnicodeString
& key
) const {
170 return uhash_geti(hash
, &key
);
173 inline void* Hashtable::remove(const UnicodeString
& key
) {
174 return uhash_remove(hash
, &key
);
177 inline int32_t Hashtable::removei(const UnicodeString
& key
) {
178 return uhash_removei(hash
, &key
);
181 inline const UHashElement
* Hashtable::find(const UnicodeString
& key
) const {
182 return uhash_find(hash
, &key
);
185 inline const UHashElement
* Hashtable::nextElement(int32_t& pos
) const {
186 return uhash_nextElement(hash
, &pos
);
189 inline void Hashtable::removeAll(void) {
190 uhash_removeAll(hash
);
193 inline UKeyComparator
* Hashtable::setKeyComparator(UKeyComparator
*keyComp
){
194 return uhash_setKeyComparator(hash
, keyComp
);
197 inline UValueComparator
* Hashtable::setValueComparator(UValueComparator
* valueComp
){
198 return uhash_setValueComparator(hash
, valueComp
);
201 inline UBool
Hashtable::equals(const Hashtable
& that
)const{
202 return uhash_equals(hash
, that
.hash
);