Skip to content

Commit cacf5ed

Browse files
committed
missing file added, minor fixes
1 parent 7a1ba96 commit cacf5ed

File tree

3 files changed

+98
-8
lines changed

3 files changed

+98
-8
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
Welcome to Algorithms In Action!
66

77
Here is the [link](https://dev-aia.vercel.app/) to run the algorithm
8-
visualiser on your browser (hosted on Vercel; the github hosting is
8+
visualiser on your browser (hosted on Vercel on a hobby account so best
9+
install it elsewhere for anything more than casual use; the github hosting is
910
currently broken for some unknown reason).
1011

1112

src/algorithms/controllers/msort_arr_td.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ export function run_msort(is_qs_median_of_3) {
419419
// recursive call once it has returned plus we need a chunk at
420420
// this level when the recursive code is collapsed
421421
chunker.add('sortL', (vis, a, cur_left, cur_mid, cur_right) => {
422+
vis.array.set(a, 'msort_arr_td');
422423
assignVarToA(vis, 'left', cur_left);
423424
assignVarToA(vis, 'mid', cur_mid);
424425
assignVarToA(vis, 'right', cur_right);
@@ -433,19 +434,20 @@ export function run_msort(is_qs_median_of_3) {
433434

434435
// dummy chunk before recursive call, as above
435436
chunker.add('preSortR', (vis, a, cur_left, cur_mid, cur_right) => {
436-
vis.array.set(a, 'msort_arr_td');
437+
// vis.array.set(a, 'msort_arr_td');
438+
for (let i = cur_left; i <= cur_mid; i++) {
439+
unhighlight(vis, i, false);
440+
}
437441
assignVarToA(vis, 'left', undefined);
438442
assignVarToA(vis, 'mid', undefined);
439443
assignVarToA(vis, 'right', undefined);
440-
for (let i = cur_left; i <= cur_mid; i++) {
441-
// unhighlight(vis, i, true);
442-
// unhighlight(vis, i, false)
443-
}
444-
// for (let i = cur_mid+1; i <= cur_right; i++) {
444+
for (let i = cur_mid+1; i <= cur_right; i++) {
445445
// highlight(vis, i, true)
446-
// }
446+
}
447447
}, [A, left, mid, right], depth);
448+
448449
MergeSort(mid+1, right, depth + 1);
450+
449451
// chunk after recursive call
450452
chunker.add('sortR', (vis, a, cur_left, cur_mid, cur_right) => {
451453
assignVarToA(vis, 'left', cur_left);
@@ -636,6 +638,11 @@ cur_max2, cur_bp) => {
636638
assignVarToA(vis, 'ap2', undefined);
637639
assignVarToA(vis, 'max2', undefined);
638640
}
641+
// XXX best highlight cur_mid+1..right from previous
642+
// recursion level?
643+
// for (let i = cur_mid+1; i <= right; i++) {
644+
// highlight(vis, i, true)
645+
// }
639646
}, [A, B, left, mid, right], depth);
640647

641648
// chunk after recursive call, as above, after adjusting
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// XXX Based on Quicksort version
2+
3+
// eslint-disable-next-line import/no-cycle
4+
// See also accompanying mods/hooks in src/context/GlobalState.js and
5+
// src/context/actions.js
6+
7+
/*
8+
* @Author: Roden Wild
9+
* @Date: 2023-09-02
10+
* @FilePath: /src/algorithms/controllers/msort_arr_tdCollapseChunkPlugin.js
11+
* @Description: logic for msort_arr_td reachability
12+
*/
13+
import { GlobalActions } from '../../context/actions';
14+
import { triggerButtonClick } from '../parameters/ButtonClickTrigger.js';
15+
16+
// XXX
17+
const MS_NAME = 'msort_arr_td';
18+
19+
let algorithmGetter = () => null;
20+
let dispatchGetter = () => null;
21+
22+
function getGlobalAlgorithm() {
23+
return algorithmGetter();
24+
}
25+
26+
// eslint-disable-next-line
27+
function getGlobalDispatch() {
28+
return dispatchGetter();
29+
}
30+
31+
window.getGlobalAlgorithm = getGlobalAlgorithm;
32+
export function initGlobalAlgorithmGettermsort_arr_td(getter, dispatchGetterFn) {
33+
algorithmGetter = getter;
34+
dispatchGetter = dispatchGetterFn;
35+
}
36+
37+
function isInmsort_arr_td(algorithm) {
38+
// eslint-disable-next-line no-param-reassign
39+
if (!algorithm) algorithm = getGlobalAlgorithm();
40+
return algorithm.id.name === MS_NAME;
41+
}
42+
43+
export function isMergeCopyExpanded() {
44+
const algorithm = getGlobalAlgorithm();
45+
if (!isInmsort_arr_td()) return false;
46+
// eslint-disable-next-line
47+
const { bookmark, pseudocode, collapse } = algorithm;
48+
return collapse.msort_arr_td.sort.MergeCopy;
49+
}
50+
51+
export function isMergeExpanded() {
52+
const algorithm = getGlobalAlgorithm();
53+
if (!isInmsort_arr_td()) return false;
54+
// eslint-disable-next-line
55+
const { bookmark, pseudocode, collapse } = algorithm;
56+
return collapse.msort_arr_td.sort.Merge;
57+
}
58+
59+
// checks if either QS recursive call is expanded (needed to determine if i
60+
// should be displayed)
61+
export function isRecursionExpanded() {
62+
const algorithm = getGlobalAlgorithm();
63+
if (!isInmsort_arr_td()) return false;
64+
// , playing, chunker
65+
66+
// eslint-disable-next-line
67+
const { bookmark, pseudocode, collapse } = algorithm;
68+
return collapse.msort_arr_td.sort.QuicksortFirst ||
69+
collapse.msort_arr_td.sort.QuicksortSecond;
70+
}
71+
72+
// Trigger refresh of display when code is expanded/collapsed.
73+
// Not so efficient - runs through all the chunks from the start. XXX
74+
// However, it seems to work and is pretty general - could possibly use it for
75+
// other algorithms rather than have specific triggers for each algorithm.
76+
export function onCollapseStateChangemsort_arr_td(chunker) {
77+
if (!isInmsort_arr_td()) return false;
78+
const algorithm = getGlobalAlgorithm();
79+
chunker.refresh();
80+
//triggerButtonClick();
81+
//GlobalActions.RUN_ALGORITHM(algorithm.state, algorithm.id);
82+
}

0 commit comments

Comments
 (0)