2 ******************************************************************************
3 * Copyright (C) 1997-2004, 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
{
30 inline void init(UHashFunction
*keyHash
, UKeyComparator
*keyComp
, UErrorCode
& status
);
34 * Construct a hashtable
35 * @param ignoreKeyCase If true, keys are case insensitive.
36 * @param status Error code
38 Hashtable(UBool ignoreKeyCase
, UErrorCode
& status
);
41 * Construct a hashtable
42 * @param status Error code
44 Hashtable(UErrorCode
& status
);
47 * Construct a hashtable, _disregarding any error_. Use this constructor
53 * Non-virtual destructor; make this virtual if Hashtable is subclassed
58 UObjectDeleter
*setValueDeleter(UObjectDeleter
*fn
);
60 int32_t count() const;
62 void* put(const UnicodeString
& key
, void* value
, UErrorCode
& status
);
64 int32_t puti(const UnicodeString
& key
, int32_t value
, UErrorCode
& status
);
66 void* get(const UnicodeString
& key
) const;
68 int32_t geti(const UnicodeString
& key
) const;
70 void* remove(const UnicodeString
& key
);
72 int32_t removei(const UnicodeString
& key
);
76 const UHashElement
* find(const UnicodeString
& key
) const;
78 const UHashElement
* nextElement(int32_t& pos
) const;
81 Hashtable(const Hashtable
&other
); // forbid copying of this class
82 Hashtable
&operator=(const Hashtable
&other
); // forbid copying of this class
85 /*********************************************************************
87 ********************************************************************/
89 inline void Hashtable::init(UHashFunction
*keyHash
, UKeyComparator
*keyComp
, UErrorCode
& status
) {
90 if (U_FAILURE(status
)) {
93 hash
= uhash_open(keyHash
, keyComp
, &status
);
94 if (U_SUCCESS(status
)) {
95 uhash_setKeyDeleter(hash
, uhash_deleteUnicodeString
);
99 inline Hashtable::Hashtable(UBool ignoreKeyCase
, UErrorCode
& status
)
102 init(ignoreKeyCase
? uhash_hashCaselessUnicodeString
103 : uhash_hashUnicodeString
,
104 ignoreKeyCase
? uhash_compareCaselessUnicodeString
105 : uhash_compareUnicodeString
,
109 inline Hashtable::Hashtable(UErrorCode
& status
)
112 init(uhash_hashUnicodeString
, uhash_compareUnicodeString
, status
);
115 inline Hashtable::Hashtable()
118 UErrorCode status
= U_ZERO_ERROR
;
119 init(uhash_hashUnicodeString
, uhash_compareUnicodeString
, status
);
122 inline Hashtable::~Hashtable() {
129 inline UObjectDeleter
*Hashtable::setValueDeleter(UObjectDeleter
*fn
) {
130 return uhash_setValueDeleter(hash
, fn
);
133 inline int32_t Hashtable::count() const {
134 return uhash_count(hash
);
137 inline void* Hashtable::put(const UnicodeString
& key
, void* value
, UErrorCode
& status
) {
138 return uhash_put(hash
, new UnicodeString(key
), value
, &status
);
141 inline int32_t Hashtable::puti(const UnicodeString
& key
, int32_t value
, UErrorCode
& status
) {
142 return uhash_puti(hash
, new UnicodeString(key
), value
, &status
);
145 inline void* Hashtable::get(const UnicodeString
& key
) const {
146 return uhash_get(hash
, &key
);
149 inline int32_t Hashtable::geti(const UnicodeString
& key
) const {
150 return uhash_geti(hash
, &key
);
153 inline void* Hashtable::remove(const UnicodeString
& key
) {
154 return uhash_remove(hash
, &key
);
157 inline int32_t Hashtable::removei(const UnicodeString
& key
) {
158 return uhash_removei(hash
, &key
);
161 inline const UHashElement
* Hashtable::find(const UnicodeString
& key
) const {
162 return uhash_find(hash
, &key
);
165 inline const UHashElement
* Hashtable::nextElement(int32_t& pos
) const {
166 return uhash_nextElement(hash
, &pos
);
169 inline void Hashtable::removeAll(void) {
170 uhash_removeAll(hash
);