Skip to main content

Bitmap

Struct Bitmap 

Source
pub struct Bitmap<T: BitStorage> { /* private fields */ }
Expand description

一个通用的位图结构,由一个实现了 BitStorage 的类型支持。

§示例

// 使用 u16 作为存储类型,总共 16 位
let mut bitmap = Bitmap::<u16>::new();
bitmap.set(3, true);
bitmap.set(5, true);

assert!(bitmap.get(3));
assert!(!bitmap.get(4));

let positions: Vec<usize> = bitmap.iter_ones().collect();
assert_eq!(positions, vec![3, 5]);

Implementations§

Source§

impl<T: BitStorage> Bitmap<T>

Source

pub fn new_empty() -> Self

创建一个所有位都为 0 的空位图。

§示例
let bitmap = Bitmap::<u16>::new_empty();
assert!(bitmap.none());
assert_eq!(bitmap.count_ones(), 0);
Source

pub fn new_full() -> Self

创建一个所有位都为 1 的全满位图。

§示例
let bitmap = Bitmap::<u8>::new_full();
assert!(bitmap.all());
assert_eq!(bitmap.count_ones(), 8);
Source

pub fn new() -> Self

创建一个空的位图,是 new_empty 的别名。

§示例
let bitmap = Bitmap::<u32>::new();
assert!(bitmap.none());
Source

pub const fn iter_ones(&self) -> PositiveIter<T>

返回一个迭代器,用于遍历所有值为 1 的位的索引。

§示例
let mut bitmap = Bitmap::<u8>::new();
bitmap.set(2, true);
bitmap.set(6, true);
let ones: Vec<usize> = bitmap.iter_ones().collect();
assert_eq!(ones, vec![2, 6]);
Source

pub const fn iter_zeros(&self) -> NegativeIter<T>

返回一个迭代器,用于遍历所有值为 0 的位的索引。

§示例
let mut bitmap = Bitmap::<u8>::new();
bitmap.set(0, true);
bitmap.set(1, true);
bitmap.set(2, true);
bitmap.set(3, true);
// 内部值为 ...00001111
let zeros: Vec<usize> = bitmap.iter_zeros().collect();
assert_eq!(zeros, vec![4, 5, 6, 7]);
Source

pub fn set(&mut self, idx: usize, set: bool)

设置指定索引的位。

true 表示设置为 1,false 表示设置为 0。

§Panics

如果 idx 超出位图的范围(idx >= T::BITS),在调试模式下会触发 panic。

§示例
let mut bitmap = Bitmap::<u8>::new();
bitmap.set(5, true);
assert!(bitmap.get(5));
bitmap.set(5, false);
assert!(!bitmap.get(5));
Source

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

获取指定索引的位的值。

返回 true 如果该位为 1,否则返回 false

§Panics

如果 idx 超出位图的范围(idx >= T::BITS),在调试模式下会触发 panic。

§示例
let mut bitmap = Bitmap::<u8>::new();
bitmap.set(7, true);
assert_eq!(bitmap.get(7), true);
assert_eq!(bitmap.get(0), false);
Source

pub fn is_one_on(&self, idx: usize) -> bool

检查指定索引的位是否为 1。get 的别名。

§示例
let mut bitmap = Bitmap::<u8>::new();
bitmap.set(1, true);
assert!(bitmap.is_one_on(1));
Source

pub fn is_zero_on(&self, idx: usize) -> bool

检查指定索引的位是否为 0。

§示例
let mut bitmap = Bitmap::<u8>::new();
bitmap.set(1, true);
assert!(bitmap.is_zero_on(0));
Source

pub fn merge(self, rhs: Bitmap<T>) -> Bitmap<T>

将两个位图进行合并(并集),等同于 | 按位或操作。

§示例
let mut b1 = Bitmap::<u8>::new();
b1.set(1, true); // 00000010
let mut b2 = Bitmap::<u8>::new();
b2.set(2, true); // 00000100

