import Arrays from './../../util/Arrays' ; import StringBuilder from './../../util/StringBuilder' ; author dswitkin google export default class ByteMatrix { private bytes : Array < Uint8Array > ; public constructor ( private width , private height ) { const bytes = new Array < Uint8Array > ( height ) ; for ( let i = 0 ; i != height ; i ++ ) { bytes [ i ] = new Uint8Array ( width ) ; } this . bytes = bytes ; } public getHeight ( ) { return this . height ; } public getWidth ( ) { return this . width ; } public get ( x , y ) { return this . bytes [ y ] [ x ] ; } return public getArray ( ) { return this . bytes ; } public setNumber ( x , y , value ) { this . bytes [ y ] [ x ] = value ; } public setBoolean ( x , y , value ) { this . bytes [ y ] [ x ] = ( value ? 1 : 0 ) ; } public clear ( value ) { for ( const aByte of this . bytes ) { Arrays . fillUint8Array ( aByte , value ) ; } } public equals ( o ) { if ( ! ( o instanceof ByteMatrix ) ) { return false ; } const other = < ByteMatrix > o ; if ( this . width !== other . width ) { return false ; } if ( this . height !== other . height ) { return false ; } for ( let y = 0 , height = this . height ; y < height ; ++ y ) { const bytesY = this . bytes [ y ] ; const otherBytesY = other . bytes [ y ] ; for ( let x = 0 , width = this . width ; x < width ; ++ x ) { if ( bytesY [ x ] !== otherBytesY [ x ] ) { return false ; } } } return true ; } public toString ( ) { const result = new StringBuilder ( ) ; for ( let y = 0 , height = this . height ; y < height ; ++ y ) { const bytesY = this . bytes [ y ] ; for ( let x = 0 , width = this . width ; x < width ; ++ x ) { switch ( bytesY [ x ] ) { case 0 : result . append ( " 0" ) ; break ; case 1 : result . append ( " 1" ) ; break ; default : result . append ( "  " ) ; break ; } } result . append ( '\n' ) ; } return result . toString ( ) ; } }