-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlru_caching.cpp
79 lines (59 loc) · 1.37 KB
/
lru_caching.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
#include<iostream>
#include<stack>
#include<set>
#include<vector>
#include<list>
#include <unordered_map>
using namespace std;
class lru_cache {
private:
int capacity;
int current_size;
int page_faults;
list<int> cache;
unordered_map<int, list<int>::iterator> cache_map;
public :
lru_cache(int size) {
this->capacity = size;
this->current_size = 0;
this->page_faults = 0;
}
void add(int job);
void display();
bool isempty();
int get_faults();
};
int lru_cache::get_faults() {
return page_faults;
}
bool lru_cache::isempty() {
return current_size != capacity;
}
void lru_cache::add(int job) {
if(cache_map.find(job) == cache_map.end()) {
page_faults++;
if(cache.size() == capacity) {
int last_job = cache.back();
cache.pop_back();
cache_map.erase(last_job);
}
}
else
cache.erase(cache_map[job]);
cache.push_front(job);
cache_map[job] = cache.begin();
}
void lru_cache::display(){
for(auto job = cache.begin(); job != cache.end(); job++)
cout << *job << "\t";
}
int main() {
vector<int> v = {1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5};
lru_cache lc(5);
for(auto job : v)
lc.add(job);
lc.display();
cout << "\n";
cout << lc.get_faults();
return 0;
}