let merged = b1.merge(b2); // 00000110
assert!(merged.get(1));
assert!(merged.get(2));
Source

pub fn count_ones(&self) -> u32

计算值为 1 的位的数量。

§示例
let mut bitmap = Bitmap::<u8>::new();
bitmap.set(0, true);
bitmap.set(2, true);
bitmap.set(4, true);
assert_eq!(bitmap.count_ones(), 3);
Source

pub fn count_zeros(&self) -> u32

计算值为 0 的位的数量。

§示例
let mut bitmap = Bitmap::<u8>::new();
bitmap.set(0, true);
bitmap.set(2, true);
bitmap.set(4, true);
// u8 有 8 位,3 个是 1,所以 5 个是 0
assert_eq!(bitmap.count_zeros(), 5);
Source

pub fn any(&self) -> bool

检查位图中是否至少有一个位是 1。

§示例
let mut b1 = Bitmap::<u8>::new();
b1.set(3, true);
assert!(b1.any());

let b2 = Bitmap::<u8>::new();
assert!(!b2.any());
Source

pub fn all(&self) -> bool

检查位图中是否所有位都是 1。

§示例
let b1 = Bitmap::<u8>::new_full();
assert!(b1.all());

let mut b2 = Bitmap::<u8>::new_full();
b2.set(4, false);
assert!(!b2.all());
Source

pub fn none(&self) -> bool

检查位图中是否所有位都是 0。

§示例
let b1 = Bitmap::<u8>::new();
assert!(b1.none());

let mut b2 = Bitmap::<u8>::new();
b2.set(0, true);
assert!(!b2.none());
Source

pub fn first_one(&self) -> Option<usize>

查找第一个值为 1 的位的索引。

如果所有位都为 0,则返回 None

§示例
let mut bitmap = Bitmap::<u16>::new();
bitmap.set(5, true);
bitmap.set(10, true);
assert_eq!(bitmap.first_one(), Some(5));

let empty_bitmap = Bitmap::<u16>::new();
assert_eq!(empty_bitmap.first_one(), None);

Trait Implementations§

Source§

impl<T: BitStorage> BitAnd for Bitmap<T>

Source§

fn bitand(self, rhs: Self) -> Self::Output

按位与(&)。

§示例
let mut b1 = Bitmap::<u8>::from(0b__0000_1101); // 位 0, 2, 3 的值为 1
let mut b2 = Bitmap::<u8>::from(0b__0000_1011); // 位 0, 1, 3 的值为 1
let result = b1 & b2;
let expected = Bitmap::<u8>::from(0b__0000_1001); // 位 0, 3 的值为 1
assert_eq!(result, expected);
Source§

type Output = Bitmap<T>

The resulting type after applying the & operator.
Source§

impl<T: BitStorage> BitAndAssign for Bitmap<T>

Source§

fn bitand_assign(&mut self, rhs: Self)

按位或后赋值(&=)。

§示例
let mut b1 = Bitmap::<u8>::from(0b__0000_1101); // 位 0, 2, 3 的值为 1
let mut b2 = Bitmap::<u8>::from(0b__0000_1011); // 位 0, 1, 3 的值为 1
b1 &= b2;
let expected = Bitmap::<u8>::from(0b__0000_1001); // 位 0, 3 的值为 1
assert_eq!(b1, expected);
Source§

impl<T: BitStorage> BitOr for Bitmap<T>

Source§

fn bitor(self, rhs: Self) -> Self::Output

按位或(|)。

§示例
let mut b1 = Bitmap::<u8>::from(0b__0000_1101); // 位 0, 2, 3 的值为 1
let mut b2 = Bitmap::<u8>::from(0b__0000_1011); // 位 0, 1, 3 的值为 1
let result = b1 | b2;
let expected = Bitmap::<u8>::from(0b__0000_1111); // 位 0, 1, 2, 3 的值为 1
assert_eq!(result, expected);
Source§

type Output = Bitmap<T>

