-
Notifications
You must be signed in to change notification settings - Fork 116
/
static_unordered_map.h
345 lines (295 loc) · 6.71 KB
/
static_unordered_map.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#ifndef _STATIC_UNORDERED_MAP_H_
#define _STATIC_UNORDERED_MAP_H_
#include "small_unordered_map.h"
/**
* XXX(stephentu): allow custom allocator
*/
template <typename Key,
typename T,
size_t StaticSize = SMALL_SIZE_MAP,
typename Hash = private_::myhash<Key>>
class static_unordered_map {
public:
typedef Key key_type;
typedef T mapped_type;
typedef std::pair<const key_type, mapped_type> value_type;
typedef Hash hasher;
typedef T & reference;
typedef const T & const_reference;
private:
typedef std::unordered_map<Key, T, Hash> large_table_type;
typedef std::pair<key_type, mapped_type> bucket_value_type;
static const bool is_trivially_destructible =
private_::is_trivially_destructible<bucket_value_type>::value;
static const size_t TableSize = private_::TableSize(StaticSize);
static_assert(StaticSize >= 1, "XXX");
static_assert(TableSize >= 1, "XXX");
struct bucket {
inline ALWAYS_INLINE bucket_value_type *
ptr()
{
return reinterpret_cast<bucket_value_type *>(&buf[0]);
}
inline ALWAYS_INLINE const bucket_value_type *
ptr() const
{
return reinterpret_cast<const bucket_value_type *>(&buf[0]);
}
inline ALWAYS_INLINE bucket_value_type &
ref()
{
return *ptr();
}
inline ALWAYS_INLINE const bucket_value_type &
ref() const
{
return *ptr();
}
template <class... Args>
inline ALWAYS_INLINE void
construct(size_t hash, Args &&... args)
{
h = hash;
new (&ref()) bucket_value_type(std::forward<Args>(args)...);
}
inline ALWAYS_INLINE void
destroy()
{
if (!is_trivially_destructible)
ref().~bucket_value_type();
}
struct bucket *bnext;
size_t h;
char buf[sizeof(value_type)];
};
// iterators are not stable across mutation
template <typename BucketType, typename ValueType>
class iterator_ : public std::iterator<std::forward_iterator_tag, ValueType> {
friend class static_unordered_map;
public:
inline iterator_() : b(0) {}
template <typename B, typename V>
inline iterator_(const iterator_<B, V> &other)
: b(other.b)
{}
inline ValueType &
operator*() const
{
return reinterpret_cast<ValueType &>(b->ref());
}
inline ValueType *
operator->() const
{
return reinterpret_cast<ValueType *>(b->ptr());
}
inline bool
operator==(const iterator_ &o) const
{
return b == o.b;
}
inline bool
operator!=(const iterator_ &o) const
{
return !operator==(o);
}
inline iterator_ &
operator++()
{
b++;
return *this;
}
inline iterator_
operator++(int)
{
iterator_ cur = *this;
++(*this);
return cur;
}
protected:
inline iterator_(BucketType *b)
: b(b)
{
}
private:
BucketType *b;
};
static size_t
chain_length(bucket *b)
{
size_t ret = 0;
while (b) {
ret++;
b = b->bnext;
}
return ret;
}
public:
typedef iterator_<bucket, value_type> iterator;
typedef iterator_<const bucket, const value_type> const_iterator;
static_unordered_map()
: n(0)
{
NDB_MEMSET(&table[0], 0, sizeof(table));
}
~static_unordered_map()
{
for (size_t i = 0; i < n; i++)
elems[i].destroy();
#ifdef ENABLE_EVENT_COUNTERS
size_t ml = 0;
for (size_t i = 0; i < TableSize; i++) {
const size_t l = chain_length(table[i]);
ml = std::max(ml, l);
}
if (ml)
private_::evt_avg_max_unordered_map_chain_length.offer(ml);
#endif
}
static_unordered_map(const static_unordered_map &other)
: n(0)
{
NDB_MEMSET(&table[0], 0, sizeof(table));
assignFrom(other);
}
static_unordered_map &
operator=(const static_unordered_map &other)
{
// self assignment
if (unlikely(this == &other))
return *this;
assignFrom(other);
return *this;
}
private:
bucket *
find_bucket(const key_type &k, size_t *hash_value)
{
const size_t h = Hash()(k);
if (hash_value)
*hash_value = h;
const size_t i = h % TableSize;
bucket *b = table[i];
while (b) {
const bool check_hash = private_::is_eq_expensive<key_type>::value;
if ((!check_hash || b->h == h) && b->ref().first == k)
return b;
b = b->bnext;
}
return 0;
}
inline ALWAYS_INLINE const bucket *
find_bucket(const key_type &k, size_t *hash_value) const
{
return const_cast<static_unordered_map *>(this)->find_bucket(k, hash_value);
}
public:
// XXX(stephentu): template away this stuff
mapped_type &
operator[](const key_type &k)
{
size_t h;
bucket *b = find_bucket(k, &h);
if (b)
return b->ref().second;
INVARIANT(n < StaticSize);
b = &elems[n++];
b->construct(h, k, mapped_type());
const size_t i = h % TableSize;
b->bnext = table[i];
table[i] = b;
return b->ref().second;
}
mapped_type &
operator[](key_type &&k)
{
size_t h;
bucket *b = find_bucket(k, &h);
if (b)
return b->ref().second;
INVARIANT(n < StaticSize);
b = &elems[n++];
b->construct(h, std::move(k), mapped_type());
const size_t i = h % TableSize;
b->bnext = table[i];
table[i] = b;
return b->ref().second;
}
inline size_t
size() const
{
return n;
}
inline bool
empty() const
{
return !n;
}
iterator
begin()
{
return iterator(&elems[0]);
}
const_iterator
begin() const
{
return const_iterator(&elems[0]);
}
inline iterator
end()
{
return iterator(&elems[n]);
}
inline const_iterator
end() const
{
return const_iterator(&elems[n]);
}
iterator
find(const key_type &k)
{
bucket * const b = find_bucket(k, 0);
if (b)
return iterator(b);
return end();
}
const_iterator
find(const key_type &k) const
{
const bucket * const b = find_bucket(k, 0);
if (b)
return const_iterator(b);
return end();
}
void
clear()
{
if (!n)
return;
NDB_MEMSET(&table[0], 0, sizeof(table));
for (size_t i = 0; i < n; i++)
elems[i].destroy();
n = 0;
}
public:
// non-standard API
inline bool is_small_type() const { return true; }
private:
// doesn't check for self assignment
inline void
assignFrom(const static_unordered_map &that)
{
clear();
for (size_t i = 0; i < that.n; i++) {
bucket * const b = &elems[n++];
const bucket * const that_b = &that.elems[i];
b->construct(that_b->h, that_b->ref().first, that_b->ref().second);
const size_t idx = b->h % TableSize;
b->bnext = table[idx];
table[idx] = b;
}
}
size_t n;
bucket elems[StaticSize];
bucket *table[TableSize];
};
#endif /* _STATIC_UNORDERED_MAP_H_ */