-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathMap.h
570 lines (504 loc) · 19.3 KB
/
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#pragma once
//------------------------------------------------------------------------------
/**
@class Oryol::Map
@ingroup Core
@brief simple key-value map similar to std::map
A key-value-pair container similar to std::map, with the following
differences:
- trying to access a non-existing element with operator[] will
trigger an assertion instead of adding it
Map is useful when the values type is small and has a cheap copy/move
operation. For big value and/or complex value types, consider using
an ArrayMap!
Allows to add multiple elements with the same key, these will be lumped
together in the internal buffer and will have adjacent indices
You can use the FindDuplicate() to check for redundant elements,
this is O(N) though.
When adding large numbers of elements, consider using the
bulk methods, these destroy the sorted order when inserting,
and sorting will happen inside EndBulk().
The Map uses a double-ended element buffer internally which
initially has spare room at the front and end. When inserting elements,
movement happens towards the end which would create less move operations
(so inserting at the front is just as fast as inserting at the end).
@see KeyValuePair, Set
*/
#include <algorithm>
#include <initializer_list>
#include "Core/Config.h"
#include "Core/Containers/elementBuffer.h"
#include "Core/Containers/KeyValuePair.h"
namespace Oryol {
template<class KEY, class VALUE> class Map {
public:
/// default constructor
Map();
/// copy constructor (truncates to actual size)
Map(const Map& rhs);
/// move constructor (same capacity and size)
Map(Map&& rhs);
/// construct from initializer list
Map(std::initializer_list<KeyValuePair<KEY,VALUE>> rhs);
/// destructor
~Map();
/// copy-assignment operator (truncates to actual size)
void operator=(const Map& rhs);
/// move-assignment operator (same capacity and size)
void operator=(Map&& 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;
/// read/write access single element
VALUE& operator[](const KEY& key);
/// read-only access single element
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();
/// test if an element exists
bool Contains(const KEY& key) const;
/// add new element
void Add(const KeyValuePair<KEY, VALUE>& kvp);
/// add new element
void Add(KeyValuePair<KEY, VALUE>&& kvp);
/// add new element
void Add(const KEY& key, const VALUE& value);
/// add new element, return false if element with key already existed
bool AddUnique(const KeyValuePair<KEY, VALUE>& kvp);
/// add new element with move-semantics, return false if element with key already existed
bool AddUnique(KeyValuePair<KEY, VALUE>&& kvp);
/// add new element, and check that it is unique
bool AddUnique(const KEY& key, const VALUE& value);
/// erase all elements matching key, does nothing if key not contained
void Erase(const KEY& key);
/// begin bulk-mode
void BeginBulk();
/// add element in bulk-mode (destroys sorting order)
void AddBulk(const KeyValuePair<KEY, VALUE>& kvp);
/// add element in bulk-mode (destroys sorting order)
void AddBulk(KeyValuePair<KEY, VALUE>&& kvp);
/// add element in bulk-mode (destroys sorting order)
void AddBulk(const KEY& key, const VALUE& value);
/// end bulk-mode (sorting happens here)
void EndBulk();
/// find the first duplicate element, or InvalidIndex if not found, this is O(N)!
int FindDuplicate(int startIndex) const;
/// find an element, returns index, or InvalidIndex
int FindIndex(const KEY& key) const;
/// erase element at index
void EraseIndex(int index);
/// get key at index
const KEY& KeyAtIndex(int index) const;
/// get value at index (read-only)
const VALUE& ValueAtIndex(int index) const;
/// get value at index (read/write)
VALUE& ValueAtIndex(int index);
/// C++ conform begin, MAY RETURN nullptr!
KeyValuePair<KEY, VALUE>* begin();
/// C++ conform begin, MAY RETURN nullptr!
const KeyValuePair<KEY, VALUE>* begin() const;
/// C++ conform end, MAY RETURN nullptr!
KeyValuePair<KEY, VALUE>* end();
/// C++ conform end, MAY RETURN nullptr!
const KeyValuePair<KEY, VALUE>* end() const;
private:
/// destroy content
void destroy();
/// copy content
void copy(const Map& rhs);
/// move content
void move(Map&& rhs);
/// reallocate with new capacity
void adjustCapacity(int newCapacity);
/// grow to make room
void grow();
_priv::elementBuffer<KeyValuePair<KEY,VALUE>> buffer;
int minGrow;
int maxGrow;
bool inBulkMode;
};
//------------------------------------------------------------------------------
template<class KEY, class VALUE>
Map<KEY, VALUE>::Map() :
minGrow(ORYOL_CONTAINER_DEFAULT_MIN_GROW),
maxGrow(ORYOL_CONTAINER_DEFAULT_MAX_GROW),
inBulkMode(false) {
// empty
}
template<class KEY, class VALUE>
Map<KEY, VALUE>::Map(std::initializer_list<KeyValuePair<KEY,VALUE>> rhs) :
minGrow(ORYOL_CONTAINER_DEFAULT_MIN_GROW),
maxGrow(ORYOL_CONTAINER_DEFAULT_MAX_GROW),
inBulkMode(false) {
this->BeginBulk();
for (const KeyValuePair<KEY,VALUE> &kvp : rhs) {
this->AddBulk(kvp);
}
this->EndBulk();
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE>
Map<KEY, VALUE>::Map(const Map& rhs) {
this->copy(rhs);
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE>
Map<KEY, VALUE>::Map(Map&& rhs) {
this->move(std::move(rhs));
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE>
Map<KEY, VALUE>::~Map() {
this->destroy();
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
Map<KEY, VALUE>::operator=(const Map& rhs) {
/// @todo: this should be optimized when rhs.size() < this->capacity()!
if (&rhs != this) {
this->destroy();
this->copy(rhs);
}
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
Map<KEY, VALUE>::operator=(Map&& rhs) {
/// @todo: this should be optimized when rhs.size() < this->capacity()!
if (&rhs != this) {
this->destroy();
this->move(std::move(rhs));
}
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
Map<KEY, VALUE>::SetAllocStrategy(int minGrow_, int maxGrow_) {
this->minGrow = minGrow_;
this->maxGrow = maxGrow_;
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> int
Map<KEY, VALUE>::GetMinGrow() const {
return this->minGrow;
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> int
Map<KEY, VALUE>::GetMaxGrow() const {
return this->maxGrow;
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> int
Map<KEY, VALUE>::Size() const {
return this->buffer.size();
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> bool
Map<KEY, VALUE>::Empty() const {
return this->buffer.size() == 0;
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> int
Map<KEY, VALUE>::Capacity() const {
return this->buffer.capacity();
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> bool
Map<KEY, VALUE>::Contains(const KEY& key) const {
o_assert_dbg(!this->inBulkMode);
return std::binary_search(this->buffer._begin(), this->buffer._end(), key);
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> VALUE&
Map<KEY, VALUE>::operator[](const KEY& key) {
o_assert_dbg(!this->inBulkMode);
o_assert_dbg(this->buffer.buf);
auto kvp = std::lower_bound(this->buffer._begin(), this->buffer._end(), key);
o_assert((kvp != this->buffer._end()) && (key == kvp->key)); // not found if this triggers
return kvp->value;
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> const VALUE&
Map<KEY, VALUE>::operator[](const KEY& key) const {
o_assert_dbg(!this->inBulkMode);
o_assert_dbg(this->buffer.buf);
auto kvp = std::lower_bound(this->buffer._begin(), this->buffer._end(), key);
o_assert_dbg((kvp != this->buffer._end()) && (key == kvp->key)); // not found if this triggers
return kvp->value;
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
Map<KEY, VALUE>::Reserve(int numElements) {
int newCapacity = this->buffer.size() + numElements;
if (newCapacity > this->buffer.capacity()) {
this->adjustCapacity(newCapacity);
}
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
Map<KEY, VALUE>::Trim() {
const int curSize = this->buffer.size();
if (curSize < this->buffer.capacity()) {
this->adjustCapacity(curSize);
}
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
Map<KEY, VALUE>::Clear() {
this->buffer.clear();
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
Map<KEY, VALUE>::Add(const KeyValuePair<KEY, VALUE>& kvp) {
o_assert_dbg(!this->inBulkMode);
if (this->buffer.spare() == 0) {
this->grow();
}
auto ptr = std::lower_bound(this->buffer._begin(), this->buffer._end(), kvp.key);
int index = int(ptr - this->buffer._begin());
this->buffer.insert(index, kvp);
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
Map<KEY, VALUE>::Add(KeyValuePair<KEY, VALUE>&& kvp) {
o_assert_dbg(!this->inBulkMode);
if (this->buffer.spare() == 0) {
this->grow();
}
auto ptr = std::lower_bound(this->buffer._begin(), this->buffer._end(), kvp.key);
int index = int(ptr - this->buffer._begin());
this->buffer.insert(index, std::move(kvp));
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
Map<KEY, VALUE>::Add(const KEY& key, const VALUE& value) {
this->Add(KeyValuePair<KEY, VALUE>(key, value));
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> bool
Map<KEY, VALUE>::AddUnique(const KeyValuePair<KEY, VALUE>& kvp) {
o_assert(!this->inBulkMode);
if (this->buffer.spare() == 0) {
this->grow();
}
auto ptr = std::lower_bound(this->buffer._begin(), this->buffer._end(), kvp.key);
if ((ptr != this->buffer._end()) && (ptr->key == kvp.key)) {
return false;
}
else {
int index = int(ptr - this->buffer._begin());
this->buffer.insert(index, kvp);
return true;
}
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> bool
Map<KEY, VALUE>::AddUnique(KeyValuePair<KEY, VALUE>&& kvp) {
o_assert(!this->inBulkMode);
if (this->buffer.spare() == 0) {
this->grow();
}
auto ptr = std::lower_bound(this->buffer._begin(), this->buffer._end(), kvp.key);
if ((ptr != this->buffer._end()) && (ptr->key == kvp.key)) {
return false;
}
else {
int index = int(ptr - this->buffer._begin());
this->buffer.insert(index, std::move(kvp));
return true;
}
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> bool
Map<KEY, VALUE>::AddUnique(const KEY& key, const VALUE& value) {
return this->AddUnique(KeyValuePair<KEY, VALUE>(key, value));
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
Map<KEY, VALUE>::Erase(const KEY& key) {
auto ptr = std::lower_bound(this->buffer._begin(), this->buffer._end(), key);
if (ptr != this->buffer._end()) {
const int index = int(ptr - this->buffer._begin());
while ((index < this->buffer.size()) && (this->buffer[index].key == key)) {
this->buffer.erase(index);
}
}
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
Map<KEY, VALUE>::BeginBulk() {
o_assert(!this->inBulkMode);
this->inBulkMode = true;
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
Map<KEY, VALUE>::AddBulk(const KeyValuePair<KEY, VALUE>& kvp) {
o_assert(this->inBulkMode);
if (this->buffer.spare() == 0) {
this->grow();
}
// keep frontSpare and backSpare balanced
if (this->buffer.frontSpare() > this->buffer.backSpare()) {
// insert at front
this->buffer.insert(0, kvp);
}
else {
// insert at back
this->buffer.insert(this->buffer.size(), kvp);
}
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
Map<KEY, VALUE>::AddBulk(KeyValuePair<KEY, VALUE>&& kvp) {
o_assert(this->inBulkMode);
if (this->buffer.spare() == 0) {
this->grow();
}
// keep frontSpare and backSpare balanced
if (this->buffer.frontSpare() > this->buffer.backSpare()) {
// insert at front
this->buffer.insert(0, std::move(kvp));
}
else {
// insert at back
this->buffer.insert(this->buffer.size(), std::move(kvp));
}
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
Map<KEY, VALUE>::AddBulk(const KEY& key, const VALUE& value) {
this->Insert(KeyValuePair<KEY, VALUE>(key, value));
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
Map<KEY, VALUE>::EndBulk() {
o_assert(this->inBulkMode);
this->inBulkMode = false;
std::sort(this->buffer._begin(), this->buffer._end());
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> int
Map<KEY, VALUE>::FindDuplicate(int startIndex) const {
o_assert(!this->inBulkMode);
const int size = this->buffer.size();
if (startIndex < size) {
for (int index = startIndex; index < (size - 1); index++) {
if (this->buffer[index].key == this->buffer[index + 1].key) {
return index;
}
}
}
return InvalidIndex;
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> int
Map<KEY, VALUE>::FindIndex(const KEY& key) const {
o_assert(!this->inBulkMode);
auto ptr = std::lower_bound(this->buffer._begin(), this->buffer._end(), key);
if ((ptr != this->buffer._end()) && (key == ptr->key)) {
return int(ptr - this->buffer._begin());
}
else {
return InvalidIndex;
}
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
Map<KEY, VALUE>::EraseIndex(int index) {
this->buffer.erase(index);
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> const KEY&
Map<KEY, VALUE>::KeyAtIndex(int index) const {
return this->buffer[index].key;
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> const VALUE&
Map<KEY, VALUE>::ValueAtIndex(int index) const {
return this->buffer[index].value;
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> VALUE&
Map<KEY, VALUE>::ValueAtIndex(int index) {
return this->buffer[index].value;
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> KeyValuePair<KEY, VALUE>*
Map<KEY, VALUE>::begin() {
return this->buffer._begin();
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> const KeyValuePair<KEY, VALUE>*
Map<KEY, VALUE>::begin() const {
return this->buffer._begin();
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> KeyValuePair<KEY, VALUE>*
Map<KEY, VALUE>::end() {
return this->buffer._end();
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> const KeyValuePair<KEY, VALUE>*
Map<KEY, VALUE>::end() const {
return this->buffer._end();
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
Map<KEY, VALUE>::destroy() {
this->minGrow = 0;
this->maxGrow = 0;
this->buffer.destroy();
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
Map<KEY, VALUE>::copy(const Map& rhs) {
this->minGrow = rhs.minGrow;
this->maxGrow = rhs.maxGrow;
this->inBulkMode = rhs.inBulkMode;
this->buffer = rhs.buffer;
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
Map<KEY, VALUE>::move(Map&& rhs) {
o_assert_dbg(!rhs.inBulkMode);
this->minGrow = rhs.minGrow;
this->maxGrow = rhs.maxGrow;
this->inBulkMode = rhs.inBulkMode;
this->buffer = std::move(rhs.buffer);
// NOTE: don't reset minGrow/maxGrow, rhs is empty, but still a valid object!
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
Map<KEY, VALUE>::adjustCapacity(int newCapacity) {
// have a balanced front and back spare
int frontSpare = (newCapacity - this->buffer.size()) >> 1;
o_assert_dbg(frontSpare >= 0);
this->buffer.alloc(newCapacity, frontSpare);
}
//------------------------------------------------------------------------------
template<class KEY, class VALUE> void
Map<KEY, VALUE>::grow() {
const int curCapacity = this->buffer.capacity();
int growBy = curCapacity >> 1;
if (growBy < minGrow) {
growBy = minGrow;
}
else if (growBy > maxGrow) {
growBy = maxGrow;
}
o_assert_dbg(growBy > 0);
int newCapacity = curCapacity + growBy;
this->adjustCapacity(newCapacity);
}
} // namespace Oryol