pub struct Binvec<const L: usize, const N: usize> { /* private fields */ }Expand description
A fixed-length array type that stores 0 or 1.
It uses up to 8 times less memory compared to a bool array.
§Note
The bool type uses 1 byte (8 bits). Therefore, storing 100 bools requires 100 bytes (800 bits).
In general situations, this is not a big problem,
but when the array length exceeds thousands or in embedded environments with limited memory capacity,
this memory usage cannot be ignored.
To use minimal memory, calculate the minimum byte array size N needed to store L bits. The formula is as follows:
N = (L + 7) / 8For example, to store 10 bits, 2 bytes are used. In this case, the remaining 6 bits are unused.
|----- byte0 -----|----- byte1 -----|
| 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0 |
^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^
10 bits used Not used§Generics
L: The number of bits to store.N: The minimum byte array length required to storeL.
§Examples
use binvec::*;
// A binvec storing 12 bits initialized to 0
let mut binvec = binvec!(12, false);Implementations§
Source§impl<const L: usize, const N: usize> Binvec<L, N>
impl<const L: usize, const N: usize> Binvec<L, N>
Sourcepub const unsafe fn get_unchecked(&self, index: usize) -> bool
pub const unsafe fn get_unchecked(&self, index: usize) -> bool
Returns the bit value at the given index without performing bounds checking.
§Safety
Calling this method with an out-of-bounds index is undefined behavior.
§Arguments
index: The bit index to retrieve.
§Returns
trueif the bit atindexis 1.falseif the bit atindexis 0.
§Examples
use binvec::*;
let binvec = binvec!(12, true);
let bit = unsafe { binvec.get_unchecked(5) };
assert_eq!(bit, true);Sourcepub fn get(&self, index: usize) -> Option<bool>
pub fn get(&self, index: usize) -> Option<bool>
Returns the bit value at the given index with bounds checking.
§Arguments
index: The bit index to retrieve.
§Returns
Some(true)if the bit atindexis 1.Some(false)if the bit atindexis 0.Noneifindexis out of bounds.
§Examples
use binvec::*;
let binvec = binvec!(12, true);
assert_eq!(binvec.get(5), Some(true));
assert_eq!(binvec.get(20), None);Sourcepub unsafe fn set_unchecked(&mut self, index: usize, value: bool)
pub unsafe fn set_unchecked(&mut self, index: usize, value: bool)
Sets the bit value at the given index without performing bounds checking.
§Safety
Calling this method with an out-of-bounds index is undefined behavior.
§Arguments
index: The bit index to set.value: The bit value to set (truefor 1,falsefor 0).
§Examples
use binvec::*;
let mut binvec = binvec!(12, false);
unsafe { binvec.set_unchecked(3, true); }
assert_eq!(binvec.get(3), Some(true));Sourcepub fn set(&mut self, index: usize, value: bool) -> Result<(), IndexOutOfBounds>
pub fn set(&mut self, index: usize, value: bool) -> Result<(), IndexOutOfBounds>
Sets the bit value at the given index with bounds checking.
§Arguments
index: The bit index to set.value: The bit value to set (truefor 1,falsefor 0).
§Returns
Ok(())if the bit was successfully set.Err(())if the index is out of bounds.
§Examples
use binvec::*;
let mut binvec = binvec!(12, false);
assert_eq!(binvec.set(5, true), Ok(()));
assert_eq!(binvec.get(5), Some(true));
assert_eq!(binvec.set(20, true), Err(error::IndexOutOfBounds));Sourcepub const fn fill(&mut self, value: bool)
pub const fn fill(&mut self, value: bool)
Fills the entire Binvec with the specified bit value.
This method sets all bits in the Binvec to either true (1) or false (0).
For bits beyond the length L in the last byte, those bits are cleared to zero.
§Arguments
value: The bit value to fill theBinvecwith (truefor 1,falsefor 0).
§Examples
use binvec::*;
let mut binvec = binvec!(12, false);
binvec.fill(true);
assert_eq!(binvec.is_all_one(), true);Sourcepub const fn count_ones(&self) -> usize
pub const fn count_ones(&self) -> usize
Sourcepub const fn count_zeros(&self) -> usize
pub const fn count_zeros(&self) -> usize
Counts the number of bits set to 0 in the Binvec.
§Returns
The total count of bits that are set to 0.
§Examples
use binvec::*;
let binvec = binvec!(12, false);
assert_eq!(binvec.count_zeros(), 12);
let mut binvec = binvec!(12, true);
binvec.set(3, false).unwrap();
assert_eq!(binvec.count_zeros(), 1);Sourcepub const fn is_all_one(&self) -> bool
pub const fn is_all_one(&self) -> bool
Checks if all bits in the Binvec are set to 1.
§Returns
true if all bits that are using is 1, otherwise false.
§Examples
use binvec::*;
let binvec = binvec!(12, true);
assert_eq!(binvec.is_all_one(), true);
let mut binvec = binvec!(12, false);
assert_eq!(binvec.is_all_one(), false);
binvec.fill(true);
assert_eq!(binvec.is_all_one(), true);Sourcepub const fn is_all_zero(&self) -> bool
pub const fn is_all_zero(&self) -> bool
Checks if all bits in the Binvec are set to 0.
§Returns
true if all bits that are using is 0, otherwise false.
§Examples
use binvec::*;
let binvec = binvec!(12, false);
assert_eq!(binvec.is_all_zero(), true);
let mut binvec = binvec!(12, true);
assert_eq!(binvec.is_all_zero(), false);
binvec.fill(false);
assert_eq!(binvec.is_all_zero(), true);Sourcepub fn iter(&self) -> BinvecIter<'_, L, N> ⓘ
pub fn iter(&self) -> BinvecIter<'_, L, N> ⓘ
Returns an iterator over the bits of the Binvec.
§Returns
A BinvecIter that yields each bit as a bool.
§Examples
use binvec::*;
let binvec = binvec!(12, true);
for bit in binvec.iter() {
assert_eq!(bit, true);
}