@@ -3,13 +3,20 @@ import ArrayTracer from '../../components/DataStructures/Array/Array1DTracer';
3
3
import MaskTracer from '../../components/DataStructures/Mask/MaskTracer' ;
4
4
import { areExpanded } from './collapseChunkPlugin' ;
5
5
import { createPopper } from '@popperjs/core' ;
6
+ import { colors } from '../../components/DataStructures/colors' ;
6
7
7
8
// radix must be a power of two; we use radix 4 here but code should work
8
9
// 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
10
12
const RADIX_BITS = 2 ;
11
13
const RADIX = 1 << RADIX_BITS ;
12
14
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
+
13
20
const SRS_BOOKMARKS = {
14
21
radix_sort : 1 ,
15
22
max_number : 2 ,
@@ -37,19 +44,28 @@ const isCountExpanded = () => {
37
44
38
45
const highlight = ( array , index , isPrimaryColor = true ) => {
39
46
if ( isPrimaryColor ) {
40
- array . select ( index ) ;
47
+ array . selectColor ( index , highlightColor ) ;
41
48
} else {
42
- array . patch ( index ) ;
49
+ array . selectColor ( index , changedColor ) ;
43
50
}
44
51
} ;
45
52
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
+ }
51
60
}
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
+ }
53
69
54
70
const updateMask = ( vis , index , bits ) => {
55
71
const mask = ( ( 1 << bits ) - 1 ) << ( index * bits ) ;
@@ -120,42 +136,38 @@ export default {
120
136
( vis , count ) => {
121
137
if ( isCountExpanded ( ) ) {
122
138
setArray ( vis . countArray , count ) ;
139
+ colorCounts ( vis . countArray , RADIX ) ;
123
140
}
124
141
} ,
125
142
[ count ]
126
143
) ;
127
144
128
145
for ( let i = 0 ; i < n ; i ++ ) {
129
146
chunker . add ( SRS_BOOKMARKS . add_count_for_loop ,
130
- ( vis , i , lastBit , count ) => {
147
+ ( vis , i , lastBit , count , A , n , k ) => {
131
148
if ( isCountExpanded ( ) ) {
132
149
setArray ( vis . countArray , count ) ;
150
+ colorCounts ( vis . countArray , RADIX ) ;
133
151
}
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 ) ;
142
153
highlight ( vis . array , i ) ;
143
154
updateBinary ( vis , A [ i ] ) ;
144
155
} ,
145
- [ i , lastBit , count ]
156
+ [ i , lastBit , count , A , n , k ]
146
157
) ;
147
158
148
159
const bit = bitsAtIndex ( A [ i ] , k , radixBits ) ;
149
160
count [ bit ] ++ ;
150
161
151
162
chunker . add ( SRS_BOOKMARKS . add_to_count ,
152
- ( vis , count ) => {
163
+ ( vis , i , count , A , n , k ) => {
153
164
if ( isCountExpanded ( ) ) {
154
165
setArray ( vis . countArray , count ) ;
155
- highlight ( vis . countArray , bit ) ;
166
+ colorCounts ( vis . countArray , RADIX ) ;
156
167
}
168
+ colorDigits ( A , vis . array , k , n ) ;
157
169
} ,
158
- [ count ]
170
+ [ i , count , A , n , k ]
159
171
) ;
160
172
161
173
lastBit = bit ;
@@ -176,19 +188,13 @@ export default {
176
188
177
189
for ( let i = 1 ; i < count . length ; i ++ ) {
178
190
chunker . add ( SRS_BOOKMARKS . cum_sum_for_loop ,
179
- ( vis , i , n , lastBit ) => {
191
+ ( vis , i , A , n , lastBit ) => {
180
192
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 ) ;
188
194
highlight ( vis . countArray , i ) ;
189
195
}
190
196
} ,
191
- [ i , n , lastBit ]
197
+ [ i , A , n , lastBit ]
192
198
) ;
193
199
194
200
count [ i ] += count [ i - 1 ] ;
@@ -197,7 +203,7 @@ export default {
197
203
( vis , count , i ) => {
198
204
if ( isCountExpanded ( ) ) {
199
205
setArray ( vis . countArray , count ) ;
200
- highlight ( vis . countArray , i , false ) ;
206
+ colorCounts ( vis . countArray , RADIX ) ;
201
207
}
202
208
} ,
203
209
[ count , i ]
@@ -224,54 +230,60 @@ export default {
224
230
for ( let i = n - 1 ; i >= 0 ; i -- ) {
225
231
const num = A [ i ] ;
226
232
chunker . add ( SRS_BOOKMARKS . populate_for_loop ,
227
- ( vis , num , i , bit , count , sortedA ) => {
233
+ ( vis , num , i , bit , count , A , sortedA , k , n ) => {
228
234
if ( i === n - 1 ) {
229
235
if ( isCountExpanded ( ) ) {
230
- unhighlight ( vis . countArray , count . length - 1 , false ) ;
236
+ // unhighlight(vis.countArray, count.length - 1, false);
231
237
}
232
238
} else {
233
- unhighlight ( vis . array , i + 1 ) ;
239
+ colorDigits ( A , vis . array , k , n ) ;
234
240
if ( isCountExpanded ( ) ) {
235
241
setArray ( vis . countArray , count ) ;
242
+ colorCounts ( vis . countArray , RADIX ) ;
236
243
setArray ( vis . tempArray , sortedA ) ;
237
- unhighlight ( vis . countArray , bit ) ;
238
- unhighlight ( vis . tempArray , count [ bit ] ) ;
244
+ colorDigits ( sortedA , vis . tempArray , k , n ) ;
239
245
}
240
246
}
241
247
updateBinary ( vis , num ) ;
242
- highlight ( vis . array , i ) ;
248
+ // highlight(vis.array, i);
243
249
} ,
244
- [ num , i , bit , count , sortedA ]
250
+ [ num , i , bit , count , A , sortedA , k , n ]
245
251
) ;
246
252
bit = bitsAtIndex ( num , k , radixBits ) ;
247
253
count [ bit ] -- ;
248
254
chunker . add ( SRS_BOOKMARKS . dec_count ,
249
- ( vis , num , i , bit , count , sortedA ) => {
255
+ ( vis , num , i , bit , count , A , sortedA , k , n ) => {
250
256
251
257
if ( isCountExpanded ( ) ) {
252
258
setArray ( vis . countArray , count ) ;
253
- highlight ( vis . countArray , bit ) ;
259
+ colorCounts ( vis . countArray , RADIX ) ;
260
+ // highlight(vis.countArray, bit);
254
261
}
262
+ colorDigits ( A , vis . array , k , n ) ;
255
263
} ,
256
- [ num , i , bit , count , sortedA ]
264
+ [ num , i , bit , count , A , sortedA , k , n ]
257
265
) ;
258
266
sortedA [ count [ bit ] ] = num ;
267
+ A [ i ] = undefined ; // blank out array element
259
268
chunker . add ( SRS_BOOKMARKS . insert_into_array ,
260
- ( vis , num , i , bit , count , sortedA ) => {
269
+ ( vis , num , i , bit , count , A , sortedA , k , n ) => {
261
270
271
+ setArray ( vis . array , A ) ;
272
+ colorDigits ( A , vis . array , k , n ) ;
262
273
if ( isCountExpanded ( ) ) {
263
274
setArray ( vis . tempArray , sortedA ) ;
264
- highlight ( vis . tempArray , count [ bit ] ) ;
275
+ colorDigits ( sortedA , vis . tempArray , k , n ) ;
276
+ // highlight(vis.tempArray, count[bit]);
265
277
}
266
278
} ,
267
- [ num , i , bit , count , sortedA ]
279
+ [ num , i , bit , count , A , sortedA , k , n ]
268
280
) ;
269
281
}
270
282
271
283
chunker . add ( SRS_BOOKMARKS . copy ,
272
- ( vis , array , n , countLength , bits ) => {
284
+ ( vis , array , n , countLength , k ) => {
273
285
setArray ( vis . array , array ) ;
274
-
286
+ colorDigits ( array , vis . array , k , n ) ;
275
287
if ( isCountExpanded ( ) ) {
276
288
setArray ( vis . tempArray , Array . apply ( null , Array ( n ) ) . map ( ( ) => undefined ) ) ;
277
289
setArray ( vis . countArray , Array . apply ( null , Array ( countLength ) ) . map ( ( ) => undefined ) ) ;
@@ -284,7 +296,7 @@ export default {
284
296
}
285
297
} , DELAY_POPPER_UPDATE ) ;
286
298
} ,
287
- [ sortedA , n , count . length , bits ]
299
+ [ sortedA , n , count . length , k ]
288
300
) ;
289
301
290
302
return sortedA ;
@@ -310,6 +322,7 @@ export default {
310
322
311
323
if ( isCountExpanded ( ) ) {
312
324
setArray ( vis . countArray , Array . apply ( null , Array ( 1 << RADIX_BITS ) ) . map ( ( ) => undefined ) ) ;
325
+ colorCounts ( vis . countArray , RADIX ) ;
313
326
setArray ( vis . tempArray , Array . apply ( null , Array ( n ) ) . map ( ( ) => undefined ) ) ;
314
327
}
315
328
// create poppers or reset poppers if they already exist
@@ -358,9 +371,14 @@ export default {
358
371
359
372
for ( let k = 0 ; k < bits / RADIX_BITS ; k ++ ) {
360
373
chunker . add ( SRS_BOOKMARKS . counting_sort_for_loop ,
361
- vis => {
374
+ ( vis , A , k ) => {
362
375
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 ) ] ) ;
363
379
}
380
+ } ,
381
+ [ A , k ]
364
382
) ;
365
383
366
384
A = countingSort ( A , k , n , RADIX_BITS ) ;
0 commit comments