1+ // Some color code could be cleaned up
2+ // Could share some code with straight radix sort and quicksort
13import ArrayTracer from '../../components/DataStructures/Array/Array1DTracer' ;
24import MaskTracer from '../../components/DataStructures/Mask/MaskTracer'
35import {
46 areExpanded ,
57} from './collapseChunkPlugin' ;
68import { 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,...
715
816// see stackFrameColour in Array1DRenderer/index.js to find corresponding function mapping to css
917const STACK_FRAME_COLOR = {
@@ -85,20 +93,12 @@ const isRecursionExpanded = () => {
8593 return areExpanded ( [ 'MSDRadixSortLeft' ] ) || areExpanded ( [ 'MSDRadixSortRight' ] ) ;
8694}
8795
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+ } ;
9599
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 ) ;
102102} ;
103103
104104const updateMask = ( vis , value ) => {
@@ -194,6 +194,8 @@ export default {
194194 assert ( vis . array ) ;
195195 assert ( cur_real_stack && cur_finished_stack_frames ) ;
196196
197+ // conditions for i,j display moved elsewhere
198+ /*
197199 if (!isPartitionExpanded()) {
198200 // j should not show up in vis if partition is collapsed
199201 cur_j = undefined;
@@ -205,6 +207,7 @@ export default {
205207 cur_i = undefined;
206208 cur_i_too_high = undefined;
207209 }
210+ */
208211
209212 vis . array . setStackDepth ( cur_real_stack . length ) ;
210213 vis . array . setStack (
@@ -226,21 +229,29 @@ export default {
226229 // init, before partition
227230 highlight ( vis , cur_i )
228231 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 ) {
230233 // 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 ) ;
234240 } 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 )
238248 if ( arr && cur_i !== undefined )
239249 updateBinary ( vis , arr [ cur_i ] )
240250 } 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
244255 highlight ( vis , cur_j ) ;
245256 // XXX probably best avoid updateBinary at swap
246257 updateBinary ( vis , arr [ cur_j ] )
@@ -250,10 +261,14 @@ export default {
250261 assignVariable ( vis , VIS_VARIABLE_STRINGS . left , left ) ;
251262 if ( right >= 0 )
252263 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+ }
257272 } ;
258273
259274
@@ -345,6 +360,8 @@ cur_i, cur_j, cur_depth, A) => {
345360
346361 vis . array . swapElements ( _n1 , _n2 ) ;
347362 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 ) ;
348365 // redo poppers: swapping the elements keeps the
349366 // contents of the poppers correct but the position needs
350367 // to change. The documentation suggests update()
@@ -410,7 +427,18 @@ arr],
410427 // Base case: If the array has 1 or fewer elements or mask is less than 0, stop
411428 partitionChunker ( MSD_BOOKMARKS . rec_function , undefined , undefined , undefined , undefined , left , right , depth , arr , mask )
412429 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 )
414442
415443 if ( left < right && mask >= 0 ) {
416444 // partition leaves final i and j highlighted, sets prev_i,
@@ -534,8 +562,8 @@ arr],
534562 chunker . add ( MSD_BOOKMARKS . done ,
535563 vis => {
536564 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 ) ;
539567 }
540568 vis . array . clearVariables ( ) ;
541569 vis . array . setStack ( deriveStack ( real_stack , finished_stack_frames ) ) ;
0 commit comments