-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNFU.cpp
More file actions
91 lines (81 loc) · 2.4 KB
/
NFU.cpp
File metadata and controls
91 lines (81 loc) · 2.4 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
#include <iostream>
#include "Page.cpp"
using namespace std;
class NFU {
private:
int nfuTableSize;
int leastRecentPage;
int count;
int locat;
int g;
int stringS;
Page * nfuTable;
Page * nfuTableString;
public:
int Value[5];
NFU(int size, int fsize) {
nfuTableSize = size;
nfuTable = new Page[size];
nfuTableString = new Page[fsize];
count = 0;
g = 0;
}
int getSize() {return nfuTableSize;}
int getLeastRecentPage(void) {
int leastRecent; //keeps track of current index of the page that occurs the least
for (int i = 0; i < getSize(); i ++) {
count = 0;
//cout << "location " << locat << endl;
for (int z = 0; z < locat; z ++) {
//compares the value
//cout << "nfuTable[i].getName() " << nfuTable[i].getName() << " nfuTableString[z].getName() " << nfuTableString[z].getName() << endl;
if (nfuTable[i].getName() == nfuTableString[z].getName()) {
count = count + 1;
Value[i] = count;
}
}
// cout << nfuTable[i].getName() << " has " << Value[i] << endl;
}
//Compare each value in the array and then figure out which one occurs the least often. It will then return the one that is and replace it.
for (int i = 1; i < getSize(); i ++){
leastRecent = 0;
if (Value[leastRecent] < Value[i]){
leastRecent = i;
}
}
return leastRecent;
}//leastrecent return
void getLocation(int location) {
locat = location;
}
void getStringSize(int string) {
stringS = string;
}
void stringPage(char page) {
for(int i = g; i < stringS; i++) {
nfuTableString[i].setName(page);
break;
}
g = g + 1;
}
bool checkForPage(char page) {
int current;
int spot;
for(current = 0; current < getSize(); current++) {
if(nfuTable[current].isSet() != true){ //checks whether the page was set or not
// cout << "Empty page at " << current << " adding page " << page << endl;
nfuTable[current].setName(page); //sets name of the page at the table's current index to the name of the page replacing it
return false;
}
if (nfuTable[current].getName() == page){
// cout << "Found page " << page << " at pos " << current << endl;
return true;
}
}
// cout << "Page fault looking for: " << page << endl;
//changes new index for furthest page, sets value using getFurthest method
nfuTable[getLeastRecentPage()].setName(page); //replaces furthest page with the needed page
// cout << "Added " << page << " to " << getLeastRecentPage() << " : " << nfuTable[getLeastRecentPage()].getName() << endl;
return false;
}
};