@@ -3,13 +3,20 @@ import ArrayTracer from '../../components/DataStructures/Array/Array1DTracer';
33import MaskTracer from '../../components/DataStructures/Mask/MaskTracer' ;
44import { areExpanded } from './collapseChunkPlugin' ;
55import { createPopper } from '@popperjs/core' ;
6+ import { colors } from '../../components/DataStructures/colors' ;
67
78// radix must be a power of two; we use radix 4 here but code should work
89// with another radix except vis.mask.setAddBase4() would need to be
9- // generalised and that call deleted if radix = 2
10+ // generalised and that call deleted if radix = 2, plus digitColor
11+ // would need generalising
1012const RADIX_BITS = 2 ;
1113const RADIX = 1 << RADIX_BITS ;
1214
15+ const highlightColor = colors . apple ; // various highlights
16+ const changedColor = colors . wood ; // for cumulative sums
17+ // colors for the 4 digits
18+ const digitColor = [ colors . peach , colors . leaf , colors . sky , colors . plum ] ;
19+
1320const SRS_BOOKMARKS = {
1421 radix_sort : 1 ,
1522 max_number : 2 ,
@@ -37,19 +44,28 @@ const isCountExpanded = () => {
3744
3845const highlight = ( array , index , isPrimaryColor = true ) => {
3946 if ( isPrimaryColor ) {
40- array . select ( index ) ;
47+ array . selectColor ( index , highlightColor ) ;
4148 } else {
42- array . patch ( index ) ;
49+ array . selectColor ( index , changedColor ) ;
4350 }
4451} ;
4552
46- const unhighlight = ( array , index , isPrimaryColor = true ) => {
47- if ( isPrimaryColor ) {
48- array . deselect ( index ) ;
49- } else {
50- array . depatch ( index ) ;
53+ // color all digits in array according to digit value
54+ const colorDigits = ( A , visA , digit , n ) => {
55+ for ( let i = 0 ; i < n ; i ++ ) {
56+ if ( A [ i ] !== undefined ) {
57+ visA . deselect ( i ) ;
58+ visA . selectColor ( i , digitColor [ bitsAtIndex ( A [ i ] , digit , RADIX_BITS ) ] ) ;
59+ }
5160 }
52- } ;
61+ }
62+
63+ // color all counts in array according to index
64+ const colorCounts = ( visA , n ) => {
65+ for ( let i = 0 ; i < n ; i ++ ) {
66+ visA . selectColor ( i , digitColor [ i ] ) ;
67+ }
68+ }
5369
5470const updateMask = ( vis , index , bits ) => {
5571 const mask = ( ( 1 << bits ) - 1 ) << ( index * bits ) ;
@@ -120,42 +136,38 @@ export default {
120136 ( vis , count ) => {
121137 if ( isCountExpanded ( ) ) {
122138 setArray ( vis . countArray , count ) ;
139+ colorCounts ( vis . countArray , RADIX ) ;
123140 }
124141 } ,
125142 [ count ]
126143 ) ;
127144
128145 for ( let i = 0 ; i < n ; i ++ ) {
129146 chunker . add ( SRS_BOOKMARKS . add_count_for_loop ,
130- ( vis , i , lastBit , count ) => {
147+ ( vis , i , lastBit , count , A , n , k ) => {
131148 if ( isCountExpanded ( ) ) {
132149 setArray ( vis . countArray , count ) ;
150+ colorCounts ( vis . countArray , RADIX ) ;
133151 }
134- if ( i !== 0 ) {
135- unhighlight ( vis . array , i - 1 ) ;
136- }
137-
138- if ( lastBit !== - 1 && isCountExpanded ( ) ) {
139- unhighlight ( vis . countArray , lastBit ) ;
140- }
141-
152+ colorDigits ( A , vis . array , k , n ) ;
142153 highlight ( vis . array , i ) ;
143154 updateBinary ( vis , A [ i ] ) ;
144155 } ,
145- [ i , lastBit , count ]
156+ [ i , lastBit , count , A , n , k ]
146157 ) ;
147158
148159 const bit = bitsAtIndex ( A [ i ] , k , radixBits ) ;
149160 count [ bit ] ++ ;
150161
151162 chunker . add ( SRS_BOOKMARKS . add_to_count ,
152- ( vis , count ) => {
163+ ( vis , i , count , A , n , k ) => {
153164 if ( isCountExpanded ( ) ) {
154165 setArray ( vis . countArray , count ) ;
155- highlight ( vis . countArray , bit ) ;
166+ colorCounts ( vis . countArray , RADIX ) ;
156167 }
168+ colorDigits ( A , vis . array , k , n ) ;
157169 } ,
158- [ count ]
170+ [ i , count , A , n , k ]
159171 ) ;
160172
161173 lastBit = bit ;
@@ -176,19 +188,13 @@ export default {
176188
177189 for ( let i = 1 ; i < count . length ; i ++ ) {
178190 chunker . add ( SRS_BOOKMARKS . cum_sum_for_loop ,
179- ( vis , i , n , lastBit ) => {
191+ ( vis , i , A , n , lastBit ) => {
180192 if ( isCountExpanded ( ) ) {
181- if ( i === 1 ) {
182- unhighlight ( vis . array , n - 1 ) ;
183- } else
184- unhighlight ( vis . countArray , i - 1 , false ) ;
185- if ( i === 1 && isCountExpanded ( ) ) {
186- unhighlight ( vis . countArray , lastBit ) ;
187- }
193+ colorCounts ( vis . countArray , RADIX ) ;
188194 highlight ( vis . countArray , i ) ;
189195 }
190196 } ,
191- [ i , n , lastBit ]
197+ [ i , A , n , lastBit ]
192198 ) ;
193199
194200 count [ i ] += count [ i - 1 ] ;
@@ -197,7 +203,7 @@ export default {
197203 ( vis , count , i ) => {
198204 if ( isCountExpanded ( ) ) {
199205 setArray ( vis . countArray , count ) ;
200- highlight ( vis . countArray , i , false ) ;
206+ colorCounts ( vis . countArray , RADIX ) ;
201207 }
202208 } ,
203209 [ count , i ]
@@ -224,54 +230,60 @@ export default {
224230 for ( let i = n - 1 ; i >= 0 ; i -- ) {
225231 const num = A [ i ] ;
226232 chunker . add ( SRS_BOOKMARKS . populate_for_loop ,
227- ( vis , num , i , bit , count , sortedA ) => {
233+ ( vis , num , i , bit , count , A , sortedA , k , n ) => {
228234 if ( i === n - 1 ) {
229235 if ( isCountExpanded ( ) ) {
230- unhighlight ( vis . countArray , count . length - 1 , false ) ;
236+ // unhighlight(vis.countArray, count.length - 1, false);
231237 }
232238 } else {
233- unhighlight ( vis . array , i + 1 ) ;
239+ colorDigits ( A , vis . array , k , n ) ;
234240 if ( isCountExpanded ( ) ) {
235241 setArray ( vis . countArray , count ) ;
242+ colorCounts ( vis . countArray , RADIX ) ;
236243 setArray ( vis . tempArray , sortedA ) ;
237- unhighlight ( vis . countArray , bit ) ;
238- unhighlight ( vis . tempArray , count [ bit ] ) ;
244+ colorDigits ( sortedA , vis . tempArray , k , n ) ;
239245 }
240246 }
241247 updateBinary ( vis , num ) ;
242- highlight ( vis . array , i ) ;
248+ // highlight(vis.array, i);
243249 } ,
244- [ num , i , bit , count , sortedA ]
250+ [ num , i , bit , count , A , sortedA , k , n ]
245251 ) ;
246252 bit = bitsAtIndex ( num , k , radixBits ) ;
247253 count [ bit ] -- ;
248254 chunker . add ( SRS_BOOKMARKS . dec_count ,
249- ( vis , num , i , bit , count , sortedA ) => {
255+ ( vis , num , i , bit , count , A , sortedA , k , n ) => {
250256
251257 if ( isCountExpanded ( ) ) {
252258 setArray ( vis . countArray , count ) ;
253- highlight ( vis . countArray , bit ) ;
259+ colorCounts ( vis . countArray , RADIX ) ;
260+ // highlight(vis.countArray, bit);
254261 }
262+ colorDigits ( A , vis . array , k , n ) ;
255263 } ,
256- [ num , i , bit , count , sortedA ]
264+ [ num , i , bit , count , A , sortedA , k , n ]
257265 ) ;
258266 sortedA [ count [ bit ] ] = num ;
267+ A [ i ] = undefined ; // blank out array element
259268 chunker . add ( SRS_BOOKMARKS . insert_into_array ,
260- ( vis , num , i , bit , count , sortedA ) => {
269+ ( vis , num , i , bit , count , A , sortedA , k , n ) => {
261270
271+ setArray ( vis . array , A ) ;
272+ colorDigits ( A , vis . array , k , n ) ;
262273 if ( isCountExpanded ( ) ) {
263274 setArray ( vis . tempArray , sortedA ) ;
264- highlight ( vis . tempArray , count [ bit ] ) ;
275+ colorDigits ( sortedA , vis . tempArray , k , n ) ;
276+ // highlight(vis.tempArray, count[bit]);
265277 }
266278 } ,
267- [ num , i , bit , count , sortedA ]
279+ [ num , i , bit , count , A , sortedA , k , n ]
268280 ) ;
269281 }
270282
271283 chunker . add ( SRS_BOOKMARKS . copy ,
272- ( vis , array , n , countLength , bits ) => {
284+ ( vis , array , n , countLength , k ) => {
273285 setArray ( vis . array , array ) ;
274-
286+ colorDigits ( array , vis . array , k , n ) ;
275287 if ( isCountExpanded ( ) ) {
276288 setArray ( vis . tempArray , Array . apply ( null , Array ( n ) ) . map ( ( ) => undefined ) ) ;
277289 setArray ( vis . countArray , Array . apply ( null , Array ( countLength ) ) . map ( ( ) => undefined ) ) ;
@@ -284,7 +296,7 @@ export default {
284296 }
285297 } , DELAY_POPPER_UPDATE ) ;
286298 } ,
287- [ sortedA , n , count . length , bits ]
299+ [ sortedA , n , count . length , k ]
288300 ) ;
289301
290302 return sortedA ;
@@ -310,6 +322,7 @@ export default {
310322
311323 if ( isCountExpanded ( ) ) {
312324 setArray ( vis . countArray , Array . apply ( null , Array ( 1 << RADIX_BITS ) ) . map ( ( ) => undefined ) ) ;
325+ colorCounts ( vis . countArray , RADIX ) ;
313326 setArray ( vis . tempArray , Array . apply ( null , Array ( n ) ) . map ( ( ) => undefined ) ) ;
314327 }
315328 // create poppers or reset poppers if they already exist
@@ -358,9 +371,14 @@ export default {
358371
359372 for ( let k = 0 ; k < bits / RADIX_BITS ; k ++ ) {
360373 chunker . add ( SRS_BOOKMARKS . counting_sort_for_loop ,
361- vis => {
374+ ( vis , A , k ) => {
362375 updateMask ( vis , k , RADIX_BITS ) ;
376+ for ( let i = 0 ; i < n ; i ++ ) {
377+ vis . array . deselect ( i ) ;
378+ vis . array . selectColor ( i , digitColor [ bitsAtIndex ( A [ i ] , k , RADIX_BITS ) ] ) ;
363379 }
380+ } ,
381+ [ A , k ]
364382 ) ;
365383
366384 A = countingSort ( A , k , n , RADIX_BITS ) ;
0 commit comments