Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/intl/icu/source/common/hash.h
Line
Count
Source (jump to first uncovered line)
1
// © 2016 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
/*
4
******************************************************************************
5
*   Copyright (C) 1997-2014, International Business Machines
6
*   Corporation and others.  All Rights Reserved.
7
******************************************************************************
8
*   Date        Name        Description
9
*   03/28/00    aliu        Creation.
10
******************************************************************************
11
*/
12
13
#ifndef HASH_H
14
#define HASH_H
15
16
#include "unicode/unistr.h"
17
#include "unicode/uobject.h"
18
#include "cmemory.h"
19
#include "uhash.h"
20
21
U_NAMESPACE_BEGIN
22
23
/**
24
 * Hashtable is a thin C++ wrapper around UHashtable, a general-purpose void*
25
 * hashtable implemented in C.  Hashtable is designed to be idiomatic and
26
 * easy-to-use in C++.
27
 *
28
 * Hashtable is an INTERNAL CLASS.
29
 */
30
class U_COMMON_API Hashtable : public UMemory {
31
    UHashtable* hash;
32
    UHashtable hashObj;
33
34
    inline void init(UHashFunction *keyHash, UKeyComparator *keyComp, UValueComparator *valueComp, UErrorCode& status);
35
36
    inline void initSize(UHashFunction *keyHash, UKeyComparator *keyComp, UValueComparator *valueComp, int32_t size, UErrorCode& status);
37
38
public:
39
    /**
40
     * Construct a hashtable
41
     * @param ignoreKeyCase If true, keys are case insensitive.
42
     * @param status Error code
43
    */
44
    Hashtable(UBool ignoreKeyCase, UErrorCode& status);
45
46
    /**
47
     * Construct a hashtable
48
     * @param ignoreKeyCase If true, keys are case insensitive.
49
     * @param size initial size allocation
50
     * @param status Error code
51
    */
52
    Hashtable(UBool ignoreKeyCase, int32_t size, UErrorCode& status);
53
54
    /**
55
     * Construct a hashtable
56
     * @param keyComp Comparator for comparing the keys
57
     * @param valueComp Comparator for comparing the values
58
     * @param status Error code
59
    */
60
    Hashtable(UKeyComparator *keyComp, UValueComparator *valueComp, UErrorCode& status);
61
62
    /**
63
     * Construct a hashtable
64
     * @param status Error code
65
    */
66
    Hashtable(UErrorCode& status);
67
68
    /**
69
     * Construct a hashtable, _disregarding any error_.  Use this constructor
70
     * with caution.
71
     */
72
    Hashtable();
73
74
    /**
75
     * Non-virtual destructor; make this virtual if Hashtable is subclassed
76
     * in the future.
77
     */
78
    ~Hashtable();
79
80
    UObjectDeleter *setValueDeleter(UObjectDeleter *fn);
81
82
    int32_t count() const;
83
84
    void* put(const UnicodeString& key, void* value, UErrorCode& status);
85
86
    int32_t puti(const UnicodeString& key, int32_t value, UErrorCode& status);
87
88
    void* get(const UnicodeString& key) const;
89
90
    int32_t geti(const UnicodeString& key) const;
91
92
    void* remove(const UnicodeString& key);
93
94
    int32_t removei(const UnicodeString& key);
95
96
    void removeAll(void);
97
98
    const UHashElement* find(const UnicodeString& key) const;
99
100
    /**
101
     * @param pos - must be UHASH_FIRST on first call, and untouched afterwards.
102
     * @see uhash_nextElement
103
     */
104
    const UHashElement* nextElement(int32_t& pos) const;
105
106
    UKeyComparator* setKeyComparator(UKeyComparator*keyComp);
107
108
    UValueComparator* setValueComparator(UValueComparator* valueComp);
109
110
    UBool equals(const Hashtable& that) const;
111
private:
112
    Hashtable(const Hashtable &other); // forbid copying of this class
113
    Hashtable &operator=(const Hashtable &other); // forbid copying of this class
114
};
115
116
/*********************************************************************
117
 * Implementation
118
 ********************************************************************/
