Skip to content

Commit ab8b441

Browse files
Add 3d handling and docs
1 parent 811f4d8 commit ab8b441

File tree

3 files changed

+319
-151
lines changed

3 files changed

+319
-151
lines changed

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,59 @@ width=2,height=2 width=4,height=4
7474
7575
```
7676

77+
## What does a 3d log (output of `.toString(string)`) look like?
78+
79+
```
80+
test-matrix x=0,y=0,z=0 child-matrix
81+
width=2,height=2,depth=3 width=4,height=4,depth=2
82+
[*][ ] [ ][ ] [*][*][ ][ ]
83+
[ ][ ] [ ][ ] [*][*][ ][ ]
84+
[ ][ ][ ][ ]
85+
[ ][ ] [ ][ ][ ][ ]
86+
[ ][ ]
87+
[ ][ ][ ][ ]
88+
[ ][ ][ ][ ]
89+
[ ][ ][ ][ ]
90+
[ ][ ][ ][ ]
91+
92+
test-matrix x=1,y=0,z=0 child-matrix
93+
width=2,height=2,depth=3 width=4,height=4,depth=2
94+
[ ][*] [ ][ ] [ ][ ][*][*]
95+
[ ][ ] [ ][ ] [ ][ ][*][*]
96+
[ ][ ][ ][ ]
97+
[ ][ ] [ ][ ][ ][ ]
98+
[ ][ ]
99+
[ ][ ][ ][ ]
100+
[ ][ ][ ][ ]
101+
[ ][ ][ ][ ]
102+
[ ][ ][ ][ ]
103+
104+
test-matrix x=0,y=1,z=0 child-matrix
105+
width=2,height=2,depth=3 width=4,height=4,depth=2
106+
[ ][ ] [ ][ ] [ ][ ][ ][ ]
107+
[*][ ] [ ][ ] [ ][ ][ ][ ]
108+
[*][*][ ][ ]
109+
[ ][ ] [*][*][ ][ ]
110+
[ ][ ]
111+
[ ][ ][ ][ ]
112+
[ ][ ][ ][ ]
113+
[ ][ ][ ][ ]
114+
[ ][ ][ ][ ]
115+
116+
test-matrix x=1,y=1,z=0 child-matrix
117+
width=2,height=2,depth=3 width=4,height=4,depth=2
118+
[ ][ ] [ ][ ] [ ][ ][ ][ ]
119+
[ ][*] [ ][ ] [ ][ ][ ][ ]
120+
[ ][ ][*][*]
121+
[ ][ ] [ ][ ][*][*]
122+
[ ][ ]
123+
[ ][ ][ ][ ]
124+
[ ][ ][ ][ ]
125+
[ ][ ][ ][ ]
126+
[ ][ ][ ][ ]
127+
// etc.
128+
```
129+
77130
## What does this log mean?
78131
1. Suppose we had a (contrived) block of CPU code we'd like to run on the GPU:
79132
```js

__tests__/test.js

