intl/icu/source/common/hash.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/common/hash.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,208 @@
     1.4 +/*
     1.5 +******************************************************************************
     1.6 +*   Copyright (C) 1997-2011, 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 "cmemory.h"
    1.20 +#include "uhash.h"
    1.21 +
    1.22 +U_NAMESPACE_BEGIN
    1.23 +
    1.24 +/**
    1.25 + * Hashtable is a thin C++ wrapper around UHashtable, a general-purpose void*
    1.26 + * hashtable implemented in C.  Hashtable is designed to be idiomatic and
    1.27 + * easy-to-use in C++.
    1.28 + *
    1.29 + * Hashtable is an INTERNAL CLASS.
    1.30 + */
    1.31 +class U_COMMON_API Hashtable : public UMemory {
    1.32 +    UHashtable* hash;
    1.33 +    UHashtable hashObj;
    1.34 +
    1.35 +    inline void init(UHashFunction *keyHash, UKeyComparator *keyComp, UValueComparator *valueComp, UErrorCode& status);
    1.36 +
    1.37 +public:
    1.38 +    /**
    1.39 +     * Construct a hashtable
    1.40 +     * @param ignoreKeyCase If true, keys are case insensitive.
    1.41 +     * @param status Error code
    1.42 +    */
    1.43 +    Hashtable(UBool ignoreKeyCase, UErrorCode& status);
    1.44 +
    1.45 +    /**
    1.46 +     * Construct a hashtable
    1.47 +     * @param keyComp Comparator for comparing the keys
    1.48 +     * @param valueComp Comparator for comparing the values
    1.49 +     * @param status Error code
    1.50 +    */
    1.51 +    Hashtable(UKeyComparator *keyComp, UValueComparator *valueComp, UErrorCode& status);
    1.52 +
    1.53 +    /**
    1.54 +     * Construct a hashtable
    1.55 +     * @param status Error code
    1.56 +    */
    1.57 +    Hashtable(UErrorCode& status);
    1.58 +
    1.59 +    /**
    1.60 +     * Construct a hashtable, _disregarding any error_.  Use this constructor
    1.61 +     * with caution.
    1.62 +     */
    1.63 +    Hashtable();
    1.64 +
    1.65 +    /**
    1.66 +     * Non-virtual destructor; make this virtual if Hashtable is subclassed
    1.67 +     * in the future.
    1.68 +     */
    1.69 +    ~Hashtable();
    1.70 +
    1.71 +    UObjectDeleter *setValueDeleter(UObjectDeleter *fn);
    1.72 +
    1.73 +    int32_t count() const;
    1.74 +
    1.75 +    void* put(const UnicodeString& key, void* value, UErrorCode& status);
    1.76 +
    1.77 +    int32_t puti(const UnicodeString& key, int32_t value, UErrorCode& status);
    1.78 +
    1.79 +    void* get(const UnicodeString& key) const;
    1.80 +    
    1.81 +    int32_t geti(const UnicodeString& key) const;
    1.82 +    
    1.83 +    void* remove(const UnicodeString& key);
    1.84 +
    1.85 +    int32_t removei(const UnicodeString& key);
    1.86 +
    1.87 +    void removeAll(void);
    1.88 +
    1.89 +    const UHashElement* find(const UnicodeString& key) const;
    1.90 +
    1.91 +    const UHashElement* nextElement(int32_t& pos) const;
    1.92 +    
    1.93 +    UKeyComparator* setKeyComparator(UKeyComparator*keyComp);
    1.94 +    
    1.95 +    UValueComparator* setValueComparator(UValueComparator* valueComp);
    1.96 +
    1.97 +    UBool equals(const Hashtable& that) const;
    1.98 +private:
    1.99 +    Hashtable(const Hashtable &other); // forbid copying of this class
   1.100 +    Hashtable &operator=(const Hashtable &other); // forbid copying of this class
   1.101 +};
   1.102 +
   1.103 +/*********************************************************************
   1.104 + * Implementation
   1.105 + ********************************************************************/
   1.106 +
   1.107 +inline void Hashtable::init(UHashFunction *keyHash, UKeyComparator *keyComp, 
   1.108 +                            UValueComparator *valueComp, UErrorCode& status) {
   1.109 +    if (U_FAILURE(status)) {
   1.110 +        return;
   1.111 +    }
   1.112 +    uhash_init(&hashObj, keyHash, keyComp, valueComp, &status);
   1.113 +    if (U_SUCCESS(status)) {
   1.114 +        hash = &hashObj;
   1.115 +        uhash_setKeyDeleter(hash, uprv_deleteUObject);
   1.116 +    }
   1.117 +}
   1.118 +
   1.119 +inline Hashtable::Hashtable(UKeyComparator *keyComp, UValueComparator *valueComp, 
   1.120 +                 UErrorCode& status) : hash(0) {
   1.121 +    init( uhash_hashUnicodeString, keyComp, valueComp, status);
   1.122 +}
   1.123 +inline Hashtable::Hashtable(UBool ignoreKeyCase, UErrorCode& status)
   1.124 + : hash(0)
   1.125 +{
   1.126 +    init(ignoreKeyCase ? uhash_hashCaselessUnicodeString
   1.127 +                        : uhash_hashUnicodeString,
   1.128 +            ignoreKeyCase ? uhash_compareCaselessUnicodeString
   1.129 +                        : uhash_compareUnicodeString,
   1.130 +            NULL,
   1.131 +            status);
   1.132 +}
   1.133 +
   1.134 +inline Hashtable::Hashtable(UErrorCode& status)
   1.135 + : hash(0)
   1.136 +{
   1.137 +    init(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, status);
   1.138 +}
   1.139 +
   1.140 +inline Hashtable::Hashtable()
   1.141 + : hash(0)
   1.142 +{
   1.143 +    UErrorCode status = U_ZERO_ERROR;
   1.144 +    init(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, status);
   1.145 +}
   1.146 +
   1.147 +inline Hashtable::~Hashtable() {
   1.148 +    if (hash != NULL) {
   1.149 +        uhash_close(hash);
   1.150 +    }
   1.151 +}
   1.152 +
   1.153 +inline UObjectDeleter *Hashtable::setValueDeleter(UObjectDeleter *fn) {
   1.154 +    return uhash_setValueDeleter(hash, fn);
   1.155 +}
   1.156 +
   1.157 +inline int32_t Hashtable::count() const {
   1.158 +    return uhash_count(hash);
   1.159 +}
   1.160 +
   1.161 +inline void* Hashtable::put(const UnicodeString& key, void* value, UErrorCode& status) {
   1.162 +    return uhash_put(hash, new UnicodeString(key), value, &status);
   1.163 +}
   1.164 +
   1.165 +inline int32_t Hashtable::puti(const UnicodeString& key, int32_t value, UErrorCode& status) {
   1.166 +    return uhash_puti(hash, new UnicodeString(key), value, &status);
   1.167 +}
   1.168 +
   1.169 +inline void* Hashtable::get(const UnicodeString& key) const {
   1.170 +    return uhash_get(hash, &key);
   1.171 +}
   1.172 +
   1.173 +inline int32_t Hashtable::geti(const UnicodeString& key) const {
   1.174 +    return uhash_geti(hash, &key);
   1.175 +}
   1.176 +
   1.177 +inline void* Hashtable::remove(const UnicodeString& key) {
   1.178 +    return uhash_remove(hash, &key);
   1.179 +}
   1.180 +
   1.181 +inline int32_t Hashtable::removei(const UnicodeString& key) {
   1.182 +    return uhash_removei(hash, &key);
   1.183 +}
   1.184 +
   1.185 +inline const UHashElement* Hashtable::find(const UnicodeString& key) const {
   1.186 +    return uhash_find(hash, &key);
   1.187 +}
   1.188 +
   1.189 +inline const UHashElement* Hashtable::nextElement(int32_t& pos) const {
   1.190 +    return uhash_nextElement(hash, &pos);
   1.191 +}
   1.192 +
   1.193 +inline void Hashtable::removeAll(void) {
   1.194 +    uhash_removeAll(hash);
   1.195 +}
   1.196 +
   1.197 +inline UKeyComparator* Hashtable::setKeyComparator(UKeyComparator*keyComp){
   1.198 +    return uhash_setKeyComparator(hash, keyComp);
   1.199 +}
   1.200 +    
   1.201 +inline UValueComparator* Hashtable::setValueComparator(UValueComparator* valueComp){
   1.202 +    return uhash_setValueComparator(hash, valueComp);
   1.203 +}
   1.204 +
   1.205 +inline UBool Hashtable::equals(const Hashtable& that)const{
   1.206 +   return uhash_equals(hash, that.hash);
   1.207 +}
   1.208 +U_NAMESPACE_END
   1.209 +
   1.210 +#endif
   1.211 +

mercurial