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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use {Bits, BitsMut, BitsPush};
use BlockType;
use iter::BlockIter;
use std::marker::PhantomData;
use std::ops;
#[derive(Debug, Clone)]
pub struct BoolAdapter<Block, T> {
bits: T,
_marker: PhantomData<Block>,
}
impl<Block: BlockType, T> BoolAdapter<Block, T> {
pub fn new(bits: T) -> Self {
BoolAdapter {
bits,
_marker: PhantomData,
}
}
pub fn into_inner(self) -> T {
self.bits
}
}
impl<Block, T> ops::Deref for BoolAdapter<Block, T> {
type Target = T;
fn deref(&self) -> &T {
&self.bits
}
}
impl<Block, T> ops::DerefMut for BoolAdapter<Block, T> {
fn deref_mut(&mut self) -> &mut T {
&mut self.bits
}
}
macro_rules! impl_for_bool_adapter {
() => {};
(
impl[$($param:tt)*] Bits for BoolAdapter<$block:ty, $target:ty>;
$( $rest:tt )*
) => {
impl<$($param)*> Bits for BoolAdapter<$block, $target> {
type Block = $block;
fn bit_len(&self) -> u64 {
self.bits.len() as u64
}
fn get_bit(&self, position: u64) -> bool {
self.bits[position as usize]
}
}
impl_for_bool_adapter! { $($rest)* }
};
(
impl[$($param:tt)*] BitsMut for BoolAdapter<$block:ty, $target:ty>;
$( $rest:tt )*
) => {
impl<$($param)*> BitsMut for BoolAdapter<$block, $target> {
fn set_bit(&mut self, position: u64, value: bool) {
self.bits[position as usize] = value
}
}
impl_for_bool_adapter! { $($rest)* }
};
(
impl[$($param:tt)*] BitsPush for BoolAdapter<$block:ty, $target:ty>;
$( $rest:tt )*
) => {
impl<$($param)*> BitsPush for BoolAdapter<$block, $target> {
fn push_bit(&mut self, value: bool) {
self.bits.push(value);
}
fn pop_bit(&mut self) -> Option<bool> {
self.bits.pop()
}
}
impl_for_bool_adapter! { $($rest)* }
};
}
impl_for_bool_adapter! {
impl[ Block: BlockType] Bits for BoolAdapter<Block, Vec<bool>>;
impl[ Block: BlockType] BitsMut for BoolAdapter<Block, Vec<bool>>;
impl[ Block: BlockType] BitsPush for BoolAdapter<Block, Vec<bool>>;
impl['a, Block: BlockType] Bits for BoolAdapter<Block, &'a mut Vec<bool>>;
impl['a, Block: BlockType] BitsMut for BoolAdapter<Block, &'a mut Vec<bool>>;
impl['a, Block: BlockType] BitsPush for BoolAdapter<Block, &'a mut Vec<bool>>;
impl['a, Block: BlockType] Bits for BoolAdapter<Block, &'a mut [bool]>;
impl['a, Block: BlockType] BitsMut for BoolAdapter<Block, &'a mut [bool]>;
impl['a, Block: BlockType] Bits for BoolAdapter<Block, &'a [bool]>;
}
impl<Block, T, U> PartialEq<U> for BoolAdapter<Block, T>
where Block: BlockType,
U: Bits<Block = Block>,
Self: Bits<Block = Block> {
fn eq(&self, other: &U) -> bool {
BlockIter::new(self) == BlockIter::new(other)
}
}