pub struct BitpackVec { /* private fields */ }
Expand description
A densely-packed vector of integers with fixed bit-length
Implementations§
Source§impl BitpackVec
impl BitpackVec
Sourcepub fn new(width: usize) -> BitpackVec
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);
Sourcepub fn from_raw_vec(data: Vec<u64>, width: usize, len: usize) -> BitpackVec
pub fn from_raw_vec(data: Vec<u64>, width: usize, len: usize) -> BitpackVec
Construct a BitpackVec
from a vector of u64
s, 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);
Sourcepub fn into_raw_vec(self) -> Vec<u64>
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);
Sourcepub fn as_raw(&self) -> &[u64]
pub fn as_raw(&self) -> &[u64]
Reference to the internal vector, see into_raw_vec
Sourcepub fn from_slice(x: &[u64]) -> BitpackVec
pub fn from_slice(x: &[u64]) -> BitpackVec
Construct a BitpackVec
from a slice of u64
s. 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);
Sourcepub fn len(&self) -> usize
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);
Sourcepub fn width(&self) -> usize
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);
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of items that can be inserted into the
BitpackVec
before the vector will grow.
Sourcepub fn fits(&self, x: u64) -> bool
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));
Sourcepub fn push(&mut self, x: u64)
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
Sourcepub fn set(&mut self, idx: usize, x: u64)
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);
Sourcepub fn at(&self, idx: usize) -> u64
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);
Sourcepub fn is_empty(&self) -> bool
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());
Sourcepub fn pop(&mut self) -> Option<u64>
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);
Sourcepub fn truncate(&mut self, len: usize)
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);
Sourcepub fn split_off(&mut self, idx: usize) -> BitpackVec
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]);
Sourcepub fn to_vec(&self) -> Vec<u64>
pub fn to_vec(&self) -> Vec<u64>
Copies the bitpacked vector into a standard, non-packed vector of
u64
s.
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]);
Sourcepub fn change_width(&mut self, new_width: usize)
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
Sourcepub fn iter(&self) -> BitpackIter<'_> ⓘ
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
impl Debug for BitpackVec
Source§impl DeepSizeOf for BitpackVec
impl DeepSizeOf for BitpackVec
Source§fn deep_size_of_children(&self, context: &mut Context) -> usize
fn deep_size_of_children(&self, context: &mut Context) -> usize
Source§fn deep_size_of(&self) -> usize
fn deep_size_of(&self) -> usize
Source§impl<'de> Deserialize<'de> for BitpackVec
impl<'de> Deserialize<'de> for BitpackVec
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Extend<u64> for BitpackVec
impl Extend<u64> for BitpackVec
Source§fn extend<T: IntoIterator<Item = u64>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = u64>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)