1 // Copyright (C) 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);
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 keyComp Comparator for comparing the keys
47 * @param valueComp Comparator for comparing the values
48 * @param status Error code
50 Hashtable(UKeyComparator *keyComp, UValueComparator *valueComp, UErrorCode& status);
53 * Construct a hashtable
54 * @param status Error code
56 Hashtable(UErrorCode& status);
59 * Construct a hashtable, _disregarding any error_. Use this constructor
65 * Non-virtual destructor; make this virtual if Hashtable is subclassed
70 UObjectDeleter *setValueDeleter(UObjectDeleter *fn);
72 int32_t count() const;
74 void* put(const UnicodeString& key, void* value, UErrorCode& status);
76 int32_t puti(const UnicodeString& key, int32_t value, UErrorCode& status);
78 void* get(const UnicodeString& key) const;
80 int32_t geti(const UnicodeString& key) const;
82 void* remove(const UnicodeString& key);
84 int32_t removei(const UnicodeString& key);
88 const UHashElement* find(const UnicodeString& key) const;
91 * @param pos - must be UHASH_FIRST on first call, and untouched afterwards.
92 * @see uhash_nextElement
94 const UHashElement* nextElement(int32_t& pos) const;
96 UKeyComparator* setKeyComparator(UKeyComparator*keyComp);
98 UValueComparator* setValueComparator(UValueComparator* valueComp);
100 UBool equals(const Hashtable& that) const;
102 Hashtable(const Hashtable &other); // forbid copying of this class
103 Hashtable &operator=(const Hashtable &other); // forbid copying of this class
106 /*********************************************************************
108 ********************************************************************/
110 inline void Hashtable::init(UHashFunction *keyHash, UKeyComparator *keyComp,
111 UValueComparator *valueComp, UErrorCode& status) {
112 if (U_FAILURE(status)) {
115 uhash_init(&hashObj, keyHash, keyComp, valueComp, &status);
116 if (U_SUCCESS(status)) {
118 uhash_setKeyDeleter(hash, uprv_deleteUObject);
122 inline Hashtable::Hashtable(UKeyComparator *keyComp, UValueComparator *valueComp,
123 UErrorCode& status) : hash(0) {
124 init( uhash_hashUnicodeString, keyComp, valueComp, status);
126 inline Hashtable::Hashtable(UBool ignoreKeyCase, UErrorCode& status)
129 init(ignoreKeyCase ? uhash_hashCaselessUnicodeString
130 : uhash_hashUnicodeString,
131 ignoreKeyCase ? uhash_compareCaselessUnicodeString
132 : uhash_compareUnicodeString,
137 inline Hashtable::Hashtable(UErrorCode& status)
140 init(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, status);
143 inline Hashtable::Hashtable()
146 UErrorCode status = U_ZERO_ERROR;
147 init(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, status);
150 inline Hashtable::~Hashtable() {
156 inline UObjectDeleter *Hashtable::setValueDeleter(UObjectDeleter *fn) {
157 return uhash_setValueDeleter(hash, fn);
160 inline int32_t Hashtable::count() const {
161 return uhash_count(hash);
164 inline void* Hashtable::put(const UnicodeString& key, void* value, UErrorCode& status) {
165 return uhash_put(hash, new UnicodeString(key), value, &status);
168 inline int32_t Hashtable::puti(const UnicodeString& key, int32_t value, UErrorCode& status) {
169 return uhash_puti(hash, new UnicodeString(key), value, &status);
172 inline void* Hashtable::get(const UnicodeString& key) const {
173 return uhash_get(hash, &key);
176 inline int32_t Hashtable::geti(const UnicodeString& key) const {
177 return uhash_geti(hash, &key);
180 inline void* Hashtable::remove(const UnicodeString& key) {
181 return uhash_remove(hash, &key);
184 inline int32_t Hashtable::removei(const UnicodeString& key) {
185 return uhash_removei(hash, &key);
188 inline const UHashElement* Hashtable::find(const UnicodeString& key) const {
189 return uhash_find(hash, &key);
192 inline const UHashElement* Hashtable::nextElement(int32_t& pos) const {
193 return uhash_nextElement(hash, &pos);
196 inline void Hashtable::removeAll(void) {
197 uhash_removeAll(hash);
200 inline UKeyComparator* Hashtable::setKeyComparator(UKeyComparator*keyComp){
201 return uhash_setKeyComparator(hash, keyComp);
204 inline UValueComparator* Hashtable::setValueComparator(UValueComparator* valueComp){
205 return uhash_setValueComparator(hash, valueComp);
208 inline UBool Hashtable::equals(const Hashtable& that)const{
209 return uhash_equals(hash, that.hash);