license import { env } from './environment' ; import { BackendValues , DataType , TensorLike , TypedArray } from './types' ; import * as base from './util_base' ; export * from './util_base' ; export * from './hash_util' ; /**
 * Create typed array for scalar value. Used for storing in `DataStorage`.
 */ export function createScalarValue ( value , dtype ) { if ( dtype === 'string' ) { return encodeString ( value ) ; } return toTypedArray ( [ value ] , dtype ) ; } function noConversionNeeded ( a , dtype ) { return ( a instanceof Float32Array && dtype === 'float32' ) || ( a instanceof Int32Array && dtype === 'int32' ) || ( a instanceof Uint8Array && dtype === 'bool' ) ; } export function toTypedArray ( a , dtype ) { if ( dtype === 'string' ) { throw new Error ( 'Cannot convert a string[] to a TypedArray' ) ; } if ( Array . isArray ( a ) ) { a = base . flatten ( a ) ; } if ( env ( ) . getBool ( 'DEBUG' ) ) { base . checkConversionForErrors ( a as number [ ] , dtype ) ; } if ( noConversionNeeded ( a , dtype ) ) { return a as TypedArray ; } if ( dtype == null || dtype === 'float32' || dtype === 'complex64' ) { return new Float32Array ( a as number [ ] ) ; } else if ( dtype === 'int32' ) { return new Int32Array ( a as number [ ] ) ; } else if ( dtype === 'bool' ) { const bool = new Uint8Array ( ( a as number [ ] ) . length ) ; for ( let i = 0 ; i < bool . length ; ++ i ) { if ( Math . round ( ( a as number [ ] ) [ i ] ) !== 0 ) { bool [ i ] = 1 ; } } return bool ; } else { throw new Error ( `Unknown data type ${ dtype }` ) ; } } doc export function now ( ) { return env ( ) . platform . now ( ) ; } doc export function fetch ( path , requestInits ? ) { return env ( ) . platform . fetch ( path , requestInits ) ; } param s param encoding doc export function encodeString ( s , encoding = 'utf-8' ) { encoding = encoding || 'utf-8' ; return env ( ) . platform . encode ( s , encoding ) ; } param bytes param encoding doc export function decodeString ( bytes , encoding = 'utf-8' ) { encoding = encoding || 'utf-8' ; return env ( ) . platform . decode ( bytes , encoding ) ; }