-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlru-cache.cc
More file actions
863 lines (741 loc) · 26.9 KB
/
lru-cache.cc
File metadata and controls
863 lines (741 loc) · 26.9 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
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
#include <concepts>
#include <functional>
#include <iterator>
#include <list>
#include <memory>
#include <ranges>
#include <unordered_map>
#include <type_traits>
#include <utility>
template<typename Key,
typename T,
class Hash = std::hash<Key>,
class KeyEqual = std::equal_to<Key>,
class Allocator = std::allocator<std::pair<Key const, T>>
>
class lru_cache
{
using list_type = std::list<std::pair<const Key, T>, Allocator>;
using key_ref = std::reference_wrapper<const Key>;
using map_node = std::pair<key_ref const, typename list_type::iterator>;
using map_allocator = typename std::allocator_traits<Allocator>::rebind_alloc<map_node>;
using map_type = std::unordered_map<key_ref,
typename list_type::iterator,
Hash, KeyEqual,
map_allocator>;
map_type::size_type capacity;
// The key-value pairs live in 'items' list, oldest first.
list_type items = {};
// And 'map' provides fast lookup by key.
map_type map = {};
public:
using key_type = Key;
using mapped_type = T;
using value_type = std::pair<key_type const, mapped_type>;
using hasher = Hash;
using key_equal = KeyEqual;
using allocator_type = Allocator;
using reference = value_type&;
using const_reference = value_type const&;
using pointer = std::allocator_traits<Allocator>::pointer;
using const_pointer = std::allocator_traits<Allocator>::const_pointer;
using size_type = map_type::size_type;
using difference_type = map_type::difference_type;
using iterator = list_type::iterator;
using const_iterator = list_type::const_iterator;
// constructors
constexpr explicit lru_cache(size_type capacity,
Hash const& hash = hasher(),
key_equal const& equal = key_equal(),
allocator_type const& alloc = allocator_type())
: capacity{capacity},
items{alloc},
map{0, hash, equal, map_allocator(alloc)}
{
reserve(capacity);
}
constexpr lru_cache(size_type capacity,
hasher const& hash,
allocator_type const& alloc = allocator_type())
: lru_cache(capacity, hash, {}, alloc)
{}
constexpr lru_cache(size_type capacity,
allocator_type const& alloc)
: lru_cache(capacity, {}, {}, alloc)
{}
constexpr lru_cache(list_type&& list,
size_type capacity,
hasher const& hash = hasher(),
key_equal const& equal = key_equal(),
allocator_type const& alloc = allocator_type())
: lru_cache(capacity, hash, equal, alloc)
{
items.splice(items.end(), list);
if (items.size() > capacity) {
items.erase(items.begin(), std::prev(items.end(), capacity));
}
build_map();
}
template<std::input_iterator InputIt, std::sentinel_for<InputIt> Sentinel>
constexpr lru_cache(InputIt first, Sentinel last,
size_type capacity,
hasher const& hash = hasher(),
key_equal const& equal = key_equal(),
allocator_type const& alloc = allocator_type())
: lru_cache(capacity, hash, equal, alloc)
{
insert(first, last);
}
template<std::ranges::input_range R>
constexpr lru_cache(std::from_range_t, R&& range,
size_type capacity,
hasher const& hash = hasher(),
key_equal const& equal = key_equal(),
allocator_type const& alloc = allocator_type())
: lru_cache(std::ranges::begin(range), std::ranges::end(range),
capacity, hash, equal, alloc)
{}
constexpr lru_cache(std::initializer_list<value_type> init,
size_type capacity,
hasher const& hash = hasher(),
key_equal const& equal = key_equal(),
allocator_type const& alloc = allocator_type())
: lru_cache(std::from_range, init, capacity, hash, equal, alloc)
{}
constexpr lru_cache(std::initializer_list<value_type> init,
size_type capacity,
allocator_type const& alloc)
: lru_cache(init, capacity, hasher(), key_equal(), alloc)
{}
// copy/move constructors and assigment
constexpr lru_cache(lru_cache const& other)
: capacity{other.capacity},
items{other.items},
map{other.map}
{
build_map();
}
constexpr lru_cache(lru_cache const& other, std::type_identity_t<Allocator> const& alloc)
: capacity{other.capacity},
items{other.items, alloc},
map{other.map, alloc}
{
build_map();
}
constexpr lru_cache(lru_cache&& other)
: capacity{other.capacity},
items{std::move(other.items.get_allocator())},
map{std::move(other.map)}
{
items.splice(items.end(), other.items);
}
constexpr lru_cache(lru_cache&& other, std::type_identity_t<Allocator> const& alloc)
: capacity{other.capacity},
items{alloc},
map{std::move(other.map), alloc}
{
items.splice(items.end(), other);
}
lru_cache& operator=(lru_cache const& other)
{
items.clear();
items.insert_range(items.end(), other);
capacity = other.capacity;
map = other.map;
build_map();
return *this;
}
lru_cache& operator=(lru_cache&& other)
noexcept(std::allocator_traits<allocator_type>::is_always_equal::value
&& std::is_nothrow_swappable<hasher>::value
&& std::is_nothrow_swappable<key_equal>::value)
{
swap(other);
return *this;
}
constexpr lru_cache& operator=(std::initializer_list<value_type> init)
{
clear();
insert(init);
}
constexpr allocator_type get_allocator() const noexcept
{
return items.get_allocator();
}
// iterators
constexpr iterator begin() { return items.begin(); }
constexpr const_iterator begin() const { return items.begin(); }
constexpr const_iterator cbegin() const { return items.begin(); }
constexpr iterator end() { return items.end(); }
constexpr const_iterator end() const { return items.end(); }
constexpr const_iterator cend() const { return items.end(); }
constexpr iterator rbegin() { return items.rbegin(); }
constexpr const_iterator rbegin() const { return items.rbegin(); }
constexpr const_iterator crbegin() const { return items.rbegin(); }
constexpr iterator rend() { return items.rend(); }
constexpr const_iterator rend() const { return items.rend(); }
constexpr const_iterator crend() const { return items.rend(); }
// capacity
constexpr size_type size() const noexcept { return items.size(); }
constexpr size_type max_size() const noexcept { return capacity; }
constexpr size_type empty() const noexcept { return items.empty(); }
// modifiers
constexpr void touch(const_iterator it)
{
items.splice(items.end(), items, it);
}
constexpr void touch(key_type const& key)
{
auto it = map.find(key);
if (it == map.end()) {
// not present - ignore
return;
}
touch(it->second);
}
template<typename K>
constexpr void touch(K const& key)
requires requires { map.find(key); }
{
auto it = map.find(key);
if (it == map.end()) {
// not present - ignore
return;
}
touch(it->second);
}
constexpr void clear() noexcept
{
map.clear();
items.clear();
}
constexpr void resize(size_type new_capacity)
{
capacity = new_capacity;
while (items.size() > capacity) {
map.erase(items.front().first);
items.pop_front();
}
map.reserve(new_capacity);
}
constexpr iterator erase(const_iterator pos)
{
map.erase(pos->first);
return items.erase(pos);
}
constexpr iterator erase(const_iterator first, const_iterator last)
{
for (auto it = first; it != last; ++it) {
map.erase(it->first);
}
return items.erase(first, last);
}
constexpr size_type erase(key_type const& key)
{
auto it = map.find(key);
if (it == map.end()) {
return 0;
}
items.erase(it->second);
map.erase(it);
return 1;
}
template<class K> constexpr size_type erase(K&& key)
requires requires { map.find(key); }
{
auto it = map.find(key);
if (it == map.end()) {
return 0;
}
items.erase(it->second);
map.erase(it);
return 1;
}
constexpr void swap(lru_cache& other)
noexcept(std::allocator_traits<allocator_type>::is_always_equal::value
&& std::is_nothrow_swappable<hasher>::value
&& std::is_nothrow_swappable<key_equal>::value)
{
items.swap(other.items);
map.swap(other.map);
std::swap(capacity, other.capacity);
}
std::pair<iterator,bool> insert(value_type const& value)
{ return internal_insert(end(), value); }
std::pair<iterator,bool> insert(value_type&& value)
{ return internal_insert(end(), std::move(value)); }
template<typename P> requires std::constructible_from<value_type, P>
std::pair<iterator,bool> insert(P&& value)
{ return emplace(std::forward<P>(value)); }
// hints are ignored - we always insert at front
constexpr iterator insert(const_iterator hint, value_type const& value)
{ return internal_insert(hint, value).first; }
constexpr iterator insert(const_iterator hint, value_type&& value)
{ return internal_insert(hint, std::move(value)).first; }
template<typename P>
constexpr iterator insert(const_iterator hint, P&& value)
{return emplace(hint, std::forward<P>(value)).first; }
template<std::input_iterator InputIt, std::sentinel_for<InputIt> Sentinel>
constexpr void insert(InputIt first, Sentinel last)
requires requires(InputIt it) { insert(*it); }
{
if constexpr (std::sized_sentinel_for<InputIt, Sentinel> and std::bidirectional_iterator<InputIt>) {
if (size_type(last - first) > capacity) {
first = std::prev(last, capacity);
}
if (size_type(last - first) + size() > capacity) {
auto const surplus = size() + (last - first) - capacity;
erase(begin(), std::next(begin(), surplus));
}
}
while (first != last) {
insert(*first++);
}
}
void insert(std::initializer_list<value_type> init)
{
insert(init.begin(), init.end());
}
template<std::ranges::input_range R>
requires std::convertible_to<value_type, std::ranges::range_value_t<R>>
constexpr void insert_range(R&& range)
{
insert(std::ranges::begin(range), std::ranges::end(range));
}
template<typename... Args>
constexpr std::pair<iterator,bool> emplace(Args&&... args)
{
return internal_emplace(end(), std::forward<Args>(args)...);
}
template<typename... Args>
constexpr iterator emplace_hint(const_iterator position, Args&&... args)
{
// we always ignore the hint
return internal_emplace(position, std::forward<Args>(args)...).first;
}
template<typename... Args>
constexpr std::pair<iterator,bool> try_emplace(key_type const& key, Args&&... args)
{
return internal_try_emplace(end(), key, std::forward<Args>(args)...);
}
template<typename... Args>
constexpr std::pair<iterator,bool> try_emplace(key_type&& key, Args&&... args)
{
return internal_try_emplace(end(), std::move(key), std::forward<Args>(args)...);
}
template<typename K, typename... Args>
constexpr std::pair<iterator,bool> try_emplace(K&& key, Args&&... args)
requires requires { map.find(key); }
{
return internal_try_emplace(end(), std::forward<K>(key), std::forward<Args>(args)...);
}
template<typename... Args>
constexpr iterator try_emplace(const_iterator position, key_type const& key, Args&&... args)
{
return internal_try_emplace(position, key, std::forward<Args>(args)...).first;
}
template<typename... Args>
constexpr std::pair<iterator,bool> try_emplace(const_iterator position, key_type&& key, Args&&... args)
{
return internal_try_emplace(position, std::move(key), std::forward<Args>(args)...).first;
}
template<typename K, typename... Args>
constexpr std::pair<iterator,bool> try_emplace(const_iterator position, K&& key, Args&&... args)
requires requires { map.find(key); }
{
return internal_try_emplace(position, std::forward<K>(key), std::forward<Args>(args)...).first;
}
template<class M>
constexpr std::pair<iterator, bool> insert_or_assign(key_type const& key, M&& obj) {
return insert_or_assign(end(), key, std::forward<M>(obj));
}
template<class M>
constexpr std::pair<iterator, bool> insert_or_assign(key_type&& key, M&& obj) {
return insert_or_assign(end(), std::move(key), std::forward<M>(obj));
}
template<class K, class M>
constexpr std::pair<iterator, bool> insert_or_assign(K&& key, M&& obj) {
return insert_or_assign(end(), std::forward<K>(key), std::forward<M>(obj));
}
template<class M>
constexpr iterator insert_or_assign(const_iterator hint, key_type const& key, M&& obj) {
auto [position, inserted] = internal_try_emplace(hint, key, std::forward<M>(obj));
if (!inserted) {
position->second = std::forward<M>(obj);
}
return {position, inserted};
}
template<class M>
constexpr iterator insert_or_assign(const_iterator hint, key_type&& key, M&& obj) {
auto [position, inserted] = internal_try_emplace(hint, std::move(key), std::forward<M>(obj));
if (!inserted) {
position->second = std::forward<M>(obj);
}
return {position, inserted};
}
template<class K, class M>
constexpr iterator insert_or_assign(const_iterator hint, K&& key, M&& obj) {
auto [position, inserted] = internal_try_emplace(hint, std::forward<K>(key), std::forward<M>(obj));
if (!inserted) {
position->second = std::forward<M>(obj);
}
return {position, inserted};
}
// observers
hasher hash_function() const { return map.hash_function(); }
key_equal key_eq() const { return map.key_eq(); }
// map operations
constexpr iterator find(key_type const& k)
{
if (auto it = map.find(k); it != map.end) {
return it->second;
}
return end();
}
constexpr const_iterator find(key_type const& k) const
{
if (auto it = map.find(k); it != map.end) {
return it->second;
}
return end();
}
template<typename K>
constexpr iterator find(K const& k)
requires requires { map.find(k); }
{
if (auto it = map.find(k); it != map.end) {
return it->second;
}
return end();
}
template<typename K>
constexpr const_iterator find(K const& k) const
requires requires { map.find(k); }
{
if (auto it = map.find(k); it != map.end) {
return it->second;
}
return end();
}
constexpr size_type count(key_type const& k) const { return map.count(k); }
template<typename K>
constexpr size_type count(K const& k) const { return map.count(k); }
constexpr bool contains(key_type const& k) const { return map.contains(k); }
template<typename K>
constexpr bool contains(K const& k) const { return map.contains(k); }
constexpr std::pair<iterator, iterator> equal_range(key_type const& k)
{
auto it = find(k);
return { it, it + (it != map.end()) };
}
constexpr std::pair<const_iterator, const_iterator> equal_range(key_type const& k) const
{
auto it = find(k);
return { it, it + (it != map.end()) };
}
template<typename K>
constexpr std::pair<iterator, iterator> equal_range(K const& k)
requires requires { map.find(k); }
{
auto it = find(k);
return { it, it + (it != map.end()) };
}
template<typename K>
constexpr std::pair<const_iterator, const_iterator> equal_range(K const& k) const
requires requires { map.find(k); }
{
auto it = find(k);
return { it, it + (it != map.end()) };
}
constexpr mapped_type& operator[](key_type const& key)
{
auto [it, inserted] = emplace(key, mapped_type()); // Q: non-movable mapped_type?
touch(it);
return it->second;
}
constexpr mapped_type& operator[](key_type&& key)
requires std::constructible_from<mapped_type>
{
auto [it, inserted] = emplace(std::move(key), mapped_type());
touch(it);
return it->second;
}
template<typename K>
constexpr mapped_type& operator[](K&& key)
{
auto [it, inserted] = emplace(std::forward<K>(key), mapped_type());
touch(it);
return it->second;
}
constexpr mapped_type& at(key_type const& k)
{
auto it = map.at(k);
touch(it);
return it->second;
}
constexpr mapped_type const& at(key_type const& k) const
{
auto it = map.at(k);
touch(it);
return it->second;
}
template<typename K>
constexpr mapped_type& at(K const& k)
requires requires { map.at(k); }
{
auto it = map.at(k);
touch(it);
return it->second;
}
template<typename K>
constexpr mapped_type const& at(K const& k) const
requires requires { map.at(k); }
{
auto it = map.at(k).second;
return it->second;
}
// peek() functions perform lookup without promoting the entry to most
// recently used position. They return a null pointer if the key is
// not present.
// Q: return std::optional<std::reference_wrapper<mapped_type>> instead?
constexpr mapped_type* peek(key_type const& k)
{
auto it = map.find(k);
return it == map.end() ? nullptr : &it->second->second;
}
constexpr mapped_type const* peek(key_type const& k) const
{
auto it = map.find(k);
return it == map.end() ? nullptr : &it->second->second;
}
template<typename K>
constexpr mapped_type* peek(K const& k)
requires requires { map.find(k); }
{
auto it = map.find(k);
return it == map.end() ? nullptr : &it->second->second;
}
template<typename K>
constexpr mapped_type const* peek(K const& k) const
requires requires { map.find(k); }
{
auto it = map.find(k);
return it == map.end() ? nullptr : &it->second->second;
}
// hash policy
constexpr float load_factor() const noexcept { return map.load_factor(); }
constexpr float max_load_factor() const noexcept { return map.max_load_factor(); }
constexpr void max_load_factor(float z) { map.max_load_factor(z); }
constexpr void rehash(size_type n) { map.rehash(n); }
constexpr void reserve(size_type n) { map.reserve(n); }
private:
void build_map()
{
// This function assumes that if map has any entries, then
// they are the correct keys for the items in the list.
for (auto it = items.begin(); it != items.end(); ++it) {
map[it->first] = it;
}
}
template<typename V>
std::pair<iterator,bool> internal_insert(iterator it, V&& value) {
if (auto map_it = map.find(value.first); map_it != map.end()) {
return { map_it->second, false };
}
// Trim list if needed
if (items.size() >= capacity) {
map.erase(items.front().first);
items.pop_front();
}
items.emplace(it, std::forward<V>(value));
--it; // now points at the new item
map.emplace(it->first, it).first;
return { it, true };
}
template<typename... Args>
constexpr std::pair<iterator,bool> internal_emplace(iterator it, Args&&... args)
{
return internal_insert(it, value_type(std::forward<Args>(args)...));
}
template<typename K, typename... Args>
constexpr std::pair<iterator,bool> internal_try_emplace(iterator it, K&& key, Args&&... args)
requires requires { map.find(key); }
{
if (auto map_it = map.find(key); map_it != map.end()) {
return { map_it->second, false };
}
// Trim list if needed
if (items.size() >= capacity) {
map.erase(items.front().first);
items.pop_front();
}
items.emplace(it,
std::piecewise_construct,
std::forward_as_tuple(std::forward<K>(key)),
std::forward_as_tuple(std::forward<Args>(args)...));
--it; // now points at the new item
map.emplace(it->first, it).first;
return { it, true };
}
};
// Deduction guides
namespace traits
{
template<std::input_iterator I>
using iter_value_t = std::iterator_traits<I>::value_type;
template<std::input_iterator I>
using iter_key_t = std::remove_const_t<typename iter_value_t<I>::first_type>;
template<std::input_iterator I>
using iter_mapped_t = typename iter_value_t<I>::second_type;
template<std::ranges::input_range R>
using range_value_t = std::ranges::range_value_t<R>;
template<std::ranges::input_range I>
using range_key_t = std::remove_const_t<typename range_value_t<I>::first_type>;
template<std::ranges::input_range I>
using range_mapped_t = typename range_value_t<I>::second_type;
}
template<std::input_iterator InputIt, std::sentinel_for<InputIt> Sentinel,
typename Hash = std::hash<traits::iter_key_t<InputIt>>,
typename KeyEqual = std::equal_to<traits::iter_key_t<InputIt>>,
typename Allocator = std::allocator<traits::iter_value_t<InputIt>>>
lru_cache(InputIt, Sentinel, std::size_t,
Hash = Hash(), KeyEqual = KeyEqual(), Allocator = Allocator())
-> lru_cache<traits::iter_key_t<InputIt>, traits::iter_mapped_t<InputIt>,
Hash, KeyEqual, Allocator>;
template<std::ranges::input_range R,
typename Hash = std::hash<traits::range_key_t<R>>,
typename KeyEqual = std::equal_to<traits::range_key_t<R>>,
typename Allocator = std::allocator<traits::range_value_t<R>>>
lru_cache(std::from_range_t, R&&, std::size_t,
Hash = Hash(), KeyEqual = KeyEqual(), Allocator = Allocator())
-> lru_cache<traits::range_key_t<R>, traits::range_mapped_t<R>, Hash, KeyEqual, Allocator>;
template<typename Key, typename T,
typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>,
typename Allocator = std::allocator<std::pair<Key const, T>>>
lru_cache(std::initializer_list<std::pair<Key const, T>>, std::size_t,
Hash = Hash(), KeyEqual = KeyEqual(), Allocator = Allocator())
-> lru_cache<Key, T, Hash, KeyEqual, Allocator>;
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <array>
#include <format>
#include <memory>
#include <string>
static auto numbers()
{
auto a = lru_cache<int,std::string>(3);
a[1] = "one";
a[2] = "two";
return a;
}
TEST(constructor, list)
{
std::list<std::pair<const int, int>> content
= { {4, 40}, {6, 60}, {8, 80} };
auto cache = lru_cache(std::move(content), 2);
auto keys = cache | std::views::keys | std::ranges::to<std::vector>();
EXPECT_THAT(keys, testing::ElementsAre(6, 8));
}
TEST(constructor, iterators)
{
std::array<std::pair<const int, int>, 3> content
= {{ {4, 40}, {6, 60}, {8, 80} }};
auto cache = lru_cache(content.begin(), content.end(), 2);
auto keys = cache | std::views::keys | std::ranges::to<std::vector>();
EXPECT_THAT(keys, testing::ElementsAre(6, 8));
}
TEST(constructor, range)
{
std::array<std::pair<const int, int>, 3> content
= {{ {4, 40}, {6, 60}, {8, 80} }};
auto cache = lru_cache(std::from_range, content, 2);
auto keys = cache | std::views::keys | std::ranges::to<std::vector>();
EXPECT_THAT(keys, testing::ElementsAre(6, 8));
}
TEST(constructor, init_list)
{
std::initializer_list<std::pair<int const, int>> content
= { {4, 40}, {6, 60}, {8, 80} };
auto cache = lru_cache(content, 2);
auto keys = cache | std::views::keys | std::ranges::to<std::vector>();
EXPECT_THAT(keys, testing::ElementsAre(6, 8));
}
TEST(constructor, copy)
{
auto a = numbers();
auto b = a;
EXPECT_EQ(*a.peek(1), "one");
EXPECT_EQ(*b.peek(1), "one");
EXPECT_NE(a.peek(1), b.peek(1));
}
TEST(constructor, move)
{
auto a = numbers();
auto two_addr = std::addressof(a[2]);
ASSERT_FALSE(a.empty());
auto b = std::move(a);
EXPECT_TRUE(a.empty());
// Check that the value has moved to b
EXPECT_EQ(two_addr, std::addressof(b[2]));
}
TEST(assignment, copy)
{
auto a = numbers();
auto b = lru_cache<int,std::string>(20);
b[5] = "five";
b = a;
// Ensure values are the same, but addresses are different.
EXPECT_EQ(a[1], "one");
EXPECT_EQ(b[1], "one");
EXPECT_NE(a.peek(1), b.peek(1));
// Check that modifying b doesn't affect a.
b[1] = "ONE";
EXPECT_EQ(a[1], "one");
EXPECT_EQ(b[1], "ONE");
}
TEST(assignment, move)
{
auto a = numbers();
auto two_addr = std::addressof(a[2]);
ASSERT_FALSE(a.empty());
auto b = lru_cache<int,std::string>(20);
b[5] = "five";
b = std::move(a);
EXPECT_THROW(b.at(5), std::out_of_range);
EXPECT_EQ(two_addr, std::addressof(b[2]));
}
TEST(touch, key)
{
auto a = numbers();
auto keys_before = a | std::views::keys | std::ranges::to<std::vector>();
EXPECT_THAT(keys_before, testing::ElementsAre(1, 2));
a.touch(1);
auto keys_after = a | std::views::keys | std::ranges::to<std::vector>();
EXPECT_THAT(keys_after, testing::ElementsAre(2, 1));
EXPECT_EQ(*a.peek(2), "two");
// has not altered the sequence
EXPECT_THAT(keys_after, testing::ElementsAre(2, 1));
}
TEST(clear, all)
{
auto a = numbers();
a.clear();
EXPECT_EQ(a.size(), 0uz);
EXPECT_EQ(nullptr, a.peek(1));
}
TEST(resize, smaller)
{
auto a = numbers();
a.resize(1);
EXPECT_EQ(a.size(), 1uz);
auto keys_after = a | std::views::keys | std::ranges::to<std::vector>();
EXPECT_THAT(keys_after, testing::ElementsAre(2));
}
TEST(resize, bigger)
{
auto a = numbers();
a.resize(4);
EXPECT_EQ(a.size(), 2uz);
auto keys_after = a | std::views::keys | std::ranges::to<std::vector>();
EXPECT_THAT(keys_after, testing::ElementsAre(1, 2));
}