1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/textandloc/fontservices/textshaperplugin/IcuSource/common/hash.h Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,175 @@
1.4 +/*
1.5 +******************************************************************************
1.6 +* Copyright (C) 1997-2004, International Business Machines
1.7 +* Corporation and others. All Rights Reserved.
1.8 +******************************************************************************
1.9 +* Date Name Description
1.10 +* 03/28/00 aliu Creation.
1.11 +******************************************************************************
1.12 +*/
1.13 +
1.14 +#ifndef HASH_H
1.15 +#define HASH_H
1.16 +
1.17 +#include "unicode/unistr.h"
1.18 +#include "unicode/uobject.h"
1.19 +#include "uhash.h"
1.20 +
1.21 +U_NAMESPACE_BEGIN
1.22 +
1.23 +/**
1.24 + * Hashtable is a thin C++ wrapper around UHashtable, a general-purpose void*
1.25 + * hashtable implemented in C. Hashtable is designed to be idiomatic and
1.26 + * easy-to-use in C++.
1.27 + *
1.28 + * Hashtable is an INTERNAL CLASS.
1.29 + */
1.30 +class U_COMMON_API Hashtable : public UMemory {
1.31 + UHashtable* hash;
1.32 +
1.33 + inline void init(UHashFunction *keyHash, UKeyComparator *keyComp, UErrorCode& status);
1.34 +
1.35 +public:
1.36 + /**
1.37 + * Construct a hashtable
1.38 + * @param ignoreKeyCase If true, keys are case insensitive.
1.39 + * @param status Error code
1.40 + */
1.41 + Hashtable(UBool ignoreKeyCase, UErrorCode& status);
1.42 +
1.43 + /**
1.44 + * Construct a hashtable
1.45 + * @param status Error code
1.46 + */
1.47 + Hashtable(UErrorCode& status);
1.48 +
1.49 + /**
1.50 + * Construct a hashtable, _disregarding any error_. Use this constructor
1.51 + * with caution.
1.52 + */
1.53 + Hashtable();
1.54 +
1.55 + /**
1.56 + * Non-virtual destructor; make this virtual if Hashtable is subclassed
1.57 + * in the future.
1.58 + */
1.59 + ~Hashtable();
1.60 +
1.61 + UObjectDeleter *setValueDeleter(UObjectDeleter *fn);
1.62 +
1.63 + int32_t count() const;
1.64 +
1.65 + void* put(const UnicodeString& key, void* value, UErrorCode& status);
1.66 +
1.67 + int32_t puti(const UnicodeString& key, int32_t value, UErrorCode& status);
1.68 +
1.69 + void* get(const UnicodeString& key) const;
1.70 +
1.71 + int32_t geti(const UnicodeString& key) const;
1.72 +
1.73 + void* remove(const UnicodeString& key);
1.74 +
1.75 + int32_t removei(const UnicodeString& key);
1.76 +
1.77 + void removeAll(void);
1.78 +
1.79 + const UHashElement* find(const UnicodeString& key) const;
1.80 +
1.81 + const UHashElement* nextElement(int32_t& pos) const;
1.82 +
1.83 +private:
1.84 + Hashtable(const Hashtable &other); // forbid copying of this class
1.85 + Hashtable &operator=(const Hashtable &other); // forbid copying of this class
1.86 +};
1.87 +
1.88 +/*********************************************************************
1.89 + * Implementation
1.90 + ********************************************************************/
1.91 +
1.92 +inline void Hashtable::init(UHashFunction *keyHash, UKeyComparator *keyComp, UErrorCode& status) {
1.93 + if (U_FAILURE(status)) {
1.94 + return;
1.95 + }
1.96 + hash = uhash_open(keyHash, keyComp, &status);
1.97 + if (U_SUCCESS(status)) {
1.98 + uhash_setKeyDeleter(hash, uhash_deleteUnicodeString);
1.99 + }
1.100 +}
1.101 +
1.102 +inline Hashtable::Hashtable(UBool ignoreKeyCase, UErrorCode& status)
1.103 + : hash(0)
1.104 +{
1.105 + init(ignoreKeyCase ? uhash_hashCaselessUnicodeString
1.106 + : uhash_hashUnicodeString,
1.107 + ignoreKeyCase ? uhash_compareCaselessUnicodeString
1.108 + : uhash_compareUnicodeString,
1.109 + status);
1.110 +}
1.111 +
1.112 +inline Hashtable::Hashtable(UErrorCode& status)
1.113 + : hash(0)
1.114 +{
1.115 + init(uhash_hashUnicodeString, uhash_compareUnicodeString, status);
1.116 +}
1.117 +
1.118 +inline Hashtable::Hashtable()
1.119 + : hash(0)
1.120 +{
1.121 + UErrorCode status = U_ZERO_ERROR;
1.122 + init(uhash_hashUnicodeString, uhash_compareUnicodeString, status);
1.123 +}
1.124 +
1.125 +inline Hashtable::~Hashtable() {
1.126 + if (hash != 0) {
1.127 + uhash_close(hash);
1.128 + hash = 0;
1.129 + }
1.130 +}
1.131 +
1.132 +inline UObjectDeleter *Hashtable::setValueDeleter(UObjectDeleter *fn) {
1.133 + return uhash_setValueDeleter(hash, fn);
1.134 +}
1.135 +
1.136 +inline int32_t Hashtable::count() const {
1.137 + return uhash_count(hash);
1.138 +}
1.139 +
1.140 +inline void* Hashtable::put(const UnicodeString& key, void* value, UErrorCode& status) {
1.141 + return uhash_put(hash, new UnicodeString(key), value, &status);
1.142 +}
1.143 +
1.144 +inline int32_t Hashtable::puti(const UnicodeString& key, int32_t value, UErrorCode& status) {
1.145 + return uhash_puti(hash, new UnicodeString(key), value, &status);
1.146 +}
1.147 +
1.148 +inline void* Hashtable::get(const UnicodeString& key) const {
1.149 + return uhash_get(hash, &key);
1.150 +}
1.151 +
1.152 +inline int32_t Hashtable::geti(const UnicodeString& key) const {
1.153 + return uhash_geti(hash, &key);
1.154 +}
1.155 +
1.156 +inline void* Hashtable::remove(const UnicodeString& key) {
1.157 + return uhash_remove(hash, &key);
1.158 +}
1.159 +
1.160 +inline int32_t Hashtable::removei(const UnicodeString& key) {
1.161 + return uhash_removei(hash, &key);
1.162 +}
1.163 +
1.164 +inline const UHashElement* Hashtable::find(const UnicodeString& key) const {
1.165 + return uhash_find(hash, &key);
1.166 +}
1.167 +
1.168 +inline const UHashElement* Hashtable::nextElement(int32_t& pos) const {
1.169 + return uhash_nextElement(hash, &pos);
1.170 +}
1.171 +
1.172 +inline void Hashtable::removeAll(void) {
1.173 + uhash_removeAll(hash);
1.174 +}
1.175 +
1.176 +U_NAMESPACE_END
1.177 +
1.178 +#endif