-
Notifications
You must be signed in to change notification settings - Fork 168
/
LRU_cache.cpp
105 lines (95 loc) · 3.13 KB
/
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
/*
Problem Link: https://leetcode.com/problems/lru-cache/
*/
// Doubly linked list node
class Node {
public:
int key;
int value;
Node *next;
Node *prev;
// param ctor
Node(int key, int value) {
this->key = key;
this->value = value;
}
};
class LRUCache {
/*
NOTATION: The nodes from left to right are the latest used ones
The nearest node to the tail ie. the right-most node is the least recently used one
*/
Node *head = new Node(-1, -1); // dummy head node
Node *tail = new Node(-1, -1); // dummy tail node
int capacity; // total size of the cache allowed
// indicates the location address of the nodes present in the cache
unordered_map<int, Node*> m; // {key -> node pointer}
public:
LRUCache(int capacity) {
this->capacity = capacity;
// connect head with tail and vice-versa
head->next = tail;
tail->prev = head;
}
// helper function to add a node just after the head node
void addNode(Node *node) {
node->next = head->next;
node->prev = head;
head->next = node;
node->next->prev = node;
}
// helper function to delete node from the tail node
void deleteNode(Node *node) {
Node *prev_node = node->prev;
prev_node->next = node->next;
node->next->prev = prev_node;
}
int get(int key) {
// check if the node exists in the cache
if(m.find(key) != m.end()) {
Node *result_node = m[key];
int result_val = result_node->value;
// now this node becomes the latest used node
// delete the entry from cache ie. unordered_map
m.erase(key);
// delete the node from DLL
deleteNode(result_node);
// add the node after the head
addNode(result_node);
// add the entry {with new node address} in the cache ie. unordered_map
m[key] = head->next;
return result_val;
}
// the key doesn't exist in the cache
return -1;
}
void put(int key, int value) {
// check if node exists in the cache: delete it since we have to insert it again
if(m.find(key) != m.end()) {
Node *result_node = m[key];
// delete the entry from cache
m.erase(key);
// delete the node from DLL
deleteNode(result_node);
}
// remove the last frequently used node if capacity is at max
if(m.size() == capacity) {
// remove the least recently used node
// ie. the node closest to the tail (right-most node)
// delete the entry from cache
m.erase(tail->prev->key);
// delete the node from DLL
deleteNode(tail->prev);
}
// add the node in DLL
addNode(new Node(key, value));
// add the entry in cache
m[key] = head->next;
}
};
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache* obj = new LRUCache(capacity);
* int param_1 = obj->get(key);
* obj->put(key,value);
*/