import { UnwrappableCodec } from "../interfaces/UnwrappableCodec" ; import { BIT_LENGTH , Bytes } from "../utils/Bytes" ; import { BytesBuffer } from "../utils/BytesBuffer" ; description export class CompactInt implements UnwrappableCodec < i64 > { private _value : i64 ; protected bitLength : i32 ; constructor ( value = 0 ) { this . _value = value ; this . bitLength = CompactInt . _computeBitLength ( value ) ; } description unwrap ( ) { return this . _value ; } description public toU8a ( ) { const bytesBuffer = new BytesBuffer ( ) ; bytesBuffer . encodeCompactInt ( this . _value ) ; return bytesBuffer . bytes ; } description param bytes param index public populateFromBytes ( bytes , index = 0 ) { assert ( bytes . length - index > 0 , "CompactInt: Empty bytes array provided" ) ; const decodedData = Bytes . decodeCompactInt ( bytes , index ) ; this . _value = decodedData . value ; this . bitLength = CompactInt . _computeBitLength ( decodedData . value ) ; } description toString ( ) { return this . _value . toString ( ) ; } param value static _computeBitLength ( value ) { if ( value < 1 << 6 ) return BIT_LENGTH . INT_8 ; else if ( value < 1 << 14 ) return BIT_LENGTH . INT_16 ; else if ( value < 1 << 30 ) return BIT_LENGTH . INT_32 ; else { return BIT_LENGTH . INT_64 ; } } description public encodedLength ( ) { return this . bitLength ; } eq ( other ) { return this . _value == other . unwrap ( ) ; } notEq ( other ) { return this . _value != other . unwrap ( ) ; } description param input param index static fromU8a ( value , index = 0 ) { assert ( value . length - index > 0 , "CompactInt: Empty bytes array provided" ) ; const decodedData = Bytes . decodeCompactInt ( value , index ) ; return new CompactInt ( decodedData . value ) ; } @ inline @ operator ( "==" ) static eq ( a , b ) { return a . eq ( b ) ; } @ inline @ operator ( "!=" ) static notEq ( a , b ) { return a . notEq ( b ) ; } toU8aPacked ( ) { return this . toU8a ( ) ; } encodedLengthPacked ( ) { return this . encodedLength ( ) ; } populateFromPackedBytes ( bytes , index = 0 ) { this . populateFromBytes ( bytes , index ) ; } }