intl/icu/source/common/hash.h

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 ******************************************************************************
michael@0 3 * Copyright (C) 1997-2011, International Business Machines
michael@0 4 * Corporation and others. All Rights Reserved.
michael@0 5 ******************************************************************************
michael@0 6 * Date Name Description
michael@0 7 * 03/28/00 aliu Creation.
michael@0 8 ******************************************************************************
michael@0 9 */
michael@0 10
michael@0 11 #ifndef HASH_H
michael@0 12 #define HASH_H
michael@0 13
michael@0 14 #include "unicode/unistr.h"
michael@0 15 #include "unicode/uobject.h"
michael@0 16 #include "cmemory.h"
michael@0 17 #include "uhash.h"
michael@0 18
michael@0 19 U_NAMESPACE_BEGIN
michael@0 20
michael@0 21 /**
michael@0 22 * Hashtable is a thin C++ wrapper around UHashtable, a general-purpose void*
michael@0 23 * hashtable implemented in C. Hashtable is designed to be idiomatic and
michael@0 24 * easy-to-use in C++.
michael@0 25 *
michael@0 26 * Hashtable is an INTERNAL CLASS.
michael@0 27 */
michael@0 28 class U_COMMON_API Hashtable : public UMemory {
michael@0 29 UHashtable* hash;
michael@0 30 UHashtable hashObj;
michael@0 31
michael@0 32 inline void init(UHashFunction *keyHash, UKeyComparator *keyComp, UValueComparator *valueComp, UErrorCode& status);
michael@0 33
michael@0 34 public:
michael@0 35 /**
michael@0 36 * Construct a hashtable
michael@0 37 * @param ignoreKeyCase If true, keys are case insensitive.
michael@0 38 * @param status Error code
michael@0 39 */
michael@0 40 Hashtable(UBool ignoreKeyCase, UErrorCode& status);
michael@0 41
michael@0 42 /**
michael@0 43 * Construct a hashtable
michael@0 44 * @param keyComp Comparator for comparing the keys
michael@0 45 * @param valueComp Comparator for comparing the values
michael@0 46 * @param status Error code
michael@0 47 */
michael@0 48 Hashtable(UKeyComparator *keyComp, UValueComparator *valueComp, UErrorCode& status);
michael@0 49
michael@0 50 /**
michael@0 51 * Construct a hashtable
michael@0 52 * @param status Error code
michael@0 53 */
michael@0 54 Hashtable(UErrorCode& status);
michael@0 55
michael@0 56 /**
michael@0 57 * Construct a hashtable, _disregarding any error_. Use this constructor
michael@0 58 * with caution.
michael@0 59 */
michael@0 60 Hashtable();
michael@0 61
michael@0 62 /**
michael@0 63 * Non-virtual destructor; make this virtual if Hashtable is subclassed
michael@0 64 * in the future.
michael@0 65 */
michael@0 66 ~Hashtable();
michael@0 67
michael@0 68 UObjectDeleter *setValueDeleter(UObjectDeleter *fn);
michael@0 69
michael@0 70 int32_t count() const;
michael@0 71
michael@0 72 void* put(const UnicodeString& key, void* value, UErrorCode& status);
michael@0 73
michael@0 74 int32_t puti(const UnicodeString& key, int32_t value, UErrorCode& status);
michael@0 75
michael@0 76 void* get(const UnicodeString& key) const;
michael@0 77
michael@0 78 int32_t geti(const UnicodeString& key) const;
michael@0 79
michael@0 80 void* remove(const UnicodeString& key);
michael@0 81
michael@0 82 int32_t removei(const UnicodeString& key);
michael@0 83
michael@0 84 void removeAll(void);
michael@0 85
michael@0 86 const UHashElement* find(const UnicodeString& key) const;
michael@0 87
michael@0 88 const UHashElement* nextElement(int32_t& pos) const;
michael@0 89
michael@0 90 UKeyComparator* setKeyComparator(UKeyComparator*keyComp);
michael@0 91
michael@0 92 UValueComparator* setValueComparator(UValueComparator* valueComp);
michael@0 93
michael@0 94 UBool equals(const Hashtable& that) const;
michael@0 95 private:
michael@0 96 Hashtable(const Hashtable &other); // forbid copying of this class
michael@0 97 Hashtable &operator=(const Hashtable &other); // forbid copying of this class
michael@0 98 };
michael@0 99
michael@0 100 /*********************************************************************
michael@0 101 * Implementation
michael@0 102 ********************************************************************/
michael@0 103
michael@0 104 inline void Hashtable::init(UHashFunction *keyHash, UKeyComparator *keyComp,
michael@0 105 UValueComparator *valueComp, UErrorCode& status) {
michael@0 106 if (U_FAILURE(status)) {
michael@0 107 return;
michael@0 108 }
michael@0 109 uhash_init(&hashObj, keyHash, keyComp, valueComp, &status);
michael@0 110 if (U_SUCCESS(status)) {
michael@0 111 hash = &hashObj;
michael@0 112 uhash_setKeyDeleter(hash, uprv_deleteUObject);
michael@0 113 }
michael@0 114 }
michael@0 115
michael@0 116 inline Hashtable::Hashtable(UKeyComparator *keyComp, UValueComparator *valueComp,
michael@0 117 UErrorCode& status) : hash(0) {
michael@0 118 init( uhash_hashUnicodeString, keyComp, valueComp, status);
michael@0 119 }
michael@0 120 inline Hashtable::Hashtable(UBool ignoreKeyCase, UErrorCode& status)
michael@0 121 : hash(0)
michael@0 122 {
michael@0 123 init(ignoreKeyCase ? uhash_hashCaselessUnicodeString
michael@0 124 : uhash_hashUnicodeString,
michael@0 125 ignoreKeyCase ? uhash_compareCaselessUnicodeString
michael@0 126 : uhash_compareUnicodeString,
michael@0 127 NULL,
michael@0 128 status);
michael@0 129 }
michael@0 130
michael@0 131 inline Hashtable::Hashtable(UErrorCode& status)
michael@0 132 : hash(0)
michael@0 133 {
michael@0 134 init(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, status);
michael@0 135 }
michael@0 136
michael@0 137 inline Hashtable::Hashtable()
michael@0 138 : hash(0)
michael@0 139 {
michael@0 140 UErrorCode status = U_ZERO_ERROR;
michael@0 141 init(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, status);
michael@0 142 }
michael@0 143
michael@0 144 inline Hashtable::~Hashtable() {
michael@0 145 if (hash != NULL) {
michael@0 146 uhash_close(hash);
michael@0 147 }
michael@0 148 }
michael@0 149
michael@0 150 inline UObjectDeleter *Hashtable::setValueDeleter(UObjectDeleter *fn) {
michael@0 151 return uhash_setValueDeleter(hash, fn);
michael@0 152 }
michael@0 153
michael@0 154 inline int32_t Hashtable::count() const {
michael@0 155 return uhash_count(hash);
michael@0 156 }
michael@0 157
michael@0 158 inline void* Hashtable::put(const UnicodeString& key, void* value, UErrorCode& status) {
michael@0 159 return uhash_put(hash, new UnicodeString(key), value, &status);
michael@0 160 }
michael@0 161
michael@0 162 inline int32_t Hashtable::puti(const UnicodeString& key, int32_t value, UErrorCode& status) {
michael@0 163 return uhash_puti(hash, new UnicodeString(key), value, &status);
michael@0 164 }
michael@0 165
michael@0 166 inline void* Hashtable::get(const UnicodeString& key) const {
michael@0 167 return uhash_get(hash, &key);
michael@0 168 }
michael@0 169
michael@0 170 inline int32_t Hashtable::geti(const UnicodeString& key) const {
michael@0 171 return uhash_geti(hash, &key);
michael@0 172 }
michael@0 173
michael@0 174 inline void* Hashtable::remove(const UnicodeString& key) {
michael@0 175 return uhash_remove(hash, &key);
michael@0 176 }
michael@0 177
michael@0 178 inline int32_t Hashtable::removei(const UnicodeString& key) {
michael@0 179 return uhash_removei(hash, &key);
michael@0 180 }
michael@0 181
michael@0 182 inline const UHashElement* Hashtable::find(const UnicodeString& key) const {
michael@0 183 return uhash_find(hash, &key);
michael@0 184 }
michael@0 185
michael@0 186 inline const UHashElement* Hashtable::nextElement(int32_t& pos) const {
michael@0 187 return uhash_nextElement(hash, &pos);
michael@0 188 }
michael@0 189
michael@0 190 inline void Hashtable::removeAll(void) {
michael@0 191 uhash_removeAll(hash);
michael@0 192 }
michael@0 193
michael@0 194 inline UKeyComparator* Hashtable::setKeyComparator(UKeyComparator*keyComp){
michael@0 195 return uhash_setKeyComparator(hash, keyComp);
michael@0 196 }
michael@0 197
michael@0 198 inline UValueComparator* Hashtable::setValueComparator(UValueComparator* valueComp){
michael@0 199 return uhash_setValueComparator(hash, valueComp);
michael@0 200 }
michael@0 201
michael@0 202 inline UBool Hashtable::equals(const Hashtable& that)const{
michael@0 203 return uhash_equals(hash, that.hash);
michael@0 204 }
michael@0 205 U_NAMESPACE_END
michael@0 206
michael@0 207 #endif
michael@0 208

mercurial