os/textandloc/fontservices/textshaperplugin/IcuSource/common/hash.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 ******************************************************************************
     3 *   Copyright (C) 1997-2004, International Business Machines
     4 *   Corporation and others.  All Rights Reserved.
     5 ******************************************************************************
     6 *   Date        Name        Description
     7 *   03/28/00    aliu        Creation.
     8 ******************************************************************************
     9 */
    10 
    11 #ifndef HASH_H
    12 #define HASH_H
    13 
    14 #include "unicode/unistr.h"
    15 #include "unicode/uobject.h"
    16 #include "uhash.h"
    17 
    18 U_NAMESPACE_BEGIN
    19 
    20 /**
    21  * Hashtable is a thin C++ wrapper around UHashtable, a general-purpose void*
    22  * hashtable implemented in C.  Hashtable is designed to be idiomatic and
    23  * easy-to-use in C++.
    24  *
    25  * Hashtable is an INTERNAL CLASS.
    26  */
    27 class U_COMMON_API Hashtable : public UMemory {
    28     UHashtable* hash;
    29 
    30     inline void init(UHashFunction *keyHash, UKeyComparator *keyComp, UErrorCode& status);
    31 
    32 public:
    33     /**
    34      * Construct a hashtable
    35      * @param ignoreKeyCase If true, keys are case insensitive.
    36      * @param status Error code
    37     */
    38     Hashtable(UBool ignoreKeyCase, UErrorCode& status);
    39 
    40     /**
    41      * Construct a hashtable
    42      * @param status Error code
    43     */
    44     Hashtable(UErrorCode& status);
    45 
    46     /**
    47      * Construct a hashtable, _disregarding any error_.  Use this constructor
    48      * with caution.
    49      */
    50     Hashtable();
    51 
    52     /**
    53      * Non-virtual destructor; make this virtual if Hashtable is subclassed
    54      * in the future.
    55      */
    56     ~Hashtable();
    57 
    58     UObjectDeleter *setValueDeleter(UObjectDeleter *fn);
    59 
    60     int32_t count() const;
    61 
    62     void* put(const UnicodeString& key, void* value, UErrorCode& status);
    63 
    64     int32_t puti(const UnicodeString& key, int32_t value, UErrorCode& status);
    65 
    66     void* get(const UnicodeString& key) const;
    67     
    68     int32_t geti(const UnicodeString& key) const;
    69     
    70     void* remove(const UnicodeString& key);
    71 
    72     int32_t removei(const UnicodeString& key);
    73 
    74     void removeAll(void);
    75 
    76     const UHashElement* find(const UnicodeString& key) const;
    77 
    78     const UHashElement* nextElement(int32_t& pos) const;
    79 
    80 private:
    81     Hashtable(const Hashtable &other); // forbid copying of this class
    82     Hashtable &operator=(const Hashtable &other); // forbid copying of this class
    83 };
    84 
    85 /*********************************************************************
    86  * Implementation
    87  ********************************************************************/
    88 
    89 inline void Hashtable::init(UHashFunction *keyHash, UKeyComparator *keyComp, UErrorCode& status) {
    90     if (U_FAILURE(status)) {
    91         return;
    92     }
    93     hash = uhash_open(keyHash, keyComp, &status);
    94     if (U_SUCCESS(status)) {
    95         uhash_setKeyDeleter(hash, uhash_deleteUnicodeString);
    96     }
    97 }
    98 
    99 inline Hashtable::Hashtable(UBool ignoreKeyCase, UErrorCode& status)
   100  : hash(0)
   101 {
   102     init(ignoreKeyCase ? uhash_hashCaselessUnicodeString
   103                         : uhash_hashUnicodeString,
   104             ignoreKeyCase ? uhash_compareCaselessUnicodeString
   105                         : uhash_compareUnicodeString,
   106             status);
   107 }
   108 
   109 inline Hashtable::Hashtable(UErrorCode& status)
   110  : hash(0)
   111 {
   112     init(uhash_hashUnicodeString, uhash_compareUnicodeString, status);
   113 }
   114 
   115 inline Hashtable::Hashtable()
   116  : hash(0)
   117 {
   118     UErrorCode status = U_ZERO_ERROR;
   119     init(uhash_hashUnicodeString, uhash_compareUnicodeString, status);
   120 }
   121 
   122 inline Hashtable::~Hashtable() {
   123     if (hash != 0) {
   124         uhash_close(hash);
   125         hash = 0;
   126     }
   127 }
   128 
   129 inline UObjectDeleter *Hashtable::setValueDeleter(UObjectDeleter *fn) {
   130     return uhash_setValueDeleter(hash, fn);
   131 }
   132 
   133 inline int32_t Hashtable::count() const {
   134     return uhash_count(hash);
   135 }
   136 
   137 inline void* Hashtable::put(const UnicodeString& key, void* value, UErrorCode& status) {
   138     return uhash_put(hash, new UnicodeString(key), value, &status);
   139 }
   140 
   141 inline int32_t Hashtable::puti(const UnicodeString& key, int32_t value, UErrorCode& status) {
   142     return uhash_puti(hash, new UnicodeString(key), value, &status);
   143 }
   144 
   145 inline void* Hashtable::get(const UnicodeString& key) const {
   146     return uhash_get(hash, &key);
   147 }
   148 
   149 inline int32_t Hashtable::geti(const UnicodeString& key) const {
   150     return uhash_geti(hash, &key);
   151 }
   152 
   153 inline void* Hashtable::remove(const UnicodeString& key) {
   154     return uhash_remove(hash, &key);
   155 }
   156 
   157 inline int32_t Hashtable::removei(const UnicodeString& key) {
   158     return uhash_removei(hash, &key);
   159 }
   160 
   161 inline const UHashElement* Hashtable::find(const UnicodeString& key) const {
   162     return uhash_find(hash, &key);
   163 }
   164 
   165 inline const UHashElement* Hashtable::nextElement(int32_t& pos) const {
   166     return uhash_nextElement(hash, &pos);
   167 }
   168 
   169 inline void Hashtable::removeAll(void) {
   170   uhash_removeAll(hash);
   171 }
   172 
   173 U_NAMESPACE_END
   174 
   175 #endif