-
Notifications
You must be signed in to change notification settings - Fork 0
/
lrucache.cpp
183 lines (158 loc) · 3.79 KB
/
lrucache.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
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
#include <iostream>
#include <unordered_map>
#include <climits>
using namespace std;
struct CacheNode
{
int key;
int value;
CacheNode *prev;
CacheNode *next;
CacheNode(int k, int v)
{
key = k;
value = v;
prev = NULL;
next = NULL;
}
};
class LRUCache{
private:
int capacity;
CacheNode *head;
CacheNode *tail;
unordered_map<int, CacheNode *> cacheMap;
public:
// @param capacity, an integer
LRUCache(int _capacity) {
this->capacity = _capacity;
this->head = NULL;
this->tail = NULL;
}
// @return an integer
int get(int key) {
if(cacheMap.find(key) == cacheMap.end())
{
return INT_MIN;
}
CacheNode *node = cacheMap[key];
removeNode(node);
updateHead(node);
return node->value;
}
// @param key, an integer
// @param value, an integer
// @return nothing
void set(int key, int value) {
CacheNode *node;
if(cacheMap.find(key) == cacheMap.end())
{
node = new CacheNode(key, value);
if(cacheMap.size() == capacity)
{
cacheMap.erase(tail->key);
tail = tail->prev;
if(tail != NULL)
{
tail->next = NULL;
}
}
cacheMap[key] = node;
}
else
{
node = cacheMap[key];
removeNode(node);
node->value = value;
}
updateHead(node);
}
// @param node, a cache node to be removed
// @return nothing
void removeNode(CacheNode *node)
{
CacheNode *prev = node->prev;
CacheNode *next = node->next;
if(prev != NULL)
{
prev->next = next;
}
else
{
head = next;
}
if(next != NULL)
{
next->prev = prev;
}
else
{
tail = prev;
}
}
// @param node, a cache node to be updated to head position
// @return nothing
void updateHead(CacheNode *node)
{
node->next = head;
node->prev = NULL;
if(head != NULL)
{
head->prev = node;
}
head = node;
if(tail == NULL)
{
tail = head;
}
}
};
int main()
{
LRUCache lru(4);
cout << "set 7 28" << endl;
lru.set(7, 28);
cout << "Press any key to continue " << endl;
getchar();
cout << "set(7, 1): " << endl;
lru.set(7, 1);
cout << "Press any key to continue " << endl;
getchar();
cout << "get(7) -> " ;
cout << lru.get(7) << endl;
cout << "Press any key to continue " << endl;
getchar();
cout << "set(8, 15): " << endl;
lru.set(8, 15);
cout << "Press any key to continue " << endl;
getchar();
cout << "get(6) -> " ;
cout << lru.get(6) << endl;
cout << "Press any key to continue " << endl;
getchar();
cout << "set(10, 27): " << endl;
lru.set(10, 27);
cout << "Press any key to continue " << endl;
getchar();
cout << "set(8, 10): " << endl;
lru.set(8, 10);
cout << "Press any key to continue " << endl;
getchar();
cout << "get(8) -> " ;
cout << lru.get(8) << endl;
cout << "Press any key to continue " << endl;
getchar();
cout << "get(6) -> " ;
cout << lru.get(6) << endl;
cout << "Press any key to continue " << endl;
getchar();
cout << "set(6, 29): " << endl;
lru.set(6, 29);
cout << "Press any key to continue " << endl;
getchar();
cout << "get(6) -> " ;
cout << lru.get(6) << endl;
cout << "Press any key to continue " << endl;
getchar();
return 0;
}