Skip to content

Commit c995c35

Browse files
committed
sort colors, REX especially
1 parent a13b0c3 commit c995c35

File tree

7 files changed

+77
-48
lines changed

7 files changed

+77
-48
lines changed

src/algorithms/controllers/MSDRadixSort.js

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
// Some color code could be cleaned up
2+
// Could share some code with straight radix sort and quicksort
13
import ArrayTracer from '../../components/DataStructures/Array/Array1DTracer';
24
import MaskTracer from '../../components/DataStructures/Mask/MaskTracer'
35
import {
46
areExpanded,
57
} from './collapseChunkPlugin';
68
import { createPopper } from '@popperjs/core';
9+
import {colors} from '../../components/DataStructures/colors';
10+
11+
const partLColor = colors.peach;
12+
const partRColor = colors.sky;
13+
const sortedColor = colors.stone;
14+
const highlightColor = colors.apple; // for i,j in partition,...
715

816
// see stackFrameColour in Array1DRenderer/index.js to find corresponding function mapping to css
917
const STACK_FRAME_COLOR = {
@@ -85,20 +93,12 @@ const isRecursionExpanded = () => {
8593
return areExpanded(['MSDRadixSortLeft']) || areExpanded(['MSDRadixSortRight']);
8694
}
8795

88-
const highlight = (vis, index, isPrimaryColor = true) => {
89-
if (isPrimaryColor) {
90-
vis.array.select(index);
91-
} else {
92-
vis.array.patch(index);
93-
}
94-
};
96+
const highlight = (vis, index) => {
97+
vis.array.selectColor(index, highlightColor);
98+
};
9599

96-
const unhighlight = (vis, index, isPrimaryColor = true) => {
97-
if (isPrimaryColor) {
98-
vis.array.deselect(index);
99-
} else {
100-
vis.array.depatch(index);
101-
}
100+
const unhighlight = (vis, index) => {
101+
vis.array.deselect(index);
102102
};
103103

104104
const updateMask = (vis, value) => {
@@ -194,6 +194,8 @@ export default {
194194
assert(vis.array);
195195
assert(cur_real_stack && cur_finished_stack_frames);
196196

197+
// conditions for i,j display moved elsewhere
198+
/*
197199
if (!isPartitionExpanded()) {
198200
// j should not show up in vis if partition is collapsed
199201
cur_j = undefined;
@@ -205,6 +207,7 @@ export default {
205207
cur_i = undefined;
206208
cur_i_too_high = undefined;
207209
}
210+
*/
208211

209212
vis.array.setStackDepth(cur_real_stack.length);
210213
vis.array.setStack(
@@ -226,21 +229,29 @@ export default {
226229
// init, before partition
227230
highlight(vis, cur_i)
228231
highlight(vis, cur_j)
229-
} else if (isRecursionExpanded() && cur_i !== undefined && tmp_j === undefined && prev_i !== undefined) {
232+
} else if (cur_i !== undefined && tmp_j === undefined && prev_i !== undefined) {
230233
// just before first recursive call
231-
unhighlight(vis, prev_i)
232-
if (prev_j >= 0) // might have fallen off array
233-
unhighlight(vis, prev_j)
234+
if (i <= right)
235+
vis.array.selectColor(prev_i, partRColor)
236+
if (prev_j >= left) // might have fallen off array
237+
vis.array.selectColor(prev_j, partLColor)
238+
for (let k = cur_i; k <= right; k++)
239+
vis.array.deselect(k);
234240
} else if (checkingLeft) {
235-
if (isRecursionExpanded() && prev_i !== undefined && prev_i !== cur_j)
236-
unhighlight(vis, prev_i)
237-
highlight(vis, cur_i)
241+
if (prev_i !== undefined && prev_i !== cur_j)
242+
for (let k = prev_i; k < cur_i && k <= right; k++)
243+
vis.array.selectColor(k, partLColor)
244+
if (cur_i < cur_j)
245+
highlight(vis, cur_i)
246+
else if (cur_i <= right)
247+
vis.array.selectColor(cur_i, partRColor)
238248
if (arr && cur_i !== undefined)
239249
updateBinary(vis, arr[cur_i])
240250
} else {
241-
if (isRecursionExpanded() && prev_j !== undefined && prev_j !== cur_i)
242-
unhighlight(vis, prev_j);
243-
if (cur_j !== undefined) { // might have fallen off array
251+
if (prev_j !== undefined && prev_j !== cur_i)
252+
for (let k = prev_j; k >= cur_j && k >= cur_i; k--)
253+
vis.array.selectColor(k, partRColor)
254+
if (cur_j !== undefined && cur_j > cur_i) { // might have fallen off array
244255
highlight(vis, cur_j);
245256
// XXX probably best avoid updateBinary at swap
246257
updateBinary(vis, arr[cur_j])
@@ -250,10 +261,14 @@ export default {
250261
assignVariable(vis, VIS_VARIABLE_STRINGS.left, left);
251262
if (right >= 0)
252263
assignVariable(vis, VIS_VARIABLE_STRINGS.right, right);
253-
assignVariable(vis, VIS_VARIABLE_STRINGS.i_left_index, cur_i);
254-
assignVariable(vis, VIS_VARIABLE_STRINGS.i_gt_n, cur_i_too_high);
255-
assignVariable(vis, VIS_VARIABLE_STRINGS.j_right_index, cur_j);
256-
assignVariable(vis, VIS_VARIABLE_STRINGS.j_eq_0, cur_j_too_low);
264+
if (isPartitionExpanded() || isRecursionExpanded()) {
265+
assignVariable(vis, VIS_VARIABLE_STRINGS.i_left_index, cur_i);
266+
assignVariable(vis, VIS_VARIABLE_STRINGS.i_gt_n, cur_i_too_high);
267+
}
268+
if (isPartitionExpanded()) {
269+
assignVariable(vis, VIS_VARIABLE_STRINGS.j_right_index, cur_j);
270+
assignVariable(vis, VIS_VARIABLE_STRINGS.j_eq_0, cur_j_too_low);
271+
}
257272
};
258273

259274

@@ -345,6 +360,8 @@ cur_i, cur_j, cur_depth, A) => {
345360

346361
vis.array.swapElements(_n1, _n2);
347362
refreshStack(vis, cur_real_stack, cur_finished_stack_frames, cur_i, cur_j, prev_i, prev_j, left, right, cur_depth, false, undefined, A, mask)
363+
vis.array.selectColor(_n1, partLColor);
364+
vis.array.selectColor(_n2, partRColor);
348365
// redo poppers: swapping the elements keeps the
349366
// contents of the poppers correct but the position needs
350367
// to change. The documentation suggests update()
@@ -410,7 +427,18 @@ arr],
410427
// Base case: If the array has 1 or fewer elements or mask is less than 0, stop
411428
partitionChunker(MSD_BOOKMARKS.rec_function, undefined, undefined, undefined, undefined, left, right, depth, arr, mask)
412429
maxIndex = undefined; // defined only for top level call
413-
chunker.add(MSD_BOOKMARKS.base_case, (vis) => {}, [], depth)
430+
chunker.add(MSD_BOOKMARKS.base_case, (vis, l, r) => {
431+
if (left < right && mask >= 0)
432+
// need to sort -> remove color
433+
for (let k = left; k <= right && k < n; k++) {
434+
vis.array.deselect(k);
435+
}
436+
else
437+
// done -> sortedColor
438+
for (let k = left; k <= right && k < n; k++) {
439+
vis.array.selectColor(k, sortedColor);
440+
}
441+
}, [left, right], depth)
414442

415443
if (left < right && mask >= 0) {
416444
// partition leaves final i and j highlighted, sets prev_i,
@@ -534,8 +562,8 @@ arr],
534562
chunker.add(MSD_BOOKMARKS.done,
535563
vis => {
536564
vis.array.setStackDepth(0)
537-
for (let i = 0; i < n; i++) {
538-
vis.array.sorted(i);
565+
for (let k = 0; k < n; k++) {
566+
vis.array.sorted(k);
539567
}
540568
vis.array.clearVariables();
541569
vis.array.setStack(deriveStack(real_stack, finished_stack_frames));

src/algorithms/controllers/msort_arr_bup.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const runAColor = colors.peach;
2929
const runBColor = colors.sky;
3030
const runCColor = colors.sky; // replace all instances of runCColor with runBColor
3131
const sortColor = colors.leaf;
32+
const doneColor = colors.stone;
3233

3334

3435
export default {
@@ -367,7 +368,7 @@ export function run_msort() {
367368

368369
chunker.add('Done', (vis) => {
369370
for (let i = 0; i < size; i++) {
370-
highlight(vis, i, sortColor);
371+
highlight(vis, i, doneColor);
371372
}
372373

373374
set_simple_stack(vis.array, ["DONE"]);

src/algorithms/controllers/msort_arr_nat.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const runAColor = colors.peach;
2626
const runBColor = colors.sky;
2727
const runCColor = colors.sky; // replace all instances of runCColor with runBColor
2828
const sortColor = colors.leaf;
29+
const doneColor = colors.stone;
2930

3031
export default {
3132
explanation: msort_arr_nat,
@@ -402,7 +403,7 @@ export function run_msort() {
402403
chunker.add('Done', (vis, a) => {
403404
vis.array.set(a, 'msort_arr_nat');
404405
for (let i = 0; i < size; i++) {
405-
highlight(vis, i, sortColor);
406+
highlight(vis, i, doneColor);
406407
}
407408

408409
set_simple_stack(vis.array, ["DONE"]);

src/algorithms/controllers/msort_arr_td.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const apColor = colors.apple;
1616
const runAColor = colors.peach;
1717
const runBColor = colors.sky;
1818
const sortColor = colors.leaf;
19+
const doneColor = colors.stone;
1920

2021
const run = run_msort();
2122

@@ -748,6 +749,11 @@ export function run_msort() {
748749
// },
749750
// [entire_num_array.length - 1],
750751
// 0);
752+
chunker.add('Done', (vis) => {
753+
for (let i = 0; i < entire_num_array.length; i++) {
754+
highlight(vis, i, doneColor);
755+
}
756+
}, [], 0);
751757

752758
return msresult;
753759
}

src/algorithms/controllers/straightRadixSort.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ import {colors} from '../../components/DataStructures/colors';
1212
const RADIX_BITS = 2;
1313
const RADIX = 1 << RADIX_BITS;
1414

15+
// color handling code has some legacy stuff that could be cleaned up
1516
const highlightColor = colors.apple; // various highlights
16-
const changedColor = colors.wood; // for cumulative sums
17-
// colors for the 4 digits
17+
const changedColor = colors.wood; // was for cumulative sums
18+
// colors for the 4 digits XXX assumes radix 4 (or less)
1819
const digitColor = [colors.peach, colors.leaf, colors.sky, colors.plum];
1920

2021
const SRS_BOOKMARKS = {
@@ -42,12 +43,8 @@ const isCountExpanded = () => {
4243
return areExpanded(["Countingsort"]);
4344
};
4445

45-
const highlight = (array, index, isPrimaryColor = true) => {
46-
if (isPrimaryColor) {
47-
array.selectColor(index, highlightColor);
48-
} else {
49-
array.selectColor(index, changedColor);
50-
}
46+
const highlight = (array, index) => {
47+
array.selectColor(index, highlightColor);
5148
};
5249

5350
// color all digits in array according to digit value
@@ -231,11 +228,7 @@ export default {
231228
const num = A[i];
232229
chunker.add(SRS_BOOKMARKS.populate_for_loop,
233230
(vis, num, i, bit, count, A, sortedA, k, n) => {
234-
if (i === n - 1) {
235-
if (isCountExpanded()) {
236-
// unhighlight(vis.countArray, count.length - 1, false);
237-
}
238-
} else {
231+
if (i !== n - 1) {
239232
colorDigits(A, vis.array, k, n);
240233
if (isCountExpanded()) {
241234
setArray(vis.countArray, count);

src/components/DataStructures/Array/Array1DRenderer/Array1DRenderer.module.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
}
118118
}
119119

120-
&.patched {
120+
&.patched { // XXX inconsistent with 2D array (other stuff also maybe)
121121
background-color: var(--leaf);
122122

123123
span {
@@ -127,7 +127,7 @@
127127
}
128128

129129
&.sorted {
130-
background-color: var(--array-2d-row-bg-sorted);
130+
background-color: var(--stone);
131131

132132
span {
133133
color: var(--array-2d-row-col-sorted);

src/components/DataStructures/Array/Array2DRenderer/Array2DRenderer.module.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145
}
146146

147147
&.sorted {
148-
background-color: var(--array-2d-row-bg-sorted);
148+
background-color: var(--stone);
149149

150150
span {
151151
color: var(--array-2d-row-col-sorted);

0 commit comments

Comments
 (0)