-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path146.lru-cache.45007200.ac.cpp
148 lines (137 loc) · 3.07 KB
/
146.lru-cache.45007200.ac.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
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
/*
* [146] LRU Cache
*
* https://leetcode.com/problems/lru-cache
*
* Hard (17.36%)
* Total Accepted:
* Total Submissions:
* Testcase Example: '["LRUCache","put","put","get","put","get","put","get","get","get"]\n[[2],[1,1],[2,2],[1],[3,3],[2],[4,4],[1],[3],[4]]'
*
*
* Design and implement a data structure for Least Recently Used (LRU) cache.
* It should support the following operations: get and put.
*
*
*
* get(key) - Get the value (will always be positive) of the key if the key
* exists in the cache, otherwise return -1.
* put(key, value) - Set or insert the value if the key is not already present.
* When the cache reached its capacity, it should invalidate the least recently
* used item before inserting a new item.
*
*
* Follow up:
* Could you do both operations in O(1) time complexity?
*
* Example:
*
* LRUCache cache = new LRUCache( 2 /* capacity */ );
*
* cache.put(1, 1);
* cache.put(2, 2);
* cache.get(1); // returns 1
* cache.put(3, 3); // evicts key 2
* cache.get(2); // returns -1 (not found)
* cache.put(4, 4); // evicts key 1
* cache.get(1); // returns -1 (not found)
* cache.get(3); // returns 3
* cache.get(4); // returns 4
*
*
*/
#include<map>
#include<algorithm>
#include<climits>
#include<cassert>
using namespace std;
class LRUCache{
struct LRULine;
struct CacheLine
{
LRULine* lru_line_ptr;
int val;
};
struct LRULine
{
CacheLine* cache_line_ptr;
int counter;
int key;
};
int counter = INT_MIN;
map<int,CacheLine> cache;
map<int,LRULine> lru;
int capacity = 0;
public:
LRUCache(int capacity) {
this->capacity = capacity;
}
inline void update_lru(LRULine* lru_line)
{
auto iter = lru.find(lru_line->counter);
assert(iter != lru.end());
auto newline = *lru_line;
newline.counter = ++counter;
auto new_iter = lru.insert(make_pair(newline.counter, newline));
lru.erase(iter);
newline.cache_line_ptr->lru_line_ptr = &new_iter.first->second;
}
inline void swap_out()
{
if (cache.size() < capacity)
{
return;
}
auto iter = lru.begin();
auto num = cache.erase(iter->second.key);
assert(num > 0);
lru.erase(iter);
}
int get(int key) {
int res = -1;
try
{
auto line = cache.at(key);
int val = line.val;
update_lru(line.lru_line_ptr);
res = val;
}
catch(out_of_range& e)
{
}
catch(...)
{
}
return res;
}
void set(int key, int value) {
try
{
auto& line = cache.at(key);
line.val = value;
update_lru(line.lru_line_ptr);
}
catch(out_of_range& e)
{
swap_out();
LRULine lru_line = {
nullptr,
++counter,
key,
};
auto lru_iter = lru.insert(make_pair(lru_line.counter, lru_line));
CacheLine cache_line = {
&(lru_iter.first->second),
value,
};
auto cache_iter = cache.insert(make_pair(key,cache_line));
lru_iter.first->second.cache_line_ptr = &cache_iter.first->second;
return;
}
catch(...)
{
return;
}
return;
}
};