BitMap

Struct BitMap 

Source
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

Source

pub fn new(size: usize) -> Self

Create a new bitmap with the specified number of bits

§Arguments
  • size - Number of bits in the bitmap
§Examples
use yimi_rutool::algorithms::BitMap;

let bitmap = BitMap::new(1000);
assert_eq!(bitmap.len(), 1000);
Source

pub fn filled(size: usize) -> Self

Create a bitmap filled with ones

§Arguments
  • size - Number of bits in the bitmap
§Examples
use yimi_rutool::algorithms::BitMap;

let bitmap = BitMap::filled(8);
assert_eq!(bitmap.count_ones(), 8);
Source

pub fn get(&self, index: usize) -> bool

Get the value of a bit at the specified index

§Arguments
  • index - Bit index (0-based)
§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));
assert!(!bitmap.get(43));
Source

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));
Source

pub fn toggle(&mut self, index: usize)

Toggle the bit at the specified index

§Arguments
  • index - Bit index (0-based)
§Examples
use yimi_rutool::algorithms::BitMap;

let mut bitmap = BitMap::new(100);
bitmap.toggle(42);
assert!(bitmap.get(42));
bitmap.toggle(42);
assert!(!bitmap.get(42));
Source

pub fn fill(&mut self, value: bool)

Fill all bits with the specified value

§Arguments
  • value - Value to set all bits to
§Examples
use yimi_rutool::algorithms::BitMap;

let mut bitmap = BitMap::new(100);
bitmap.fill(true);
assert_eq!(bitmap.count_ones(), 100);

bitmap.fill(false);
assert_eq!(bitmap.count_ones(), 0);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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());
Source

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());
Source

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());
Source

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 set
Source

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 bitmap2
Source

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 set
Source

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);
Source

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]);
Source

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]);
Source

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 bounds

Trait Implementations§

Source§

impl Clone for BitMap

Source§

fn clone(&self) -> BitMap

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BitMap

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Index<usize> for BitMap

Source§

type Output = bool

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl PartialEq for BitMap

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,