type SliceRange = [ number , number ] ; class Array2D < T > { private array : Array < Array < T > > ; private width : number ; private height : number ; constructor ( array ) { this . array = array ; this . updateWidth ( ) ; this . updateHeight ( ) ; } private updateWidth ( ) { this . width = Math . min ( ... this . array . map ( row => row . length ) ) ; } private updateHeight ( ) { this . height = this . array . length ; } getHeight ( ) { return this . height ; } getWidth ( ) { return this . width ; } get ( x , y ) { return this . array [ y ] [ x ] ; } set ( x , y , value ) { if ( ! this . array [ y ] ) { this . array [ y ] = [ ] ; } this . array [ y ] [ x ] = value ; this . updateWidth ( ) ; this . updateHeight ( ) ; } slice ( [ xMin , xMax ] , [ yMin , yMax ] ) { return new Array2D ( this . array . slice ( yMin , yMax ) . map ( ( row ) => { return row . slice ( xMin , xMax ) ; } ) ) ; } forEach ( callback ) { this . array . forEach ( ( row , y ) => { row . forEach ( ( item , x ) => { callback ( item , x , y ) ; } ) ; } ) ; } } export default Array2D ;