-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulator_MultiArrayQueue.html
More file actions
442 lines (412 loc) · 17.3 KB
/
Simulator_MultiArrayQueue.html
File metadata and controls
442 lines (412 loc) · 17.3 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="Interactive Simulator of the Multi-Array Queue">
<title>Interactive Simulator of the Multi-Array Queue</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
color: #24292e;
background-color: #fdfdfd;
-webkit-text-size-adjust: 100%;
-webkit-font-feature-settings: "kern" 1;
-moz-font-feature-settings: "kern" 1;
-o-font-feature-settings: "kern" 1;
font-feature-settings: "kern" 1;
font-kerning: normal;
padding-right: 30px;
padding-left: 30px;
}
h1 {
margin-bottom: 10px;
}
h4 {
margin-top: 0px;
margin-bottom: 10px;
}
button {
padding: 10px;
margin-right: 20px;
margin-bottom: 10px;
}
p.remark {
margin-top: 10px;
font-size: small;
line-height: 1.5;
}
</style>
</head>
<body>
<h1>Interactive Simulator of the Multi-Array Queue</h1>
<h4 id="subcap"></h4>
<canvas id="canv" width="560" height="560"></canvas>
<br>
<button id="MAQ_0_4" onclick="start_again(0,4)">Start again MAQ(0,4)</button>
<button id="MAQ_1_3" onclick="start_again(1,3)">MAQ(1,3)</button>
<button id="MAQ_2_2" onclick="start_again(2,2)">MAQ(2,2)</button>
<button id="MAQ_3_2" onclick="start_again(3,2)">MAQ(3,2)</button>
<button id="MAQ_7_1" onclick="start_again(7,1)">MAQ(7,1)</button>
<button id="MAQ_15_0" onclick="start_again(15,0)">MAQ(15,0)</button>
<button id="MAQ_1_0" onclick="start_again(1,0)">MAQ(1,0)</button>
<button id="MAQ_0_0" onclick="start_again(0,0)">MAQ(0,0) corner case</button>
<br>
<button id="enqueue" onclick="remark(null); enqueue()"><b>Enqueue</b></button>
<button id="dequeue" onclick="remark(null); dequeue()"><b>Dequeue</b></button>
<button id="enqueue_dequeue" onclick="remark(null); enqueue(); dequeue()"><b>Enqueue + Dequeue</b></button>
<button id="play_scenario" onclick="play_scenario()"><b>Play a scenario with microbursts on the Enqueue side</b></button>
<div id="rem"></div>
<script>
"use strict"
const subcap = document.getElementById("subcap")
const canv = document.getElementById("canv")
const rem = document.getElementById("rem")
const ctx = canv.getContext("2d")
var remText
// ----------------------
function remark( t ) {
if (!t) {
remText = null
rem.innerHTML = ''
} else {
if (remText) remText += " " + t
else remText = t
rem.innerHTML = '<p class="remark">' + remText
}
}
function assertionError( t ) {
remark('<span style="color:#FF0000; font-weight:bold;">' + t + '</span>')
}
function resultAttention( t ) {
remark('<span style="color:#0000FF; font-weight:bold;">' + t + '</span>')
}
function result( t ) {
remark('<span style="font-weight:bold;">' + t + '</span>')
}
// ----------------------
const leftOff = 50
const objSize = 30
const divDist = 12
const bottOff = divDist * 5
const arrDist = divDist * 7 + objSize
function arrow_up(x, y) {
ctx.lineTo( x, y )
ctx.lineTo( x - 3, y + 5 )
ctx.moveTo( x, y )
ctx.lineTo( x + 3, y + 5 )
}
function arrow_left(x, y ) {
ctx.lineTo( x, y )
ctx.lineTo( x + 5, y - 3 )
ctx.moveTo( x, y )
ctx.lineTo( x + 5, y + 3 )
}
function draw_object( rix, ix, full, writer, reader ) {
if ( full ) ctx.fillStyle = "#FF9999"
else ctx.fillStyle = "#FFFFFF"
ctx.fillRect( leftOff + (rix * arrDist), canv.height - bottOff - objSize - (ix * objSize), objSize, objSize )
ctx.lineWidth = 2
ctx.strokeStyle = "#000000"
ctx.strokeRect( leftOff + (rix * arrDist), canv.height - bottOff - objSize - (ix * objSize), objSize, objSize )
let txt
if (writer && reader) txt = "WR"
else if (writer) txt = "W"
else if (reader) txt = "R"
if (txt) {
ctx.font = "14px Arial"
ctx.textAlign = "center"
ctx.fillStyle = "#000000"
ctx.fillText( txt, leftOff + (rix * arrDist) + (objSize / 2), canv.height - bottOff - objSize - (ix * objSize) + (objSize / 2) + 5)
}
}
function draw_diversion(dix, rix, ix) {
ctx.lineWidth = 1
ctx.strokeStyle = "#000000"
ctx.beginPath()
ctx.moveTo( leftOff + (rix * arrDist) + objSize , canv.height - bottOff - (ix * objSize) )
ctx.lineTo( leftOff + (rix * arrDist) + objSize + ((2 + dix) * divDist) , canv.height - bottOff - (ix * objSize) )
ctx.lineTo( leftOff + (rix * arrDist) + objSize + ((2 + dix) * divDist) , canv.height - bottOff + ((1 + dix) * divDist) )
ctx.lineTo( leftOff + ((1 + dix) * arrDist) + (objSize / 2) , canv.height - bottOff + ((1 + dix) * divDist) )
arrow_up ( leftOff + ((1 + dix) * arrDist) + (objSize / 2) , canv.height - bottOff )
ctx.stroke()
ctx.beginPath()
ctx.moveTo( leftOff + ((1 + dix) * arrDist) + (objSize / 2) , canv.height - bottOff - ((firstArraySize << (1 + dix)) * objSize) )
ctx.lineTo( leftOff + ((1 + dix) * arrDist) + (objSize / 2) , canv.height - bottOff - ((firstArraySize << (1 + dix)) * objSize) - divDist )
ctx.lineTo( leftOff + (rix * arrDist) + objSize + ((2 + dix) * divDist) , canv.height - bottOff - ((firstArraySize << (1 + dix)) * objSize) - divDist )
ctx.lineTo( leftOff + (rix * arrDist) + objSize + ((2 + dix) * divDist) , canv.height - bottOff - (ix * objSize) - (objSize / 2) )
arrow_left( leftOff + (rix * arrDist) + objSize , canv.height - bottOff - (ix * objSize) - (objSize / 2) )
ctx.stroke()
}
// ----------------------
// private data of the Multi-Array Queue
var firstArraySize
var ringsMaxIndex
var diversions
var writerPositionRix
var writerPositionIx
var readerPositionRix
var readerPositionIx
function enqueue() {
let isQueueExtensionPossible = (ringsMaxIndex < diversions.length) // if there is room yet for the extension
let extendQueue = false
let writerRix = writerPositionRix // writer prospective
let writerIx = writerPositionIx
let tmpRix
go_forward:
for (;;)
{
writerIx ++ // prospective move forward
// if the prospective move goes "beyond" the end of rings[writerRix]
if ((firstArraySize << writerRix) == writerIx)
{
if (0 == writerRix) // if in rings[0]
{
remark("Moved to rings[0][0].")
writerRix = 0
writerIx = 0
// do not break here because from rings[0][0] eventually diversion(s) shall be followed forward
}
else // i.e. we are in a "higher" rings[N]
{
remark("Followed diversion[" + (writerRix - 1) + "] back.")
tmpRix = writerRix
writerRix = diversions[tmpRix - 1].rix // follow diversion[N-1] back
writerIx = diversions[tmpRix - 1].ix
// if the prospective move has hit the reader (that is in the previous round) "from behind"
if ((readerPositionRix == writerRix) && (readerPositionIx == writerIx))
{
if (isQueueExtensionPossible)
{
assertionError("Hit reader on the return path of a diversion.")
return
}
else
{
resultAttention("Queue is full.")
return
}
}
break go_forward // the prospective move forward is done, we are on the return path of a diversion
}
}
// if the prospective move reached (an entry side of) a diversion: follow it - to the beginning of respective rings[rix]
// (another diversion may sit there, so then continue following)
for (tmpRix = 1 + writerRix; tmpRix <= ringsMaxIndex; tmpRix ++)
{
if ((diversions[tmpRix - 1].rix == writerRix) && (diversions[tmpRix - 1].ix == writerIx))
{
remark("Followed diversion[" + (tmpRix - 1) + "] forward.")
writerRix = tmpRix // move to the first element of the array of Objects the diversion leads to
writerIx = 0
}
}
// if the prospective move has hit the reader (that is in the previous round) "from behind"
if ((readerPositionRix == writerRix) && (readerPositionIx == writerIx))
{
if (isQueueExtensionPossible)
{
remark("Extended the Queue.")
extendQueue = true
}
else
{
resultAttention("Queue is full.")
return
}
}
// the forward-looking check to prevent the next writer from hitting the reader "from behind"
// on the return path of a diversion (see Paper for explanation)
else
{
let testNextWriterRix = writerRix
let testNextWriterIx = writerIx
test_next:
for (; ((0 != testNextWriterRix) && ((firstArraySize << testNextWriterRix) == (1 + testNextWriterIx))) ;)
{
tmpRix = testNextWriterRix
testNextWriterRix = diversions[tmpRix - 1].rix // follow the diversion back
testNextWriterIx = diversions[tmpRix - 1].ix
if ((readerPositionRix == testNextWriterRix) && (readerPositionIx == testNextWriterIx)) // if we would hit the reader
{
if (isQueueExtensionPossible)
{
remark("Created new diversion because next writer could hit reader on the return path of a diversion.")
extendQueue = true
}
break test_next
}
}
}
break go_forward // prospective move forward is now done
}
// preparations are done, start the actual work
if (extendQueue)
{
let rixMaxNew = 1 + ringsMaxIndex
// impossible for writerPos to be already in the diversions array, but better check ...
for (tmpRix = 1; tmpRix <= ringsMaxIndex; tmpRix ++)
{
if ((diversions[tmpRix - 1].rix == writerRix) && (diversions[tmpRix - 1].ix == writerIx))
{
assertionError("Duplicity in the diversions array.")
return
}
}
diversions[rixMaxNew - 1] = new Object() // write the new diversion = the prospective writer position
diversions[rixMaxNew - 1].rix = writerRix
diversions[rixMaxNew - 1].ix = writerIx
ringsMaxIndex = rixMaxNew // increment ringsMaxIndex
draw_object( rixMaxNew, 0, true, true, false )
for (let tmpIx = 1; tmpIx < (firstArraySize << rixMaxNew); tmpIx ++)
{
draw_object( rixMaxNew, tmpIx, false, false, false )
}
draw_diversion( rixMaxNew - 1, writerRix, writerIx )
redrawOldWriterPosition( writerPositionRix, writerPositionIx, readerPositionRix, readerPositionIx )
writerPositionRix = rixMaxNew // new writer position = first array element of the new array
writerPositionIx = 0
}
else // no extendQueue
{
draw_object( writerRix, writerIx, true, true, false )
redrawOldWriterPosition( writerPositionRix, writerPositionIx, readerPositionRix, readerPositionIx )
writerPositionRix = writerRix // new writer position = prospective writer position
writerPositionIx = writerIx
}
result("Enqueued.")
}
function redrawOldWriterPosition( oldWriterRix, oldWriterIx, currReaderRix, currReaderIx )
{
if ((currReaderRix == oldWriterRix) && (currReaderIx == oldWriterIx)) {
draw_object( oldWriterRix, oldWriterIx, false, false, true )
} else {
draw_object( oldWriterRix, oldWriterIx, true, false, false )
}
}
// ----------------------
function dequeue() {
let readerRix = readerPositionRix // reader prospective
let readerIx = readerPositionIx
let tmpRix
// if the reader stands on the writer: the Queue is empty
if ((writerPositionRix == readerRix) && (writerPositionIx == readerIx))
{
resultAttention("Queue is empty.")
return
}
go_forward:
for (;;)
{
readerIx ++ // prospective move forward
// if the prospective move goes "beyond" the end of rings[readerRix]
if ((firstArraySize << readerRix) == readerIx)
{
if (0 == readerRix) // if in rings[0]
{
remark("Moved to rings[0][0].")
readerRix = 0
readerIx = 0
// do not break here because from rings[0][0] eventually diversion(s) shall be followed forward
}
else // i.e. we are in a "higher" rings[N]
{
remark("Followed diversion[" + (readerRix - 1) + "] back.")
tmpRix = readerRix
readerRix = diversions[tmpRix - 1].rix // follow diversion[N-1] back
readerIx = diversions[tmpRix - 1].ix
break go_forward // the prospective move forward is done, we are on the return path of a diversion
}
}
// if the prospective move reached (an entry side of) a diversion: follow it - to the beginning of respective rings[rix]
// (another diversion may sit there, so then continue following)
for (tmpRix = 1 + readerRix; tmpRix <= ringsMaxIndex; tmpRix ++)
{
if ((diversions[tmpRix - 1].rix == readerRix) && (diversions[tmpRix - 1].ix == readerIx))
{
remark("Followed diversion[" + (tmpRix - 1) + "] forward.")
readerRix = tmpRix // move to the first element of the array of Objects the diversion leads to
readerIx = 0
}
}
break go_forward // prospective move forward is now done
}
if ((writerPositionRix == readerRix) && (writerPositionIx == readerIx)) {
draw_object(readerRix, readerIx, false, true, true )
} else {
draw_object(readerRix, readerIx, false, false, true )
}
redrawOldReaderPosition( readerPositionRix, readerPositionIx, writerPositionRix, writerPositionIx )
readerPositionRix = readerRix // new reader position = prospective reader position
readerPositionIx = readerIx
result("Dequeued.")
}
function redrawOldReaderPosition( oldReaderRix, oldReaderIx, currWriterRix, currWriterIx )
{
draw_object( oldReaderRix, oldReaderIx, false, false, false )
}
// ----------------------
const scenario = [+300, +300, +300, +300, -300, +100, +100, +50, +50, +50,
+50, +50, +50, +50, +50, +50, +50, +50, +50, +50,
+50, +50, +50, +50, -200, -200, -200, -200, -200, -200,
+50, +50, -100, -100, -100, -100, -100, -100, -100, -100,
+50, +50, -100, -100, -100, -100, -100, -100, -100, -100,
+50, +50, +50, +50, +50, +50, +50, +50, +50, +50,
-100, -100, -100, -100, -100, -100, -100, -100, -100, -100,
-100, -100, -100, -100, +100, -100, +100, -100, +100, -100,
+100, -100, +50, +50, +50, +50, +50, +50, -100, -100,
-100, -100, -100, -100, +100, -100, +100, -100, +100, -100]
var scenario_step
function play_scenario() {
disable_buttons(true)
scenario_step = 0
setTimeout(play_scenario_step, Math.abs(scenario[scenario_step]))
}
function play_scenario_step() {
if (scenario[scenario_step] < 0) {remark(null); dequeue()}
else {remark(null); enqueue()}
scenario_step ++
if (scenario_step < scenario.length) {
setTimeout(play_scenario_step, Math.abs(scenario[scenario_step]))
} else {
disable_buttons(false)
}
}
function disable_buttons(disabled) {
document.getElementById("MAQ_0_4").disabled = disabled
document.getElementById("MAQ_1_3").disabled = disabled
document.getElementById("MAQ_2_2").disabled = disabled
document.getElementById("MAQ_3_2").disabled = disabled
document.getElementById("MAQ_7_1").disabled = disabled
document.getElementById("MAQ_15_0").disabled = disabled
document.getElementById("MAQ_1_0").disabled = disabled
document.getElementById("MAQ_0_0").disabled = disabled
document.getElementById("enqueue").disabled = disabled
document.getElementById("dequeue").disabled = disabled
document.getElementById("enqueue_dequeue").disabled = disabled
document.getElementById("play_scenario").disabled = disabled
}
// ----------------------
function start_again(initialCapacity, cntAllowedExtensions) {
subcap.innerHTML = 'Contains simplified versions of the algorithms' +
' (Constructor parameters: initialCapacity = ' + initialCapacity +
', cntAllowedExtensions = ' + cntAllowedExtensions + ')'
firstArraySize = 1 + initialCapacity
ringsMaxIndex = 0
diversions = new Array(cntAllowedExtensions)
writerPositionRix = 0
writerPositionIx = firstArraySize - 1
readerPositionRix = 0
readerPositionIx = firstArraySize - 1
ctx.clearRect( 0, 0, canv.width, canv.height )
remark(null)
for (let tmpIx = 0; tmpIx < (firstArraySize - 1); tmpIx ++)
{
draw_object( 0, tmpIx, false, false, false )
}
draw_object( 0, (firstArraySize - 1), false, true, true )
}
start_again(0,4) // MAQ(0,4) is default
</script>
<noscript>Sorry, your browser does not support JavaScript!</noscript>
</body>
</html>