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