Struct deepmesa_encoding::prefix::varbyte::VarByteEncoder [−][src]
pub struct VarByteEncoder { /* fields omitted */ }
Expand description
Encodes unsigned values using variable byte encoding.
Each value is encoded in 8 byte chunks. The MSB of each byte is used to indicate whether there are more bytes to read for this element. The MSB is set to 0 if there is are additional bytes in the element and is set to 1 to indicate that this is the last byte in the element.
Examples:
decimal 255 = 1111_1111
encoding for decimal 255 = 0000_0001_0111_1111;
decimal 2 = 0000_0010
encoding for decimal 2 = 1000_0010;
The variable byte encoder, encodes the data in a
BitVector
. The .encode()
accepts a
u128
value and pushes encoded bits to the BitVector
.
The encoder maintains a count of number of values encoded
(.elements()
), the number of bits used
to encode those elements
(.encoded_len()
) and the compression
ratio (.comp_ratio()
). The compression
ratio is the average number of bits per value needed to encode
all the values.
Examples
use deepmesa::encoding::VarByteEncoder; let mut vbe = VarByteEncoder::new(); // encode 255 vbe.encode(255); assert_eq!(vbe.elements(), 1); assert_eq!(vbe.encoded_len(), 16); assert_eq!(vbe.comp_ratio(), 16.0); // Get the underlying BitVector. let encoded = vbe.encoded(); assert_eq!(encoded.read_u16(0), (0b0000_0001_1111_1111, 16)); // encode 2 vbe.encode(2); assert_eq!(vbe.elements(), 2); assert_eq!(vbe.encoded_len(), 24); assert_eq!(vbe.comp_ratio(), 12.0); let encoded = vbe.encoded(); assert_eq!(encoded.read_u8(16), (0b1000_0010, 8));
Encoded bits can be decoded using the VarByteDecoder
.
Implementations
Creates a new VarByteEncoder
with the underlying BitVector
initialized with
a capacity of 1024 bits. .
Creates a new VarByteEncoder
with the underlying BitVector
initialized with
the specified capacity_bits
;
Returns an immutable reference to the underlying
BitVector
containing the encoded bits.
Returns the length in bits of the underlying
BitVector
containing the encoded bits.
Returns the number of elements encoded in the
underlying BitVector
.
Returns the compression ratio of the encoded
elements. This is simply the
.encoded_len()
divided by
the number of .elements()
;
Auto Trait Implementations
impl RefUnwindSafe for VarByteEncoder
impl Send for VarByteEncoder
impl Sync for VarByteEncoder
impl Unpin for VarByteEncoder
impl UnwindSafe for VarByteEncoder