1
+ // Some animation deleted, some added; could delete some commented out stuff
1
2
import ArrayTracer from '../../components/DataStructures/Array/Array1DTracer' ;
2
3
import MaskTracer from '../../components/DataStructures/Mask/MaskTracer' ;
3
4
import { areExpanded } from './collapseChunkPlugin' ;
@@ -8,10 +9,10 @@ const SRS_BOOKMARKS = {
8
9
radix_sort : 1 ,
9
10
max_number : 2 ,
10
11
counting_sort_for_loop : 3 ,
11
- counting_sort : 4 ,
12
- count_nums : 5 ,
13
- cumulative_sum : 6 ,
14
- populate_array : 7 ,
12
+ // counting_sort: 4, // no longer used
13
+ // count_nums: 5, // no longer used
14
+ // cumulative_sum: 6, // no longer used
15
+ // populate_array: 7, // no longer used
15
16
populate_for_loop : 8 ,
16
17
insert_into_array : 9 ,
17
18
copy : 10 ,
@@ -20,6 +21,9 @@ const SRS_BOOKMARKS = {
20
21
add_count_for_loop : 13 ,
21
22
cum_sum_for_loop : 14 ,
22
23
add_cum_sum : 15 ,
24
+ zero_counts : 16 ,
25
+ get_digit1 : 17 ,
26
+ dec_count : 18 ,
23
27
} ;
24
28
25
29
const isCountExpanded = ( ) => {
@@ -63,6 +67,8 @@ const bitsAtIndex = (num, index, bits) => {
63
67
return num >> ( index * bits ) & ( ( 1 << bits ) - 1 ) ;
64
68
} ;
65
69
70
+ // could check that isArray is defined and avoid some of the
71
+ // isCountExpanded() checks elsewhere
66
72
const setArray = ( visArray , array ) => {
67
73
visArray . set ( array , 'straightRadixSort' ) ;
68
74
} ;
@@ -83,11 +89,21 @@ export default {
83
89
const count = Array . apply ( null , Array ( 1 << bits ) ) . map ( ( ) => 0 ) ;
84
90
let lastBit = - 1 ;
85
91
86
- chunker . add ( SRS_BOOKMARKS . count_nums ) ;
92
+ chunker . add ( SRS_BOOKMARKS . zero_counts ,
93
+ ( vis , count ) => {
94
+ if ( isCountExpanded ( ) ) {
95
+ setArray ( vis . countArray , count ) ;
96
+ }
97
+ } ,
98
+ [ count ]
99
+ ) ;
87
100
88
101
for ( let i = 0 ; i < n ; i ++ ) {
89
102
chunker . add ( SRS_BOOKMARKS . add_count_for_loop ,
90
- ( vis , i , lastBit ) => {
103
+ ( vis , i , lastBit , count ) => {
104
+ if ( isCountExpanded ( ) ) {
105
+ setArray ( vis . countArray , count ) ;
106
+ }
91
107
if ( i !== 0 ) {
92
108
unhighlight ( vis . array , i - 1 ) ;
93
109
}
@@ -99,7 +115,7 @@ export default {
99
115
highlight ( vis . array , i ) ;
100
116
updateBinary ( vis , A [ i ] ) ;
101
117
} ,
102
- [ i , lastBit ]
118
+ [ i , lastBit , count ]
103
119
) ;
104
120
105
121
const bit = bitsAtIndex ( A [ i ] , k , bits ) ;
@@ -118,6 +134,7 @@ export default {
118
134
lastBit = bit ;
119
135
}
120
136
137
+ /*
121
138
chunker.add(SRS_BOOKMARKS.cumulative_sum,
122
139
(vis, n, lastBit) => {
123
140
unhighlight(vis.array, n - 1);
@@ -128,15 +145,23 @@ export default {
128
145
},
129
146
[n, lastBit]
130
147
);
148
+ */
131
149
132
150
for ( let i = 1 ; i < count . length ; i ++ ) {
133
151
chunker . add ( SRS_BOOKMARKS . cum_sum_for_loop ,
134
- ( vis , i ) => {
135
- if ( i === 1 && isCountExpanded ( ) ) {
136
- highlight ( vis . countArray , 0 ) ;
152
+ ( vis , i , n , lastBit ) => {
153
+ if ( isCountExpanded ( ) ) {
154
+ if ( i === 1 ) {
155
+ unhighlight ( vis . array , n - 1 ) ;
156
+ } else
157
+ unhighlight ( vis . countArray , i - 1 , false ) ;
158
+ if ( i === 1 && isCountExpanded ( ) ) {
159
+ unhighlight ( vis . countArray , lastBit ) ;
160
+ }
161
+ highlight ( vis . countArray , i ) ;
137
162
}
138
163
} ,
139
- [ i ]
164
+ [ i , n , lastBit ]
140
165
) ;
141
166
142
167
count [ i ] += count [ i - 1 ] ;
@@ -145,15 +170,16 @@ export default {
145
170
( vis , count , i ) => {
146
171
if ( isCountExpanded ( ) ) {
147
172
setArray ( vis . countArray , count ) ;
148
- highlight ( vis . countArray , i ) ;
173
+ highlight ( vis . countArray , i , false ) ;
149
174
}
150
175
} ,
151
176
[ count , i ]
152
177
)
153
178
}
154
179
155
- const sortedA = Array . apply ( null , Array ( n ) ) . map ( ( ) => 0 ) ;
180
+ const sortedA = Array . apply ( null , Array ( n ) ) . map ( ( ) => undefined ) ;
156
181
182
+ /*
157
183
chunker.add(SRS_BOOKMARKS.populate_array,
158
184
(vis, countLength) => {
159
185
if (isCountExpanded()) {
@@ -162,31 +188,54 @@ export default {
162
188
},
163
189
[count.length]
164
190
);
191
+ */
165
192
166
- chunker . add ( SRS_BOOKMARKS . populate_for_loop ) ;
193
+ // chunker.add(SRS_BOOKMARKS.populate_for_loop);
167
194
168
195
let bit ;
169
196
170
197
for ( let i = n - 1 ; i >= 0 ; i -- ) {
171
198
const num = A [ i ] ;
199
+ chunker . add ( SRS_BOOKMARKS . populate_for_loop ,
200
+ ( vis , num , i , bit , count , sortedA ) => {
201
+ if ( i === n - 1 ) {
202
+ if ( isCountExpanded ( ) ) {
203
+ unhighlight ( vis . countArray , count . length - 1 , false ) ;
204
+ }
205
+ } else {
206
+ unhighlight ( vis . array , i + 1 ) ;
207
+ if ( isCountExpanded ( ) ) {
208
+ setArray ( vis . countArray , count ) ;
209
+ setArray ( vis . tempArray , sortedA ) ;
210
+ unhighlight ( vis . countArray , bit ) ;
211
+ unhighlight ( vis . tempArray , count [ bit ] ) ;
212
+ }
213
+ }
214
+ updateBinary ( vis , num ) ;
215
+ highlight ( vis . array , i ) ;
216
+ } ,
217
+ [ num , i , bit , count , sortedA ]
218
+ ) ;
172
219
bit = bitsAtIndex ( num , k , bits ) ;
173
220
count [ bit ] -- ;
221
+ chunker . add ( SRS_BOOKMARKS . dec_count ,
222
+ ( vis , num , i , bit , count , sortedA ) => {
223
+
224
+ if ( isCountExpanded ( ) ) {
225
+ setArray ( vis . countArray , count ) ;
226
+ highlight ( vis . countArray , bit ) ;
227
+ }
228
+ } ,
229
+ [ num , i , bit , count , sortedA ]
230
+ ) ;
174
231
sortedA [ count [ bit ] ] = num ;
175
232
chunker . add ( SRS_BOOKMARKS . insert_into_array ,
176
233
( vis , num , i , bit , count , sortedA ) => {
177
- if ( i !== n - 1 ) {
178
- unhighlight ( vis . array , i + 1 ) ;
179
- }
180
234
181
235
if ( isCountExpanded ( ) ) {
182
- setArray ( vis . countArray , count ) ;
183
236
setArray ( vis . tempArray , sortedA ) ;
184
- highlight ( vis . countArray , bit ) ;
185
237
highlight ( vis . tempArray , count [ bit ] ) ;
186
238
}
187
-
188
- updateBinary ( vis , num ) ;
189
- highlight ( vis . array , i ) ;
190
239
} ,
191
240
[ num , i , bit , count , sortedA ]
192
241
) ;
@@ -249,7 +298,7 @@ export default {
249
298
250
299
A = countingSort ( A , k , n , BITS ) ;
251
300
252
- chunker . add ( SRS_BOOKMARKS . counting_sort ) ;
301
+ // chunker.add(SRS_BOOKMARKS.counting_sort);
253
302
}
254
303
255
304
chunker . add ( SRS_BOOKMARKS . done ,
@@ -273,15 +322,15 @@ export function initVisualisers() {
273
322
order : 0 ,
274
323
} ,
275
324
array : {
276
- instance : new ArrayTracer ( 'array' , null , 'Array view ' , { arrayItemMagnitudes : false } ) ,
325
+ instance : new ArrayTracer ( 'array' , null , 'Array A ' , { arrayItemMagnitudes : false } ) ,
277
326
order : 1 ,
278
327
} ,
279
328
countArray : {
280
329
instance : new ArrayTracer ( 'countArray' , null , 'Count array' , { arrayItemMagnitudes : false } ) ,
281
330
order : 1 ,
282
331
} ,
283
332
tempArray : {
284
- instance : new ArrayTracer ( 'tempArray' , null , 'Temp array' , { arrayItemMagnitudes : false } ) ,
333
+ instance : new ArrayTracer ( 'tempArray' , null , 'Temp array B ' , { arrayItemMagnitudes : false } ) ,
285
334
order : 1 ,
286
335
} ,
287
336
} ;
@@ -292,9 +341,9 @@ export function initVisualisers() {
292
341
order : 0 ,
293
342
} ,
294
343
array : {
295
- instance : new ArrayTracer ( 'array' , null , 'Array view ' , { arrayItemMagnitudes : true } ) ,
344
+ instance : new ArrayTracer ( 'array' , null , 'Array A ' , { arrayItemMagnitudes : true } ) ,
296
345
order : 1 ,
297
346
} ,
298
347
} ;
299
348
}
300
- }
349
+ }
0 commit comments