pub struct BitMap { /* private fields */ }Expand description
A memory-efficient bitmap implementation
Provides efficient bit manipulation operations for use in bloom filters and other data structures that require bit-level operations.
§Examples
use yimi_rutool::algorithms::BitMap;
let mut bitmap = BitMap::new(100);
bitmap.set(42, true);
assert!(bitmap.get(42));
assert_eq!(bitmap.count_ones(), 1);Implementations§
Source§impl BitMap
 
impl BitMap
Sourcepub fn set(&mut self, index: usize, value: bool)
 
pub fn set(&mut self, index: usize, value: bool)
Set the value of a bit at the specified index
§Arguments
index- Bit index (0-based)value- Value to set (true for 1, false for 0)
§Panics
Panics if index is out of bounds
§Examples
use yimi_rutool::algorithms::BitMap;
let mut bitmap = BitMap::new(100);
bitmap.set(42, true);
assert!(bitmap.get(42));
bitmap.set(42, false);
assert!(!bitmap.get(42));Sourcepub fn clear(&mut self)
 
pub fn clear(&mut self)
Clear all bits (set to 0)
§Examples
use yimi_rutool::algorithms::BitMap;
let mut bitmap = BitMap::filled(100);
bitmap.clear();
assert_eq!(bitmap.count_ones(), 0);Sourcepub fn count_ones(&self) -> usize
 
pub fn count_ones(&self) -> usize
Count the number of bits set to 1
§Examples
use yimi_rutool::algorithms::BitMap;
let mut bitmap = BitMap::new(100);
bitmap.set(10, true);
bitmap.set(20, true);
bitmap.set(30, true);
assert_eq!(bitmap.count_ones(), 3);Sourcepub fn count_zeros(&self) -> usize
 
pub fn count_zeros(&self) -> usize
Count the number of bits set to 0
§Examples
use yimi_rutool::algorithms::BitMap;
let mut bitmap = BitMap::new(100);
bitmap.set(10, true);
assert_eq!(bitmap.count_zeros(), 99);Sourcepub fn len(&self) -> usize
 
pub fn len(&self) -> usize
Get the number of bits in the bitmap
§Examples
use yimi_rutool::algorithms::BitMap;
let bitmap = BitMap::new(1000);
assert_eq!(bitmap.len(), 1000);Sourcepub fn is_empty(&self) -> bool
 
pub fn is_empty(&self) -> bool
Check if the bitmap is empty (size 0)
§Examples
use yimi_rutool::algorithms::BitMap;
let empty_bitmap = BitMap::new(0);
assert!(empty_bitmap.is_empty());
let bitmap = BitMap::new(100);
assert!(!bitmap.is_empty());Sourcepub fn all_zeros(&self) -> bool
 
pub fn all_zeros(&self) -> bool
Check if all bits are set to 0
§Examples
use yimi_rutool::algorithms::BitMap;
let mut bitmap = BitMap::new(100);
assert!(bitmap.all_zeros());
bitmap.set(50, true);
assert!(!bitmap.all_zeros());Sourcepub fn all_ones(&self) -> bool
 
pub fn all_ones(&self) -> bool
Check if all bits are set to 1
§Examples
use yimi_rutool::algorithms::BitMap;
let mut bitmap = BitMap::new(100);
assert!(!bitmap.all_ones());
bitmap.fill(true);
assert!(bitmap.all_ones());Sourcepub fn and(&mut self, other: &BitMap)
 
pub fn and(&mut self, other: &BitMap)
Perform bitwise AND operation with another bitmap
§Arguments
other- The other bitmap to AND with
§Panics
Panics if the bitmaps have different sizes
§Examples
use yimi_rutool::algorithms::BitMap;
let mut bitmap1 = BitMap::new(100);
let mut bitmap2 = BitMap::new(100);
bitmap1.set(10, true);
bitmap1.set(20, true);
bitmap2.set(10, true);
bitmap2.set(30, true);
bitmap1.and(&bitmap2);
assert!(bitmap1.get(10));  // Both had this bit set
assert!(!bitmap1.get(20)); // Only bitmap1 had this bit set
assert!(!bitmap1.get(30)); // Only bitmap2 had this bit setSourcepub fn or(&mut self, other: &BitMap)
 
