-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlru_cache.cpp
78 lines (70 loc) · 1.21 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
#include<iostream>
#include<list>
#include<map>
#include<iterator>
using namespace std;
class Element
{
public:
int key;
int value;
Element(int k, int v)
{
this->key = k;
this->value = v;
}
};
class LRU
{
public:
list<Element> q;
map<int, list<Element>::iterator > hash;
int size;
LRU(int size)
{
this->size = size;
}
void set(int x, int y)
{
// if element already in list then move it to the back of the queue
if(hash.find(x) != hash.end())
{
q.erase(hash[x]);
}
// if list (cache size) is full then remove the least recently used element .ie. the element in front of the queue
else if(q.size() == this->size)
{
hash.erase(hash.find((*(q.begin())).key));
q.erase(q.begin());
}
q.push_back(Element(x,y));
hash[x] = --q.end();
}
int get(int x)
{
if(hash.find(x) == hash.end())
return -1;
return (*(hash[x])).value;
}
void print_cache()
{
for(list<Element>::iterator it = q.begin(); it != q.end(); it++)
{
cout << (*(it)).value << " ";
}
cout << endl;
}
};
int main()
{
LRU cache(4);
cache.set(1,1);
cache.set(2,2);
cache.set(3,3);
cache.print_cache();
cache.set(1,1);
cache.print_cache();
cache.set(4,4);
cache.set(5,5);
cache.print_cache();
}