-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource_Code.cpp
More file actions
150 lines (120 loc) · 3.92 KB
/
Copy pathSource_Code.cpp
File metadata and controls
150 lines (120 loc) · 3.92 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
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
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
//Create double linked list with key and value
struct Node{
int key,val;
Node* prev;
Node* next;
Node(int k,int v){
key=k;
val=v;
prev=NULL;
next=NULL;
}
};
//making class for lru cache
class LRU_Cache{
private:
int capacity;
unordered_map<int,Node*> mp;
Node* head;
Node* tail;
//delete function to delete the node which we want to
void Delete_Node(Node* node){
node->prev->next=node->next;
node->next->prev=node->prev;
}
//Funtion to add node after head node
void Add_front(Node* node){
head->next->prev=node;
node->next=head->next;
head->next=node;
node->prev=head;
}
public:
//Constructor with the value of capactiy of the LRU Cache object
LRU_Cache(int cap){
capacity=cap;
head=new Node(0,0);
tail=new Node(0,0);
head->next=tail;
tail->prev=head;
}
//put function with key and value inputs that inserts the key and value in LRU cache
void put(int key,int value){
if(mp.find(key)!=mp.end()){
Node* temp=mp[key];
temp->val=value;
Delete_Node(temp);
Add_front(temp);
}
else{
Node* temp=new Node(key,value);
if((int)mp.size()<capacity){
mp[key]=temp;
Add_front(temp);
}
else{
mp.erase(tail->prev->key);
Delete_Node(tail->prev);
delete tail->prev;
Add_front(temp);
mp[key]=temp;
}
}
}
//function that returns value is the key is present else returns -1
int get(int key){
if(mp.find(key)!=mp.end()){
Node* temp=mp[key];
Delete_Node(temp);
Add_front(temp);
return temp->val;
}
else{
return -1;
}
}
//destructor that wll help to destroy any object created
~LRU_Cache() {
Node* temp=head;
while(temp){
Node* next=temp->next;
delete temp;
temp=next;
}
}
};
int main() {
fastio
LRU_Cache cache(3); // capacity = 3
cout << "Inserting (101, 5001)" << endl;
cache.put(101, 5001);
cout << "Inserting (202, 6002)" << endl;
cache.put(202, 6002);
cout << "Inserting (303, 7003)" << endl;
cache.put(303, 7003);
cout << "Accessing key 101 -> Value = " << cache.get(101) << endl; // Expected: 5001
cout << "Inserting (404, 8004) -> should evict LRU (202)" << endl;
cache.put(404, 8004);
cout << "Accessing key 202 -> Value = " << cache.get(202) << " (Expected: -1, since evicted)" << endl;
cout << "Overwriting key 303 with new value 9999" << endl;
cache.put(303, 9999);
cout << "Accessing key 303 -> Value = " << cache.get(303) << " (Expected: 9999)" << endl;
cout << "Inserting (505, 10005) -> should evict LRU (404)" << endl;
cache.put(505, 10005);
cout << "Accessing key 404 -> Value = " << cache.get(404) << " (Expected: -1, since evicted)" << endl;
cout << "Accessing key 101 -> Value = " << cache.get(101) << " (Expected: 5001)" << endl;
cout << "Accessing key 505 -> Value = " << cache.get(505) << " (Expected: 10005)" << endl;
cout << "\n Testing edge case with capacity = 1\n" << endl;
LRU_Cache single(1);
cout << "Inserting (888, 8888)" << endl;
single.put(888, 8888);
cout << "Accessing key 888 -> Value = " << single.get(888) << endl;
cout << "Inserting (999, 9999) -> should evict 888" << endl;
single.put(999, 9999);
cout << "Accessing key 888 -> Value = " << single.get(888) << " (Expected: -1, evicted)" << endl;
cout << "Accessing key 999 -> Value = " << single.get(999) << endl;
return 0;
}