Struct bitmac::var_bitmap::VarBitmap
source · [−]pub struct VarBitmap<D, B, S> { /* private fields */ }
Expand description
A bitmap that can be resized by custom resizing strategy.
Any structure that implements the ContainerRead
(for read-only access) and
ContainerWrite
+ Resizable
(for mutable access) traits can be a container of bitmap (e.g. Vec<T>
).
It has the same interface as StaticBitmap
except that mutable access requires resizable container.
Container tries to grow if the changing bit is out of bounds.
Usage example:
use bitmac::{VarBitmap, LSB, MinimumRequiredStrategy};
// You can directly check every single bit
let bitmap = VarBitmap::<_, LSB, MinimumRequiredStrategy>::from_container(vec![0b0000_0001u8]);
assert!(bitmap.get(0));
assert!(!bitmap.get(11));
assert!(!bitmap.get(13));
// You can iterate over bits
let bitmap = VarBitmap::<_, LSB, MinimumRequiredStrategy>::from_container(vec![0b0000_1001u8, 0b0000_1000]);
let mut iter = bitmap.iter().by_bits().enumerate();
assert_eq!(iter.next(), Some((0, true)));
assert_eq!(iter.next(), Some((1, false)));
assert_eq!(iter.next(), Some((2, false)));
assert_eq!(iter.next(), Some((3, true)));
assert_eq!(iter.next(), Some((4, false)));
// You can check multiple bits at the same time through the intersection
use bitmac::Intersection;
let bitmap = VarBitmap::<_, LSB, MinimumRequiredStrategy>::from_container(vec![0b0000_1001u8, 0b0000_1000]);
// .. by creating specific new container for result
let test = [0b0000_1001u8, 0b0000_0000];
assert_eq!(bitmap.intersection::<[u8; 2]>(&test), test);
// .. by using preallocated container for result
let test = [0b0000_1001u8, 0b0000_0000];
let mut result = [0u8; 2];
bitmap.intersection_in(&test, &mut result);
assert_eq!(result, test);
// .. by comparing length of difference that is equivalent to count of ones (bits) in result
let test = [0b0000_1001u8, 0b0000_0000];
assert_eq!(bitmap.intersection_len(&test), test.iter().fold(0, |acc, &v| acc + v.count_ones() as usize));
// You can directly change every bit
let mut bitmap = VarBitmap::<_, LSB, MinimumRequiredStrategy>::from_container(vec![0b0000_1001u8, 0b0001_1000]);
assert!(bitmap.get(0));
assert!(bitmap.get(3));
assert!(bitmap.get(11));
assert!(bitmap.get(12));
assert!(!bitmap.get(13));
assert!(!bitmap.get(128));
bitmap.set(12, false);
assert!(!bitmap.get(12));
bitmap.set(13, true);
assert!(bitmap.get(13));
// If you change the bit exceeding container's length and new bit state is `1` (`true`)
// then the container will automatically grow
bitmap.set(127, true);
assert!(bitmap.get(127));
assert_eq!(bitmap.as_ref().len(), 16);
Implementations
sourceimpl<D, B, S, N> VarBitmap<D, B, S> where
D: ContainerRead<B, Slot = N>,
B: BitAccess,
S: GrowStrategy,
N: Number,
impl<D, B, S, N> VarBitmap<D, B, S> where
D: ContainerRead<B, Slot = N>,
B: BitAccess,
S: GrowStrategy,
N: Number,
sourcepub fn new(data: D, resizing_strategy: S) -> Self
pub fn new(data: D, resizing_strategy: S) -> Self
Creates new bitmap from container with specified strategy.
sourcepub fn count_ones(&self) -> usize
pub fn count_ones(&self) -> usize
Returns number of ones in the bitmap.
sourcepub fn count_zeros(&self) -> usize
pub fn count_zeros(&self) -> usize
Returns number of zeros in the bitmap.
sourceimpl<D, B, S, N> VarBitmap<D, B, S> where
D: ContainerRead<B, Slot = N> + Default,
B: BitAccess,
S: GrowStrategy,
N: Number,
impl<D, B, S, N> VarBitmap<D, B, S> where
D: ContainerRead<B, Slot = N> + Default,
B: BitAccess,
S: GrowStrategy,
N: Number,
sourcepub fn with_resizing_strategy(resizing_strategy: S) -> Self
pub fn with_resizing_strategy(resizing_strategy: S) -> Self
Creates default bitmap with specified strategy.
sourceimpl<D, B, S, N> VarBitmap<D, B, S> where
D: ContainerRead<B, Slot = N>,
B: BitAccess,
S: GrowStrategy + Default,
N: Number,
impl<D, B, S, N> VarBitmap<D, B, S> where
D: ContainerRead<B, Slot = N>,
B: BitAccess,
S: GrowStrategy + Default,
N: Number,
sourcepub fn from_container(data: D) -> Self
pub fn from_container(data: D) -> Self
Creates new bitmap from container with default strategy.
sourceimpl<D, B, S> VarBitmap<D, B, S>
impl<D, B, S> VarBitmap<D, B, S>
sourcepub fn into_inner(self) -> D
pub fn into_inner(self) -> D
Converts bitmap into inner container.
sourceimpl<D, B, S, N> VarBitmap<D, B, S> where
D: ContainerRead<B, Slot = N>,
N: Number,
B: BitAccess,
impl<D, B, S, N> VarBitmap<D, B, S> where
D: ContainerRead<B, Slot = N>,
N: Number,
B: BitAccess,
sourcepub fn as_static<'a>(&'a self) -> StaticBitmap<&'a D, B> where
&'a D: ContainerRead<B>,
pub fn as_static<'a>(&'a self) -> StaticBitmap<&'a D, B> where
&'a D: ContainerRead<B>,
Represents bitmap as static bitmap over &D
container.
sourcepub fn into_static(self) -> StaticBitmap<D, B>
pub fn into_static(self) -> StaticBitmap<D, B>
Converts bitmap into static bitmap.
sourceimpl<D, B, S> VarBitmap<D, B, S> where
D: ContainerRead<B>,
B: BitAccess,
impl<D, B, S> VarBitmap<D, B, S> where
D: ContainerRead<B>,
B: BitAccess,
sourcepub fn get(&self, idx: usize) -> bool
pub fn get(&self, idx: usize) -> bool
Gets single bit state.
Usage example:
use bitmac::{StaticBitmap, LSB};
let bitmap = StaticBitmap::<_, LSB>::new([0b0000_0001u8, 0b0000_1000]);
assert!(bitmap.get(0));
assert!(bitmap.get(11));
assert!(!bitmap.get(13));
// Out of bounds bits always returns false
assert!(!bitmap.get(128));
sourceimpl<D, B, S, N> VarBitmap<D, B, S> where
D: ContainerWrite<B, Slot = N> + Resizable<Slot = N>,
N: Number,
S: GrowStrategy,
B: BitAccess,
impl<D, B, S, N> VarBitmap<D, B, S> where
D: ContainerWrite<B, Slot = N> + Resizable<Slot = N>,
N: Number,
S: GrowStrategy,
B: BitAccess,
sourcepub fn set(&mut self, idx: usize, val: bool)
pub fn set(&mut self, idx: usize, val: bool)
Sets new state for a single bit.
Panic
Panics if resizing fails.
See non-panic function try_set
.
Usage example:
use bitmac::{VarBitmap, LSB, MinimumRequiredStrategy, LimitStrategy};
let mut bitmap = VarBitmap::<_, LSB, LimitStrategy<MinimumRequiredStrategy>>::new(
vec![0u8; 1], LimitStrategy{ strategy: Default::default(), limit: 3 },
);
bitmap.set(6, true);
assert!(bitmap.get(6));
bitmap.set(13, true);
assert!(bitmap.get(13));
bitmap.set(13, false);
assert!(!bitmap.get(13));
// bitmap.set(128, false); <-- Panics
sourcepub fn try_set(&mut self, idx: usize, val: bool) -> Result<(), ResizeError>
pub fn try_set(&mut self, idx: usize, val: bool) -> Result<(), ResizeError>
Sets new state for a single bit.
Returns Err(_)
if resizing fails.
Usage example:
use bitmac::{VarBitmap, LSB, MinimumRequiredStrategy, LimitStrategy};
let mut bitmap = VarBitmap::<_, LSB, LimitStrategy<MinimumRequiredStrategy>>::new(
vec![0u8; 1], LimitStrategy{ strategy: Default::default(), limit: 3 },
);
assert!(bitmap.try_set(12, true).is_ok());
assert!(bitmap.get(12));
assert_eq!(bitmap.as_ref().len(), 2);
assert!(bitmap.try_set(12, false).is_ok());
assert!(!bitmap.get(12));
assert_eq!(bitmap.as_ref().len(), 2);
// Grow strategy returns error
assert!(bitmap.try_set(128, true).is_err());
assert!(!bitmap.get(128));
assert_eq!(bitmap.as_ref().len(), 2);
Trait Implementations
sourceimpl<D, B, S> ContainerRead<B> for VarBitmap<D, B, S> where
D: ContainerRead<B>,
B: BitAccess,
impl<D, B, S> ContainerRead<B> for VarBitmap<D, B, S> where
D: ContainerRead<B>,
B: BitAccess,
type Slot = <D as ContainerRead<B>>::Slot
sourcefn slots_count(&self) -> usize
fn slots_count(&self) -> usize
Gets number of stored slots.
sourcefn bits_count(&self) -> usize
fn bits_count(&self) -> usize
Gets max number of bits.
sourceimpl<D, B, S> ContainerWrite<B> for VarBitmap<D, B, S> where
D: ContainerWrite<B>,
B: BitAccess,
impl<D, B, S> ContainerWrite<B> for VarBitmap<D, B, S> where
D: ContainerWrite<B>,
B: BitAccess,
sourcefn get_mut_slot(&mut self, idx: usize) -> &mut Self::Slot
fn get_mut_slot(&mut self, idx: usize) -> &mut Self::Slot
Gets mutable reference to stored slot.
sourceimpl<D, B, S, N> Debug for VarBitmap<D, B, S> where
D: ContainerRead<B, Slot = N>,
N: Number,
B: BitAccess,
impl<D, B, S, N> Debug for VarBitmap<D, B, S> where
D: ContainerRead<B, Slot = N>,
N: Number,
B: BitAccess,
sourceimpl<D, N, B, S> From<D> for VarBitmap<D, B, S> where
D: ContainerRead<B, Slot = N>,
N: Number,
B: BitAccess,
S: Default,
impl<D, N, B, S> From<D> for VarBitmap<D, B, S> where
D: ContainerRead<B, Slot = N>,
N: Number,
B: BitAccess,
S: Default,
sourceimpl<D, B, S, Rhs, N> Intersection<Rhs, N, B> for VarBitmap<D, B, S> where
D: ContainerRead<B, Slot = N>,
B: BitAccess,
Rhs: ContainerRead<B, Slot = N>,
N: Number,
impl<D, B, S, Rhs, N> Intersection<Rhs, N, B> for VarBitmap<D, B, S> where
D: ContainerRead<B, Slot = N>,
B: BitAccess,
Rhs: ContainerRead<B, Slot = N>,
N: Number,
sourcefn intersection_in<Dst>(&self, rhs: &Rhs, dst: &mut Dst) where
Dst: ContainerWrite<B, Slot = N>,
fn intersection_in<Dst>(&self, rhs: &Rhs, dst: &mut Dst) where
Dst: ContainerWrite<B, Slot = N>,
Calculates intersection in-place. Result will be stored in dst
. Read more
sourcefn try_intersection_in<Dst>(
&self,
rhs: &Rhs,
dst: &mut Dst
) -> Result<(), IntersectionError> where
Dst: ContainerWrite<B, Slot = N>,
fn try_intersection_in<Dst>(
&self,
rhs: &Rhs,
dst: &mut Dst
) -> Result<(), IntersectionError> where
Dst: ContainerWrite<B, Slot = N>,
Calculates intersection in-place. Result will be stored in dst
. Read more
sourcefn intersection<Dst>(&self, rhs: &Rhs) -> Dst where
Dst: ContainerWrite<B, Slot = N> + TryWithSlots,
fn intersection<Dst>(&self, rhs: &Rhs) -> Dst where
Dst: ContainerWrite<B, Slot = N> + TryWithSlots,
Calculates intersection. Result container will be created with try_with_slots
function. Read more
sourcefn try_intersection<Dst>(&self, rhs: &Rhs) -> Result<Dst, IntersectionError> where
Dst: ContainerWrite<B, Slot = N> + TryWithSlots,
fn try_intersection<Dst>(&self, rhs: &Rhs) -> Result<Dst, IntersectionError> where
Dst: ContainerWrite<B, Slot = N> + TryWithSlots,
Calculates intersection. Result container will be created with try_with_slots
function. Read more
sourcefn intersection_len(&self, rhs: &Rhs) -> usize
fn intersection_len(&self, rhs: &Rhs) -> usize
Calculates intersection length - ones count. It doesn’t allocate for storing intersection result. Read more
sourceimpl<'a, D, B, S> IntoIterator for &'a VarBitmap<D, B, S> where
D: ContainerRead<B>,
B: BitAccess,
impl<'a, D, B, S> IntoIterator for &'a VarBitmap<D, B, S> where
D: ContainerRead<B>,
B: BitAccess,
sourceimpl<D, B, S> IntoIterator for VarBitmap<D, B, S> where
D: ContainerRead<B>,
B: BitAccess,
impl<D, B, S> IntoIterator for VarBitmap<D, B, S> where
D: ContainerRead<B>,
B: BitAccess,
sourceimpl<D: PartialEq, B: PartialEq, S: PartialEq> PartialEq<VarBitmap<D, B, S>> for VarBitmap<D, B, S>
impl<D: PartialEq, B: PartialEq, S: PartialEq> PartialEq<VarBitmap<D, B, S>> for VarBitmap<D, B, S>
sourceimpl<D, B, S, Rhs, N> Union<Rhs, N, B> for VarBitmap<D, B, S> where
D: ContainerRead<B, Slot = N>,
B: BitAccess,
Rhs: ContainerRead<B, Slot = N>,
N: Number,
impl<D, B, S, Rhs, N> Union<Rhs, N, B> for VarBitmap<D, B, S> where
D: ContainerRead<B, Slot = N>,
B: BitAccess,
Rhs: ContainerRead<B, Slot = N>,
N: Number,
sourcefn union_in<Dst>(&self, rhs: &Rhs, dst: &mut Dst) where
Dst: ContainerWrite<B, Slot = N>,
fn union_in<Dst>(&self, rhs: &Rhs, dst: &mut Dst) where
Dst: ContainerWrite<B, Slot = N>,
Calculates union in-place. Result will be stored in dst
. Read more
sourcefn try_union_in<Dst>(&self, rhs: &Rhs, dst: &mut Dst) -> Result<(), UnionError> where
Dst: ContainerWrite<B, Slot = N>,
fn try_union_in<Dst>(&self, rhs: &Rhs, dst: &mut Dst) -> Result<(), UnionError> where
Dst: ContainerWrite<B, Slot = N>,
Calculates union in-place. Result will be stored in dst
. Read more
sourcefn union<Dst>(&self, rhs: &Rhs) -> Dst where
Dst: ContainerWrite<B, Slot = N> + TryWithSlots,
fn union<Dst>(&self, rhs: &Rhs) -> Dst where
Dst: ContainerWrite<B, Slot = N> + TryWithSlots,
Calculates union. Result container will be created with try_with_slots
function. Read more
sourcefn try_union<Dst>(&self, rhs: &Rhs) -> Result<Dst, UnionError> where
Dst: ContainerWrite<B, Slot = N> + TryWithSlots,
fn try_union<Dst>(&self, rhs: &Rhs) -> Result<Dst, UnionError> where
Dst: ContainerWrite<B, Slot = N> + TryWithSlots,
Calculates union. Result container will be created with try_with_slots
function. Read more
impl<D: Eq, B: Eq, S: Eq> Eq for VarBitmap<D, B, S>
impl<D, B, S> StructuralEq for VarBitmap<D, B, S>
impl<D, B, S> StructuralPartialEq for VarBitmap<D, B, S>
Auto Trait Implementations
impl<D, B, S> RefUnwindSafe for VarBitmap<D, B, S> where
B: RefUnwindSafe,
D: RefUnwindSafe,
S: RefUnwindSafe,
impl<D, B, S> Send for VarBitmap<D, B, S> where
B: Send,
D: Send,
S: Send,
impl<D, B, S> Sync for VarBitmap<D, B, S> where
B: Sync,
D: Sync,
S: Sync,
impl<D, B, S> Unpin for VarBitmap<D, B, S> where
B: Unpin,
D: Unpin,
S: Unpin,
impl<D, B, S> UnwindSafe for VarBitmap<D, B, S> where
B: UnwindSafe,
D: UnwindSafe,
S: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more