X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/374ca955a76ecab1204ca8bfa63ff9238d998416..HEAD:/icuSources/common/hash.h diff --git a/icuSources/common/hash.h b/icuSources/common/hash.h index f1ea543c..f02cb708 100644 --- a/icuSources/common/hash.h +++ b/icuSources/common/hash.h @@ -1,6 +1,8 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /* ****************************************************************************** -* Copyright (C) 1997-2004, International Business Machines +* Copyright (C) 1997-2014, International Business Machines * Corporation and others. All Rights Reserved. ****************************************************************************** * Date Name Description @@ -13,6 +15,7 @@ #include "unicode/unistr.h" #include "unicode/uobject.h" +#include "cmemory.h" #include "uhash.h" U_NAMESPACE_BEGIN @@ -26,8 +29,11 @@ U_NAMESPACE_BEGIN */ class U_COMMON_API Hashtable : public UMemory { UHashtable* hash; + UHashtable hashObj; - inline void init(UHashFunction *keyHash, UKeyComparator *keyComp, UErrorCode& status); + inline void init(UHashFunction *keyHash, UKeyComparator *keyComp, UValueComparator *valueComp, UErrorCode& status); + + inline void initSize(UHashFunction *keyHash, UKeyComparator *keyComp, UValueComparator *valueComp, int32_t size, UErrorCode& status); public: /** @@ -35,48 +41,73 @@ public: * @param ignoreKeyCase If true, keys are case insensitive. * @param status Error code */ - Hashtable(UBool ignoreKeyCase, UErrorCode& status); + inline Hashtable(UBool ignoreKeyCase, UErrorCode& status); /** * Construct a hashtable + * @param ignoreKeyCase If true, keys are case insensitive. + * @param size initial size allocation * @param status Error code */ - Hashtable(UErrorCode& status); + inline Hashtable(UBool ignoreKeyCase, int32_t size, UErrorCode& status); + + /** + * Construct a hashtable + * @param keyComp Comparator for comparing the keys + * @param valueComp Comparator for comparing the values + * @param status Error code + */ + inline Hashtable(UKeyComparator *keyComp, UValueComparator *valueComp, UErrorCode& status); + + /** + * Construct a hashtable + * @param status Error code + */ + inline Hashtable(UErrorCode& status); /** * Construct a hashtable, _disregarding any error_. Use this constructor * with caution. */ - Hashtable(); + inline Hashtable(); /** * Non-virtual destructor; make this virtual if Hashtable is subclassed * in the future. */ - ~Hashtable(); + inline ~Hashtable(); + + inline UObjectDeleter *setValueDeleter(UObjectDeleter *fn); + + inline int32_t count() const; + + inline void* put(const UnicodeString& key, void* value, UErrorCode& status); + + inline int32_t puti(const UnicodeString& key, int32_t value, UErrorCode& status); - UObjectDeleter *setValueDeleter(UObjectDeleter *fn); + inline void* get(const UnicodeString& key) const; - int32_t count() const; + inline int32_t geti(const UnicodeString& key) const; - void* put(const UnicodeString& key, void* value, UErrorCode& status); + inline void* remove(const UnicodeString& key); - int32_t puti(const UnicodeString& key, int32_t value, UErrorCode& status); + inline int32_t removei(const UnicodeString& key); - void* get(const UnicodeString& key) const; - - int32_t geti(const UnicodeString& key) const; - - void* remove(const UnicodeString& key); + inline void removeAll(void); - int32_t removei(const UnicodeString& key); + inline const UHashElement* find(const UnicodeString& key) const; - void removeAll(void); + /** + * @param pos - must be UHASH_FIRST on first call, and untouched afterwards. + * @see uhash_nextElement + */ + inline const UHashElement* nextElement(int32_t& pos) const; - const UHashElement* find(const UnicodeString& key) const; + inline UKeyComparator* setKeyComparator(UKeyComparator*keyComp); - const UHashElement* nextElement(int32_t& pos) const; + inline UValueComparator* setValueComparator(UValueComparator* valueComp); + inline UBool equals(const Hashtable& that) const; private: Hashtable(const Hashtable &other); // forbid copying of this class Hashtable &operator=(const Hashtable &other); // forbid copying of this class @@ -86,16 +117,35 @@ private: * Implementation ********************************************************************/ -inline void Hashtable::init(UHashFunction *keyHash, UKeyComparator *keyComp, UErrorCode& status) { +inline void Hashtable::init(UHashFunction *keyHash, UKeyComparator *keyComp, + UValueComparator *valueComp, UErrorCode& status) { if (U_FAILURE(status)) { return; } - hash = uhash_open(keyHash, keyComp, &status); + uhash_init(&hashObj, keyHash, keyComp, valueComp, &status); if (U_SUCCESS(status)) { - uhash_setKeyDeleter(hash, uhash_deleteUnicodeString); + hash = &hashObj; + uhash_setKeyDeleter(hash, uprv_deleteUObject); } } +inline void Hashtable::initSize(UHashFunction *keyHash, UKeyComparator *keyComp, + UValueComparator *valueComp, int32_t size, UErrorCode& status) { + if (U_FAILURE(status)) { + return; + } + uhash_initSize(&hashObj, keyHash, keyComp, valueComp, size, &status); + if (U_SUCCESS(status)) { + hash = &hashObj; + uhash_setKeyDeleter(hash, uprv_deleteUObject); + } +} + +inline Hashtable::Hashtable(UKeyComparator *keyComp, UValueComparator *valueComp, + UErrorCode& status) : hash(0) { + init( uhash_hashUnicodeString, keyComp, valueComp, status); +} + inline Hashtable::Hashtable(UBool ignoreKeyCase, UErrorCode& status) : hash(0) { @@ -103,26 +153,37 @@ inline Hashtable::Hashtable(UBool ignoreKeyCase, UErrorCode& status) : uhash_hashUnicodeString, ignoreKeyCase ? uhash_compareCaselessUnicodeString : uhash_compareUnicodeString, + NULL, + status); +} + +inline Hashtable::Hashtable(UBool ignoreKeyCase, int32_t size, UErrorCode& status) + : hash(0) +{ + initSize(ignoreKeyCase ? uhash_hashCaselessUnicodeString + : uhash_hashUnicodeString, + ignoreKeyCase ? uhash_compareCaselessUnicodeString + : uhash_compareUnicodeString, + NULL, size, status); } inline Hashtable::Hashtable(UErrorCode& status) : hash(0) { - init(uhash_hashUnicodeString, uhash_compareUnicodeString, status); + init(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, status); } inline Hashtable::Hashtable() : hash(0) { UErrorCode status = U_ZERO_ERROR; - init(uhash_hashUnicodeString, uhash_compareUnicodeString, status); + init(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, status); } inline Hashtable::~Hashtable() { - if (hash != 0) { + if (hash != NULL) { uhash_close(hash); - hash = 0; } } @@ -167,9 +228,21 @@ inline const UHashElement* Hashtable::nextElement(int32_t& pos) const { } inline void Hashtable::removeAll(void) { - uhash_removeAll(hash); + uhash_removeAll(hash); +} + +inline UKeyComparator* Hashtable::setKeyComparator(UKeyComparator*keyComp){ + return uhash_setKeyComparator(hash, keyComp); +} + +inline UValueComparator* Hashtable::setValueComparator(UValueComparator* valueComp){ + return uhash_setValueComparator(hash, valueComp); } +inline UBool Hashtable::equals(const Hashtable& that)const{ + return uhash_equals(hash, that.hash); +} U_NAMESPACE_END #endif +