-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorderedmap.h
633 lines (506 loc) · 15.1 KB
/
orderedmap.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
// Adapted from https://github.com/mandeepsandhu/qt-ordered-map
#ifndef ORDEREDMAP_H
#define ORDEREDMAP_H
#include <list>
#include <QtGlobal>
#include <QHash>
#include <QList>
#include <QPair>
#ifdef Q_COMPILER_INITIALIZER_LISTS
#include <initializer_list>
#endif
template <typename Key> inline bool oMHashEqualToKey(const Key &key1, const Key &key2)
{
// Key type must provide '==' operator
return key1 == key2;
}
template <typename Ptr> inline bool oMHashEqualToKey(Ptr *key1, Ptr *key2)
{
Q_ASSERT(sizeof(quintptr) == sizeof(Ptr *));
return quintptr(key1) == quintptr(key2);
}
template <typename Ptr> inline bool oMHashEqualToKey(const Ptr *key1, const Ptr *key2)
{
Q_ASSERT(sizeof(quintptr) == sizeof(const Ptr *));
return quintptr(key1) == quintptr(key2);
}
template <typename Key, typename Value>
class OrderedMap
{
class OMHash;
typedef typename std::list<Key>::iterator QllIterator;
typedef typename std::list<Key>::const_iterator QllConstIterator;
typedef QPair<Value, QllIterator> OMHashValue;
typedef typename OMHash::iterator OMHashIterator;
typedef typename OMHash::const_iterator OMHashConstIterator;
public:
class iterator;
class const_iterator;
typedef typename OrderedMap<Key, Value>::iterator Iterator;
typedef typename OrderedMap<Key, Value>::const_iterator ConstIterator;
explicit OrderedMap();
#ifdef Q_COMPILER_INITIALIZER_LISTS
OrderedMap(std::initializer_list<std::pair<Key,Value> > list);
#endif
OrderedMap(const OrderedMap<Key, Value>& other);
#if (QT_VERSION >= 0x050200)
OrderedMap(OrderedMap<Key, Value>&& other);
#endif
void clear();
bool contains(const Key &key) const;
int count() const;
bool empty() const;
iterator insert(const Key &key, const Value &value);
bool isEmpty() const;
QList<Key> keys() const;
int remove(const Key &key);
int size() const;
Value take(const Key &key);
Value value(const Key &key) const;
Value value(const Key &key, const Value &defaultValue) const;
QList<Value> values() const;
OrderedMap<Key, Value> & operator=(const OrderedMap<Key, Value>& other);
#if (QT_VERSION >= 0x050200)
OrderedMap<Key, Value> & operator=(OrderedMap<Key, Value>&& other);
#endif
bool operator==(const OrderedMap<Key, Value> &other) const;
bool operator!=(const OrderedMap<Key, Value> &other) const;
Value& operator[](const Key &key);
const Value operator[](const Key &key) const;
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
iterator erase(iterator pos);
iterator find(const Key& key);
const_iterator find(const Key& key) const;
class const_iterator;
class iterator
{
QllIterator qllIter;
OMHash *data;
friend class const_iterator;
friend class OrderedMap;
public:
iterator() : data(NULL) {}
iterator(const QllIterator &qllIter, OMHash *data) :
qllIter(qllIter), data(data) {}
const Key & key() const
{
return *qllIter;
}
Value & value() const
{
OMHashIterator hit = data->find(*qllIter);
OMHashValue &pair = hit.value();
return pair.first;
}
Value & operator*() const
{
return value();
}
iterator operator+(int i) const
{
QllIterator q = qllIter;
q += i;
return iterator(q, data);
}
iterator operator-(int i) const
{
return operator +(- i);
}
iterator& operator+=(int i)
{
qllIter += i;
return *this;
}
iterator& operator-=(int i)
{
qllIter -= i;
return *this;
}
iterator& operator++()
{
++qllIter;
return *this;
}
iterator operator++(int)
{
iterator it = *this;
qllIter++;
return it;
}
iterator operator--()
{
--qllIter;
return *this;
}
iterator operator--(int)
{
iterator it = *this;
qllIter--;
return it;
}
bool operator ==(const iterator &other) const
{
return (qllIter == other.qllIter);
}
bool operator !=(const iterator &other) const
{
return (qllIter != other.qllIter);
}
};
class const_iterator
{
QllConstIterator qllConstIter;
const OMHash *data;
public:
const_iterator() : data(NULL) {}
const_iterator(const iterator &i) :
qllConstIter(i.qllIter), data(i.data) {}
const_iterator(const QllConstIterator &qllConstIter, const OMHash* data) :
qllConstIter(qllConstIter), data(data) {}
const Key & key() const
{
return *qllConstIter;
}
const Value & value() const
{
OMHashConstIterator hit = data->find(*qllConstIter);
const OMHashValue &pair = hit.value();
return pair.first;
}
const Value & operator*() const
{
return value();
}
const_iterator operator+(int i) const
{
QllConstIterator q = qllConstIter;
q += i;
return const_iterator(q, data);
}
const_iterator operator-(int i) const
{
return operator +(- i);
}
const_iterator& operator+=(int i)
{
qllConstIter += i;
return *this;
}
const_iterator& operator-=(int i)
{
qllConstIter -= i;
return *this;
}
const_iterator& operator++()
{
++qllConstIter;
return *this;
}
const_iterator operator++(int)
{
const_iterator it = *this;
qllConstIter++;
return it;
}
const_iterator operator--()
{
--qllConstIter;
return *this;
}
const_iterator operator--(int)
{
const_iterator it = *this;
qllConstIter--;
return it;
}
bool operator ==(const const_iterator &other) const
{
return (qllConstIter == other.qllConstIter);
}
bool operator !=(const const_iterator &other) const
{
return (qllConstIter != other.qllConstIter);
}
};
private:
class OMHash : public QHash<Key, OMHashValue >
{
public:
bool operator == (const OMHash &other) const
{
if (size() != other.size()) {
return false;
}
if (QHash<Key, OMHashValue >::operator ==(other)) {
return true;
}
typename QHash<Key, OMHashValue >::const_iterator it1 = this->constBegin();
typename QHash<Key, OMHashValue >::const_iterator it2 = other.constBegin();
while(it1 != this->end()) {
OMHashValue v1 = it1.value();
OMHashValue v2 = it2.value();
if ((v1.first != v2.first) || !oMHashEqualToKey<Key>(it1.key(), it2.key())) {
return false;
}
++it1;
++it2;
}
return true;
}
};
private:
void copy(const OrderedMap<Key, Value> &other);
OMHash data;
std::list<Key> insertOrder;
};
template <typename Key, typename Value>
OrderedMap<Key, Value>::OrderedMap() {}
#ifdef Q_COMPILER_INITIALIZER_LISTS
template<typename Key, typename Value>
OrderedMap<Key, Value>::OrderedMap(std::initializer_list<std::pair<Key, Value> > list)
{
typedef typename std::initializer_list<std::pair<Key,Value> >::const_iterator const_initlist_iter;
for (const_initlist_iter it = list.begin(); it != list.end(); ++it)
insert(it->first, it->second);
}
#endif
template <typename Key, typename Value>
OrderedMap<Key, Value>::OrderedMap(const OrderedMap<Key, Value>& other)
{
copy(other);
}
#if (QT_VERSION >= 0x050200)
template <typename Key, typename Value>
OrderedMap<Key, Value>::OrderedMap(OrderedMap<Key, Value>&& other)
{
data = std::move(other.data);
insertOrder = std::move(other.insertOrder);
}
#endif
template <typename Key, typename Value>
void OrderedMap<Key, Value>::clear()
{
data.clear();
insertOrder.clear();
}
template <typename Key, typename Value>
bool OrderedMap<Key, Value>::contains(const Key &key) const
{
return data.contains(key);
}
template <typename Key, typename Value>
int OrderedMap<Key, Value>::count() const
{
return data.count();
}
template <typename Key, typename Value>
bool OrderedMap<Key, Value>::empty() const
{
return data.empty();
}
template <typename Key, typename Value>
typename OrderedMap<Key, Value>::iterator OrderedMap<Key, Value>::insert(const Key &key, const Value &value)
{
OMHashIterator it = data.find(key);
if (it == data.end()) {
// New key
QllIterator ioIter = insertOrder.insert(insertOrder.end(), key);
OMHashValue pair(value, ioIter);
data.insert(key, pair);
return iterator(ioIter, &data);
}
OMHashValue pair = it.value();
// remove old reference
insertOrder.erase(pair.second);
// Add new reference
QllIterator ioIter = insertOrder.insert(insertOrder.end(), key);
pair.first = value;
pair.second = ioIter;
data.insert(key, pair);
return iterator(ioIter, &data);
}
template <typename Key, typename Value>
bool OrderedMap<Key, Value>::isEmpty() const
{
return data.isEmpty();
}
template<typename Key, typename Value>
QList<Key> OrderedMap<Key, Value>::keys() const
{
return QList<Key>(insertOrder.begin(), insertOrder.end());
}
template<typename Key, typename Value>
int OrderedMap<Key, Value>::remove(const Key &key)
{
OMHashIterator it = data.find(key);
if (it == data.end()) {
return 0;
}
OMHashValue pair = it.value();
insertOrder.erase(pair.second);
data.erase(it);
return 1;
}
template<typename Key, typename Value>
int OrderedMap<Key, Value>::size() const
{
return data.size();
}
template<typename Key, typename Value>
void OrderedMap<Key, Value>::copy(const OrderedMap<Key, Value> &other)
{
/* Since I'm storing iterators of QLinkedList, I simply cannot make
* a trivial copy of the linked list. This is a limitation due to implicit
* sharing used in Qt containers, due to which iterator active on one
* QLL can change the data of another QLL even after creating a copy.
*
* Because of this, the old iterators have to be invalidated and new ones
* have to be generated.
*/
insertOrder.clear();
// Copy hash
data = other.data;
QllConstIterator cit = other.insertOrder.begin();
for (; cit != other.insertOrder.end(); ++cit) {
Key key = *cit;
QllIterator ioIter = insertOrder.insert(insertOrder.end(), key);
OMHashIterator it = data.find(key);
(*it).second = ioIter;
}
}
template<typename Key, typename Value>
Value OrderedMap<Key, Value>::take(const Key &key)
{
OMHashIterator it = data.find(key);
if (it == data.end()) {
return Value();
}
OMHashValue pair = it.value();
insertOrder.erase(pair.second);
data.erase(it);
return pair.first;
}
template <typename Key, typename Value>
Value OrderedMap<Key, Value>::value(const Key &key) const
{
return data.value(key).first;
}
template <typename Key, typename Value>
Value OrderedMap<Key, Value>::value(const Key &key, const Value &defaultValue) const
{
OMHashConstIterator it = data.constFind(key);
if (it == data.end()) {
return defaultValue;
}
OMHashValue pair = it.value();
return pair.first;
}
template <typename Key, typename Value>
QList<Value> OrderedMap<Key, Value>::values() const
{
QList<Value> values;
foreach (const Key &key, insertOrder.toStdList()) {
OMHashValue v = data.value(key);
values.append(v.first);
}
return values;
}
template <typename Key, typename Value>
OrderedMap<Key, Value> & OrderedMap<Key, Value>::operator=(const OrderedMap<Key, Value>& other)
{
if (this != &other) {
copy(other);
}
return *this;
}
#if (QT_VERSION >= 0x050200)
template <typename Key, typename Value>
OrderedMap<Key, Value> & OrderedMap<Key, Value>::operator=(OrderedMap<Key, Value>&& other)
{
if (this != &other) {
data = other.data;
insertOrder = other.insertOrder;
}
return *this;
}
#endif
template <typename Key, typename Value>
bool OrderedMap<Key, Value>::operator==(const OrderedMap<Key, Value> &other) const
{
// 2 Ordered maps are equal if they have the same contents in the same order
return ((data == other.data) && (insertOrder == other.insertOrder));
}
template <typename Key, typename Value>
bool OrderedMap<Key, Value>::operator!=(const OrderedMap<Key, Value> &other) const
{
return ((data != other.data) || (insertOrder != other.insertOrder));
}
template <typename Key, typename Value>
Value& OrderedMap<Key, Value>::operator[](const Key &key)
{
OMHashIterator it = data.find(key);
if (it == data.end()) {
insert(key, Value());
it = data.find(key);
}
OMHashValue &pair = it.value();
return pair.first;
}
template <typename Key, typename Value>
const Value OrderedMap<Key, Value>::operator[](const Key &key) const
{
return value(key);
}
template <typename Key, typename Value>
typename OrderedMap<Key, Value>::iterator OrderedMap<Key, Value>::begin()
{
return iterator(insertOrder.begin(), &data);
}
template <typename Key, typename Value>
typename OrderedMap<Key, Value>::const_iterator OrderedMap<Key, Value>::begin() const
{
return const_iterator(insertOrder.begin(), &data);
}
template <typename Key, typename Value>
typename OrderedMap<Key, Value>::iterator OrderedMap<Key, Value>::end()
{
return iterator(insertOrder.end(), &data);
}
template <typename Key, typename Value>
typename OrderedMap<Key, Value>::const_iterator OrderedMap<Key, Value>::end() const
{
return const_iterator(insertOrder.end(), &data);
}
template <typename Key, typename Value>
typename OrderedMap<Key, Value>::iterator OrderedMap<Key, Value>::erase(iterator pos)
{
OMHashIterator hit = data.find(*(pos.qllIter));
if (hit == data.end()) {
return pos;
}
data.erase(hit);
QllIterator ioIter = insertOrder.erase(pos.qllIter);
return iterator(ioIter, &data);
}
template <typename Key, typename Value>
typename OrderedMap<Key, Value>::iterator OrderedMap<Key, Value>::find(const Key& key)
{
OMHashIterator hit = data.find(key);
if (hit == data.end()) {
return end();
}
return iterator(hit.value().second, &data);
}
template <typename Key, typename Value>
typename OrderedMap<Key, Value>::const_iterator OrderedMap<Key, Value>::find(const Key& key) const
{
OMHashConstIterator hit = data.find(key);
if (hit == data.end()) {
return end();
}
return const_iterator(hit.value().second, &data);
}
#endif // ORDEREDMAP_H