2000-03-28 22:04:39 +00:00
|
|
|
/*
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
|
|
|
* Copyright (C) 1997-2001, 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 "uhash.h"
|
|
|
|
#include "unicode/unistr.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
class Hashtable {
|
|
|
|
UHashtable* hash;
|
|
|
|
|
|
|
|
public:
|
2001-06-20 22:37:16 +00:00
|
|
|
Hashtable(UBool ignoreKeyCase, UErrorCode& status);
|
2000-03-28 22:04:39 +00:00
|
|
|
|
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
|
|
|
*/
|
2001-06-20 22:37:16 +00:00
|
|
|
Hashtable(UBool ignoreKeyCase = FALSE);
|
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();
|
|
|
|
|
|
|
|
UObjectDeleter setValueDeleter(UObjectDeleter fn);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
void* get(const UnicodeString& key) const;
|
|
|
|
|
|
|
|
void* remove(const UnicodeString& key);
|
2000-03-29 19:16:54 +00:00
|
|
|
|
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;
|
2000-03-28 22:04:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* Implementation
|
|
|
|
********************************************************************/
|
|
|
|
|
2001-06-20 22:37:16 +00:00
|
|
|
inline Hashtable::Hashtable(UBool ignoreKeyCase, UErrorCode& status) :
|
|
|
|
hash(0) {
|
2000-03-28 22:04:39 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2001-06-20 22:37:16 +00:00
|
|
|
hash = uhash_open(ignoreKeyCase ? uhash_hashCaselessUnicodeString
|
|
|
|
: uhash_hashUnicodeString,
|
|
|
|
ignoreKeyCase ? uhash_compareCaselessUnicodeString
|
|
|
|
: uhash_compareUnicodeString,
|
|
|
|
&status);
|
2000-03-28 22:04:39 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
uhash_setKeyDeleter(hash, uhash_deleteUnicodeString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-20 22:37:16 +00:00
|
|
|
inline Hashtable::Hashtable(UBool ignoreKeyCase) : hash(0) {
|
2000-03-30 04:17:27 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2001-06-20 22:37:16 +00:00
|
|
|
hash = uhash_open(ignoreKeyCase ? uhash_hashCaselessUnicodeString
|
|
|
|
: uhash_hashUnicodeString,
|
|
|
|
ignoreKeyCase ? uhash_compareCaselessUnicodeString
|
|
|
|
: uhash_compareUnicodeString,
|
|
|
|
&status);
|
2000-03-30 04:17:27 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
uhash_setKeyDeleter(hash, uhash_deleteUnicodeString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-28 22:04:39 +00:00
|
|
|
inline Hashtable::~Hashtable() {
|
|
|
|
if (hash != 0) {
|
|
|
|
uhash_close(hash);
|
|
|
|
hash = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline UObjectDeleter Hashtable::setValueDeleter(UObjectDeleter fn) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void* Hashtable::get(const UnicodeString& key) const {
|
|
|
|
return uhash_get(hash, &key);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void* Hashtable::remove(const UnicodeString& key) {
|
|
|
|
return uhash_remove(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
|
|
|
}
|
|
|
|
|
2000-03-28 22:04:39 +00:00
|
|
|
#endif
|