pub fn or(&mut self, other: &BitMap)
Perform bitwise OR operation with another bitmap
§Arguments
other- The other bitmap to OR with
§Panics
Panics if the bitmaps have different sizes
§Examples
use yimi_rutool::algorithms::BitMap;
let mut bitmap1 = BitMap::new(100);
let mut bitmap2 = BitMap::new(100);
bitmap1.set(10, true);
bitmap2.set(20, true);
bitmap1.or(&bitmap2);
assert!(bitmap1.get(10)); // From bitmap1
assert!(bitmap1.get(20)); // From bitmap2Sourcepub fn xor(&mut self, other: &BitMap)
 
pub fn xor(&mut self, other: &BitMap)
Perform bitwise XOR operation with another bitmap
§Arguments
other- The other bitmap to XOR with
§Panics
Panics if the bitmaps have different sizes
§Examples
use yimi_rutool::algorithms::BitMap;
let mut bitmap1 = BitMap::new(100);
let mut bitmap2 = BitMap::new(100);
bitmap1.set(10, true);
bitmap1.set(20, true);
bitmap2.set(10, true);
bitmap2.set(30, true);
bitmap1.xor(&bitmap2);
assert!(!bitmap1.get(10)); // Both had this bit set
assert!(bitmap1.get(20));  // Only bitmap1 had this bit set
assert!(bitmap1.get(30));  // Only bitmap2 had this bit setSourcepub fn not(&mut self)
 
pub fn not(&mut self)
Perform bitwise NOT operation (invert all bits)
§Examples
use yimi_rutool::algorithms::BitMap;
let mut bitmap = BitMap::new(100);
bitmap.set(10, true);
bitmap.not();
assert!(!bitmap.get(10));
assert_eq!(bitmap.count_ones(), 99);Sourcepub fn iter_ones(&self) -> impl Iterator<Item = usize> + '_
 
pub fn iter_ones(&self) -> impl Iterator<Item = usize> + '_
Get an iterator over all set bit indices
§Examples
use yimi_rutool::algorithms::BitMap;
let mut bitmap = BitMap::new(100);
bitmap.set(10, true);
bitmap.set(20, true);
bitmap.set(30, true);
let set_bits: Vec<usize> = bitmap.iter_ones().collect();
assert_eq!(set_bits, vec![10, 20, 30]);Sourcepub fn iter_zeros(&self) -> impl Iterator<Item = usize> + '_
 
pub fn iter_zeros(&self) -> impl Iterator<Item = usize> + '_
Get an iterator over all unset bit indices
§Examples
use yimi_rutool::algorithms::BitMap;
let mut bitmap = BitMap::new(5);
bitmap.set(1, true);
bitmap.set(3, true);
let unset_bits: Vec<usize> = bitmap.iter_zeros().collect();
assert_eq!(unset_bits, vec![0, 2, 4]);Sourcepub fn resize(&mut self, new_size: usize)
 
pub fn resize(&mut self, new_size: usize)
Resize the bitmap to a new size
If the new size is larger, new bits are set to false. If the new size is smaller, excess bits are discarded.
§Arguments
new_size- New size in bits
§Examples
use yimi_rutool::algorithms::BitMap;
let mut bitmap = BitMap::new(100);
bitmap.set(50, true);
bitmap.resize(200);
assert_eq!(bitmap.len(), 200);
assert!(bitmap.get(50)); // Existing data preserved
bitmap.resize(25);
assert_eq!(bitmap.len(), 25);
// bit 50 is now out of boundsTrait Implementations§
impl Eq for BitMap
Auto Trait Implementations§
impl Freeze for BitMap
impl RefUnwindSafe for BitMap
impl Send for BitMap
impl Sync for BitMap
impl Unpin for BitMap
impl UnwindSafe for BitMap
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
 
impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
 
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
    T: Clone,
 
impl<T> CloneToUninit for Twhere
    T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
 
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
 
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
 
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
 
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
 
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
 
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
 
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
 
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more