Skip to content

Commit

Permalink
Merge pull request #316 from luciankt/pseudocode-rewrite
Browse files Browse the repository at this point in the history
Pseudocode revamp
  • Loading branch information
ljones315 authored Dec 27, 2023
2 parents c51bb02 + 269aa52 commit 38251ad
Show file tree
Hide file tree
Showing 37 changed files with 3,593 additions and 2,066 deletions.
69 changes: 62 additions & 7 deletions src/algo/Algorithm.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,32 @@ export default class Algorithm {
this.commands = [];
}

addCodeToCanvasBaseAll(code, key, start_x = 0, start_y = 0, line_height = CODE_LINE_HEIGHT) {
return {
english: this.addCodeToCanvasBase(
code[key]['english'],
start_x,
start_y,
line_height,
CODE_STANDARD_COLOR,
32,
),
code: this.addCodeToCanvasBase(
code[key]['code'],
start_x,
start_y,
line_height,
CODE_STANDARD_COLOR,
33,
),
};
}

addCodeToCanvasBase(code, start_x, start_y, line_height, standard_color, layer) {
line_height = typeof line_height !== 'undefined' ? line_height : CODE_LINE_HEIGHT;
standard_color =
typeof standard_color !== 'undefined' ? standard_color : CODE_STANDARD_COLOR;
layer = typeof layer !== 'undefined' ? layer : 32;
layer = typeof layer != 'undefined' ? layer : 32;
const isCode = true;
const codeID = Array(code.length);
let i, j;
Expand All @@ -244,17 +265,51 @@ export default class Algorithm {
return codeID;
}

highlight(ind1, ind2, codeID) {
codeID = codeID === undefined ? this.codeID : codeID;
this.cmd(act.setForegroundColor, codeID[ind1][ind2], CODE_HIGHLIGHT_COLOR);
highlight(ind1, ind2, codeID, type) {
if (!codeID) return;
// Type specified
if (type) {
this.highlight(ind1, ind2, codeID[type]);
return;
}
// Single pseudocode type
if (codeID[0] !== undefined) {
this.cmd(act.setForegroundColor, codeID[ind1][ind2], CODE_HIGHLIGHT_COLOR);
return;
}
// Multiple pseudocode types
if (codeID.english.length)
this.cmd(act.setForegroundColor, codeID.english[ind1][ind2], CODE_HIGHLIGHT_COLOR);
if (codeID.code.length)
this.cmd(act.setForegroundColor, codeID.code[ind1][ind2], CODE_HIGHLIGHT_COLOR);
}

unhighlight(ind1, ind2, codeID) {
codeID = codeID === undefined ? this.codeID : codeID;
this.cmd(act.setForegroundColor, codeID[ind1][ind2], CODE_STANDARD_COLOR);
unhighlight(ind1, ind2, codeID, type) {
if (!codeID) return;
// Type specified
if (type) {
this.unhighlight(ind1, ind2, codeID[type]);
return;
}
// Single pseudocode type
if (codeID[0] !== undefined) {
this.cmd(act.setForegroundColor, codeID[ind1][ind2], CODE_STANDARD_COLOR);
return;
}
// Multiple pseudocode types
if (codeID.english.length)
this.cmd(act.setForegroundColor, codeID.english[ind1][ind2], CODE_STANDARD_COLOR);
if (codeID.code.length)
this.cmd(act.setForegroundColor, codeID.code[ind1][ind2], CODE_STANDARD_COLOR);
}

removeCode(codeID) {
if (!codeID) return;
if (codeID.english) {
this.removeCode(codeID.english);
this.removeCode(codeID.code);
return;
}
for (let i = 0; i < codeID.length; i++) {
for (let j = 0; j < codeID[i].length; j++) {
this.cmd(act.delete, codeID[i][j]);
Expand Down
114 changes: 45 additions & 69 deletions src/algo/ArrayList.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import Algorithm, {
addLabelToAlgorithmBar,
} from './Algorithm';
import { act } from '../anim/AnimationMain';
import pseudocodeText from '../pseudocode.json';

const INFO_MSG_X = 25;
const INFO_MSG_Y = 15;
Expand Down Expand Up @@ -208,60 +209,7 @@ export default class ArrayList extends Algorithm {
this.length = SIZE;
this.commands = [];

this.addFBCode = [
['procedure addFront(data)'],
[' addAtIndex(0, data)'],
['end procedure'],
[''],
['procedure addBack(data)'],
[' addAtIndex(size, data)'],
['end procedure'],
];

this.addIndexCode = [
['procedure addAtIndex(index, data)'],
[' if size == array.length'],
[' T[] newArray ← new array[size * 2]'],
[' for i ← 0 to index - 1, i++:'],
[' newArray[i] ← array[i]'],
[' newArray[index] ← data'],
[' for i ← index to size - 1, i++:'],
[' newArray[i + 1] ← array[i]'],
[' array ← newArray'],
[' else'],
[' for i ← size to index + 1, i--:'],
[' array[i] ← array[i - 1]'],
[' array[index] ← data'],
[' size++'],
['end procedure'],
];

this.removeFBCode = [
['procedure removeFront()'],
[' removeFromIndex(0)'],
['end procedure'],
[''],
['procedure removeBack()'],
[' removeFromIndex(size - 1)'],
['end procedure'],
];

this.removeIndexCode = [
['procedure removeFromIndex(index)'],
[' T data ← array[index]'],
[' array[index] ← null'],
[' for i ← index to size - 2, i++:'],
[' array[i] ← array[i + 1]'],
[' array[size - 1] ← null'],
[' size--'],
[' return data'],
['end procedure'],
];

this.addFBCodeID = [];
this.addIndexCodeID = [];
this.removeFBCodeID = [];
this.removeIndexCodeID = [];
this.pseudocode = pseudocodeText.ArrayList;

this.infoLabelID = this.nextIndex++;
this.cmd(act.createLabel, this.infoLabelID, '', INFO_MSG_X, INFO_MSG_Y, 0);
Expand Down Expand Up @@ -415,7 +363,7 @@ export default class ArrayList extends Algorithm {
i--;
} else {
set.add(value);
this.implementAction(this.add.bind(this), value, 0);
this.implementAction(this.add.bind(this), value, 0, true, false, false, true);
}
this.animationManager.skipForward();
this.animationManager.clearHistory();
Expand Down Expand Up @@ -472,16 +420,24 @@ export default class ArrayList extends Algorithm {
return this.commands;
}

add(elemToAdd, index, isAddFront, isAddBack, isAddIndex) {
add(elemToAdd, index, isAddFront, isAddBack, isAddIndex, skipPseudocode) {
this.commands = [];
this.setInfoText('');

this.addFBCodeID = this.addCodeToCanvasBase(this.addFBCode, CODE_START_X, CODE_START_Y);
this.addIndexCodeID = this.addCodeToCanvasBase(
this.addIndexCode,
CODE_START_X + 250,
CODE_START_Y,
);
if (!skipPseudocode) {
this.addFBCodeID = this.addCodeToCanvasBaseAll(
this.pseudocode,
'addFB',
CODE_START_X,
CODE_START_Y,
);
this.addIndexCodeID = this.addCodeToCanvasBaseAll(
this.pseudocode,
'addIndex',
CODE_START_X + 300,
CODE_START_Y,
);
}

const labPushID = this.nextIndex++;
const labPushValID = this.nextIndex++;
Expand Down Expand Up @@ -582,8 +538,10 @@ export default class ArrayList extends Algorithm {

this.size = this.size + 1;

this.removeCode(this.addFBCodeID);
this.removeCode(this.addIndexCodeID);
if (!skipPseudocode) {
this.removeCode(this.addFBCodeID);
this.removeCode(this.addIndexCodeID);
}

return this.commands;
}
Expand All @@ -592,14 +550,16 @@ export default class ArrayList extends Algorithm {
this.commands = [];
this.setInfoText('');

this.removeFBCodeID = this.addCodeToCanvasBase(
this.removeFBCode,
this.removeFBCodeID = this.addCodeToCanvasBaseAll(
this.pseudocode,
'removeFB',
CODE_START_X,
CODE_START_Y,
);
this.removeIndexCodeID = this.addCodeToCanvasBase(
this.removeIndexCode,
CODE_START_X + 250,
this.removeIndexCodeID = this.addCodeToCanvasBaseAll(
this.pseudocode,
'removeIndex',
CODE_START_X + 300,
CODE_START_Y,
);

Expand Down Expand Up @@ -701,6 +661,19 @@ export default class ArrayList extends Algorithm {
this.commands = [];
this.setInfoText('');

this.addFBCodeID = this.addCodeToCanvasBaseAll(
this.pseudocode,
'addFB',
CODE_START_X,
CODE_START_Y,
);
this.addIndexCodeID = this.addCodeToCanvasBaseAll(
this.pseudocode,
'addIndex',
CODE_START_X + 300,
CODE_START_Y,
);

const labPushID = this.nextIndex++;
const labPushValID = this.nextIndex++;
const labPushResizeID = this.nextIndex++;
Expand Down Expand Up @@ -910,6 +883,9 @@ export default class ArrayList extends Algorithm {

this.size = this.size + 1;

this.removeCode(this.addFBCodeID);
this.removeCode(this.addIndexCodeID);

return this.commands;
}

Expand Down
Loading

0 comments on commit 38251ad

Please sign in to comment.