3
3
4
4
import { msort_arr_bup } from '../explanations' ;
5
5
import ArrayTracer from '../../components/DataStructures/Array/Array1DTracer' ;
6
+
7
+
6
8
import {
7
- areExpanded ,
8
- } from './collapseChunkPlugin' ;
9
+ isMergeCopyExpanded ,
10
+ isMergeExpanded ,
11
+ highlight ,
12
+ highlightB ,
13
+ unhighlight ,
14
+ highlight2Runlength ,
15
+ assignVarToA ,
16
+ assignVarToB ,
17
+ displayMergeLabels ,
18
+ highlightAPointers ,
19
+ set_simple_stack ,
20
+ resetArrayA
21
+ } from './msort_shared.js' ;
9
22
10
23
11
24
const run = run_msort ( ) ;
@@ -50,51 +63,10 @@ export function initVisualisers() {
50
63
}
51
64
}
52
65
53
- // arrayB exists and is displayed only if MergeCopy is expanded
54
- function isMergeCopyExpanded ( ) {
55
- return areExpanded ( [ 'MergeCopy' ] ) ;
56
- }
57
-
58
- // We don't strictly need isMergeExpanded: only needed if last chunk of
59
- // merge still had extra vars displayed. Some code still needs
60
- // isMergeCopyExpanded because it uses arrayB
61
- function isMergeExpanded ( ) {
62
- return areExpanded ( [ 'MergeCopy' , 'Merge' ] ) ; // MergeCopy contains Merge
63
- }
64
-
65
66
// -------------------------------------------------------------------------------
66
67
// Define helper functions
67
68
// -------------------------------------------------------------------------------
68
69
69
- // Highlights Array A either red or green
70
- // Can add more colours in future
71
- function highlight ( vis , index , color ) {
72
- if ( color == 'red' ) {
73
- vis . array . select ( index ) ;
74
- }
75
- if ( color == 'green' ) {
76
- vis . array . patch ( index ) ;
77
- }
78
- }
79
- // Same as highlight() but checks isMergeExpanded()/arrayB is displayed, otherwise does nothing
80
- function highlightB ( vis , index , color ) {
81
- if ( isMergeExpanded ( ) ) {
82
- if ( color == 'red' ) {
83
- vis . arrayB . select ( index ) ;
84
- }
85
- if ( color == 'green' ) {
86
- vis . arrayB . patch ( index ) ;
87
- }
88
- }
89
- }
90
-
91
- // Highlights two runlengths two different colours
92
- function highlight2Runlength ( vis , left , mid , right , colorA , colorB ) {
93
- // highlight first runlength color A
94
- for ( let i = left ; i <= mid ; i ++ ) highlight ( vis , i , colorA ) ;
95
- // highlight second runlength color B
96
- for ( let j = mid + 1 ; j <= right ; j ++ ) highlight ( vis , j , colorB ) ;
97
- }
98
70
99
71
// Highlight entire array alternating colors for runlength
100
72
function highlightAllRunlengths ( vis , runlength , colorA , colorB , size ) {
@@ -103,125 +75,20 @@ function highlightAllRunlengths(vis, runlength, colorA, colorB, size) {
103
75
for ( let i = 0 ; i < size ; i ++ ) {
104
76
if ( toggle == 0 ) {
105
77
highlight ( vis , i , colorA ) ;
106
- console . log ( "toggle == 0" ) ;
107
78
}
108
79
if ( toggle == 1 ) {
109
80
highlight ( vis , i , colorB ) ;
110
- console . log ( "toggle == 1" ) ;
111
81
}
112
- console . log ( "(i + 1) % runlength = " + ( runlength % ( i + 1 ) ) ) ;
113
- console . log ( "(runlength = " + ( runlength ) ) ;
114
- // Switch color after completing a run of length 'runlength'
115
- if ( ( i + 1 ) % runlength == 0 ) {
116
-
117
- console . log ( "(i + 1) % runlength == 0" ) ;
118
-
119
- toggle = 1 - toggle ; // Flip toggle between 0 and 1
120
-
121
- } console . log ( "toggle = " + toggle ) ;
122
- }
123
- }
124
-
125
- // Unhighlight entire array alternating colors for runlength
126
- function unhighlightAllRunlengths ( vis , runlength , colorA , colorB , size ) {
127
- let toggle = 0 ; // 0 = colorA, 1 = colorB
128
82
129
- for ( let i = 0 ; i < size ; i ++ ) {
130
- if ( toggle == 0 ) {
131
- unhighlight ( vis , i , colorA ) ;
132
- console . log ( "toggle == 0" ) ;
133
- }
134
- if ( toggle == 1 ) {
135
- unhighlight ( vis , i , colorB ) ;
136
- console . log ( "toggle == 1" ) ;
137
- }
138
- console . log ( "(i + 1) % runlength = " + ( runlength % ( i + 1 ) ) ) ;
139
- console . log ( "(runlength = " + ( runlength ) ) ;
140
83
// Switch color after completing a run of length 'runlength'
141
84
if ( ( i + 1 ) % runlength == 0 ) {
142
-
143
- console . log ( "(i + 1) % runlength == 0" ) ;
144
-
145
85
toggle = 1 - toggle ; // Flip toggle between 0 and 1
146
-
147
- } console . log ( "toggle = " + toggle ) ;
148
- }
149
- }
150
-
151
- // unhighlights arrayA
152
- function unhighlight ( vis , index , color ) {
153
- if ( color == 'red' ) {
154
- vis . array . deselect ( index ) ;
155
- }
156
- if ( color == 'green' ) {
157
- vis . array . depatch ( index ) ;
158
- }
159
- }
160
-
161
- function highlightAPointers ( vis , ap1 , max1 , ap2 , max2 , color ) {
162
- if ( ap1 <= max1 ) {
163
- highlight ( vis , ap1 , color ) ;
164
- }
165
- if ( ap2 <= max2 ) {
166
- highlight ( vis , ap2 , color ) ;
167
- }
168
- }
169
-
170
- // this function sets array a, highlighting from left to mid red, and set the stack
171
- function resetArrayA ( vis , A , left , mid , right , runlength ) {
172
- vis . array . set ( A , 'msort_arr_bup' ) ;
173
- highlight2Runlength ( vis , left , mid , right , colorA , colorA ) ;
174
- set_simple_stack ( vis . array , [ runlength ] ) ;
175
- }
176
-
177
- // Assigns label to array A at index, checks if index is greater than size of array
178
- // if index is greater than size, assign label to last element in array
179
- function assignVarToA ( vis , variable_name , index , size ) {
180
- if ( index === undefined )
181
- vis . array . removeVariable ( variable_name ) ;
182
- else if ( index >= size )
183
- vis . array . assignVariable ( variable_name , size - 1 )
184
- else
185
- vis . array . assignVariable ( variable_name , index ) ;
186
- }
187
-
188
- // Same as above function bet also checks if array B is displayed
189
- function assignVarToB ( vis , variable_name , index , size ) {
190
- if ( isMergeExpanded ( ) ) {
191
- if ( index === undefined )
192
- vis . arrayB . removeVariable ( variable_name ) ;
193
- else if ( index >= size )
194
- vis . arrayB . assignVariable ( variable_name , size - 1 ) ;
195
- else
196
- vis . arrayB . assignVariable ( variable_name , index ) ;
86
+ }
197
87
}
198
88
}
199
89
200
- // Display the runlength of the array at the runlength'th element
201
- function displayRunlength ( vis , runlength , size ) {
202
- let text = 'runlength = ' + runlength ;
203
- let index = runlength - 1 ;
204
-
205
- assignVarToA ( vis , text , index , size ) ;
206
- }
207
-
208
- // Display all the labels needed for Merge()
209
- function displayMergeLabels ( vis , ap1 , max1 , ap2 , max2 , bp , size ) {
210
- assignVarToA ( vis , 'ap1' , ap1 , size ) ;
211
- assignVarToA ( vis , 'max1' , max1 , size ) ;
212
- assignVarToA ( vis , 'ap2' , ap2 , size ) ;
213
- assignVarToA ( vis , 'max2' , max2 , size ) ;
214
- if ( isMergeExpanded ( ) ) assignVarToB ( vis , 'bp' , bp , size ) ;
215
- }
216
-
217
90
218
91
219
- function set_simple_stack ( vis_array , c_stk ) {
220
- console . log ( "set_simple_stack" + c_stk ) ;
221
- console . log ( c_stk ) ;
222
- vis_array . setList ( c_stk ) ;
223
- }
224
-
225
92
/**
226
93
*
227
94
* @param {object } chunker
@@ -259,7 +126,8 @@ export function run_msort() {
259
126
} , [ A , B , size ] , length ) ;
260
127
261
128
chunker . add ( 'runlength' , ( vis , c_rlength ) => {
262
- displayRunlength ( vis , c_rlength , size ) ;
129
+
130
+ assignVarToA ( vis , "runlength" , c_rlength , size ) ;
263
131
264
132
set_simple_stack ( vis . array , [ c_rlength ] ) ;
265
133
highlightAllRunlengths ( vis , c_rlength , colorA , colorB , size ) ;
@@ -271,23 +139,23 @@ export function run_msort() {
271
139
chunker . add ( 'MainWhile' , ( vis , c_rlength , c_left ) => {
272
140
273
141
// display size label
274
- assignVarToA ( vis , ( "size = " + size ) , size , size ) ;
142
+ assignVarToA ( vis , "size" , size , size ) ;
275
143
144
+ } , [ runlength , left ] ) ;
276
145
146
+ chunker . add ( 'left' , ( vis , a , c_left , c_rlength ) => {
147
+ vis . array . set ( a , 'msort_arr_bup' ) ;
277
148
278
- } , [ runlength , left ] ) ;
279
149
280
- chunker . add ( 'left' , ( vis , c_left , c_rlength ) => {
281
150
assignVarToA ( vis , 'left' , c_left , size ) ;
282
-
283
- unhighlightAllRunlengths ( vis , c_rlength , colorA , colorB , size ) ;
151
+ set_simple_stack ( vis . array , [ c_rlength ] ) ;
284
152
285
153
let left_2 = c_left ;
286
154
let mid_2 = ( c_rlength + c_left - 1 ) ;
287
155
let right_2 = ( Math . min ( c_rlength * 2 , size ) - 1 ) ;
288
156
289
157
highlight2Runlength ( vis , left_2 , mid_2 , right_2 , colorA , colorB ) ;
290
- } , [ left , runlength ] ) ;
158
+ } , [ A , left , runlength ] ) ;
291
159
292
160
while ( ( left + runlength ) <= size ) {
293
161
@@ -305,8 +173,8 @@ export function run_msort() {
305
173
306
174
chunker . add ( 'mid' , ( vis , c_mid , c_rlength ) => {
307
175
// remove runlength and size labels
308
- assignVarToA ( vis , ( 'runlength = ' + c_rlength ) , undefined , size ) ;
309
- assignVarToA ( vis , ( 'size = ' + ( size ) ) , undefined , size ) ;
176
+ assignVarToA ( vis , 'runlength' , undefined , size ) ;
177
+ assignVarToA ( vis , 'size' , undefined , size ) ;
310
178
311
179
assignVarToA ( vis , 'mid' , c_mid , size ) ;
312
180
} , [ mid , runlength ] ) ;
@@ -321,7 +189,7 @@ export function run_msort() {
321
189
322
190
// now in the nitty gritty of Merge
323
191
// highlight the two parts you want to merge red
324
- resetArrayA ( vis , a , c_left , c_mid , c_right , c_rlength ) ;
192
+ resetArrayA ( vis , a , c_left , c_mid , c_right , c_rlength , colorA , colorA ) ;
325
193
326
194
assignVarToA ( vis , 'left' , undefined , size ) ; // ap1 replaces left
327
195
assignVarToA ( vis , 'ap1' , c_ap1 , size ) ;
@@ -348,7 +216,6 @@ export function run_msort() {
348
216
if ( isMergeExpanded ( ) ) {
349
217
assignVarToA ( vis , 'right' , undefined , size ) ; // max2 replaces right
350
218
assignVarToA ( vis , 'max2' , c_max2 , size ) ;
351
-
352
219
}
353
220
} , [ max2 ] ) ;
354
221
@@ -365,7 +232,7 @@ export function run_msort() {
365
232
366
233
chunker . add ( 'MergeWhile' , ( vis , a , c_left , c_right , c_mid , c_ap1 , c_max1 , c_ap2 , c_max2 , c_bp , c_rlength ) => {
367
234
368
- resetArrayA ( vis , a , c_left , c_mid , c_right , c_rlength ) ;
235
+ resetArrayA ( vis , a , c_left , c_mid , c_right , c_rlength , colorA , colorA ) ;
369
236
370
237
displayMergeLabels ( vis , c_ap1 , c_max1 , c_ap2 , c_max2 , c_bp , size ) ;
371
238
@@ -387,8 +254,7 @@ export function run_msort() {
387
254
388
255
if ( isMergeExpanded ( ) ) vis . arrayB . set ( b , 'msort_arr_bup' ) ;
389
256
390
- resetArrayA ( vis , a , c_left , c_mid , c_right , c_rlength ) ;
391
-
257
+ resetArrayA ( vis , a , c_left , c_mid , c_right , c_rlength , colorA , colorA ) ;
392
258
393
259
displayMergeLabels ( vis , c_ap1 , c_max1 , c_ap2 , c_max2 , c_bp , size ) ;
394
260
// future color: should be colorA & colorB
@@ -402,7 +268,7 @@ export function run_msort() {
402
268
ap1 = ap1 + 1 ;
403
269
chunker . add ( 'ap1++' , ( vis , a , c_left , c_mid , c_right , c_rlength ,
404
270
c_ap1 , c_max1 , c_ap2 , c_max2 , c_bp ) => {
405
- resetArrayA ( vis , a , c_left , c_mid , c_right , c_rlength ) ;
271
+ resetArrayA ( vis , a , c_left , c_mid , c_right , c_rlength , colorA , colorA ) ;
406
272
displayMergeLabels ( vis , c_ap1 , c_max1 , c_ap2 , c_max2 , c_bp , size ) ;
407
273
408
274
assignVarToA ( vis , 'ap1' , c_ap1 , size ) ;
@@ -427,7 +293,7 @@ export function run_msort() {
427
293
428
294
chunker . add ( 'copyap2' , ( vis , a , b , c_ap1 , c_ap2 , c_bp , c_max1 , c_max2 , c_left , c_right , c_mid , c_rlength ) => {
429
295
if ( isMergeExpanded ( ) ) vis . arrayB . set ( b , 'msort_arr_bup' ) ;
430
- resetArrayA ( vis , a , c_left , c_mid , c_right , c_rlength ) ;
296
+ resetArrayA ( vis , a , c_left , c_mid , c_right , c_rlength , colorA , colorA ) ;
431
297
432
298
displayMergeLabels ( vis , c_ap1 , c_max1 , c_ap2 , c_max2 , c_bp , size ) ;
433
299
@@ -443,7 +309,7 @@ export function run_msort() {
443
309
ap2 = ap2 + 1 ;
444
310
chunker . add ( 'ap2++' , ( vis , a , c_left , c_mid , c_right , c_rlength ,
445
311
c_ap1 , c_max1 , c_ap2 , c_max2 , c_bp ) => {
446
- resetArrayA ( vis , a , c_left , c_mid , c_right , c_rlength ) ;
312
+ resetArrayA ( vis , a , c_left , c_mid , c_right , c_rlength , colorA , colorA ) ;
447
313
displayMergeLabels ( vis , c_ap1 , c_max1 , c_ap2 , c_max2 , c_bp , size ) ;
448
314
449
315
assignVarToA ( vis , "ap2" , c_ap2 , size ) ;
@@ -466,7 +332,7 @@ export function run_msort() {
466
332
467
333
chunker . add ( 'CopyRest1' , ( vis , a , b , c_ap1 , c_max1 , c_left , c_right , c_mid , c_bp , c_rlength ) => {
468
334
469
- resetArrayA ( vis , a , c_left , c_mid , c_right , c_rlength ) ;
335
+ resetArrayA ( vis , a , c_left , c_mid , c_right , c_rlength , colorA , colorA ) ;
470
336
471
337
if ( isMergeExpanded ( ) ) vis . arrayB . set ( b , 'msort_arr_bup' ) ;
472
338
@@ -488,7 +354,7 @@ export function run_msort() {
488
354
}
489
355
490
356
chunker . add ( 'CopyRest2' , ( vis , a , b , c_ap2 , c_max2 , c_left , c_right , c_mid , c_rlength ) => {
491
- resetArrayA ( vis , a , c_left , c_mid , c_right , c_rlength ) ;
357
+ resetArrayA ( vis , a , c_left , c_mid , c_right , c_rlength , colorA , colorA ) ;
492
358
493
359
if ( isMergeExpanded ( ) ) vis . arrayB . set ( b , 'msort_arr_bup' ) ;
494
360
assignVarToA ( vis , 'ap2' , c_ap2 , size ) ;
@@ -549,7 +415,7 @@ export function run_msort() {
549
415
set_simple_stack ( vis . array , [ c_rlength ] ) ;
550
416
551
417
if ( c_rlength < size ) {
552
- displayRunlength ( vis , c_rlength , size ) ;
418
+ assignVarToA ( vis , "runlength" , c_rlength , size ) ;
553
419
}
554
420
555
421
} , [ runlength ] ) ;
0 commit comments