PackedVec

Struct PackedVec 

Source
pub struct PackedVec<T, StorageT = usize> { /* private fields */ }
Expand description

A PackedVec stores vectors of integers efficiently while providing an API similar to Vec. The basic idea is to store each element using the minimum number of bits needed to represent every element in the Vec. For example, if we have a Vec<u64> with elements [20, 30, 140], every element wastes most of its 64 bits: 7 bits is sufficient to represent the range of elements in the vector. Given this input vector, PackedVec stores each elements using exactly 7 bits, saving substantial memory. For vectors which often contain small ranges of numbers, and which are created rarely, but read from frequently, this can be a significant memory and performance win.

§Examples

PackedVec has two main API differences from Vec: a PackedVec is created from a Vec; and a PackedVec returns values rather than references. Both points can be seen in this example:

use packedvec::PackedVec;
let v = vec![-1, 30, 120];
let pv = PackedVec::new(v.clone());
assert_eq!(pv.get(0), Some(-1));
assert_eq!(pv.get(2), Some(120));
assert_eq!(pv.get(3), None);
assert_eq!(v.iter().cloned().collect::<Vec<_>>(), pv.iter().collect::<Vec<_>>());

§Storage backing type

PackedVec defaults to using usize as a storage backing type. You can choose your own storage type with the new_with_storaget() constructor. In general we recommend using the default usize backing storage unless you have rigorously benchmarked your particular use case and are sure that a different storage type is superior.

Implementations§

Source§

impl<T> PackedVec<T, usize>

Source

pub fn new(vec: Vec<T>) -> Self

Constructs a new PackedVec from vec. The PackedVec has usize backing storage, which is likely to be the best choice in nearly all situations.

Source§

impl<'a, T, StorageT> PackedVec<T, StorageT>
where T: 'static + AsPrimitive<StorageT> + FromPrimitive + Ord + PrimInt + ToPrimitive, StorageT: AsPrimitive<T> + PrimInt + Unsigned,

Source

pub fn new_with_storaget(vec: Vec<T>) -> PackedVec<T, StorageT>

Constructs a new PackedVec from vec (with a user-defined backing storage type).

Source

pub fn get(&self, index: usize) -> Option<T>

Return the value at the specified index.

§Example
use packedvec::PackedVec;
let v: Vec<u8> = vec![1, 2, 3, 4];
let packedvec = PackedVec::new(v);
let val: Option<u8> = packedvec.get(3);
assert_eq!(val, Some(4));
Source

pub unsafe fn get_unchecked(&self, index: usize) -> T

Returns a reference to an element or subslice, without doing bounds checking.

This is generally not recommended, use with caution! For a safe alternative see get().

§Safety

The caller must ensure that the index specified does not exceed this PackedVec’s bound.

§Example
use packedvec::PackedVec;
let v: Vec<u8> = vec![1, 2, 3, 4];
let packedvec = PackedVec::new(v);
assert_eq!(unsafe { packedvec.get_unchecked(3) }, 4);
Source

pub fn is_empty(&self) -> bool

Returns true if the PackedVec has no elements.

§Example
use packedvec::PackedVec;
let v: Vec<u16> = vec![];
let packedvec = PackedVec::new(v);
assert_eq!(packedvec.is_empty(), true);
let v: Vec<u16> = vec![1, 2, 3, 4];
let packedvec = PackedVec::new(v);
assert_eq!(packedvec.is_empty(), false);
Source

pub fn len(&self) -> usize

Return the number of elements in this PackedVec.

§Example
use packedvec::PackedVec;
let v: Vec<u16> = vec![1, 2, 3, 4];
let packedvec = PackedVec::new(v);
assert_eq!(packedvec.len(), 4);
Source

pub fn bwidth(&self) -> usize

How many bits does each packed element consume?

§Example
use packedvec::PackedVec;
let packedvec = PackedVec::new(vec![1, 2, 3, 4]);
assert_eq!(packedvec.bwidth(), 2);
Source

pub fn iter(&'a self) -> PackedVecIter<'a, T, StorageT>

Returns an iterator over the PackedVec.

Trait Implementations§

Source§

impl<T: Clone, StorageT: Clone> Clone for PackedVec<T, StorageT>

Source§

fn clone(&self) -> PackedVec<T, StorageT>

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<T: Debug, StorageT: Debug> Debug for PackedVec<T, StorageT>

Source§

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

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

impl<T: Debug + Hash> Hash for PackedVec<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: PartialEq> PartialEq for PackedVec<T>

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<T: Debug + PartialEq> Eq for PackedVec<T>

Auto Trait Implementations§

§

impl<T, StorageT> Freeze for PackedVec<T, StorageT>
where T: Freeze,

§

impl<T, StorageT> RefUnwindSafe for PackedVec<T, StorageT>
where T: RefUnwindSafe, StorageT: RefUnwindSafe,

§

impl<T, StorageT> Send for PackedVec<T, StorageT>
where T: Send, StorageT: Send,

§

impl<T, StorageT> Sync for PackedVec<T, StorageT>
where T: Sync, StorageT: Sync,

§

impl<T, StorageT> Unpin for PackedVec<T, StorageT>
where T: Unpin, StorageT: Unpin,

§

impl<T, StorageT> UnwindSafe for PackedVec<T, StorageT>
where T: UnwindSafe, StorageT: 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> 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.