@@ -54,6 +54,7 @@ const MSD_BOOKMARKS = {
54
54
partition_right : 305 ,
55
55
swap_condition : 309 ,
56
56
swap : 310 ,
57
+ inc_dec : 'inc_dec' ,
57
58
pre_sort_left : 400 ,
58
59
sort_left : 401 ,
59
60
pre_sort_right : 500 ,
@@ -159,7 +160,13 @@ export default {
159
160
// where mid is undefined until after partition
160
161
const finished_stack_frames = [ ] ;
161
162
const real_stack = [ ] ;
162
- let leftCheck = false
163
+ // refreshStack does lots of work with highlighting etc but is
164
+ // called from other functions which are called from other
165
+ // function and somewhere along the line one of the functions is
166
+ // chunker.add. Often the arguments are not given explicitly -
167
+ // it's a mess. We assign the current bookmark to whereAreWe for
168
+ // now, to be used in refreshStack. Maybe worth a re-write XXX.
169
+ let whereAreWe ;
163
170
164
171
// ----------------------------------------------------------------------------------------------------------------------------
165
172
// Define helper functions
@@ -169,14 +176,12 @@ export default {
169
176
// This function is the only way information is cached and incremented properly in the while loop
170
177
const partitionChunker = ( bookmark , i , j , prev_i , prev_j , left , right , depth , arr , mask ) => {
171
178
assert ( bookmark !== undefined ) ; // helps catch bugs early, and trace them in stack
172
- const args_array = [ real_stack , finished_stack_frames , i , j , prev_i , prev_j , left , right , depth , leftCheck , maxIndex , arr , mask ]
179
+ const args_array = [ real_stack , finished_stack_frames , i , j , prev_i , prev_j , left , right , depth , whereAreWe , maxIndex , arr , mask ]
173
180
chunker . add ( bookmark , refreshStack , args_array , depth )
174
181
}
175
182
176
- // we use a global flag in case we scan through the whole partition
177
- // and stop at the end without finding the mask bit we are looking
178
- // for
179
- const refreshStack = ( vis , cur_real_stack , cur_finished_stack_frames , cur_i , cur_j , prev_i , prev_j , left , right , cur_depth , checkingLeft , maxIndex , arr , mask ) => {
183
+ // see comment above on whereAreWe
184
+ const refreshStack = ( vis , cur_real_stack , cur_finished_stack_frames , cur_i , cur_j , prev_i , prev_j , left , right , cur_depth , whereAreWe , maxIndex , arr , mask ) => {
180
185
// If we fall off the start/end of the array we just use the
181
186
// first/last element and give the actual value of j/i
182
187
let cur_i_too_high ;
@@ -198,17 +203,13 @@ export default {
198
203
assert ( vis . array ) ;
199
204
assert ( cur_real_stack && cur_finished_stack_frames ) ;
200
205
201
- vis . array . setStackDepth ( cur_real_stack . length ) ;
202
- vis . array . setStack (
203
- deriveStack ( cur_real_stack , cur_finished_stack_frames , cur_i , cur_j , cur_depth )
204
- ) ;
206
+ // XXX This was getting very messy - colors depends a
207
+ // lot on where we are and we had a bunch of tricky testing of
208
+ // various vars to determine that. Now we use whereAreWe to
209
+ // simplify some things at least.
205
210
206
- // XXX This is getting very messy - (un)highlighting depends a
207
- // lot on where we are and we have a bunch of tricky testing of
208
- // various vars to determine that. Could pass in bookmark to
209
- // simplify some things at least??
210
211
// Show the binary representation for the current index
211
- // plus (un)highlight appropriate element(s)
212
+ // plus color appropriate element(s)
212
213
updateMask ( vis , mask ) // only needed for start of recursive function
213
214
if ( maxIndex !== undefined ) { // top level call to recursive fn
214
215
unhighlight ( vis , maxIndex )
@@ -222,8 +223,18 @@ export default {
222
223
vis . array . selectColor ( prev_j , partLColor )
223
224
for ( let k = cur_i ; k <= right ; k ++ )
224
225
vis . array . deselect ( k ) ;
225
- } else if ( checkingLeft ) {
226
+ } else if ( whereAreWe === MSD_BOOKMARKS . partition_left ) {
226
227
// note i can fall off RHS of array...
228
+ if ( cur_i !== undefined && cur_i <= right ) {
229
+ updateBinary ( vis , arr [ cur_i ] ) ;
230
+ if ( ( arr [ cur_i ] >> mask & 1 ) === 1 )
231
+ vis . array . selectColor ( cur_i , partRColor )
232
+ else {
233
+ vis . array . selectColor ( cur_i , partLColor ) ;
234
+ cur_i ++ ; // show incremented i XXX fix i_too_high
235
+ }
236
+ }
237
+ /*
227
238
if (prev_i !== undefined && prev_i !== cur_j) {
228
239
let real_i = cur_i;
229
240
if (cur_i === undefined)
@@ -235,7 +246,8 @@ export default {
235
246
}
236
247
if (arr && cur_i !== undefined)
237
248
updateBinary(vis, arr[cur_i])
238
- } else {
249
+ */
250
+ } else if ( whereAreWe === MSD_BOOKMARKS . partition_right ) {
239
251
// note j can fall off LHS of array...
240
252
if ( prev_j !== undefined && prev_j !== cur_i )
241
253
for ( let k = prev_j ; k >= cur_j && k >= cur_i ; k -- )
@@ -246,6 +258,11 @@ export default {
246
258
updateBinary ( vis , arr [ cur_j ] )
247
259
}
248
260
}
261
+
262
+ vis . array . setStackDepth ( cur_real_stack . length ) ;
263
+ vis . array . setStack (
264
+ deriveStack ( cur_real_stack , cur_finished_stack_frames , cur_i , cur_j , cur_depth )
265
+ ) ;
249
266
if ( left < A . length ) // shouldn't happen with initial mask choice
250
267
assignVariable ( vis , VIS_VARIABLE_STRINGS . left , left ) ;
251
268
if ( right >= 0 ) {
@@ -382,16 +399,18 @@ arr],
382
399
prev_i = i ; // save prev value for unhighlighting
383
400
prev_j = j ;
384
401
// Build the left group until it reaches the mask (find the big element)
385
- leftCheck = true
402
+ // leftCheck = true
403
+ whereAreWe = MSD_BOOKMARKS . partition_left ;
386
404
while ( i <= j && ( arr [ i ] >> mask & 1 ) === 0 ) {
387
- // partitionChunkerWrapper(MSD_BOOKMARKS.partition_left)
405
+ partitionChunkerWrapper ( MSD_BOOKMARKS . partition_left )
388
406
i ++
389
407
}
390
408
partitionChunkerWrapper ( MSD_BOOKMARKS . partition_left )
391
409
// Build the right group until it fails the mask (find the small element)
392
- leftCheck = false
410
+ // leftCheck = false
411
+ whereAreWe = MSD_BOOKMARKS . partition_right ;
393
412
while ( j > i && ( arr [ j ] >> mask & 1 ) === 1 ) {
394
- // partitionChunkerWrapper(MSD_BOOKMARKS.partition_right)
413
+ partitionChunkerWrapper ( MSD_BOOKMARKS . partition_right )
395
414
j --
396
415
}
397
416
partitionChunkerWrapper ( MSD_BOOKMARKS . partition_right )
@@ -400,6 +419,9 @@ arr],
400
419
if ( i < j ) {
401
420
partitionChunkerWrapper ( MSD_BOOKMARKS . swap_condition )
402
421
swapAction ( MSD_BOOKMARKS . swap , i , j )
422
+ i ++ ;
423
+ j -- ;
424
+ partitionChunkerWrapper ( MSD_BOOKMARKS . inc_dec )
403
425
} else {
404
426
// about to return i from partition. We update the "mid" of
405
427
// the partition on the stack here so it is displayed at the
0 commit comments