forked from estraier/tkrzw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tkrzw_containers.h
1718 lines (1540 loc) · 46 KB
/
tkrzw_containers.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
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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************************************
* Miscellaneous data containers
*
* Copyright 2020 Google LLC
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*************************************************************************************************/
#ifndef _TKRZW_CONTAINERS_H
#define _TKRZW_CONTAINERS_H
#include <algorithm>
#include <atomic>
#include <memory>
#include <mutex>
#include <set>
#include <vector>
#include <cinttypes>
#include <cstring>
#include "tkrzw_lib_common.h"
#include "tkrzw_thread_util.h"
namespace tkrzw {
/**
* Doubly-linked hash map.
* @param KEYTYPE the key type.
* @param VALUETYPE the value type.
* @param HASHTYPE the hash functor.
* @param EQUALTOTYPE the equality checking functor.
*/
template <typename KEYTYPE, typename VALUETYPE,
typename HASHTYPE = std::hash<KEYTYPE>, typename EQUALTOTYPE = std::equal_to<KEYTYPE>>
class LinkedHashMap final {
public:
/** The default value of the number of buckets. */
static constexpr size_t DEFAULT_NUM_BUCKETS = 101;
/**
* Record data.
*/
struct Record final {
/** The key. */
KEYTYPE key;
/** The value. */
VALUETYPE value;
/** The child record. */
Record* child;
/** The previous record. */
Record* prev;
/** The next record. */
Record* next;
/**
* Constructor.
* @param key The key.
* @param value The value.
*/
Record(const KEYTYPE& key, const VALUETYPE& value) :
key(key), value(value), child(nullptr), prev(nullptr), next(nullptr) {
}
};
/**
* Const iterator of records.
* @details The iterator is invalidated when the current record is removed.
*/
class ConstIterator final {
friend class LinkedHashMap;
public:
/**
* Copy constructor.
* @param rhs The right-hand-side object.
*/
ConstIterator(const ConstIterator& rhs) : map_(rhs.map_), rec_(rhs.rec_) {}
/**
* Gets the reference to the current record.
* @return The reference to the current record.
*/
const Record& operator *() const {
return *rec_;
}
/**
* Accesses a member of the current record.
* @return The pointer to the current record.
*/
const Record* operator->() const {
return rec_;
}
/**
* Assignment operator from the self type.
* @param rhs The right-hand-side object.
* @return The reference to itself.
*/
ConstIterator& operator =(const ConstIterator& rhs) {
if (&rhs == this) return *this;
map_ = rhs.map_;
rec_ = rhs.rec_;
return *this;
}
/**
* Equality operator with the self type.
* @param rhs The right-hand-side object.
* @return True if the both are equal, or false if not.
*/
bool operator ==(const ConstIterator& rhs) const {
return map_ == rhs.map_ && rec_ == rhs.rec_;
}
/**
* Non-equality operator with the self type.
* @param rhs The right-hand-side object.
* @return False if the both are equal, or true if not.
*/
bool operator !=(const ConstIterator& rhs) const {
return map_ != rhs.map_ || rec_ != rhs.rec_;
}
/**
* Preposting increment operator.
* @return The iterator itself.
*/
ConstIterator& operator ++() {
rec_ = rec_->next;
return *this;
}
/**
* Postpositive increment operator.
* @return An iterator of the old position.
*/
ConstIterator operator ++(int) {
ConstIterator old(*this);
rec_ = rec_->next;
return old;
}
/**
* Preposting decrement operator.
* @return The iterator itself.
*/
ConstIterator& operator --() {
if (rec_) {
rec_ = rec_->prev;
} else {
rec_ = map_->last_;
}
return *this;
}
/**
* Postpositive decrement operator.
* @return An iterator of the old position.
*/
ConstIterator operator --(int) {
ConstIterator old(*this);
if (rec_) {
rec_ = rec_->prev;
} else {
rec_ = map_->last_;
}
return old;
}
private:
/**
* Constructor.
* @param map The container.
* @param rec The pointer to the current record.
*/
ConstIterator(const LinkedHashMap* map, const Record* rec) : map_(map), rec_(rec) {}
/** The container. */
const LinkedHashMap* map_;
/** The current record. */
const Record* rec_;
};
/**
* Iterator of records.
* @details The iterator is invalidated when the current record is removed.
*/
class Iterator final {
friend class LinkedHashMap;
public:
/**
* Copy constructor.
* @param rhs The right-hand-side object.
*/
Iterator(const Iterator& rhs) : map_(rhs.map_), rec_(rhs.rec_) {}
/**
* Cast operator to the const iterator.
* @return The casted const iterator.
*/
operator ConstIterator() const {
return ConstIterator(map_, rec_);
}
/**
* Gets the reference to the current record.
* @return The reference to the current record.
*/
Record& operator *() {
return *rec_;
}
/**
* Gets the reference to the current record.
* @return The reference to the current record.
*/
const Record& operator *() const {
return *rec_;
}
/**
* Accesses a member of the current record.
* @return The pointer to the current record.
*/
Record* operator->() {
return rec_;
}
/**
* Accesses a member of the current record.
* @return The pointer to the current record.
*/
Record* operator->() const {
return rec_;
}
/**
* Assignment operator from the self type.
* @param rhs The right-hand-side object.
* @return The reference to itself.
*/
Iterator& operator =(const Iterator& rhs) {
if (&rhs == this) return *this;
map_ = rhs.map_;
rec_ = rhs.rec_;
return *this;
}
/**
* Equality operator with the self type.
* @param rhs The right-hand-side object.
* @return True if the both are equal, or false if not.
*/
bool operator ==(const Iterator& rhs) const {
return map_ == rhs.map_ && rec_ == rhs.rec_;
}
/**
* Equality operator with the const type.
* @param rhs The right-hand-side object.
* @return True if the both are equal, or false if not.
*/
bool operator ==(const ConstIterator& rhs) const {
return map_ == rhs.map_ && rec_ == rhs.rec_;
}
/**
* Non-equality operator with the self type.
* @param rhs The right-hand-side object.
* @return False if the both are equal, or true if not.
*/
bool operator !=(const Iterator& rhs) const {
return map_ != rhs.map_ || rec_ != rhs.rec_;
}
/**
* Non-equality operator with the const type.
* @param rhs The right-hand-side object.
* @return False if the both are equal, or true if not.
*/
bool operator !=(const ConstIterator& rhs) const {
return map_ != rhs.map_ || rec_ != rhs.rec_;
}
/**
* Preposting increment operator.
* @return The iterator itself.
*/
Iterator& operator ++() {
rec_ = rec_->next;
return *this;
}
/**
* Postpositive increment operator.
* @return An iterator of the old position.
*/
Iterator operator ++(int) {
Iterator old(*this);
rec_ = rec_->next;
return old;
}
/**
* Preposting decrement operator.
* @return The iterator itself.
*/
Iterator& operator --() {
if (rec_) {
rec_ = rec_->prev;
} else {
rec_ = map_->last_;
}
return *this;
}
/**
* Postpositive decrement operator.
* @return An iterator of the old position.
*/
Iterator operator --(int) {
Iterator old(*this);
if (rec_) {
rec_ = rec_->prev;
} else {
rec_ = map_->last_;
}
return old;
}
private:
/**
* Constructor.
* @param map The container.
* @param rec The pointer to the current record.
*/
Iterator(LinkedHashMap* map, Record* rec) : map_(map), rec_(rec) {}
/** The container. */
LinkedHashMap* map_;
/** The current record. */
Record* rec_;
};
/**
* Enumeration for move modes.
*/
enum MoveMode : int32_t {
/** To keep the current position. */
MOVE_CURRENT = 0,
/** To move to the first. */
MOVE_FIRST = 1,
/** To move to the last. */
MOVE_LAST = 2,
};
/**
* Default constructor.
*/
LinkedHashMap();
/**
* Constructor.
* @param num_buckets The number of buckets of the hash table.
*/
explicit LinkedHashMap(size_t num_buckets);
/**
* Destructor.
*/
~LinkedHashMap();
/**
* Copy and assignment are disabled.
*/
explicit LinkedHashMap(const LinkedHashMap& rhs) = delete;
LinkedHashMap& operator =(const LinkedHashMap& rhs) = delete;
/**
* Retrieves a record.
* @param key The key.
* @param mode The moving mode.
* @return The pointer to the corresponding record, or nullptr on failure.
*/
Record* Get(const KEYTYPE& key, MoveMode mode = MOVE_CURRENT);
/**
* Retrieves a record.
* @param key The key.
* @param default_value The value to be returned on failure.
* @param mode The moving mode.
* @return The value of the record or the default value on failure.
*/
VALUETYPE GetSimple(const KEYTYPE& key, VALUETYPE default_value = VALUETYPE(),
MoveMode mode = MOVE_CURRENT);
/**
* Stores a record.
* @param key The key.
* @param value The value.
* @param mode The moving mode.
* @param overwrite Whether to overwrite the existing value.
* @return The pointer to the stored record or the existing record.
*/
Record* Set(const KEYTYPE& key, const VALUETYPE& value,
bool overwrite = true, MoveMode mode = MOVE_CURRENT);
/**
* Removes a record.
* @param key The key.
* @return True on success, or false on failure.
*/
bool Remove(const KEYTYPE& key);
/**
* Migrates a record to another map.
* @param key The key.
* @param dest The destination map.
* @param mode The moving mode.
* @return The pointer to the migrated record, or nullptr on failure.
*/
Record* Migrate(const KEYTYPE& key, LinkedHashMap* dest, MoveMode mode);
/**
* Removes all records.
*/
void clear();
/**
* Gets the number of records.
* @return The number of records.
*/
size_t size() const;
/**
* Checks whether no records exist.
* @return True if there's no record or false if there are one or more records.
*/
bool empty() const;
/**
* Gets an iterator at the first record.
* @return The iterator at the first record.
*/
Iterator begin();
/**
* Gets a const iterator at the first record.
* @return The const iterator at the first record.
*/
ConstIterator begin() const;
/**
* Gets an iterator of the end sentry.
* @return The iterator at the end sentry.
*/
Iterator end();
/**
* Gets a const iterator of the end sentry.
* @return The const iterator at the end sentry.
*/
ConstIterator end() const;
/**
* Gets an iterator at a record.
* @param key The key of the record to find.
* @return The pointer to the value of the corresponding record, or nullptr on failure.
*/
Iterator find(const KEYTYPE& key);
/**
* Gets an iterator at a record.
* @param key The key of the record to find.
* @return The pointer to the value of the corresponding record, or nullptr on failure.
*/
ConstIterator find(const KEYTYPE& key) const;
/**
* Refers to a record.
* @param key The key of the record to find.
* @return The reference to the record.
* @details If there's no matching record. a new record is created.
*/
VALUETYPE& operator[](const KEYTYPE& key);
/**
* Insert a record without overwriting an existing record.
* @param record The record to insert.
* @return A pair. The first element is an iterator to the inserted or existing element.
* The second element is a boolean of whether the insertion is successful.
*/
std::pair<Iterator, bool> insert(const Record& record);
/**
* Removes a record.
* @param key The key.
* @return 1 on success or 0 if there's no matching key.
*/
size_t erase(const KEYTYPE& key);
/**
* Gets the reference of the first record.
* @return The reference of the first record.
*/
Record& front();
/**
* Gets the reference of the first record.
* @return The reference of the first record.
*/
const Record& front() const;
/**
* Gets the reference of the last record.
* @return The reference of the last record.
*/
Record& back();
/**
* Gets the reference of the last record.
* @return The reference of the last record.
*/
const Record& back() const;
/**
* Rehashes all records to a new number of records.
* @param num_buckets The new number of buckets.
*/
void rehash(size_t num_buckets);
/**
* Gets average number of elements per bucket.
* @return The average number of elements per bucket.
*/
float load_factor() const;
private:
/**
* Initialize fields.
*/
void Initialize();
/**
* Clean up fields.
*/
void Destroy();
/** The functor of the hash function. */
HASHTYPE hash_;
/** The functor of the equalto function. */
EQUALTOTYPE equalto_;
/** The bucket array. */
Record** buckets_;
/** The number of buckets. */
size_t num_buckets_;
/** The first record. */
Record* first_;
/** The last record. */
Record* last_;
/** The number of records. */
size_t num_records_;
};
/**
* LRU cache.
* @param VALUETYPE the value type.
*/
template <class VALUETYPE>
class LRUCache final {
typedef LinkedHashMap<int64_t, std::shared_ptr<VALUETYPE>> CacheMap;
public:
/**
* Iterator to access each record.
* @details The iterator is invalidated when the current record is removed.
*/
class Iterator final {
friend class LRUCache<VALUETYPE>;
public:
/**
* Get the value of the current record.
* @param id The pointer to store the ID of the record. If it is nullptr, it is ignored.
* @return A shared pointer to the value object. It points to nullptr on failure.
*/
std::shared_ptr<VALUETYPE> Get(int64_t* id);
/**
* Moves the iterator to the next record.
*/
void Next();
private:
/**
* Constructor.
* @param map The pointer to the cache map.
*/
Iterator(CacheMap* cache);
CacheMap* cache_;
typename CacheMap::Iterator it_;
};
/**
* Constructor.
* @param capacity The maximum number of records in the cache.
*/
LRUCache(size_t capacity);
/**
* Adds a record.
* @param id The ID of the record.
* @param value The pointer to the value object. Ownership is taken.
* @return A shared pointer to the value object.
* @details If there is an existing record of the same ID, the new value object is just deleted
* and the return value refers to the existing record.
*/
std::shared_ptr<VALUETYPE> Add(int64_t id, VALUETYPE* value);
/**
* Gives back a record which has been removed.
* @param id The ID of the record.
* @param value The moved pointer to the value object. Ownership is taken.
*/
void GiveBack(int64_t id, std::shared_ptr<VALUETYPE>&& value);
/**
* Gets the value of a record.
* @param id The ID of the record.
* @return A shared pointer to the value object. It points to nullptr on failure.
*/
std::shared_ptr<VALUETYPE> Get(int64_t id);
/**
* Removes a record.
* @param id The ID of the record.
*/
void Remove(int64_t id);
/**
* Removes the least recent used record.
* @param id The pointer to store the ID of the record. If it is nullptr, it is ignored.
* @return A shared pointer to the value object. It points to nullptr on failure.
*/
std::shared_ptr<VALUETYPE> RemoveLRU(int64_t* id = nullptr);
/**
* Gets the number of records.
* @return The number of records.
*/
size_t Size() const;
/**
* Checks whether no records exist.
* @return True if there's no record or false if there are one or more records.
*/
bool IsEmpty() const;
/**
* Checks whether stored records exceeds the capacity.
* @return True if stored records exceeds the capacity or false if not.
*/
bool IsSaturated() const;
/**
* Removes all records.
*/
void Clear();
/**
* Makes an iterator for each record.
* @return The iterator for each record.
*/
Iterator MakeIterator();
private:
size_t capacity_;
CacheMap cache_;
};
/**
* Double-layered LRU cache.
* @param VALUETYPE the value type.
*/
template <class VALUETYPE>
class DoubleLRUCache final {
typedef LinkedHashMap<int64_t, std::shared_ptr<VALUETYPE>> CacheMap;
public:
/**
* Iterator to access each record.
* @details The iterator is invalidated when the current record is removed.
*/
class Iterator final {
friend class DoubleLRUCache<VALUETYPE>;
public:
/**
* Get the value of the current record.
* @param id The pointer to store the ID of the record. If it is nullptr, it is ignored.
* @return A shared pointer to the value object. It points to nullptr on failure.
*/
std::shared_ptr<VALUETYPE> Get(int64_t* id);
/**
* Moves the iterator to the next record.
*/
void Next();
private:
/**
* Constructor.
* @param hot The pointer to the hot cache.
* @param warm The pointer to the warm cache.
*/
Iterator(CacheMap* hot, CacheMap* warm);
CacheMap* hot_;
CacheMap* warm_;
bool on_hot_;
typename CacheMap::Iterator it_;
};
/**
* Constructor.
* @param hot_capacity The maximum number of records in the hot cache.
* @param warm_capacity The maximum number of records in the worm cache.
*/
DoubleLRUCache(size_t hot_capacity, size_t warm_capacity);
/**
* Adds a record.
* @param id The ID of the record.
* @param value The pointer to the value object. Ownership is taken.
* @return A shared pointer to the value object.
* @details If there is an existing record of the same ID, the new value object is just deleted
* and the return value refers to the existing record.
*/
std::shared_ptr<VALUETYPE> Add(int64_t id, VALUETYPE* value);
/**
* Gives back a record which has been removed.
* @param id The ID of the record.
* @param value The moved pointer to the value object. Ownership is taken.
*/
void GiveBack(int64_t id, std::shared_ptr<VALUETYPE>&& value);
/**
* Gets the value of a record.
* @param id The ID of the record.
* @param promotion True if the record can be promoted to the hot list.
* @return A shared pointer to the value object. It points to nullptr on failure.
*/
std::shared_ptr<VALUETYPE> Get(int64_t id, bool promotion);
/**
* Removes a record.
* @param id The ID of the record.
*/
void Remove(int64_t id);
/**
* Removes the least recent used record.
* @param id The pointer to store the ID of the record. If it is nullptr, it is ignored.
* @return A shared pointer to the value object. It points to nullptr on failure.
*/
std::shared_ptr<VALUETYPE> RemoveLRU(int64_t* id = nullptr);
/**
* Gets the number of records.
* @return The number of records.
*/
size_t Size() const;
/**
* Checks whether no records exist.
* @return True if there's no record or false if there are one or more records.
*/
bool IsEmpty() const;
/**
* Checks whether stored records exceeds the capacity.
* @return True if stored records exceeds the capacity or false if not.
*/
bool IsSaturated() const;
/**
* Removes all records.
*/
void Clear();
/**
* Makes an iterator for each record.
* @return The iterator for each record.
*/
Iterator MakeIterator();
private:
size_t hot_capacity_;
size_t warm_capacity_;
CacheMap hot_;
CacheMap warm_;
};
/**
* Thread-safe wrapper of std::set.
*/
template <class VALUETYPE>
class AtomicSet final {
public:
/**
* Constructor.
*/
AtomicSet();
/**
* Checks whether a record exists.
* @param data The record data.
* @return True if the record exists or false if not.
*/
bool Check(const VALUETYPE& data);
/**
* Inserts a record.
* @param data The record data.
* @return True if the insertion is sucesss or false on failure.
*/
bool Insert(const VALUETYPE& data);
/**
* Inserts a record in move semantics.
* @param data The record data.
* @return True if the insertion is sucesss or false on failure.
*/
bool Insert(VALUETYPE&& data);
/**
* Removes a record.
* @param data The record data.
* @return True on success or false on failure.
*/
bool Remove(const VALUETYPE& data);
/**
* Checks whether the set is empty.
* @return True if the set is empty or false if not.
*/
bool IsEmpty() const;
/**
* Removes the least ID from the set.
* @return The least ID or the default value on failure.
*/
VALUETYPE Pop();
/**
* Clears all IDs from the set.
*/
void Clear();
private:
std::set<VALUETYPE> set_;
std::atomic_bool empty_;
SpinMutex mutex_;
};
/**
* Adds a pair of a cont and a payload to a heap vector.
* @param cost The cost.
* @param payload The payload.
* @param capacity The capacity of the heap vector.
* @param heap The pointer to the heap vector.
*/
template<typename C, typename T>
void HeapByCostAdd(const C& cost, const T& payload, size_t capacity,
std::vector<std::pair<C, T>>* heap) {
if (heap->size() >= capacity) {
const auto& front = heap->front();
if (cost > front.first || (cost == front.first && payload >= front.second)) {
return;
}
std::pop_heap(heap->begin(), heap->end());
heap->pop_back();
}
heap->emplace_back(std::make_pair(cost, payload));
std::push_heap(heap->begin(), heap->end());
}
/**
* Finishes a heap vector to be in sorted order.
* @param heap The heap vector.
*/
template<typename C, typename T>
void HeapByCostFinish(std::vector<std::pair<C, T>>* heap) {
auto it = heap->end();
while (it > heap->begin()) {
std::pop_heap(heap->begin(), it);
it--;
}
}
template <typename KEYTYPE, typename VALUETYPE, typename HASHTYPE, typename EQUALTOTYPE>
inline LinkedHashMap<KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE>::LinkedHashMap() :
buckets_(nullptr), num_buckets_(DEFAULT_NUM_BUCKETS),
first_(nullptr), last_(nullptr), num_records_(0) {
Initialize();
}
template <typename KEYTYPE, typename VALUETYPE, typename HASHTYPE, typename EQUALTOTYPE>
inline LinkedHashMap<KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE>::LinkedHashMap(
size_t num_buckets) :
buckets_(nullptr), num_buckets_(num_buckets),
first_(nullptr), last_(nullptr), num_records_(0) {
if (num_buckets_ < 1) {
num_buckets_ = DEFAULT_NUM_BUCKETS;
}
Initialize();
}
template <typename KEYTYPE, typename VALUETYPE, typename HASHTYPE, typename EQUALTOTYPE>
inline LinkedHashMap<KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE>::~LinkedHashMap() {
Destroy();
}
template <typename KEYTYPE, typename VALUETYPE, typename HASHTYPE, typename EQUALTOTYPE>
inline typename LinkedHashMap<KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE>::Record*
LinkedHashMap<KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE>::Get(
const KEYTYPE& key, MoveMode mode) {
const size_t bucket_index = hash_(key) % num_buckets_;
Record* rec = buckets_[bucket_index];
while (rec) {
if (equalto_(rec->key, key)) {
switch (mode) {
default: {
break;
}
case MOVE_FIRST: {
if (first_ != rec) {
if (last_ == rec) {
last_ = rec->prev;
}
if (rec->prev != nullptr) {
rec->prev->next = rec->next;
}
if (rec->next != nullptr) {
rec->next->prev = rec->prev;
}
rec->prev = nullptr;
rec->next = first_;
first_->prev = rec;
first_ = rec;
}
break;
}
case MOVE_LAST: {
if (last_ != rec) {
if (first_ == rec) {
first_ = rec->next;
}
if (rec->prev != nullptr) {
rec->prev->next = rec->next;
}
if (rec->next != nullptr) {
rec->next->prev = rec->prev;
}
rec->prev = last_;
rec->next = nullptr;
last_->next = rec;
last_ = rec;
}
break;
}
}
return rec;
} else {
rec = rec->child;
}
}
return nullptr;
}
template <typename KEYTYPE, typename VALUETYPE, typename HASHTYPE, typename EQUALTOTYPE>
VALUETYPE LinkedHashMap<KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE>::GetSimple(
const KEYTYPE& key, VALUETYPE default_value, MoveMode mode) {
const Record* rec = Get(key, mode);
return rec == nullptr ? default_value : rec->value;
}
template <typename KEYTYPE, typename VALUETYPE, typename HASHTYPE, typename EQUALTOTYPE>
typename LinkedHashMap<KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE>::Record *
LinkedHashMap<KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE>::Set(
const KEYTYPE& key, const VALUETYPE& value, bool overwrite, MoveMode mode) {
const size_t bucket_index = hash_(key) % num_buckets_;
Record* rec = buckets_[bucket_index];
while (rec) {
if (equalto_(rec->key, key)) {
if (!overwrite) {