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>
impl<T: BitStorage> Bitmap<T>
Sourcepub fn new_empty() -> Self
pub fn new_empty() -> Self
创建一个所有位都为 0 的空位图。
§示例
let bitmap = Bitmap::<u16>::new_empty();
assert!(bitmap.none());
assert_eq!(bitmap.count_ones(), 0);Sourcepub fn new_full() -> Self
pub fn new_full() -> Self
创建一个所有位都为 1 的全满位图。
§示例
let bitmap = Bitmap::<u8>::new_full();
assert!(bitmap.all());
assert_eq!(bitmap.count_ones(), 8);Sourcepub const fn iter_ones(&self) -> PositiveIter<T> ⓘ
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]);Sourcepub const fn iter_zeros(&self) -> NegativeIter<T> ⓘ
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]);Sourcepub fn is_one_on(&self, idx: usize) -> bool
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));Sourcepub fn is_zero_on(&self, idx: usize) -> bool
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));Sourcepub fn merge(self, rhs: Bitmap<T>) -> Bitmap<T>
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));Sourcepub fn count_ones(&self) -> u32
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);Sourcepub fn count_zeros(&self) -> u32
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);Sourcepub fn any(&self) -> bool
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());Sourcepub fn all(&self) -> bool
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());Trait Implementations§
Source§impl<T: BitStorage> BitAnd for Bitmap<T>
impl<T: BitStorage> BitAnd for Bitmap<T>
Source§fn bitand(self, rhs: Self) -> Self::Output
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§impl<T: BitStorage> BitAndAssign for Bitmap<T>
impl<T: BitStorage> BitAndAssign for Bitmap<T>
Source§fn bitand_assign(&mut self, rhs: Self)
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>
impl<T: BitStorage> BitOr for Bitmap<T>
Source§fn bitor(self, rhs: Self) -> Self::Output
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§impl<T: BitStorage> BitOrAssign for Bitmap<T>
impl<T: BitStorage> BitOrAssign for Bitmap<T>
Source§fn bitor_assign(&mut self, rhs: Self)
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>
impl<T: BitStorage> BitXor for Bitmap<T>
Source§fn bitxor(self, rhs: Self) -> Self::Output
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§impl<T: BitStorage> BitXorAssign for Bitmap<T>
impl<T: BitStorage> BitXorAssign for Bitmap<T>
Source§fn bitxor_assign(&mut self, rhs: Self)
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);impl<T: Copy + BitStorage> Copy for Bitmap<T>
impl<T: Eq + BitStorage> Eq for Bitmap<T>
Source§impl<T: BitStorage> From<T> for Bitmap<T>
impl<T: BitStorage> From<T> for Bitmap<T>
Source§impl<T: BitStorage> IntoIterator for &Bitmap<T>
impl<T: BitStorage> IntoIterator for &Bitmap<T>
Source§impl<T: BitStorage> IntoIterator for Bitmap<T>
impl<T: BitStorage> IntoIterator for Bitmap<T>
Source§impl<T: BitStorage> Not for Bitmap<T>
impl<T: BitStorage> Not for Bitmap<T>
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> 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
Mutably borrows from an owned value. Read more