From ccdcde4d997d3347a1800ed9c42d96ca1fca8005 Mon Sep 17 00:00:00 2001 From: skapoor68 Date: Mon, 10 Apr 2023 23:53:17 -0400 Subject: [PATCH] add indices to PM tables --- src/algo/BoyerMoore.js | 17 +++++++++++++++-- src/algo/BruteForce.js | 17 +++++++++++++++-- src/algo/KMP.js | 18 ++++++++++++++++-- src/algo/RabinKarp.js | 21 +++++++++++++++++---- 4 files changed, 63 insertions(+), 10 deletions(-) diff --git a/src/algo/BoyerMoore.js b/src/algo/BoyerMoore.js index 7e989517..19602b41 100644 --- a/src/algo/BoyerMoore.js +++ b/src/algo/BoyerMoore.js @@ -33,7 +33,7 @@ import Algorithm, { import { act } from '../anim/AnimationMain'; const ARRAY_START_X = 100; -const ARRAY_START_Y = 30; +const ARRAY_START_Y = 60; const MAX_LENGTH = 22; @@ -295,8 +295,21 @@ export default class BoyerMoore extends Algorithm { for (let i = 0; i < text.length; i++) { const xpos = i * this.cellSize + ARRAY_START_X; - const ypos = ARRAY_START_Y; + const ypos = ARRAY_START_Y - 25; this.textRowID[i] = this.nextIndex; + this.cmd( + act.createLabel, + this.nextIndex++, + i, + xpos, + ypos, + ); + } + + for (let i = 0; i < text.length; i++) { + const xpos = i * this.cellSize + ARRAY_START_X; + const ypos = ARRAY_START_Y; + this.textRowID[i + text.length] = this.nextIndex; this.cmd( act.createRectangle, this.nextIndex, diff --git a/src/algo/BruteForce.js b/src/algo/BruteForce.js index e4afa0ae..eb1f9bcf 100644 --- a/src/algo/BruteForce.js +++ b/src/algo/BruteForce.js @@ -32,7 +32,7 @@ import Algorithm, { import { act } from '../anim/AnimationMain'; const ARRAY_START_X = 100; -const ARRAY_START_Y = 30; +const ARRAY_START_Y = 60; const MAX_LENGTH = 22; @@ -170,8 +170,21 @@ export default class BruteForce extends Algorithm { for (let i = 0; i < text.length; i++) { xpos = i * this.cellSize + ARRAY_START_X; - ypos = ARRAY_START_Y; + ypos = ARRAY_START_Y - 25; this.textRowID[i] = this.nextIndex; + this.cmd( + act.createLabel, + this.nextIndex++, + i, + xpos, + ypos, + ); + } + + for (let i = 0; i < text.length; i++) { + xpos = i * this.cellSize + ARRAY_START_X; + ypos = ARRAY_START_Y; + this.textRowID[i + text.length] = this.nextIndex; this.cmd( act.createRectangle, this.nextIndex, diff --git a/src/algo/KMP.js b/src/algo/KMP.js index fecebb70..1565fb62 100644 --- a/src/algo/KMP.js +++ b/src/algo/KMP.js @@ -32,7 +32,7 @@ import Algorithm, { import { act } from '../anim/AnimationMain'; const ARRAY_START_X = 100; -const ARRAY_START_Y = 30; +const ARRAY_START_Y = 60; const MAX_LENGTH = 22; @@ -215,10 +215,24 @@ export default class KMP extends Algorithm { } let xpos, ypos; + for (let i = 0; i < text.length; i++) { xpos = i * this.cellSize + ARRAY_START_X; - ypos = ARRAY_START_Y; + ypos = ARRAY_START_Y - 25; this.textRowID[i] = this.nextIndex; + this.cmd( + act.createLabel, + this.nextIndex++, + i, + xpos, + ypos, + ); + } + + for (let i = 0; i < text.length; i++) { + xpos = i * this.cellSize + ARRAY_START_X; + ypos = ARRAY_START_Y; + this.textRowID[i + text.length] = this.nextIndex; this.cmd( act.createRectangle, this.nextIndex, diff --git a/src/algo/RabinKarp.js b/src/algo/RabinKarp.js index c262190d..81c4ce0b 100644 --- a/src/algo/RabinKarp.js +++ b/src/algo/RabinKarp.js @@ -32,7 +32,7 @@ import Algorithm, { import { act } from '../anim/AnimationMain'; const ARRAY_START_X = 100; -const ARRAY_START_Y = 30; +const ARRAY_START_Y = 60; const MAX_LENGTH = 22; @@ -223,8 +223,21 @@ export default class RabinKarp extends Algorithm { for (let i = 0; i < text.length; i++) { xpos = i * this.cellSize + ARRAY_START_X; - ypos = ARRAY_START_Y; + ypos = ARRAY_START_Y - 25; this.textRowID[i] = this.nextIndex; + this.cmd( + act.createLabel, + this.nextIndex++, + i, + xpos, + ypos, + ); + } + + for (let i = 0; i < text.length; i++) { + xpos = i * this.cellSize + ARRAY_START_X; + ypos = ARRAY_START_Y; + this.textRowID[i + text.length] = this.nextIndex; this.cmd( act.createRectangle, this.nextIndex, @@ -356,9 +369,9 @@ export default class RabinKarp extends Algorithm { this.unhighlight(6, 0); this.highlight(7, 0); xpos = i * this.cellSize + ARRAY_START_X; - this.cmd(act.createHighlightCircle, iPointerID, '#0000FF', xpos, ARRAY_START_Y); + this.cmd(act.createHighlightCircle, iPointerID, '#0000FF', xpos, ARRAY_START_Y, this.cellSize / 2); ypos = (row + 1) * this.cellSize + ARRAY_START_Y; - this.cmd(act.createHighlightCircle, jPointerID, '#0000FF', xpos, ypos); + this.cmd(act.createHighlightCircle, jPointerID, '#0000FF', xpos, ypos, this.cellSize / 2); this.cmd(act.step); this.unhighlight(7, 0); this.highlight(8, 0);