Struct IntVecBuilder

Source
pub struct IntVecBuilder<'a, E: Endianness> { /* private fields */ }
Expand description

A builder for creating an IntVec from a slice (&[u64]).

This builder is obtained by calling IntVec::builder. It provides a fluent interface for setting parameters like the sampling rate k and the compression CodecSpec.

Because it operates on a slice, it can automatically select the best codec parameters by analyzing the data first. This is the recommended way to construct an IntVec when all data is available in memory.

Implementations§

Source§

impl<'a, E: Endianness> IntVecBuilder<'a, E>

Source

pub fn k(self, k: usize) -> Self

Sets the sampling rate k for DSI-based codecs.

The sampling rate determines how frequently a sample of the bitstream’s position is stored. A smaller k leads to faster random access but increases memory overhead. A larger k reduces memory usage but slows down access.

The value must be greater than 0. This parameter is ignored for FixedLength encoding, as random access is already $O(1)$.

§Arguments
  • k: The sampling rate.
Source

pub fn codec(self, codec_spec: CodecSpec) -> Self

Sets the codec specification for compression.

This determines which compression algorithm will be used. See CodecSpec for available options. It can be a specific codec (e.g., Gamma) or a request for automatic selection (Auto).

§Arguments
  • codec_spec: The desired codec specification.
§Example
use compressed_intvec::prelude::*;

let data: &[u64] = &[1, 2, 3, 4, 5];
let intvec = LEIntVec::builder(data)
    .codec(CodecSpec::Gamma)
    .build()
    .unwrap();

assert_eq!(intvec.encoding(), Encoding::Dsi(dsi_bitstream::prelude::Codes::Gamma));
Source

pub fn build(self) -> Result<IntVec<E>, IntVecError>

Builds the IntVec, consuming the builder.

This method performs the main compression logic. It resolves the CodecSpec, automatically selecting parameters if requested (e.g., for CodecSpec::Auto or CodecSpec::Rice { log2_b: None }). It then encodes the input data and builds the final IntVec structure.

§Returns

A Result containing the constructed IntVec on success, or an IntVecError if there’s a problem, such as:

  • k=0 for a DSI-based codec.
  • A value in the input data does not fit within the specified number of bits for FixedLength encoding.
§Examples
§Automatic Codec Selection

Using CodecSpec::Auto (the default) allows the builder to choose an optimal codec based on a sample of the data.

use compressed_intvec::prelude::*;

let data: Vec<u64> = (0..100).map(|x| x * x).collect();

// Build with default settings (auto codec, k=32).
let intvec = LEIntVec::builder(&data).build().unwrap();

assert_eq!(intvec.len(), 100);
assert_eq!(intvec.get(10), Some(100)); // 10*10
§Specifying a Codec

You can explicitly set the codec and sampling rate.

use compressed_intvec::prelude::*;

let data: &[u64] = &[1, 2, 3, 4, 5];

// Use Gamma coding with a small sampling rate.
let intvec = LEIntVec::builder(data)
    .codec(CodecSpec::Gamma)
    .k(2)
    .build()
    .unwrap();

assert_eq!(intvec.get(4), Some(5));
assert_eq!(intvec.get_sampling_rate(), Some(2));
§Using Fixed-Length Encoding

For data that is bounded, FixedLength can be very efficient.

use compressed_intvec::prelude::*;

// All values are less than 256, so they fit in 8 bits.
let data: &[u64] = &[100, 200, 150, 255, 0];

let intvec = LEIntVec::builder(data)
    .codec(CodecSpec::FixedLength { num_bits: Some(8) })
    .build()
    .unwrap();

// For FixedLength, sampling rate is not applicable.
assert_eq!(intvec.get_sampling_rate(), None);
assert_eq!(intvec.get(1), Some(200));

// Building fails if a value does not fit.
let data_too_large: &[u64] = &[100, 200, 300]; // 300 does not fit in 8 bits
let result = LEIntVec::builder(data_too_large)
    .codec(CodecSpec::FixedLength { num_bits: Some(8) })
    .build();
assert!(matches!(result, Err(IntVecError::InvalidParameters(_))));
§Implementation Notes

After writing the compressed bitstream, this method calls shrink_to_fit on the underlying Vec<u64> used for storage. While this may incur a one-time cost of reallocation and copying, it ensures that the final IntVec occupies the minimum necessary memory, which is critical for a data structure focused on compression.

Trait Implementations§

Source§

impl<'a, E: Debug + Endianness> Debug for IntVecBuilder<'a, E>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, E> Freeze for IntVecBuilder<'a, E>

§

impl<'a, E> RefUnwindSafe for IntVecBuilder<'a, E>
where E: RefUnwindSafe,

§

impl<'a, E> Send for IntVecBuilder<'a, E>

§

impl<'a, E> Sync for IntVecBuilder<'a, E>

§

impl<'a, E> Unpin for IntVecBuilder<'a, E>
where E: Unpin,

§

impl<'a, E> UnwindSafe for IntVecBuilder<'a, E>
where E: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CastableFrom<T> for T

Source§

fn cast_from(value: T) -> T

Call Self as W
Source§

impl<T, U> CastableInto<U> for T
where U: CastableFrom<T>,

Source§

fn cast(self) -> U

Call W::cast_from(self)
Source§

impl<T> DowncastableFrom<T> for T

Source§

fn downcast_from(value: T) -> T

Truncate the current UnsignedInt to a possibly smaller size
Source§

impl<T, U> DowncastableInto<U> for T
where U: DowncastableFrom<T>,

Source§

fn downcast(self) -> U

Call W::downcast_from(self)
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Splat<T> for T

Source§

fn splat(value: T) -> T

Source§

impl<T> To<T> for T

Source§

fn to(self) -> T

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UpcastableFrom<T> for T

Source§

fn upcast_from(value: T) -> T

Extend the current UnsignedInt to a possibly bigger size.
Source§

impl<T, U> UpcastableInto<U> for T
where U: UpcastableFrom<T>,

Source§

fn upcast(self) -> U

Call W::upcast_from(self)
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V