-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlrucache.cpp
More file actions
118 lines (111 loc) · 2.76 KB
/
lrucache.cpp
File metadata and controls
118 lines (111 loc) · 2.76 KB
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
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <set>
#include <cassert>
using namespace std;
struct Node{
Node* next;
Node* prev;
int value;
int key;
Node(Node* p, Node* n, int k, int val):prev(p),next(n),key(k),value(val){};
Node(int k, int val):prev(NULL),next(NULL),key(k),value(val){};
};
class Cache{
protected:
map<int,Node*> mp; //map the key to the node in the linked list
int cp; //capacity
Node* tail; // double linked list tail pointer
Node* head; // double linked list head pointer
virtual void set(int, int) = 0; //set function
virtual int get(int) = 0; //get function
};
class LRUCache: public Cache {
public: LRUCache(int capacity);
//~LRUCache();
// Node* tail; // double linked list tail pointer
// Node* head; // double linked list head pointer
void set(int,int);
int get(int);
};
LRUCache::LRUCache(int capacity){
cp = capacity;
head = new Node(0,0);
tail = new Node(0,0);
head->next = tail;
// head->prev=NULL;
tail->prev=head;
// tail->next=NULL;
}
void LRUCache::set(int key,int value){
map<int,Node*>::iterator it = mp.find(key);
if (mp.size()==0){
Node *present= new Node(head,tail,key,value);
head->next = present;
tail->prev = present;
mp[key] = present;
}
else {
if(it!=mp.end()){
Node *present = it->second;
present->prev->next = present->next;
present->next->prev = present->prev;
present->next = head->next;
head->next->prev = present;
head->next = present;
present->prev = head;
present->value = value;
}
else{
if(mp.size() < cp){
Node *present = new Node(head,head->next,key,value);
head->next->prev = present;
head->next = present;
mp[key] = present;
}
else{
map<int,Node*>::iterator del_node = mp.find(tail->prev->key);
tail->prev = tail->prev->prev;
Node *present= new Node(head,head->next,key,value);
head->next->prev = present;
head->next = present;
mp[key] = present;
delete del_node->second;
mp.erase(del_node);
}
}
}
}
int LRUCache::get(int key){
int value=-1;
map<int,Node*>::iterator it;
it = mp.find(key);
if(it != mp.end()){
value = it->second->value;
set(key,value);
}
return value;
}
int main() {
int n, capacity,i;
cin >> n >> capacity;
LRUCache l(capacity);
for(i=0;i<n;i++) {
string command;
cin >> command;
if(command == "get") {
int key;
cin >> key;
cout << l.get(key) << endl;
}
else if(command == "set") {
int key, value;
cin >> key >> value;
l.set(key,value);
}
}
return 0;
}