The resulting type after applying the | operator.
Source§

impl<T: BitStorage> BitOrAssign for Bitmap<T>

Source§

fn bitor_assign(&mut self, rhs: Self)

按位或后赋值(|=)。

§示例
let mut b1 = Bitmap::<u8>::from(0b__0000_1101); // 位 0, 2, 3 的值为 1
let mut b2 = Bitmap::<u8>::from(0b__0000_1011); // 位 0, 1, 3 的值为 1
b1 |= b2;
let expected = Bitmap::<u8>::from(0b__0000_1111); // 位 0, 1, 2, 3 的值为 1
assert_eq!(b1, expected);
Source§

impl<T: BitStorage> BitXor for Bitmap<T>

Source§

fn bitxor(self, rhs: Self) -> Self::Output

按位异或(^)

§示例
let mut b1 = Bitmap::<u8>::from(0b__0000_1101); // 位 0, 2, 3 的值为 1
let mut b2 = Bitmap::<u8>::from(0b__0000_1011); // 位 0, 1, 3 的值为 1
let result = b1 ^ b2;
let expected = Bitmap::<u8>::from(0b__0000_0110); // 位 1, 2 的值为 1
assert_eq!(result, expected);
Source§

type Output = Bitmap<T>

The resulting type after applying the ^ operator.
Source§

impl<T: BitStorage> BitXorAssign for Bitmap<T>

Source§

fn bitxor_assign(&mut self, rhs: Self)

按位或后赋值(^=)。

§示例
let mut b1 = Bitmap::<u8>::from(0b__0000_1101); // 位 0, 2, 3 的值为 1
let mut b2 = Bitmap::<u8>::from(0b__0000_1011); // 位 0, 1, 3 的值为 1
b1 ^= b2;
let expected = Bitmap::<u8>::from(0b__0000_0110); // 位 1, 2 的值为 1
assert_eq!(b1, expected);
Source§

impl<T: Clone + BitStorage> Clone for Bitmap<T>

Source§

fn clone(&self) -> Bitmap<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Copy + BitStorage> Copy for Bitmap<T>

Source§

impl<T: Debug + BitStorage> Debug for Bitmap<T>

Source§

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

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

impl<T: Default + BitStorage> Default for Bitmap<T>

Source§

fn default() -> Bitmap<T>

Returns the “default value” for a type. Read more
Source§

impl<T: Eq + BitStorage> Eq for Bitmap<T>

Source§

impl<T: BitStorage> From<T> for Bitmap<T>

Source§

fn from(val: T) -> Self

Converts to this type from the input type.
Source§

impl<T: BitStorage> IntoIterator for &Bitmap<T>

Source§

type Item = usize

The type of the elements being iterated over.
Source§

type IntoIter = PositiveIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: BitStorage> IntoIterator for Bitmap<T>

Source§

type Item = usize

The type of the elements being iterated over.
Source§

type IntoIter = PositiveIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: BitStorage> Not for Bitmap<T>

Source§

fn not(self) -> Self::Output

按位取反(!)

§示例
let b = Bitmap::<u8>::from(0b__1111_0000);
let result = !b;
let expected = Bitmap::<u8>::from(0b__0000_1111);
assert_eq!(result, expected);
Source§

type Output = Bitmap<T>

The resulting type after applying the ! operator.
Source§

impl<T: PartialEq + BitStorage> PartialEq for Bitmap<T>

Source§

fn eq(&self, other: &Bitmap<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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<T: BitStorage> StructuralPartialEq for Bitmap<T>

Auto Trait Implementations§

§

impl<T> Freeze for Bitmap<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Bitmap<T>
where T: RefUnwindSafe,

§

impl<T> Send for Bitmap<T>
where T: Send,

§

impl<T> Sync for Bitmap<T>
where T: Sync,

§

impl<T> Unpin for Bitmap<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Bitmap<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Bitmap<T>
where T: UnwindSafe,

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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.