-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
116 lines (99 loc) · 2.64 KB
/
Copy pathscript.js
File metadata and controls
116 lines (99 loc) · 2.64 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
var cap = document.getElementById('cap');
var list = document.getElementById('list');
var text = document.getElementById('text');
var add = document.getElementById('add');
var fifo = document.getElementById('fifo');
var faults = document.getElementById('faults');
var hits = document.getElementById('hits');
var hit=0;
let page_faults = 0;
var indexes = [];
let s = new Set();
var i =0;
class Fifo extends Array {
constructor(...elem) {
super(...elem);
}
display_elements(s2) {
for (let i = 0; i < s2.length; i++) {
var x = document.createElement('li');
var t = document.createTextNode(s2[i]);
x.appendChild(t);
list.appendChild(x);
}
}
}
function insertAt(array, index, ...elementsArray) {
array.splice(index, 0, ...elementsArray);
}
let removeElement = (array, n) => {
let newArray = [];
for (let i = 0; i < array.length; i++) {
if (array[i] !== n) {
newArray.push(array[i]);
}
}
return newArray;
};
const s1 = new Fifo();
function pageFaults()
{
var pageValue = text.value;
var capacity=cap.value;
$('ul').empty();
text.value='';
// Check if the set can hold more pages
if (s.size < capacity)
{
// Insert it leto set if not present
// already which represents page fault
if (!s.has(pageValue))
{
s.add(pageValue);
// increment page fault
page_faults++;
// Push the current page leto the queue
indexes.push(pageValue);
}
else{
hit++;
}
}
// If the set is full then need to perform FIFO
// i.e. remove the first page of the queue from
// set and queue both and insert the current page
else
{
if(i == capacity){
i=0;
}
// Check if current page is not already
// present in the set
if (!s.has(pageValue))
{
//Pop the first page from the queue
let val = indexes[i];
//indexes.shift();
//arrayRemove(indexes,val);
indexes = removeElement(indexes,val);
// Remove the indexes page
s.delete(val);
// insert the current page
s.add(pageValue);
// push the current page leto
// the queue
//indexes.unshift(pageValue);
insertAt(indexes,i++,pageValue);
// Increment page faults
page_faults++;
}
else{
hit++;
}
}
for(let j=indexes.length-1;j>=0;j--){
s1.display_elements(indexes[j]);
}
faults.innerHTML=page_faults;
hits.innerHTML=hit;
}