Files
bit-set/src/block.rs

51 lines
1.0 KiB
Rust

use std::mem;
use std::ops::*;
use std::hash::Hash;
use std::convert::From;
pub trait BitBlock
: Copy
+ Add<Self, Output = Self>
+ Div<Self, Output = Self>
+ From<u8>
+ Sub<Self, Output = Self>
+ Shl<usize, Output = Self>
+ Shr<usize, Output = Self>
+ Not<Output = Self>
+ BitAnd<Self, Output = Self>
+ BitOr<Self, Output = Self>
+ BitXor<Self, Output = Self>
+ Rem<Self, Output = Self>
+ Eq
+ Ord
+ Hash {
const ZERO: Self;
const ONE: Self;
fn bits() -> u8;
fn count_ones(self) -> usize;
}
macro_rules! bit_block_impl {
($(($t: ty, $size: expr)),*) => ($(
impl BitBlock for $t {
const ZERO: Self = 0;
const ONE: Self = 1;
#[inline]
fn bits() -> u8 { $size }
#[inline]
fn count_ones(self) -> usize { self.count_ones() as usize }
}
)*)
}
bit_block_impl!{
(u8, 8),
(u16, 16),
(u32, 32),
(u64, 64),
(usize, (mem::size_of::<usize>() * 8) as u8)
}