-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptimal.cpp
More file actions
92 lines (85 loc) · 2.91 KB
/
Optimal.cpp
File metadata and controls
92 lines (85 loc) · 2.91 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
#include "Page.cpp"
#include <iostream>
#include <time.h>
using namespace std;
/** @author Jared Baker
@date 12/01/2014
Implements Optimal algorithm used in virtual page replacement. The algorythm looks into the page's future to decide what will be used
-- OPTTableSize : value to keep track of the page table's size
-- OPTTable : pointer to an Page object; used in construction of page tables
-- func
*/
class OPT {
private:
int OPTTableSize;
int loc;
int g;
int StringSize;
Page * OPTTable;
Page * OPTTableString;
public:
/** Constructor to create a new Optimal page table using the Page class.
*/
OPT( int size, int fsize){
OPTTableSize = size;
OPTTable = new Page[size];
OPTTableString = new Page[fsize];
loc = 0;
g = 0;
StringSize = 0;
}
int Value [10]; //holds value distances for table from current location.
int getSize() { return OPTTableSize; }
/** Compares each value with similar values ahead of its current location. This returns
that value that is furthest from the current. That value is to be deleted.
*/
int getFurthest(void){
int furthestaway; //keeps track of current index of the furthest page
for (int i = 0; i < getSize(); i ++) {
for (int z = loc + 1; z < StringSize; z ++) {
//compares the value
if (OPTTable[i].getName() == OPTTableString[z].getName()) { //I need to compare with each value that is currently ahead of its location. On the list. Is there a simple way of doing this?
Value[i] = z-loc; //If positive, this should store how far the next similar value is from current.
break;
}
else{
Value[i] = StringSize + 1; //If no value matches, returns the highest possible size value. Thus it will be replaced.
}
}
}
furthestaway = 0;
//Compare each value in the array and then figure out which one is furthest away. It will then return the one that is and replace it.
for (int i = 0; i < getSize(); i ++){
if (Value[furthestaway] < Value[i]){
furthestaway = i;
}
}
return furthestaway;
}
void getstringsize(int string) {
StringSize = string;
}
void stringpage(char page){
for (int i = g; i < StringSize; i ++){
OPTTableString[i].setName(page);
break;
}
g = g + 1;
}
bool checkForPage(char page, int pos){
loc = pos;
for (int i = 0; i < getSize(); i ++){
if(OPTTable[i].isSet() != true){ //checks whether the page was set or not
OPTTable[i].setName(page); //sets name of the page at the table's current index to the name of the page replacing it
return false;
}
if (OPTTable[i].getName() == page){
return true;
}
}
int furthest = getFurthest();
//changes new index for furthest page, sets value using getFurthest method
OPTTable[furthest].setName(page); //replaces furthest page with the needed page
return false;
}
};