-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.cc
72 lines (63 loc) · 1.55 KB
/
cache.cc
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
#include <unordered_map>
class Node {
public:
int K, V;
Node* next;
Node* prev;
Node(int key, int val) : K(key), V(val), next(nullptr), prev(nullptr){}
};
class LRUcache {
private:
std::unordered_map<int, Node*> cache;
int capacity;
Node* head;
Node* tail;
void removeNode(Node* node) {
Node* P = node->prev;
Node* N = node->next;
P->next = N;
N->prev = P;
}
void insertFront(Node* node) {
node->next = head->next;
node->prev = head;
head->next->prev = node;
head->next = node;
}
void moveToFront(Node* node){
removeNode(node);
insertFront(node);
}
public:
LRUcache(int cap): capacity(cap) {
head = new Node(0, 0);
tail = new Node(0, 0);
head->next = tail;
tail->prev = head;
}
int get(int key) {
if(cache.find(key) != cache.end()) {
Node* node = cache[key];
moveToFront(node);
return node->V;
}
return -1;
}
int put(int k, int v) {
if(cache.find(k) != cache.end()){
Node* node = cache[k];
node->V = v;
moveToFront(node);
} else {
if(cache.size() == capacity){
Node* tailP = tail->prev;
removeNode(tailP);
cache.erase(tailP->K);
delete tailP;
}
Node* newNode = new Node(k, v);
cache[k] = newNode;
insertFront(newNode);
}
}
};