@@ -56,10 +56,18 @@ class Array2DTracer extends Tracer {
56
56
/**
57
57
* @param {array } array2d
58
58
* @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
59
64
*/
60
65
set ( array2d = [ ] , algo , kth = 1 , highlightRow , splitArray ) {
66
+ // set the array2d based of the splitArray values
61
67
if ( splitArray === undefined || splitArray . rowLength < 1 ) {
62
68
this . splitArray = { doSplit : false } ;
69
+
70
+ // set the value of array cells
63
71
this . data = array2d . map ( ( array1d ) =>
64
72
[ ...array1d ] . map ( ( value , i ) => new Element ( value , i ) )
65
73
) ;
@@ -68,15 +76,18 @@ class Array2DTracer extends Tracer {
68
76
this . splitArray = splitArray ;
69
77
this . splitArray . doSplit = true ;
70
78
79
+ // check if the rows have headers
71
80
if ( Array . isArray ( splitArray . rowHeader ) && splitArray . rowHeader . length ) {
72
81
this . splitArray . hasHeader = true ;
73
82
} else {
74
83
this . splitArray . hasHeader = false ;
75
84
}
76
85
let split = [ ] ;
77
86
87
+ // splitting the array into multiple arrays of length rowLength
78
88
let step = 0 ;
79
89
while ( step < array2d [ 0 ] . length ) {
90
+ // one smaller array
80
91
let arr2d = [ ] ;
81
92
for ( let i = 0 ; i < array2d . length ; i ++ ) {
82
93
arr2d . push ( [
@@ -94,9 +105,12 @@ class Array2DTracer extends Tracer {
94
105
}
95
106
96
107
step += splitArray . rowLength ;
108
+
109
+ // push to a main array of multiple split arrays
97
110
split . push ( arr2d ) ;
98
111
}
99
112
113
+ // set the value of array cells
100
114
for ( const item of split ) {
101
115
this . data . push ( item . map ( ( array1d ) =>
102
116
[ ...array1d ] . map ( ( value , i ) => new Element ( value , i ) )
@@ -161,8 +175,14 @@ class Array2DTracer extends Tracer {
161
175
}
162
176
}
163
177
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
+ */
166
186
fill ( sx , sy , ex = sx , ey = sy , c = 0 ) {
167
187
if ( ! this . splitArray . doSplit ) {
168
188
for ( let x = sx ; x <= ex ; x ++ ) {
@@ -172,8 +192,11 @@ class Array2DTracer extends Tracer {
172
192
}
173
193
} else {
174
194
for ( let i = 0 ; i < this . data . length ; i ++ ) {
195
+ // when it is just one cell for each row
175
196
if ( sy === ey ) {
176
197
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
177
200
if ( relativeY > this . splitArray . rowLength ) {
178
201
sy -= this . splitArray . rowLength ;
179
202
ey -= this . splitArray . rowLength ;
@@ -192,19 +215,23 @@ class Array2DTracer extends Tracer {
192
215
}
193
216
194
217
218
+ // when there are multiple columns
219
+ // if the relative start position is over the split array length, wrap to next split array
195
220
let relativeSY = sy + ( this . splitArray . hasHeader ? 1 : 0 ) ;
196
221
if ( relativeSY > this . splitArray . rowLength ) {
197
222
sy -= this . splitArray . rowLength ;
198
223
ey -= this . splitArray . rowLength ;
199
224
continue ;
200
225
}
201
226
227
+
228
+ // if the relative start position is over the split array length, limit
202
229
let relativeEY = ey + ( this . splitArray . hasHeader ? 1 : 0 ) ;
203
230
if ( relativeEY > this . splitArray . rowLength ) {
204
231
relativeEY = this . splitArray . rowLength ;
205
232
}
206
233
207
- // out of range
234
+ // out of range, stop
208
235
if ( relativeEY < 0 ) {
209
236
break ;
210
237
}
@@ -226,7 +253,13 @@ class Array2DTracer extends Tracer {
226
253
}
227
254
}
228
255
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
+ */
230
263
unfill ( sx , sy , ex = sx , ey = sy ) {
231
264
if ( ! this . splitArray . doSplit ) {
232
265
for ( let x = sx ; x <= ex ; x ++ ) {
@@ -353,7 +386,7 @@ class Array2DTracer extends Tracer {
353
386
// add variable to item if not undefined or null
354
387
if ( idx !== null && idx !== undefined ) {
355
388
// check if idx is in subarray
356
- // add i to account for header offset
389
+ // account for header offset
357
390
let relativeIdx = idx + ( this . splitArray . hasHeader ? 1 : 0 ) ;
358
391
if ( relativeIdx > 0 && relativeIdx <= this . splitArray . rowLength )
359
392
_newData [ row ] [ relativeIdx ] . variables . push ( v ) ;
@@ -382,7 +415,7 @@ class Array2DTracer extends Tracer {
382
415
return newEl ;
383
416
}
384
417
}
385
-
418
+
386
419
if ( ! this . splitArray . doSplit ) {
387
420
const newData = cloneDeepWith ( this . data , customizer ) ;
388
421
0 commit comments