Skip to content

Commit d87309a

Browse files
committed
refactor: all common functions for bup and nat in msort_shared.js gave meaningful names to colours
1 parent 268fa1c commit d87309a

File tree

3 files changed

+240
-392
lines changed

3 files changed

+240
-392
lines changed

src/algorithms/controllers/msort_arr_bup.js

Lines changed: 35 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,22 @@
33

44
import { msort_arr_bup } from '../explanations';
55
import ArrayTracer from '../../components/DataStructures/Array/Array1DTracer';
6+
7+
68
import {
7-
areExpanded,
8-
} from './collapseChunkPlugin';
9+
isMergeCopyExpanded,
10+
isMergeExpanded,
11+
highlight,
12+
highlightB,
13+
unhighlight,
14+
highlight2Runlength,
15+
assignVarToA,
16+
assignVarToB,
17+
displayMergeLabels,
18+
highlightAPointers,
19+
set_simple_stack,
20+
resetArrayA
21+
} from './msort_shared.js';
922

1023

1124
const run = run_msort();
@@ -50,51 +63,10 @@ export function initVisualisers() {
5063
}
5164
}
5265

53-
// arrayB exists and is displayed only if MergeCopy is expanded
54-
function isMergeCopyExpanded() {
55-
return areExpanded(['MergeCopy']);
56-
}
57-
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-
6566
// -------------------------------------------------------------------------------
6667
// Define helper functions
6768
// -------------------------------------------------------------------------------
6869

69-
// Highlights Array A either red or green
70-
// Can add more colours in future
71-
function highlight(vis, index, color) {
72-
if (color == 'red') {
73-
vis.array.select(index);
74-
}
75-
if (color == 'green') {
76-
vis.array.patch(index);
77-
}
78-
}
79-
// Same as highlight() but checks isMergeExpanded()/arrayB is displayed, otherwise does nothing
80-
function highlightB(vis, index, color) {
81-
if (isMergeExpanded()) {
82-
if (color == 'red') {
83-
vis.arrayB.select(index);
84-
}
85-
if (color == 'green') {
86-
vis.arrayB.patch(index);
87-
}
88-
}
89-
}
90-
91-
// Highlights two runlengths two different colours
92-
function highlight2Runlength(vis, left, mid, right, colorA, colorB) {
93-
// highlight first runlength color A
94-
for (let i = left; i <= mid; i++) highlight(vis, i, colorA);
95-
// highlight second runlength color B
96-
for (let j = mid + 1; j <= right; j++) highlight(vis, j, colorB);
97-
}
9870

9971
// Highlight entire array alternating colors for runlength
10072
function highlightAllRunlengths(vis, runlength, colorA, colorB, size) {
@@ -103,125 +75,20 @@ function highlightAllRunlengths(vis, runlength, colorA, colorB, size) {
10375
for (let i = 0; i < size; i++) {
10476
if (toggle == 0) {
10577
highlight(vis, i, colorA);
106-
console.log("toggle == 0");
10778
}
10879
if (toggle == 1) {
10980
highlight(vis, i, colorB);
110-
console.log("toggle == 1");
11181
}
112-
console.log("(i + 1) % runlength = " + (runlength % (i + 1)));
113-
console.log("(runlength = " + (runlength));
114-
// Switch color after completing a run of length 'runlength'
115-
if ((i + 1) % runlength == 0) {
116-
117-
console.log("(i + 1) % runlength == 0");
118-
119-
toggle = 1 - toggle; // Flip toggle between 0 and 1
120-
121-
} console.log("toggle = " + toggle);
122-
}
123-
}
124-
125-
// Unhighlight entire array alternating colors for runlength
126-
function unhighlightAllRunlengths(vis, runlength, colorA, colorB, size) {
127-
let toggle = 0; // 0 = colorA, 1 = colorB
12882

129-
for (let i = 0; i < size; i++) {
130-
if (toggle == 0) {
131-
unhighlight(vis, i, colorA);
132-
console.log("toggle == 0");
133-
}
134-
if (toggle == 1) {
135-
unhighlight(vis, i, colorB);
136-
console.log("toggle == 1");
137-
}
138-
console.log("(i + 1) % runlength = " + (runlength % (i + 1)));
139-
console.log("(runlength = " + (runlength));
14083
// Switch color after completing a run of length 'runlength'
14184
if ((i + 1) % runlength == 0) {
142-
143-
console.log("(i + 1) % runlength == 0");
144-
14585
toggle = 1 - toggle; // Flip toggle between 0 and 1
146-
147-
} console.log("toggle = " + toggle);
148-
}
149-
}
150-
151-
// unhighlights arrayA
152-
function unhighlight(vis, index, color) {
153-
if (color == 'red') {
154-
vis.array.deselect(index);
155-
}
156-
if (color == 'green') {
157-
vis.array.depatch(index);
158-
}
159-
}
160-
161-
function highlightAPointers(vis, ap1, max1, ap2, max2, color) {
162-
if (ap1 <= max1) {
163-
highlight(vis, ap1, color);
164-
}
165-
if (ap2 <= max2) {
166-
highlight(vis, ap2, color);
167-
}
168-
}
169-
170-
// this function sets array a, highlighting from left to mid red, and set the stack
171-
function resetArrayA(vis, A, left, mid, right, runlength) {
172-
vis.array.set(A, 'msort_arr_bup');
173-
highlight2Runlength(vis, left, mid, right, colorA, colorA);
174-
set_simple_stack(vis.array, [runlength]);
175-
}
176-
177-
// Assigns label to array A at index, checks if index is greater than size of array
178-
// if index is greater than size, assign label to last element in array
179-
function assignVarToA(vis, variable_name, index, size) {
180-
if (index === undefined)
181-
vis.array.removeVariable(variable_name);
182-
else if (index >= size)
183-
vis.array.assignVariable(variable_name, size - 1)
184-
else
185-
vis.array.assignVariable(variable_name, index);
186-
}
187-
188-
// Same as above function bet also checks if array B is displayed
189-
function assignVarToB(vis, variable_name, index, size) {
190-
if (isMergeExpanded()) {
191-
if (index === undefined)
192-
vis.arrayB.removeVariable(variable_name);
193-
else if (index >= size)
194-
vis.arrayB.assignVariable(variable_name, size - 1);
195-
else
196-
vis.arrayB.assignVariable(variable_name, index);
86+
}
19787
}
19888
}
19989

