PackedIntegers

Struct PackedIntegers 

Source
pub struct PackedIntegers { /* private fields */ }
Expand description

An array of integers packed together in blocks using a variable number of bits for each integer, along with an index to allow fast random access.

Implementations§

Source§

impl PackedIntegers

Source

pub fn len(&self) -> usize

The number of integers packed into the array.

Source

pub fn from_vec(data: Vec<u64>) -> Self

Build a packed array of integers from a vector.

This re-uses the space the vector is allocated in while building the packed array, though it may involve a re-allocation of the space to reduce the size at the end (it should never increase this allocation, though the additional size of the separate index may mean more space is used overall in some cases).

use ::packed_integer_array::PackedIntegers;
let input_data: Vec<u64> = vec![5, 138, 10, 90, 242, 312, 541, 48];
let packed = PackedIntegers::from_vec(input_data.clone());
assert_eq!(input_data.len(), packed.len());
for i in 0..input_data.len() {
    assert_eq!(Some(input_data[i]), packed.get(i));
}
Source

pub fn from_iter<I, T>(iter: I) -> Self
where T: Into<u64>, I: IntoIterator<Item = T>,

Build a packed array of integers from an iterator.

use ::packed_integer_array::PackedIntegers;
let input_data: Vec<u64> = vec![5, 138, 10, 90, 242, 312, 541, 48];
let packed = PackedIntegers::from_iter(input_data.iter().cloned());
assert_eq!(input_data.len(), packed.len());
for i in 0..input_data.len() {
    assert_eq!(Some(input_data[i]), packed.get(i));
}
Source

pub fn get(&self, idx: usize) -> Option<u64>

Get the integer at a specific index.

This operation should be fast, and does not involve unpacking many others of the packed integers or using a lot of memory for the unpacking.

Returns None for out-of-bounds.

use rand::prelude::*;
use ::packed_integer_array::PackedIntegers;
let mut rng = rand::thread_rng();
let len = rng.gen_range(137, 549);
let unpacked: Vec<u64> = (0..len).map(|_| rng.gen()).collect();
let packed = PackedIntegers::from_iter(unpacked.iter().cloned());
for i in 0..len {
    assert_eq!(Some(unpacked[i]), packed.get(i));
}
assert_eq!(None, packed.get(len));
assert_eq!(None, packed.get(len + 5));
Source§

impl PackedIntegers

Source

pub fn iter<'a>(&'a self) -> Iter<'a>

Iterate through the packed integers without consuming self.

use rand::prelude::*;
use ::packed_integer_array::PackedIntegers;
let mut rng = rand::thread_rng();
let len = rng.gen_range(0, 10000);
let data: Vec<u64> = (0..len).map(|_| rng.gen()).collect();
let packed = PackedIntegers::from_iter(data.iter().cloned());
let unpacked: Vec<_> = packed.iter().collect();
assert_eq!(data, unpacked);

Trait Implementations§

Source§

impl Clone for PackedIntegers

Source§

fn clone(&self) -> PackedIntegers

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PackedIntegers

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'a> IntoIterator for &'a PackedIntegers

Source§

type IntoIter = Iter<'a>

Which kind of iterator are we turning this into?
Source§

type Item = u64

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Iter<'a>

Creates an iterator from a value. Read more
Source§

impl IntoIterator for PackedIntegers

Source§

type IntoIter = IntoIter

Which kind of iterator are we turning this into?
Source§

type Item = u64

The type of the elements being iterated over.
Source§

fn into_iter(self) -> IntoIter

Creates an iterator from a value. Read more
Source§

impl Ord for PackedIntegers

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for PackedIntegers

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for PackedIntegers

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for PackedIntegers

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.