X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/b75a7d8f3b4adbae880cab104ce2c6a50eee4db2..340931cb2e044a2141d11567dd0f782524e32994:/icuSources/common/hash.h diff --git a/icuSources/common/hash.h b/icuSources/common/hash.h index dfad39d1..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-2001, 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,6 +29,11 @@ U_NAMESPACE_BEGIN */ class U_COMMON_API Hashtable : public UMemory { UHashtable* hash; + UHashtable hashObj; + + 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: /** @@ -33,43 +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 + */ + 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. - * @param ignoreKeyCase if TRUE, keys are case insensitive */ - Hashtable(UBool ignoreKeyCase = FALSE); + inline Hashtable(); /** * Non-virtual destructor; make this virtual if Hashtable is subclassed * in the future. */ - ~Hashtable(); + inline ~Hashtable(); - UObjectDeleter *setValueDeleter(UObjectDeleter *fn); + inline UObjectDeleter *setValueDeleter(UObjectDeleter *fn); - int32_t count() const; + inline int32_t count() const; - void* put(const UnicodeString& key, void* value, UErrorCode& status); + inline void* put(const UnicodeString& key, void* value, UErrorCode& status); - int32_t puti(const UnicodeString& key, int32_t value, UErrorCode& status); + inline int32_t puti(const UnicodeString& key, int32_t value, UErrorCode& status); - void* get(const UnicodeString& key) const; - - int32_t geti(const UnicodeString& key) const; - - void* remove(const UnicodeString& key); + inline void* get(const UnicodeString& key) const; - int32_t removei(const UnicodeString& key); + inline int32_t geti(const UnicodeString& key) const; - void removeAll(void); + inline void* remove(const UnicodeString& key); - const UHashElement* find(const UnicodeString& key) const; + inline int32_t removei(const UnicodeString& key); + + inline void removeAll(void); + + inline const UHashElement* find(const UnicodeString& key) const; + + /** + * @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* nextElement(int32_t& pos) const; + inline UKeyComparator* setKeyComparator(UKeyComparator*keyComp); + 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 @@ -79,37 +117,73 @@ private: * Implementation ********************************************************************/ -inline Hashtable::Hashtable(UBool ignoreKeyCase, UErrorCode& status) : - hash(0) { +inline void Hashtable::init(UHashFunction *keyHash, UKeyComparator *keyComp, + UValueComparator *valueComp, UErrorCode& status) { if (U_FAILURE(status)) { return; } - hash = uhash_open(ignoreKeyCase ? uhash_hashCaselessUnicodeString - : uhash_hashUnicodeString, - ignoreKeyCase ? uhash_compareCaselessUnicodeString - : uhash_compareUnicodeString, - &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 Hashtable::Hashtable(UBool ignoreKeyCase) : hash(0) { - UErrorCode status = U_ZERO_ERROR; - hash = uhash_open(ignoreKeyCase ? uhash_hashCaselessUnicodeString - : uhash_hashUnicodeString, - ignoreKeyCase ? uhash_compareCaselessUnicodeString - : uhash_compareUnicodeString, - &status); +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)) { - uhash_setKeyDeleter(hash, uhash_deleteUnicodeString); + 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) +{ + init(ignoreKeyCase ? uhash_hashCaselessUnicodeString + : 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, NULL, status); +} + +inline Hashtable::Hashtable() + : hash(0) +{ + UErrorCode status = U_ZERO_ERROR; + init(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, status); +} + inline Hashtable::~Hashtable() { - if (hash != 0) { + if (hash != NULL) { uhash_close(hash); - hash = 0; } } @@ -154,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 +