]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/hash.h
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / common / hash.h
1 /*
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 ******************************************************************************
9 */
10
11 #ifndef HASH_H
12 #define HASH_H
13
14 #include "unicode/unistr.h"
15 #include "unicode/uobject.h"
16 #include "uhash.h"
17
18 U_NAMESPACE_BEGIN
19
20 /**
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
23 * easy-to-use in C++.
24 *
25 * Hashtable is an INTERNAL CLASS.
26 */
27 class U_COMMON_API Hashtable : public UMemory {
28 UHashtable* hash;
29
30 public:
31 /**
32 * Construct a hashtable
33 * @param ignoreKeyCase If true, keys are case insensitive.
34 * @param status Error code
35 */
36 Hashtable(UBool ignoreKeyCase, UErrorCode& status);
37
38 /**
39 * Construct a hashtable, _disregarding any error_. Use this constructor
40 * with caution.
41 * @param ignoreKeyCase if TRUE, keys are case insensitive
42 */
43 Hashtable(UBool ignoreKeyCase = FALSE);
44
45 /**
46 * Non-virtual destructor; make this virtual if Hashtable is subclassed
47 * in the future.
48 */
49 ~Hashtable();
50
51 UObjectDeleter *setValueDeleter(UObjectDeleter *fn);
52
53 int32_t count() const;
54
55 void* put(const UnicodeString& key, void* value, UErrorCode& status);
56
57 int32_t puti(const UnicodeString& key, int32_t value, UErrorCode& status);
58
59 void* get(const UnicodeString& key) const;
60
61 int32_t geti(const UnicodeString& key) const;
62
63 void* remove(const UnicodeString& key);
64
65 int32_t removei(const UnicodeString& key);
66
67 void removeAll(void);
68
69 const UHashElement* find(const UnicodeString& key) const;
70
71 const UHashElement* nextElement(int32_t& pos) const;
72
73 private:
74 Hashtable(const Hashtable &other); // forbid copying of this class
75 Hashtable &operator=(const Hashtable &other); // forbid copying of this class
76 };
77
78 /*********************************************************************
79 * Implementation
80 ********************************************************************/
81
82 inline Hashtable::Hashtable(UBool ignoreKeyCase, UErrorCode& status) :
83 hash(0) {
84 if (U_FAILURE(status)) {
85 return;
86 }
87 hash = uhash_open(ignoreKeyCase ? uhash_hashCaselessUnicodeString
88 : uhash_hashUnicodeString,
89 ignoreKeyCase ? uhash_compareCaselessUnicodeString
90 : uhash_compareUnicodeString,
91 &status);
92 if (U_SUCCESS(status)) {
93 uhash_setKeyDeleter(hash, uhash_deleteUnicodeString);
94 }
95 }
96
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,
103 &status);
104 if (U_SUCCESS(status)) {
105 uhash_setKeyDeleter(hash, uhash_deleteUnicodeString);
106 }
107 }
108
109 inline Hashtable::~Hashtable() {
110 if (hash != 0) {
111 uhash_close(hash);
112 hash = 0;
113 }
114 }
115
116 inline UObjectDeleter *Hashtable::setValueDeleter(UObjectDeleter *fn) {
117 return uhash_setValueDeleter(hash, fn);
118 }
119
120 inline int32_t Hashtable::count() const {
121 return uhash_count(hash);
122 }
123
124 inline void* Hashtable::put(const UnicodeString& key, void* value, UErrorCode& status) {
125 return uhash_put(hash, new UnicodeString(key), value, &status);
126 }
127
128 inline int32_t Hashtable::puti(const UnicodeString& key, int32_t value, UErrorCode& status) {
129 return uhash_puti(hash, new UnicodeString(key), value, &status);
130 }
131
132 inline void* Hashtable::get(const UnicodeString& key) const {
133 return uhash_get(hash, &key);
134 }
135
136 inline int32_t Hashtable::geti(const UnicodeString& key) const {
137 return uhash_geti(hash, &key);
138 }
139
140 inline void* Hashtable::remove(const UnicodeString& key) {
141 return uhash_remove(hash, &key);
142 }
143
144 inline int32_t Hashtable::removei(const UnicodeString& key) {
145 return uhash_removei(hash, &key);
146 }
147
148 inline const UHashElement* Hashtable::find(const UnicodeString& key) const {
149 return uhash_find(hash, &key);
150 }
151
152 inline const UHashElement* Hashtable::nextElement(int32_t& pos) const {
153 return uhash_nextElement(hash, &pos);
154 }
155
156 inline void Hashtable::removeAll(void) {
157 uhash_removeAll(hash);
158 }
159
160 U_NAMESPACE_END
161
162 #endif