Skip to content

Commit 5c6dc16

Browse files
committed
collapseChunkPlugin.js in msort+QS, QS bug fix
1 parent e0e8ea9 commit 5c6dc16

File tree

8 files changed

+81
-268
lines changed

8 files changed

+81
-268
lines changed

src/algorithms/controllers/collapseChunkPlugin.js

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const importsThis = ['quickSort', 'quickSortM3', 'msort_arr_td', 'transitiveClos
1313
// src/context/actions.js
1414
import { GlobalActions } from '../../context/actions';
1515

16+
// not sure if finding the running algorithm needs such complex code, but this
17+
// seems to work...
1618
let algorithmGetter = () => null;
1719

1820
function getGlobalAlgorithm() {
@@ -32,37 +34,26 @@ export function initGlobalAlgorithmGetter(getter) {
3234
// of blocks in this interface will help remind programmers to include the
3335
// block we are interested in *plus enclosing blocks* (Main is always
3436
// expanded so that is not needed).
35-
export function areExpanded(Blocks) {
37+
export function areExpanded(blocks) {
3638
const algorithm = getGlobalAlgorithm();
3739
const alg_name = algorithm.id.name;
3840
const { bookmark, pseudocode, collapse } = algorithm;
39-
return Blocks.reduce((acc, curr) =>
40-
(acc && collapse[alg_name.sort][curr]), true);
41+
return blocks.reduce((acc, curr) =>
42+
(acc && collapse[alg_name].sort[curr]), true);
4143
}
4244

4345
// Trigger refresh of display when code is expanded/collapsed.
4446
// Not so efficient - runs through all the chunks from the start. XXX
45-
// However, it seems to work fine and is pretty general. We could
46-
// possibly add a shortcut for algorithms that never change display
47-
// depending on what blocks are collapsed. XXX
47+
// However, it seems to work fine and is pretty general.
48+
// Added a shortcut for algorithms that never change display
49+
// depending on what blocks are collapsedi, which requires algorithms
50+
// that use this code to be explicitly listed at the start of this file
51+
// (could invert the selection and list tose that don't need it, to be
52+
// more robust).
4853
export function onCollapseChange(chunker) {
49-
// const algorithm = getGlobalAlgorithm();
54+
const algorithm = getGlobalAlgorithm();
55+
const alg_name = algorithm.id.name;
56+
if (!importsThis.includes(alg_name)) return;
5057
chunker.refresh();
5158
}
5259

53-
54-
/////////////////////////////////////////////////////
55-
56-
export function isMergeCopyExpanded() {
57-
return areExpanded(['MergeCopy']);
58-
}
59-
60-
export function isMergeExpanded() {
61-
return areExpanded(['MergeCopy', 'Merge']);
62-
}
63-
64-
// checks if either recursive call is expanded (needed to determine if i
65-
// should be displayed)
66-
export function isRecursionExpanded() {
67-
return areExpanded(['MergesortL']) || areExpanded(['MergesortR']);
68-
}

src/algorithms/controllers/msort_arr_td.js

Lines changed: 36 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
// Merge sort for arrays, top down
22
// Adapted code from Quicksort...
33
// XXX Could do with a good clean up!
4-
// Lots of crud, including abandonned attempt at QS-style stack display.
4+
// Lots of crud, mostly abandonned attempt at QS-style stack display.
55
// Uses simple stack display like DFSrec; stack vanishes inside
66
// merge+copy because screen real-estate is limited and details of merge
77
// are independent of stack details anyway (may cause some surprise
88
// though)
99

1010
import { msort_arr_td } from '../explanations';
1111

12-
const is_qs_median_of_3 = false;
13-
const run = run_msort(is_qs_median_of_3);
12+
const run = run_msort();
1413

1514
export default {
1615
explanation: msort_arr_td,
@@ -19,7 +18,7 @@ export default {
1918
};
2019

2120

22-
// Quicksort common code
21+
// XXX (was) Quicksort common code
2322
// Example of a recursive algorithm that could serve as a guide to
2423
// implementing others. Some things to note:
2524
// 1) A depth parameter is added to the recursive code and also passed
@@ -46,30 +45,28 @@ export default {
4645
import ArrayTracer from '../../components/DataStructures/Array/Array1DTracer';
4746

4847
import {
49-
// XXX may not need isMergeExpanded? only needed if last chunk of
50-
// merge still as extra vars displayed
51-
isMergeExpanded,
52-
isMergeCopyExpanded,
53-
isRecursionExpanded,
48+
areExpanded,
5449
} from './collapseChunkPlugin';
5550

56-
// MergeExpandedR true if Merge code is really/recursively expanded (ie,
57-
// visible in animation). Its possible for Merge to be expanded but the
58-
// outer MergeCopy not expanded.
59-
function isMergeExpandedR() {
60-
return isMergeCopyExpanded() && isMergeExpanded()
51+
/////////////////////////////////////////////////////
52+
53+
// arrayB exists and is displayed only if MergeCopy is expanded
54+
function isMergeCopyExpanded() {
55+
return areExpanded(['MergeCopy']);
6156
}
6257

63-
// visualisation variable strings
64-
// For now we use a special case for i&j running off the left of the
65-
// array since we can't easily render them in the right place
66-
const VIS_VARIABLE_STRINGS = {
67-
i_left_index: 'i',
68-
j_right_index: 'j',
69-
i_eq_0: 'i==0',
70-
j_eq_0: 'j==0',
71-
pivot: 'pivot',
72-
};
58+
// We don't strictly need isMergeExpanded: only needed if last chunk of
59+
// merge still had extra vars displayed. Some code still needs
60+
// isMergeCopyExpanded because it uses arrayB
61+
function isMergeExpanded() {
62+
return areExpanded(['MergeCopy', 'Merge']); // MergeCopy contains Merge
63+
}
64+
65+
// checks if either recursive call is expanded (otherwise stack is not
66+
// displayed)
67+
function isRecursionExpanded() {
68+
return areExpanded(['MergesortL']) || areExpanded(['MergesortR']);
69+
}
7370

7471
// see stackFrameColour in index.js to find corresponding function mapping to css
7572
const STACK_FRAME_COLOR = {
@@ -85,21 +82,6 @@ const STACK_FRAME_COLOR = {
8582
// for simple DFS-like stack display
8683
let simple_stack = [];
8784

88-
/*
89-
90-
(loc is line of code)
91-
bookmarks are loc identifiers into a REAL file
92-
REAL files are the pseudocode files
93-
(search \\B and find quicksort)
94-
keep up to date with this file
95-
96-
MEDIAN3_ if loc is only in median of 3 quicksort
97-
SHARED_ if shared loc between MEDIAN3 and RIGHT_PIVOT
98-
RIGHT_P_ if an loc is only in the right pivot quicksort
99-
100-
*/
101-
102-
10385

10486
// ----------------------------------------------------------------------------------------------------------------------------
10587

@@ -167,7 +149,7 @@ const unhighlightB = (vis, index, isPrimaryColor = true) => {
167149
// ----------------------------------------------------------------------------------------------------------------------------
168150

169151

170-
// Nice to hide array B entirely if things are collapsed
152+
// We hide array B entirely if things mergeCopy is collapsed
171153
export function initVisualisers() {
172154
if (isMergeCopyExpanded()) {
173155
return {
@@ -202,7 +184,7 @@ export function initVisualisers() {
202184
* @param {array} nodes array of numbers needs to be sorted
203185
*/
204186

205-
export function run_msort(is_qs_median_of_3) {
187+
export function run_msort() {
206188

207189
return function run(chunker, { nodes }) {
208190
// can't rename from nodes
@@ -329,11 +311,6 @@ export function run_msort(is_qs_median_of_3) {
329311
derive_stack(cur_real_stack, cur_finished_stack_frames, cur_i, cur_j, cur_pivot_index, cur_depth)
330312
);
331313

332-
// assignVarToA(vis, VIS_VARIABLE_STRINGS.i_left_index, cur_i);
333-
// assignVarToA(vis, VIS_VARIABLE_STRINGS.i_eq_0, cur_i_too_low);
334-
// assignVarToA(vis, VIS_VARIABLE_STRINGS.pivot, cur_pivot_index);
335-
// assignVarToA(vis, VIS_VARIABLE_STRINGS.j_right_index, cur_j);
336-
// assignVarToA(vis, VIS_VARIABLE_STRINGS.j_eq_0, cur_j_too_low);
337314
};
338315

339316

@@ -356,7 +333,7 @@ export function run_msort(is_qs_median_of_3) {
356333
// ----------------------------------------------------------------------------------------------------------------------------
357334

358335
function renderInMerge(vis, a, b, cur_left, cur_ap1, cur_ap2, cur_bp, cur_max1, cur_max2, c_stk) {
359-
if (isMergeExpandedR()) {
336+
if (isMergeExpanded()) {
360337
vis.array.set(a, 'msort_arr_td');
361338
// set_simple_stack(vis.array, c_stk);
362339
assignVarToA(vis, 'ap1', cur_ap1);
@@ -519,32 +496,32 @@ c_stk) => {
519496
for (let i = cur_mid+1; i <= cur_right; i++) {
520497
unhighlight(vis, i, false)
521498
}
522-
if (isMergeExpandedR()) {
499+
if (isMergeExpanded()) {
523500
assignVarToA(vis, 'left', undefined);
524501
assignVarToA(vis, 'ap1', cur_left);
525502
highlight(vis, cur_left, true);
526503
}
527504
}, [A, left, mid, right], depth);
528505
chunker.add('max1', (vis, a, cur_left, cur_mid, cur_right) => {
529-
if (isMergeExpandedR()) {
506+
if (isMergeExpanded()) {
530507
assignVarToA(vis, 'mid', undefined);
531508
assignVarToA(vis, 'max1', cur_mid);
532509
}
533510
}, [A, left, mid, right], depth);
534511
chunker.add('ap2', (vis, a, cur_left, cur_mid, cur_right) => {
535-
if (isMergeExpandedR()) {
512+
if (isMergeExpanded()) {
536513
assignVarToA(vis, 'ap2', cur_mid+1);
537514
highlight(vis, cur_mid+1, true);
538515
}
539516
}, [A, left, mid, right], depth);
540517
chunker.add('max2', (vis, a, cur_left, cur_mid, cur_right) => {
541-
if (isMergeExpandedR()) {
518+
if (isMergeExpanded()) {
542519
assignVarToA(vis, 'right', undefined);
543520
assignVarToA(vis, 'max2', right);
544521
}
545522
}, [A, left, mid, right], depth);
546523
chunker.add('bp', (vis, a, cur_left, cur_mid, cur_right) => {
547-
if (isMergeExpandedR()) {
524+
if (isMergeExpanded()) {
548525
assignVarToB(vis, 'bp', left);
549526
}
550527
}, [A, left, mid, right], depth);
@@ -573,7 +550,7 @@ cur_max1, cur_max2, cur_stk, cur_left);
573550
cur_bp, cur_max1, cur_max2, cur_stk, cur_left) => {
574551
renderInMerge(vis, a, b, cur_left, cur_ap1, cur_ap2, cur_bp,
575552
cur_max1, cur_max2, cur_stk, cur_left);
576-
if (isMergeExpandedR()) {
553+
if (isMergeExpanded()) {
577554
highlightB(vis, cur_bp, false);
578555
}
579556
}, [A, B, ap1, ap2, bp, max1, max2, simple_stack, left], depth);
@@ -582,7 +559,7 @@ cur_max1, cur_max2, cur_stk, cur_left);
582559
cur_max1, cur_max2, cur_stk, cur_left) => {
583560
renderInMerge(vis, a, b, cur_left, cur_ap1, cur_ap2, cur_bp,
584561
cur_max1, cur_max2, cur_stk, cur_left);
585-
if (isMergeExpandedR()) {
562+
if (isMergeExpanded()) {
586563
highlightB(vis, cur_bp, false);
587564
}
588565
}, [A, B, ap1, ap2, bp, max1, max2, simple_stack, left], depth);
@@ -599,7 +576,7 @@ cur_max1, cur_max2, cur_stk, cur_left);
599576
cur_bp, cur_max1, cur_max2, cur_stk, cur_left) => {
600577
renderInMerge(vis, a, b, cur_left, cur_ap1, cur_ap2, cur_bp,
601578
cur_max1, cur_max2, cur_stk, cur_left);
602-
if (isMergeExpandedR()) {
579+
if (isMergeExpanded()) {
603580
highlightB(vis, cur_bp, false);
604581
}
605582
}, [A, B, ap1, ap2, bp, max1, max2, simple_stack, left], depth);
@@ -608,7 +585,7 @@ cur_max1, cur_max2, cur_stk, cur_left);
608585
cur_max1, cur_max2, cur_stk, cur_left) => {
609586
renderInMerge(vis, a, b, cur_left, cur_ap1, cur_ap2, cur_bp,
610587
cur_max1, cur_max2, cur_stk, cur_left);
611-
if (isMergeExpandedR()) {
588+
if (isMergeExpanded()) {
612589
highlightB(vis, cur_bp, false);
613590
}
614591
}, [A, B, ap1, ap2, bp, max1, max2, simple_stack, left], depth);
@@ -629,7 +606,7 @@ cur_max1, cur_max2, cur_stk, cur_left);
629606

630607
chunker.add('CopyRest1', (vis, a, b, cur_left, cur_ap1,
631608
cur_ap2, cur_max1, cur_max2, cur_bp, c_stk) => {
632-
if (isMergeExpandedR()) {
609+
if (isMergeExpanded()) {
633610
vis.array.set(a, 'msort_arr_td');
634611
// set_simple_stack(vis.array, c_stk);
635612
// unhighlight(vis, cur_ap1, true);
@@ -666,7 +643,7 @@ cur_max2, cur_b, c_stk) => {
666643
highlightB(vis, i, false);
667644
}
668645
}
669-
if (isMergeExpandedR()) {
646+
if (isMergeExpanded()) {
670647
if (cur_ap2 < a.length) {
671648
unhighlight(vis, cur_ap2, true);
672649
assignVarToA(vis, 'ap2', undefined);
@@ -693,7 +670,7 @@ cur_right, c_stk) => {
693670
for (let i = cur_left; i <= cur_right; i++) {
694671
highlight(vis, i, false);
695672
}
696-
if (isMergeExpandedR()) {
673+
if (isMergeExpanded()) {
697674
assignVarToA(vis, 'ap1', undefined);
698675
assignVarToA(vis, 'max1', undefined);
699676
assignVarToA(vis, 'ap2', undefined);

src/algorithms/controllers/msort_arr_tdCollapseChunkPlugin.js

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)