Skip to content

Commit e0e8ea9

Browse files
committed
collapseChunkPlugin (generic code) works for msort
1 parent fea3789 commit e0e8ea9

File tree

5 files changed

+77
-5
lines changed

5 files changed

+77
-5
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Stuff to access what chunks are collapsed, so animation can depend on
2+
// that (eg show less/more information). There was an accumulation
3+
// of different versions of this for different algorithms. This is a
4+
// more generic one that requires some extra some code in algorithm files.
5+
// Possibly should be put elsewhere, eg, src/context/
6+
// NOTE: any algorithm controller file that imports this must be listed
7+
// below, otherwise hooks into what happens when blocks are expanded or
8+
// contracted are not enabled.
9+
const importsThis = ['quickSort', 'quickSortM3', 'msort_arr_td', 'transitiveClosure'];
10+
11+
// eslint-disable-next-line import/no-cycle
12+
// See also accompanying mods/hooks in src/context/GlobalState.js and
13+
// src/context/actions.js
14+
import { GlobalActions } from '../../context/actions';
15+
16+
let algorithmGetter = () => null;
17+
18+
function getGlobalAlgorithm() {
19+
return algorithmGetter();
20+
}
21+
22+
window.getGlobalAlgorithm = getGlobalAlgorithm;
23+
export function initGlobalAlgorithmGetter(getter) {
24+
algorithmGetter = getter;
25+
}
26+
27+
// Checks if list of pseudocode blocks are all currently expanded. Note
28+
// that "inner" blocks can be expanded even when "outer" ones are not, eg,
29+
// for msort_arr_td has block Merge inside MergeCopy. To check if Merge
30+
// code is visible (so certain variables should be displayed), we should
31+
// check both Merge and MergeCopy are expanded. Hopefully, using a list
32+
// of blocks in this interface will help remind programmers to include the
33+
// block we are interested in *plus enclosing blocks* (Main is always
34+
// expanded so that is not needed).
35+
export function areExpanded(Blocks) {
36+
const algorithm = getGlobalAlgorithm();
37+
const alg_name = algorithm.id.name;
38+
const { bookmark, pseudocode, collapse } = algorithm;
39+
return Blocks.reduce((acc, curr) =>
40+
(acc && collapse[alg_name.sort][curr]), true);
41+
}
42+
43+
// Trigger refresh of display when code is expanded/collapsed.
44+
// 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
48+
export function onCollapseChange(chunker) {
49+
// const algorithm = getGlobalAlgorithm();
50+
chunker.refresh();
51+
}
52+
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ export default {
4646
import ArrayTracer from '../../components/DataStructures/Array/Array1DTracer';
4747

4848
import {
49+
// XXX may not need isMergeExpanded? only needed if last chunk of
50+
// merge still as extra vars displayed
4951
isMergeExpanded,
5052
isMergeCopyExpanded,
5153
isRecursionExpanded,
52-
} from './msort_arr_tdCollapseChunkPlugin';
54+
} from './collapseChunkPlugin';
5355

5456
// MergeExpandedR true if Merge code is really/recursively expanded (ie,
5557
// visible in animation). Its possible for Merge to be expanded but the

src/algorithms/controllers/quickSortCollapseChunkPlugin.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,4 @@ export function onCollapseStateChangeQS(chunker) {
7272
if (!isInQuickSort()) return false;
7373
const algorithm = getGlobalAlgorithm();
7474
chunker.refresh();
75-
// XX re-runs algorithm from start - Not what we want
76-
//triggerButtonClick();
77-
//GlobalActions.RUN_ALGORITHM(algorithm.state, algorithm.id);
7875
}

src/context/GlobalState.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import React, { createContext, useState } from 'react';
2+
// generic version of collapseChunkPlugin - could probably adapt code and
3+
// delete some others XXX
4+
import { initGlobalAlgorithmGetter } from '../algorithms/controllers/collapseChunkPlugin';
25
import { initGlobalAlgotithmGetter } from '../algorithms/controllers/transitiveClosureCollapseChunkPlugin';
36
import { initGlobalAlgorithmGetterQS } from '../algorithms/controllers/quickSortCollapseChunkPlugin';
47
import { initGlobalAlgorithmGettermsort_arr_td } from '../algorithms/controllers/msort_arr_tdCollapseChunkPlugin';
@@ -35,7 +38,6 @@ export const GlobalProvider = ({ children }) => {
3538

3639
initGlobalAlgotithmGetter(
3740
() => globalState.algorithm,
38-
() => dispatch,
3941
);
4042
initGlobalAlgorithmGetterQS(
4143
() => globalState.algorithm,

src/context/actions.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import algorithms from '../algorithms';
55
import Chunker from './chunker';
66
import findBookmark from '../pseudocode/findBookmark';
7+
// generic version of collapseChunkPlugin - could probably adapt code and
8+
// delete some others XXX
9+
import { onCollapseChange } from '../algorithms/controllers/collapseChunkPlugin';
710
import { onCollapseStateChange } from '../algorithms/controllers/transitiveClosureCollapseChunkPlugin';
811
import { onCollapseStateChangeQS } from '../algorithms/controllers/quickSortCollapseChunkPlugin';
912
import { onCollapseStateChangemsort_arr_td } from '../algorithms/controllers/msort_arr_tdCollapseChunkPlugin';

0 commit comments

Comments
 (0)