deepmesa_collections/bitvec/bitslice.rs
1/*
2 BitVector: A fast contiguous growable array of bits allocated
3 on the heap that allows storing and manipulating an arbitrary
4 number of bits. This collection is backed by a Vector<u8> which
5 manages the underlying memory.
6
7 Copyright 2021 "Rahul Singh <rsingh@arrsingh.com>"
8
9 Licensed under the Apache License, Version 2.0 (the "License");
10 you may not use this file except in compliance with the License.
11 You may obtain a copy of the License at
12
13 http://www.apache.org/licenses/LICENSE-2.0
14
15 Unless required by applicable law or agreed to in writing, software
16 distributed under the License is distributed on an "AS IS" BASIS,
17 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 See the License for the specific language governing permissions and
19 limitations under the License.
20*/
21
22use crate::bitvec::{
23 bitops,
24 bitref::{BitRef, BitRefMut},
25 bitvec::BitVector,
26 bytes,
27 iter::{Iter, IterMut, IterOnes, IterU128, IterU16, IterU32, IterU64, IterU8, IterZeros},
28 traits::{
29 AsMsb0, BitwiseClearAssign, BitwiseLsbAssign, BitwiseMsbAssign, BitwisePartialAssign,
30 NotLsbAssign, NotMsbAssign, NotPartialAssign,
31 },
32 BitCount, BitOrder,
33};
34use core::convert::TryFrom;
35use core::fmt;
36use core::fmt::Debug;
37use core::ops::BitAndAssign;
38use core::ops::BitOrAssign;
39use core::ops::BitXorAssign;
40use core::ops::Index;
41use core::ops::IndexMut;
42use core::ops::Not;
43use core::ops::Range;
44use core::ops::RangeFrom;
45use core::ops::RangeFull;
46use core::ops::RangeInclusive;
47use core::ops::RangeTo;
48use core::ops::RangeToInclusive;
49
50/// A slice of bits backed by a [`BitVector`](../struct.BitVector.html).
51///
52/// The `BitSlice` is an unsized type and is a view into a range
53/// within a [`BitVector`](BitVector). A [`BitVector`](BitVector) can
54/// produce mutable or immutable slices which in turn can be
55/// subsliced.
56///
57/// The `BitSlice` is a wrapper around `[u8]` and borrows memory via
58/// reference types `&BitSlice` and `&mut BitSlice`. The memory in a
59/// `BitSlice` is owned and managed by the underlying
60/// [`BitVector`](BitVector).
61///
62/// # Examples
63/// ```
64/// use deepmesa_collections::BitVector;
65///
66/// let mut bv = BitVector::with_capacity(20);
67/// bv.push_u8(0b1011_0011, None);
68/// bv.push_u8(0b1011_0011, None);
69///
70/// let slice = &bv[0..16];
71///
72/// assert_eq!(slice.len(), 16);
73/// assert_eq!(slice[0], true);
74/// assert_eq!(slice[1], false);
75///
76/// let slice_mut = &mut bv[9..11];
77/// assert_eq!(slice_mut[0], false);
78/// slice_mut.set(0, true);
79/// assert_eq!(slice_mut[0], true);
80///
81/// ```
82#[repr(transparent)]
83pub struct BitSlice([u8]);
84
85impl Debug for BitSlice {
86 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87 let offset = self.offset();
88 write!(
89 f,
90 "BitSlice {{ bit_len: {}, offset: {}, bits:\n",
91 self.len(),
92 offset,
93 )?;
94
95 let len = self.len();
96 write!(f, "{{")?;
97 if len > 0 {
98 let sb_idx = 0;
99 let eb_idx = (len - 1) / 8;
100 let mut count = 0;
101 for i in sb_idx..=eb_idx {
102 if count % 4 == 0 {
103 write!(f, "\n ")?;
104 }
105
106 write!(f, "{:08b} ", self.0[i])?;
107 count += 1;
108 }
109 }
110 write!(f, "\n}}}}")?;
111 Ok(())
112 }
113}
114
115macro_rules! iter_unsigned {
116 (
117 $(#[$outer:meta])*
118 $iter_fn: ident, $iter_type: ident
119 ) => {
120 $(#[$outer])*
121 pub fn $iter_fn(&self) -> $iter_type {
122 $iter_type::new(&self.0, self.offset(), self.len())
123 }
124 };
125}
126
127macro_rules! read_unsigned {
128 (
129 $(#[$outer:meta])*
130 $u_type:ty, $max_bits: literal, $read_fn: ident
131 ) => {
132 $(#[$outer])*
133 pub fn $read_fn(&self, start: usize) -> ($u_type, BitCount) {
134 let len = self.len();
135 start_bounds_check!(start, len);
136
137 let offset = self.offset();
138 let (val, bit_count) =
139 bytes::read_bits(&self.0, start + offset, len + offset, $max_bits, BitOrder::Lsb0);
140
141 (val as $u_type, bit_count)
142 }
143 };
144}
145
146macro_rules! read_bits_unsigned {
147 (
148 $(#[$outer:meta])*
149 $i:ident, $b: literal, $read_bits_fn: ident
150 ) => {
151 $(#[$outer])*
152 pub fn $read_bits_fn(&self, start: usize, max_bits: BitCount) -> ($i, BitCount) {
153 let len = self.len();
154 start_bounds_check!(start, len);
155
156 if max_bits > $b {
157 panic!(
158 "cannot read more than $b bits into a $i. max_bits={}",
159 max_bits
160 );
161 }
162 match max_bits {
163 0=> (0,0),
164 _ => {
165 let offset = self.offset();
166 let (val, bit_count) =
167 bytes::read_bits(&self.0, start + offset, len + offset, max_bits, BitOrder::Lsb0);
168 (val as $i, bit_count)
169 }
170 }
171 }
172 };
173}
174
175macro_rules! as_unsigned {
176 (
177 $(#[$outer:meta])*
178 $u_type:ty, $max_bits: literal, $as_fn: ident
179 ) => {
180 $(#[$outer])*
181 pub fn $as_fn(&self) -> ($u_type, BitCount) {
182 let len = self.len();
183 if len > $max_bits {
184 panic!(
185 concat!("len {} bits is too big to fit into a ", stringify!($i)),
186 len
187 );
188 }
189 let offset = self.offset();
190 let (val, count) = bytes::read_bits(&self.0, offset, len + offset, $max_bits, BitOrder::Lsb0);
191 (val as $u_type, count)
192 }
193 };
194}
195
196try_from_bitslice!(u8, 8);
197try_from_bitslice!(u16, 16);
198try_from_bitslice!(u32, 32);
199try_from_bitslice!(u64, 64);
200try_from_bitslice!(u128, 128);
201
202macro_rules! bitwise_partial_assign {
203 ($val: expr, $start:expr, $len: expr, $rhs: expr, &=) => {
204 $val.and_partial_assign($start, $len, $rhs);
205 };
206 ($val: expr, $start:expr, $len: expr, $rhs: expr, |=) => {
207 $val.or_partial_assign($start, $len, $rhs);
208 };
209 ($val: expr, $start:expr, $len: expr, $rhs: expr, ^=) => {
210 $val.xor_partial_assign($start, $len, $rhs);
211 };
212}
213
214macro_rules! bitwise_lsb_assign {
215 ($val: expr, $n:expr, $rhs: expr, &=) => {
216 $val.and_lsb_assign($n, $rhs);
217 };
218 ($val: expr, $n:expr, $rhs: expr, |=) => {
219 $val.or_lsb_assign($n, $rhs);
220 };
221 ($val: expr, $n:expr, $rhs: expr, ^=) => {
222 $val.xor_lsb_assign($n, $rhs);
223 };
224}
225
226macro_rules! bitwise_msb_assign {
227 ($val: expr, $n:expr, $rhs: expr, &=) => {
228 $val.and_msb_assign($n, $rhs);
229 };
230 ($val: expr, $n:expr, $rhs: expr, |=) => {
231 $val.or_msb_assign($n, $rhs);
232 };
233 ($val: expr, $n:expr, $rhs: expr, ^=) => {
234 $val.xor_msb_assign($n, $rhs);
235 };
236}
237
238macro_rules! impl_bitwise_assign_bitslice {
239 ($t_name: ident, $fn_name: ident, $for: ty, $op: tt) => {
240 impl $t_name<&BitSlice> for $for {
241 fn $fn_name(&mut self, rhs: &BitSlice) {
242 let len = self.len();
243 if len == 0 {
244 return;
245 }
246
247 let eb_idx = (len - 1) / 8;
248 let offset = self.offset();
249 let ss_start = 8 - offset;
250 if rhs.len() == 0 {
251 return;
252 }
253 let (rhs_fb, rhs_fb_r) = rhs.read_bits_u8(0, 8 - offset);
254
255 //Special Case the first byte
256 if rhs_fb_r < 8 - offset {
257 //left shift the rhs_fb to align the bits
258 let rhs_fb = rhs_fb << (8 - offset - rhs_fb_r);
259 bitwise_partial_assign!(&mut self.0[0], offset as u8, rhs_fb_r as u8, rhs_fb, $op);
260 return;
261 } else {
262 bitwise_partial_assign!(&mut self.0[0], offset as u8, rhs_fb_r as u8, rhs_fb, $op);
263 }
264 let ss = &rhs[ss_start..];
265 if ss.len() == 0 {
266 return;
267 }
268
269 let mut rhs_iter = ss.iter_u8();
270 let mut bits_processed = rhs_fb_r;
271 for i in 1..=eb_idx {
272 if let Some((rhs_byte, rhs_bits)) = rhs_iter.next() {
273 if rhs_bits == 8 {
274 let bits_rem = len - bits_processed;
275 if bits_rem >= 8 {
276 b_expr!(self.0[i] $op rhs_byte);
277 } else {
278 bitwise_msb_assign!(self.0[i], bits_rem as u8, rhs_byte, $op);
279 }
280 } else {
281 bitwise_msb_assign!(self.0[i], rhs_bits as u8, (rhs_byte).as_msb0(rhs_bits), $op);
282 }
283
284 bits_processed += rhs_bits;
285 } else {
286 return;
287 }
288 }
289 }
290 }
291 };
292}
293
294impl_bitwise_assign_bitslice!(BitAndAssign, bitand_assign, &mut BitSlice, &=);
295impl_bitwise_assign_bitslice!(BitOrAssign, bitor_assign, &mut BitSlice, |=);
296impl_bitwise_assign_bitslice!(BitXorAssign, bitxor_assign, &mut BitSlice, ^=);
297
298macro_rules! bitwise_no_op {
299 ($len:ident, $rhs: ident, &=) => {
300 if $len == 0 || $rhs {
301 return;
302 }
303 };
304 ($len: ident, $rhs: ident, |=) => {
305 if $len == 0 || !$rhs {
306 return;
307 }
308 };
309 ($len: ident, $rhs: ident, ^=) => {
310 if $len == 0 || !$rhs {
311 return;
312 }
313 };
314}
315
316macro_rules! impl_bitwise_assign_bool {
317 ($t_name: ident, $fn_name: ident, $for: ty, $op: tt) => {
318 impl $t_name<bool> for $for {
319 fn $fn_name(&mut self, rhs: bool) {
320 let len = self.len();
321 bitwise_no_op!(len, rhs, $op);
322
323 let offset = self.offset();
324 if len < 8 {
325 bitwise_partial_assign!(&mut self.0[0], offset as u8, len as u8, rhs, $op);
326 return;
327 }
328
329 let mut rhs_val: u8 = 0;
330 if rhs {
331 rhs_val = u8::MAX;
332 }
333
334 let eb_idx = (len - 1) / 8;
335 for i in 0..=eb_idx {
336 if i == 0 {
337 bitwise_lsb_assign!(&mut self.0[i], (8 - offset) as u8, rhs, $op);
338 } else if i == eb_idx {
339 bitwise_msb_assign!(&mut self.0[i], ((len % 8) + offset) as u8, rhs, $op);
340 } else {
341 b_expr!(self.0[i] $op rhs_val);
342 }
343 }
344 }
345 }
346 };
347}
348
349impl_bitwise_assign_bool!(BitAndAssign, bitand_assign, BitSlice, &=);
350impl_bitwise_assign_bool!(BitAndAssign, bitand_assign, &mut BitSlice, &=);
351
352impl_bitwise_assign_bool!(BitOrAssign, bitor_assign, BitSlice, |=);
353impl_bitwise_assign_bool!(BitOrAssign, bitor_assign, &mut BitSlice, |=);
354
355impl_bitwise_assign_bool!(BitXorAssign, bitxor_assign, BitSlice, ^=);
356impl_bitwise_assign_bool!(BitXorAssign, bitxor_assign, &mut BitSlice, ^=);
357
358impl Not for &mut BitSlice {
359 type Output = Self;
360 fn not(self) -> Self::Output {
361 let len = self.len();
362 if len == 0 {
363 return self;
364 }
365
366 let offset = self.offset();
367 if len < 8 {
368 self.0[0].not_partial_assign(offset as u8, len as u8);
369 return self;
370 }
371
372 let eb_idx = (len - 1) / 8;
373 for i in 0..=eb_idx {
374 if i == 0 {
375 self.0[i].not_lsb_assign((8 - offset) as u8);
376 } else if i == eb_idx {
377 self.0[i].not_msb_assign(((len % 8) + offset) as u8);
378 } else {
379 self.0[i] = !self.0[i];
380 }
381 }
382
383 self
384 }
385}
386
387impl BitSlice {
388 /// Returns the number of bits in the [`BitSlice`](BitSlice)
389 ///
390 /// # Examples
391 /// ```
392 /// use deepmesa_collections::BitVector;
393 ///
394 /// let mut bv = BitVector::with_capacity(22);
395 /// bv.push_u8(0b1001_1011, None);
396 /// let s = &bv[2..4];
397 /// assert_eq!(s.len(), 2);
398 /// ```
399 #[inline(always)]
400 pub fn len(&self) -> usize {
401 slice_unpack_len!(self.0.len())
402 }
403
404 pub fn is_empty(&self) -> bool {
405 self.len() == 0
406 }
407
408 pub fn repeat(&self, n: usize) -> BitVector {
409 let new_len = n * self.len();
410 //TODO: check that new_len isn't greater than max
411 // capacity. Ideally do the check in the BitVector Constructor
412 let mut bv = BitVector::with_capacity(new_len);
413 for _ in 0..n {
414 bv.extend_from_bitslice(self);
415 }
416 bv
417 }
418
419 pub fn set_all(&mut self, value: bool) {
420 self.fill(value);
421 }
422
423 /// Fills the slice with the specified bit.
424 ///
425 /// # Examples
426 /// ```
427 /// use deepmesa_collections::BitVector;
428 ///
429 /// let mut bv = BitVector::new();
430 /// bv.push_u8(0b1000_0001, None);
431 /// let s = &mut bv[1..7];
432 /// s.fill(true);
433 /// assert_eq!(bv.read_u8(0), (0b1111_1111, 8));
434 /// ```
435 pub fn fill(&mut self, bit: bool) {
436 if bit {
437 *self |= true;
438 } else {
439 *self &= false;
440 }
441 }
442
443 /// Returns a boolean value indicating whether the bit at the
444 /// specified index is set or `None` if the index is greater than
445 /// or equal to the number of bits in the slice.
446 ///
447 /// # Examples
448 /// ```
449 /// use deepmesa_collections::BitVector;
450 ///
451 /// let mut bv = BitVector::new();
452 /// bv.push_u8(0b1010_0011, None);
453 /// let s = &bv[1..7];
454 /// assert_eq!(s.get(0), Some(false));
455 /// assert_eq!(s.get(1), Some(true));
456 /// assert_eq!(s.get(7), None);
457 /// ```
458 pub fn get(&self, index: usize) -> Option<bool> {
459 if index >= self.len() {
460 return None;
461 }
462
463 let index = index + self.offset();
464 return Some(bit_at_unchecked!(index, self.0));
465 }
466
467 /// Returns a mutable reference to the bit at the specified index
468 /// or `None` if the index is greater than or equal to the number
469 /// of bits in the slice.
470 ///
471 /// # Examples
472 /// ```
473 /// use deepmesa_collections::BitVector;
474 ///
475 /// let mut bv = BitVector::with_capacity(20);
476 /// bv.push_u8(0b1011_1100, None);
477 /// assert_eq!(bv[0], true);
478 ///
479 /// let s = &mut bv[0..7];
480 /// *s.get_mut(0).unwrap() = false;
481 /// assert_eq!(bv[0], false);
482 /// ```
483 pub fn get_mut(&mut self, index: usize) -> Option<BitRefMut<bool>> {
484 if index >= self.len() {
485 return None;
486 }
487
488 let bit_idx = index + self.offset();
489 let bit = bit_at_unchecked!(bit_idx, self.0);
490
491 let byte_idx = bit_idx / 8;
492 let byte_ptr = self.0[byte_idx..byte_idx].as_mut_ptr();
493 return Some(BitRefMut::<bool>::new(bit, byte_ptr, bit_idx));
494 }
495
496 /// Returns true if any bit in the slice is set to `1` and false
497 /// otherwise.
498 ///
499 /// # Examples
500 /// ```
501 /// use deepmesa_collections::BitVector;
502 ///
503 /// let mut bv = BitVector::with_capacity(20);
504 /// bv.push_u8(0b1011_0000, None);
505 ///
506 /// let s = &bv[0..4];
507 /// assert_eq!(s.any(), true);
508 /// let s = &bv[4..8];
509 /// assert_eq!(s.any(), false);
510 /// ```
511 pub fn any(&self) -> bool {
512 if self.len() == 0 {
513 return false;
514 }
515 let mut iter = self.iter_u128();
516 while let Some((val, _)) = iter.next() {
517 if val > 0 {
518 return true;
519 }
520 }
521 return false;
522 }
523
524 /// Returns true if all the bits in the slice are set to `1` and
525 /// false otherwise.
526 ///
527 /// # Examples
528 /// ```
529 /// use deepmesa_collections::BitVector;
530 ///
531 /// let mut bv = BitVector::with_capacity(20);
532 /// bv.push_u8(0b1011_1111, None);
533 ///
534 /// let s = &bv[0..4];
535 /// assert_eq!(s.all(), false);
536 /// let s = &bv[4..8];
537 /// assert_eq!(s.all(), true);
538 /// ```
539 pub fn all(&self) -> bool {
540 if self.len() == 0 {
541 return true;
542 }
543 let mut iter = self.iter_u128();
544 while let Some((val, bit_count)) = iter.next() {
545 if bit_count == 128 {
546 if val != u128::MAX {
547 return false;
548 }
549 } else {
550 if val != (2u128.pow(bit_count as u32) - 1) {
551 return false;
552 }
553 }
554 }
555 return true;
556 }
557
558 /// Sets the bit at the specified index with the given bit
559 /// `value`.
560 ///
561 /// # Examples
562 /// ```
563 /// use deepmesa_collections::BitVector;
564 ///
565 /// let mut bv = BitVector::with_capacity(20);
566 /// bv.push_u8(0b1011_1111, None);
567 /// assert_eq!(bv.get(1), Some(false));
568 ///
569 /// let s = &mut bv[0..4];
570 /// s.set(1, true);
571 /// assert_eq!(bv.get(1), Some(true));
572 /// ```
573 pub fn set(&mut self, index: usize, value: bool) {
574 if index >= self.len() {
575 panic!(
576 "index out of bounds: the len is {} but the index is {}",
577 self.len(),
578 index
579 );
580 }
581
582 let index = index + self.offset();
583 set_unchecked!(index, value, &mut self.0);
584 }
585
586 /// Counts the number of bits from the start of the bitslice to
587 /// the first bit set to `0`.
588 ///
589 /// Returns `0` if the slice is empty.
590 ///
591 /// # Examples
592 /// ```
593 /// use deepmesa_collections::BitVector;
594 ///
595 /// let mut bv = BitVector::with_capacity(20);
596 /// bv.push_u8(0b1111_1000, Some(8));
597 /// bv.push_u8(0b0011_1101, Some(8));
598 ///
599 /// let s = &bv[2..2];
600 /// assert_eq!(s.leading_ones(), 0);
601 ///
602 /// let s = &bv[0..4];
603 /// assert_eq!(s.leading_ones(), 4);
604 ///
605 /// let s = &bv[8..];
606 /// assert_eq!(s.leading_ones(), 0);
607 ///
608 /// let s = &bv[10..];
609 /// assert_eq!(s.leading_ones(), 4);
610 /// ```
611 pub fn leading_ones(&self) -> usize {
612 let len = self.len();
613 match len {
614 0 => 0,
615 _ => {
616 let offset = self.offset();
617 bytes::leading_ones(&self.0, offset, len + offset)
618 }
619 }
620 }
621
622 /// Counts the number of bits from the start of the bitslice to
623 /// the first bit set to `1`.
624 ///
625 /// Returns `0` if the slice is empty.
626 ///
627 /// # Examples
628 /// ```
629 /// use deepmesa_collections::BitVector;
630 ///
631 /// let mut bv = BitVector::with_capacity(20);
632 /// bv.push_u8(0b0000_0111, Some(8));
633 /// bv.push_u8(0b1100_0010, Some(8));
634 ///
635 /// let s = &bv[2..2];
636 /// assert_eq!(s.leading_zeros(), 0);
637 ///
638 /// let s = &bv[0..4];
639 /// assert_eq!(s.leading_zeros(), 4);
640 ///
641 /// let s = &bv[8..];
642 /// assert_eq!(s.leading_zeros(), 0);
643 ///
644 /// let s = &bv[10..];
645 /// assert_eq!(s.leading_zeros(), 4);
646 /// ```
647 pub fn leading_zeros(&self) -> usize {
648 let len = self.len();
649 match len {
650 0 => 0,
651 _ => {
652 let offset = self.offset();
653 bytes::leading_zeros(&self.0, offset, len + offset)
654 }
655 }
656 }
657
658 /// Counts the number of bits from the end of the bitslice to the
659 /// last bit that is set to `0`.
660 ///
661 /// Returns 0 if the slice is empty.
662 ///
663 /// # Examples
664 /// ```
665 /// use deepmesa_collections::BitVector;
666 ///
667 /// let mut bv = BitVector::with_capacity(20);
668 /// bv.push_u8(0b0000_0111, Some(8));
669 /// bv.push_u8(0b1100_0111, Some(8));
670 ///
671 /// let s = &bv[2..2];
672 /// assert_eq!(s.trailing_ones(), 0);
673 ///
674 /// let s = &bv[0..4];
675 /// assert_eq!(s.trailing_ones(), 0);
676 ///
677 /// let s = &bv[4..10];
678 /// assert_eq!(s.trailing_ones(), 5);
679 ///
680 /// let s = &bv[10..];
681 /// assert_eq!(s.trailing_ones(), 3);
682 /// ```
683 pub fn trailing_ones(&self) -> usize {
684 let len = self.len();
685 match len {
686 0 => 0,
687 _ => {
688 let offset = self.offset();
689 bytes::trailing_ones(&self.0, offset, len + offset)
690 }
691 }
692 }
693
694 /// Counts the number of bits from the end of the bitslice to the
695 /// last bit that is set to `1`.
696 ///
697 /// Returns 0 if the slice is empty.
698 ///
699 /// # Examples
700 /// ```
701 /// use deepmesa_collections::BitVector;
702 ///
703 /// let mut bv = BitVector::with_capacity(20);
704 /// bv.push_u8(0b1111_1000, Some(8));
705 /// bv.push_u8(0b0011_1000, Some(8));
706 ///
707 /// let s = &bv[2..2];
708 /// assert_eq!(s.trailing_zeros(), 0);
709 ///
710 /// let s = &bv[0..4];
711 /// assert_eq!(s.trailing_zeros(), 0);
712 ///
713 /// let s = &bv[4..10];
714 /// assert_eq!(s.trailing_zeros(), 5);
715 ///
716 /// let s = &bv[10..];
717 /// assert_eq!(s.trailing_zeros(), 3);
718 /// ```
719 pub fn trailing_zeros(&self) -> usize {
720 let len = self.len();
721 match len {
722 0 => 0,
723 _ => {
724 let offset = self.offset();
725 bytes::trailing_zeros(&self.0, offset, len + offset)
726 }
727 }
728 }
729
730 /// Counts the bits in the slice that are set to `1`.
731 /// Returns 0 if the slice is empty.
732 ///
733 /// # Examples
734 /// ```
735 /// use deepmesa_collections::BitVector;
736 ///
737 /// let mut bv = BitVector::with_capacity(20);
738 /// bv.push_u8(0b1111_1000, Some(8));
739 /// bv.push_u8(0b0011_1000, Some(8));
740 ///
741 /// let s = &bv[2..2];
742 /// assert_eq!(s.count_ones(), 0);
743 ///
744 /// let s = &bv[0..4];
745 /// assert_eq!(s.count_ones(), 4);
746 ///
747 /// let s = &bv[4..10];
748 /// assert_eq!(s.count_ones(), 1);
749 ///
750 /// let s = &bv[10..];
751 /// assert_eq!(s.count_ones(), 3);
752 /// ```
753 pub fn count_ones(&self) -> usize {
754 let len = self.len();
755 match len {
756 0 => 0,
757 _ => {
758 let offset = self.offset();
759 bytes::count_ones(&self.0, offset, len + offset)
760 }
761 }
762 }
763
764 /// Counts the bits in the slice that are set to `0`.
765 /// Returns 0 if the slice is empty.
766 ///
767 /// # Examples
768 /// ```
769 /// use deepmesa_collections::BitVector;
770 ///
771 /// let mut bv = BitVector::with_capacity(20);
772 /// bv.push_u8(0b0000_0111, Some(8));
773 /// bv.push_u8(0b1100_0111, Some(8));
774 ///
775 /// let s = &bv[2..2];
776 /// assert_eq!(s.count_zeros(), 0);
777 ///
778 /// let s = &bv[0..4];
779 /// assert_eq!(s.count_zeros(), 4);
780 ///
781 /// let s = &bv[4..10];
782 /// assert_eq!(s.count_zeros(), 1);
783 ///
784 /// let s = &bv[10..];
785 /// assert_eq!(s.count_zeros(), 3);
786 /// ```
787 pub fn count_zeros(&self) -> usize {
788 let len = self.len();
789 match len {
790 0 => 0,
791 _ => {
792 let offset = self.offset();
793 bytes::count_zeros(&self.0, offset, len + offset)
794 }
795 }
796 }
797
798 /// Returns the index of the first bit in the `BitSlice` that is
799 /// set to `1`. Returns None if there are no bits set to `1` or if
800 /// the slice is empty.
801 ///
802 /// # Examples
803 /// ```
804 /// use deepmesa_collections::BitVector;
805 ///
806 /// let mut bv = BitVector::with_capacity(20);
807 /// bv.push_u8(0b0000_0111, Some(8));
808 /// bv.push_u8(0b1100_0111, Some(8));
809 ///
810 /// let s = &bv[2..2];
811 /// assert_eq!(s.first_one(), None);
812 ///
813 /// let s = &bv[0..4];
814 /// assert_eq!(s.first_one(), None);
815 ///
816 /// let s = &bv[4..10];
817 /// assert_eq!(s.first_one(), Some(1));
818 ///
819 /// let s = &bv[10..];
820 /// assert_eq!(s.first_one(), Some(3));
821 /// ```
822 pub fn first_one(&self) -> Option<usize> {
823 let len = self.len();
824 match len {
825 0 => None,
826 _ => {
827 let offset = self.offset();
828 match bytes::first_one(&self.0, offset, len + offset) {
829 None => None,
830 Some(idx) => Some(idx - offset),
831 }
832 }
833 }
834 }
835
836 /// Returns the index of the first bit in the `BitSlice` that is
837 /// set to `0`. Returns None if there are no bits set to `0` or if
838 /// the slice is empty.
839 ///
840 /// # Examples
841 /// ```
842 /// use deepmesa_collections::BitVector;
843 ///
844 /// let mut bv = BitVector::with_capacity(20);
845 /// bv.push_u8(0b1111_1000, Some(8));
846 /// bv.push_u8(0b0011_1000, Some(8));
847 ///
848 /// let s = &bv[2..2];
849 /// assert_eq!(s.first_zero(), None);
850 ///
851 /// let s = &bv[0..4];
852 /// assert_eq!(s.first_zero(), None);
853 ///
854 /// let s = &bv[4..10];
855 /// assert_eq!(s.first_zero(), Some(1));
856 ///
857 /// let s = &bv[10..];
858 /// assert_eq!(s.first_zero(), Some(3));
859 /// ```
860 pub fn first_zero(&self) -> Option<usize> {
861 let len = self.len();
862 match len {
863 0 => None,
864 _ => {
865 let offset = self.offset();
866 match bytes::first_zero(&self.0, offset, len + offset) {
867 None => None,
868 Some(idx) => Some(idx - offset),
869 }
870 }
871 }
872 }
873
874 /// Returns the index of the last bit in the `BitSlice` that is
875 /// set to `1`. Returns None if there are no bits set to `1` or if
876 /// the slice is empty.
877 ///
878 /// # Examples
879 /// ```
880 /// use deepmesa_collections::BitVector;
881 ///
882 /// let mut bv = BitVector::with_capacity(20);
883 /// bv.push_u8(0b0000_0111, Some(8));
884 /// bv.push_u8(0b1100_0110, Some(8));
885 ///
886 /// let s = &bv[2..2];
887 /// assert_eq!(s.last_one(), None);
888 ///
889 /// let s = &bv[0..4];
890 /// assert_eq!(s.last_one(), None);
891 ///
892 /// let s = &bv[4..10];
893 /// assert_eq!(s.last_one(), Some(5));
894 ///
895 /// let s = &bv[10..];
896 /// assert_eq!(s.last_one(), Some(4));
897 /// ```
898 pub fn last_one(&self) -> Option<usize> {
899 let len = self.len();
900 match len {
901 0 => None,
902 _ => {
903 let offset = self.offset();
904 match bytes::last_one(&self.0, offset, len + offset) {
905 None => None,
906 Some(idx) => Some(idx - offset),
907 }
908 }
909 }
910 }
911
912 /// Returns the index of the last bit in the `BitSlice` that is
913 /// set to `0`. Returns None if there are no bits set to `0` or if
914 /// the slice is empty.
915 ///
916 /// # Examples
917 /// ```
918 /// use deepmesa_collections::BitVector;
919 ///
920 /// let mut bv = BitVector::with_capacity(20);
921 /// bv.push_u8(0b1111_1000, Some(8));
922 /// bv.push_u8(0b0011_1001, Some(8));
923 ///
924 /// let s = &bv[2..2];
925 /// assert_eq!(s.last_zero(), None);
926 ///
927 /// let s = &bv[0..4];
928 /// assert_eq!(s.last_zero(), None);
929 ///
930 /// let s = &bv[4..10];
931 /// assert_eq!(s.last_zero(), Some(5));
932 ///
933 /// let s = &bv[10..];
934 /// assert_eq!(s.last_zero(), Some(4));
935 /// ```
936 pub fn last_zero(&self) -> Option<usize> {
937 let len = self.len();
938 match len {
939 0 => None,
940 _ => {
941 let offset = self.offset();
942 match bytes::last_zero(&self.0, offset, len + offset) {
943 None => None,
944 Some(idx) => Some(idx - offset),
945 }
946 }
947 }
948 }
949
950 /// Returns an immutable reference to the first bit in the
951 /// `BitSlice` or None if the `BitSlice` is empty.
952 ///
953 /// # Examples
954 /// ```
955 /// use deepmesa_collections::BitVector;
956 ///
957 /// let mut bv = BitVector::with_capacity(20);
958 /// bv.push_u8(0b0010_0100, Some(8));
959 ///
960 /// let s = &bv[2..2];
961 /// assert_eq!(s.first(), None);
962 ///
963 /// let s = &bv[2..5];
964 /// assert_eq!(s.first().as_deref(), Some(&true));
965 /// ```
966 pub fn first(&self) -> Option<BitRef<bool>> {
967 match self.len() {
968 0 => None,
969 _ => {
970 let bit = bit_at_unchecked!(self.offset(), self.0);
971 Some(BitRef::<bool>::new(bit))
972 }
973 }
974 }
975
976 /// Returns a mutable reference to the first bit in the
977 /// `BitSlice` or None if the `BitSlice` is empty.
978 ///
979 /// # Examples
980 /// ```
981 /// use deepmesa_collections::BitVector;
982 ///
983 /// let mut bv = BitVector::with_capacity(20);
984 /// bv.push_u8(0b0010_0100, Some(8));
985 ///
986 /// let s = &mut bv[2..2];
987 /// assert_eq!(s.first_mut(), None);
988 ///
989 /// let s = &mut bv[2..5];
990 /// assert_eq!(s.first_mut().as_deref(), Some(&true));
991 /// *s.first_mut().unwrap() = false;
992 /// assert_eq!(s.first_mut().as_deref(), Some(&false));
993 /// ```
994 pub fn first_mut(&mut self) -> Option<BitRefMut<bool>> {
995 match self.len() {
996 0 => None,
997 _ => {
998 let bit_idx = self.offset();
999 let bit = bit_at_unchecked!(bit_idx, self.0);
1000
1001 let byte_idx = bit_idx / 8;
1002 let byte_ptr = self.0[byte_idx..byte_idx].as_mut_ptr();
1003 return Some(BitRefMut::<bool>::new(bit, byte_ptr, bit_idx));
1004 }
1005 }
1006 }
1007
1008 /// Returns an immutable reference to the last bit in the
1009 /// `BitSlice` or None if the `BitSlice` is empty.
1010 ///
1011 /// # Examples
1012 /// ```
1013 /// use deepmesa_collections::BitVector;
1014 ///
1015 /// let mut bv = BitVector::with_capacity(20);
1016 /// bv.push_u8(0b0010_0100, Some(8));
1017 ///
1018 /// let s = &bv[2..2];
1019 /// assert_eq!(s.last(), None);
1020 ///
1021 /// let s = &bv[2..5];
1022 /// assert_eq!(s.last().as_deref(), Some(&false));
1023 /// ```
1024 pub fn last(&self) -> Option<BitRef<bool>> {
1025 match self.len() {
1026 0 => None,
1027 _ => {
1028 let bit = bit_at_unchecked!(self.len() - 1 + self.offset(), self.0);
1029 Some(BitRef::<bool>::new(bit))
1030 }
1031 }
1032 }
1033
1034 /// Returns a mutable reference to the first bit in the
1035 /// `BitSlice` or None if the `BitSlice` is empty.
1036 ///
1037 /// # Examples
1038 /// ```
1039 /// use deepmesa_collections::BitVector;
1040 ///
1041 /// let mut bv = BitVector::with_capacity(20);
1042 /// bv.push_u8(0b0010_0100, Some(8));
1043 ///
1044 /// let s = &mut bv[2..2];
1045 /// assert_eq!(s.last_mut(), None);
1046 ///
1047 /// let s = &mut bv[2..6];
1048 /// assert_eq!(s.last_mut().as_deref(), Some(&true));
1049 /// *s.last_mut().unwrap() = false;
1050 /// assert_eq!(s.last_mut().as_deref(), Some(&false));
1051 /// ```
1052 pub fn last_mut(&mut self) -> Option<BitRefMut<bool>> {
1053 match self.len() {
1054 0 => None,
1055 _ => {
1056 let bit_idx = self.len() - 1 + self.offset();
1057 let bit = bit_at_unchecked!(bit_idx, self.0);
1058
1059 let byte_idx = bit_idx / 8;
1060 let byte_ptr = self.0[byte_idx..byte_idx].as_mut_ptr();
1061 return Some(BitRefMut::<bool>::new(bit, byte_ptr, bit_idx));
1062 }
1063 }
1064 }
1065
1066 /// Iterates over all the bits in the `BitSlice` that are set to
1067 /// `1`.
1068 ///
1069 /// # Examples
1070 /// ```
1071 /// use deepmesa_collections::BitVector;
1072 ///
1073 /// let mut bv = BitVector::with_capacity(20);
1074 /// bv.push_u8(0b0010_0101, Some(8));
1075 ///
1076 /// let s = &bv[2..2];
1077 /// let mut iter = s.iter_ones();
1078 /// assert_eq!(iter.next(), None);
1079 ///
1080 /// let s = &bv[2..6];
1081 /// let mut iter = s.iter_ones();
1082 /// assert_eq!(iter.next(), Some(0));
1083 /// assert_eq!(iter.next(), Some(3));
1084 /// assert_eq!(iter.next(), None);
1085 /// ```
1086 pub fn iter_ones(&self) -> IterOnes {
1087 IterOnes::new(&self.0, self.offset(), self.len())
1088 }
1089
1090 /// Iterates over all the bits in the `BitSlice` that are set to
1091 /// `0`.
1092 ///
1093 /// # Examples
1094 /// ```
1095 /// use deepmesa_collections::BitVector;
1096 ///
1097 /// let mut bv = BitVector::with_capacity(20);
1098 /// bv.push_u8(0b1101_1010, Some(8));
1099 ///
1100 /// let s = &bv[2..2];
1101 /// let mut iter = s.iter_zeros();
1102 /// assert_eq!(iter.next(), None);
1103 ///
1104 /// let s = &bv[2..6];
1105 /// let mut iter = s.iter_zeros();
1106 /// assert_eq!(iter.next(), Some(0));
1107 /// assert_eq!(iter.next(), Some(3));
1108 /// assert_eq!(iter.next(), None);
1109 /// ```
1110 pub fn iter_zeros(&self) -> IterZeros {
1111 IterZeros::new(&self.0, self.offset(), self.len())
1112 }
1113
1114 /// Returns an iterator over the bits of this
1115 /// [`BitSlice`](BitSlice)
1116 ///
1117 /// # Examples
1118 /// ```
1119 /// use deepmesa_collections::BitVector;
1120 ///
1121 /// let mut bv = BitVector::new();
1122 /// bv.push_u8(0b1011_0011, None);
1123 ///
1124 /// let s = &bv[0..3];
1125 /// let mut iter = s.iter();
1126 /// assert_eq!(iter.next(), Some(true));
1127 /// assert_eq!(iter.next(), Some(false));
1128 /// assert_eq!(iter.next(), Some(true));
1129 /// assert_eq!(iter.next(), None);
1130 /// ```
1131 pub fn iter(&self) -> Iter {
1132 Iter::new(&self.0, self.offset(), self.len())
1133 }
1134
1135 /// Returns a mutable iterator that allows modifying the bits of
1136 /// this [`BitSlice`](BitSlice)
1137 ///
1138 /// # Examples
1139 /// ```
1140 /// use deepmesa_collections::BitVector;
1141 ///
1142 /// let mut bv = BitVector::with_capacity(20);
1143 /// bv.push_u8(0b1011_1100, None);
1144 /// assert_eq!(bv[0], true);
1145 ///
1146 /// let s = &mut bv[0..7];
1147 /// let iter = s.iter_mut();
1148 /// for mut bit in iter {
1149 /// *bit = true;
1150 /// }
1151 /// assert_eq!(bv.read_u8(0), (0b1111_1110, 8));
1152 /// ```
1153 pub fn iter_mut(&mut self) -> IterMut {
1154 let offset = self.offset();
1155 let len = self.len();
1156 IterMut::new(&mut self.0, offset, len)
1157 }
1158
1159 iter_unsigned!(
1160 /// Returns an iterator that iterates over the
1161 /// [`BitSlice`](BitSlice) 8 bits at a time. Each invocation
1162 /// of `iter.next` returns a u8 value and the number of bits
1163 /// read. The bits are read from the lower to the higher index
1164 /// from the slice and shifted right, so the bit at the lower
1165 /// index is the MSB of returned value while the bit at the
1166 /// highest index is the LSB.
1167 ///
1168 /// The iterator returns None if there are no more bits to
1169 /// return
1170 ///
1171 /// # Examples
1172 /// ```
1173 /// use deepmesa_collections::BitVector;
1174 /// let mut bv = BitVector::new();
1175 /// bv.push_u16(0b0101_1101_0011_1010, Some(16));
1176 ///
1177 /// let s = &bv[..];
1178 /// let mut iter = s.iter_u8();
1179 /// assert_eq!(iter.next(), Some((0b0101_1101, 8)));
1180 /// assert_eq!(iter.next(), Some((0b0011_1010, 8)));
1181 /// assert_eq!(iter.next(), None);
1182 /// ```
1183 iter_u8,
1184 IterU8
1185 );
1186 iter_unsigned!(
1187 /// Returns an iterator that iterates over the
1188 /// [`BitSlice`](BitSlice) 16 bits at a time. Each invocation
1189 /// of `iter.next` returns a u16 value and the number of bits
1190 /// read. The bits are read from the lower to the higher index
1191 /// from the slice and shifted right, so the bit at the lower
1192 /// index is the MSB of returned value while the bit at the
1193 /// highest index is the LSB.
1194 ///
1195 /// The iterator returns None if there are no more bits to
1196 /// return
1197 ///
1198 /// # Examples
1199 /// ```
1200 /// use deepmesa_collections::BitVector;
1201 /// let mut bv = BitVector::new();
1202 /// bv.push_u16(0b0101_1101_0011_1010, Some(16));
1203 ///
1204 /// let s = &bv[..];
1205 /// let mut iter = s.iter_u16();
1206 /// assert_eq!(iter.next(), Some((0b0101_1101_0011_1010, 16)));
1207 /// assert_eq!(iter.next(), None);
1208 /// ```
1209 iter_u16,
1210 IterU16
1211 );
1212 iter_unsigned!(
1213 /// Returns an iterator that iterates over the
1214 /// [`BitSlice`](BitSlice) 32 bits at a time. Each invocation
1215 /// of `iter.next` returns a u32 value and the number of bits
1216 /// read. The bits are read from the lower to the higher index
1217 /// from the slice and shifted right, so the bit at the lower
1218 /// index is the MSB of returned value while the bit at the
1219 /// highest index is the LSB.
1220 ///
1221 /// The iterator returns None if there are no more bits to
1222 /// return
1223 ///
1224 /// # Examples
1225 /// ```
1226 /// use deepmesa_collections::BitVector;
1227 /// let mut bv = BitVector::new();
1228 /// bv.push_u16(0b0101_1101_0011_1010, Some(16));
1229 /// bv.push_u16(0b1111_0011_1100_0000, Some(16));
1230 ///
1231 /// let s = &bv[..];
1232 /// let mut iter = s.iter_u32();
1233 /// assert_eq!(iter.next(), Some((0b0101_1101_0011_1010_1111_0011_1100_0000, 32)));
1234 /// assert_eq!(iter.next(), None);
1235 /// ```
1236 iter_u32,
1237 IterU32
1238 );
1239 iter_unsigned!(
1240 /// Returns an iterator that iterates over the
1241 /// [`BitSlice`](BitSlice) 64 bits at a time. Each invocation
1242 /// of `iter.next` returns a u64 value and the number of bits
1243 /// read. The bits are read from the lower to the higher index
1244 /// from the slice and shifted right, so the bit at the lower
1245 /// index is the MSB of returned value while the bit at the
1246 /// highest index is the LSB.
1247 ///
1248 /// The iterator returns None if there are no more bits to
1249 /// return
1250 ///
1251 /// # Examples
1252 /// ```
1253 /// use deepmesa_collections::BitVector;
1254 /// let mut bv = BitVector::new();
1255 /// bv.push_u64(u64::MAX, Some(64));
1256 ///
1257 /// let s = &bv[..];
1258 /// let mut iter = s.iter_u64();
1259 /// assert_eq!(iter.next(), Some((u64::MAX, 64)));
1260 /// assert_eq!(iter.next(), None);
1261 /// ```
1262 iter_u64,
1263 IterU64
1264 );
1265 iter_unsigned!(
1266 /// Returns an iterator that iterates over the
1267 /// [`BitSlice`](BitSlice) 128 bits at a time. Each invocation
1268 /// of `iter.next` returns a u128 value and the number of bits
1269 /// read. The bits are read from the lower to the higher index
1270 /// from the slice and shifted right, so the bit at the lower
1271 /// index is the MSB of returned value while the bit at the
1272 /// highest index is the LSB.
1273 ///
1274 /// The iterator returns None if there are no more bits to
1275 /// return
1276 ///
1277 /// # Examples
1278 /// ```
1279 /// use deepmesa_collections::BitVector;
1280 /// let mut bv = BitVector::new();
1281 /// bv.push_u64(u64::MAX, Some(64));
1282 /// bv.push_u64(u64::MAX, Some(64));
1283 ///
1284 /// let s = &bv[..];
1285 /// let mut iter = s.iter_u128();
1286 /// assert_eq!(iter.next(), Some((u128::MAX, 128)));
1287 /// assert_eq!(iter.next(), None);
1288 /// ```
1289 iter_u128,
1290 IterU128
1291 );
1292
1293 as_unsigned!(
1294 /// Returns the contents of this slice as a [`u8`]. This
1295 /// method will panic if the length of the slice is greater
1296 /// than or equal to 8. The bits are read from the lower to
1297 /// the higher index from the slice and shifted right, so the
1298 /// bit at the lower index is the MSB of returned value while
1299 /// the bit at the highest index is the LSB.
1300 ///
1301 /// Returns a [`u8`] value and the number of bits read as a
1302 /// tuple.
1303 ///
1304 /// # Examples
1305 /// ```
1306 /// use deepmesa_collections::BitVector;
1307 ///
1308 /// let mut bv = BitVector::new();
1309 /// bv.push_u8(0b0011_0110, Some(8));
1310 ///
1311 /// let s = &bv[..];
1312 /// assert_eq!(s.as_u8(), (0b0011_0110, 8));
1313 /// ```
1314 /// If a Result is preferred over a panic, then
1315 /// using the `TryFrom<&BitSlice>` trait may be used.
1316 ///
1317 /// # Example of `TryFrom<&BitSlice> for u8`
1318 ///
1319 /// ```
1320 /// use deepmesa_collections::BitVector;
1321 /// use core::convert::TryFrom;
1322 ///
1323 /// let mut bv = BitVector::new();
1324 /// bv.push_u8(0b0011_0110, Some(8));
1325 /// bv.push_u8(0b0011_0110, Some(8));
1326 ///
1327 /// let s = &bv[0..8];
1328 /// match u8::try_from(s) {
1329 /// Ok(val) => assert_eq!(val, 0b0011_0110),
1330 /// Err(e) => assert!(false, "{}", e),
1331 /// }
1332 /// ```
1333 u8,
1334 8,
1335 as_u8
1336 );
1337 as_unsigned!(
1338 /// Returns the contents of this slice as a [`u16`]. This
1339 /// method will panic if the length of the slice is greater
1340 /// than or equal to 16. The bits are read from the lower to
1341 /// the higher index from the slice and shifted right, so the
1342 /// bit at the lower index is the MSB of returned value while
1343 /// the bit at the highest index is the LSB.
1344 ///
1345 /// Returns a [`u16`] value and the number of bits read as a
1346 /// tuple.
1347 ///
1348 /// # Examples
1349 /// ```
1350 /// use deepmesa_collections::BitVector;
1351 ///
1352 /// let mut bv = BitVector::new();
1353 /// bv.push_u8(0b0011_0110, Some(8));
1354 /// bv.push_u8(0b0011_0110, Some(8));
1355 ///
1356 /// let s = &bv[..];
1357 /// assert_eq!(s.as_u16(), (0b0011_0110_0011_0110, 16));
1358 /// ```
1359 ///
1360 /// If a Result is preferred over a panic, then
1361 /// using the `TryFrom<&BitSlice>` trait may be used.
1362 ///
1363 /// # Example of `TryFrom<&BitSlice> for u16`
1364 ///
1365 /// ```
1366 /// use deepmesa_collections::BitVector;
1367 /// use core::convert::TryFrom;
1368 ///
1369 /// let mut bv = BitVector::new();
1370 /// bv.push_u8(0b0011_0110, Some(8));
1371 /// bv.push_u8(0b0011_0110, Some(8));
1372 ///
1373 /// let s = &bv[..];
1374 /// match u16::try_from(s) {
1375 /// Ok(val) => assert_eq!(val, 0b0011_0110_0011_0110),
1376 /// Err(e) => assert!(false, "{}", e),
1377 /// }
1378 /// ```
1379 u16,
1380 16,
1381 as_u16
1382 );
1383 as_unsigned!(
1384 /// Returns the contents of this slice as a [`u32`]. This
1385 /// method will panic if the length of the slice is greater
1386 /// than or equal to 32. The bits are read from the lower
1387 /// to the higher index from the slice and shifted right, so
1388 /// the bit at the lower index is the MSB of returned value
1389 /// while the bit at the highest index is the LSB.
1390 ///
1391 /// Returns a [`u32`] value and the number of bits read as a
1392 /// tuple.
1393 ///
1394 /// # Examples
1395 /// ```
1396 /// use deepmesa_collections::BitVector;
1397 ///
1398 /// let mut bv = BitVector::new();
1399 /// bv.push_u32(u32::MAX, Some(32));
1400 ///
1401 /// let s = &bv[..];
1402 /// assert_eq!(s.as_u32(), (u32::MAX, 32));
1403 /// ```
1404 ///
1405 /// If a Result is preferred over a panic, then
1406 /// using the `TryFrom<&BitSlice>` trait may be used.
1407 ///
1408 /// # Example of `TryFrom<&BitSlice> for u32`
1409 ///
1410 /// ```
1411 /// use deepmesa_collections::BitVector;
1412 /// use core::convert::TryFrom;
1413 ///
1414 /// let mut bv = BitVector::new();
1415 /// bv.push_u32(u32::MAX, Some(32));
1416 ///
1417 /// let s = &bv[..];
1418 /// match u32::try_from(s) {
1419 /// Ok(val) => assert_eq!(val, u32::MAX),
1420 /// Err(e) => assert!(false, "{}", e),
1421 /// }
1422 /// ```
1423 u32,
1424 32,
1425 as_u32
1426 );
1427 as_unsigned!(
1428 /// Returns the contents of this slice as a [`u64`]. This
1429 /// method will panic if the length of the slice is greater
1430 /// than or equal to 64. The bits are read from the lower
1431 /// to the higher index from the slice and shifted right, so
1432 /// the bit at the lower index is the MSB of returned value
1433 /// while the bit at the highest index is the LSB.
1434 ///
1435 /// Returns a [`u64`] value and the number of bits read as a
1436 /// tuple.
1437 ///
1438 /// # Examples
1439 /// ```
1440 /// use deepmesa_collections::BitVector;
1441 ///
1442 /// let mut bv = BitVector::new();
1443 /// bv.push_u64(u64::MAX, Some(64));
1444 ///
1445 /// let s = &bv[..];
1446 /// assert_eq!(s.as_u64(), (u64::MAX, 64));
1447 /// ```
1448 ///
1449 /// If a Result is preferred over a panic, then
1450 /// using the `TryFrom<&BitSlice>` trait may be used.
1451 ///
1452 /// # Example of `TryFrom<&BitSlice> for u64`
1453 ///
1454 /// ```
1455 /// use deepmesa_collections::BitVector;
1456 /// use core::convert::TryFrom;
1457 ///
1458 /// let mut bv = BitVector::new();
1459 /// bv.push_u64(u64::MAX, Some(64));
1460 ///
1461 /// let s = &bv[..];
1462 /// match u64::try_from(s) {
1463 /// Ok(val) => assert_eq!(val, u64::MAX),
1464 /// Err(e) => assert!(false, "{}", e),
1465 /// }
1466 /// ```
1467 u64,
1468 64,
1469 as_u64
1470 );
1471 as_unsigned!(
1472 /// Returns the contents of this slice as a [`u128`]. This
1473 /// method will panic if the length of the slice is greater
1474 /// than or equal to 128. The bits are read from the lower
1475 /// to the higher index from the slice and shifted right, so
1476 /// the bit at the lower index is the MSB of returned value
1477 /// while the bit at the highest index is the LSB.
1478 ///
1479 /// Returns a [`u128`] value and the number of bits read as a
1480 /// tuple.
1481 ///
1482 /// # Examples
1483 /// ```
1484 /// use deepmesa_collections::BitVector;
1485 ///
1486 /// let mut bv = BitVector::new();
1487 /// bv.push_u128(u128::MAX, Some(128));
1488 ///
1489 /// let s = &bv[..];
1490 /// assert_eq!(s.as_u128(), (u128::MAX, 128));
1491 /// ```
1492 ///
1493 /// If a Result is preferred over a panic, then
1494 /// using the `TryFrom<&BitSlice>` trait may be used.
1495 ///
1496 /// # Example of `TryFrom<&BitSlice> for u128`
1497 ///
1498 /// ```
1499 /// use deepmesa_collections::BitVector;
1500 /// use core::convert::TryFrom;
1501 ///
1502 /// let mut bv = BitVector::new();
1503 /// bv.push_u128(u128::MAX, Some(128));
1504 ///
1505 /// let s = &bv[..];
1506 /// match u128::try_from(s) {
1507 /// Ok(val) => assert_eq!(val, u128::MAX),
1508 /// Err(e) => assert!(false, "{}", e),
1509 /// }
1510 /// ```
1511 u128,
1512 128,
1513 as_u128
1514 );
1515
1516 read_unsigned!(
1517 /// Reads upto 8 bits from this [`BitSlice`](BitSlice) into
1518 /// a u8 starting at the specified `start` position. This
1519 /// method will panic if `start` is greater than or equal to
1520 /// the length of the slice.
1521 ///
1522 /// The bits are read from the lower to the higher index from
1523 /// the slice and shifted right, so the bit at the lower
1524 /// index is the MSB of returned value while the bit at the
1525 /// highest index is the LSB.
1526 ///
1527 /// # Examples
1528 /// ```
1529 /// use deepmesa_collections::BitVector;
1530 /// let mut bv = BitVector::new();
1531 /// bv.push_u8(0b0011_0110, Some(8));
1532 ///
1533 /// let s = &bv[..];
1534 /// let (val, read) = s.read_u8(0);
1535 /// assert_eq!(read, 8);
1536 /// assert_eq!(val, 0b0011_0110);
1537 ///
1538 /// let (val, read) = s.read_u8(4);
1539 /// assert_eq!(read, 4);
1540 /// assert_eq!(val, 0b0000_0110);
1541 /// ```
1542 u8,
1543 8,
1544 read_u8
1545 );
1546 read_unsigned!(
1547 /// Reads upto 16 bits from this [`BitSlice`](BitSlice) into a
1548 /// u16 starting at the specified `start` position. This
1549 /// method will panic if `start` is greater than or equal to
1550 /// the length of the slice.
1551 ///
1552 /// The bits are read from the lower to the higher index from
1553 /// the slice and shifted right, so the bit at the lower
1554 /// index is the MSB of returned value while the bit at the
1555 /// highest index is the LSB.
1556 ///
1557 /// # Examples
1558 /// ```
1559 /// use deepmesa_collections::BitVector;
1560 /// let mut bv = BitVector::new();
1561 /// bv.push_u16(0b0011_0110_1100_0011, Some(16));
1562 ///
1563 /// let s = &bv[..];
1564 /// let (val, read) = s.read_u16(0);
1565 /// assert_eq!(read, 16);
1566 /// assert_eq!(val, 0b0011_0110_1100_0011);
1567 ///
1568 /// let (val, read) = s.read_u16(4);
1569 /// assert_eq!(read, 12);
1570 /// assert_eq!(val, 0b0000_0110_1100_0011);
1571 /// ```
1572 u16,
1573 16,
1574 read_u16
1575 );
1576 read_unsigned!(
1577 /// Reads upto 32 bits from this [`BitSlice`](BitSlice) into
1578 /// a u32 starting at the specified `start` position. This
1579 /// method will panic if `start` is greater than or equal to
1580 /// the length of the slice.
1581 ///
1582 /// The bits are read from the lower to the higher index from
1583 /// the slice and shifted right, so the bit at the lower
1584 /// index is the MSB of returned value while the bit at the
1585 /// highest index is the LSB.
1586 ///
1587 /// # Examples
1588 /// ```
1589 /// use deepmesa_collections::BitVector;
1590 /// let mut bv = BitVector::new();
1591 /// bv.push_u16(0b0011_0110_1100_0011, Some(16));
1592 /// bv.push_u16(0b1100_1010_0100_1100, Some(16));
1593 ///
1594 /// let s = &bv[..];
1595 /// let (val, read) = s.read_u32(0);
1596 /// assert_eq!(read, 32);
1597 /// assert_eq!(val, 0b0011_0110_1100_0011_1100_1010_0100_1100);
1598 ///
1599 /// let (val, read) = s.read_u16(16);
1600 /// assert_eq!(read, 16);
1601 /// assert_eq!(val, 0b1100_1010_0100_1100);
1602 /// ```
1603 u32,
1604 32,
1605 read_u32
1606 );
1607 read_unsigned!(
1608 /// Reads upto 64 bits from this [`BitSlice`](BitSlice) into
1609 /// a u64 starting at the specified `start` position. This
1610 /// method will panic if `start` is greater than or equal to
1611 /// the length of the slice.
1612 ///
1613 /// The bits are read from the lower to the higher index from
1614 /// the slice and shifted right, so the bit at the lower
1615 /// index is the MSB of returned value while the bit at the
1616 /// highest index is the LSB.
1617 ///
1618 /// # Examples
1619 /// ```
1620 /// use deepmesa_collections::BitVector;
1621 /// let mut bv = BitVector::new();
1622 /// bv.push_u16(0b0011_0110_1100_0011, Some(16));
1623 /// bv.push_u16(0b1100_1010_0100_1100, Some(16));
1624 ///
1625 /// let s = &bv[..];
1626 /// let (val, read) = s.read_u64(20);
1627 /// assert_eq!(read, 12);
1628 //
1629 /// assert_eq!(val, 0b1010_0100_1100);
1630 ///
1631 /// let (val, read) = s.read_u64(16);
1632 /// assert_eq!(read, 16);
1633 /// assert_eq!(val, 0b1100_1010_0100_1100);
1634 /// ```
1635 u64,
1636 64,
1637 read_u64
1638 );
1639 read_unsigned!(
1640 /// Reads upto 128 bits from this [`BitSlice`](BitSlice)
1641 /// into a u128 starting at the specified `start`
1642 /// position. This method will panic if `start` is greater
1643 /// than or equal to the length of the slice.
1644 ///
1645 /// The bits are read from the lower to the higher index from
1646 /// the slice and shifted right, so the bit at the lower
1647 /// index is the MSB of returned value while the bit at the
1648 /// highest index is the LSB.
1649 ///
1650 /// # Examples
1651 /// ```
1652 /// use deepmesa_collections::BitVector;
1653 /// let mut bv = BitVector::new();
1654 /// bv.push_u16(0b0011_0110_1100_0011, Some(16));
1655 /// bv.push_u16(0b1100_1010_0100_1100, Some(16));
1656 ///
1657 /// let s = &bv[..];
1658 /// let (val, read) = bv.read_u128(20);
1659 /// assert_eq!(read, 12);
1660 //
1661 /// assert_eq!(val, 0b1010_0100_1100);
1662 ///
1663 /// let (val, read) = s.read_u128(16);
1664 /// assert_eq!(read, 16);
1665 /// assert_eq!(val, 0b1100_1010_0100_1100);
1666 /// ```
1667 u128,
1668 128,
1669 read_u128
1670 );
1671
1672 read_bits_unsigned!(
1673 /// Reads upto `max_bits` bits from this
1674 /// [`BitSlice`](BitSlice) into a u8 starting at the
1675 /// specified `start` position. This method will panic if
1676 /// `max_bits` is greater than 8 or if `start` is greater than
1677 /// or equal to the length of the slice.
1678 ///
1679 /// The bits are read from the lower to the higher index from
1680 /// the slice and shifted right, so the bit at the lower index
1681 /// is the MSB of returned value while the bit at the highest
1682 /// index is the LSB.
1683 ///
1684 /// Here is an illustrative example for a slice with 8
1685 /// bits.
1686 ///
1687 /// ```text
1688 /// 0 1 2 3 4 5 6 7
1689 /// [ 0,0,1,1,0,1,1,0 ]
1690 /// MSB [_______] LSB
1691 /// ^ Start = 2
1692 ///
1693 /// value read = 0b1101
1694 /// ```
1695 /// Reading 4 bits from the start position of 2, results in a
1696 /// u8 value of decimal 13.
1697 ///
1698 /// This method returns the read value as well as the number of
1699 /// bits read as a tuple.
1700 ///
1701 /// # Examples
1702 /// ```
1703 /// use deepmesa_collections::BitVector;
1704 /// let mut bv = BitVector::new();
1705 /// // Push 8 bits: 0b0011_0110
1706 /// bv.push_u8(0b0011_0110, Some(8));
1707 ///
1708 /// let s = &bv[..];
1709 /// let (val, read) = s.read_bits_u8(2, 4);
1710 /// assert_eq!(read,4);
1711 /// assert_eq!(val, 0b0000_1101);
1712 /// assert_eq!(val, 13);
1713 /// ```
1714 u8,
1715 8,
1716 read_bits_u8
1717 );
1718 read_bits_unsigned!(
1719 /// Reads upto `max_bits` bits from this
1720 /// [`BitSlice`](BitSlice) into a u16 starting at the
1721 /// specified `start` position. This method will panic if
1722 /// `max_bits` is greater than 16 or if `start` is greater
1723 /// than or equal to the length of the slice.
1724 ///
1725 /// The bits are read from the lower to the higher index from
1726 /// the slice and shifted right, so the bit at the lower
1727 /// index is the MSB of returned value while the bit at the
1728 /// highest index is the LSB.
1729 ///
1730 /// Here is an illustrative example for a slice with 8
1731 /// bits.
1732 ///
1733 /// ```text
1734 /// 0 1 2 3 4 5 6 7
1735 /// [ 0,0,1,1,0,1,1,0 ]
1736 /// MSB [_______] LSB
1737 /// ^ Start = 2
1738 ///
1739 /// value read = 0b1101
1740 /// ```
1741 /// Reading 4 bits from the start position of 2, results in a
1742 /// u16 value of decimal 13.
1743 ///
1744 /// This method returns the read value as well as the number of
1745 /// bits read as a tuple.
1746 ///
1747 /// # Examples
1748 /// ```
1749 /// use deepmesa_collections::BitVector;
1750 /// let mut bv = BitVector::new();
1751 /// // Push 8 bits: 0b0011_0110
1752 /// bv.push_u8(0b0011_0110, Some(8));
1753 ///
1754 /// let s = &bv[..];
1755 /// let (val, read) = s.read_bits_u16(2, 4);
1756 /// assert_eq!(read,4);
1757 /// assert_eq!(val, 0b0000_1101);
1758 /// assert_eq!(val, 13);
1759 /// ```
1760 u16,
1761 16,
1762 read_bits_u16
1763 );
1764 read_bits_unsigned!(
1765 /// Reads upto `max_bits` bits from this
1766 /// [`BitSlice`](BitSlice) into a u32 starting at the
1767 /// specified `start` position. This method will panic if
1768 /// `max_bits` is greater than 32 or if `start` is greater
1769 /// than or equal to the length of the slice.
1770 ///
1771 /// The bits are read from the lower to the higher index from
1772 /// the slice and shifted right, so the bit at the lower
1773 /// index is the MSB of returned value while the bit at the
1774 /// highest index is the LSB.
1775 ///
1776 /// Here is an illustrative example for a slice with 8
1777 /// bits.
1778 ///
1779 /// ```text
1780 /// 0 1 2 3 4 5 6 7
1781 /// [ 0,0,1,1,0,1,1,0 ]
1782 /// MSB [_______] LSB
1783 /// ^ Start = 2
1784 ///
1785 /// value read = 0b1101
1786 /// ```
1787 /// Reading 4 bits from the start position of 2, results in a
1788 /// u32 value of decimal 13.
1789 ///
1790 /// This method returns the read value as well as the number of
1791 /// bits read as a tuple.
1792 ///
1793 /// # Examples
1794 /// ```
1795 /// use deepmesa_collections::BitVector;
1796 /// let mut bv = BitVector::new();
1797 /// // Push 8 bits: 0b0011_0110
1798 /// bv.push_u8(0b0011_0110, Some(8));
1799 ///
1800 /// let s = &bv[..];
1801 /// let (val, read) = s.read_bits_u32(2, 4);
1802 /// assert_eq!(read,4);
1803 /// assert_eq!(val, 0b0000_1101);
1804 /// assert_eq!(val, 13);
1805 /// ```
1806 u32,
1807 32,
1808 read_bits_u32
1809 );
1810 read_bits_unsigned!(
1811 /// Reads upto `max_bits` bits from this
1812 /// [`BitSlice`](BitSlice) into a u64 starting at the
1813 /// specified `start` position. This method will panic if
1814 /// `max_bits` is greater than 64 or if `start` is greater
1815 /// than or equal to the length of the slice.
1816 ///
1817 /// The bits are read from the lower to the higher index from
1818 /// the slice and shifted right, so the bit at the lower
1819 /// index is the MSB of returned value while the bit at the
1820 /// highest index is the LSB.
1821 ///
1822 /// Here is an illustrative example for a slice with 8
1823 /// elements.
1824 ///
1825 /// ```text
1826 /// 0 1 2 3 4 5 6 7
1827 /// [ 0,0,1,1,0,1,1,0 ]
1828 /// MSB [_______] LSB
1829 /// ^ Start = 2
1830 ///
1831 /// value read = 0b1101
1832 /// ```
1833 /// Reading 4 bits from the start position of 2, results in a
1834 /// u64 value of decimal 13.
1835 ///
1836 /// This method returns the read value as well as the number of
1837 /// bits read as a tuple.
1838 ///
1839 /// # Examples
1840 /// ```
1841 /// use deepmesa_collections::BitVector;
1842 /// let mut bv = BitVector::new();
1843 /// // Push 8 bits: 0b0011_0110
1844 /// bv.push_u8(0b0011_0110, Some(8));
1845 ///
1846 /// let s = &bv[..];
1847 /// let (val, read) = s.read_bits_u64(2, 4);
1848 /// assert_eq!(read,4);
1849 /// assert_eq!(val, 0b0000_1101);
1850 /// assert_eq!(val, 13);
1851 /// ```
1852 u64,
1853 64,
1854 read_bits_u64
1855 );
1856 read_bits_unsigned!(
1857 /// Reads upto `max_bits` bits from this
1858 /// [`BitSlice`](BitSlice) into a u128 starting at the
1859 /// specified `start` position. This method will panic if
1860 /// `max_bits` is greater than 128 or if `start` is greater
1861 /// than or equal to the length of the slice.
1862 ///
1863 /// The bits are read from the lower to the higher index from
1864 /// the slice and shifted right, so the bit at the lower
1865 /// index is the MSB of returned value while the bit at the
1866 /// highest index is the LSB.
1867 ///
1868 /// Here is an illustrative example for a slice with 8
1869 /// bits.
1870 ///
1871 /// ```text
1872 /// 0 1 2 3 4 5 6 7
1873 /// [ 0,0,1,1,0,1,1,0 ]
1874 /// MSB [_______] LSB
1875 /// ^ Start = 2
1876 ///
1877 /// value read = 0b1101
1878 /// ```
1879 /// Reading 4 bits from the start position of 2, results in a
1880 /// u128 value of decimal 13.
1881 ///
1882 /// This method returns the read value as well as the number of
1883 /// bits read as a tuple.
1884 ///
1885 /// # Examples
1886 /// ```
1887 /// use deepmesa_collections::BitVector;
1888 /// let mut bv = BitVector::new();
1889 /// // Push 8 bits: 0b0011_0110
1890 /// bv.push_u8(0b0011_0110, Some(8));
1891 ///
1892 /// let s = &bv[..];
1893 /// let (val, read) = s.read_bits_u128(2, 4);
1894 /// assert_eq!(read,4);
1895 /// assert_eq!(val, 0b0000_1101);
1896 /// assert_eq!(val, 13);
1897 /// ```
1898 ///
1899 u128,
1900 128,
1901 read_bits_u128
1902 );
1903}
1904
1905// Helpers and private methods
1906impl BitSlice {
1907 /// Returns the 3 most significant bits of the length
1908 #[inline(always)]
1909 pub(super) fn offset(&self) -> usize {
1910 slice_unpack_offset!(self.0.len())
1911 }
1912}
1913
1914impl Index<usize> for &BitSlice {
1915 type Output = bool;
1916 fn index(&self, index: usize) -> &Self::Output {
1917 match self.get(index) {
1918 None => {
1919 panic!(
1920 "index out of bounds: the len is {} but the index is {}",
1921 self.len(),
1922 index
1923 );
1924 }
1925 Some(true) => &true,
1926 Some(false) => &false,
1927 }
1928 }
1929}
1930
1931impl Index<usize> for &mut BitSlice {
1932 type Output = bool;
1933 fn index(&self, index: usize) -> &Self::Output {
1934 match self.get(index) {
1935 None => {
1936 panic!(
1937 "index out of bounds: the len is {} but the index is {}",
1938 self.len(),
1939 index
1940 );
1941 }
1942 Some(true) => &true,
1943 Some(false) => &false,
1944 }
1945 }
1946}
1947
1948impl_index_range!(&BitSlice, BitSlice, 0);
1949impl_index_range!(&mut BitSlice, BitSlice, 0);
1950impl_index_range_mut!(&mut BitSlice, BitSlice, 0);
1951
1952#[cfg(test)]
1953mod tests {
1954 use super::*;
1955 use crate::bitvec::{bitvec::BitVector, BitOrder};
1956 #[test]
1957 fn test_slice() {
1958 let mut bv = BitVector::with_capacity(20);
1959
1960 bv.push_u8(0b1011_0011, None);
1961 bv.push_u8(0b1011_0011, None);
1962 assert_eq!(bv.get(0), Some(true));
1963 assert_eq!(bv.get(1), Some(false));
1964
1965 assert_eq!(bv.len(), 16);
1966 let s = &bv[0..16];
1967 assert_eq!(s.len(), 16);
1968
1969 assert_eq!(bv[0], true);
1970 assert_eq!(bv[1], false);
1971
1972 let slice = &bv[9..11];
1973
1974 assert_eq!(slice.len(), 2);
1975 assert_eq!(slice.offset(), 1);
1976 assert_eq!(slice[0], false);
1977 assert_eq!(slice[1], true);
1978
1979 let ms = &mut bv[9..11];
1980 assert_eq!(ms[0], false);
1981 ms.set(0, true);
1982 assert_eq!(ms[0], true);
1983
1984 let slice2 = &bv[9..11];
1985
1986 assert_eq!(slice2[0], true);
1987 }
1988
1989 #[test]
1990 fn test_read_bits_u8() {
1991 let mut bv = BitVector::with_capacity(20);
1992 bv.push_bits_u8(0b1100_1011, 8, BitOrder::Msb0);
1993 bv.push_bits_u8(0b1010_0101, 8, BitOrder::Msb0);
1994
1995 // let bv = &*bvec;
1996 assert_eq!(bv.len(), 16);
1997
1998 // Read a byte from start = 0
1999 let (byte, bit_count) = bv.read_bits_u8(0, 8);
2000 assert_eq!(bit_count, 8);
2001 assert_eq!(byte, 0b1100_1011);
2002
2003 //Read a byte from start = 4
2004 let (byte, bit_count) = bv.read_bits_u8(4, 8);
2005 assert_eq!(bit_count, 8);
2006 assert_eq!(byte, 0b1011_1010);
2007
2008 //Read a byte from start = 12
2009 let (byte, bit_count) = bv.read_bits_u8(12, 8);
2010 assert_eq!(bit_count, 4);
2011 assert_eq!(byte, 0b0000_0101);
2012
2013 //Read a byte from start = 15
2014 let (byte, bit_count) = bv.read_bits_u8(15, 8);
2015 assert_eq!(bit_count, 1);
2016 assert_eq!(byte, 0b0000_0001);
2017 }
2018
2019 #[test]
2020 fn test_read_u8_slice() {
2021 let mut bvec = BitVector::with_capacity(20);
2022 bvec.push_bits_u8(0b1100_1011, 8, BitOrder::Msb0);
2023 bvec.push_bits_u8(0b1010_0101, 8, BitOrder::Msb0);
2024
2025 let slice = &bvec[2..13]; // [0010_1110, 100]
2026 assert_eq!(slice.len(), 11);
2027
2028 let (byte, bit_count) = slice.read_bits_u8(0, 8);
2029 assert_eq!(bit_count, 8);
2030 assert_eq!(byte, 0b0010_1110);
2031
2032 let (byte, bit_count) = slice.read_bits_u8(4, 8);
2033 assert_eq!(bit_count, 7);
2034 assert_eq!(byte, 0b0111_0100);
2035
2036 let (byte, bit_count) = slice.read_bits_u8(5, 2);
2037 assert_eq!(bit_count, 2);
2038 assert_eq!(byte, 0b0000_0011);
2039
2040 let (byte, bit_count) = slice.read_bits_u8(8, 8);
2041 assert_eq!(bit_count, 3);
2042 assert_eq!(byte, 0b0000_0100);
2043
2044 let (byte, bit_count) = slice.read_bits_u8(10, 8);
2045 assert_eq!(bit_count, 1);
2046 assert_eq!(byte, 0b0000_0000);
2047 }
2048
2049 #[test]
2050 fn test_from() {
2051 let mut bvec = BitVector::with_capacity(20);
2052 bvec.push_bits_u8(0b1100_1011, 8, BitOrder::Msb0);
2053 bvec.push_bits_u8(0b1010_0101, 8, BitOrder::Msb0);
2054
2055 let slice = &bvec[8..16];
2056 let (val, read) = slice.as_u16();
2057 assert_eq!(read, 8);
2058 assert_eq!(val, 0b1010_0101);
2059 let val2: u16 = u16::try_from(slice).unwrap();
2060 assert_eq!(val2, 0b1010_0101);
2061 }
2062
2063 #[test]
2064 fn test_bit_not_3() {
2065 let mut bv = BitVector::with_capacity(128);
2066 bv.push_u8(0b1001_0011, Some(8));
2067 let mut s = &mut bv[2..5];
2068 assert_eq!(s.len(), 3);
2069 assert_eq!(s.read_u8(0), (0b0000_0010, 3));
2070 s = !s;
2071 assert_eq!(s.read_u8(0), (0b0000_0101, 3));
2072 assert_eq!(bv.read_u8(0), (0b1010_1011, 8));
2073 }
2074
2075 #[test]
2076 fn test_bit_not_8() {
2077 let mut bv = BitVector::with_capacity(128);
2078 bv.push_u8(0b1001_0011, Some(8));
2079 let mut s = &mut bv[0..8];
2080 assert_eq!(s.len(), 8);
2081 assert_eq!(s.read_u8(0), (0b1001_0011, 8));
2082 s = !s;
2083 assert_eq!(s.read_u8(0), (0b0110_1100, 8));
2084 assert_eq!(bv.read_u8(0), (0b0110_1100, 8));
2085 }
2086
2087 #[test]
2088 #[allow(unused_assignments)]
2089 fn test_bit_not_0() {
2090 let mut bv = BitVector::with_capacity(128);
2091 bv.push_u8(0b1001_0011, Some(8));
2092 let mut s = &mut bv[2..2];
2093 assert_eq!(s.len(), 0);
2094 s = !s;
2095 assert_eq!(bv.read_u8(0), (0b1001_0011, 8));
2096 }
2097
2098 #[test]
2099 fn test_bit_not_25() {
2100 let mut bv = BitVector::with_capacity(128);
2101 bv.push_u16(0b1001_0011_0101_1010, Some(16));
2102 bv.push_u16(0b0110_1100_1010_0101, Some(16));
2103 bv.push_u16(0b1110_1011_1101_0111, Some(16));
2104
2105 // 0 7 8 15 23 31 39 47
2106 // bv: 1001_0011_0101_1010 0110_1100 1010_0101 1110_1011_1101_0111
2107 // ^ ^
2108 // slice: 01_1010 0110_1100 1010_0101 111
2109 // ^ ^ ^ ^
2110 // 0 7 15 23
2111 let mut s = &mut bv[10..35];
2112 assert_eq!(s.len(), 25);
2113 assert_eq!(s.read_u8(0), (0b0110_1001, 8));
2114 assert_eq!(s.read_u8(8), (0b1011_0010, 8));
2115 assert_eq!(s.read_u8(16), (0b1001_0111, 8));
2116 assert_eq!(s.read_u8(24), (0b0000_0001, 1));
2117
2118 s = !s;
2119 assert_eq!(s.read_u8(0), (0b1001_0110, 8));
2120 assert_eq!(s.read_u8(8), (0b0100_1101, 8));
2121 assert_eq!(s.read_u8(16), (0b0110_1000, 8));
2122 assert_eq!(s.read_u8(24), (0b0000_0000, 1));
2123
2124 assert_eq!(bv.read_u16(0), (0b1001_0011_0110_0101, 16));
2125 assert_eq!(bv.read_u16(16), (0b1001_0011_0101_1010, 16));
2126 assert_eq!(bv.read_u16(32), (0b0000_1011_1101_0111, 16));
2127 }
2128
2129 #[test]
2130 fn test_bit_and_8() {
2131 let mut bv = BitVector::with_capacity(128);
2132 bv.push_u8(0b1001_0011, Some(8));
2133 let mut s = &mut bv[0..8];
2134 assert_eq!(s.len(), 8);
2135 assert_eq!(s.read_u8(0), (0b1001_0011, 8));
2136 s &= false;
2137 assert_eq!(s.read_u8(0), (0b0000_0000, 8));
2138 assert_eq!(bv.read_u8(0), (0b0000_0000, 8));
2139 bv.clear();
2140 bv.push_u8(0b1001_0011, Some(8));
2141 let mut s = &mut bv[0..8];
2142 assert_eq!(s.len(), 8);
2143 assert_eq!(s.read_u8(0), (0b1001_0011, 8));
2144 s &= true;
2145 assert_eq!(s.read_u8(0), (0b1001_0011, 8));
2146 assert_eq!(bv.read_u8(0), (0b1001_0011, 8));
2147 }
2148
2149 #[test]
2150 fn test_bit_and_0() {
2151 let mut bv = BitVector::with_capacity(128);
2152 bv.push_u8(0b1001_0011, Some(8));
2153 let mut s = &mut bv[2..2];
2154 assert_eq!(s.len(), 0);
2155 s &= false;
2156 assert_eq!(bv.read_u8(0), (0b1001_0011, 8));
2157 bv.clear();
2158 bv.push_u8(0b1001_0011, Some(8));
2159 let mut s = &mut bv[2..2];
2160 assert_eq!(s.len(), 0);
2161 s &= true;
2162 assert_eq!(bv.read_u8(0), (0b1001_0011, 8));
2163 }
2164
2165 #[test]
2166 fn test_bit_and_3() {
2167 let mut bv = BitVector::with_capacity(128);
2168 bv.push_u8(0b1001_0011, Some(8));
2169 let mut s = &mut bv[2..5];
2170 assert_eq!(s.len(), 3);
2171 assert_eq!(s.read_u8(0), (0b0000_0010, 3));
2172 s &= false;
2173 assert_eq!(s.read_u8(0), (0b0000_0000, 3));
2174 assert_eq!(bv.read_u8(0), (0b1000_0011, 8));
2175 bv.clear();
2176 bv.push_u8(0b1001_0011, Some(8));
2177 let mut s = &mut bv[2..5];
2178 assert_eq!(s.len(), 3);
2179 assert_eq!(s.read_u8(0), (0b0000_0010, 3));
2180 s &= true;
2181 assert_eq!(s.read_u8(0), (0b0000_0010, 3));
2182 assert_eq!(bv.read_u8(0), (0b1001_0011, 8));
2183 }
2184
2185 #[test]
2186 fn test_bit_and_25() {
2187 let mut bv = BitVector::with_capacity(128);
2188 bv.push_u16(0b1001_0011_0101_1010, Some(16));
2189 bv.push_u16(0b0110_1100_1010_0101, Some(16));
2190 bv.push_u16(0b1110_1011_1101_0111, Some(16));
2191
2192 // 0 7 8 15 23 31 39 47
2193 // bv: 1001_0011_0101_1010 0110_1100 1010_0101 1110_1011_1101_0111
2194 // ^ ^
2195 // slice: 01_1010 0110_1100 1010_0101 111
2196 // ^ ^ ^ ^
2197 // 0 7 15 23
2198 let mut s = &mut bv[10..35];
2199 assert_eq!(s.len(), 25);
2200 assert_eq!(s.read_u8(0), (0b0110_1001, 8));
2201 assert_eq!(s.read_u8(8), (0b1011_0010, 8));
2202 assert_eq!(s.read_u8(16), (0b1001_0111, 8));
2203 assert_eq!(s.read_u8(24), (0b0000_0001, 1));
2204
2205 s &= true;
2206 assert_eq!(s.read_u8(0), (0b0110_1001, 8));
2207 assert_eq!(s.read_u8(8), (0b1011_0010, 8));
2208 assert_eq!(s.read_u8(16), (0b1001_0111, 8));
2209 assert_eq!(s.read_u8(24), (0b0000_0001, 1));
2210 assert_eq!(bv.read_u16(0), (0b1001_0011_0101_1010, 16));
2211 assert_eq!(bv.read_u16(16), (0b0110_1100_1010_0101, 16));
2212 assert_eq!(bv.read_u16(32), (0b1110_1011_1101_0111, 16));
2213
2214 bv.clear();
2215 bv.push_u16(0b1001_0011_0101_1010, Some(16));
2216 bv.push_u16(0b0110_1100_1010_0101, Some(16));
2217 bv.push_u16(0b1110_1011_1101_0111, Some(16));
2218 let mut s = &mut bv[10..35];
2219 s &= false;
2220 assert_eq!(s.read_u8(0), (0b0000_0000, 8));
2221 assert_eq!(s.read_u8(8), (0b0000_0000, 8));
2222 assert_eq!(s.read_u8(16), (0b0000_0000, 8));
2223 assert_eq!(s.read_u8(24), (0b0000_0000, 1));
2224 // range [10..35] in the bitvector should be cleared
2225 assert_eq!(bv.read_u16(0), (0b1001_0011_0100_0000, 16));
2226 assert_eq!(bv.read_u16(16), (0b0000_0000_0000_0000, 16));
2227 assert_eq!(bv.read_u16(32), (0b0000_1011_1101_0111, 16));
2228 }
2229
2230 #[test]
2231 fn test_bit_or_8() {
2232 let mut bv = BitVector::with_capacity(128);
2233 bv.push_u8(0b1001_0011, Some(8));
2234 let mut s = &mut bv[0..8];
2235 assert_eq!(s.len(), 8);
2236 assert_eq!(s.read_u8(0), (0b1001_0011, 8));
2237 s |= true;
2238 assert_eq!(s.read_u8(0), (0b1111_1111, 8));
2239 assert_eq!(bv.read_u8(0), (0b1111_1111, 8));
2240 bv.clear();
2241 bv.push_u8(0b1001_0011, Some(8));
2242 let mut s = &mut bv[0..8];
2243 assert_eq!(s.len(), 8);
2244 assert_eq!(s.read_u8(0), (0b1001_0011, 8));
2245 s |= false;
2246 assert_eq!(s.read_u8(0), (0b1001_0011, 8));
2247 assert_eq!(bv.read_u8(0), (0b1001_0011, 8));
2248 }
2249
2250 #[test]
2251 fn test_bit_or_0() {
2252 let mut bv = BitVector::with_capacity(128);
2253 bv.push_u8(0b1001_0011, Some(8));
2254 let mut s = &mut bv[2..2];
2255 assert_eq!(s.len(), 0);
2256 s |= false;
2257 assert_eq!(bv.read_u8(0), (0b1001_0011, 8));
2258 bv.clear();
2259 bv.push_u8(0b1001_0011, Some(8));
2260 let mut s = &mut bv[2..2];
2261 assert_eq!(s.len(), 0);
2262 s |= true;
2263 assert_eq!(bv.read_u8(0), (0b1001_0011, 8));
2264 }
2265
2266 #[test]
2267 fn test_bit_or_3() {
2268 let mut bv = BitVector::with_capacity(128);
2269 bv.push_u8(0b1001_0011, Some(8));
2270 let mut s = &mut bv[2..5];
2271 assert_eq!(s.len(), 3);
2272 assert_eq!(s.read_u8(0), (0b0000_0010, 3));
2273 s |= false;
2274 assert_eq!(s.read_u8(0), (0b0000_0010, 3));
2275 assert_eq!(bv.read_u8(0), (0b1001_0011, 8));
2276 bv.clear();
2277 bv.push_u8(0b1001_0000, Some(8));
2278 let mut s = &mut bv[2..5];
2279 assert_eq!(s.len(), 3);
2280 assert_eq!(s.read_u8(0), (0b0000_0010, 3));
2281 s |= true;
2282 assert_eq!(s.read_u8(0), (0b0000_0111, 3));
2283 assert_eq!(bv.read_u8(0), (0b1011_1000, 8));
2284 }
2285
2286 #[test]
2287 fn test_bit_or_25() {
2288 let mut bv = BitVector::with_capacity(128);
2289 bv.push_u16(0b1001_0011_0101_1010, Some(16));
2290 bv.push_u16(0b0110_1100_1010_0101, Some(16));
2291 bv.push_u16(0b1110_1011_1101_0111, Some(16));
2292
2293 // 0 7 8 15 23 31 39 47
2294 // bv: 1001_0011_0101_1010 0110_1100 1010_0101 1110_1011_1101_0111
2295 // ^ ^
2296 // slice: 01_1010 0110_1100 1010_0101 111
2297 // ^ ^ ^ ^
2298 // 0 7 15 23
2299 let mut s = &mut bv[10..35];
2300 assert_eq!(s.len(), 25);
2301 assert_eq!(s.read_u8(0), (0b0110_1001, 8));
2302 assert_eq!(s.read_u8(8), (0b1011_0010, 8));
2303 assert_eq!(s.read_u8(16), (0b1001_0111, 8));
2304 assert_eq!(s.read_u8(24), (0b0000_0001, 1));
2305
2306 s |= false;
2307 assert_eq!(s.read_u8(0), (0b0110_1001, 8));
2308 assert_eq!(s.read_u8(8), (0b1011_0010, 8));
2309 assert_eq!(s.read_u8(16), (0b1001_0111, 8));
2310 assert_eq!(s.read_u8(24), (0b0000_0001, 1));
2311 assert_eq!(bv.read_u16(0), (0b1001_0011_0101_1010, 16));
2312 assert_eq!(bv.read_u16(16), (0b0110_1100_1010_0101, 16));
2313 assert_eq!(bv.read_u16(32), (0b1110_1011_1101_0111, 16));
2314
2315 bv.clear();
2316 bv.push_u16(0b1001_0011_0101_1010, Some(16));
2317 bv.push_u16(0b0110_1100_1010_0101, Some(16));
2318 bv.push_u16(0b1110_1011_1101_0111, Some(16));
2319 let mut s = &mut bv[10..35];
2320 s |= true;
2321 assert_eq!(s.read_u8(0), (0b1111_1111, 8));
2322 assert_eq!(s.read_u8(8), (0b1111_1111, 8));
2323 assert_eq!(s.read_u8(16), (0b1111_1111, 8));
2324 assert_eq!(s.read_u8(24), (0b0000_0001, 1));
2325 // range [10..35] in the bitvector should be all 1
2326 assert_eq!(bv.read_u16(0), (0b1001_0011_0111_1111, 16));
2327 assert_eq!(bv.read_u16(16), (0b1111_1111_1111_1111, 16));
2328 assert_eq!(bv.read_u16(32), (0b1110_1011_1101_0111, 16));
2329 }
2330
2331 #[test]
2332 fn test_bit_xor_8() {
2333 let mut bv = BitVector::with_capacity(128);
2334 bv.push_u8(0b1001_0011, Some(8));
2335 let mut s = &mut bv[0..8];
2336 assert_eq!(s.len(), 8);
2337 assert_eq!(s.read_u8(0), (0b1001_0011, 8));
2338 s ^= true;
2339 assert_eq!(s.read_u8(0), (0b0110_1100, 8));
2340 assert_eq!(bv.read_u8(0), (0b0110_1100, 8));
2341 bv.clear();
2342 bv.push_u8(0b1001_0011, Some(8));
2343 let mut s = &mut bv[0..8];
2344 assert_eq!(s.len(), 8);
2345 assert_eq!(s.read_u8(0), (0b1001_0011, 8));
2346 s ^= false;
2347 assert_eq!(s.read_u8(0), (0b1001_0011, 8));
2348 assert_eq!(bv.read_u8(0), (0b1001_0011, 8));
2349 }
2350
2351 #[test]
2352 fn test_bit_xor_0() {
2353 let mut bv = BitVector::with_capacity(128);
2354 bv.push_u8(0b1001_0011, Some(8));
2355 let mut s = &mut bv[2..2];
2356 assert_eq!(s.len(), 0);
2357 s ^= false;
2358 assert_eq!(bv.read_u8(0), (0b1001_0011, 8));
2359 bv.clear();
2360 bv.push_u8(0b1001_0011, Some(8));
2361 let mut s = &mut bv[2..2];
2362 assert_eq!(s.len(), 0);
2363 s ^= true;
2364 assert_eq!(bv.read_u8(0), (0b1001_0011, 8));
2365 }
2366
2367 #[test]
2368 fn test_bit_xor_3() {
2369 let mut bv = BitVector::with_capacity(128);
2370 bv.push_u8(0b1001_0011, Some(8));
2371 let mut s = &mut bv[2..5];
2372 assert_eq!(s.len(), 3);
2373 assert_eq!(s.read_u8(0), (0b0000_0010, 3));
2374 s ^= false;
2375 assert_eq!(s.read_u8(0), (0b0000_0010, 3));
2376 assert_eq!(bv.read_u8(0), (0b1001_0011, 8));
2377 bv.clear();
2378 bv.push_u8(0b1001_0000, Some(8));
2379 let mut s = &mut bv[2..5];
2380 assert_eq!(s.len(), 3);
2381 assert_eq!(s.read_u8(0), (0b0000_0010, 3));
2382 s ^= true;
2383 assert_eq!(s.read_u8(0), (0b0000_0101, 3));
2384 assert_eq!(bv.read_u8(0), (0b1010_1000, 8));
2385 }
2386
2387 #[test]
2388 fn test_bit_xor_25() {
2389 let mut bv = BitVector::with_capacity(128);
2390 bv.push_u16(0b1001_0011_0101_1010, Some(16));
2391 bv.push_u16(0b0110_1100_1010_0101, Some(16));
2392 bv.push_u16(0b1110_1011_1101_0111, Some(16));
2393
2394 // 0 7 8 15 23 31 39 47
2395 // bv: 1001_0011_0101_1010 0110_1100 1010_0101 1110_1011_1101_0111
2396 // ^ ^
2397 // slice: 01_1010 0110_1100 1010_0101 111
2398 // ^ ^ ^ ^
2399 // 0 7 15 23
2400 let mut s = &mut bv[10..35];
2401 assert_eq!(s.len(), 25);
2402 assert_eq!(s.read_u8(0), (0b0110_1001, 8));
2403 assert_eq!(s.read_u8(8), (0b1011_0010, 8));
2404 assert_eq!(s.read_u8(16), (0b1001_0111, 8));
2405 assert_eq!(s.read_u8(24), (0b0000_0001, 1));
2406
2407 s ^= false;
2408 assert_eq!(s.read_u8(0), (0b0110_1001, 8));
2409 assert_eq!(s.read_u8(8), (0b1011_0010, 8));
2410 assert_eq!(s.read_u8(16), (0b1001_0111, 8));
2411 assert_eq!(s.read_u8(24), (0b0000_0001, 1));
2412 assert_eq!(bv.read_u16(0), (0b1001_0011_0101_1010, 16));
2413 assert_eq!(bv.read_u16(16), (0b0110_1100_1010_0101, 16));
2414 assert_eq!(bv.read_u16(32), (0b1110_1011_1101_0111, 16));
2415
2416 bv.clear();
2417 bv.push_u16(0b1001_0011_0101_1010, Some(16));
2418 bv.push_u16(0b0110_1100_1010_0101, Some(16));
2419 bv.push_u16(0b1110_1011_1101_0111, Some(16));
2420 let mut s = &mut bv[10..35];
2421 s ^= true;
2422 assert_eq!(s.read_u8(0), (0b1001_0110, 8));
2423 assert_eq!(s.read_u8(8), (0b0100_1101, 8));
2424 assert_eq!(s.read_u8(16), (0b0110_1000, 8));
2425 assert_eq!(s.read_u8(24), (0b0000_0000, 1));
2426 // range [10..35] in the bitvector should be flipped
2427 assert_eq!(bv.read_u16(0), (0b1001_0011_0110_0101, 16));
2428 assert_eq!(bv.read_u16(16), (0b1001_0011_0101_1010, 16));
2429 assert_eq!(bv.read_u16(32), (0b0000_1011_1101_0111, 16));
2430 }
2431
2432 #[test]
2433 fn test_bit_and_slice() {
2434 let mut bv = BitVector::with_capacity(128);
2435 bv.push_u16(0b1001_0011_0101_1010, Some(16));
2436 bv.push_u16(0b0110_1100_1010_0101, Some(16));
2437 bv.push_u16(0b1110_1011_1101_0111, Some(16));
2438
2439 let mut s = &mut bv[10..35];
2440 assert_eq!(s.len(), 25);
2441
2442 let mut bv2 = BitVector::with_capacity(128);
2443 bv2.push_u16(0b1110_1011_1101_0111, Some(16));
2444 bv2.push_u16(0b0110_1101, Some(8));
2445 let s2 = &bv2[0..];
2446 assert_eq!(s2.len(), 24);
2447
2448 s &= s2;
2449
2450 // bv: 1001_0011_0101_1010 0110_1100_1010_0101 1110_1011_1101_0111
2451 // 7 ^ 15 23 31 ^ 39 47
2452 // 10 35
2453 // bv2: (s2) 11 1010 1111 0101 1101 1011 01
2454 // s &= s2 01 1010 0110 0100 1000 0001 011
2455 // res 1001_0011_0101 1010 0110 0100 1000 0001 0110_1011_1101_0111
2456 assert_eq!(s.read_u32(0), (0b01_1010_0110_0100_1000_0001_011, 25));
2457 assert_eq!(
2458 bv.read_u64(0),
2459 (
2460 0b1001_0011_0101_1010_0110_0100_1000_0001_0110_1011_1101_0111,
2461 48
2462 )
2463 );
2464 }
2465
2466 #[test]
2467 fn test_bit_and_slice_0() {
2468 let mut bv = BitVector::with_capacity(128);
2469 bv.push_u16(0b1001_0011_0101_1010, Some(16));
2470 bv.push_u16(0b0110_1100_1010_0101, Some(16));
2471 bv.push_u16(0b1110_1011_1101_0111, Some(16));
2472
2473 let mut s = &mut bv[10..35];
2474 assert_eq!(s.len(), 25);
2475
2476 let mut bv2 = BitVector::with_capacity(128);
2477 bv2.push_u16(0b1110_1011_1101_0111, Some(16));
2478 bv2.push_u16(0b0110_1101, Some(8));
2479 let s2 = &bv2[0..0];
2480 assert_eq!(s2.len(), 0);
2481
2482 s &= s2;
2483 // bv: 1001_0011_0101_1010 0110_1100_1010_0101 1110_1011_1101_0111
2484 // 7 ^ 15 23 31 ^ 39 47
2485 // 10 35
2486 // s2 []
2487 // s &= s2 01_1010 0110_1100_1010_0101 111
2488 // res 1001_0011_0101_1010 0110_1100_1010_0101 1110_1011_1101_0111
2489 assert_eq!(s.read_u32(0), (0b01_1010_0110_1100_1010_0101_111, 25));
2490 assert_eq!(
2491 bv.read_u64(0),
2492 (
2493 0b1001_0011_0101_1010_0110_1100_1010_0101_1110_1011_1101_0111,
2494 48
2495 )
2496 );
2497 }
2498
2499 #[test]
2500 fn test_bit_and_slice_3() {
2501 let mut bv = BitVector::with_capacity(128);
2502 bv.push_u16(0b1001_0011_0101_1010, Some(16));
2503 bv.push_u16(0b0110_1100_1010_0101, Some(16));
2504 bv.push_u16(0b1110_1011_1101_0111, Some(16));
2505
2506 let mut s = &mut bv[10..35];
2507 assert_eq!(s.len(), 25);
2508
2509 let mut bv2 = BitVector::with_capacity(128);
2510 bv2.push_u16(0b1010_1011_1101_0111, Some(16));
2511 bv2.push_u16(0b0110_1101, Some(8));
2512 let s2 = &bv2[0..3];
2513 assert_eq!(s2.len(), 3);
2514
2515 s &= s2;
2516 // bv: 1001_0011_0101_1010 0110_1100_1010_0101 1110_1011_1101_0111
2517 // 7 ^ 15 23 31 ^ 39 47
2518 // 10 35
2519 // s2 10 1
2520 // s &= s2 00_1010 0110_1100_1010_0101 111
2521 // res 1001_0011_0100_1010 0110_1100_1010_0101 1110_1011_1101_0111
2522 assert_eq!(s.read_u32(0), (0b00_1010_0110_1100_1010_0101_111, 25));
2523 assert_eq!(
2524 bv.read_u64(0),
2525 (
2526 0b1001_0011_0100_1010_0110_1100_1010_0101_1110_1011_1101_0111,
2527 48
2528 )
2529 );
2530 }
2531
2532 #[test]
2533 fn test_bit_and_slice_8() {
2534 let mut bv = BitVector::with_capacity(128);
2535 bv.push_u16(0b1001_0011_0101_1010, Some(16));
2536 bv.push_u16(0b0110_1100_1010_0101, Some(16));
2537 bv.push_u16(0b1110_1011_1101_0111, Some(16));
2538
2539 let mut s = &mut bv[10..35];
2540 assert_eq!(s.len(), 25);
2541
2542 let mut bv2 = BitVector::with_capacity(128);
2543 bv2.push_u16(0b1010_1010_1101_0111, Some(16));
2544 bv2.push_u16(0b0110_1101, Some(8));
2545 let s2 = &bv2[0..8];
2546 assert_eq!(s2.len(), 8);
2547
2548 s &= s2;
2549 // bv: 1001_0011_0101_1010 0110_1100_1010_0101 1110_1011_1101_0111
2550 // 7 ^ 15 23 31 ^ 39 47
2551 // 10 35
2552 // s2 10 1010 10
2553 // s &= s2 00_1010 0010_1100_1010_0101 111
2554 // res 1001_0011_0100_1010 0010_1100_1010_0101 1110_1011_1101_0111
2555 assert_eq!(s.read_u32(0), (0b00_1010_0010_1100_1010_0101_111, 25));
2556 assert_eq!(
2557 bv.read_u64(0),
2558 (
2559 0b1001_0011_0100_1010_0010_1100_1010_0101_1110_1011_1101_0111,
2560 48
2561 )
2562 );
2563 }
2564
2565 #[test]
2566 fn test_bit_and_slice_32() {
2567 let mut bv = BitVector::with_capacity(128);
2568 bv.push_u16(0b1001_0011_0101_1010, Some(16));
2569 bv.push_u16(0b0110_1100_1010_0101, Some(16));
2570 bv.push_u16(0b1110_1011_1101_0111, Some(16));
2571
2572 let mut s = &mut bv[10..35];
2573 assert_eq!(s.len(), 25);
2574
2575 let mut bv2 = BitVector::with_capacity(128);
2576 bv2.push_u16(0b1010_1010_1101_0111, Some(16));
2577 bv2.push_u16(0b0110_1101_0011_1010, Some(16));
2578 let s2 = &bv2[0..32];
2579 assert_eq!(s2.len(), 32);
2580
2581 s &= s2;
2582 // 0 1 2 3
2583 // bv: 1001_0011_0101_1010 0110_1100_1010_0101 1110_1011_1101_0111
2584 // 7 ^ 15 23 31 ^ 39 47
2585 // 10 35
2586 // s2 10 1010 1011 0101 1101 1011 0100 11_1010
2587 // s &= s2 00 1010 0010 0100 1000 0001 010
2588 // res 1001_0011_0100 1010 0010 0100 1000 0001 0100_1011_1101_0111
2589
2590 assert_eq!(s.read_u32(0), (0b00_1010_0010_0100_1000_0001_010, 25));
2591 assert_eq!(
2592 bv.read_u64(0),
2593 (
2594 0b1001_0011_0100_1010_0010_0100_1000_0001_0100_1011_1101_0111,
2595 48
2596 )
2597 );
2598 }
2599
2600 #[test]
2601 fn test_bit_or_slice_0() {
2602 let mut bv = BitVector::with_capacity(128);
2603 bv.push_u16(0b1001_0011_0101_1010, Some(16));
2604 bv.push_u16(0b0110_1100_1010_0101, Some(16));
2605 bv.push_u16(0b1110_1011_1101_0111, Some(16));
2606
2607 let mut s = &mut bv[10..35];
2608 assert_eq!(s.len(), 25);
2609
2610 let mut bv2 = BitVector::with_capacity(128);
2611 bv2.push_u16(0b1110_1011_1101_0111, Some(16));
2612 bv2.push_u16(0b0110_1101, Some(8));
2613 let s2 = &bv2[0..0];
2614 assert_eq!(s2.len(), 0);
2615
2616 s |= s2;
2617 // bv: 1001_0011_0101_1010 0110_1100_1010_0101 1110_1011_1101_0111
2618 // 7 ^ 15 23 31 ^ 39 47
2619 // 10 35
2620 // s2 []
2621 // s |= s2 01_1010 0110_1100_1010_0101 111
2622 // res 1001_0011_0101_1010 0110_1100_1010_0101 1110_1011_1101_0111
2623 assert_eq!(s.read_u32(0), (0b01_1010_0110_1100_1010_0101_111, 25));
2624 assert_eq!(
2625 bv.read_u64(0),
2626 (
2627 0b1001_0011_0101_1010_0110_1100_1010_0101_1110_1011_1101_0111,
2628 48
2629 )
2630 );
2631 }
2632
2633 #[test]
2634 fn test_bit_or_slice_3() {
2635 let mut bv = BitVector::with_capacity(128);
2636 bv.push_u16(0b1001_0011_0101_1010, Some(16));
2637 bv.push_u16(0b0110_1100_1010_0101, Some(16));
2638 bv.push_u16(0b1110_1011_1101_0111, Some(16));
2639
2640 let mut s = &mut bv[10..35];
2641 assert_eq!(s.len(), 25);
2642
2643 let mut bv2 = BitVector::with_capacity(128);
2644 bv2.push_u16(0b1010_1011_1101_0111, Some(16));
2645 bv2.push_u16(0b0110_1101, Some(8));
2646 let s2 = &bv2[0..3];
2647 assert_eq!(s2.len(), 3);
2648
2649 s |= s2;
2650 // bv: 1001_0011_0101_1010 0110_1100_1010_0101 1110_1011_1101_0111
2651 // 7 ^ 15 23 31 ^ 39 47
2652 // 10 35
2653 // s2 10 1
2654 // s |= s2 11_1010 0110_1100_1010_0101 111
2655 // res 1001_0011_0111_1010 0110_1100_1010_0101 1110_1011_1101_0111
2656 assert_eq!(s.read_u32(0), (0b11_1010_0110_1100_1010_0101_111, 25));
2657 assert_eq!(
2658 bv.read_u64(0),
2659 (
2660 0b1001_0011_0111_1010_0110_1100_1010_0101_1110_1011_1101_0111,
2661 48
2662 )
2663 );
2664 }
2665
2666 #[test]
2667 fn test_bit_or_slice_8() {
2668 let mut bv = BitVector::with_capacity(128);
2669 bv.push_u16(0b1001_0011_0101_1010, Some(16));
2670 bv.push_u16(0b0110_1100_1010_0101, Some(16));
2671 bv.push_u16(0b1110_1011_1101_0111, Some(16));
2672
2673 let mut s = &mut bv[10..35];
2674 assert_eq!(s.len(), 25);
2675
2676 let mut bv2 = BitVector::with_capacity(128);
2677 bv2.push_u16(0b1010_1010_1101_0111, Some(16));
2678 bv2.push_u16(0b0110_1101, Some(8));
2679 let s2 = &bv2[0..8];
2680 assert_eq!(s2.len(), 8);
2681
2682 s |= s2;
2683 // bv: 1001_0011_0101_1010 0110_1100_1010_0101 1110_1011_1101_0111
2684 // 7 ^ 15 23 31 ^ 39 47
2685 // 10 35
2686 // s2 10 1010 10
2687 // s |= s2 11_1010 1110_1100_1010_0101 111
2688 // res 1001_0011_0111_1010 1110_1100_1010_0101 1110_1011_1101_0111
2689 assert_eq!(s.read_u32(0), (0b11_1010_1110_1100_1010_0101_111, 25));
2690 assert_eq!(
2691 bv.read_u64(0),
2692 (
2693 0b1001_0011_0111_1010_1110_1100_1010_0101_1110_1011_1101_0111,
2694 48
2695 )
2696 );
2697 }
2698
2699 #[test]
2700 fn test_bit_or_slice_32() {
2701 let mut bv = BitVector::with_capacity(128);
2702 bv.push_u16(0b1001_0011_0101_1010, Some(16));
2703 bv.push_u16(0b0110_1100_1010_0101, Some(16));
2704 bv.push_u16(0b1110_1011_1101_0111, Some(16));
2705
2706 let mut s = &mut bv[10..35];
2707 assert_eq!(s.len(), 25);
2708
2709 let mut bv2 = BitVector::with_capacity(128);
2710 bv2.push_u16(0b1010_1010_1101_0111, Some(16));
2711 bv2.push_u16(0b0110_1101_0011_1010, Some(16));
2712 let s2 = &bv2[0..32];
2713 assert_eq!(s2.len(), 32);
2714
2715 s |= s2;
2716 // 0 1 2 3
2717 // bv: 1001_0011_0101_1010 0110_1100_1010_0101 1110_1011_1101_0111
2718 // 7 ^ 15 23 31 ^ 39 47
2719 // 10 35
2720 // s2 10 1010 1011 0101 1101 1011 0100 11_1010
2721 // s |= s2 11_1010 1111 1101 1111 1111 111
2722 // res 1001_0011_0111_1010 1111 1101 1111 1111 1110_1011_1101_0111
2723
2724 assert_eq!(s.read_u32(0), (0b11_1010_1111_1101_1111_1111_111, 25));
2725 assert_eq!(
2726 bv.read_u64(0),
2727 (
2728 0b1001_0011_0111_1010_1111_1101_1111_1111_1110_1011_1101_0111,
2729 48
2730 )
2731 );
2732 }
2733
2734 #[test]
2735 fn test_bit_xor_slice_0() {
2736 let mut bv = BitVector::with_capacity(128);
2737 bv.push_u16(0b1001_0011_0101_1010, Some(16));
2738 bv.push_u16(0b0110_1100_1010_0101, Some(16));
2739 bv.push_u16(0b1110_1011_1101_0111, Some(16));
2740
2741 let mut s = &mut bv[10..35];
2742 assert_eq!(s.len(), 25);
2743
2744 let mut bv2 = BitVector::with_capacity(128);
2745 bv2.push_u16(0b1110_1011_1101_0111, Some(16));
2746 bv2.push_u16(0b0110_1101, Some(8));
2747 let s2 = &bv2[0..0];
2748 assert_eq!(s2.len(), 0);
2749
2750 s ^= s2;
2751 // bv: 1001_0011_0101_1010 0110_1100_1010_0101 1110_1011_1101_0111
2752 // 7 ^ 15 23 31 ^ 39 47
2753 // 10 35
2754 // s2 []
2755 // s ^= s2 01_1010 0110_1100_1010_0101 111
2756 // res 1001_0011_0101_1010 0110_1100_1010_0101 1110_1011_1101_0111
2757 assert_eq!(s.read_u32(0), (0b01_1010_0110_1100_1010_0101_111, 25));
2758 assert_eq!(
2759 bv.read_u64(0),
2760 (
2761 0b1001_0011_0101_1010_0110_1100_1010_0101_1110_1011_1101_0111,
2762 48
2763 )
2764 );
2765 }
2766
2767 #[test]
2768 fn test_bit_xor_slice_3() {
2769 let mut bv = BitVector::with_capacity(128);
2770 bv.push_u16(0b1001_0011_0101_1010, Some(16));
2771 bv.push_u16(0b0110_1100_1010_0101, Some(16));
2772 bv.push_u16(0b1110_1011_1101_0111, Some(16));
2773
2774 let mut s = &mut bv[10..35];
2775 assert_eq!(s.len(), 25);
2776
2777 let mut bv2 = BitVector::with_capacity(128);
2778 bv2.push_u16(0b1010_1011_1101_0111, Some(16));
2779 bv2.push_u16(0b0110_1101, Some(8));
2780 let s2 = &bv2[0..3];
2781 assert_eq!(s2.len(), 3);
2782
2783 s ^= s2;
2784 // bv: 1001_0011_0101_1010 0110_1100_1010_0101 1110_1011_1101_0111
2785 // 7 ^ 15 23 31 ^ 39 47
2786 // 10 35
2787 // s2 10 1
2788 // s ^= s2 11_0010 0110_1100_1010_0101 111
2789 // res 1001_0011_0111_0010 0110_1100_1010_0101 1110_1011_1101_0111
2790 assert_eq!(s.read_u32(0), (0b11_0010_0110_1100_1010_0101_111, 25));
2791 assert_eq!(
2792 bv.read_u64(0),
2793 (
2794 0b1001_0011_0111_0010_0110_1100_1010_0101_1110_1011_1101_0111,
2795 48
2796 )
2797 );
2798 }
2799
2800 #[test]
2801 fn test_bit_xor_slice_8() {
2802 let mut bv = BitVector::with_capacity(128);
2803 bv.push_u16(0b1001_0011_0101_1010, Some(16));
2804 bv.push_u16(0b0110_1100_1010_0101, Some(16));
2805 bv.push_u16(0b1110_1011_1101_0111, Some(16));
2806
2807 let mut s = &mut bv[10..35];
2808 assert_eq!(s.len(), 25);
2809
2810 let mut bv2 = BitVector::with_capacity(128);
2811 bv2.push_u16(0b1010_1010_1101_0111, Some(16));
2812 bv2.push_u16(0b0110_1101, Some(8));
2813 let s2 = &bv2[0..8];
2814 assert_eq!(s2.len(), 8);
2815
2816 s ^= s2;
2817 // bv: 1001_0011_0101_1010 0110_1100_1010_0101 1110_1011_1101_0111
2818 // 7 ^ 15 23 31 ^ 39 47
2819 // 10 35
2820 // s2 10 1010 10
2821 // s ^= s2 11_0000 1110_1100_1010_0101 111
2822 // res 1001_0011_0111_0000 1110_1100_1010_0101 1110_1011_1101_0111
2823 assert_eq!(s.read_u32(0), (0b11_0000_1110_1100_1010_0101_111, 25));
2824 assert_eq!(
2825 bv.read_u64(0),
2826 (
2827 0b1001_0011_0111_0000_1110_1100_1010_0101_1110_1011_1101_0111,
2828 48
2829 )
2830 );
2831 }
2832
2833 #[test]
2834 fn test_bit_xor_slice_32() {
2835 let mut bv = BitVector::with_capacity(128);
2836 bv.push_u16(0b1001_0011_0101_1010, Some(16));
2837 bv.push_u16(0b0110_1100_1010_0101, Some(16));
2838 bv.push_u16(0b1110_1011_1101_0111, Some(16));
2839
2840 let mut s = &mut bv[10..35];
2841 assert_eq!(s.len(), 25);
2842
2843 let mut bv2 = BitVector::with_capacity(128);
2844 bv2.push_u16(0b1010_1010_1101_0111, Some(16));
2845 bv2.push_u16(0b0110_1101_0011_1010, Some(16));
2846 let s2 = &bv2[0..32];
2847 assert_eq!(s2.len(), 32);
2848
2849 s ^= s2;
2850 // 0 1 2 3
2851 // bv: 1001_0011_0101_1010 0110_1100_1010_0101 1110_1011_1101_0111
2852 // 7 ^ 15 23 31 ^ 39 47
2853 // 10 35
2854 // 01_1010 0110_1100_1010_0101 111
2855 // s2 10 1010 1011 0101 1101 1011 0100 11_1010
2856 // s ^= s2 11_0000 1101 1001 0111 1110 101
2857 // res 1001_0011_0111_0000 1101 1001 0111 1110 1010_1011_1101_0111
2858
2859 assert_eq!(s.read_u32(0), (0b11_0000_1101_1001_0111_1110_101, 25));
2860 assert_eq!(
2861 bv.read_u64(0),
2862 (
2863 0b1001_0011_0111_0000_1101_1001_0111_1110_1010_1011_1101_0111,
2864 48
2865 )
2866 );
2867 }
2868
2869 #[test]
2870 fn test_subslice() {
2871 let mut bv = BitVector::with_capacity(128);
2872 bv.push_u16(0b1001_0011_0101_1010, Some(16));
2873
2874 let slice = &bv[8..16];
2875 assert_eq!(slice.len(), 8);
2876 assert_eq!(slice.read_u8(0), (0b0101_1010, 8));
2877 let ss = &slice[2..6];
2878 assert_eq!(ss.read_u8(0), (0b0000_0110, 4));
2879
2880 let mut slice = &mut bv[8..16];
2881 assert_eq!(slice.len(), 8);
2882 assert_eq!(slice.read_u8(0), (0b0101_1010, 8));
2883 let ss_mut = &mut slice[2..7];
2884 assert_eq!(ss_mut.len(), 5);
2885 assert_eq!(ss_mut.read_u8(0), (0b0000_1101, 5));
2886
2887 ss_mut.fill(true);
2888 assert_eq!(ss_mut.read_u16(0), (0b0001_1111, 5));
2889 ss_mut.fill(false);
2890 assert_eq!(ss_mut.read_u16(0), (0b0000_0000, 5));
2891 }
2892
2893 #[test]
2894 fn test_slice_fill_0() {
2895 let mut bv = BitVector::with_capacity(128);
2896 bv.push_u16(0b1001_0011_0101_1010, Some(16));
2897 bv.push_u16(0b0110_1100_1010_0101, Some(16));
2898 bv.push_u16(0b1110_1011_1101_0111, Some(16));
2899
2900 let s = &mut bv[10..35];
2901 assert_eq!(s.len(), 25);
2902 s.fill(false);
2903 assert_eq!(s.read_u32(0), (0, 25));
2904 assert_eq!(
2905 bv.read_u64(0),
2906 (
2907 0b1001_0011_0100_0000_0000_0000_0000_0000_000_0_1011_1101_0111,
2908 48
2909 )
2910 );
2911 }
2912
2913 #[test]
2914 fn test_slice_fill_1() {
2915 let mut bv = BitVector::with_capacity(128);
2916 bv.push_u16(0b1001_0011_0101_1010, Some(16));
2917 bv.push_u16(0b0110_1100_1010_0101, Some(16));
2918 bv.push_u16(0b1110_1011_1101_0111, Some(16));
2919
2920 let s = &mut bv[10..35];
2921 assert_eq!(s.len(), 25);
2922 s.fill(true);
2923 assert_eq!(s.read_u32(0), (0b11_1111_1111_1111_1111_1111_111, 25));
2924 assert_eq!(
2925 bv.read_u64(0),
2926 (
2927 0b1001_0011_0111_1111_1111_1111_1111_1111_1110_1011_1101_0111,
2928 48
2929 )
2930 );
2931 }
2932
2933 #[test]
2934 fn test_all() {
2935 //test that all bits in the slice are 1
2936 let mut bv = BitVector::with_capacity(128);
2937 bv.push_u128(u128::MAX, None);
2938 let slice = &bv[..];
2939 assert_eq!(slice.all(), true);
2940
2941 let mut bv = BitVector::with_capacity(128);
2942 bv.push_u16(0b1001_0011_1111_1111, Some(16));
2943 assert_eq!(bv[0..8].all(), false);
2944 assert_eq!(bv[8..16].all(), true);
2945 assert_eq!(bv[0..1].all(), true);
2946 assert_eq!(bv[1..3].all(), false);
2947 assert_eq!(bv[6..].all(), true);
2948 }
2949
2950 #[test]
2951 fn test_any() {
2952 let mut bv = BitVector::with_capacity(128);
2953 bv.push_u128(u128::MAX, None);
2954 let slice = &bv[..];
2955 assert_eq!(slice.any(), true);
2956
2957 let mut bv = BitVector::with_capacity(128);
2958 bv.push_u128(0, Some(128));
2959 let slice = &bv[..];
2960 assert_eq!(slice.any(), false);
2961
2962 let mut bv = BitVector::with_capacity(128);
2963 bv.push_u16(0b1001_0011_1111_0000, Some(16));
2964 assert_eq!(bv[0..8].any(), true);
2965 assert_eq!(bv[8..16].any(), true);
2966 assert_eq!(bv[0..1].any(), true);
2967 assert_eq!(bv[1..3].any(), false);
2968 assert_eq!(bv[6..].any(), true);
2969 assert_eq!(bv[12..].any(), false);
2970 }
2971
2972 #[test]
2973 fn test_get_mut() {
2974 let mut bv = BitVector::with_capacity(20);
2975 bv.push_u8(0b1010_1101, None);
2976
2977 let s = &mut bv[2..7];
2978 *s.get_mut(0).unwrap() = false;
2979 *s.get_mut(1).unwrap() = true;
2980 *s.get_mut(2).unwrap() = false;
2981 *s.get_mut(3).unwrap() = false;
2982 *s.get_mut(4).unwrap() = true;
2983
2984 assert_eq!(bv.read_u8(0), (0b1001_0011, 8));
2985 }
2986
2987 #[test]
2988 fn test_iter_mut() {
2989 let mut bv = BitVector::with_capacity(20);
2990 bv.push_u8(0b1011_1100, None);
2991 assert_eq!(bv[0], true);
2992 let s = &mut bv[0..7];
2993 let iter = s.iter_mut();
2994 for mut bit in iter {
2995 *bit = true;
2996 }
2997 assert_eq!(bv.read_u8(0), (0b1111_1110, 8));
2998
2999 let s = &mut bv[0..7];
3000 for mut bit in s.iter_mut() {
3001 *bit = false;
3002 }
3003 assert_eq!(bv.read_u8(0), (0b0000_0000, 8));
3004 }
3005
3006 #[test]
3007 fn test_first() {
3008 let mut bv = BitVector::with_capacity(20);
3009 bv.push_u8(0b1011_1100, None);
3010 assert_eq!(bv[0], true);
3011 let s = &bv[0..8];
3012 assert_eq!(*(s.first().unwrap()), true);
3013 assert_eq!(s.first().as_deref(), Some(&true));
3014 assert_eq!(bv[0], true);
3015 }
3016
3017 #[test]
3018 fn test_first_mut() {
3019 let mut bv = BitVector::with_capacity(20);
3020 bv.push_u8(0b1011_1100, None);
3021 assert_eq!(bv[0], true);
3022 let s = &mut bv[0..8];
3023 assert_eq!(*(s.first_mut().unwrap()), true);
3024 *s.first_mut().unwrap() = false;
3025 assert_eq!(*(s.first_mut().unwrap()), false);
3026 assert_eq!(s.first().as_deref(), Some(&false));
3027 assert_eq!(bv[0], false);
3028 }
3029
3030 #[test]
3031 fn test_last() {
3032 let mut bv = BitVector::with_capacity(20);
3033 bv.push_u8(0b1011_1100, None);
3034 assert_eq!(bv[7], false);
3035 let s = &bv[0..8];
3036 assert_eq!(*(s.last().unwrap()), false);
3037 assert_eq!(s.last().as_deref(), Some(&false));
3038 assert_eq!(bv[7], false);
3039 }
3040
3041 #[test]
3042 fn test_last_mut() {
3043 let mut bv = BitVector::with_capacity(20);
3044 bv.push_u8(0b0000_0000, Some(8));
3045 assert_eq!(bv[7], false);
3046 let s = &mut bv[0..8];
3047 assert_eq!(*(s.last_mut().unwrap()), false);
3048 *s.last_mut().unwrap() = true;
3049 assert_eq!(*(s.last().unwrap()), true);
3050 assert_eq!(s.last().as_deref(), Some(&true));
3051 assert_eq!(bv[7], true);
3052 }
3053
3054 #[test]
3055 fn test_iter_ones() {
3056 let mut bv = BitVector::with_capacity(128);
3057 bv.push_u16(0b1100_1010_0011_0101, None);
3058 bv.push_u16(0b1000_1100_0011_1111, None);
3059 let s = &bv[2..2];
3060 let mut iter = s.iter_ones();
3061 assert_eq!(iter.next(), None);
3062
3063 let s = &bv[2..4];
3064 let mut iter = s.iter_ones();
3065 assert_eq!(iter.next(), None);
3066
3067 let s = &bv[2..19];
3068 let mut iter = s.iter_ones();
3069 assert_eq!(iter.next(), Some(2));
3070 assert_eq!(iter.next(), Some(4));
3071 assert_eq!(iter.next(), Some(8));
3072 assert_eq!(iter.next(), Some(9));
3073 assert_eq!(iter.next(), Some(11));
3074 assert_eq!(iter.next(), Some(13));
3075 assert_eq!(iter.next(), Some(14));
3076 assert_eq!(iter.next(), None);
3077 }
3078
3079 #[test]
3080 fn test_iter_zeros() {
3081 let mut bv = BitVector::with_capacity(128);
3082 bv.push_u16(0b0011_0101_1100_1010, Some(16));
3083 bv.push_u16(0b0111_0011_1100_0000, Some(16));
3084
3085 let s = &bv[2..2];
3086
3087 let mut iter = s.iter_zeros(); //IterZeros::new(&s.0, s.offset(), s.len());
3088 assert_eq!(iter.next(), None);
3089
3090 let s = &bv[2..4];
3091 let mut iter = s.iter_zeros(); //IterZeros::new(&s.0, s.offset(), s.len());
3092 assert_eq!(iter.next(), None);
3093
3094 let s = &bv[2..19];
3095 let mut iter = s.iter_zeros(); //IterZeros::new(&s.0, s.offset(), s.len());
3096 assert_eq!(iter.next(), Some(2));
3097 assert_eq!(iter.next(), Some(4));
3098 assert_eq!(iter.next(), Some(8));
3099 assert_eq!(iter.next(), Some(9));
3100 assert_eq!(iter.next(), Some(11));
3101 assert_eq!(iter.next(), Some(13));
3102 assert_eq!(iter.next(), Some(14));
3103 assert_eq!(iter.next(), None);
3104 }
3105
3106 #[test]
3107 fn test_count_ones() {
3108 let mut bv = BitVector::with_capacity(20);
3109 bv.push_u8(0b1111_1000, Some(8));
3110 bv.push_u8(0b0011_1000, Some(8));
3111
3112 let s = &bv[4..10];
3113 assert_eq!(s.count_ones(), 1);
3114
3115 let mut bv = BitVector::with_capacity(20);
3116 bv.push_u8(0b1110_1111, Some(8));
3117 let s = &bv[2..6];
3118 assert_eq!(s.count_ones(), 3);
3119 }
3120}