Skip to content

Commit 280f5d3

Browse files
feat: Track index lookup counts in toString up to 9, with over 9 being represented by ' + '
Added tests as well.
1 parent bc28764 commit 280f5d3

File tree

3 files changed

+226
-13
lines changed

3 files changed

+226
-13
lines changed

Diff for: __tests__/test.js

+168
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,174 @@ describe('MatrixLog', () => {
424424
expect(lines[10]).toBe('');
425425
});
426426
});
427+
describe('child points used more than once in matrix', () => {
428+
it('can track 2 points', () => {
429+
const matrixLog = new MatrixLog('test-matrix', 2, 2);
430+
matrixLog.at({ x: 0, y: 0 })
431+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 })
432+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 });
433+
434+
const lines = matrixLog.toString('child-matrix').split(/\n/);
435+
expect(lines.length).toBe(7);
436+
expect(lines[0]).toBe('test-matrix x=0,y=0 child-matrix');
437+
expect(lines[1]).toBe('width=2,height=2 width=4,height=4');
438+
expect(lines[2]).toBe('[*][ ] [2][ ][ ][ ]');
439+
expect(lines[3]).toBe('[ ][ ] [ ][ ][ ][ ]');
440+
expect(lines[4]).toBe(' [ ][ ][ ][ ]');
441+
expect(lines[5]).toBe(' [ ][ ][ ][ ]');
442+
expect(lines[6]).toBe('');
443+
});
444+
it('can track 5 points', () => {
445+
const matrixLog = new MatrixLog('test-matrix', 2, 2);
446+
matrixLog.at({ x: 0, y: 0 })
447+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 })
448+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 })
449+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 })
450+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 })
451+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 });
452+
453+
const lines = matrixLog.toString('child-matrix').split(/\n/);
454+
expect(lines.length).toBe(7);
455+
expect(lines[0]).toBe('test-matrix x=0,y=0 child-matrix');
456+
expect(lines[1]).toBe('width=2,height=2 width=4,height=4');
457+
expect(lines[2]).toBe('[*][ ] [5][ ][ ][ ]');
458+
expect(lines[3]).toBe('[ ][ ] [ ][ ][ ][ ]');
459+
expect(lines[4]).toBe(' [ ][ ][ ][ ]');
460+
expect(lines[5]).toBe(' [ ][ ][ ][ ]');
461+
expect(lines[6]).toBe('');
462+
});
463+
it('can track 9 points', () => {
464+
const matrixLog = new MatrixLog('test-matrix', 2, 2);
465+
matrixLog.at({ x: 0, y: 0 })
466+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 })
467+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 })
468+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 })
469+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 })
470+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 })
471+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 })
472+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 })
473+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 })
474+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 });
475+
476+
const lines = matrixLog.toString('child-matrix').split(/\n/);
477+
expect(lines.length).toBe(7);
478+
expect(lines[0]).toBe('test-matrix x=0,y=0 child-matrix');
479+
expect(lines[1]).toBe('width=2,height=2 width=4,height=4');
480+
expect(lines[2]).toBe('[*][ ] [9][ ][ ][ ]');
481+
expect(lines[3]).toBe('[ ][ ] [ ][ ][ ][ ]');
482+
expect(lines[4]).toBe(' [ ][ ][ ][ ]');
483+
expect(lines[5]).toBe(' [ ][ ][ ][ ]');
484+
expect(lines[6]).toBe('');
485+
});
486+
it('can track for more than 9 points', () => {
487+
const matrixLog = new MatrixLog('test-matrix', 2, 2);
488+
matrixLog.at({ x: 0, y: 0 })
489+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 })
490+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 })
491+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 })
492+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 })
493+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 })
494+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 })
495+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 })
496+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 })
497+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 })
498+
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 });
499+
500+
const lines = matrixLog.toString('child-matrix').split(/\n/);
501+
expect(lines.length).toBe(7);
502+
expect(lines[0]).toBe('test-matrix x=0,y=0 child-matrix');
503+
expect(lines[1]).toBe('width=2,height=2 width=4,height=4');
504+
expect(lines[2]).toBe('[*][ ] [+][ ][ ][ ]');
505+
expect(lines[3]).toBe('[ ][ ] [ ][ ][ ][ ]');
506+
expect(lines[4]).toBe(' [ ][ ][ ][ ]');
507+
expect(lines[5]).toBe(' [ ][ ][ ][ ]');
508+
expect(lines[6]).toBe('');
509+
});
510+
});
511+
describe('child points used more than once out of matrix', () => {
512+
it('can track 2 points', () => {
513+
const matrixLog = new MatrixLog('test-matrix', 2, 2);
514+
matrixLog.at({ x: 0, y: 0 })
515+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 })
516+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 });
517+
518+
const lines = matrixLog.toString('child-matrix').split(/\n/);
519+
expect(lines.length).toBe(7);
520+
expect(lines[0]).toBe('test-matrix x=0,y=0 child-matrix');
521+
expect(lines[1]).toBe('width=2,height=2 width=4,height=4');
522+
expect(lines[2]).toBe('[*][ ] [ ][ ][ ][ ] 2 ');
523+
expect(lines[3]).toBe('[ ][ ] [ ][ ][ ][ ] - ');
524+
expect(lines[4]).toBe(' [ ][ ][ ][ ] - ');
525+
expect(lines[5]).toBe(' [ ][ ][ ][ ] - ');
526+
expect(lines[6]).toBe('');
527+
});
528+
it('can track 5 points', () => {
529+
const matrixLog = new MatrixLog('test-matrix', 2, 2);
530+
matrixLog.at({ x: 0, y: 0 })
531+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 })
532+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 })
533+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 })
534+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 })
535+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 });
536+
537+
const lines = matrixLog.toString('child-matrix').split(/\n/);
538+
expect(lines.length).toBe(7);
539+
expect(lines[0]).toBe('test-matrix x=0,y=0 child-matrix');
540+
expect(lines[1]).toBe('width=2,height=2 width=4,height=4');
541+
expect(lines[2]).toBe('[*][ ] [ ][ ][ ][ ] 5 ');
542+
expect(lines[3]).toBe('[ ][ ] [ ][ ][ ][ ] - ');
543+
expect(lines[4]).toBe(' [ ][ ][ ][ ] - ');
544+
expect(lines[5]).toBe(' [ ][ ][ ][ ] - ');
545+
expect(lines[6]).toBe('');
546+
});
547+
it('can track 9 points', () => {
548+
const matrixLog = new MatrixLog('test-matrix', 2, 2);
549+
matrixLog.at({ x: 0, y: 0 })
550+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 })
551+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 })
552+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 })
553+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 })
554+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 })
555+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 })
556+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 })
557+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 })
558+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 });
559+
560+
const lines = matrixLog.toString('child-matrix').split(/\n/);
561+
expect(lines.length).toBe(7);
562+
expect(lines[0]).toBe('test-matrix x=0,y=0 child-matrix');
563+
expect(lines[1]).toBe('width=2,height=2 width=4,height=4');
564+
expect(lines[2]).toBe('[*][ ] [ ][ ][ ][ ] 9 ');
565+
expect(lines[3]).toBe('[ ][ ] [ ][ ][ ][ ] - ');
566+
expect(lines[4]).toBe(' [ ][ ][ ][ ] - ');
567+
expect(lines[5]).toBe(' [ ][ ][ ][ ] - ');
568+
expect(lines[6]).toBe('');
569+
});
570+
it('can track for more than 9 points', () => {
571+
const matrixLog = new MatrixLog('test-matrix', 2, 2);
572+
matrixLog.at({ x: 0, y: 0 })
573+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 })
574+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 })
575+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 })
576+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 })
577+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 })
578+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 })
579+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 })
580+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 })
581+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 })
582+
.add({ name: 'child-matrix', x: 4, y: 0, width: 4, height: 4 });
583+
584+
const lines = matrixLog.toString('child-matrix').split(/\n/);
585+
expect(lines.length).toBe(7);
586+
expect(lines[0]).toBe('test-matrix x=0,y=0 child-matrix');
587+
expect(lines[1]).toBe('width=2,height=2 width=4,height=4');
588+
expect(lines[2]).toBe('[*][ ] [ ][ ][ ][ ] + ');
589+
expect(lines[3]).toBe('[ ][ ] [ ][ ][ ][ ] - ');
590+
expect(lines[4]).toBe(' [ ][ ][ ][ ] - ');
591+
expect(lines[5]).toBe(' [ ][ ][ ][ ] - ');
592+
expect(lines[6]).toBe('');
593+
});
594+
});
427595
});
428596
});
429597
describe('3d mode', () => {

Diff for: index.js

+57-12
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,33 @@ class MatrixLog {
196196
const row = [];
197197
for (let x = location.lowX; x <= location.highX; x++) {
198198
if (y < 0 || x < 0 || y >= location.height || x >= location.width) {
199-
row.push(
200-
points[z][y] && points[z][y].indexOf(x) > -1
201-
? MatrixLog.dependantPointOutString
202-
: MatrixLog.pointOutString
203-
);
199+
if (points[z][y]) {
200+
const count = this.countInArray(points[z][y], x);
201+
if (count === 1) {
202+
row.push(MatrixLog.dependantPointOutString);
203+
} else if (count > 1) {
204+
const string = MatrixLog['dependantPointOutString' + count] || MatrixLog.dependantPointOutStringOften;
205+
row.push(string);
206+
} else {
207+
row.push(MatrixLog.pointOutString);
208+
}
209+
} else {
210+
row.push(MatrixLog.pointOutString);
211+
}
204212
} else {
205-
row.push(
206-
points[z][y].indexOf(x) > -1
207-
? MatrixLog.dependantPointInString
208-
: MatrixLog.pointInString
209-
);
213+
if (points[z][y]) {
214+
const count = this.countInArray(points[z][y], x);
215+
if (count === 1) {
216+
row.push(MatrixLog.dependantPointInString);
217+
} else if (count > 1) {
218+
const string = MatrixLog['dependantPointInString' + count] || MatrixLog.dependantPointInStringOften;
219+
row.push(string);
220+
} else {
221+
row.push(MatrixLog.pointInString);
222+
}
223+
} else {
224+
row.push(MatrixLog.pointInString);
225+
}
210226
}
211227
}
212228
matrix.push(row.join(''));
@@ -269,11 +285,40 @@ class MatrixLog {
269285
}
270286
return result;
271287
}
288+
289+
countInArray(array, value) {
290+
let count = 0;
291+
let start = 0;
292+
while ((start = array.indexOf(value, start)) > -1) {
293+
count++;
294+
start++;
295+
}
296+
return count;
297+
}
272298
}
273299

274-
MatrixLog.dependantPointInString = '[*]';
275300
MatrixLog.pointInString = '[ ]';
276-
MatrixLog.dependantPointOutString = ' * ';
301+
MatrixLog.dependantPointInString = '[*]';
302+
MatrixLog.dependantPointInString2 = '[2]';
303+
MatrixLog.dependantPointInString3 = '[3]';
304+
MatrixLog.dependantPointInString4 = '[4]';
305+
MatrixLog.dependantPointInString5 = '[5]';
306+
MatrixLog.dependantPointInString6 = '[6]';
307+
MatrixLog.dependantPointInString7 = '[7]';
308+
MatrixLog.dependantPointInString8 = '[8]';
309+
MatrixLog.dependantPointInString9 = '[9]';
310+
MatrixLog.dependantPointInStringOften = '[+]';
311+
277312
MatrixLog.pointOutString = ' - ';
313+
MatrixLog.dependantPointOutString = ' * ';
314+
MatrixLog.dependantPointOutString2 = ' 2 ';
315+
MatrixLog.dependantPointOutString3 = ' 3 ';
316+
MatrixLog.dependantPointOutString4 = ' 4 ';
317+
MatrixLog.dependantPointOutString5 = ' 5 ';
318+
MatrixLog.dependantPointOutString6 = ' 6 ';
319+
MatrixLog.dependantPointOutString7 = ' 7 ';
320+
MatrixLog.dependantPointOutString8 = ' 8 ';
321+
MatrixLog.dependantPointOutString9 = ' 9 ';
322+
MatrixLog.dependantPointOutStringOften = ' + ';
278323

279324
module.exports = MatrixLog;

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "matrix-log.js",
3-
"version": "1.3.0",
3+
"version": "1.4.0",
44
"description": "A matrix log dependency utility. Useful for converting and testing algorithm behavior of CPU code to GPU code.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)