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