-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptimal.java
More file actions
63 lines (50 loc) · 1.31 KB
/
Copy pathOptimal.java
File metadata and controls
63 lines (50 loc) · 1.31 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
import java.util.Arrays;
import java.util.Stack;
public class Optimal {
Pages pages;
ReferenceString str;
int optCount[];
public Optimal(Pages pages) {
this.pages = pages;
}
public int simulate(ReferenceString str) {
this.str = str;
for (int i = 0; i < str.size(); i++)
if (!pages.hasSpace() && !pages.contains(str.get(i))) {
pages.remove(findLRU(str, i));
pages.add(str.get(i));
}
else if (pages.hasSpace() && !pages.contains(str.get(i))) {
pages.add(str.get(i));
}
return pages.pageFaults;
}
public int findLRU(ReferenceString str, int start) {
optCount = new int[pages.getRSS()];
int opt = -1;
for (int page = 0; page < optCount.length; page++) {
for (int pos = start; pos < str.size(); pos++)
if (str.get(pos) == pages.get(page)) {
optCount[page] = pos;
break;
}
if (optCount[page] == 0)
optCount[page] = Integer.MAX_VALUE;
}
for (int i = 0, max = Integer.MIN_VALUE; i < optCount.length; i++)
if (max < optCount[i]) {
max = optCount[i];
opt = i;
}
return pages.get(opt);
}
public void print() {
pages.print();
}
public static void main(String[]args) {
Optimal optimal = new Optimal(new Pages(3));
ReferenceString str = new ReferenceString(.9, 10000);
str.fillRandomly();
System.out.println(optimal.simulate(str));
}
}