bitpack_vec

Struct BitpackVec

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

A densely-packed vector of integers with fixed bit-length

Implementations§

Source§

impl BitpackVec

Source

pub fn new(width: usize) -> BitpackVec

Constructs a new BitpackVec with the given bit-width.

use bitpack_vec::BitpackVec;
let bv = BitpackVec::new(5);
assert_eq!(bv.width(), 5);
Source

pub fn from_raw_vec(data: Vec<u64>, width: usize, len: usize) -> BitpackVec

Construct a BitpackVec from a vector of u64s, interpreting the vector with the given bitwidth.

use bitpack_vec::BitpackVec;

let v = vec![6];
let bv = BitpackVec::from_raw_vec(v, 5, 12);
assert_eq!(bv.at(0), 6);
assert_eq!(bv.at(1), 0);
Source

pub fn into_raw_vec(self) -> Vec<u64>

Returns the internal vector representing the bitpacked data

use bitpack_vec::BitpackVec;

let bv = BitpackVec::from_slice(&[10, 15]);
let v = bv.into_raw_vec();
assert_eq!(v.len(), 1);
assert_eq!(v[0], (15 << 4) | 10);
Source

pub fn as_raw(&self) -> &[u64]

Reference to the internal vector, see into_raw_vec

Source

pub fn from_slice(x: &[u64]) -> BitpackVec

Construct a BitpackVec from a slice of u64s. The smallest correct bitwidth will be computed.

use bitpack_vec::BitpackVec;
let bv = BitpackVec::from_slice(&[5, 12, 13]);
assert_eq!(bv.width(), 4);
assert_eq!(bv.at(2), 13);
Source

pub fn len(&self) -> usize

Returns the number of items in the bitpacked vector.

use bitpack_vec::BitpackVec;
let bv = BitpackVec::from_slice(&[5, 12, 13]);
assert_eq!(bv.len(), 3);
Source

pub fn width(&self) -> usize

Returns the packing width of the vector (the size in bits of each element).

use bitpack_vec::BitpackVec;
let bv = BitpackVec::from_slice(&[5, 12, 13]);
assert_eq!(bv.width(), 4);
Source

pub fn capacity(&self) -> usize

Returns the number of items that can be inserted into the BitpackVec before the vector will grow.

Source

pub fn fits(&self, x: u64) -> bool

Determines if x can fit inside this vector.

use bitpack_vec::BitpackVec;

let bv = BitpackVec::new(5);

assert!(bv.fits(31));
assert!(!bv.fits(32));
Source

pub fn push(&mut self, x: u64)

Appends an item to the back of the BitpackVec. Panics if the item is too large for the vector’s bitwidth.

use bitpack_vec::BitpackVec;
let mut bv = BitpackVec::new(5);

bv.push(4);
bv.push(22);

assert_eq!(bv.at(0), 4);
assert_eq!(bv.at(1), 22);

Adding items that are too large will cause a panic:

use bitpack_vec::BitpackVec;
let mut bv = BitpackVec::new(5);

bv.push(90);  // panics
Source

pub fn set(&mut self, idx: usize, x: u64)

Sets an existing element to a particular value. Panics if idx is out of range or if x does not fit within the bitwidth.

use bitpack_vec::BitpackVec;
let mut bv = BitpackVec::new(5);

bv.push(5);

assert_eq!(bv.at(0), 5);
bv.set(0, 9);
assert_eq!(bv.at(0), 9);
Source

pub fn at(&self, idx: usize) -> u64

Returns the value at the specified index. Panics if idx is out of range.

use bitpack_vec::BitpackVec;
let mut bv = BitpackVec::new(5);

bv.push(5);

assert_eq!(bv.at(0), 5);
Source

pub fn is_empty(&self) -> bool

Determines if the vector’s length is 0 (empty)

use bitpack_vec::BitpackVec;
let mut bv = BitpackVec::new(5);

assert!(bv.is_empty());
bv.push(5);
assert!(!bv.is_empty());
Source

pub fn pop(&mut self) -> Option<u64>

Removes and returns the last element in the vector. Returns None is the vector is empty.

use bitpack_vec::BitpackVec;
let mut bv = BitpackVec::new(5);

bv.push(5);
bv.push(6);

assert_eq!(bv.pop(), Some(6));
assert_eq!(bv.pop(), Some(5));
assert_eq!(bv.pop(), None);
Source

pub fn truncate(&mut self, len: usize)

Truncates the vector to the given length.

use bitpack_vec::BitpackVec;
let mut bv = BitpackVec::new(5);

bv.push(5);
bv.push(6);
bv.push(7);

assert_eq!(bv.len(), 3);
bv.truncate(1);
assert_eq!(bv.len(), 1);
assert_eq!(bv.at(0), 5);
Source

pub fn split_off(&mut self, idx: usize) -> BitpackVec

Split the vector into two parts, so that self will contain all elements from 0 to idx (exclusive), and the returned value will contain all elements from idx to the end of the vector.

use bitpack_vec::BitpackVec;
let mut bv = BitpackVec::new(5);

bv.push(5);
bv.push(6);
bv.push(7);
bv.push(8);

let bv_rest = bv.split_off(2);

assert_eq!(bv.to_vec(), &[5, 6]);
assert_eq!(bv_rest.to_vec(), &[7, 8]);
Source

pub fn to_vec(&self) -> Vec<u64>

Copies the bitpacked vector into a standard, non-packed vector of u64s.

use bitpack_vec::BitpackVec;
let mut bv = BitpackVec::new(5);

bv.push(5);
bv.push(6);
bv.push(7);
bv.push(8);

let v = bv.to_vec();

assert_eq!(v, vec![5, 6, 7, 8]);
Source

pub fn change_width(&mut self, new_width: usize)

Changes the width of the vector via copying (O(n)). Panics if any element in the current vector will not fit in new_width bits.

use bitpack_vec::BitpackVec;
let mut bv = BitpackVec::new(5);


assert!(!bv.fits(34)); // doesn't fit
bv.change_width(6);
assert!(bv.fits(34)); // fits now
Source

pub fn iter(&self) -> BitpackIter<'_>

Allows iteration over the values in the bit vector.

use bitpack_vec::BitpackVec;
let mut bv = BitpackVec::new(5);

bv.push(5);
bv.push(6);
bv.push(7);
bv.push(8);

let v: Vec<u64> = bv.iter().filter(|x| x % 2 == 0).collect();
assert_eq!(v, vec![6, 8]);

Trait Implementations§

Source§

impl Debug for BitpackVec

Source§

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

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

impl DeepSizeOf for BitpackVec

Source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Returns an estimation of the heap-managed storage of this object. This does not include the size of the object itself. Read more
Source§

fn deep_size_of(&self) -> usize

Returns an estimation of a total size of memory owned by the object, including heap-managed storage. Read more
Source§

impl<'de> Deserialize<'de> for BitpackVec

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Extend<u64> for BitpackVec

Source§

fn extend<T: IntoIterator<Item = u64>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl PartialEq for BitpackVec

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 Serialize for BitpackVec

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

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> 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, 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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,