-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptimal.cpp
More file actions
127 lines (111 loc) · 3.14 KB
/
optimal.cpp
File metadata and controls
127 lines (111 loc) · 3.14 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
#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<limits.h>
using namespace std;
typedef vector<pair<int,int> >::iterator vit;
bool foo_neg(const pair<int,int> &a)
{
return ((a.second < 0)==true);
}
bool foo2(const pair<int,int> &a,const pair<int,int> &b)
{
return ((a.second < b.second)==true);
}
bool v_search(vector<pair<int,int> > v3,int item)
{
for(vit it=v3.begin() ; it!=v3.end() ; it++)
{
if(it->first==item)
return true;
else
continue;
}
return false;
}
vit v_search2(vector<pair<int,int> > &v5,int item)
{
for(vit it=v5.begin() ; it!=v5.end() ; it++)
{
if(it->first==item)
return it;
}
}
int main(){
int page_fault=0;
vector<pair<int,int> > v(3);
for(int i=0;i<v.size();i++)
v[i]=make_pair(-1,-1);
int str[]={7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1};
int x=sizeof(str)/sizeof(str[0]);
vector<int> tars(str,str+x);
cout<<"\nReference String is: ";
for(int i=0;i<tars.size();i++)
cout<<tars[i]<<" ";
cout<<"\n\n";
for(int i=0;i<tars.size();i++)
{
if(v_search(v,tars[i]))
{
if(i<tars.size()-1 && find(tars.begin()+i+1,tars.end(),tars[i])!=tars.end())
{
int index=find(tars.begin()+i+1,tars.end(),tars[i])-tars.begin();
vit it = v_search2(v,tars[i]); //here we use v_search2();
it->second=index;
cout<<".\n";
continue;
}
else
{
vit it = v_search2(v,tars[i]); //here we use v_search2();
it->second=INT_MAX;
cout<<".\n";
continue;
}
}
else
{
if(i<tars.size()-1 && find(tars.begin()+i+1,tars.end(),tars[i])!=tars.end())
{
int index=find(tars.begin()+i+1,tars.end(),tars[i])-tars.begin();
if(find_if(v.begin(),v.end(),foo_neg)!=v.end()) //this condition is only for initial three vector v fill-up
{
vit it = find_if (v.begin(), v.end(), foo_neg);
*it=make_pair(tars[i],index);
page_fault++;
for(int j=0;j<v.size() && v[j].first!=-1;j++)
cout<<v[j].first<<" ";
}
else{
vit it = max_element (v.begin(), v.end(), foo2);
*it=make_pair(tars[i],index);
page_fault++;
for(int j=0;j<v.size() && v[j].first!=-1;j++)
cout<<v[j].first<<" ";
}
}
else
{
if(find_if(v.begin(),v.end(),foo_neg)!=v.end()) //this condition is only for initial three vector v fill-up
{
vit it = find_if (v.begin(), v.end(), foo_neg);
*it=make_pair(tars[i],INT_MAX);
page_fault++;
for(int j=0;j<v.size() && v[j].first!=-1;j++)
cout<<v[j].first<<" ";
}
else
{
vit it = max_element (v.begin(), v.end(), foo2);
*it=make_pair(tars[i],INT_MAX);
page_fault++;
for(int j=0;j<v.size() && v[j].first!=-1;j++)
cout<<v[j].first<<" ";
}
}
}
cout<<"\n";
}
cout<<"\nTotal page fault is: "<<page_fault<<"\n\n";
}