Lines changed: 161 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const MatrixLog = require('../index');
33
describe('MatrixLog', () => {
44
describe('2d', () => {
55
describe('.add()', () => {
6-
test('can work on a smaller than parent pattern', () => {
6+
it('can work on a smaller than parent pattern', () => {
77
const x = 0;
88
const y = 0;
99
const z = 0;
@@ -24,7 +24,7 @@ describe('MatrixLog', () => {
2424
});
2525
});
2626
describe('.toString()', () => {
27-
test('repeats for every point logged', () => {
27+
it('repeats for every point logged', () => {
2828
const matrixLog = new MatrixLog('test-matrix', 2, 1);
2929

3030
matrixLog.at({ x: 0, y: 0 })
@@ -361,87 +361,172 @@ describe('MatrixLog', () => {
361361
});
362362
});
363363
describe('.toString()', () => {
364-
describe('parent of 2x2, pattern 4,4', () => {
365-
test('0,0', () => {
366-
const matrixLog = new MatrixLog('test-matrix', 2, 2);
367-
matrixLog.at({ x: 0, y: 0 })
368-
.add({ name: 'child-matrix', x: 0, y: 0, width: 4, height: 4 })
369-
.add({ name: 'child-matrix', x: 0, y: 1, width: 4, height: 4 })
370-
.add({ name: 'child-matrix', x: 1, y: 0, width: 4, height: 4 })
371-
.add({ name: 'child-matrix', x: 1, y: 1, width: 4, height: 4 });
364+
describe('parent of 2x2x3, pattern 4,4,4', () => {
365+
test('0,0,', () => {
366+
const matrixLog = new MatrixLog('test-matrix', 2, 2, 3);
367+
matrixLog.at({ x: 0, y: 0, z: 2 })
368+
.add({ name: 'child-matrix', x: 0, y: 0, z: 3, width: 4, height: 4, depth: 4 })
369+
.add({ name: 'child-matrix', x: 0, y: 1, z: 3, width: 4, height: 4, depth: 4 })
370+
.add({ name: 'child-matrix', x: 1, y: 0, z: 3, width: 4, height: 4, depth: 4 })
371+
.add({ name: 'child-matrix', x: 1, y: 1, z: 3, width: 4, height: 4, depth: 4 });
372372

373373
console.log(matrixLog.toString('child-matrix'));
374374

375375
const lines = matrixLog.toString('child-matrix').split(/\n/);
376-
expect(lines.length).toBe(7);
377-
expect(lines[0]).toBe('test-matrix x=0,y=0 child-matrix');
378-
expect(lines[1]).toBe('width=2,height=2 width=4,height=4');
379-
expect(lines[2]).toBe('[*][ ] [*][*][ ][ ]');
380-
expect(lines[3]).toBe('[ ][ ] [*][*][ ][ ]');
381-
expect(lines[4]).toBe(' [ ][ ][ ][ ]');
382-
expect(lines[5]).toBe(' [ ][ ][ ][ ]');
383-
expect(lines[6]).toBe('');
384-
});
385-
386-
test('1,0', () => {
387-
const matrixLog = new MatrixLog('test-matrix', 2, 2);
388-
matrixLog.at({ x: 1, y: 0 })
389-
.add({ name: 'child-matrix', x: 2, y: 0, width: 4, height: 4 })
390-
.add({ name: 'child-matrix', x: 3, y: 0, width: 4, height: 4 })
391-
.add({ name: 'child-matrix', x: 2, y: 1, width: 4, height: 4 })
392-
.add({ name: 'child-matrix', x: 3, y: 1, width: 4, height: 4 });
393-
394-
395-
const lines = matrixLog.toString('child-matrix').split(/\n/);
396-
expect(lines.length).toBe(7);
397-
expect(lines[0]).toBe('test-matrix x=1,y=0 child-matrix');
398-
expect(lines[1]).toBe('width=2,height=2 width=4,height=4');
399-
expect(lines[2]).toBe('[ ][*] [ ][ ][*][*]');
400-
expect(lines[3]).toBe('[ ][ ] [ ][ ][*][*]');
401-
expect(lines[4]).toBe(' [ ][ ][ ][ ]');
402-
expect(lines[5]).toBe(' [ ][ ][ ][ ]');
403-
expect(lines[6]).toBe('');
376+
expect(lines.length).toBe(12);
377+
expect(lines[0]).toBe('test-matrix x=0,y=0,z=2 child-matrix');
378+
expect(lines[1]).toBe('width=2,height=2,depth=3 width=4,height=4,depth=4');
379+
expect(lines[2]).toBe('[ ][ ] [ ][ ] [ ][ ][ ][ ] [ ][ ][ ][ ]');
380+
expect(lines[3]).toBe('[ ][ ] [ ][ ] [ ][ ][ ][ ] [ ][ ][ ][ ]');
381+
expect(lines[4]).toBe(' [ ][ ][ ][ ] [ ][ ][ ][ ]');
382+
expect(lines[5]).toBe('[*][ ] [ ][ ][ ][ ] [ ][ ][ ][ ]');
383+
expect(lines[6]).toBe('[ ][ ] ');
384+
expect(lines[7]).toBe(' [ ][ ][ ][ ] [*][*][ ][ ]');
385+
expect(lines[8]).toBe(' [ ][ ][ ][ ] [*][*][ ][ ]');
386+
expect(lines[9]).toBe(' [ ][ ][ ][ ] [ ][ ][ ][ ]');
387+
expect(lines[10]).toBe(' [ ][ ][ ][ ] [ ][ ][ ][ ]');
388+
expect(lines[11]).toBe('');
404389
});
390+
});
391+
});
392+
});
393+
describe('.getChildLog()', () => {
394+
test('4,4,4', () => {
395+
const matrixLog = new MatrixLog('parent', 4, 4, 4);
396+
matrixLog.at({ x: 0, y: 0, z: 0 });
397+
matrixLog.add({
398+
name: 'child',
399+
x: 0,
400+
y: 0,
401+
z: 0,
402+
width: 4,
403+
height: 4,
404+
depth: 4
405+
});
406+
matrixLog.add({
407+
name: 'child',
408+
x: 1,
409+
y: 1,
410+
z: 1,
411+
width: 4,
412+
height: 4,
413+
depth: 4
414+
});
415+
matrixLog.add({
416+
name: 'child',
417+
x: 2,
418+
y: 2,
419+
z: 2,
420+
width: 4,
421+
height: 4,
422+
depth: 4
423+
});
424+
matrixLog.add({
425+
name: 'child',
426+
x: 3,
427+
y: 3,
428+
z: 3,
429+
width: 4,
430+
height: 4,
431+
depth: 4
432+
});
405433

406-
test('0,1', () => {
407-
const matrixLog = new MatrixLog('test-matrix', 2, 2);
408-
matrixLog.at({ x: 0, y: 1 })
409-
.add({ name: 'child-matrix', x: 0, y: 2, width: 4, height: 4 })
410-
.add({ name: 'child-matrix', x: 1, y: 2, width: 4, height: 4 })
411-
.add({ name: 'child-matrix', x: 0, y: 3, width: 4, height: 4 })
412-
.add({ name: 'child-matrix', x: 1, y: 3, width: 4, height: 4 });
413-
414-
const lines = matrixLog.toString('child-matrix').split(/\n/);
415-
expect(lines.length).toBe(7);
416-
expect(lines[0]).toBe('test-matrix x=0,y=1 child-matrix');
417-
expect(lines[1]).toBe('width=2,height=2 width=4,height=4');
418-
expect(lines[2]).toBe('[ ][ ] [ ][ ][ ][ ]');
419-
expect(lines[3]).toBe('[*][ ] [ ][ ][ ][ ]');
420-
expect(lines[4]).toBe(' [*][*][ ][ ]');
421-
expect(lines[5]).toBe(' [*][*][ ][ ]');
422-
expect(lines[6]).toBe('');
423-
});
424-
425-
test('1,1', () => {
426-
const matrixLog = new MatrixLog('test-matrix', 2, 2);
427-
428-
matrixLog.at({ x: 1, y: 1 })
429-
.add({ name: 'child-matrix', x: 2, y: 2, width: 4, height: 4 })
430-
.add({ name: 'child-matrix', x: 3, y: 2, width: 4, height: 4 })
431-
.add({ name: 'child-matrix', x: 2, y: 3, width: 4, height: 4 })
432-
.add({ name: 'child-matrix', x: 3, y: 3, width: 4, height: 4 });
433-
434+
const matrices = matrixLog.getChildLog('child');
435+
expect(matrices).toEqual([
436+
['[*][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]'],
437+
['[ ][ ][ ][ ]', '[ ][*][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]'],
438+
['[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][*][ ]', '[ ][ ][ ][ ]'],
439+
['[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][*]']
440+
]
441+
);
442+
});
443+
});
444+
describe('.getParentLog()', () => {
445+
test('4,4,4', () => {
446+
const matrixLog = new MatrixLog('parent', 4, 4, 4);
447+
matrixLog.at({ x: 0, y: 0, z: 0 });
448+
expect(matrixLog.getParentLog()).toEqual([
449+
['[*][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]'],
450+
['[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]'],
451+
['[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]'],
452+
['[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]']
453+
]
454+
);
455+
matrixLog.at({ x: 1, y: 1, z: 1 });
456+
expect(matrixLog.getParentLog()).toEqual([
457+
['[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]'],
458+
['[ ][ ][ ][ ]', '[ ][*][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]'],
459+
['[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]'],
460+
['[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]']
461+
]
462+
);
463+
matrixLog.at({ x: 2, y: 2, z: 2 });
464+
expect(matrixLog.getParentLog()).toEqual([
465+
['[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]'],
466+
['[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]'],
467+
['[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][*][ ]', '[ ][ ][ ][ ]'],
468+
['[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]']
469+
]
470+
);
471+
matrixLog.at({ x: 3, y: 3, z: 3 });
472+
expect(matrixLog.getParentLog()).toEqual([
473+
['[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]'],
474+
['[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]'],
475+
['[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]'],
476+
['[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][ ]', '[ ][ ][ ][*]']
477+
]
478+
);
479+
});
480+
});
481+
describe('.logToGrid()', () => {
482+
test('4,4,4', () => {
483+
const matrixLog = new MatrixLog('parent', 4, 4, 4);
484+
expect(matrixLog.logToGrid([
485+
['[1][2][3][4]', '[5][6][7][8]', '[9][10][11][12]', '[13][14][15][16]'],
486+
['[17][18][19][20]', '[21][22][23][24]', '[25][26][27][28]', '[29][30][31][32]'],
487+
['[33][34][35][36]', '[37][38][39][40]', '[41][42][43][44]', '[45][46][47][48]'],
488+
['[49][50][51][52]', '[53][54][55][56]', '[57][58][59][60]', '[61][62][63][64]']
489+
], 4)).toEqual([
490+
'[1][2][3][4] [17][18][19][20]',
491+
'[5][6][7][8] [21][22][23][24]',
492+
'[9][10][11][12] [25][26][27][28]',
493+
'[13][14][15][16] [29][30][31][32]',
494+
' ',
495+
'[33][34][35][36] [49][50][51][52]',
496+
'[37][38][39][40] [53][54][55][56]',
497+
'[41][42][43][44] [57][58][59][60]',
498+
'[45][46][47][48] [61][62][63][64]'
499+
]
500+
);
501+
});
502+
describe('.mergeGrids()', () => {
503+
test('4,4,3', () => {
504+
const matrixLog = new MatrixLog('parent', 4, 4, 3);
505+
matrixLog.at({ x: 0, y: 0, z: 0 })
506+
.add({
507+
name: 'child',
508+
x: 0,
509+
y: 0,
510+
z: 0,
511+
width: 3,
512+
height: 3,
513+
depth: 3,
514+
});
434515

435-
const lines = matrixLog.toString('child-matrix').split(/\n/);
436-
expect(lines.length).toBe(7);
437-
expect(lines[0]).toBe('test-matrix x=1,y=1 child-matrix');
438-
expect(lines[1]).toBe('width=2,height=2 width=4,height=4');
439-
expect(lines[2]).toBe('[ ][ ] [ ][ ][ ][ ]');
440-
expect(lines[3]).toBe('[ ][*] [ ][ ][ ][ ]');
441-
expect(lines[4]).toBe(' [ ][ ][*][*]');
442-
expect(lines[5]).toBe(' [ ][ ][*][*]');
443-
expect(lines[6]).toBe('');
444-
});
516+
const parentGrid = matrixLog.logToGrid(matrixLog.getParentLog(), matrixLog.depth);
517+
const childGrid = matrixLog.logToGrid(matrixLog.getChildLog('child'), matrixLog.location['child'].depth);
518+
519+
expect(matrixLog.mergeGrids(parentGrid, childGrid)).toEqual([
520+
'[*][ ][ ][ ] [ ][ ][ ][ ]' + ' '.repeat(matrixLog.rowSpacing) + '[*][ ][ ] [ ][ ][ ]',
521+
'[ ][ ][ ][ ] [ ][ ][ ][ ]' + ' '.repeat(matrixLog.rowSpacing) + '[ ][ ][ ] [ ][ ][ ]',
522+
'[ ][ ][ ][ ] [ ][ ][ ][ ]' + ' '.repeat(matrixLog.rowSpacing) + '[ ][ ][ ] [ ][ ][ ]',
523+
'[ ][ ][ ][ ] [ ][ ][ ][ ]' + ' '.repeat(matrixLog.rowSpacing) + ' ',
524+
' ' + ' '.repeat(matrixLog.rowSpacing) + '[ ][ ][ ]',
525+
'[ ][ ][ ][ ] ' + ' '.repeat(matrixLog.rowSpacing) + '[ ][ ][ ]',
526+
'[ ][ ][ ][ ] ' + ' '.repeat(matrixLog.rowSpacing) + '[ ][ ][ ]',
527+
'[ ][ ][ ][ ]',
528+
'[ ][ ][ ][ ]',
529+
]);
445530
});
446531
});
447532
});

0 commit comments

Comments
 (0)