200-
// Display the runlength of the array at the runlength'th element
201-
function displayRunlength(vis, runlength, size) {
202-
let text = 'runlength = ' + runlength;
203-
let index = runlength - 1;
204-
205-
assignVarToA(vis, text, index, size);
206-
}
207-
208-
// Display all the labels needed for Merge()
209-
function displayMergeLabels(vis, ap1, max1, ap2, max2, bp, size) {
210-
assignVarToA(vis, 'ap1', ap1, size);
211-
assignVarToA(vis, 'max1', max1, size);
212-
assignVarToA(vis, 'ap2', ap2, size);
213-
assignVarToA(vis, 'max2', max2, size);
214-
if (isMergeExpanded()) assignVarToB(vis, 'bp', bp, size);
215-
}
216-
21790

21891

219-
function set_simple_stack(vis_array, c_stk) {
220-
console.log("set_simple_stack" + c_stk);
221-
console.log(c_stk);
222-
vis_array.setList(c_stk);
223-
}
224-
22592
/**
22693
*
22794
* @param {object} chunker
@@ -259,7 +126,8 @@ export function run_msort() {
259126
}, [A, B, size], length);
260127

261128
chunker.add('runlength', (vis, c_rlength) => {
262-
displayRunlength(vis, c_rlength, size);
129+
130+
assignVarToA(vis, "runlength", c_rlength, size);
263131

264132
set_simple_stack(vis.array, [c_rlength]);
265133
highlightAllRunlengths(vis, c_rlength, colorA, colorB, size);
@@ -271,23 +139,23 @@ export function run_msort() {
271139
chunker.add('MainWhile', (vis, c_rlength, c_left) => {
272140

273141
// display size label
274-
assignVarToA(vis, ("size = " + size), size, size);
142+
assignVarToA(vis, "size", size, size);
275143

144+
}, [runlength, left]);
276145

146+
chunker.add('left', (vis, a, c_left, c_rlength) => {
147+
vis.array.set(a, 'msort_arr_bup');
277148

278-
}, [runlength, left]);
279149

280-
chunker.add('left', (vis, c_left, c_rlength) => {
281150
assignVarToA(vis, 'left', c_left, size);
282-
283-
unhighlightAllRunlengths(vis, c_rlength, colorA, colorB, size);
151+
set_simple_stack(vis.array, [c_rlength]);
284152

285153
let left_2 = c_left;
286154
let mid_2 = (c_rlength + c_left - 1);
287155
let right_2 = (Math.min(c_rlength * 2, size) - 1);
288156

289157
highlight2Runlength(vis, left_2, mid_2, right_2, colorA, colorB);
290-
}, [left, runlength]);
158+
}, [A, left, runlength]);
291159

292160
while ((left + runlength) <= size) {
293161

@@ -305,8 +173,8 @@ export function run_msort() {
305173

306174
chunker.add('mid', (vis, c_mid, c_rlength) => {
307175
// remove runlength and size labels
308-
assignVarToA(vis, ('runlength = ' + c_rlength), undefined, size);
309-
assignVarToA(vis, ('size = ' + (size)), undefined, size);
176+
assignVarToA(vis, 'runlength', undefined, size);
177+
assignVarToA(vis, 'size', undefined, size);
310178

311179
assignVarToA(vis, 'mid', c_mid, size);
312180
}, [mid, runlength]);
@@ -321,7 +189,7 @@ export function run_msort() {
321189

322190
// now in the nitty gritty of Merge
323191
// highlight the two parts you want to merge red
324-
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength);
192+
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength, colorA, colorA);
325193

326194
assignVarToA(vis, 'left', undefined, size); // ap1 replaces left
327195
assignVarToA(vis, 'ap1', c_ap1, size);
@@ -348,7 +216,6 @@ export function run_msort() {
348216
if (isMergeExpanded()) {
349217
assignVarToA(vis, 'right', undefined, size); // max2 replaces right
350218
assignVarToA(vis, 'max2', c_max2, size);
351-
352219
}
353220
}, [max2]);
354221

@@ -365,7 +232,7 @@ export function run_msort() {
365232

366233
chunker.add('MergeWhile', (vis, a, c_left, c_right, c_mid, c_ap1, c_max1, c_ap2, c_max2, c_bp, c_rlength) => {
367234

368-
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength);
235+
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength, colorA, colorA);
369236

370237
displayMergeLabels(vis, c_ap1, c_max1, c_ap2, c_max2, c_bp, size);
371238

@@ -387,8 +254,7 @@ export function run_msort() {
387254

388255
if (isMergeExpanded()) vis.arrayB.set(b, 'msort_arr_bup');
389256

390-
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength);
391-
257+
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength, colorA, colorA);
392258

393259
displayMergeLabels(vis, c_ap1, c_max1, c_ap2, c_max2, c_bp, size);
394260
// future color: should be colorA & colorB
@@ -402,7 +268,7 @@ export function run_msort() {
402268
ap1 = ap1 + 1;
403269
chunker.add('ap1++', (vis, a, c_left, c_mid, c_right, c_rlength,
404270
c_ap1, c_max1, c_ap2, c_max2, c_bp) => {
405-
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength);
271+
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength, colorA, colorA);
406272
displayMergeLabels(vis, c_ap1, c_max1, c_ap2, c_max2, c_bp, size);
407273

408274
assignVarToA(vis, 'ap1', c_ap1, size);
@@ -427,7 +293,7 @@ export function run_msort() {
427293

428294
chunker.add('copyap2', (vis, a, b, c_ap1, c_ap2, c_bp, c_max1, c_max2, c_left, c_right, c_mid, c_rlength) => {
429295
if (isMergeExpanded()) vis.arrayB.set(b, 'msort_arr_bup');
430-
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength);
296+
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength, colorA, colorA);
431297

432298
displayMergeLabels(vis, c_ap1, c_max1, c_ap2, c_max2, c_bp, size);
433299

@@ -443,7 +309,7 @@ export function run_msort() {
443309
ap2 = ap2 + 1;
444310
chunker.add('ap2++', (vis, a, c_left, c_mid, c_right, c_rlength,
445311
c_ap1, c_max1, c_ap2, c_max2, c_bp) => {
446-
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength);
312+
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength, colorA, colorA);
447313
displayMergeLabels(vis, c_ap1, c_max1, c_ap2, c_max2, c_bp, size);
448314

449315
assignVarToA(vis, "ap2", c_ap2, size);
@@ -466,7 +332,7 @@ export function run_msort() {
466332

467333
chunker.add('CopyRest1', (vis, a, b, c_ap1, c_max1, c_left, c_right, c_mid, c_bp, c_rlength) => {
468334

469-
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength);
335+
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength, colorA, colorA);
470336

471337
if (isMergeExpanded()) vis.arrayB.set(b, 'msort_arr_bup');
472338

@@ -488,7 +354,7 @@ export function run_msort() {
488354
}
489355

490356
chunker.add('CopyRest2', (vis, a, b, c_ap2, c_max2, c_left, c_right, c_mid, c_rlength) => {
491-
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength);
357+
resetArrayA(vis, a, c_left, c_mid, c_right, c_rlength, colorA, colorA);
492358

493359
if (isMergeExpanded()) vis.arrayB.set(b, 'msort_arr_bup');
494360
assignVarToA(vis, 'ap2', c_ap2, size);
@@ -549,7 +415,7 @@ export function run_msort() {
549415
set_simple_stack(vis.array, [c_rlength]);
550416

551417
if (c_rlength < size) {
552-
displayRunlength(vis, c_rlength, size);
418+
assignVarToA(vis, "runlength", c_rlength, size);
553419
}
554420

555421
}, [runlength]);

0 commit comments

Comments
 (0)