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
|
// IWYU pragma: private, include "WAVM/Inline/HashMap.h"
// You should only include this file indirectly by including HashMap.h.
// Use these macros to compress the boilerplate template declarations in a non-inline member
// function definition for HashMap.
#define HASHMAP_PARAMETERS typename Key, typename Value, typename KeyHashPolicy
#define HASHMAP_ARGUMENTS Key, Value, KeyHashPolicy
template<HASHMAP_PARAMETERS>
HashMap<HASHMAP_ARGUMENTS>::HashMap(Uptr reserveNumPairs) : table(reserveNumPairs)
{
}
template<HASHMAP_PARAMETERS>
HashMap<HASHMAP_ARGUMENTS>::HashMap(const std::initializer_list<Pair>& initializerList)
: table(initializerList.size())
{
for(const Pair& pair : initializerList)
{
const bool result = add(pair.key, pair.value);
WAVM_ASSERT(result);
}
}
template<HASHMAP_PARAMETERS>
template<typename... ValueArgs>
Value& HashMap<HASHMAP_ARGUMENTS>::getOrAdd(const Key& key, ValueArgs&&... valueArgs)
{
const Uptr hashAndOccupancy
= KeyHashPolicy::getKeyHash(key) | HashTableBucket<Pair>::isOccupiedMask;
HashTableBucket<Pair>& bucket = table.getBucketForAdd(hashAndOccupancy, key);
if(!bucket.hashAndOccupancy)
{
bucket.hashAndOccupancy = hashAndOccupancy;
bucket.storage.construct(key, std::forward<ValueArgs>(valueArgs)...);
}
return bucket.storage.get().value;
}
template<HASHMAP_PARAMETERS>
template<typename... ValueArgs>
bool HashMap<HASHMAP_ARGUMENTS>::add(const Key& key, ValueArgs&&... valueArgs)
{
const Uptr hashAndOccupancy
= KeyHashPolicy::getKeyHash(key) | HashTableBucket<Pair>::isOccupiedMask;
HashTableBucket<Pair>& bucket = table.getBucketForAdd(hashAndOccupancy, key);
if(!bucket.hashAndOccupancy)
{
bucket.hashAndOccupancy = hashAndOccupancy;
bucket.storage.construct(key, std::forward<ValueArgs>(valueArgs)...);
return true;
}
else
{
return false;
}
}
template<HASHMAP_PARAMETERS>
template<typename... ValueArgs>
void HashMap<HASHMAP_ARGUMENTS>::addOrFail(const Key& key, ValueArgs&&... valueArgs)
{
const Uptr hashAndOccupancy
= KeyHashPolicy::getKeyHash(key) | HashTableBucket<Pair>::isOccupiedMask;
HashTableBucket<Pair>& bucket = table.getBucketForAdd(hashAndOccupancy, key);
WAVM_ASSERT(!bucket.hashAndOccupancy);
bucket.hashAndOccupancy = hashAndOccupancy;
bucket.storage.construct(key, std::forward<ValueArgs>(valueArgs)...);
}
template<HASHMAP_PARAMETERS>
template<typename... ValueArgs>
Value& HashMap<HASHMAP_ARGUMENTS>::set(const Key& key, ValueArgs&&... valueArgs)
{
const Uptr hashAndOccupancy
= KeyHashPolicy::getKeyHash(key) | HashTableBucket<Pair>::isOccupiedMask;
HashTableBucket<Pair>& bucket = table.getBucketForAdd(hashAndOccupancy, key);
if(!bucket.hashAndOccupancy)
{
bucket.hashAndOccupancy = hashAndOccupancy;
bucket.storage.construct(key, std::forward<ValueArgs>(valueArgs)...);
}
else
{
bucket.storage.get().value = Value(std::forward<ValueArgs>(valueArgs)...);
}
return bucket.storage.get().value;
}
template<HASHMAP_PARAMETERS> bool HashMap<HASHMAP_ARGUMENTS>::remove(const Key& key)
{
return table.remove(KeyHashPolicy::getKeyHash(key), key);
}
template<HASHMAP_PARAMETERS> void HashMap<HASHMAP_ARGUMENTS>::removeOrFail(const Key& key)
{
const bool removed = table.remove(KeyHashPolicy::getKeyHash(key), key);
WAVM_ASSERT(removed);
}
template<HASHMAP_PARAMETERS> bool HashMap<HASHMAP_ARGUMENTS>::contains(const Key& key) const
{
const Uptr hash = KeyHashPolicy::getKeyHash(key);
const HashTableBucket<Pair>* bucket = table.getBucketForRead(hash, key);
WAVM_ASSERT(!bucket
|| bucket->hashAndOccupancy == (hash | HashTableBucket<Pair>::isOccupiedMask));
return bucket != nullptr;
}
template<HASHMAP_PARAMETERS>
const Value& HashMap<HASHMAP_ARGUMENTS>::operator[](const Key& key) const
{
const Uptr hash = KeyHashPolicy::getKeyHash(key);
const HashTableBucket<Pair>* bucket = table.getBucketForRead(hash, key);
WAVM_ASSERT(bucket);
if(!bucket)
{
// In addition to the assert, use WAVM_UNREACHABLE to ensure that GCC's -Wnull-dereference
// warning isn't triggered because it thinks this function might return nullptr.
WAVM_UNREACHABLE();
}
WAVM_ASSERT(bucket->hashAndOccupancy == (hash | HashTableBucket<Pair>::isOccupiedMask));
return bucket->storage.get().value;
}
template<HASHMAP_PARAMETERS> Value& HashMap<HASHMAP_ARGUMENTS>::operator[](const Key& key)
{
const Uptr hash = KeyHashPolicy::getKeyHash(key);
HashTableBucket<Pair>* bucket = table.getBucketForModify(hash, key);
WAVM_ASSERT(bucket);
if(!bucket)
{
// In addition to the assert, use WAVM_UNREACHABLE to ensure that GCC's -Wnull-dereference
// warning isn't triggered because it thinks this function might return nullptr.
WAVM_UNREACHABLE();
}
WAVM_ASSERT(bucket->hashAndOccupancy == (hash | HashTableBucket<Pair>::isOccupiedMask));
return bucket->storage.get().value;
}
template<HASHMAP_PARAMETERS> const Value* HashMap<HASHMAP_ARGUMENTS>::get(const Key& key) const
{
const Uptr hash = KeyHashPolicy::getKeyHash(key);
const HashTableBucket<Pair>* bucket = table.getBucketForRead(hash, key);
if(!bucket) { return nullptr; }
else
{
WAVM_ASSERT(bucket->hashAndOccupancy == (hash | HashTableBucket<Pair>::isOccupiedMask));
return &bucket->storage.get().value;
}
}
template<HASHMAP_PARAMETERS> Value* HashMap<HASHMAP_ARGUMENTS>::get(const Key& key)
{
const Uptr hash = KeyHashPolicy::getKeyHash(key);
HashTableBucket<Pair>* bucket = table.getBucketForModify(hash, key);
if(!bucket) { return nullptr; }
else
{
WAVM_ASSERT(bucket->hashAndOccupancy == (hash | HashTableBucket<Pair>::isOccupiedMask));
return &bucket->storage.get().value;
}
}
template<HASHMAP_PARAMETERS>
const HashMapPair<Key, Value>* HashMap<HASHMAP_ARGUMENTS>::getPair(const Key& key) const
{
const Uptr hash = KeyHashPolicy::getKeyHash(key);
const HashTableBucket<Pair>* bucket = table.getBucketForRead(hash, key);
if(!bucket) { return nullptr; }
else
{
WAVM_ASSERT(bucket->hashAndOccupancy == (hash | HashTableBucket<Pair>::isOccupiedMask));
return &bucket->storage.get();
}
}
template<HASHMAP_PARAMETERS> void HashMap<HASHMAP_ARGUMENTS>::clear() { table.clear(); }
template<HASHMAP_PARAMETERS> HashMapIterator<Key, Value> HashMap<HASHMAP_ARGUMENTS>::begin() const
{
// Find the first occupied bucket.
HashTableBucket<Pair>* beginBucket = table.getBuckets();
HashTableBucket<Pair>* endBucket = table.getBuckets() + table.numBuckets();
while(beginBucket < endBucket && !beginBucket->hashAndOccupancy) { ++beginBucket; };
return HashMapIterator<Key, Value>(beginBucket, endBucket);
}
template<HASHMAP_PARAMETERS> HashMapIterator<Key, Value> HashMap<HASHMAP_ARGUMENTS>::end() const
{
return HashMapIterator<Key, Value>(table.getBuckets() + table.numBuckets(),
table.getBuckets() + table.numBuckets());
}
template<HASHMAP_PARAMETERS> Uptr HashMap<HASHMAP_ARGUMENTS>::size() const { return table.size(); }
template<HASHMAP_PARAMETERS>
void HashMap<HASHMAP_ARGUMENTS>::analyzeSpaceUsage(Uptr& outTotalMemoryBytes,
Uptr& outMaxProbeCount,
F32& outOccupancy,
F32& outAverageProbeCount) const
{
return table.analyzeSpaceUsage(
outTotalMemoryBytes, outMaxProbeCount, outOccupancy, outAverageProbeCount);
}
template<typename Key, typename Value>
template<typename... ValueArgs>
HashMapPair<Key, Value>::HashMapPair(const Key& inKey, ValueArgs&&... valueArgs)
: key(inKey), value(std::forward<ValueArgs>(valueArgs)...)
{
}
template<typename Key, typename Value>
template<typename... ValueArgs>
HashMapPair<Key, Value>::HashMapPair(Key&& inKey, ValueArgs&&... valueArgs)
: key(std::move(inKey)), value(std::forward<ValueArgs>(valueArgs)...)
{
}
template<typename Key, typename Value>
bool HashMapIterator<Key, Value>::operator!=(const HashMapIterator& other) const
{
return bucket != other.bucket;
}
template<typename Key, typename Value>
bool HashMapIterator<Key, Value>::operator==(const HashMapIterator& other) const
{
return bucket == other.bucket;
}
template<typename Key, typename Value> HashMapIterator<Key, Value>::operator bool() const
{
return bucket < endBucket && bucket->hashAndOccupancy;
}
template<typename Key, typename Value> void HashMapIterator<Key, Value>::operator++()
{
do
{
++bucket;
} while(bucket < endBucket && !bucket->hashAndOccupancy);
}
template<typename Key, typename Value>
const HashMapPair<Key, Value>& HashMapIterator<Key, Value>::operator*() const
{
WAVM_ASSERT(bucket->hashAndOccupancy);
return bucket->storage.get();
}
template<typename Key, typename Value>
const HashMapPair<Key, Value>* HashMapIterator<Key, Value>::operator->() const
{
WAVM_ASSERT(bucket->hashAndOccupancy);
return &bucket->storage.get();
}
template<typename Key, typename Value>
HashMapIterator<Key, Value>::HashMapIterator(
const HashTableBucket<HashMapPair<Key, Value>>* inBucket,
const HashTableBucket<HashMapPair<Key, Value>>* inEndBucket)
: bucket(inBucket), endBucket(inEndBucket)
{
}
#undef HASHMAP_PARAMETERS
#undef HASHMAP_ARGUMENTS
|