import { CommandInterface } from '../Commands' ; import { ScalarStruct } from './ScalarStruct' ; import * as Constants from '../Constants' ; import { Variables } from './Variables' ; import { Variable } from './Variable' ; import { Matrix } from './Matrix' ; export class Struct extends Matrix { private static STRUCT_TYPENAME : string = 'struct' ; private _fields : Array < string > ; param name param value param freeIndices param fixedIndices constructor ( name = '' , value = '' , freeIndices = [ ] , fixedIndices = [ ] ) { super ( name , value , freeIndices , fixedIndices , false , Struct . STRUCT_TYPENAME ) ; this . _fields = value . split ( Constants . FIELDS_SEPARATOR ) ; } private static remove ( prefix , fields ) { const N = prefix . length ; return fields . map ( ( v ) => v . substr ( N ) ) ; } public typename ( ) { return Struct . STRUCT_TYPENAME ; } public loads ( type ) { return type === this . typename ( ) ; } public createConcreteType ( name , value , freeIndices , fixedIndices , validValue , type ) { return new Struct ( name , value , freeIndices , fixedIndices ) ; } public loadNew ( name , type , runtime , callback ) { ScalarStruct . getFields ( name , runtime , ( fields ) => { fields = Struct . remove ( name , fields ) ; const value = fields . join ( Constants . FIELDS_SEPARATOR ) ; Variables . getSize ( name , runtime , ( size ) => { const struct = this . createConcreteType ( name , value , size , [ ] , false , type ) ; callback ( struct ) ; } ) ; } ) ; } public createChildType ( value , freeIndices , fixedIndices , validValue ) { let struct ; if ( freeIndices . length === 0 ) { const name = this . makeName ( this . basename ( ) , freeIndices , fixedIndices ) ; const fields = this . _fields . map ( ( v ) => name + v ) ; struct = new ScalarStruct ( name , fields ) ; } else { struct = this . createConcreteType ( this . basename ( ) , value , freeIndices , fixedIndices , validValue , Struct . STRUCT_TYPENAME ) ; } return struct ; } public loadChildrenRange ( runtime , offset , count , callback ) { const vars = new Array < Variable > ( count ) ; const freeIndices = this . freeIndices ( ) ; const fixedIndices = this . fixedIndices ( ) ; const childrenFreeIndices = freeIndices . slice ( 0 , freeIndices . length - 1 ) ; const value = this . _fields . join ( Constants . FIELDS_SEPARATOR ) ; for ( let i = 0 ; i !== count ; ++ i ) { const childFixedIndices = [ i + offset + 1 ] . concat ( fixedIndices ) ; vars [ i ] = this . createChildType ( value , childrenFreeIndices , childFixedIndices , false ) ; } callback ( vars ) ; } }