2017-01-20 00:20:31 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
2016-06-15 18:58:17 +00:00
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
2000-03-28 22:04:39 +00:00
|
|
|
/*
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 1997-2014, International Business Machines
|
|
|
|
* 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"
|
2011-06-03 05:23:57 +00:00
|
|
|
#include "cmemory.h"
|
2002-06-27 01:19:20 +00:00
|
|
|
#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;
|
2006-05-27 07:33:10 +00:00
|
|
|
UHashtable hashObj;
|
2000-03-28 22:04:39 +00:00
|
|
|
|
2005-11-11 19:23:09 +00:00
|
|
|
inline void init(UHashFunction *keyHash, UKeyComparator *keyComp, UValueComparator *valueComp, UErrorCode& status);
|
2004-10-08 01:52:53 +00:00
|
|
|
|
2017-09-20 00:39:40 +00:00
|
|
|
inline void initSize(UHashFunction *keyHash, UKeyComparator *keyComp, UValueComparator *valueComp, int32_t size, 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
|
|
|
|
2017-09-20 00:39:40 +00:00
|
|
|
/**
|
|
|
|
* Construct a hashtable
|
|
|
|
* @param ignoreKeyCase If true, keys are case insensitive.
|
|
|
|
* @param size initial size allocation
|
|
|
|
* @param status Error code
|
|
|
|
*/
|
|
|
|
Hashtable(UBool ignoreKeyCase, int32_t size, UErrorCode& status);
|
|
|
|
|
2005-11-11 19:23:09 +00:00
|
|
|
/**
|
|
|
|
* Construct a hashtable
|
2010-02-16 23:43:22 +00:00
|
|
|
* @param keyComp Comparator for comparing the keys
|
|
|
|
* @param valueComp Comparator for comparing the values
|
2005-11-11 19:23:09 +00:00
|
|
|
* @param status Error code
|
|
|
|
*/
|
|
|
|
Hashtable(UKeyComparator *keyComp, UValueComparator *valueComp, UErrorCode& status);
|
|
|
|
|
2004-10-08 01:52:53 +00:00
|
|
|
/**
|
|
|
|
* Construct a hashtable
|
|
|
|
* @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.
|
|
|
|
*/
|
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;
|
2017-09-20 00:39:40 +00:00
|
|
|
|
2001-10-16 20:53:13 +00:00
|
|
|
int32_t geti(const UnicodeString& key) const;
|
2017-09-20 00:39:40 +00:00
|
|
|
|
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;
|
|
|
|
|
2014-12-09 23:54:56 +00:00
|
|
|
/**
|
|
|
|
* @param pos - must be UHASH_FIRST on first call, and untouched afterwards.
|
|
|
|
* @see uhash_nextElement
|
|
|
|
*/
|
2000-03-30 04:17:27 +00:00
|
|
|
const UHashElement* nextElement(int32_t& pos) const;
|
2017-09-20 00:39:40 +00:00
|
|
|
|
2010-02-16 23:43:22 +00:00
|
|
|
UKeyComparator* setKeyComparator(UKeyComparator*keyComp);
|
2017-09-20 00:39:40 +00:00
|
|
|
|
2010-02-16 23:43:22 +00:00
|
|
|
UValueComparator* setValueComparator(UValueComparator* valueComp);
|
2002-06-29 00:04:16 +00:00
|
|
|
|
2005-11-11 19:23:09 +00:00
|
|
|
UBool equals(const Hashtable& that) 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
|
|
|
|
********************************************************************/
|
|
|
|
|
2017-09-20 00:39:40 +00:00
|
|
|
inline void Hashtable::init(UHashFunction *keyHash, UKeyComparator *keyComp,
|
2005-11-11 19:23:09 +00:00
|
|
|
UValueComparator *valueComp, UErrorCode& status) {
|
2000-03-28 22:04:39 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2006-05-27 07:33:10 +00:00
|
|
|
uhash_init(&hashObj, keyHash, keyComp, valueComp, &status);
|
2000-03-28 22:04:39 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
2006-05-27 07:33:10 +00:00
|
|
|
hash = &hashObj;
|
2011-06-03 05:23:57 +00:00
|
|
|
uhash_setKeyDeleter(hash, uprv_deleteUObject);
|
2000-03-28 22:04:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-20 00:39:40 +00:00
|
|
|
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)) {
|
|
|
|
hash = &hashObj;
|
|
|
|
uhash_setKeyDeleter(hash, uprv_deleteUObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Hashtable::Hashtable(UKeyComparator *keyComp, UValueComparator *valueComp,
|
2006-05-27 07:33:10 +00:00
|
|
|
UErrorCode& status) : hash(0) {
|
2005-11-11 19:23:09 +00:00
|
|
|
init( uhash_hashUnicodeString, keyComp, valueComp, status);
|
|
|
|
}
|
2017-09-20 00:39:40 +00:00
|
|
|
|
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,
|
2005-11-11 19:23:09 +00:00
|
|
|
NULL,
|
2004-10-08 01:52:53 +00:00
|
|
|
status);
|
|
|
|
}
|
|
|
|
|
2017-09-20 00:39:40 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2004-10-08 01:52:53 +00:00
|
|
|
inline Hashtable::Hashtable(UErrorCode& status)
|
|
|
|
: hash(0)
|
|
|
|
{
|
2005-11-11 19:23:09 +00:00
|
|
|
init(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, status);
|
2004-10-08 01:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline Hashtable::Hashtable()
|
|
|
|
: hash(0)
|
|
|
|
{
|
2000-03-30 04:17:27 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2005-11-11 19:23:09 +00:00
|
|
|
init(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, status);
|
2000-03-30 04:17:27 +00:00
|
|
|
}
|
|
|
|
|
2000-03-28 22:04:39 +00:00
|
|
|
inline Hashtable::~Hashtable() {
|
2006-05-27 07:33:10 +00:00
|
|
|
if (hash != NULL) {
|
2000-03-28 22:04:39 +00:00
|
|
|
uhash_close(hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2006-05-27 07:33:10 +00:00
|
|
|
uhash_removeAll(hash);
|
2002-03-19 07:16:19 +00:00
|
|
|
}
|
|
|
|
|
2010-02-16 23:43:22 +00:00
|
|
|
inline UKeyComparator* Hashtable::setKeyComparator(UKeyComparator*keyComp){
|
2005-11-11 19:23:09 +00:00
|
|
|
return uhash_setKeyComparator(hash, keyComp);
|
|
|
|
}
|
2017-09-20 00:39:40 +00:00
|
|
|
|
2010-02-16 23:43:22 +00:00
|
|
|
inline UValueComparator* Hashtable::setValueComparator(UValueComparator* valueComp){
|
2005-11-11 19:23:09 +00:00
|
|
|
return uhash_setValueComparator(hash, valueComp);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline UBool Hashtable::equals(const Hashtable& that)const{
|
|
|
|
return uhash_equals(hash, that.hash);
|
|
|
|
}
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2000-03-28 22:04:39 +00:00
|
|
|
#endif
|
2006-05-27 07:33:10 +00:00
|
|
|
|