-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_val_LRU_cache.cpp
122 lines (106 loc) · 2.59 KB
/
key_val_LRU_cache.cpp
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
#include <iostream>
#include <memory>
#include <vector>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <regex>
#include <functional>
#include <fstream>
#include <assert.h>
#include <stack>
// Implement last recently used cache
// Implemented just a cache as a bonus
// Implemented as a template as a bonus
// Implemented key-value cache instead of just simple set as a bonus
template <typename Key, typename Value, bool isLRU = true>
class myCache
{
public:
myCache(const size_t size)
:_size(size)
{
}
// O(1), worst O(n)
void push(const Key& key, const Value& val)
{
// O(1), worst O(n)
if (_storage.find(key) != _storage.end())
{
*_storage[key] = { key,val };
}
else
{
if (_list.size() >= _size)
{
// O(1), worst O(n)
_storage.erase(_list.back().first);
_list.pop_back();
}
// O(1)
_list.push_front({ key,val });
// O(1), worst O(n)
_storage[key] = _list.begin();
}
}
const Value* get(const Key& key)
{
if (_storage.find(key) != _storage.end())
{
auto it = _storage.at(key);
const Value* ret = &it->second;
if (isLRU)
{
// What makes it LRU cache instead of just cache:
// Move it to the front
_list.splice(_list.begin(), _list, it);
_storage[key] = _list.begin();
}
return ret;
}
return nullptr;
}
protected:
const size_t _size;
std::unordered_map<Key,
typename std::list<
std::pair<Key, Value>
>::iterator
> _storage;
std::list<std::pair<Key, Value> > _list;
};
//LRU
int main()
{
std::cout << "Hello world!\n";
myCache<int, std::string, true> cache(5);
cache.push(0, "Zero");
cache.push(1, "First");
cache.push(2, "Second");
cache.push(3, "Third");
cache.push(4, "Forth");
const std::string* str = 0;
for (size_t i = 0; i < 2; ++i)
{
if (str = cache.get(i))
{
std::cout << "From cache: " << *str << "\n";
}
}
cache.push(5, "Fiveth");
cache.push(6, "Sixth");
cache.push(7, "Seventh");
std::cout << "\n\n";
for (size_t i = 0; i < 8; ++i)
{
if (str = cache.get(i))
{
std::cout << "From cache: " << *str << "\n";
}
}
return 0;
}