michael@0: /* michael@0: ****************************************************************************** michael@0: * Copyright (C) 1997-2011, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ****************************************************************************** michael@0: * Date Name Description michael@0: * 03/28/00 aliu Creation. michael@0: ****************************************************************************** michael@0: */ michael@0: michael@0: #ifndef HASH_H michael@0: #define HASH_H michael@0: michael@0: #include "unicode/unistr.h" michael@0: #include "unicode/uobject.h" michael@0: #include "cmemory.h" michael@0: #include "uhash.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: /** michael@0: * Hashtable is a thin C++ wrapper around UHashtable, a general-purpose void* michael@0: * hashtable implemented in C. Hashtable is designed to be idiomatic and michael@0: * easy-to-use in C++. michael@0: * michael@0: * Hashtable is an INTERNAL CLASS. michael@0: */ michael@0: class U_COMMON_API Hashtable : public UMemory { michael@0: UHashtable* hash; michael@0: UHashtable hashObj; michael@0: michael@0: inline void init(UHashFunction *keyHash, UKeyComparator *keyComp, UValueComparator *valueComp, UErrorCode& status); michael@0: michael@0: public: michael@0: /** michael@0: * Construct a hashtable michael@0: * @param ignoreKeyCase If true, keys are case insensitive. michael@0: * @param status Error code michael@0: */ michael@0: Hashtable(UBool ignoreKeyCase, UErrorCode& status); michael@0: michael@0: /** michael@0: * Construct a hashtable michael@0: * @param keyComp Comparator for comparing the keys michael@0: * @param valueComp Comparator for comparing the values michael@0: * @param status Error code michael@0: */ michael@0: Hashtable(UKeyComparator *keyComp, UValueComparator *valueComp, UErrorCode& status); michael@0: michael@0: /** michael@0: * Construct a hashtable michael@0: * @param status Error code michael@0: */ michael@0: Hashtable(UErrorCode& status); michael@0: michael@0: /** michael@0: * Construct a hashtable, _disregarding any error_. Use this constructor michael@0: * with caution. michael@0: */ michael@0: Hashtable(); michael@0: michael@0: /** michael@0: * Non-virtual destructor; make this virtual if Hashtable is subclassed michael@0: * in the future. michael@0: */ michael@0: ~Hashtable(); michael@0: michael@0: UObjectDeleter *setValueDeleter(UObjectDeleter *fn); michael@0: michael@0: int32_t count() const; michael@0: michael@0: void* put(const UnicodeString& key, void* value, UErrorCode& status); michael@0: michael@0: int32_t puti(const UnicodeString& key, int32_t value, UErrorCode& status); michael@0: michael@0: void* get(const UnicodeString& key) const; michael@0: michael@0: int32_t geti(const UnicodeString& key) const; michael@0: michael@0: void* remove(const UnicodeString& key); michael@0: michael@0: int32_t removei(const UnicodeString& key); michael@0: michael@0: void removeAll(void); michael@0: michael@0: const UHashElement* find(const UnicodeString& key) const; michael@0: michael@0: const UHashElement* nextElement(int32_t& pos) const; michael@0: michael@0: UKeyComparator* setKeyComparator(UKeyComparator*keyComp); michael@0: michael@0: UValueComparator* setValueComparator(UValueComparator* valueComp); michael@0: michael@0: UBool equals(const Hashtable& that) const; michael@0: private: michael@0: Hashtable(const Hashtable &other); // forbid copying of this class michael@0: Hashtable &operator=(const Hashtable &other); // forbid copying of this class michael@0: }; michael@0: michael@0: /********************************************************************* michael@0: * Implementation michael@0: ********************************************************************/ michael@0: michael@0: inline void Hashtable::init(UHashFunction *keyHash, UKeyComparator *keyComp, michael@0: UValueComparator *valueComp, UErrorCode& status) { michael@0: if (U_FAILURE(status)) { michael@0: return; michael@0: } michael@0: uhash_init(&hashObj, keyHash, keyComp, valueComp, &status); michael@0: if (U_SUCCESS(status)) { michael@0: hash = &hashObj; michael@0: uhash_setKeyDeleter(hash, uprv_deleteUObject); michael@0: } michael@0: } michael@0: michael@0: inline Hashtable::Hashtable(UKeyComparator *keyComp, UValueComparator *valueComp, michael@0: UErrorCode& status) : hash(0) { michael@0: init( uhash_hashUnicodeString, keyComp, valueComp, status); michael@0: } michael@0: inline Hashtable::Hashtable(UBool ignoreKeyCase, UErrorCode& status) michael@0: : hash(0) michael@0: { michael@0: init(ignoreKeyCase ? uhash_hashCaselessUnicodeString michael@0: : uhash_hashUnicodeString, michael@0: ignoreKeyCase ? uhash_compareCaselessUnicodeString michael@0: : uhash_compareUnicodeString, michael@0: NULL, michael@0: status); michael@0: } michael@0: michael@0: inline Hashtable::Hashtable(UErrorCode& status) michael@0: : hash(0) michael@0: { michael@0: init(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, status); michael@0: } michael@0: michael@0: inline Hashtable::Hashtable() michael@0: : hash(0) michael@0: { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: init(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, status); michael@0: } michael@0: michael@0: inline Hashtable::~Hashtable() { michael@0: if (hash != NULL) { michael@0: uhash_close(hash); michael@0: } michael@0: } michael@0: michael@0: inline UObjectDeleter *Hashtable::setValueDeleter(UObjectDeleter *fn) { michael@0: return uhash_setValueDeleter(hash, fn); michael@0: } michael@0: michael@0: inline int32_t Hashtable::count() const { michael@0: return uhash_count(hash); michael@0: } michael@0: michael@0: inline void* Hashtable::put(const UnicodeString& key, void* value, UErrorCode& status) { michael@0: return uhash_put(hash, new UnicodeString(key), value, &status); michael@0: } michael@0: michael@0: inline int32_t Hashtable::puti(const UnicodeString& key, int32_t value, UErrorCode& status) { michael@0: return uhash_puti(hash, new UnicodeString(key), value, &status); michael@0: } michael@0: michael@0: inline void* Hashtable::get(const UnicodeString& key) const { michael@0: return uhash_get(hash, &key); michael@0: } michael@0: michael@0: inline int32_t Hashtable::geti(const UnicodeString& key) const { michael@0: return uhash_geti(hash, &key); michael@0: } michael@0: michael@0: inline void* Hashtable::remove(const UnicodeString& key) { michael@0: return uhash_remove(hash, &key); michael@0: } michael@0: michael@0: inline int32_t Hashtable::removei(const UnicodeString& key) { michael@0: return uhash_removei(hash, &key); michael@0: } michael@0: michael@0: inline const UHashElement* Hashtable::find(const UnicodeString& key) const { michael@0: return uhash_find(hash, &key); michael@0: } michael@0: michael@0: inline const UHashElement* Hashtable::nextElement(int32_t& pos) const { michael@0: return uhash_nextElement(hash, &pos); michael@0: } michael@0: michael@0: inline void Hashtable::removeAll(void) { michael@0: uhash_removeAll(hash); michael@0: } michael@0: michael@0: inline UKeyComparator* Hashtable::setKeyComparator(UKeyComparator*keyComp){ michael@0: return uhash_setKeyComparator(hash, keyComp); michael@0: } michael@0: michael@0: inline UValueComparator* Hashtable::setValueComparator(UValueComparator* valueComp){ michael@0: return uhash_setValueComparator(hash, valueComp); michael@0: } michael@0: michael@0: inline UBool Hashtable::equals(const Hashtable& that)const{ michael@0: return uhash_equals(hash, that.hash); michael@0: } michael@0: U_NAMESPACE_END michael@0: michael@0: #endif michael@0: