forked from floooh/oryol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayMap.h
More file actions
317 lines (268 loc) · 11 KB
/
ArrayMap.h
File metadata and controls
317 lines (268 loc) · 11 KB
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
#pragma once
//------------------------------------------------------------------------------
/**
@class Oryol::ArrayMap
@ingroup Core
@brief map class which stores value in a separate array
An ArrayMap works like a Map, but stores the values in a separate
array from keys. This is useful if:
- the value type is big and/or has an expensive copy/move operation
- the values must remain in the order they have been added
- erasing is only fast when the order of values is not important,
in this case, use EraseSwap (see notes on erasing below)
Iterating over an ArrayMap with begin(), end() will iterate over
the values in the order the values have been added.
The operator[] will access elements by key (just as in a map). To
access an element by its value index, use ValueAtIndex()!
Be careful about erasing elements, this can be slow:
- EraseSwap() is best, this do a swap in the value array, but still
needs to iterate over the keymap to find and replace the swapped-in index
- Erase() and EraseIndex() need to do a sweep over the key map to
fix-up indices, and are thus O(N)!!!
@see Array, HashSet, Map, Set
*/
#include "Core/Config.h"
#include "Core/Containers/Array.h"
#include "Core/Containers/Map.h"
namespace Oryol {
template<class KEY, class VALUE> class ArrayMap {
public:
/// default constructor
ArrayMap();
/// copy constructor (truncates to actual size)
ArrayMap(const ArrayMap& rhs);
/// move constructor (same capacity and size)
ArrayMap(ArrayMap&& rhs);
/// copy-assignment operator (truncates to actual size)
void operator=(const ArrayMap& rhs);
/// move-assignment operator (same capacity and size)
void operator=(ArrayMap&& rhs);
/// set allocation strategy
void SetAllocStrategy(int minGrow, int maxGrow=ORYOL_CONTAINER_DEFAULT_MAX_GROW);
/// get min grow value
int GetMinGrow() const;
/// get max grow value
int GetMaxGrow() const;
/// get number of elements in array
int Size() const;
/// return true if empty
bool Empty() const;
/// get capacity of array
int Capacity() const;
/// test if an element exists
bool Contains(const KEY& key) const;
/// read/write access single element by key
VALUE& operator[](const KEY& key);
/// read-only access single element by key
const VALUE& operator[](const KEY& key) const;
/// increase capacity to hold at least numElements more elements
void Reserve(int numElements);
/// trim capacity to size (this involves a re-alloc)
void Trim();
/// clear the array (deletes elements, keeps capacity)
void Clear();
/// add-copy new element
void Add(const KEY& key, const VALUE& value);
/// add-move element
void Add(KEY&& key, VALUE&& value);
/// find value index
int FindValueIndex(const KEY& key);
/// read-access value at index
const VALUE& ValueAtIndex(int valueIndex) const;
/// read-write-access value at index
VALUE& ValueAtIndex(int valueIndex);
/// erase element by key, reasonably fast, but destroys value ordering
void EraseSwap(const KEY& key);
/// erase element by key, keep value order (involves moving values around)
void Erase(const KEY& key);
/// C++ conform begin, MAY RETURN nullptr!
VALUE* begin();
/// C++ conform begin, MAY RETURN nullptr!
const VALUE* begin() const;
/// C++ conform end, MAY RETURN nullptr!
VALUE* end();
/// C++ conform end, MAY RETURN nullptr!
VALUE* end() const;
private:
Map<KEY, int> indexMap; // maps keys to indices into value array
Array<VALUE> valueArray;
};
//------------------------------------------------------------------------------
template<class KEY, class VALUE>
ArrayMap<KEY, VALUE>::ArrayMap() {
// empty
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE>
ArrayMap<KEY, VALUE>::ArrayMap(const ArrayMap& rhs) :
indexMap(rhs.indexMap),
valueArray(rhs.valueArray) {
// empty
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE>
ArrayMap<KEY, VALUE>::ArrayMap(ArrayMap&& rhs) :
indexMap(std::move(rhs.indexMap)),
valueArray(std::move(rhs.valueArray)) {
// empty
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
ArrayMap<KEY, VALUE>::operator=(const ArrayMap& rhs) {
if (&rhs != this) {
this->indexMap = rhs.indexMap;
this->valueArray = rhs.valueArray;
}
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
ArrayMap<KEY, VALUE>::operator=(ArrayMap&& rhs) {
if (&rhs != this) {
this->indexMap = std::move(rhs.indexMap);
this->valueArray = std::move(rhs.valueArray);
}
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
ArrayMap<KEY, VALUE>::SetAllocStrategy(int minGrow, int maxGrow) {
this->indexMap.SetAllocStrategy(minGrow, maxGrow);
this->valueArray.SetAllocStrategy(minGrow, maxGrow);
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> int
ArrayMap<KEY, VALUE>::GetMinGrow() const {
return this->valueArray.GetMinGrow();
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> int
ArrayMap<KEY, VALUE>::GetMaxGrow() const {
return this->valueArray.GetMaxGrow();
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> int
ArrayMap<KEY, VALUE>::Size() const {
return this->valueArray.Size();
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> bool
ArrayMap<KEY, VALUE>::Empty() const {
return this->valueArray.Empty();
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> int
ArrayMap<KEY, VALUE>::Capacity() const {
return this->valueArray.Capacity();
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> bool
ArrayMap<KEY, VALUE>::Contains(const KEY& key) const {
return this->indexMap.Contains(key);
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> VALUE&
ArrayMap<KEY, VALUE>::operator[](const KEY& key) {
return this->valueArray[this->indexMap[key]];
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> const VALUE&
ArrayMap<KEY, VALUE>::operator[](const KEY& key) const {
return this->valueArray[this->indexMap[key]];
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
ArrayMap<KEY, VALUE>::Reserve(int numElements) {
this->indexMap.Reserve(numElements);
this->valueArray.Reserve(numElements);
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
ArrayMap<KEY, VALUE>::Trim() {
this->indexMap.Trim();
this->valueArray.Trim();
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
ArrayMap<KEY, VALUE>::Clear() {
this->indexMap.Clear();
this->valueArray.Clear();
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
ArrayMap<KEY, VALUE>::Add(const KEY& key, const VALUE& value) {
this->valueArray.Add(value);
this->indexMap.Add(key, this->valueArray.Size() - 1);
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
ArrayMap<KEY, VALUE>::Add(KEY&& key, VALUE&& value) {
this->valueArray.Add(std::move(value));
this->indexMap.Add(std::move(key), this->valueArray.Size() - 1);
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> int
ArrayMap<KEY, VALUE>::FindValueIndex(const KEY& key) {
return this->indexMap[key];
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> const VALUE&
ArrayMap<KEY, VALUE>::ValueAtIndex(int valueIndex) const {
return this->valueArray[valueIndex];
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> VALUE&
ArrayMap<KEY, VALUE>::ValueAtIndex(int valueIndex) {
return this->valueArray[valueIndex];
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
ArrayMap<KEY, VALUE>::EraseSwap(const KEY& key) {
const int mapIndex = this->indexMap.FindIndex(key);
const int valueIndex = this->indexMap.ValueAtIndex(mapIndex);
this->indexMap.EraseIndex(mapIndex);
this->valueArray.EraseSwapBack(valueIndex);
// fix-up indices
const int swappedIndex = this->valueArray.Size();
if (valueIndex != swappedIndex) {
for (auto& elm : this->indexMap) {
if (swappedIndex == elm.value) {
elm.value = valueIndex;
break;
}
}
}
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
ArrayMap<KEY, VALUE>::Erase(const KEY& key) {
const int mapIndex = this->indexMap.FindIndex(key);
const int valueIndex = this->indexMap.ValueAtIndex(mapIndex);
this->indexMap.EraseIndex(mapIndex);
this->valueArray.Erase(valueIndex);
// fix up indices
for (auto& elm : this->indexMap) {
if (elm.value > valueIndex) {
elm.value--;
}
}
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> VALUE*
ArrayMap<KEY, VALUE>::begin() {
return this->valueArray.begin();
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> const VALUE*
ArrayMap<KEY, VALUE>::begin() const {
return this->valueArray.begin();
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> VALUE*
ArrayMap<KEY, VALUE>::end() {
return this->valueArray.end();
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> VALUE*
ArrayMap<KEY, VALUE>::end() const {
return this->valueArray.end();
}
} // namespace Oryol