1+ // Some animation deleted, some added; could delete some commented out stuff
12import ArrayTracer from '../../components/DataStructures/Array/Array1DTracer' ;
23import MaskTracer from '../../components/DataStructures/Mask/MaskTracer' ;
34import { areExpanded } from './collapseChunkPlugin' ;
@@ -8,10 +9,10 @@ const SRS_BOOKMARKS = {
89 radix_sort : 1 ,
910 max_number : 2 ,
1011 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
1516 populate_for_loop : 8 ,
1617 insert_into_array : 9 ,
1718 copy : 10 ,
@@ -20,6 +21,9 @@ const SRS_BOOKMARKS = {
2021 add_count_for_loop : 13 ,
2122 cum_sum_for_loop : 14 ,
2223 add_cum_sum : 15 ,
24+ zero_counts : 16 ,
25+ get_digit1 : 17 ,
26+ dec_count : 18 ,
2327} ;
2428
2529const isCountExpanded = ( ) => {
@@ -63,6 +67,8 @@ const bitsAtIndex = (num, index, bits) => {
6367 return num >> ( index * bits ) & ( ( 1 << bits ) - 1 ) ;
6468} ;
6569
70+ // could check that isArray is defined and avoid some of the
71+ // isCountExpanded() checks elsewhere
6672const setArray = ( visArray , array ) => {
6773 visArray . set ( array , 'straightRadixSort' ) ;
6874} ;
@@ -83,11 +89,21 @@ export default {
8389 const count = Array . apply ( null , Array ( 1 << bits ) ) . map ( ( ) => 0 ) ;
8490 let lastBit = - 1 ;
8591
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+ ) ;
87100
88101 for ( let i = 0 ; i < n ; i ++ ) {
89102 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+ }
91107 if ( i !== 0 ) {
92108 unhighlight ( vis . array , i - 1 ) ;
93109 }
@@ -99,7 +115,7 @@ export default {
99115 highlight ( vis . array , i ) ;
100116 updateBinary ( vis , A [ i ] ) ;
101117 } ,
102- [ i , lastBit ]
118+ [ i , lastBit , count ]
103119 ) ;
104120
105121 const bit = bitsAtIndex ( A [ i ] , k , bits ) ;
@@ -118,6 +134,7 @@ export default {
118134 lastBit = bit ;
119135 }
120136
137+ /*
121138 chunker.add(SRS_BOOKMARKS.cumulative_sum,
122139 (vis, n, lastBit) => {
123140 unhighlight(vis.array, n - 1);
@@ -128,15 +145,23 @@ export default {
128145 },
129146 [n, lastBit]
130147 );
148+ */
131149
132150 for ( let i = 1 ; i < count . length ; i ++ ) {
133151 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 ) ;
137162 }
138163 } ,
139- [ i ]
164+ [ i , n , lastBit ]
140165 ) ;
141166
142167 count [ i ] += count [ i - 1 ] ;
@@ -145,15 +170,16 @@ export default {
145170 ( vis , count , i ) => {
146171 if ( isCountExpanded ( ) ) {
147172 setArray ( vis . countArray , count ) ;
148- highlight ( vis . countArray , i ) ;
173+ highlight ( vis . countArray , i , false ) ;
149174 }
150175 } ,
151176 [ count , i ]
152177 )
153178 }
154179
155- const sortedA = Array . apply ( null , Array ( n ) ) . map ( ( ) => 0 ) ;
180+ const sortedA = Array . apply ( null , Array ( n ) ) . map ( ( ) => undefined ) ;
156181
182+ /*
157183 chunker.add(SRS_BOOKMARKS.populate_array,
158184 (vis, countLength) => {
159185 if (isCountExpanded()) {
@@ -162,31 +188,54 @@ export default {
162188 },
163189 [count.length]
164190 );
191+ */
165192
166- chunker . add ( SRS_BOOKMARKS . populate_for_loop ) ;
193+ // chunker.add(SRS_BOOKMARKS.populate_for_loop);
167194
168195 let bit ;
169196
170197 for ( let i = n - 1 ; i >= 0 ; i -- ) {
171198 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+ ) ;
172219 bit = bitsAtIndex ( num , k , bits ) ;
173220 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+ ) ;
174231 sortedA [ count [ bit ] ] = num ;
175232 chunker . add ( SRS_BOOKMARKS . insert_into_array ,
176233 ( vis , num , i , bit , count , sortedA ) => {
177- if ( i !== n - 1 ) {
178- unhighlight ( vis . array , i + 1 ) ;
179- }
180234
181235 if ( isCountExpanded ( ) ) {
182- setArray ( vis . countArray , count ) ;
183236 setArray ( vis . tempArray , sortedA ) ;
184- highlight ( vis . countArray , bit ) ;
185237 highlight ( vis . tempArray , count [ bit ] ) ;
186238 }
187-
188- updateBinary ( vis , num ) ;
189- highlight ( vis . array , i ) ;
190239 } ,
191240 [ num , i , bit , count , sortedA ]
192241 ) ;
@@ -249,7 +298,7 @@ export default {
249298
250299 A = countingSort ( A , k , n , BITS ) ;
251300
252- chunker . add ( SRS_BOOKMARKS . counting_sort ) ;
301+ // chunker.add(SRS_BOOKMARKS.counting_sort);
253302 }
254303
255304 chunker . add ( SRS_BOOKMARKS . done ,
@@ -273,15 +322,15 @@ export function initVisualisers() {
273322 order : 0 ,
274323 } ,
275324 array : {
276- instance : new ArrayTracer ( 'array' , null , 'Array view ' , { arrayItemMagnitudes : false } ) ,
325+ instance : new ArrayTracer ( 'array' , null , 'Array A ' , { arrayItemMagnitudes : false } ) ,
277326 order : 1 ,
278327 } ,
279328 countArray : {
280329 instance : new ArrayTracer ( 'countArray' , null , 'Count array' , { arrayItemMagnitudes : false } ) ,
281330 order : 1 ,
282331 } ,
283332 tempArray : {
284- instance : new ArrayTracer ( 'tempArray' , null , 'Temp array' , { arrayItemMagnitudes : false } ) ,
333+ instance : new ArrayTracer ( 'tempArray' , null , 'Temp array B ' , { arrayItemMagnitudes : false } ) ,
285334 order : 1 ,
286335 } ,
287336 } ;
@@ -292,9 +341,9 @@ export function initVisualisers() {
292341 order : 0 ,
293342 } ,
294343 array : {
295- instance : new ArrayTracer ( 'array' , null , 'Array view ' , { arrayItemMagnitudes : true } ) ,
344+ instance : new ArrayTracer ( 'array' , null , 'Array A ' , { arrayItemMagnitudes : true } ) ,
296345 order : 1 ,
297346 } ,
298347 } ;
299348 }
300- }
349+ }
0 commit comments