-
Notifications
You must be signed in to change notification settings - Fork 0
/
lru_counter.cpp
76 lines (67 loc) · 1.1 KB
/
lru_counter.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
#include<bits/stdc++.h>
using namespace std;
int main()
{
int f,n;
cout<<"Enter number of frames\n";
cin>>f;
cout<<"Enter length of string\n";
cin>>n;
int string[n];
vector<int> cache, temp;
vector<int> counter;
cout<<"Enter the string\n";
for(int i=0; i<n; i++)
{
cin>>string[i];
}
int it, time=1;
for(int i=0; i<n; i++)
{
bool flag = true;
for(int j=0; j<cache.size(); j++)
{
if(cache[j] == string[i])
{
flag = false;
it = j;
break;
}
}
if(flag == true)
{
int min=9999,itr;
if(cache.size() < f)
{
cache.push_back(string[i]);
counter.push_back(time);
}
else
{
for(int k=0; k<cache.size(); k++)
{
if(counter[k] < min)
{
min = counter[k];
itr = k;
}
}
cache.erase(cache.begin() + itr);
counter.erase(counter.begin() + itr);
cache.push_back(string[i]);
counter.push_back(time);
}
}
if(flag == false)
{
counter[it] = time;
}
time++;
for(int k=0; k<cache.size(); k++)
{
cout<<cache[k]<<"("<<counter[k]<<")"<<" ";
}
cout<<endl;
}
return 0;
}