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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
use {Bits, BitsMut, BitSliceable, BlockType};
use iter::BlockIter;
use range_compat::*;
#[derive(Copy, Clone, Debug)]
pub struct BitSliceAdapter<T> {
bits: T,
start: u64,
len: u64,
}
impl<T: Bits> BitSliceAdapter<T> {
pub fn new(bits: T, start: u64, len: u64) -> Self {
assert!( start + len <= bits.bit_len(),
"BitSliceAdapter::new: out of bounds");
BitSliceAdapter { bits, start, len }
}
fn reslice(self, start: u64, len: u64) -> Self {
assert!( start + len <= self.bit_len(),
"BitSliceAdapter::reslice: out of bounds" );
BitSliceAdapter {
bits: self.bits,
start: self.start + start,
len,
}
}
fn reslice_ref(&self, start: u64, len: u64) -> BitSliceAdapter<&T> {
assert!( start + len <= self.bit_len(),
"BitSliceAdapter::reslice: out of bounds" );
BitSliceAdapter {
bits: &self.bits,
start: self.start + start,
len,
}
}
}
impl<T, U> PartialEq<U> for BitSliceAdapter<T>
where T: Bits,
U: Bits<Block = T::Block> {
fn eq(&self, other: &U) -> bool {
BlockIter::new(self) == BlockIter::new(other)
}
}
macro_rules! impl_bit_sliceable_adapter {
(
$(
impl[ $($param:tt)* ] BitSliceable for $target:ty ;
)+
) => {
$(
impl<$($param)*> ::BitSliceable<::std::ops::Range<u64>> for $target {
type Slice = ::adapter::BitSliceAdapter<Self>;
fn bit_slice(self, range: ::std::ops::Range<u64>) -> Self::Slice {
assert!( range.start <= range.end,
format!("{}::slice: bad range", stringify!($target)) );
::adapter::BitSliceAdapter::new(self, range.start, range.end - range.start)
}
}
impl<$($param)*> ::BitSliceable<::std::ops::RangeFrom<u64>> for $target {
type Slice = ::adapter::BitSliceAdapter<Self>;
fn bit_slice(self, range: ::std::ops::RangeFrom<u64>) -> Self::Slice {
let len = self.bit_len();
self.bit_slice(range.start .. len)
}
}
impl<$($param)*> ::BitSliceable<::std::ops::RangeTo<u64>> for $target {
type Slice = ::adapter::BitSliceAdapter<Self>;
fn bit_slice(self, range: ::std::ops::RangeTo<u64>) -> Self::Slice {
::adapter::BitSliceAdapter::new(self, 0, range.end)
}
}
impl<$($param)*> ::BitSliceable<::std::ops::RangeFull> for $target {
type Slice = ::adapter::BitSliceAdapter<Self>;
fn bit_slice(self, _range: ::std::ops::RangeFull) -> Self::Slice {
let len = self.bit_len();
::adapter::BitSliceAdapter::new(self, 0, len)
}
}
#[cfg(inclusive_range)]
impl<$($param)*> ::BitSliceable<::std::ops::RangeInclusive<u64>> for $target {
type Slice = ::adapter::BitSliceAdapter<Self>;
fn bit_slice(self, range: ::std::ops::RangeInclusive<u64>) -> Self::Slice {
let (start, end) = ::range_compat::get_inclusive_bounds(range)
.expect("BitSliceable::bit_slice: bad inclusive range");
::adapter::BitSliceAdapter::new(self, start, end - start + 1)
}
}
#[cfg(inclusive_range)]
impl<$($param)*> ::BitSliceable<::std::ops::RangeToInclusive<u64>> for $target {
type Slice = ::adapter::BitSliceAdapter<Self>;
fn bit_slice(self, range: ::std::ops::RangeToInclusive<u64>) -> Self::Slice {
::adapter::BitSliceAdapter::new(self, 0, range.end + 1)
}
}
)+
};
}
impl<T: Bits> Bits for BitSliceAdapter<T> {
type Block = T::Block;
fn bit_len(&self) -> u64 {
self.len
}
fn get_bit(&self, position: u64) -> bool {
assert!( position < self.bit_len(),
"BitSliceAdapter::get_bit: out of bounds" );
self.bits.get_bit(self.start + position)
}
fn get_block(&self, position: usize) -> Self::Block {
assert!( position < self.block_len(),
"BitSliceAdapter::get_block: out of bounds" );
let (real_start, real_len) =
get_block_addr::<T::Block>(self.start, self.len, position);
self.bits.get_bits(real_start, real_len)
}
fn get_bits(&self, start: u64, count: usize) -> Self::Block {
assert!( start + count as u64 <= self.bit_len(),
"BitSliceAdapter::get_bits: out of bounds" );
self.bits.get_bits(self.start + start, count)
}
}
impl<T: BitsMut> BitsMut for BitSliceAdapter<T> {
fn set_bit(&mut self, position: u64, value: bool) {
assert!( position < self.bit_len(),
"BitSliceAdapter::set_bit: out of bounds" );
self.bits.set_bit(self.start + position, value);
}
fn set_block(&mut self, position: usize, value: Self::Block) {
assert!( position < self.block_len(),
"BitSliceAdapter::get_block: out of bounds" );
let (real_start, real_len) =
get_block_addr::<T::Block>(self.start, self.len, position);
self.bits.set_bits(real_start, real_len, value);
}
fn set_bits(&mut self, start: u64, count: usize, value: Self::Block) {
assert!( start + count as u64 <= self.bit_len(),
"BitSliceAdapter::set_bits: out of bounds" );
self.bits.set_bits(self.start + start, count, value);
}
}
fn get_block_addr<Block: BlockType>(start: u64, len: u64, position: usize)
-> (u64, usize) {
let real_start = start + Block::mul_nbits(position);
let block_size = Block::nbits() as u64;
let real_len = if real_start + block_size < start + len {
block_size
} else {
(start + len - real_start)
};
(real_start, real_len as usize)
}
impl_index_from_bits! {
impl[T: Bits] Index<u64> for BitSliceAdapter<T>;
}
impl<T: Bits> BitSliceable<Range<u64>> for BitSliceAdapter<T> {
type Slice = Self;
fn bit_slice(self, range: Range<u64>) -> Self::Slice {
assert!( range.start <= range.end,
"BitSliceAdapter::bit_slice: bad range" );
self.reslice(range.start, range.end - range.start)
}
}
impl<T: Bits> BitSliceable<RangeTo<u64>> for BitSliceAdapter<T> {
type Slice = Self;
fn bit_slice(self, range: RangeTo<u64>) -> Self::Slice {
self.reslice(0, range.end)
}
}
impl<T: Bits> BitSliceable<RangeFrom<u64>> for BitSliceAdapter<T> {
type Slice = Self;
fn bit_slice(self, range: RangeFrom<u64>) -> Self::Slice {
let len = self.bit_len();
assert!( range.start <= len,
"BitSliceAdapter::bit_slice: out of bounds" );
self.reslice(range.start, len - range.start)
}
}
impl<T: Bits> BitSliceable<RangeFull> for BitSliceAdapter<T> {
type Slice = Self;
fn bit_slice(self, _range: RangeFull) -> Self::Slice {
self
}
}
#[cfg(inclusive_range)]
impl<T: Bits> BitSliceable<RangeInclusive<u64>> for BitSliceAdapter<T> {
type Slice = Self;
fn bit_slice(self, range: RangeInclusive<u64>) -> Self::Slice {
let (start, limit) = get_inclusive_bounds(range)
.expect("BitSliceAdapter::bit_slice: bad range");
self.reslice(start, limit - start + 1)
}
}
#[cfg(inclusive_range)]
impl<T: Bits> BitSliceable<RangeToInclusive<u64>> for BitSliceAdapter<T> {
type Slice = Self;
fn bit_slice(self, range: RangeToInclusive<u64>) -> Self::Slice {
self.reslice(0, range.end + 1)
}
}
impl<'a, T: Bits> BitSliceable<Range<u64>> for &'a BitSliceAdapter<T> {
type Slice = BitSliceAdapter<&'a T>;
fn bit_slice(self, range: Range<u64>) -> Self::Slice {
assert!( range.start <= range.end,
"BitSliceAdapter::bit_slice: bad range" );
self.reslice_ref(range.start, range.end - range.start)
}
}
impl<'a, T: Bits> BitSliceable<RangeTo<u64>> for &'a BitSliceAdapter<T> {
type Slice = BitSliceAdapter<&'a T>;
fn bit_slice(self, range: RangeTo<u64>) -> Self::Slice {
self.reslice_ref(0, range.end)
}
}
impl<'a, T: Bits> BitSliceable<RangeFrom<u64>> for &'a BitSliceAdapter<T> {
type Slice = BitSliceAdapter<&'a T>;
fn bit_slice(self, range: RangeFrom<u64>) -> Self::Slice {
let len = self.bit_len();
assert!( range.start <= len,
"BitSliceAdapter::bit_slice: out of bounds" );
self.reslice_ref(range.start, len - range.start)
}
}
impl<'a, T: Bits> BitSliceable<RangeFull> for &'a BitSliceAdapter<T> {
type Slice = BitSliceAdapter<&'a T>;
fn bit_slice(self, _range: RangeFull) -> Self::Slice {
self.reslice_ref(0, self.bit_len())
}
}
#[cfg(inclusive_range)]
impl<'a, T: Bits> BitSliceable<RangeInclusive<u64>> for &'a BitSliceAdapter<T> {
type Slice = BitSliceAdapter<&'a T>;
fn bit_slice(self, range: RangeInclusive<u64>) -> Self::Slice {
let (start, limit) = get_inclusive_bounds(range)
.expect("BitSliceAdapter::bit_slice: bad range");
self.reslice_ref(start, limit - start + 1)
}
}
#[cfg(inclusive_range)]
impl<'a, T: Bits> BitSliceable<RangeToInclusive<u64>> for &'a BitSliceAdapter<T> {
type Slice = BitSliceAdapter<&'a T>;
fn bit_slice(self, range: RangeToInclusive<u64>) -> Self::Slice {
self.reslice_ref(0, range.end + 1)
}
}