1
+ // Some color code could be cleaned up
2
+ // Could share some code with straight radix sort and quicksort
1
3
import ArrayTracer from '../../components/DataStructures/Array/Array1DTracer' ;
2
4
import MaskTracer from '../../components/DataStructures/Mask/MaskTracer'
3
5
import {
4
6
areExpanded ,
5
7
} from './collapseChunkPlugin' ;
6
8
import { createPopper } from '@popperjs/core' ;
9
+ import { colors } from '../../components/DataStructures/colors' ;
10
+
11
+ const partLColor = colors . peach ;
12
+ const partRColor = colors . sky ;
13
+ const sortedColor = colors . stone ;
14
+ const highlightColor = colors . apple ; // for i,j in partition,...
7
15
8
16
// see stackFrameColour in Array1DRenderer/index.js to find corresponding function mapping to css
9
17
const STACK_FRAME_COLOR = {
@@ -85,20 +93,12 @@ const isRecursionExpanded = () => {
85
93
return areExpanded ( [ 'MSDRadixSortLeft' ] ) || areExpanded ( [ 'MSDRadixSortRight' ] ) ;
86
94
}
87
95
88
- const highlight = ( vis , index , isPrimaryColor = true ) => {
89
- if ( isPrimaryColor ) {
90
- vis . array . select ( index ) ;
91
- } else {
92
- vis . array . patch ( index ) ;
93
- }
94
- } ;
96
+ const highlight = ( vis , index ) => {
97
+ vis . array . selectColor ( index , highlightColor ) ;
98
+ } ;
95
99
96
- const unhighlight = ( vis , index , isPrimaryColor = true ) => {
97
- if ( isPrimaryColor ) {
98
- vis . array . deselect ( index ) ;
99
- } else {
100
- vis . array . depatch ( index ) ;
101
- }
100
+ const unhighlight = ( vis , index ) => {
101
+ vis . array . deselect ( index ) ;
102
102
} ;
103
103
104
104
const updateMask = ( vis , value ) => {
@@ -194,6 +194,8 @@ export default {
194
194
assert ( vis . array ) ;
195
195
assert ( cur_real_stack && cur_finished_stack_frames ) ;
196
196
197
+ // conditions for i,j display moved elsewhere
198
+ /*
197
199
if (!isPartitionExpanded()) {
198
200
// j should not show up in vis if partition is collapsed
199
201
cur_j = undefined;
@@ -205,6 +207,7 @@ export default {
205
207
cur_i = undefined;
206
208
cur_i_too_high = undefined;
207
209
}
210
+ */
208
211
209
212
vis . array . setStackDepth ( cur_real_stack . length ) ;
210
213
vis . array . setStack (
@@ -226,21 +229,29 @@ export default {
226
229
// init, before partition
227
230
highlight ( vis , cur_i )
228
231
highlight ( vis , cur_j )
229
- } else if ( isRecursionExpanded ( ) && cur_i !== undefined && tmp_j === undefined && prev_i !== undefined ) {
232
+ } else if ( cur_i !== undefined && tmp_j === undefined && prev_i !== undefined ) {
230
233
// just before first recursive call
231
- unhighlight ( vis , prev_i )
232
- if ( prev_j >= 0 ) // might have fallen off array
233
- unhighlight ( vis , prev_j )
234
+ if ( i <= right )
235
+ vis . array . selectColor ( prev_i , partRColor )
236
+ if ( prev_j >= left ) // might have fallen off array
237
+ vis . array . selectColor ( prev_j , partLColor )
238
+ for ( let k = cur_i ; k <= right ; k ++ )
239
+ vis . array . deselect ( k ) ;
234
240
} else if ( checkingLeft ) {
235
- if ( isRecursionExpanded ( ) && prev_i !== undefined && prev_i !== cur_j )
236
- unhighlight ( vis , prev_i )
237
- highlight ( vis , cur_i )
241
+ if ( prev_i !== undefined && prev_i !== cur_j )
242
+ for ( let k = prev_i ; k < cur_i && k <= right ; k ++ )
243
+ vis . array . selectColor ( k , partLColor )
244
+ if ( cur_i < cur_j )
245
+ highlight ( vis , cur_i )
246
+ else if ( cur_i <= right )
247
+ vis . array . selectColor ( cur_i , partRColor )
238
248
if ( arr && cur_i !== undefined )
239
249
updateBinary ( vis , arr [ cur_i ] )
240
250
} else {
241
- if ( isRecursionExpanded ( ) && prev_j !== undefined && prev_j !== cur_i )
242
- unhighlight ( vis , prev_j ) ;
243
- if ( cur_j !== undefined ) { // might have fallen off array
251
+ if ( prev_j !== undefined && prev_j !== cur_i )
252
+ for ( let k = prev_j ; k >= cur_j && k >= cur_i ; k -- )
253
+ vis . array . selectColor ( k , partRColor )
254
+ if ( cur_j !== undefined && cur_j > cur_i ) { // might have fallen off array
244
255
highlight ( vis , cur_j ) ;
245
256
// XXX probably best avoid updateBinary at swap
246
257
updateBinary ( vis , arr [ cur_j ] )
@@ -250,10 +261,14 @@ export default {
250
261
assignVariable ( vis , VIS_VARIABLE_STRINGS . left , left ) ;
251
262
if ( right >= 0 )
252
263
assignVariable ( vis , VIS_VARIABLE_STRINGS . right , right ) ;
253
- assignVariable ( vis , VIS_VARIABLE_STRINGS . i_left_index , cur_i ) ;
254
- assignVariable ( vis , VIS_VARIABLE_STRINGS . i_gt_n , cur_i_too_high ) ;
255
- assignVariable ( vis , VIS_VARIABLE_STRINGS . j_right_index , cur_j ) ;
256
- assignVariable ( vis , VIS_VARIABLE_STRINGS . j_eq_0 , cur_j_too_low ) ;
264
+ if ( isPartitionExpanded ( ) || isRecursionExpanded ( ) ) {
265
+ assignVariable ( vis , VIS_VARIABLE_STRINGS . i_left_index , cur_i ) ;
266
+ assignVariable ( vis , VIS_VARIABLE_STRINGS . i_gt_n , cur_i_too_high ) ;
267
+ }
268
+ if ( isPartitionExpanded ( ) ) {
269
+ assignVariable ( vis , VIS_VARIABLE_STRINGS . j_right_index , cur_j ) ;
270
+ assignVariable ( vis , VIS_VARIABLE_STRINGS . j_eq_0 , cur_j_too_low ) ;
271
+ }
257
272
} ;
258
273
259
274
@@ -345,6 +360,8 @@ cur_i, cur_j, cur_depth, A) => {
345
360
346
361
vis . array . swapElements ( _n1 , _n2 ) ;
347
362
refreshStack ( vis , cur_real_stack , cur_finished_stack_frames , cur_i , cur_j , prev_i , prev_j , left , right , cur_depth , false , undefined , A , mask )
363
+ vis . array . selectColor ( _n1 , partLColor ) ;
364
+ vis . array . selectColor ( _n2 , partRColor ) ;
348
365
// redo poppers: swapping the elements keeps the
349
366
// contents of the poppers correct but the position needs
350
367
// to change. The documentation suggests update()
@@ -410,7 +427,18 @@ arr],
410
427
// Base case: If the array has 1 or fewer elements or mask is less than 0, stop
411
428
partitionChunker ( MSD_BOOKMARKS . rec_function , undefined , undefined , undefined , undefined , left , right , depth , arr , mask )
412
429
maxIndex = undefined ; // defined only for top level call
413
- chunker . add ( MSD_BOOKMARKS . base_case , ( vis ) => { } , [ ] , depth )
430
+ chunker . add ( MSD_BOOKMARKS . base_case , ( vis , l , r ) => {
431
+ if ( left < right && mask >= 0 )
432
+ // need to sort -> remove color
433
+ for ( let k = left ; k <= right && k < n ; k ++ ) {
434
+ vis . array . deselect ( k ) ;
435
+ }
436
+ else
437
+ // done -> sortedColor
438
+ for ( let k = left ; k <= right && k < n ; k ++ ) {
439
+ vis . array . selectColor ( k , sortedColor ) ;
440
+ }
441
+ } , [ left , right ] , depth )
414
442
415
443
if ( left < right && mask >= 0 ) {
416
444
// partition leaves final i and j highlighted, sets prev_i,
@@ -534,8 +562,8 @@ arr],
534
562
chunker . add ( MSD_BOOKMARKS . done ,
535
563
vis => {
536
564
vis . array . setStackDepth ( 0 )
537
- for ( let i = 0 ; i < n ; i ++ ) {
538
- vis . array . sorted ( i ) ;
565
+ for ( let k = 0 ; k < n ; k ++ ) {
566
+ vis . array . sorted ( k ) ;
539
567
}
540
568
vis . array . clearVariables ( ) ;
541
569
vis . array . setStack ( deriveStack ( real_stack , finished_stack_frames ) ) ;
0 commit comments