119
120
inline void Hashtable::init(UHashFunction *keyHash, UKeyComparator *keyComp,
121
0
                            UValueComparator *valueComp, UErrorCode& status) {
122
0
    if (U_FAILURE(status)) {
123
0
        return;
124
0
    }
125
0
    uhash_init(&hashObj, keyHash, keyComp, valueComp, &status);
126
0
    if (U_SUCCESS(status)) {
127
0
        hash = &hashObj;
128
0
        uhash_setKeyDeleter(hash, uprv_deleteUObject);
129
0
    }
130
0
}
131
132
inline void Hashtable::initSize(UHashFunction *keyHash, UKeyComparator *keyComp,
133
0
                                UValueComparator *valueComp, int32_t size, UErrorCode& status) {
134
0
    if (U_FAILURE(status)) {
135
0
        return;
136
0
    }
137
0
    uhash_initSize(&hashObj, keyHash, keyComp, valueComp, size, &status);
138
0
    if (U_SUCCESS(status)) {
139
0
        hash = &hashObj;
140
0
        uhash_setKeyDeleter(hash, uprv_deleteUObject);
141
0
    }
142
0
}
143
144
inline Hashtable::Hashtable(UKeyComparator *keyComp, UValueComparator *valueComp,
145
                 UErrorCode& status) : hash(0) {
146
    init( uhash_hashUnicodeString, keyComp, valueComp, status);
147
}
148
149
inline Hashtable::Hashtable(UBool ignoreKeyCase, UErrorCode& status)
150
 : hash(0)
151
0
{
152
0
    init(ignoreKeyCase ? uhash_hashCaselessUnicodeString
153
0
                        : uhash_hashUnicodeString,
154
0
            ignoreKeyCase ? uhash_compareCaselessUnicodeString
155
0
                        : uhash_compareUnicodeString,
156
0
            NULL,
157
0
            status);
158
0
}
159
160
inline Hashtable::Hashtable(UBool ignoreKeyCase, int32_t size, UErrorCode& status)
161
 : hash(0)
162
{
163
    initSize(ignoreKeyCase ? uhash_hashCaselessUnicodeString
164
                        : uhash_hashUnicodeString,
165
            ignoreKeyCase ? uhash_compareCaselessUnicodeString
166
                        : uhash_compareUnicodeString,
167
            NULL, size,
168
            status);
169
}
170
171
inline Hashtable::Hashtable(UErrorCode& status)
172
 : hash(0)
173
0
{
174
0
    init(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, status);
175
0
}
176
177
inline Hashtable::Hashtable()
178
 : hash(0)
179
0
{
180
0
    UErrorCode status = U_ZERO_ERROR;
181
0
    init(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, status);
182
0
}
183
184
0
inline Hashtable::~Hashtable() {
185
0
    if (hash != NULL) {
186
0
        uhash_close(hash);
187
0
    }
188
0
}
189
190
0
inline UObjectDeleter *Hashtable::setValueDeleter(UObjectDeleter *fn) {
191
0
    return uhash_setValueDeleter(hash, fn);
192
0
}
193
194
0
inline int32_t Hashtable::count() const {
195
0
    return uhash_count(hash);
196
0
}
197
198
0
inline void* Hashtable::put(const UnicodeString& key, void* value, UErrorCode& status) {
199
0
    return uhash_put(hash, new UnicodeString(key), value, &status);
200
0
}
201
202
0
inline int32_t Hashtable::puti(const UnicodeString& key, int32_t value, UErrorCode& status) {
203
0
    return uhash_puti(hash, new UnicodeString(key), value, &status);
204
0
}
205
206
0
inline void* Hashtable::get(const UnicodeString& key) const {
207
0
    return uhash_get(hash, &key);
208
0
}
209
210
0
inline int32_t Hashtable::geti(const UnicodeString& key) const {
211
0
    return uhash_geti(hash, &key);
212
0
}
213
214
0
inline void* Hashtable::remove(const UnicodeString& key) {
215
0
    return uhash_remove(hash, &key);
216
0
}
217
218
0
inline int32_t Hashtable::removei(const UnicodeString& key) {
219
0
    return uhash_removei(hash, &key);
220
0
}
221
222
0
inline const UHashElement* Hashtable::find(const UnicodeString& key) const {
223
0
    return uhash_find(hash, &key);
224
0
}
225
226
0
inline const UHashElement* Hashtable::nextElement(int32_t& pos) const {
227
0
    return uhash_nextElement(hash, &pos);
228
0
}
229
230
0
inline void Hashtable::removeAll(void) {
231
0
    uhash_removeAll(hash);
232
0
}
233
234
0
inline UKeyComparator* Hashtable::setKeyComparator(UKeyComparator*keyComp){
235
0
    return uhash_setKeyComparator(hash, keyComp);
236
0
}
237
238
0
inline UValueComparator* Hashtable::setValueComparator(UValueComparator* valueComp){
239
0
    return uhash_setValueComparator(hash, valueComp);
240
0
}
241
242
0
inline UBool Hashtable::equals(const Hashtable& that)const{
243
0
   return uhash_equals(hash, that.hash);
244
0
}
245
U_NAMESPACE_END
246
247
#endif
248