import { CompactInt } from "." ; import { BytesReader } from "./BytesReader" ; import { Codec } from "./interfaces/Codec" ; import { UnwrappableCodec } from "./interfaces/UnwrappableCodec" ; description export class ScaleMap < K extends Codec , V extends Codec > implements UnwrappableCodec < Map < K , V > > { /**
     * Map value of ScaleMap
     */ public readonly data : Map < K , V > ; constructor ( data = new Map ( ) ) { this . data = data ; } description unwrap ( ) { return this . data ; } param key has ( key ) { return this . data . has ( key ) ; } param key get ( key ) { return this . data . get ( key ) ; } param key param value set ( key , value ) { this . data . set ( key , value ) ; } param key delete ( key ) { this . data . delete ( key ) ; } /**
     * Get array of keys of the ScaleMap
     */ keys ( ) { return this . data . keys ( ) ; } /**
     * Get array of values of the ScaleMap
     */ values ( ) { return this . data . values ( ) ; } /**
     * The number of bytes this Map has
     */ encodedLength ( ) { return this . toU8a ( ) . length ; } /**
     * Convert ScaleMap to u8[]
     * Length is encoded first, followed by all key and value encodings concatenated
     */ toU8a ( ) { let result = [ ] ; let keys = this . data . keys ( ) ; let lenData = new CompactInt ( keys . length ) ; result = result . concat ( lenData . toU8a ( ) ) ; for ( let i = 0 ; i < keys . length ; i ++ ) { result = result . concat ( keys [ i ] . toU8a ( ) ) . concat ( this . data . get ( keys [ i ] ) . toU8a ( ) ) ; } return result ; } description param bytes param index populateFromBytes ( bytes , index = 0 ) { const bytesReader = new BytesReader ( bytes . slice ( index ) ) ; const lenComp = bytesReader . readInto < CompactInt > ( ) ; for ( let i = 0 ; i < lenComp . unwrap ( ) ; i ++ ) { const key = bytesReader . readInto < K > ( ) ; const value = bytesReader . readInto < V > ( ) ; this . data . set ( key , value ) ; } } description param a param b eq ( other ) { let areEqual = true ; const aKeys = this . data . keys ( ) ; const bKeys = other . data . keys ( ) ; if ( aKeys . length != bKeys . length ) { return false ; } for ( let i = 0 ; i < aKeys . length ; i ++ ) { if ( aKeys [ i ] != bKeys [ i ] ) { areEqual = false ; break ; } } return areEqual ; } description param a param b notEq ( other ) { return ! this . eq ( other ) ; } static fromU8a < K extends Codec , V extends Codec > ( input , index = 0 ) { const data = new Map < K , V > ( ) ; const bytesReader = new BytesReader ( input ) ; const lenComp = bytesReader . readInto < CompactInt > ( ) ; for ( let i = 0 ; i < lenComp . unwrap ( ) ; i ++ ) { const key = bytesReader . readInto < K > ( ) ; const value = bytesReader . readInto < V > ( ) ; data . set ( key , value ) ; } return new ScaleMap < K , V > ( data ) ; } toU8aPacked ( ) { return this . toU8a ( ) ; } encodedLengthPacked ( ) { return this . encodedLength ( ) ; } populateFromPackedBytes ( bytes , index = 0 ) { this . populateFromBytes ( bytes , index ) ; } }