-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_map.h
More file actions
786 lines (657 loc) · 27 KB
/
Copy pathhash_map.h
File metadata and controls
786 lines (657 loc) · 27 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
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
// Короче это кукушка на двух массивах (пж не путать с кукушкой на одном массиве - это принципиально идейно два разных алгоритма не похожих друг на друга)
#pragma once
#include <cassert>
#include <list>
#include <memory>
#include <stdexcept>
#include <vector>
const size_t INITIAL_CAPACITY_1 = 40;
const size_t INITIAL_CAPACITY_2 = 41;
const double LOAD_FACTOR = 0.33;
template<typename KeyType, typename ValueType, typename Hasher = std::hash<KeyType>>
class HashMap {
private:
std::vector<std::unique_ptr<std::pair<const KeyType, ValueType>>> array_1_, array_2_;
std::list<std::unique_ptr<std::pair<const KeyType, ValueType>>> bad_items_;
size_t item_count_;
Hasher hasher_;
void RebuildIfNeeded();
public:
HashMap();
HashMap(Hasher hasher);
template<typename IteratorType>
HashMap(IteratorType begin, IteratorType end);
template<typename IteratorType>
HashMap(IteratorType begin, IteratorType end, Hasher hasher);
HashMap(std::initializer_list<std::pair<KeyType, ValueType>> given_items);
HashMap(std::initializer_list<std::pair<KeyType, ValueType>> given_items, Hasher hasher);
~HashMap() = default;
HashMap(const HashMap<KeyType, ValueType, Hasher> &other);
HashMap &operator=(const HashMap<KeyType, ValueType, Hasher> &other);
size_t size() const;
size_t bad_size() const;
bool empty() const;
Hasher hash_function() const;
void insert(std::pair<KeyType, ValueType> new_pair);
void erase(KeyType key);
class iterator {
private:
friend class HashMap<KeyType, ValueType, Hasher>;
enum IteratingState {
FIRST_ARRAY,
SECOND_ARRAY,
BAD_ITEMS
};
HashMap<KeyType, ValueType, Hasher> *original_map_;
IteratingState iteration_stage_;
size_t index_;
typename std::list<std::unique_ptr<std::pair<const KeyType, ValueType>>>::iterator bad_items_iterator_;
void move_to_nearest_item();
void move_to_next_item();
public:
iterator();
iterator(HashMap<KeyType, ValueType, Hasher> *original_map, IteratingState iteration_stage, size_t index,
typename std::list<std::unique_ptr<std::pair<const KeyType, ValueType>>>::iterator bad_items_iterator);
std::pair<const KeyType, ValueType> &operator*() const;
std::pair<const KeyType, ValueType> *operator->() const;
iterator &operator++();
iterator operator++(int);
bool operator==(const iterator &other) const;
bool operator!=(const iterator &other) const;
};
class const_iterator {
private:
friend class HashMap<KeyType, ValueType, Hasher>;
enum IteratingState {
FIRST_ARRAY,
SECOND_ARRAY,
BAD_ITEMS
};
const HashMap<KeyType, ValueType, Hasher> *original_map_;
IteratingState iteration_stage_;
size_t index_;
typename std::list<std::unique_ptr<std::pair<const KeyType, ValueType>>>::const_iterator bad_items_iterator_;
void move_to_nearest_item();
void move_to_next_item();
public:
const_iterator();
const_iterator(const HashMap<KeyType, ValueType, Hasher> *original_map, IteratingState iteration_stage,
size_t index,
typename std::list<std::unique_ptr<std::pair<const KeyType, ValueType>>>::const_iterator bad_items_iterator);
const std::pair<const KeyType, ValueType> &operator*() const;
const std::pair<const KeyType, ValueType> *operator->() const;
const_iterator &operator++();
const_iterator operator++(int);
bool operator==(const const_iterator &other) const;
bool operator!=(const const_iterator &other) const;
};
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
iterator find(KeyType key);
const_iterator find(KeyType key) const;
ValueType &operator[](KeyType key);
const ValueType &at(KeyType key) const;
void clear();
};
///////////// HashMap
template<typename KeyType, typename ValueType, typename Hasher>
void HashMap<KeyType, ValueType, Hasher>::RebuildIfNeeded() {
if ((double) (array_1_.size() + array_2_.size()) * LOAD_FACTOR > item_count_) {
return;
}
std::vector<std::unique_ptr<std::pair<const KeyType, ValueType>>> buffer;
buffer.reserve(item_count_);
for (auto &item: array_1_) {
if (item.get() != nullptr) {
buffer.push_back(std::move(item));
}
}
for (auto &item: array_2_) {
if (item.get() != nullptr) {
buffer.push_back(std::move(item));
}
}
for (auto &item: bad_items_) {
if (item.get() != nullptr) {
buffer.push_back(std::move(item));
}
}
array_1_.resize(array_1_.size() * 2 + 1);
array_2_.resize(array_2_.size() * 2 + 1);
bad_items_.clear();
for (auto &item: array_1_) {
item = nullptr;
}
for (auto &item: array_2_) {
item = nullptr;
}
item_count_ = 0;
for (const auto &item: buffer) {
insert(std::pair<KeyType, ValueType>(item->first, item->second));
}
}
template<typename KeyType, typename ValueType, typename Hasher>
HashMap<KeyType, ValueType, Hasher>::HashMap() : array_1_(INITIAL_CAPACITY_1), array_2_(INITIAL_CAPACITY_2),
bad_items_(), item_count_(0), hasher_() {
}
template<typename KeyType, typename ValueType, typename Hasher>
HashMap<KeyType, ValueType, Hasher>::HashMap(Hasher hasher) : array_1_(INITIAL_CAPACITY_1),
array_2_(INITIAL_CAPACITY_2),
bad_items_(), item_count_(0), hasher_(hasher) {
}
template<typename KeyType, typename ValueType, typename Hasher>
template<typename IteratorType>
HashMap<KeyType, ValueType, Hasher>::HashMap(IteratorType begin, IteratorType end)
: array_1_(INITIAL_CAPACITY_1), array_2_(INITIAL_CAPACITY_2), bad_items_(), item_count_(0), hasher_() {
for (IteratorType iterator = begin; iterator != end; ++iterator) {
insert(std::pair<KeyType, ValueType>(iterator->first, iterator->second));
}
}
template<typename KeyType, typename ValueType, typename Hasher>
template<typename IteratorType>
HashMap<KeyType, ValueType, Hasher>::HashMap(IteratorType begin, IteratorType end, Hasher hasher)
: array_1_(INITIAL_CAPACITY_1), array_2_(INITIAL_CAPACITY_2), bad_items_(), item_count_(0), hasher_(hasher) {
for (IteratorType iterator = begin; iterator != end; ++iterator) {
insert(std::pair<KeyType, ValueType>(iterator->first, iterator->second));
}
}
template<typename KeyType, typename ValueType, typename Hasher>
HashMap<KeyType, ValueType, Hasher>::HashMap(std::initializer_list<std::pair<KeyType, ValueType>> given_items)
: array_1_(INITIAL_CAPACITY_1), array_2_(INITIAL_CAPACITY_2), bad_items_(), item_count_(0), hasher_() {
for (const auto &item: given_items) {
insert(std::pair<KeyType, ValueType>(item.first, item.second));
}
}
template<typename KeyType, typename ValueType, typename Hasher>
HashMap<KeyType, ValueType, Hasher>::HashMap(std::initializer_list<std::pair<KeyType, ValueType>> given_items,
Hasher hasher) : array_1_(INITIAL_CAPACITY_1),
array_2_(INITIAL_CAPACITY_2), bad_items_(),
item_count_(0), hasher_(hasher) {
for (const auto &item: given_items) {
insert(std::pair<KeyType, ValueType>(item.first, item.second));
}
}
template<typename KeyType, typename ValueType, typename Hasher>
size_t HashMap<KeyType, ValueType, Hasher>::size() const {
return item_count_;
}
template<typename KeyType, typename ValueType, typename Hasher>
size_t HashMap<KeyType, ValueType, Hasher>::bad_size() const {
return bad_items_.size();
}
template<typename KeyType, typename ValueType, typename Hasher>
bool HashMap<KeyType, ValueType, Hasher>::empty() const {
return size() == 0;
}
template<typename KeyType, typename ValueType, typename Hasher>
Hasher HashMap<KeyType, ValueType, Hasher>::hash_function() const {
return hasher_;
}
template<typename KeyType, typename ValueType, typename Hasher>
void HashMap<KeyType, ValueType, Hasher>::insert(std::pair<KeyType, ValueType> new_pair) {
if (find(new_pair.first) != end()) {
return;
}
RebuildIfNeeded();
size_t initial_hash_1 = hasher_(new_pair.first) % array_1_.size();
size_t initial_hash_2 = hasher_(new_pair.first) % array_2_.size();
if (array_1_[initial_hash_1] == nullptr) {
++item_count_;
array_1_[initial_hash_1] = std::make_unique<std::pair<const KeyType, ValueType>>(new_pair.first, new_pair.second);
return;
}
if (array_2_[initial_hash_2] == nullptr) {
++item_count_;
array_2_[initial_hash_2] = std::make_unique<std::pair<const KeyType, ValueType>>(new_pair.first, new_pair.second);
return;
}
auto buffer = std::make_unique<std::pair<const KeyType, ValueType>>(new_pair.first, new_pair.second);
auto current_hash_1 = initial_hash_1;
auto current_hash_2 = hasher_(array_1_[initial_hash_1]->first) % array_2_.size();
std::swap(buffer, array_1_[initial_hash_1]);
size_t iteration_count = 0;
while (!(current_hash_1 == initial_hash_1 && current_hash_2 == initial_hash_2)) {
if (array_1_[current_hash_1] == nullptr) {
std::swap(array_1_[current_hash_1], buffer);
++item_count_;
return;
}
if (array_2_[current_hash_2] == nullptr) {
std::swap(array_2_[current_hash_2], buffer);
++item_count_;
return;
}
if (iteration_count % 2 == 0) {
current_hash_1 = hasher_(array_2_[current_hash_2]->first) % array_1_.size();
std::swap(buffer, array_2_[current_hash_2]);
} else {
current_hash_2 = hasher_(array_1_[current_hash_1]->first) % array_2_.size();
std::swap(buffer, array_1_[current_hash_1]);
}
++iteration_count;
}
if (current_hash_1 == initial_hash_1 && current_hash_2 == initial_hash_2) {
bad_items_.push_front(std::move(buffer));
++item_count_;
return;
}
assert(false); // we shouldn't be here
}
template<typename KeyType, typename ValueType, typename Hasher>
void HashMap<KeyType, ValueType, Hasher>::erase(KeyType key) {
size_t hash1 = hasher_(key) % array_1_.size();
size_t hash2 = hasher_(key) % array_2_.size();
if (array_1_[hash1] && array_1_[hash1]->first == key) {
array_1_[hash1] = nullptr;
--item_count_;
return;
}
if (array_2_[hash2] && array_2_[hash2]->first == key) {
array_2_[hash2] = nullptr;
--item_count_;
return;
}
for (auto item_iterator = bad_items_.begin(); item_iterator != bad_items_.end(); ++item_iterator) {
if ((*item_iterator)->first == key) {
*item_iterator = nullptr;
bad_items_.erase(item_iterator);
--item_count_;
return;
}
}
}
template<typename KeyType, typename ValueType, typename Hasher>
typename HashMap<KeyType, ValueType, Hasher>::iterator HashMap<KeyType, ValueType, Hasher>::begin() {
return HashMap::iterator(this, iterator::IteratingState::FIRST_ARRAY, 0, bad_items_.begin());
}
template<typename KeyType, typename ValueType, typename Hasher>
typename HashMap<KeyType, ValueType, Hasher>::const_iterator HashMap<KeyType, ValueType, Hasher>::begin() const {
return HashMap::const_iterator(this, const_iterator::IteratingState::FIRST_ARRAY, 0, bad_items_.begin());
}
template<typename KeyType, typename ValueType, typename Hasher>
typename HashMap<KeyType, ValueType, Hasher>::iterator HashMap<KeyType, ValueType, Hasher>::end() {
return HashMap::iterator(this, iterator::IteratingState::BAD_ITEMS, 0, bad_items_.end());
}
template<typename KeyType, typename ValueType, typename Hasher>
typename HashMap<KeyType, ValueType, Hasher>::const_iterator HashMap<KeyType, ValueType, Hasher>::end() const {
return HashMap::const_iterator(this, const_iterator::IteratingState::BAD_ITEMS, 0, bad_items_.end());
}
template<typename KeyType, typename ValueType, typename Hasher>
typename HashMap<KeyType, ValueType, Hasher>::iterator HashMap<KeyType, ValueType, Hasher>::find(KeyType key) {
size_t hash1 = hasher_(key) % array_1_.size();
size_t hash2 = hasher_(key) % array_2_.size();
if (array_1_[hash1] && array_1_[hash1]->first == key) {
return iterator(this, iterator::IteratingState::FIRST_ARRAY, hash1, bad_items_.begin());
}
if (array_2_[hash2] && array_2_[hash2]->first == key) {
return iterator(this, iterator::IteratingState::SECOND_ARRAY, hash2, bad_items_.begin());
}
for (auto item_iterator = bad_items_.begin(); item_iterator != bad_items_.end(); ++item_iterator) {
if ((*item_iterator)->first == key) {
return iterator(this, iterator::IteratingState::BAD_ITEMS, 0, item_iterator);
}
}
return end();
}
template<typename KeyType, typename ValueType, typename Hasher>
typename HashMap<KeyType, ValueType, Hasher>::const_iterator
HashMap<KeyType, ValueType, Hasher>::find(KeyType key) const {
size_t hash1 = hasher_(key) % array_1_.size();
size_t hash2 = hasher_(key) % array_2_.size();
if (array_1_[hash1] && array_1_[hash1]->first == key) {
return const_iterator(this, const_iterator::IteratingState::FIRST_ARRAY, hash1, bad_items_.begin());
}
if (array_2_[hash2] && array_2_[hash2]->first == key) {
return const_iterator(this, const_iterator::IteratingState::SECOND_ARRAY, hash2, bad_items_.begin());
}
for (auto item_iterator = bad_items_.begin(); item_iterator != bad_items_.end(); ++item_iterator) {
if ((*item_iterator)->first == key) {
return const_iterator(this, const_iterator::IteratingState::BAD_ITEMS, 0, item_iterator);
}
}
return end();
}
template<typename KeyType, typename ValueType, typename Hasher>
ValueType &HashMap<KeyType, ValueType, Hasher>::operator[](KeyType key) {
size_t hash1 = hasher_(key) % array_1_.size();
size_t hash2 = hasher_(key) % array_2_.size();
if (array_1_[hash1] && array_1_[hash1]->first == key) {
return array_1_[hash1]->second;
}
if (array_2_[hash2] && array_2_[hash2]->first == key) {
return array_2_[hash2]->second;
}
for (auto item_iterator = bad_items_.begin(); item_iterator != bad_items_.end(); ++item_iterator) {
if ((*item_iterator)->first == key) {
return (*item_iterator)->second;
}
}
insert(std::pair<KeyType, ValueType>(key, ValueType()));
return operator[](key);
}
template<typename KeyType, typename ValueType, typename Hasher>
const ValueType &HashMap<KeyType, ValueType, Hasher>::at(KeyType key) const {
size_t hash1 = hasher_(key) % array_1_.size();
size_t hash2 = hasher_(key) % array_2_.size();
if (array_1_[hash1] && array_1_[hash1]->first == key) {
return array_1_[hash1]->second;
}
if (array_2_[hash2] && array_2_[hash2]->first == key) {
return array_2_[hash2]->second;
}
for (auto item_iterator = bad_items_.begin(); item_iterator != bad_items_.end(); ++item_iterator) {
if ((*item_iterator)->first == key) {
return (*item_iterator)->second;
}
}
throw std::out_of_range("error: map does not contain key");
}
template<typename KeyType, typename ValueType, typename Hasher>
void HashMap<KeyType, ValueType, Hasher>::clear() {
array_1_.clear();
array_2_.clear();
array_1_.resize(INITIAL_CAPACITY_1);
array_2_.resize(INITIAL_CAPACITY_2);
bad_items_.clear();
item_count_ = 0;
}
template<typename KeyType, typename ValueType, typename Hasher>
HashMap<KeyType, ValueType, Hasher>::HashMap(const HashMap<KeyType, ValueType, Hasher> &other)
: array_1_(other.array_1_.size()), array_2_(other.array_2_.size()), bad_items_(), item_count_(0),
hasher_(other.hasher_) {
for (const auto &item: other) {
insert(std::pair<KeyType, ValueType>(item.first, item.second));
}
}
template<typename KeyType, typename ValueType, typename Hasher>
HashMap<KeyType, ValueType, Hasher> &
HashMap<KeyType, ValueType, Hasher>::operator=(const HashMap<KeyType, ValueType, Hasher> &other) {
if (this == &other) {
return *this;
}
clear();
array_1_.resize(other.array_1_.size());
array_2_.resize(other.array_2_.size());
item_count_ = other.item_count_;
hasher_ = other.hasher_;
for (size_t i = 0; i < array_1_.size(); ++i) {
if (other.array_1_[i] != nullptr) {
array_1_[i] = std::make_unique<std::pair<const KeyType, ValueType>>(other.array_1_[i]->first,
other.array_1_[i]->second);
}
}
for (size_t i = 0; i < array_2_.size(); ++i) {
if (other.array_2_[i] != nullptr) {
array_2_[i] = std::make_unique<std::pair<const KeyType, ValueType>>(other.array_2_[i]->first,
other.array_2_[i]->second);
}
}
for (const auto &item : other.bad_items_) {
bad_items_.push_back(std::make_unique<std::pair<const KeyType, ValueType>>(item->first, item->second));
}
return *this;
}
/////////////////////////////////////////// iterator
template<typename KeyType, typename ValueType, typename Hasher>
HashMap<KeyType, ValueType, Hasher>::iterator::iterator() : original_map_(nullptr), iteration_stage_(FIRST_ARRAY),
index_(0), bad_items_iterator_() {
}
template<typename KeyType, typename ValueType, typename Hasher>
void HashMap<KeyType, ValueType, Hasher>::iterator::move_to_next_item() {
switch (iteration_stage_) {
case IteratingState::FIRST_ARRAY:
++index_;
while (index_ < original_map_->array_1_.size() && original_map_->array_1_[index_] == nullptr) {
++index_;
}
if (index_ >= original_map_->array_1_.size() || original_map_->array_1_[index_] == nullptr) {
index_ = 0;
iteration_stage_ = IteratingState::SECOND_ARRAY;
if (original_map_->array_2_[0] == nullptr) {
move_to_nearest_item();
}
}
break;
case IteratingState::SECOND_ARRAY:
++index_;
while (index_ < original_map_->array_2_.size() && original_map_->array_2_[index_] == nullptr) {
++index_;
}
if (index_ >= original_map_->array_2_.size() || original_map_->array_2_[index_] == nullptr) {
index_ = 0;
iteration_stage_ = IteratingState::BAD_ITEMS;
bad_items_iterator_ = original_map_->bad_items_.begin();
}
break;
case IteratingState::BAD_ITEMS:
++bad_items_iterator_;
break;
default:
assert(false); // we shouldn't be here
}
}
template<typename KeyType, typename ValueType, typename Hasher>
void HashMap<KeyType, ValueType, Hasher>::iterator::move_to_nearest_item() {
switch (iteration_stage_) {
case IteratingState::FIRST_ARRAY:
if (original_map_->array_1_[index_] == nullptr) {
move_to_next_item();
}
break;
case IteratingState::SECOND_ARRAY:
if (original_map_->array_2_[index_] == nullptr) {
move_to_next_item();
}
break;
case IteratingState::BAD_ITEMS:
break;
default:
assert(false); // we shouldn't be here
}
}
template<typename KeyType, typename ValueType, typename Hasher>
HashMap<KeyType, ValueType, Hasher>::iterator::iterator(
HashMap<KeyType, ValueType, Hasher> *original_map,
IteratingState iteration_stage, size_t index,
typename std::list<std::unique_ptr<std::pair<const KeyType, ValueType>>>::iterator bad_items_iterator)
: original_map_(original_map), iteration_stage_(iteration_stage), index_(index),
bad_items_iterator_(bad_items_iterator) {
move_to_nearest_item();
}
template<typename KeyType, typename ValueType, typename Hasher>
std::pair<const KeyType, ValueType> &HashMap<KeyType, ValueType, Hasher>::iterator::operator*() const {
switch (iteration_stage_) {
case IteratingState::FIRST_ARRAY:
return *(original_map_->array_1_[index_]);
case IteratingState::SECOND_ARRAY:
return *(original_map_->array_2_[index_]);
case IteratingState::BAD_ITEMS:
return bad_items_iterator_->operator*();
default:
assert(false); // we shouldn't be here
}
}
template<typename KeyType, typename ValueType, typename Hasher>
std::pair<const KeyType, ValueType> *HashMap<KeyType, ValueType, Hasher>::iterator::operator->() const {
switch (iteration_stage_) {
case IteratingState::FIRST_ARRAY:
return original_map_->array_1_[index_].get();
case IteratingState::SECOND_ARRAY:
return original_map_->array_2_[index_].get();
case IteratingState::BAD_ITEMS:
return bad_items_iterator_->operator->();
default:
assert(false); // we shouldn't be here
}
}
template<typename KeyType, typename ValueType, typename Hasher>
typename HashMap<KeyType, ValueType, Hasher>::iterator &HashMap<KeyType, ValueType, Hasher>::iterator::operator++() {
move_to_next_item();
return *this;
}
template<typename KeyType, typename ValueType, typename Hasher>
typename HashMap<KeyType, ValueType, Hasher>::iterator HashMap<KeyType, ValueType, Hasher>::iterator::operator++(int) {
auto result = iterator(*this);
move_to_next_item();
return result;
}
template<typename KeyType, typename ValueType, typename Hasher>
bool HashMap<KeyType, ValueType, Hasher>::iterator::operator==(const HashMap::iterator &other) const {
assert(original_map_ != nullptr);
assert(other.original_map_ != nullptr);
if (original_map_ != other.original_map_) {
return false;
}
if (iteration_stage_ != other.iteration_stage_) {
return false;
}
switch (iteration_stage_) {
case IteratingState::FIRST_ARRAY:
return index_ == other.index_;
case IteratingState::SECOND_ARRAY:
return index_ == other.index_;
case IteratingState::BAD_ITEMS:
return bad_items_iterator_ == other.bad_items_iterator_;
default:
assert(false); // we shouldn't be here
}
}
template<typename KeyType, typename ValueType, typename Hasher>
bool HashMap<KeyType, ValueType, Hasher>::iterator::operator!=(const HashMap::iterator &other) const {
return !(this->operator==(other));
}
/////////////////////////////////////////// const_iterator
template<typename KeyType, typename ValueType, typename Hasher>
void HashMap<KeyType, ValueType, Hasher>::const_iterator::move_to_nearest_item() {
switch (iteration_stage_) {
case IteratingState::FIRST_ARRAY:
if (original_map_->array_1_[index_] == nullptr) {
move_to_next_item();
}
break;
case IteratingState::SECOND_ARRAY:
if (original_map_->array_2_[index_] == nullptr) {
move_to_next_item();
}
break;
case IteratingState::BAD_ITEMS:
break;
default:
assert(false); // we shouldn't be here
}
}
template<typename KeyType, typename ValueType, typename Hasher>
void HashMap<KeyType, ValueType, Hasher>::const_iterator::move_to_next_item() {
switch (iteration_stage_) {
case IteratingState::FIRST_ARRAY:
++index_;
while (index_ < original_map_->array_1_.size() && original_map_->array_1_[index_] == nullptr) {
++index_;
}
if (index_ >= original_map_->array_1_.size() || original_map_->array_1_[index_] == nullptr) {
index_ = 0;
iteration_stage_ = IteratingState::SECOND_ARRAY;
if (original_map_->array_2_[0] == nullptr) {
move_to_nearest_item();
}
}
break;
case IteratingState::SECOND_ARRAY:
++index_;
while (index_ < original_map_->array_2_.size() && original_map_->array_2_[index_] == nullptr) {
++index_;
}
if (index_ >= original_map_->array_2_.size() || original_map_->array_2_[index_] == nullptr) {
index_ = 0;
iteration_stage_ = IteratingState::BAD_ITEMS;
bad_items_iterator_ = original_map_->bad_items_.begin();
}
break;
case IteratingState::BAD_ITEMS:
++bad_items_iterator_;
break;
default:
assert(false); // we shouldn't be here
}
}
template<typename KeyType, typename ValueType, typename Hasher>
HashMap<KeyType, ValueType, Hasher>::const_iterator::const_iterator()
: original_map_(nullptr), iteration_stage_(FIRST_ARRAY), index_(0), bad_items_iterator_() {
}
template<typename KeyType, typename ValueType, typename Hasher>
HashMap<KeyType, ValueType, Hasher>::const_iterator::const_iterator(
const HashMap<KeyType, ValueType, Hasher> *original_map, IteratingState
iteration_stage, size_t
index,
typename std::list<std::unique_ptr<std::pair<const KeyType, ValueType>>>::const_iterator
bad_items_iterator)
: original_map_(original_map), iteration_stage_(iteration_stage), index_(index),
bad_items_iterator_(bad_items_iterator) {
move_to_nearest_item();
}
template<typename KeyType, typename ValueType, typename Hasher>
const std::pair<const KeyType, ValueType> &HashMap<KeyType, ValueType, Hasher>::const_iterator::operator*() const {
switch (iteration_stage_) {
case IteratingState::FIRST_ARRAY:
return *(original_map_->array_1_[index_]);
case IteratingState::SECOND_ARRAY:
return *(original_map_->array_2_[index_]);
case IteratingState::BAD_ITEMS:
return bad_items_iterator_->operator*();
default:
assert(false); // we shouldn't be here
}
}
template<typename KeyType, typename ValueType, typename Hasher>
const std::pair<const KeyType, ValueType> *HashMap<KeyType, ValueType, Hasher>::const_iterator::operator->() const {
switch (iteration_stage_) {
case IteratingState::FIRST_ARRAY:
return original_map_->array_1_[index_].get();
case IteratingState::SECOND_ARRAY:
return original_map_->array_2_[index_].get();
case IteratingState::BAD_ITEMS:
return bad_items_iterator_->operator->();
default:
assert(false); // we shouldn't be here
}
}
template<typename KeyType, typename ValueType, typename Hasher>
typename HashMap<KeyType, ValueType, Hasher>::const_iterator &
HashMap<KeyType, ValueType, Hasher>::const_iterator::operator++() {
move_to_next_item();
return *this;
}
template<typename KeyType, typename ValueType, typename Hasher>
typename HashMap<KeyType, ValueType, Hasher>::const_iterator
HashMap<KeyType, ValueType, Hasher>::const_iterator::operator++(int) {
auto result = const_iterator(*this);
move_to_next_item();
return result;
}
template<typename KeyType, typename ValueType, typename Hasher>
bool HashMap<KeyType, ValueType, Hasher>::const_iterator::operator==(const HashMap::const_iterator &other) const {
assert(original_map_ != nullptr);
assert(other.original_map_ != nullptr);
if (original_map_ != other.original_map_) {
return false;
}
if (iteration_stage_ != other.iteration_stage_) {
return false;
}
switch (iteration_stage_) {
case IteratingState::FIRST_ARRAY:
return index_ == other.index_;
case IteratingState::SECOND_ARRAY:
return index_ == other.index_;
case IteratingState::BAD_ITEMS:
return bad_items_iterator_ == other.bad_items_iterator_;
default:
assert(false); // we shouldn't be here
}
}
template<typename KeyType, typename ValueType, typename Hasher>
bool HashMap<KeyType, ValueType, Hasher>::const_iterator::operator!=(const HashMap::const_iterator &other) const {
return !(this->operator==(other));
}