-
Notifications
You must be signed in to change notification settings - Fork 0
/
lru.js
174 lines (167 loc) · 5.11 KB
/
lru.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
class node {
constructor(data) {
this.data = data;
this.l = null;
this.r = null;
}
}
class Dequeue {
constructor() {
this.left = null;
this.right = null;
this.size = 0;
}
addFront(x) {
let newNode = new node(x);
newNode.r = this.left;
if (this.left != null)
this.left.l = newNode;
else {
this.right = newNode;
}
this.left = newNode;
this.size++;
}
deleteLast() {
let tmp = this.right;
this.right = tmp.l;
this.right.r = null;
this.size--;
}
delete(x) {
if (x == null) return;
//first element
if (x == this.left && x == this.right) {
this.left = null;
this.right = null;
this.size--;
return;
}
if (x == this.left) {
this.left = x.r;
x.r.l = null;
this.size--;
return;
}
if (x == this.right) {
this.right = x.l;
this.right.r = null;
this.size--;
return;
}
x.l.r = x.r;
x.r.l = x.l;
this.size--;
}
display() {
let fr = [];
let temp = this.left;
while (temp != null) {
fr.push(temp.data);
temp = temp.r;
}
return fr;
}
}
class lru {
constructor(capacity) {
this.capacity = capacity;
this.pageFaults = 0;
this.pageHits = 0;
this.searchIndex = -1;
this.map = new Map();
this.dequeue = new Dequeue();
}
refer(token) {
if (this.map.has(token)) {
this.pageHits++;
this.searchIndex = 0;
this.dequeue.delete(this.map.get(token));
this.map.delete(token);
} else {
this.pageFaults++;
this.searchIndex = -1;
if (this.capacity == this.dequeue.size) {
this.dequeue.deleteLast();
this.map.delete(this.frames[this.frames.length - 1]);
}
}
this.dequeue.addFront(token);
this.map.set(token, this.dequeue.left);
this.frames = this.dequeue.display();
}
}
document.getElementById("submit").addEventListener("click", LRU);
function LRU() {
let res = document.getElementById("input").value.split(" ");
let frames = document.getElementById("frames").value;
let ref = [];
if (res.length > 16) {
alert(" please Enter maximum 16 number ")
} else {
for (let i = 0; i < res.length; i++) {
if (res[i] != " " && res[i] != "") {
ref.push(res[i]);
}
}
createTable("table", frames, ref);
let obj = new lru(frames);
for (let i = 0; i < ref.length; i++) {
obj.refer(ref[i]);
fillcol("table", i + 1, obj.frames, frames, obj.searchIndex);
}
summary('summary', obj.pageFaults, obj.pageFaults + obj.pageHits, frames);
}
}
function fillcol(tablename, col, objframes, n, searchindex) {
for (let i = 1; i <= objframes.length; i++) {
let cell = document.getElementById(tablename + i + '' + col);
cell.innerHTML = objframes[i - 1];
}
n++;
let cell = document.getElementById(tablename + n + '' + col);
if (searchindex == -1) cell.innerHTML = "MISS";
else {
// console.log(tablename+n+''+col);
cell.innerHTML = "HIT";
document.getElementById(tablename + (searchindex + 1) + '' + col).classList.add("bg-success", "text-white");
}
}
function createTable(tablename, frames, ref) {
document.getElementById(tablename).innerHTML = "";
let table = '<tr class="font-weight-bold"><td id="' + tablename + '00">Reference</td>';
for (let i = 0; i < ref.length; i++) {
table += '<td id="' + tablename + '0' + (i + 1) + '">' + ref[i] + "</td>";
}
table += "</tr>";
for (let i = 0; i < frames; i++) {
table += '<tr><td class="font-weight-bold" id="' + tablename + (i + 1) + '0">Frame ' + (i + 1) + "</td>";
for (let j = 0; j < ref.length; j++) {
table += '<td id="' + tablename + (i + 1) + (j + 1) + '"></td>';
}
table += "</tr>";
}
frames++;
table += '<tr><td class="font-weight-bold" id="' + tablename + frames + '0">Status</td>';
for (var j = 1; j <= ref.length; j++) {
table += '<td id="' + tablename + frames + j + '"></td>';
}
table += "</tr>";
console.log(table);
document.getElementById(tablename).innerHTML += table;
}
function summary(id, pagefaults, pages, frames) {
let summary = "";
let missratio = (pagefaults / pages).toPrecision(2);
let hitratio = (1 - missratio).toPrecision(2);
summary += `<div>Pages:${pages}</div>`;
summary += `<div>Frames:${frames}</div>`;
summary += `<div>Hits:${pages-pagefaults}</div>`;
summary += `<div>Faults:${pagefaults}</div>`;
summary += `<div>Hit Ratio:${hitratio}</div>`;
summary += `<div>Miss Ratio:${missratio}</div>`;
document.getElementById(id).innerHTML = summary;
}
function reset() {
location.reload();
}