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