How to resolve the algorithm Variable-length quantity step by step in the Julia programming language
How to resolve the algorithm Variable-length quantity step by step in the Julia programming language
Table of Contents
Problem Statement
Implement some operations on variable-length quantities, at least including conversions from a normal number in the language to the binary representation of the variable-length quantity for that number, and vice versa. Any variants are acceptable.
With above operations,
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Variable-length quantity step by step in the Julia programming language
The provided Julia code defines a custom type VLQ
(Variable-Length Quantization) and overloads the UInt64
constructor to convert VLQ values to UInt64
integers. Variable-Length Quantization is a technique used to represent integers using a variable number of bytes, where the most significant bit of each byte indicates whether there are more bytes to follow.
Here's a detailed explanation of the code:
-
mutable struct VLQ
: This line defines a mutable struct namedVLQ
that has a single fieldquant
, which is a vector ofUInt8
(unsigned 8-bit integers). Themutable
keyword allows the struct's fields to be modified after it has been created. -
function VLQ(n::T)
: This function takes an integern
of typeT
, whereT
is any integer type (Integer
,Int
, etc.). It convertsn
into a VLQ representation by computing its digits using base 128. The digits are stored in thequant
field of the VLQ struct. -
@inbounds for i in 2:length(quant)
: This loop iterates over the elements of thequant
vector, starting from the second element (index 2) and ending at the last element. It sets the most significant bit (bit 7) of each element to 1, indicating that there are more bytes to follow, except for the last element. -
reverse(quant)
: This line reverses the order of the elements in thequant
vector. The VLQ representation is typically encoded in reverse order, so this step ensures that the resulting VLQ is in the correct format. -
import Base.UInt64
: This line imports theUInt64
type from the Base module. -
function Base.UInt64(vlq::VLQ)
: This function overloads theUInt64
constructor to convert aVLQ
value to aUInt64
integer. -
reverse(vlq.quant)
: Similar to the previous code, it reverses the order of the elements in thequant
vector. -
n = shift!(quant)
: This line extracts the first element of thequant
vector as aUInt64
and stores it inn
. Theshift!
function removes the first element from the vector, effectively "shifting" the remaining elements to the left. -
p = one(UInt64)
: This initializesp
to the value 1 as aUInt64
. -
for i in quant
: This loop iterates over the remaining elements of thequant
vector, from the second element to the last. -
p *= 0x80
: In each iteration, it multipliesp
by 0x80 (128) to shift the current value to the left by 7 bits. -
n += p * ( i & 0x7f)
: It adds ton
the product ofp
and the current elementi
, bitwise-ANDed with 0x7f (127). This effectively adds the next 7 bits of the VLQ representation ton
. -
return n
: This line returns the finalUInt64
integer representing the converted VLQ value. -
const test
: This defines a constant namedtest
as a vector ofUInt64
values. These values are used as test cases for the VLQ conversion. -
for i in test
: This loop iterates over the elements of thetest
vector. -
vlq = VLQ(i)
: For each elementi
, it converts it into a VLQ representation using theVLQ
constructor. -
j = UInt(vlq)
: It converts the VLQ representationvlq
back to aUInt64
integer using the overloadedUInt64
constructor. -
@printf "0x%-8x => [%-25s] => 0x%x\n"
: This line formats and prints the originalUInt64
value, the VLQ representation as a string of hex values, and the convertedUInt64
value. The formatting ensures that the output is aligned and readable.
Overall, this code demonstrates how to represent integers using VLQ, convert VLQ representations back to UInt64
integers, and test the conversion process using a set of predefined values.
Source code in the julia programming language
using Printf
mutable struct VLQ
quant::Vector{UInt8}
end
function VLQ(n::T) where T <: Integer
quant = UInt8.(digits(n, 128))
@inbounds for i in 2:length(quant) quant[i] |= 0x80 end
VLQ(reverse(quant))
end
import Base.UInt64
function Base.UInt64(vlq::VLQ)
quant = reverse(vlq.quant)
n = shift!(quant)
p = one(UInt64)
for i in quant
p *= 0x80
n += p * ( i & 0x7f)
end
return n
end
const test = [0x00200000, 0x001fffff, 0x00000000, 0x0000007f,
0x00000080, 0x00002000, 0x00003fff, 0x00004000,
0x08000000, 0x0fffffff]
for i in test
vlq = VLQ(i)
j = UInt(vlq)
@printf "0x%-8x => [%-25s] => 0x%x\n" i join(("0x" * hex(r, 2) for r in vlq.quant), ", ") j
end
You may also check:How to resolve the algorithm Introspection step by step in the Python programming language
You may also check:How to resolve the algorithm Combinations step by step in the K programming language
You may also check:How to resolve the algorithm Strip comments from a string step by step in the Scheme programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the Efene programming language
You may also check:How to resolve the algorithm Munchausen numbers step by step in the Ada programming language