deepmesa_collections/bitvec/
mod.rs

1mod bitops;
2
3#[macro_use]
4mod macros;
5
6pub(crate) mod bitref;
7pub(crate) mod bitslice;
8pub(crate) mod bitvec;
9pub(super) mod bytes;
10pub(crate) mod iter;
11pub(crate) mod traits;
12
13pub use crate::bitvec::bitslice::BitSlice;
14pub use crate::bitvec::iter::Iter;
15pub use crate::bitvec::iter::IterMut;
16pub use crate::bitvec::iter::IterOnes;
17pub use crate::bitvec::iter::IterU128;
18pub use crate::bitvec::iter::IterU16;
19pub use crate::bitvec::iter::IterU32;
20pub use crate::bitvec::iter::IterU64;
21pub use crate::bitvec::iter::IterU8;
22pub use crate::bitvec::iter::IterZeros;
23pub use crate::bitvec::traits::AsLsb0;
24pub use crate::bitvec::traits::AsMsb0;
25pub use crate::bitvec::traits::BitwiseClear;
26pub use crate::bitvec::traits::BitwiseClearAssign;
27pub use crate::bitvec::traits::BitwiseLsb;
28pub use crate::bitvec::traits::BitwiseLsbAssign;
29pub use crate::bitvec::traits::BitwiseMsb;
30pub use crate::bitvec::traits::BitwiseMsbAssign;
31pub use crate::bitvec::traits::BitwisePartial;
32pub use crate::bitvec::traits::BitwisePartialAssign;
33pub use crate::bitvec::traits::NotLsb;
34pub use crate::bitvec::traits::NotLsbAssign;
35pub use crate::bitvec::traits::NotMsb;
36pub use crate::bitvec::traits::NotMsbAssign;
37pub use crate::bitvec::traits::NotPartial;
38pub use crate::bitvec::traits::NotPartialAssign;
39
40type BitCount = usize;
41
42/// This enum indicates the order in which bits are traversed
43/// and counted.
44///
45/// [`Msb0`](#variant.Msb0) indicates that the MSB (Most Significant
46/// Bit) should be considered as position `0` and consequently bits
47/// should be counted from the MSB to the LSB.
48///
49/// [`Lsb0`](#variant.Lsb0) indicates tht the LSB (Least Significant
50/// Bit) should be considered as position `0` and the bits should
51/// therefore be counter from the LSB to the MSB.
52///
53/// Here is an illustrative example:
54///
55/// # Examples
56/// ```text
57/// let val: u8 = 0b1011_1100;
58/// ```
59/// Counting 4 bits from the `Msb0` would yield `1011` while counting 4
60/// bits from the `Lsb0` would result in `1100`
61///
62#[derive(Debug, Clone, Copy, PartialEq, Eq)]
63pub enum BitOrder {
64    /// Counts bits from the LSB (Least Significant Bit) to the MSB
65    /// (Most Significant Bit).
66    Lsb0,
67    /// Counts bits from the MSB (Most Significant Bit) to the LSB
68    /// (Least Significant Bit).
69    Msb0,
70}
71
72/// A macro to construct a new [`BitVector`](struct.BitVector.html) from a bit pattern.
73///
74/// This macro accepts a sequence of 1s and 0s separated by commas,
75/// (an array of bits effectively) and creates a new BitVector from
76/// that pattern. Any token other than a `0` or a `1` will result in a
77/// panic.
78///
79/// # Examples
80/// ```
81/// use deepmesa_collections::BitVector;
82/// use deepmesa_collections::bitvector;
83///
84/// let bv = bitvector![1,0,1,1,0,1,0,1];
85/// assert_eq!(bv.len(), 8);
86/// assert_eq!(bv.read_u8(0), (0b1011_0101, 8));
87/// ```
88///
89/// The macro can also accept a single bit (1 or 0) and a length and
90/// construct a BitVector of that length with the repeating bit.
91///
92/// # Examples
93/// ```
94/// use deepmesa_collections::BitVector;
95/// use deepmesa_collections::bitvector;
96///
97/// let bv = bitvector![1;100];
98/// assert_eq!(bv.len(), 100);
99/// ```
100///
101/// Finally this macro can also be used to construct an empty
102/// BitVector.
103///
104/// # Examples
105/// ```
106/// use deepmesa_collections::BitVector;
107/// use deepmesa_collections::bitvector;
108///
109/// let bv = bitvector!();
110/// assert_eq!(bv.len(), 0);
111/// ```
112#[macro_export]
113macro_rules! bitvector {
114    () => {
115        BitVector::new()
116    };
117    ($($arg:tt)*) => {{
118        let slice = [$($arg)*];
119        let mut bv = BitVector::new();
120        for item in &slice {
121            match item {
122                0 => bv.push(false),
123                1 => bv.push(true),
124                _ => panic!("{} is not a binary digit", item),
125            }
126        }
127        bv
128    }};
129    ($item: expr, $len: expr) => {
130        {
131            match $item {
132                0=> BitVector::repeat(false, $len),
133                1=> BitVector::repeat(true, $len),
134                _ => panic!("{} is not a binary digit", item),
135            }
136        }
137    };
138}