import { Codec } from "../interfaces/Codec" ; import { DecodedData } from "../interfaces/DecodedData" ; import { UnwrappableCodec } from "../interfaces/UnwrappableCodec" ; import { ArrayUtils } from "../utils/Arrays" ; import { Bytes } from "../utils/Bytes" ; import { BytesBuffer } from "../utils/BytesBuffer" ; export abstract class AbstractArray < ScaleType extends Codec , NativeType > implements UnwrappableCodec < Array < NativeType > > { public values : Array < NativeType > ; constructor ( input = [ ] ) { this . values = new Array < NativeType > ( input . length ) ; Bytes . copy < NativeType > ( input , this . values ) ; } description public unwrap ( ) { return this . values ; } public eq ( other ) { return ArrayUtils . areArraysEqual ( this . values , other . values ) ; } public notEq ( other ) { return ! ArrayUtils . areArraysEqual ( this . values , other . values ) ; } description public toU8a ( ) { const bytesBuffer = new BytesBuffer ( ) ; bytesBuffer . encodeCompactInt ( this . values . length ) ; for ( let i = 0 ; i < this . values . length ; i ++ ) { const element = instantiate < ScaleType > ( this . values [ i ] ) ; bytesBuffer . write ( element . toU8a ( ) ) ; } return bytesBuffer . bytes ; } description abstract encodedLength ( ) ; description param bytes param index abstract populateFromBytes ( bytes , index ) ; description public abstract decodeElement ( value ) ; description static fromU8a < TypeOfScaleArray > ( input ) { const data = Bytes . decodeCompactInt ( input ) ; let bytes = input . slice ( data . decBytes ) ; const scaleArray = instantiate < TypeOfScaleArray > ( [ ] ) ; for ( let i = 0 ; i < data . value ; i ++ ) { const element = scaleArray . decodeElement ( bytes ) ; scaleArray . values . push ( element . value ) ; bytes = bytes . slice ( element . decBytes ) ; } return scaleArray ; } toU8aPacked ( ) { return this . toU8a ( ) ; } encodedLengthPacked ( ) { return this . encodedLength ( ) ; } populateFromPackedBytes ( bytes , index = 0 ) { this . populateFromBytes ( bytes , index ) ; } }