-
Notifications
You must be signed in to change notification settings - Fork 0
/
LRUCache.cpp
157 lines (139 loc) · 3.73 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
// LRU Cache
// The task is to design and implement methods of an LRU cache. The class has two methods get() and set() which are defined as follows.
// get(x) : Returns the value of the key x if the key exists in the cache otherwise returns -1.
// set(x,y) : inserts the value if the key x is not already present. If the cache reaches its capacity it should invalidate the least recently used item before inserting the new item.
// In the constructor of the class the size of the cache should be intitialized.
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// design the class in the most optimal way
struct Node {
int key;
int value;
Node *next, *pre;
Node(int key, int value) {
this->key = key;
this->value = value;
next = pre = NULL;
}
};
class LRUCache
{
private:
static unordered_map<int, Node *> hsmap;
static int capacity, count;
static Node *head, *tail;
public:
//Constructor for initializing the cache capacity with the given value.
LRUCache(int cap)
{
unordered_map<int, Node *> temp;
hsmap = temp;
capacity = cap;
head = new Node(0, 0);
tail = new Node(0, 0);
head->next = tail;
head->pre = NULL;
tail->next = NULL;
tail->pre = head;
count = 0;
}
static void addToHead(Node *node)
{
node->next = head->next;
node->next->pre = node;
node->pre = head;
head->next = node;
}
//Function to delete a node.
static void deleteNode(Node *node)
{
node->pre->next = node->next;
node->next->pre = node->pre;
}
//Function to return value corresponding to the key.
static int get(int key)
{
//if element is present in map,
if (hsmap.count(key) > 0)
{
Node *node = hsmap[key];
int result = node->value;
deleteNode(node);
addToHead(node);
//returning the value.
return result;
}
//else we return -1.
return -1;
}
//Function for storing key-value pair.
static void set(int key, int value)
{
if (hsmap.count(key) > 0)
{
Node *node = hsmap[key];
node->value = value;
deleteNode(node);
addToHead(node);
}
else
{
Node *node = new Node(key, value);
hsmap[key] = node;
if (count < capacity) {
count++;
addToHead(node);
}
else {
hsmap.erase(tail->pre->key);
deleteNode(tail->pre);
addToHead(node);
}
}
}
};
//initializing static members.
unordered_map<int, Node *> temp;
int LRUCache::capacity = 0;
Node *LRUCache::head = new Node(0, 0);
Node *LRUCache::tail = new Node(0, 0);
int LRUCache::count = 0;
unordered_map<int, Node *> LRUCache::hsmap = temp;
// { Driver Code Starts.
int main()
{
int t;
cin >> t;
while (t--)
{
int capacity;
cin >> capacity;
LRUCache *cache = new LRUCache(capacity);
int queries;
cin >> queries;
while (queries--)
{
string q;
cin >> q;
if (q == "SET")
{
int key;
cin >> key;
int value;
cin >> value;
cache->set(key, value);
}
else
{
int key;
cin >> key;
cout << cache->get(key) << " ";
}
}
cout << endl;
}
return 0;
}
// } Driver Code Ends