2 ******************************************************************************
3 * Copyright (C) 1997-2001, 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
{
32 * Construct a hashtable
33 * @param ignoreKeyCase If true, keys are case insensitive.
34 * @param status Error code
36 Hashtable(UBool ignoreKeyCase
, UErrorCode
& status
);
39 * Construct a hashtable, _disregarding any error_. Use this constructor
41 * @param ignoreKeyCase if TRUE, keys are case insensitive
43 Hashtable(UBool ignoreKeyCase
= FALSE
);
46 * Non-virtual destructor; make this virtual if Hashtable is subclassed
51 UObjectDeleter
*setValueDeleter(UObjectDeleter
*fn
);
53 int32_t count() const;
55 void* put(const UnicodeString
& key
, void* value
, UErrorCode
& status
);
57 int32_t puti(const UnicodeString
& key
, int32_t value
, UErrorCode
& status
);
59 void* get(const UnicodeString
& key
) const;
61 int32_t geti(const UnicodeString
& key
) const;
63 void* remove(const UnicodeString
& key
);
65 int32_t removei(const UnicodeString
& key
);
69 const UHashElement
* find(const UnicodeString
& key
) const;
71 const UHashElement
* nextElement(int32_t& pos
) const;
74 Hashtable(const Hashtable
&other
); // forbid copying of this class
75 Hashtable
&operator=(const Hashtable
&other
); // forbid copying of this class
78 /*********************************************************************
80 ********************************************************************/
82 inline Hashtable::Hashtable(UBool ignoreKeyCase
, UErrorCode
& status
) :
84 if (U_FAILURE(status
)) {
87 hash
= uhash_open(ignoreKeyCase
? uhash_hashCaselessUnicodeString
88 : uhash_hashUnicodeString
,
89 ignoreKeyCase
? uhash_compareCaselessUnicodeString
90 : uhash_compareUnicodeString
,
92 if (U_SUCCESS(status
)) {
93 uhash_setKeyDeleter(hash
, uhash_deleteUnicodeString
);
97 inline Hashtable::Hashtable(UBool ignoreKeyCase
) : hash(0) {
98 UErrorCode status
= U_ZERO_ERROR
;
99 hash
= uhash_open(ignoreKeyCase
? uhash_hashCaselessUnicodeString
100 : uhash_hashUnicodeString
,
101 ignoreKeyCase
? uhash_compareCaselessUnicodeString
102 : uhash_compareUnicodeString
,
104 if (U_SUCCESS(status
)) {
105 uhash_setKeyDeleter(hash
, uhash_deleteUnicodeString
);
109 inline Hashtable::~Hashtable() {
116 inline UObjectDeleter
*Hashtable::setValueDeleter(UObjectDeleter
*fn
) {
117 return uhash_setValueDeleter(hash
, fn
);
120 inline int32_t Hashtable::count() const {
121 return uhash_count(hash
);
124 inline void* Hashtable::put(const UnicodeString
& key
, void* value
, UErrorCode
& status
) {
125 return uhash_put(hash
, new UnicodeString(key
), value
, &status
);
128 inline int32_t Hashtable::puti(const UnicodeString
& key
, int32_t value
, UErrorCode
& status
) {
129 return uhash_puti(hash
, new UnicodeString(key
), value
, &status
);
132 inline void* Hashtable::get(const UnicodeString
& key
) const {
133 return uhash_get(hash
, &key
);
136 inline int32_t Hashtable::geti(const UnicodeString
& key
) const {
137 return uhash_geti(hash
, &key
);
140 inline void* Hashtable::remove(const UnicodeString
& key
) {
141 return uhash_remove(hash
, &key
);
144 inline int32_t Hashtable::removei(const UnicodeString
& key
) {
145 return uhash_removei(hash
, &key
);
148 inline const UHashElement
* Hashtable::find(const UnicodeString
& key
) const {
149 return uhash_find(hash
, &key
);
152 inline const UHashElement
* Hashtable::nextElement(int32_t& pos
) const {
153 return uhash_nextElement(hash
, &pos
);
156 inline void Hashtable::removeAll(void) {
157 uhash_removeAll(hash
);