-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbit_vec.rs
88 lines (76 loc) · 2.85 KB
/
bit_vec.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
pub trait BitVec: Sized + Copy + Clone + Eq + PartialEq {
fn empty() -> Self;
// match sub bit vector start at offset and it's length is bits.
fn sub_equal(&self, offset: u32, bits: u32, other: &Self) -> bool;
// extract a sub vector start at offset and it's length is bits.
fn extract_bits(&self, offset: u32, bits: u32) -> Self;
// find the left most significant bit position of mismatch sub vec start at offset.
// start at 0..31 or 0..63 or 0..128
fn mismatch(&self, offset: u32, other: &Self) -> u32;
fn safe_to_usize(&self) -> usize;
fn from_bit_str(_: &str) -> Self;
fn is_empty(&self) -> bool;
}
const fn bit_size_of<T>() -> usize {
std::mem::size_of::<T>() * 8
}
macro_rules! bit_vec_impl {
($T:ty) => {
impl BitVec for $T {
#[inline(always)]
fn empty() -> $T {
0
}
// match sub bit vector start at offset and it's length is bits.
#[inline]
fn sub_equal(&self, offset: u32, mut bits: u32, other: &$T) -> bool {
if bits == 0 || offset as usize >= bit_size_of::<$T>() {
return true;
}
bits = if bits as usize > bit_size_of::<$T>() {
bit_size_of::<$T>() as u32
} else {
bits
};
((other ^ self) << offset >> (bit_size_of::<$T>() - bits as usize)) == 0
}
// extract a sub vector start at offset and it's length is bits.
#[inline]
fn extract_bits(&self, offset: u32, bits: u32) -> $T {
if (offset as usize) < bit_size_of::<$T>() {
return self << offset >> (bit_size_of::<$T>() - bits as usize);
}
0
}
// find the left most significant bit position of mismatch sub vec start at offset.
// start at 0..31 or 0..63
#[inline]
fn mismatch(&self, offset: u32, other: &$T) -> u32 {
<$T>::leading_zeros((other ^ self) << offset >> offset)
}
#[inline(always)]
fn safe_to_usize(&self) -> usize {
*self as usize
}
#[inline]
fn from_bit_str(value: &str) -> Self {
let mut data: $T = 0;
let len = value.len();
for (i, c) in value.chars().enumerate() {
if c == '1' {
data |= 1 << (bit_size_of::<$T>() - 1 - i);
}
}
data |= 1 << (bit_size_of::<$T>() - 1 - len);
data
}
#[inline(always)]
fn is_empty(&self) -> bool {
*self == 0
}
}
};
}
bit_vec_impl!(u32);
bit_vec_impl!(u64);
bit_vec_impl!(u128);