import System from './System' ; import IllegalArgumentException from '../IllegalArgumentException' ; import ArrayIndexOutOfBoundsException from '../ArrayIndexOutOfBoundsException' ; import { int } from '../../customTypings' ; export default class Arrays { param a param val public static fill ( a , val ) { for ( let i = 0 , len = a . length ; i < len ; i ++ ) a [ i ] = val ; } param a param fromIndex param toIndex param val throws throws public static fillWithin ( a , fromIndex , toIndex , val ) { Arrays . rangeCheck ( a . length , fromIndex , toIndex ) ; for ( let i = fromIndex ; i < toIndex ; i ++ ) a [ i ] = val ; } /**
     * Checks that {@code fromIndex} and {@code toIndex} are in
     * the range and throws an exception if they aren't.
     */ static rangeCheck ( arrayLength , fromIndex , toIndex ) { if ( fromIndex > toIndex ) { throw new IllegalArgumentException ( 'fromIndex(' + fromIndex + ') > toIndex(' + toIndex + ')' ) ; } if ( fromIndex < 0 ) { throw new ArrayIndexOutOfBoundsException ( fromIndex ) ; } if ( toIndex > arrayLength ) { throw new ArrayIndexOutOfBoundsException ( toIndex ) ; } } public static asList < T = any > ( ... args ) { return args ; } public static create < T = any > ( rows , cols , value ? ) { let arr = Array . from ( { length : rows } ) ; return arr . map ( x => Array . from < T > ( { length : cols } ) . fill ( value ) ) ; } public static createInt32Array ( rows , cols , value ? ) { let arr = Array . from ( { length : rows } ) ; return arr . map ( x => Int32Array . from ( { length : cols } ) . fill ( value ) ) ; } public static equals ( first , second ) { if ( ! first ) { return false ; } if ( ! second ) { return false ; } if ( ! first . length ) { return false ; } if ( ! second . length ) { return false ; } if ( first . length !== second . length ) { return false ; } for ( let i = 0 , length = first . length ; i < length ; i ++ ) { if ( first [ i ] !== second [ i ] ) { return false ; } } return true ; } public static hashCode ( a ) { if ( a === null ) { return 0 ; } let result = 1 ; for ( const element of a ) { result = 31 * result + element ; } return result ; } public static fillUint8Array ( a , value ) { for ( let i = 0 ; i !== a . length ; i ++ ) { a [ i ] = value ; } } public static copyOf ( original , newLength ) { return original . slice ( 0 , newLength ) ; } public static copyOfUint8Array ( original , newLength ) { if ( original . length <= newLength ) { const newArray = new Uint8Array ( newLength ) ; newArray . set ( original ) ; return newArray ; } return original . slice ( 0 , newLength ) ; } public static copyOfRange ( original , from , to ) { const newLength = to - from ; const copy = new Int32Array ( newLength ) ; System . arraycopy ( original , from , copy , 0 , newLength ) ; return copy ; } public static binarySearch ( ar , el , comparator ? ) { if ( undefined === comparator ) { comparator = Arrays . numberComparator ; } let m = 0 ; let n = ar . length - 1 ; while ( m <= n ) { const k = ( n + m ) >> 1 ; const cmp = comparator ( el , ar [ k ] ) ; if ( cmp > 0 ) { m = k + 1 ; } else if ( cmp < 0 ) { n = k - 1 ; } else { return k ; } } return - m - 1 ; } public static numberComparator ( a , b ) { return a - b ; } }