pub struct BoolVec { /* private fields */ }Expand description
A vector that can store 8 booleans in a single byte
You can use the BoolVec::new(), BoolVec::default() and BoolVec::with_size()
functions to initialize a new BoolVec.
Or you can use the bool_vec::boolvec![] macro,
which works exactly like the vec![] macro.
§Capacity
Capacity is always a multiple of 8, check BoolVec.capacity() docs for more infos.
§Length
Length works like the length of a normal Vec.
§Formatting specifiers
§You can debug print and pretty print:
use bool_vec::boolvec;
let bv = boolvec![true; 16];
// Debug print the vector
println!("{bv:?}");
// Pretty-print the vector. Prints 8 values for each line
println!("{bv:#?}");§You can print the underlying bytes of the BoolVec if you need them
use bool_vec::boolvec;
let mut bv = boolvec![true; 9];
bv.set(2, false).unwrap();
assert_eq!(format!("{bv:b}"), "[11011111, 10000000]")
// You can also apply padding and pretty printing. See formatting specifiers.Implementations§
Source§impl BoolVec
impl BoolVec
Sourcepub fn new() -> Self
pub fn new() -> Self
Allocate empty BoolVec with capacity: 0 and len: 0
use bool_vec::{boolvec, BoolVec};
let bv = BoolVec::new();
assert_eq!(bv, boolvec![]);Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Allocate empty BoolVec with specified capacity and len: 0
use bool_vec::{boolvec, BoolVec};
let bv1 = BoolVec::with_capacity(3);
let mut bv2 = boolvec![true; 3];
bv2.pop();
bv2.pop();
bv2.pop();
assert_eq!(bv1.capacity(), 8);
assert_eq!(bv1.capacity(), bv2.capacity());To see why capacity in this case is 8 please do check BoolVec::capacity() documentation
Sourcepub fn from<S: AsRef<[bool]>>(slice: S) -> Self
pub fn from<S: AsRef<[bool]>>(slice: S) -> Self
Create BoolVec from a slice or vector of booleans
use bool_vec::{boolvec, BoolVec};
let bv = BoolVec::from([true, false, true]);
assert_eq!(bv, boolvec![true, false, true]);Sourcepub fn get(&self, int_index: usize) -> Option<bool>
pub fn get(&self, int_index: usize) -> Option<bool>
Get bool value from a BoolVec. Returns None if index overflows BoolVec.len()
use bool_vec::boolvec;
let bv = boolvec![true, false, true];
assert_eq!(bv.get(0), Some(true));
assert_eq!(bv.get(4), None);Sourcepub fn set(&mut self, int_index: usize, value: bool) -> Option<()>
pub fn set(&mut self, int_index: usize, value: bool) -> Option<()>
Set bool value in vector. Returns None if index overflows BoolVec.len()
use bool_vec::boolvec;
let mut bv = boolvec![true, false, true];
bv.set(0, false);
assert_eq!(bv.get(0), Some(false));
assert_eq!(bv.set(4, true), None);Sourcepub fn negate(&mut self, int_index: usize) -> Option<bool>
pub fn negate(&mut self, int_index: usize) -> Option<bool>
Alters BoolVec by negating the value at the specified index. Returns None if index overflows BoolVec.len(), otherwise returns Some(negated value)
use bool_vec::boolvec;
let mut bv = boolvec![true, false, true];
assert_eq!(bv.negate(0), Some(false));
assert_eq!(bv.get(0), Some(false));Sourcepub fn push(&mut self, value: bool)
pub fn push(&mut self, value: bool)
Appends a bool to the back of a BoolVec
use bool_vec::boolvec;
let mut bv = boolvec![true; 8];
assert_eq!(bv.capacity(), bv.bytes_capacity()*8);
assert_eq!(bv.len(), 8);
assert_eq!(bv.bytes_len(), 1);
bv.push(false);
assert_eq!(bv.capacity(), bv.bytes_capacity()*8);
assert_eq!(bv.len(), 9);
assert_eq!(bv.bytes_len(), 2);
assert_eq!(bv.get(8), Some(false));Sourcepub fn pop(&mut self) -> Option<bool>
pub fn pop(&mut self) -> Option<bool>
Removes the last element from a BoolVec and returns it, or None if it is empty
use bool_vec::boolvec;
let mut bv = boolvec![true; 8];
assert_eq!(bv.capacity(), 8);
assert_eq!(bv.len(), 8);
assert_eq!(bv.pop(), Some(true));
assert_eq!(bv.capacity(), 8);
assert_eq!(bv.len(), 7);use bool_vec::boolvec;
let mut bv1 = boolvec![true];
let bv2 = boolvec![];
bv1.pop();
assert_eq!(bv1, bv2);Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns vector capacity (how many booleans the BoolVec can hold without reallocating). Capacity will always be a power of 8 since a single boolean takes 1 bit of space and you can only allocate a minimum of 1 byte at a time.
For example 8 booleans here take a single byte of space.
use bool_vec::boolvec;
let bv = boolvec![true; 10];
assert_eq!(bv.capacity(), 16);
assert_eq!(bv.bytes_len(), 2);In the example 2 bytes are currently allocated, please read BoolVec::bytes_len() documentation for further details.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns BoolVec’s length (the number of booleans in the BoolVec)
use bool_vec::boolvec;
let bv = boolvec![true; 10];
assert_eq!(bv.len(), 10);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the BoolVec contains no booleans
use bool_vec::boolvec;
let bv = boolvec![];
assert!(bv.is_empty());use bool_vec::boolvec;
let mut bv = boolvec![true];
bv.pop();
assert!(bv.is_empty());Sourcepub fn bytes_capacity(&self) -> usize
pub fn bytes_capacity(&self) -> usize
Returns the capacity of the underlying vector that stores the values. This function is here just for testing purposes
This capacity is expressed in bytes, while the normal capacity of BoolVec is expressed in bits.
use bool_vec::boolvec;
let mut bv = boolvec![true; 8];
bv.push(true);
assert_eq!(bv.capacity(), bv.bytes_capacity()*8);Sourcepub fn bytes_len(&self) -> usize
pub fn bytes_len(&self) -> usize
Returns the number of elements of the underlying vector that stores the values. This function is here just for testing purposes
This length is expressed in bytes, while the normal length of BoolVec is expressed in bits.
use bool_vec::boolvec;
let mut bv = boolvec![true; 8];
assert_eq!(bv.len(), 8);
assert_eq!(bv.bytes_len(), 1);
bv.push(true);
assert_eq!(bv.len(), 9);
assert_eq!(bv.bytes_len(), 2);Sourcepub fn into_vec(&self) -> Vec<bool>
pub fn into_vec(&self) -> Vec<bool>
Copies BoolVec data into a Vec<bool>
use bool_vec::boolvec;
let bv = boolvec![true, false, true];
let vector = bv.into_vec();
assert_eq!(vector, vec![true, false, true]);To achieve the opposite (getting a BoolVec from a Vec<bool> or a &[bool]),
please see BoolVec::from()
WARNING: This might require a non indifferent amount of memory if you are working with a large amount of data, please consider trying to work with BoolVec directly if you can