Chunk

Trait Chunk 

Source
pub trait Chunk:
    Copy
    + Eq
    + Not<Output = Self>
    + BitAnd<Output = Self>
    + BitOr<Output = Self>
    + BitXor<Output = Self>
    + 'static {
    const BITS: u32;
    const ZERO: Self;

    // Required methods
    fn mask(bit: u32) -> Self;
    fn count_ones(self) -> u32;
    fn highest_bit(self) -> u32;
    fn lowest_bit(self) -> u32;
    fn remove_lowest_bit(&mut self);
}
Expand description

用于按位压缩存储一些布尔值。

Required Associated Constants§

Source

const BITS: u32

能存储的位数

必须是二的幂。

Source

const ZERO: Self

assert_eq!(u64::ZERO.count_ones(), 0);

Required Methods§

Source

fn mask(bit: u32) -> Self

单个位

要求输入 bit 小于 Chunk::BITS;输出仅有一位且尾随 bit 个零。

let bit = 5;
let mask = u64::mask(bit);
assert_eq!(mask.count_ones(), 1);
assert_eq!(mask.highest_bit(), bit);
assert_eq!(mask.lowest_bit(), bit);
Source

fn count_ones(self) -> u32

计算位的数量

Source

fn highest_bit(self) -> u32

最前一个 bit

Source

fn lowest_bit(self) -> u32

最后一个 bit

Source

fn remove_lowest_bit(&mut self)

删除最后一位

let c = 15532u64;
let mut c2 = c;
c2.remove_lowest_bit();
assert_eq!(c2, c ^ u64::mask(c.trailing_zeros()));
assert_eq!(c2.count_ones(), c.count_ones() - 1);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Chunk for u32

Source§

const BITS: u32 = 32u32

Source§

const ZERO: Self = 0u32

Source§

fn mask(bit: u32) -> Self

Source§

fn count_ones(self) -> u32

Source§

fn highest_bit(self) -> u32

Source§

fn lowest_bit(self) -> u32

Source§

fn remove_lowest_bit(&mut self)

Source§

impl Chunk for u64

Source§

const BITS: u32 = 64u32

Source§

const ZERO: Self = 0u64

Source§

fn mask(bit: u32) -> Self

Source§

fn count_ones(self) -> u32

Source§

fn highest_bit(self) -> u32

Source§

fn lowest_bit(self) -> u32

Source§

fn remove_lowest_bit(&mut self)

Source§

impl Chunk for u128

Source§

const BITS: u32 = 128u32

Source§

const ZERO: Self = 0u128

Source§

fn mask(bit: u32) -> Self

Source§

fn count_ones(self) -> u32

Source§

fn highest_bit(self) -> u32

Source§

fn lowest_bit(self) -> u32

Source§

fn remove_lowest_bit(&mut self)

Source§

impl Chunk for usize

Source§

const BITS: u32 = 32u32

Source§

const ZERO: Self = 0usize

Source§

fn mask(bit: u32) -> Self

Source§

fn count_ones(self) -> u32

Source§

fn highest_bit(self) -> u32

Source§

fn lowest_bit(self) -> u32

Source§

fn remove_lowest_bit(&mut self)

Implementors§