Struct packedvec::PackedVec[][src]

pub struct PackedVec<T, StorageT = usize> { /* fields omitted */ }
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

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

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

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));

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);

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);

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);

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);

Returns an iterator over the PackedVec.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Feeds this value into the given Hasher. Read more

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

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.