Skip to content

Commit b0a1289

Browse files
committed
chore: added comments
fix: missing automated tests
1 parent 39b02d3 commit b0a1289

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

.github/workflows/hashing.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ jobs:
2323
npm install
2424
npm run test-hashinsert
2525
npm run test-hashsearch
26+
npm run test-hashdelete

src/components/DataStructures/Array/Array2DTracer.js

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,18 @@ class Array2DTracer extends Tracer {
5656
/**
5757
* @param {array} array2d
5858
* @param {string} algo used to mark if it is a specific algorithm
59+
* @param {any} kth used to display kth
60+
* @param {number} highlightRow used mark the row to highlight
61+
* @param {Object} splitArray determine how to split the array
62+
* @param {number} splitArray.rowLength determine the length of a split array
63+
* @param {string[]} splitArray.rowHeader determine the header of each row of a split array
5964
*/
6065
set(array2d = [], algo, kth = 1, highlightRow, splitArray) {
66+
// set the array2d based of the splitArray values
6167
if (splitArray === undefined || splitArray.rowLength < 1) {
6268
this.splitArray = {doSplit: false};
69+
70+
// set the value of array cells
6371
this.data = array2d.map((array1d) =>
6472
[...array1d].map((value, i) => new Element(value, i))
6573
);
@@ -68,15 +76,18 @@ class Array2DTracer extends Tracer {
6876
this.splitArray = splitArray;
6977
this.splitArray.doSplit = true;
7078

79+
// check if the rows have headers
7180
if (Array.isArray(splitArray.rowHeader) && splitArray.rowHeader.length) {
7281
this.splitArray.hasHeader = true;
7382
} else {
7483
this.splitArray.hasHeader = false;
7584
}
7685
let split = [];
7786

87+
// splitting the array into multiple arrays of length rowLength
7888
let step = 0;
7989
while (step < array2d[0].length) {
90+
// one smaller array
8091
let arr2d = [];
8192
for (let i = 0; i < array2d.length; i++ ) {
8293
arr2d.push([
@@ -94,9 +105,12 @@ class Array2DTracer extends Tracer {
94105
}
95106

96107
step += splitArray.rowLength;
108+
109+
// push to a main array of multiple split arrays
97110
split.push(arr2d);
98111
}
99112

113+
// set the value of array cells
100114
for (const item of split) {
101115
this.data.push(item.map((array1d) =>
102116
[...array1d].map((value, i) => new Element(value, i))
@@ -161,8 +175,14 @@ class Array2DTracer extends Tracer {
161175
}
162176
}
163177

164-
// a simple fill function based on aia themes
165-
// where green=1, yellow=2, and red=3
178+
/**
179+
* a simple fill function based on aia themes
180+
* @param {number} sx the starting row to fill
181+
* @param {number} sy the starting row to fill
182+
* @param {number} ex the ending row to fill, defaults to sx
183+
* @param {number} ey the ending row to fill, defaults to sy
184+
* @param {number} c the color value, where green=1, yellow=2, and red=3
185+
*/
166186
fill(sx, sy, ex = sx, ey = sy, c = 0) {
167187
if (!this.splitArray.doSplit) {
168188
for (let x = sx; x <= ex; x++) {
@@ -172,8 +192,11 @@ class Array2DTracer extends Tracer {
172192
}
173193
} else {
174194
for (let i = 0; i < this.data.length; i++) {
195+
// when it is just one cell for each row
175196
if (sy === ey) {
176197
let relativeY = sy + (this.splitArray.hasHeader ? 1 : 0);
198+
199+
// if the relative start position is over the split array length, wrap to next split array
177200
if (relativeY > this.splitArray.rowLength) {
178201
sy -= this.splitArray.rowLength;
179202
ey -= this.splitArray.rowLength;
@@ -192,19 +215,23 @@ class Array2DTracer extends Tracer {
192215
}
193216

194217

218+
// when there are multiple columns
219+
// if the relative start position is over the split array length, wrap to next split array
195220
let relativeSY = sy + (this.splitArray.hasHeader ? 1 : 0);
196221
if (relativeSY > this.splitArray.rowLength) {
197222
sy -= this.splitArray.rowLength;
198223
ey -= this.splitArray.rowLength;
199224
continue;
200225
}
201226

227+
228+
// if the relative start position is over the split array length, limit
202229
let relativeEY = ey + (this.splitArray.hasHeader ? 1 : 0);
203230
if (relativeEY > this.splitArray.rowLength) {
204231
relativeEY = this.splitArray.rowLength;
205232
}
206233

207-
// out of range
234+
// out of range, stop
208235
if (relativeEY < 0) {
209236
break;
210237
}
@@ -226,7 +253,13 @@ class Array2DTracer extends Tracer {
226253
}
227254
}
228255

229-
// unfills the given element (used with fill)
256+
/**
257+
* unfills the given element (used with fill)
258+
* @param {number} sx the starting row to unfill
259+
* @param {number} sy the starting row to unfill
260+
* @param {number} ex the ending row to unfill, defaults to sx
261+
* @param {number} ey the ending row to unfill, defaults to sy
262+
*/
230263
unfill(sx, sy, ex = sx, ey = sy) {
231264
if (!this.splitArray.doSplit) {
232265
for (let x = sx; x <= ex; x++) {
@@ -353,7 +386,7 @@ class Array2DTracer extends Tracer {
353386
// add variable to item if not undefined or null
354387
if (idx !== null && idx !== undefined) {
355388
// check if idx is in subarray
356-
// add i to account for header offset
389+
// account for header offset
357390
let relativeIdx = idx + (this.splitArray.hasHeader ? 1 : 0);
358391
if (relativeIdx > 0 && relativeIdx <= this.splitArray.rowLength)
359392
_newData[row][relativeIdx].variables.push(v);
@@ -382,7 +415,7 @@ class Array2DTracer extends Tracer {
382415
return newEl;
383416
}
384417
}
385-
418+
386419
if (!this.splitArray.doSplit) {
387420
const newData = cloneDeepWith(this.data, customizer);
388421

src/components/DataStructures/common/Tracer.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ class Tracer {
3838
set() {
3939
}
4040

41-
// set visualiser size multiplier
41+
/**
42+
* Set visualiser size (flex value for renderer)
43+
* @param {*} size
44+
*/
4245
setSize(size) {
4346
this.size = size;
4447
}

0 commit comments

Comments
 (0)