├── .gitignore ├── Cargo.toml ├── src ├── bits │ └── join_ones.rs ├── internal.rs ├── macros.rs ├── endian.rs ├── lib.rs ├── slice.rs ├── array.rs ├── number.rs ├── bits_mut.rs ├── bits_owned.rs ├── bits.rs └── set.rs ├── .github └── workflows │ └── ci.yml └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /Cargo.lock 3 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "bittle" 3 | version = "0.6.0" 4 | authors = ["John-John Tedro "] 5 | edition = "2021" 6 | rust-version = "1.65" 7 | description = "Zero-cost bitsets over native Rust types" 8 | documentation = "https://docs.rs/bittle" 9 | readme = "README.md" 10 | homepage = "https://github.com/udoprog/bittle" 11 | repository = "https://github.com/udoprog/bittle" 12 | license = "MIT OR Apache-2.0" 13 | keywords = ["bits", "bitvec", "container", "data-structure", "no_std"] 14 | categories = ["data-structures"] 15 | -------------------------------------------------------------------------------- /src/bits/join_ones.rs: -------------------------------------------------------------------------------- 1 | use crate::Endian; 2 | 3 | use super::Bits; 4 | 5 | /// A joined iterator. 6 | /// 7 | /// Created using [`Bits::join_ones`][crate::Bits::join_ones]. 8 | pub struct JoinOnes<'a, A, E, B> 9 | where 10 | A: 'a + ?Sized + Bits, 11 | E: Endian, 12 | { 13 | mask: A::IterOnesIn<'a, E>, 14 | iter: B, 15 | last: u32, 16 | } 17 | 18 | impl<'a, A, E, B> JoinOnes<'a, A, E, B> 19 | where 20 | A: ?Sized + Bits, 21 | E: Endian, 22 | { 23 | pub(crate) fn new(mask: A::IterOnesIn<'a, E>, iter: B) -> Self { 24 | Self { 25 | mask, 26 | iter, 27 | last: 0, 28 | } 29 | } 30 | } 31 | 32 | impl<'a, A, E, B> Iterator for JoinOnes<'a, A, E, B> 33 | where 34 | A: 'a + ?Sized + Bits, 35 | E: Endian, 36 | B: Iterator, 37 | { 38 | type Item = B::Item; 39 | 40 | fn next(&mut self) -> Option { 41 | let index = self.mask.next()?; 42 | let offset = index - self.last; 43 | let buf = self.iter.nth(offset as usize)?; 44 | self.last = index + 1; 45 | Some(buf) 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | pull_request: {} 5 | push: 6 | branches: 7 | - main 8 | schedule: 9 | - cron: '35 2 * * 6' 10 | 11 | concurrency: 12 | group: ${{ github.workflow }}-${{ github.ref }} 13 | cancel-in-progress: true 14 | 15 | jobs: 16 | msrv: 17 | runs-on: ubuntu-latest 18 | steps: 19 | - uses: actions/checkout@v4 20 | - uses: dtolnay/rust-toolchain@1.65 21 | - run: cargo build --workspace --lib 22 | 23 | test: 24 | runs-on: ubuntu-latest 25 | steps: 26 | - uses: actions/checkout@v4 27 | - uses: dtolnay/rust-toolchain@stable 28 | - run: cargo test --workspace --all-targets 29 | - run: cargo test --workspace --doc 30 | 31 | clippy: 32 | runs-on: ubuntu-latest 33 | steps: 34 | - uses: actions/checkout@v4 35 | - uses: dtolnay/rust-toolchain@stable 36 | with: 37 | components: clippy 38 | - run: cargo clippy --all-targets -- -D warnings 39 | 40 | rustfmt: 41 | runs-on: ubuntu-latest 42 | steps: 43 | - uses: actions/checkout@v4 44 | - uses: dtolnay/rust-toolchain@stable 45 | with: 46 | components: rustfmt 47 | - run: cargo fmt --check --all 48 | -------------------------------------------------------------------------------- /src/internal.rs: -------------------------------------------------------------------------------- 1 | macro_rules! impl_iter { 2 | ( 3 | $(#[$meta:meta])* 4 | {$($i:tt)*} $name:ident < $($lt:lifetime ,)? $($g:ident),* > 5 | {$build:ident, $assoc:path, $iter:path} 6 | {$var:ident : $var_ty:ty => $len:expr} 7 | {$($clone_bound:tt)*} 8 | ) => { 9 | $(#[$meta])* 10 | pub struct $name<$($lt ,)? $($i)*> 11 | where 12 | T: BitsOwned, 13 | E: Endian, 14 | { 15 | iter: $iter, 16 | head: (Option<$assoc>, u32), 17 | tail: (Option<$assoc>, u32), 18 | } 19 | 20 | impl<$($lt ,)? $($i)*> $name<$($lt ,)? $($g),*> 21 | where 22 | T: BitsOwned, 23 | E: Endian, 24 | { 25 | #[inline] 26 | pub(crate) fn new($var: $var_ty) -> Self { 27 | let mut iter = $var.into_iter(); 28 | let head = (iter.next().map(|v| v.$build()), 0); 29 | let tail = (iter.next_back().map(|v| v.$build()), u32::try_from($len).ok().and_then(|v| v.saturating_sub(1).checked_mul(T::BITS)).expect("collection overflow")); 30 | Self { iter, head, tail } 31 | } 32 | } 33 | 34 | impl<$($lt ,)? $($i)*> Clone for $name<$($lt ,)? $($g),*> 35 | where 36 | T: BitsOwned, 37 | E: Endian, 38 | $assoc: Clone, 39 | $($clone_bound)* 40 | { 41 | #[inline] 42 | fn clone(&self) -> Self { 43 | Self { 44 | iter: self.iter.clone(), 45 | head: self.head.clone(), 46 | tail: self.tail.clone(), 47 | } 48 | } 49 | } 50 | 51 | impl<$($lt ,)? $($i)*> Iterator for $name<$($lt ,)? $($g),*> 52 | where 53 | T: BitsOwned, 54 | E: Endian, 55 | { 56 | type Item = u32; 57 | 58 | fn next(&mut self) -> Option { 59 | while let Some((index, o)) = $crate::internal::next_or_unset(&mut self.head, Iterator::next) { 60 | if let index @ Some(_) = index { 61 | return index; 62 | } 63 | 64 | self.head = (self.iter.next().map(|v| v.$build()), o.checked_add(T::BITS)?); 65 | } 66 | 67 | let (index, _) = $crate::internal::next_or_unset(&mut self.tail, Iterator::next)?; 68 | index 69 | } 70 | } 71 | 72 | impl<$($lt ,)? $($i)*> DoubleEndedIterator for $name<$($lt ,)? $($g),*> 73 | where 74 | T: BitsOwned, 75 | $assoc: DoubleEndedIterator, 76 | E: Endian, 77 | { 78 | fn next_back(&mut self) -> Option { 79 | while let Some((index, o)) = $crate::internal::next_or_unset(&mut self.tail, DoubleEndedIterator::next_back) { 80 | if let index @ Some(_) = index { 81 | return index; 82 | } 83 | 84 | self.tail = (self.iter.next_back().map(|v| v.$build()), o.checked_sub(T::BITS)?); 85 | } 86 | 87 | let (index, _) = $crate::internal::next_or_unset(&mut self.head, DoubleEndedIterator::next_back)?; 88 | index 89 | } 90 | } 91 | 92 | impl<$($lt ,)? $($i)*> core::iter::FusedIterator for $name<$($lt ,)? $($g),*> 93 | where 94 | T: BitsOwned, 95 | E: Endian, 96 | { 97 | } 98 | }; 99 | } 100 | 101 | #[inline] 102 | pub(crate) fn next_or_unset( 103 | iter: &mut (Option, u32), 104 | advance: F, 105 | ) -> Option<(Option, u32)> 106 | where 107 | F: FnOnce(&mut I) -> Option, 108 | { 109 | let &mut (Some(ref mut it), offset) = iter else { 110 | return None; 111 | }; 112 | 113 | let value = if let Some(index) = advance(it) { 114 | offset.checked_add(index) 115 | } else { 116 | iter.0 = None; 117 | None 118 | }; 119 | 120 | Some((value, offset)) 121 | } 122 | -------------------------------------------------------------------------------- /src/macros.rs: -------------------------------------------------------------------------------- 1 | /// Construct a bit set with specific values set using [`DefaultEndian`] 2 | /// indexing. 3 | /// 4 | /// [`DefaultEndian`]: crate::DefaultEndian 5 | /// 6 | /// # Examples 7 | /// 8 | /// ``` 9 | /// use bittle::Bits; 10 | /// 11 | /// let mask: u8 = bittle::set![0, 1, 3]; 12 | /// assert!(mask.iter_ones().eq([0, 1, 3])); 13 | /// assert_eq!(mask, 0b00001011); 14 | /// ``` 15 | /// 16 | /// Set ranges of bits: 17 | /// 18 | /// ``` 19 | /// use bittle::Bits; 20 | /// 21 | /// let mask: u8 = bittle::set![0..=4, 6..]; 22 | /// assert!(mask.iter_ones().eq([0, 1, 2, 3, 4, 6, 7])); 23 | /// ``` 24 | #[macro_export] 25 | macro_rules! set { 26 | (priv $set:ident {$_:ident},) => {}; 27 | 28 | (priv-range $set:ident {$set_bit:ident}, $range:expr) => {{ 29 | for index in $range { 30 | $crate::BitsMut::$set_bit(&mut $set, index); 31 | } 32 | }}; 33 | 34 | (priv $set:ident {$($tt:tt)*}, $from:literal ..= $to:literal) => {{ 35 | $crate::set!(priv-range $set {$($tt)*}, $from ..= $to); 36 | }}; 37 | 38 | (priv $set:ident {$($tt:tt)*}, $from:literal ..= $to:literal, $($rest:tt)*) => {{ 39 | $crate::set!(priv-range $set {$($tt)*}, $from ..= $to); 40 | $crate::set!(priv $set {$($tt)*}, $($rest)*); 41 | }}; 42 | 43 | (priv $set:ident {$($tt:tt)*}, $from:literal .. $to:literal) => {{ 44 | $crate::set!(priv-range $set {$($tt)*}, $from .. $to); 45 | }}; 46 | 47 | (priv $set:ident {$($tt:tt)*}, $from:literal .. $to:literal, $($rest:tt)*) => {{ 48 | $crate::set!(priv-range $set {$($tt)*}, $from .. $to); 49 | $crate::set!(priv $set {$($tt)*}, $($rest)*); 50 | }}; 51 | 52 | (priv $set:ident {$($tt:tt)*}, $from:literal ..) => {{ 53 | $crate::set!(priv-range $set {$($tt)*}, $from .. $crate::Bits::bits_capacity(&$set)); 54 | }}; 55 | 56 | (priv $set:ident {$($tt:tt)*}, $from:literal .., $($rest:tt)*) => {{ 57 | $crate::set!(priv-range $set {$($tt)*}, $from .. $crate::Bits::bits_capacity(&$set)); 58 | $crate::set!(priv $set {$($tt)*}, $($rest)*); 59 | }}; 60 | 61 | (priv $set:ident {$set_bit:ident}, $index:expr) => {{ 62 | $crate::BitsMut::$set_bit(&mut $set, $index); 63 | }}; 64 | 65 | (priv $set:ident {$($tt:tt)*}, $index:expr, $($rest:tt)*) => {{ 66 | $crate::set!(priv $set {$($tt)*}, $index); 67 | $crate::set!(priv $set {$($tt)*}, $($rest)*); 68 | }}; 69 | 70 | ($($tt:tt)*) => {{ 71 | let mut set = $crate::BitsOwned::ZEROS; 72 | $crate::set!(priv set {set_bit}, $($tt)*); 73 | set 74 | }}; 75 | } 76 | 77 | /// Construct a bit set with specific values set using [`LittleEndian`] 78 | /// indexing. 79 | /// 80 | /// [`LittleEndian`]: crate::LittleEndian 81 | /// 82 | /// # Examples 83 | /// 84 | /// ``` 85 | /// use bittle::Bits; 86 | /// 87 | /// let mask: u8 = bittle::set_le![0, 1, 3]; 88 | /// assert!(mask.iter_ones_le().eq([0, 1, 3])); 89 | /// assert_eq!(mask, 0b11010000); 90 | /// ``` 91 | /// 92 | /// Set ranges of bits: 93 | /// 94 | /// ``` 95 | /// use bittle::Bits; 96 | /// 97 | /// let mask: u8 = bittle::set_le![0..=4, 6..]; 98 | /// assert!(mask.iter_ones_le().eq([0, 1, 2, 3, 4, 6, 7])); 99 | /// assert_eq!(mask, 0b11111011); 100 | /// ``` 101 | #[macro_export] 102 | macro_rules! set_le { 103 | ($($tt:tt)*) => {{ 104 | let mut set = $crate::BitsOwned::ZEROS; 105 | $crate::set!(priv set {set_bit_le}, $($tt)*); 106 | set 107 | }}; 108 | } 109 | 110 | /// Construct a bit set with specific values set using [`BigEndian`] 111 | /// indexing. 112 | /// 113 | /// [`BigEndian`]: crate::BigEndian 114 | /// 115 | /// # Examples 116 | /// 117 | /// ``` 118 | /// use bittle::Bits; 119 | /// 120 | /// let mask: u8 = bittle::set_be![0, 1, 3]; 121 | /// assert!(mask.iter_ones_be().eq([0, 1, 3])); 122 | /// assert_eq!(mask, 0b00001011); 123 | /// ``` 124 | /// 125 | /// Set ranges of bits: 126 | /// 127 | /// ``` 128 | /// use bittle::Bits; 129 | /// 130 | /// let mask: u8 = bittle::set_be![0..=4, 6..]; 131 | /// assert!(mask.iter_ones_be().eq([0, 1, 2, 3, 4, 6, 7])); 132 | /// assert_eq!(mask, 0b11011111); 133 | /// ``` 134 | #[macro_export] 135 | macro_rules! set_be { 136 | ($($tt:tt)*) => {{ 137 | let mut set = $crate::BitsOwned::ZEROS; 138 | $crate::set!(priv set {set_bit_be}, $($tt)*); 139 | set 140 | }}; 141 | } 142 | -------------------------------------------------------------------------------- /src/endian.rs: -------------------------------------------------------------------------------- 1 | //! Traits for dealing with bitset endianness. 2 | //! 3 | //! Endianness typically refers to the order in which bytes are in memory, but 4 | //! here the concept refers to the order in which bits are addressed when you 5 | //! consider the binary literal of a primitive. 6 | //! 7 | //! * [`BigEndian`] where the bit furthest to the left refers to the highest 8 | //! index. 9 | //! * [`LittleEndian`] where the bit furthest to the left refers to the lowest 10 | //! index. 11 | 12 | #![allow(clippy::module_name_repetitions)] 13 | 14 | use crate::number::Number; 15 | 16 | mod sealed { 17 | pub trait Sealed {} 18 | impl Sealed for super::BigEndian {} 19 | impl Sealed for super::LittleEndian {} 20 | } 21 | 22 | /// Trait governing endian-dependent operations for a primitive. 23 | pub trait Endian: self::sealed::Sealed { 24 | #[doc(hidden)] 25 | fn mask(index: u32) -> T 26 | where 27 | T: Number; 28 | 29 | #[doc(hidden)] 30 | fn ones(value: T) -> u32 31 | where 32 | T: Number; 33 | 34 | #[doc(hidden)] 35 | fn ones_rev(value: T) -> u32 36 | where 37 | T: Number; 38 | 39 | #[doc(hidden)] 40 | fn zeros(value: T) -> u32 41 | where 42 | T: Number; 43 | 44 | #[doc(hidden)] 45 | fn zeros_rev(value: T) -> u32 46 | where 47 | T: Number; 48 | } 49 | 50 | /// Big-endian indexing for bit sets. 51 | /// 52 | /// This can be used in combination with methods such as [`Bits::test_bit_in`]. 53 | /// 54 | /// Big-endian indexing is constructed increasingly from right to left for 55 | /// individual primitives, such as the following [`u8`] literal: 56 | /// 57 | /// ```text 58 | /// 0b0010_0010u8 59 | /// ^ ^- index 1 60 | /// '------ index 5 61 | /// ``` 62 | /// 63 | /// Arrays are treated the same as expected where the index grows from smallest 64 | /// to largest address, but each interior primitive is indexed in big endian 65 | /// ordering. 66 | /// 67 | /// ```text 68 | /// 0 --------- 8 8 -------- 15 69 | /// [0b0010_0010u8, 0b1000_0000u8] 70 | /// ^ ^ ^- index 15 71 | /// | '--------- index 1 72 | /// '-------------- index 5 73 | /// ``` 74 | /// 75 | /// [`Bits::test_bit_in`]: crate::Bits::test_bit_in 76 | #[non_exhaustive] 77 | pub struct BigEndian; 78 | 79 | impl Endian for BigEndian { 80 | #[inline] 81 | fn mask(index: u32) -> T 82 | where 83 | T: Number, 84 | { 85 | T::BIT_RIGHT.wrapping_shl(index) 86 | } 87 | 88 | #[inline] 89 | fn ones(value: T) -> u32 90 | where 91 | T: Number, 92 | { 93 | value.trailing_ones() 94 | } 95 | 96 | #[inline] 97 | fn ones_rev(value: T) -> u32 98 | where 99 | T: Number, 100 | { 101 | value.leading_ones() 102 | } 103 | 104 | #[inline] 105 | fn zeros(value: T) -> u32 106 | where 107 | T: Number, 108 | { 109 | value.trailing_zeros() 110 | } 111 | 112 | #[inline] 113 | fn zeros_rev(value: T) -> u32 114 | where 115 | T: Number, 116 | { 117 | value.leading_zeros() 118 | } 119 | } 120 | 121 | /// Little-endian indexing for bit sets. 122 | /// 123 | /// This can be used in combination with methods such as [`Bits::test_bit_in`]. 124 | /// 125 | /// Little-endian indexing is constructed increasingly from left to right for 126 | /// individual primitives, such as the following [`u8`] literal: 127 | /// 128 | /// ```text 129 | /// 0b0010_0010u8 130 | /// ^ ^- index 6 131 | /// '------ index 2 132 | /// ``` 133 | /// 134 | /// Arrays are treated the same as expected where the index grows from smallest 135 | /// to largest address: 136 | /// 137 | /// ```text 138 | /// 0 --------- 8 8 -------- 15 139 | /// [0b0010_0010u8, 0b1000_0000u8] 140 | /// ^ ^ ^- index 8 141 | /// | '--------- index 6 142 | /// '-------------- index 2 143 | /// ``` 144 | /// 145 | /// [`Bits::test_bit_in`]: crate::Bits::test_bit_in 146 | #[non_exhaustive] 147 | pub struct LittleEndian; 148 | 149 | impl Endian for LittleEndian { 150 | #[inline] 151 | fn mask(index: u32) -> T 152 | where 153 | T: Number, 154 | { 155 | T::BIT_LEFT.wrapping_shr(index) 156 | } 157 | 158 | #[inline] 159 | fn ones(value: T) -> u32 160 | where 161 | T: Number, 162 | { 163 | value.leading_ones() 164 | } 165 | 166 | #[inline] 167 | fn ones_rev(value: T) -> u32 168 | where 169 | T: Number, 170 | { 171 | value.trailing_ones() 172 | } 173 | 174 | #[inline] 175 | fn zeros(value: T) -> u32 176 | where 177 | T: Number, 178 | { 179 | value.leading_zeros() 180 | } 181 | 182 | #[inline] 183 | fn zeros_rev(value: T) -> u32 184 | where 185 | T: Number, 186 | { 187 | value.trailing_ones() 188 | } 189 | } 190 | 191 | /// The default endianness to use. 192 | pub type DefaultEndian = BigEndian; 193 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bittle 2 | 3 | [github](https://github.com/udoprog/bittle) 4 | [crates.io](https://crates.io/crates/bittle) 5 | [docs.rs](https://docs.rs/bittle) 6 | [build status](https://github.com/udoprog/bittle/actions?query=branch%3Amain) 7 | 8 | Zero-cost bitsets over native Rust types. 9 | 10 | The name `bittle` comes from `bit` and `little`. Small bitsets! 11 | 12 |
13 | 14 | ## Usage 15 | 16 | Add `bittle` as a dependency in your `Cargo.toml`: 17 | 18 |
19 | 20 | ```toml 21 | [dependencies] 22 | bittle = "0.6.0" 23 | ``` 24 | 25 |
26 | 27 | ## Guide 28 | 29 | A bit is always identified by a [`u32`] by its index, and the exact location 30 | for a bit in a primitive numbers is defined by its endianness, which is 31 | [`BigEndian`] by default. 32 | 33 | [`BigEndian`] indexing grows from right to left, such as the following 34 | [`u8`] literal: 35 | 36 | ```text 37 | 0b0010_0010u8 38 | ^ ^- index 1 39 | '------ index 5 40 | ``` 41 | 42 |
43 | 44 | To interact with these bits we define the [`Bits`], [`BitsMut`], and 45 | [`BitsOwned`] traits. These traits are implemented for primitive types such 46 | as `u32`, `[u32; 4]`, or `&[u32]`: 47 | 48 | ```rust 49 | use bittle::Bits; 50 | 51 | let array: [u32; 4] = [0, 1, 2, 3]; 52 | assert!(array.iter_ones().eq([32, 65, 96, 97])); 53 | 54 | let n = 0b00000000_00000000_00000000_00010001u32; 55 | assert!(n.iter_ones().eq([0, 4])); 56 | 57 | let array_of_arrays: [[u8; 4]; 2] = [[16, 0, 0, 0], [0, 0, 1, 0]]; 58 | assert!(array_of_arrays.iter_ones().eq([4, 48])); 59 | 60 | let mut vec: Vec = vec![0, 1, 2, 3]; 61 | assert!(vec.iter_ones().eq([32, 65, 96, 97])); 62 | ``` 63 | 64 |
65 | 66 | We also provide the [`set!`] macro, which is a zero-cost convenience method 67 | for constructing primitive forms of bit sets: 68 | 69 | ```rust 70 | use bittle::Bits; 71 | 72 | let array: [u32; 4] = bittle::set![32, 65, 96, 97]; 73 | assert!(array.iter_ones().eq([32, 65, 96, 97])); 74 | 75 | let n: u32 = bittle::set![0, 4]; 76 | assert!(n.iter_ones().eq([0, 4])); 77 | 78 | let array_of_arrays: [[u8; 4]; 2] = bittle::set![4, 48]; 79 | assert!(array_of_arrays.iter_ones().eq([4, 48])); 80 | ``` 81 | 82 |
83 | 84 | Since a vector is not a primitive bit set, it could instead make use of 85 | [`BitsMut`] directly: 86 | 87 | ```rust 88 | use bittle::{Bits, BitsMut}; 89 | 90 | let mut vec: Vec = vec![0u32; 4]; 91 | vec.set_bit(32); 92 | vec.set_bit(65); 93 | vec.set_bit(96); 94 | vec.set_bit(97); 95 | assert!(vec.iter_ones().eq([32, 65, 96, 97])); 96 | assert_eq!(vec, [0, 1, 2, 3]); 97 | ``` 98 | 99 |
100 | 101 | Due to how broadly these traits are implemented, we also try to avoid using 102 | names which are commonly used in other APIs, instead opt for bit-specific 103 | terminology such as: 104 | 105 | * Something like `is_empty` becomes `all_zeros` - since with bits you're 106 | thinking about "ones and zeros". 107 | * Testing if a bit is set is `test_bit`, or in general adding the `*_bit` 108 | suffix to operations over individual bits. 109 | * Clearing all bits becomes `clear_bits`, or in general adding the `*_bits` 110 | suffix when operating over *all* bits. 111 | 112 | ```rust 113 | use bittle::{Bits, BitsMut}; 114 | 115 | let mut set = [0u16; 2]; 116 | 117 | set.set_bit(15); 118 | assert!(set.test_bit(15)); 119 | 120 | set.union_assign(&bittle::set![31, 7]); 121 | assert!(set.test_bit(31) && set.test_bit(7)); 122 | 123 | set.clear_bit(31); 124 | assert!(!set.test_bit(31)); 125 | 126 | set.clear_bits(); 127 | assert!(set.all_zeros()); 128 | ``` 129 | 130 |
131 | 132 | Some other interesting operations, such as [`Bits::join_ones`] are available, 133 | allowing bitsets to act like masks over other iterators: 134 | 135 | ```rust 136 | use bittle::{Bits, BitsMut}; 137 | 138 | let elements = vec![10, 48, 101]; 139 | let mut m = 0u128; 140 | 141 | m.set_bit(0); 142 | assert!(m.join_ones(&elements).eq([&10])); 143 | m.set_bit(2); 144 | assert!(m.join_ones(&elements).eq([&10, &101])); 145 | ``` 146 | 147 |
148 | 149 | [`BigEndian`]: https://docs.rs/bittle/latest/bittle/struct.BigEndian.html 150 | [`Bits::join_ones`]: https://docs.rs/bittle/latest/bittle/trait.Bits.html#method.join_ones 151 | [`Bits::test_bit_in`]: https://docs.rs/bittle/latest/bittle/trait.Bits.html#method.test_bit_in 152 | [`Bits::test_bit_le`]: https://docs.rs/bittle/latest/bittle/trait.Bits.html#method.test_bit_le 153 | [`Bits`]: https://docs.rs/bittle/latest/bittle/trait.Bits.html 154 | [`BitsMut`]: https://docs.rs/bittle/latest/bittle/trait.BitsMut.html 155 | [`BitsOwned`]: https://docs.rs/bittle/latest/bittle/trait.BitsOwned.html 156 | [`Copy`]: https://doc.rust-lang.org/std/marker/trait.Copy.html 157 | [`set!`]: https://docs.rs/bittle/latest/bittle/macro.set.html 158 | [`u32`]: https://doc.rust-lang.org/std/primitive.u32.html 159 | [`u8`]: https://doc.rust-lang.org/std/primitive.u8.html 160 | [see issue #2]: https://github.com/udoprog/bittle/pull/2 161 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! [github](https://github.com/udoprog/bittle) 2 | //! [crates.io](https://crates.io/crates/bittle) 3 | //! [docs.rs](https://docs.rs/bittle) 4 | //! 5 | //! Zero-cost bitsets over native Rust types. 6 | //! 7 | //! The name `bittle` comes from `bit` and `little`. Small bitsets! 8 | //! 9 | //!
10 | //! 11 | //! ## Usage 12 | //! 13 | //! Add `bittle` as a dependency in your `Cargo.toml`: 14 | //! 15 | //!
16 | //! 17 | //! ```toml 18 | //! [dependencies] 19 | //! bittle = "0.6.0" 20 | //! ``` 21 | //! 22 | //!
23 | //! 24 | //! ## Guide 25 | //! 26 | //! A bit is always identified by a [`u32`] by its index, and the exact location 27 | //! for a bit in a primitive numbers is defined by its endianness, which is 28 | //! [`BigEndian`] by default. 29 | //! 30 | //! [`BigEndian`] indexing grows from right to left, such as the following 31 | //! [`u8`] literal: 32 | //! 33 | //! ```text 34 | //! 0b0010_0010u8 35 | //! ^ ^- index 1 36 | //! '------ index 5 37 | //! ``` 38 | //! 39 | //!
40 | //! 41 | //! To interact with these bits we define the [`Bits`], [`BitsMut`], and 42 | //! [`BitsOwned`] traits. These traits are implemented for primitive types such 43 | //! as `u32`, `[u32; 4]`, or `&[u32]`: 44 | //! 45 | //! ``` 46 | //! use bittle::Bits; 47 | //! 48 | //! let array: [u32; 4] = [0, 1, 2, 3]; 49 | //! assert!(array.iter_ones().eq([32, 65, 96, 97])); 50 | //! 51 | //! let n = 0b00000000_00000000_00000000_00010001u32; 52 | //! assert!(n.iter_ones().eq([0, 4])); 53 | //! 54 | //! let array_of_arrays: [[u8; 4]; 2] = [[16, 0, 0, 0], [0, 0, 1, 0]]; 55 | //! assert!(array_of_arrays.iter_ones().eq([4, 48])); 56 | //! 57 | //! let mut vec: Vec = vec![0, 1, 2, 3]; 58 | //! assert!(vec.iter_ones().eq([32, 65, 96, 97])); 59 | //! ``` 60 | //! 61 | //!
62 | //! 63 | //! We also provide the [`set!`] macro, which is a zero-cost convenience method 64 | //! for constructing primitive forms of bit sets: 65 | //! 66 | //! ``` 67 | //! use bittle::Bits; 68 | //! 69 | //! let array: [u32; 4] = bittle::set![32, 65, 96, 97]; 70 | //! assert!(array.iter_ones().eq([32, 65, 96, 97])); 71 | //! 72 | //! let n: u32 = bittle::set![0, 4]; 73 | //! assert!(n.iter_ones().eq([0, 4])); 74 | //! 75 | //! let array_of_arrays: [[u8; 4]; 2] = bittle::set![4, 48]; 76 | //! assert!(array_of_arrays.iter_ones().eq([4, 48])); 77 | //! ``` 78 | //! 79 | //!
80 | //! 81 | //! Since a vector is not a primitive bit set, it could instead make use of 82 | //! [`BitsMut`] directly: 83 | //! 84 | //! ``` 85 | //! use bittle::{Bits, BitsMut}; 86 | //! 87 | //! let mut vec: Vec = vec![0u32; 4]; 88 | //! vec.set_bit(32); 89 | //! vec.set_bit(65); 90 | //! vec.set_bit(96); 91 | //! vec.set_bit(97); 92 | //! assert!(vec.iter_ones().eq([32, 65, 96, 97])); 93 | //! assert_eq!(vec, [0, 1, 2, 3]); 94 | //! ``` 95 | //! 96 | //!
97 | //! 98 | //! Due to how broadly these traits are implemented, we also try to avoid using 99 | //! names which are commonly used in other APIs, instead opt for bit-specific 100 | //! terminology such as: 101 | //! 102 | //! * Something like `is_empty` becomes `all_zeros` - since with bits you're 103 | //! thinking about "ones and zeros". 104 | //! * Testing if a bit is set is `test_bit`, or in general adding the `*_bit` 105 | //! suffix to operations over individual bits. 106 | //! * Clearing all bits becomes `clear_bits`, or in general adding the `*_bits` 107 | //! suffix when operating over *all* bits. 108 | //! 109 | //! ```rust 110 | //! use bittle::{Bits, BitsMut}; 111 | //! 112 | //! let mut set = [0u16; 2]; 113 | //! 114 | //! set.set_bit(15); 115 | //! assert!(set.test_bit(15)); 116 | //! 117 | //! set.union_assign(&bittle::set![31, 7]); 118 | //! assert!(set.test_bit(31) && set.test_bit(7)); 119 | //! 120 | //! set.clear_bit(31); 121 | //! assert!(!set.test_bit(31)); 122 | //! 123 | //! set.clear_bits(); 124 | //! assert!(set.all_zeros()); 125 | //! ``` 126 | //! 127 | //!
128 | //! 129 | //! Some other interesting operations, such as [`Bits::join_ones`] are available, 130 | //! allowing bitsets to act like masks over other iterators: 131 | //! 132 | //! ```rust 133 | //! use bittle::{Bits, BitsMut}; 134 | //! 135 | //! let elements = vec![10, 48, 101]; 136 | //! let mut m = 0u128; 137 | //! 138 | //! m.set_bit(0); 139 | //! assert!(m.join_ones(&elements).eq([&10])); 140 | //! m.set_bit(2); 141 | //! assert!(m.join_ones(&elements).eq([&10, &101])); 142 | //! ``` 143 | //! 144 | //!
145 | //! 146 | //! [`BigEndian`]: https://docs.rs/bittle/latest/bittle/struct.BigEndian.html 147 | //! [`Bits::join_ones`]: https://docs.rs/bittle/latest/bittle/trait.Bits.html#method.join_ones 148 | //! [`Bits::test_bit_in`]: https://docs.rs/bittle/latest/bittle/trait.Bits.html#method.test_bit_in 149 | //! [`Bits::test_bit_le`]: https://docs.rs/bittle/latest/bittle/trait.Bits.html#method.test_bit_le 150 | //! [`Bits`]: https://docs.rs/bittle/latest/bittle/trait.Bits.html 151 | //! [`BitsMut`]: https://docs.rs/bittle/latest/bittle/trait.BitsMut.html 152 | //! [`BitsOwned`]: https://docs.rs/bittle/latest/bittle/trait.BitsOwned.html 153 | //! [`Copy`]: https://doc.rust-lang.org/std/marker/trait.Copy.html 154 | //! [`set!`]: https://docs.rs/bittle/latest/bittle/macro.set.html 155 | //! [`u32`]: https://doc.rust-lang.org/std/primitive.u32.html 156 | //! [`u8`]: https://doc.rust-lang.org/std/primitive.u8.html 157 | //! [see issue #2]: https://github.com/udoprog/bittle/pull/2 158 | 159 | #![deny(missing_docs)] 160 | #![deny(rustdoc::broken_intra_doc_links)] 161 | #![no_std] 162 | 163 | // This library makes hard assumptions on u32 <= usize. 164 | const _: () = assert!(core::mem::size_of::() <= core::mem::size_of::()); 165 | 166 | #[macro_use] 167 | mod macros; 168 | 169 | #[macro_use] 170 | mod internal; 171 | 172 | pub mod array; 173 | pub mod number; 174 | pub mod slice; 175 | 176 | mod set; 177 | pub use self::set::Set; 178 | 179 | mod bits; 180 | pub use self::bits::Bits; 181 | 182 | mod bits_mut; 183 | pub use self::bits_mut::BitsMut; 184 | 185 | mod bits_owned; 186 | pub use self::bits_owned::BitsOwned; 187 | 188 | mod endian; 189 | pub use self::endian::{BigEndian, DefaultEndian, Endian, LittleEndian}; 190 | 191 | pub mod prelude { 192 | //! Prelude use to conveniently import all relevant bits-oriented traits. 193 | pub use crate::{Bits, BitsMut, BitsOwned}; 194 | } 195 | -------------------------------------------------------------------------------- /src/slice.rs: -------------------------------------------------------------------------------- 1 | //! [Bits] associated types for slices. 2 | 3 | use crate::bits::Bits; 4 | use crate::bits_mut::BitsMut; 5 | use crate::endian::{DefaultEndian, Endian}; 6 | use crate::BitsOwned; 7 | 8 | impl Bits for [T] 9 | where 10 | T: BitsOwned, 11 | { 12 | type IterOnes<'a> 13 | = IterOnes<'a, T, DefaultEndian> 14 | where 15 | Self: 'a; 16 | type IterOnesIn<'a, E> 17 | = IterOnes<'a, T, E> 18 | where 19 | Self: 'a, 20 | E: Endian; 21 | type IterZeros<'a> 22 | = IterZeros<'a, T, DefaultEndian> 23 | where 24 | Self: 'a; 25 | type IterZerosIn<'a, E> 26 | = IterZeros<'a, T, E> 27 | where 28 | Self: 'a, 29 | E: Endian; 30 | 31 | /// Count the number of ones in the slice. 32 | /// 33 | /// # Examples 34 | /// 35 | /// ``` 36 | /// use bittle::Bits; 37 | /// 38 | /// let a: [u8; 2] = bittle::set![4, 11, 14]; 39 | /// let a = a.as_slice(); 40 | /// assert_eq!(a.count_ones(), 3); 41 | /// ``` 42 | #[inline] 43 | fn count_ones(&self) -> u32 { 44 | self.iter().map(Bits::count_ones).sum() 45 | } 46 | 47 | /// Count the number of zeros in the slice. 48 | /// 49 | /// # Examples 50 | /// 51 | /// ``` 52 | /// use bittle::Bits; 53 | /// 54 | /// let a: [u8; 2] = bittle::set![4, 11, 14]; 55 | /// let a = a.as_slice(); 56 | /// assert_eq!(a.count_zeros(), 13); 57 | /// ``` 58 | #[inline] 59 | fn count_zeros(&self) -> u32 { 60 | self.iter().map(Bits::count_zeros).sum() 61 | } 62 | 63 | /// Get the capacity of the slice. 64 | /// 65 | /// The capacity of a slice is determined by its length. 66 | /// 67 | /// # Examples 68 | /// 69 | /// ``` 70 | /// use bittle::Bits; 71 | /// 72 | /// let a: [u8; 4] = bittle::set![4, 11, 14]; 73 | /// let a = &a[..2]; 74 | /// assert_eq!(a.bits_capacity(), 16); 75 | /// ``` 76 | #[inline] 77 | fn bits_capacity(&self) -> u32 { 78 | let len = u32::try_from(self.len()).unwrap_or(u32::MAX); 79 | len.saturating_mul(T::BITS) 80 | } 81 | 82 | /// Test if the slice is all zeros. 83 | /// 84 | /// # Examples 85 | /// 86 | /// ``` 87 | /// use bittle::Bits; 88 | /// 89 | /// let a: [u8; 2] = bittle::set![]; 90 | /// let a = a.as_slice(); 91 | /// assert!(a.all_zeros()); 92 | /// 93 | /// let a: [u8; 2] = bittle::set![4, 11, 14]; 94 | /// let a = a.as_slice(); 95 | /// assert!(!a.all_zeros()); 96 | /// ``` 97 | #[inline] 98 | fn all_zeros(&self) -> bool { 99 | self.iter().all(Bits::all_zeros) 100 | } 101 | 102 | /// Test if the slice is all ones. 103 | /// 104 | /// # Examples 105 | /// 106 | /// ``` 107 | /// use bittle::Bits; 108 | /// 109 | /// let a: [u8; 2] = bittle::set![0..16]; 110 | /// let a = a.as_slice(); 111 | /// assert!(a.all_ones()); 112 | /// 113 | /// let a: [u8; 2] = bittle::set![4, 11, 14]; 114 | /// let a = a.as_slice(); 115 | /// assert!(!a.all_ones()); 116 | /// ``` 117 | #[inline] 118 | fn all_ones(&self) -> bool { 119 | self.iter().all(Bits::all_ones) 120 | } 121 | 122 | /// Test if the given bit is set in the slice. 123 | /// 124 | /// # Examples 125 | /// 126 | /// ``` 127 | /// use bittle::Bits; 128 | /// 129 | /// let mut a: [u8; 2] = bittle::set![4, 11, 14]; 130 | /// let a: &[u8] = a.as_slice(); 131 | /// assert!(a.test_bit(4)); 132 | /// ``` 133 | #[inline] 134 | fn test_bit(&self, index: u32) -> bool { 135 | self.test_bit_in::(index) 136 | } 137 | 138 | /// Test if the given bit is set in the slice. 139 | /// 140 | /// # Examples 141 | /// 142 | /// ``` 143 | /// use bittle::{Bits, BigEndian}; 144 | /// 145 | /// let mut a: [u8; 2] = bittle::set_be![4, 11, 14]; 146 | /// let a: &[u8] = a.as_slice(); 147 | /// assert!(a.test_bit_in::(4)); 148 | /// ``` 149 | #[inline] 150 | fn test_bit_in(&self, index: u32) -> bool 151 | where 152 | E: Endian, 153 | { 154 | self[(index / T::BITS) as usize % self.len()].test_bit_in::(index % T::BITS) 155 | } 156 | 157 | /// Iterates over all ones in the slice using [`DefaultEndian`] indexing. 158 | /// 159 | /// # Examples 160 | /// 161 | /// ``` 162 | /// use bittle::Bits; 163 | /// 164 | /// let mut a: [u8; 2] = bittle::set![4, 11, 14]; 165 | /// let a: &[u8] = a.as_slice(); 166 | /// assert!(a.iter_ones().eq([4, 11, 14])); 167 | /// 168 | /// let a: &[u8] = &[]; 169 | /// assert!(a.iter_ones().eq([])); 170 | /// ``` 171 | #[inline] 172 | fn iter_ones(&self) -> Self::IterOnes<'_> { 173 | IterOnes::new(self) 174 | } 175 | 176 | /// Iterates over all ones in the slice using a custom [`Endian`] indexing. 177 | /// 178 | /// # Examples 179 | /// 180 | /// ``` 181 | /// use bittle::{Bits, LittleEndian}; 182 | /// 183 | /// let mut a: [u8; 2] = bittle::set_le![4, 11, 14]; 184 | /// let a: &[u8] = a.as_slice(); 185 | /// assert!(a.iter_ones_in::().eq([4, 11, 14])); 186 | /// ``` 187 | #[inline] 188 | fn iter_ones_in(&self) -> Self::IterOnesIn<'_, E> 189 | where 190 | E: Endian, 191 | { 192 | IterOnes::new(self) 193 | } 194 | 195 | /// Iterates over all zeros in the slice using [`DefaultEndian`] indexing. 196 | /// 197 | /// # Examples 198 | /// 199 | /// ``` 200 | /// use bittle::Bits; 201 | /// 202 | /// let mut a: [u8; 2] = bittle::set![4, 11, 14]; 203 | /// let a: &[u8] = a.as_slice(); 204 | /// assert!(a.iter_zeros().eq([0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 12, 13, 15])); 205 | /// 206 | /// let a: &[u8] = &[]; 207 | /// assert!(a.iter_zeros().eq([])); 208 | /// ``` 209 | #[inline] 210 | fn iter_zeros(&self) -> Self::IterZeros<'_> { 211 | IterZeros::new(self) 212 | } 213 | 214 | /// Iterates over all zeros in the slice using a custom [`Endian`] indexing 215 | /// 216 | /// # Examples 217 | /// 218 | /// ``` 219 | /// use bittle::{Bits, LittleEndian}; 220 | /// 221 | /// let mut a: [u8; 2] = bittle::set_le![4, 11, 14]; 222 | /// let a: &[u8] = a.as_slice(); 223 | /// assert!(a.iter_zeros_in::().eq([0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 12, 13, 15])); 224 | /// ``` 225 | #[inline] 226 | fn iter_zeros_in(&self) -> Self::IterZerosIn<'_, E> 227 | where 228 | E: Endian, 229 | { 230 | IterZeros::new(self) 231 | } 232 | } 233 | 234 | impl BitsMut for [T] 235 | where 236 | T: BitsOwned, 237 | { 238 | /// Set the given bit is set in the slice. 239 | /// 240 | /// # Examples 241 | /// 242 | /// ``` 243 | /// use bittle::{Bits, BitsMut}; 244 | /// 245 | /// let mut a: [u8; 2] = bittle::set![7]; 246 | /// let a: &mut [u8] = a.as_mut_slice(); 247 | /// a.set_bit(13); 248 | /// assert!(a.iter_ones().eq([7, 13])); 249 | /// ``` 250 | #[inline] 251 | fn set_bit_in(&mut self, index: u32) 252 | where 253 | E: Endian, 254 | { 255 | self[(index / T::BITS) as usize % self.len()].set_bit_in::(index % T::BITS); 256 | } 257 | 258 | #[inline] 259 | fn set_bit(&mut self, index: u32) { 260 | self.set_bit_in::(index); 261 | } 262 | 263 | /// Set the given bit is set in the slice. 264 | /// 265 | /// # Examples 266 | /// 267 | /// ``` 268 | /// use bittle::{Bits, BitsMut}; 269 | /// 270 | /// let mut a: [u8; 2] = bittle::set![7, 13]; 271 | /// let a: &mut [u8] = a.as_mut_slice(); 272 | /// a.clear_bit(13); 273 | /// assert!(a.iter_ones().eq([7])); 274 | /// ``` 275 | #[inline] 276 | fn clear_bit_in(&mut self, index: u32) 277 | where 278 | E: Endian, 279 | { 280 | self[(index / T::BITS) as usize % self.len()].clear_bit_in::(index % T::BITS); 281 | } 282 | 283 | #[inline] 284 | fn clear_bit(&mut self, index: u32) { 285 | self.clear_bit_in::(index); 286 | } 287 | 288 | /// Clear the entire slice, or set all bits to zeros. 289 | /// 290 | /// # Examples 291 | /// 292 | /// ``` 293 | /// use bittle::{Bits, BitsMut}; 294 | /// 295 | /// let mut a: [u8; 2] = bittle::set![7, 13]; 296 | /// let a: &mut [u8] = a.as_mut_slice(); 297 | /// a.clear_bits(); 298 | /// assert!(a.all_zeros()); 299 | /// ``` 300 | #[inline] 301 | fn clear_bits(&mut self) { 302 | for b in self { 303 | b.clear_bits(); 304 | } 305 | } 306 | 307 | #[inline] 308 | fn union_assign(&mut self, other: &Self) { 309 | for (o, i) in self.iter_mut().zip(other) { 310 | o.union_assign(i); 311 | } 312 | } 313 | 314 | #[inline] 315 | fn conjunction_assign(&mut self, other: &Self) { 316 | for (o, i) in self.iter_mut().zip(other) { 317 | o.conjunction_assign(i); 318 | } 319 | } 320 | 321 | #[inline] 322 | fn difference_assign(&mut self, other: &Self) { 323 | for (o, i) in self.iter_mut().zip(other) { 324 | o.difference_assign(i); 325 | } 326 | } 327 | 328 | #[inline] 329 | fn symmetric_difference_assign(&mut self, other: &Self) { 330 | for (o, i) in self.iter_mut().zip(other) { 331 | o.symmetric_difference_assign(i); 332 | } 333 | } 334 | } 335 | 336 | impl_iter! { 337 | /// A borrowing iterator over the bits set to one in a slice. 338 | {T, E} IterOnes<'a, T, E> 339 | {iter_ones_in, T::IterOnesIn<'a, E>, core::slice::Iter<'a, T>} 340 | {slice: &'a [T] => slice.len()} 341 | {} 342 | } 343 | 344 | impl_iter! { 345 | /// An owned iterator over the bits set to zero in an array. 346 | {T, E} IterZeros<'a, T, E> 347 | {iter_zeros_in, T::IterZerosIn<'a, E>, core::slice::Iter<'a, T>} 348 | {slice: &'a [T] => slice.len()} 349 | {} 350 | } 351 | -------------------------------------------------------------------------------- /src/array.rs: -------------------------------------------------------------------------------- 1 | //! [Bits] associated types for primitive arrays. 2 | 3 | use crate::bits::Bits; 4 | use crate::bits_mut::BitsMut; 5 | use crate::bits_owned::BitsOwned; 6 | use crate::endian::{DefaultEndian, Endian}; 7 | use crate::slice::{IterOnes, IterZeros}; 8 | 9 | impl BitsOwned for [T; N] 10 | where 11 | T: Eq + BitsOwned, 12 | { 13 | #[allow(clippy::cast_possible_truncation)] 14 | const BITS: u32 = match T::BITS.checked_mul(N as u32) { 15 | Some(value) => value, 16 | None => panic!("32-bit overflow in capacity"), 17 | }; 18 | const ZEROS: Self = [T::ZEROS; N]; 19 | const ONES: Self = [T::ONES; N]; 20 | 21 | type IntoIterOnes = IntoIterOnes; 22 | type IntoIterOnesIn 23 | = IntoIterOnes 24 | where 25 | E: Endian; 26 | type IntoIterZeros = IntoIterZeros; 27 | type IntoIterZerosIn 28 | = IntoIterZeros 29 | where 30 | E: Endian; 31 | 32 | #[inline] 33 | fn zeros() -> Self { 34 | Self::ZEROS 35 | } 36 | 37 | #[inline] 38 | fn ones() -> Self { 39 | Self::ONES 40 | } 41 | 42 | #[inline] 43 | fn with_bit_in(mut self, bit: u32) -> Self 44 | where 45 | E: Endian, 46 | { 47 | self[(bit / T::BITS) as usize % N].set_bit_in::(bit % T::BITS); 48 | self 49 | } 50 | 51 | #[inline] 52 | fn with_bit(self, bit: u32) -> Self { 53 | self.with_bit_in::(bit) 54 | } 55 | 56 | #[inline] 57 | fn without_bit_in(mut self, bit: u32) -> Self 58 | where 59 | E: Endian, 60 | { 61 | self[(bit / T::BITS) as usize % N].clear_bit_in::(bit % T::BITS); 62 | self 63 | } 64 | 65 | #[inline] 66 | fn without_bit(self, bit: u32) -> Self { 67 | self.without_bit_in::(bit) 68 | } 69 | 70 | #[inline] 71 | fn union(mut self, other: Self) -> Self { 72 | for (o, i) in self.iter_mut().zip(other) { 73 | o.union_assign(&i); 74 | } 75 | 76 | self 77 | } 78 | 79 | #[inline] 80 | fn conjunction(mut self, other: Self) -> Self { 81 | for (o, i) in self.iter_mut().zip(other) { 82 | o.conjunction_assign(&i); 83 | } 84 | 85 | self 86 | } 87 | 88 | #[inline] 89 | fn difference(mut self, other: Self) -> Self { 90 | for (o, i) in self.iter_mut().zip(other) { 91 | o.difference_assign(&i); 92 | } 93 | 94 | self 95 | } 96 | 97 | #[inline] 98 | fn symmetric_difference(mut self, other: Self) -> Self { 99 | for (o, i) in self.iter_mut().zip(other) { 100 | o.symmetric_difference_assign(&i); 101 | } 102 | 103 | self 104 | } 105 | 106 | #[inline] 107 | fn into_iter_ones(self) -> Self::IntoIterOnes { 108 | IntoIterOnes::new(self) 109 | } 110 | 111 | #[inline] 112 | fn into_iter_ones_in(self) -> Self::IntoIterOnesIn 113 | where 114 | E: Endian, 115 | { 116 | IntoIterOnes::new(self) 117 | } 118 | 119 | #[inline] 120 | fn into_iter_zeros(self) -> Self::IntoIterZeros { 121 | IntoIterZeros::new(self) 122 | } 123 | 124 | #[inline] 125 | fn into_iter_zeros_in(self) -> Self::IntoIterZerosIn 126 | where 127 | E: Endian, 128 | { 129 | IntoIterZeros::new(self) 130 | } 131 | } 132 | 133 | impl Bits for [T; N] 134 | where 135 | T: Eq + BitsOwned, 136 | { 137 | type IterOnes<'a> 138 | = IterOnes<'a, T, DefaultEndian> 139 | where 140 | Self: 'a; 141 | type IterOnesIn<'a, E> 142 | = IterOnes<'a, T, E> 143 | where 144 | Self: 'a, 145 | E: Endian; 146 | type IterZeros<'a> 147 | = IterZeros<'a, T, DefaultEndian> 148 | where 149 | Self: 'a; 150 | type IterZerosIn<'a, E> 151 | = IterZeros<'a, T, E> 152 | where 153 | Self: 'a, 154 | E: Endian; 155 | 156 | /// Count the number of ones in the array. 157 | /// 158 | /// # Examples 159 | /// 160 | /// ``` 161 | /// use bittle::Bits; 162 | /// 163 | /// let a: [u8; 2] = bittle::set![4, 11, 14]; 164 | /// assert_eq!(a.count_ones(), 3); 165 | /// ``` 166 | #[inline] 167 | fn count_ones(&self) -> u32 { 168 | self.iter().map(Bits::count_ones).sum() 169 | } 170 | 171 | /// Count the number of zeros in the array. 172 | /// 173 | /// # Examples 174 | /// 175 | /// ``` 176 | /// use bittle::Bits; 177 | /// 178 | /// let a: [u8; 2] = bittle::set![4, 11, 14]; 179 | /// assert_eq!(a.count_zeros(), 13); 180 | /// ``` 181 | #[inline] 182 | fn count_zeros(&self) -> u32 { 183 | self.iter().map(Bits::count_zeros).sum() 184 | } 185 | 186 | /// Get the capacity of the array. 187 | /// 188 | /// The capacity of a array is determined by its length. 189 | /// 190 | /// # Examples 191 | /// 192 | /// ``` 193 | /// use bittle::Bits; 194 | /// 195 | /// let a: [u8; 4] = bittle::set![4, 11, 14]; 196 | /// assert_eq!(a.bits_capacity(), 32); 197 | /// ``` 198 | /// 199 | /// Also note that the capacity of an array is known at compile-time through 200 | /// [`BitsOwned::BITS`]: 201 | /// 202 | /// ``` 203 | /// use bittle::BitsOwned; 204 | /// 205 | /// const CAP: u32 = <[u8; 4]>::BITS; 206 | /// assert_eq!(CAP, 32); 207 | /// ``` 208 | #[inline] 209 | fn bits_capacity(&self) -> u32 { 210 | Self::BITS 211 | } 212 | 213 | /// Test if the array is all zeros. 214 | /// 215 | /// # Examples 216 | /// 217 | /// ``` 218 | /// use bittle::Bits; 219 | /// 220 | /// let a: [u8; 2] = bittle::set![]; 221 | /// assert!(a.all_zeros()); 222 | /// 223 | /// let a: [u8; 2] = bittle::set![4, 11, 14]; 224 | /// assert!(!a.all_zeros()); 225 | /// ``` 226 | #[inline] 227 | fn all_zeros(&self) -> bool { 228 | *self == Self::ZEROS 229 | } 230 | 231 | /// Test if the array is all ones. 232 | /// 233 | /// # Examples 234 | /// 235 | /// ``` 236 | /// use bittle::Bits; 237 | /// 238 | /// let a: [u8; 2] = bittle::set![0..16]; 239 | /// assert!(a.all_ones()); 240 | /// 241 | /// let a: [u8; 2] = bittle::set![4, 11, 14]; 242 | /// assert!(!a.all_ones()); 243 | /// ``` 244 | #[inline] 245 | fn all_ones(&self) -> bool { 246 | *self == Self::ONES 247 | } 248 | 249 | /// Test if the given bit is set in the array. 250 | /// 251 | /// # Examples 252 | /// 253 | /// ``` 254 | /// use bittle::Bits; 255 | /// 256 | /// let mut a: [u8; 2] = bittle::set![4, 11, 14]; 257 | /// assert!(a.test_bit(4)); 258 | /// ``` 259 | #[inline] 260 | fn test_bit(&self, index: u32) -> bool { 261 | self.test_bit_in::(index) 262 | } 263 | 264 | /// Test if the given bit is set in the array. 265 | /// 266 | /// # Examples 267 | /// 268 | /// ``` 269 | /// use bittle::{Bits, BigEndian}; 270 | /// 271 | /// let mut a: [u8; 2] = bittle::set_be![4, 11, 14]; 272 | /// assert!(a.test_bit_in::(4)); 273 | /// ``` 274 | #[inline] 275 | fn test_bit_in(&self, index: u32) -> bool 276 | where 277 | E: Endian, 278 | { 279 | self[(index / T::BITS) as usize % N].test_bit_in::(index % T::BITS) 280 | } 281 | 282 | /// Iterates over all ones in the array using [`DefaultEndian`] indexing. 283 | /// 284 | /// # Examples 285 | /// 286 | /// ``` 287 | /// use bittle::Bits; 288 | /// 289 | /// let mut a: [u8; 2] = bittle::set![4, 11, 14]; 290 | /// assert!(a.iter_ones().eq([4, 11, 14])); 291 | /// 292 | /// let a: [u8; 0] = []; 293 | /// assert!(a.iter_ones().eq([])); 294 | /// ``` 295 | #[inline] 296 | fn iter_ones(&self) -> Self::IterOnes<'_> { 297 | IterOnes::new(self) 298 | } 299 | 300 | /// Iterates over all ones in the array using a custom [`Endian`] indexing. 301 | /// 302 | /// # Examples 303 | /// 304 | /// ``` 305 | /// use bittle::{Bits, LittleEndian}; 306 | /// 307 | /// let mut a: [u8; 2] = bittle::set_le![4, 11, 14]; 308 | /// assert!(a.iter_ones_in::().eq([4, 11, 14])); 309 | /// ``` 310 | #[inline] 311 | fn iter_ones_in(&self) -> Self::IterOnesIn<'_, E> 312 | where 313 | E: Endian, 314 | { 315 | IterOnes::new(self) 316 | } 317 | 318 | /// Iterates over all zeros in the array using [`DefaultEndian`] indexing. 319 | /// 320 | /// # Examples 321 | /// 322 | /// ``` 323 | /// use bittle::Bits; 324 | /// 325 | /// let mut a: [u8; 2] = bittle::set![4, 11, 14]; 326 | /// assert!(a.iter_zeros().eq([0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 12, 13, 15])); 327 | /// 328 | /// let a: [u8; 0] = []; 329 | /// assert!(a.iter_zeros().eq([])); 330 | /// ``` 331 | #[inline] 332 | fn iter_zeros(&self) -> Self::IterZeros<'_> { 333 | IterZeros::new(self) 334 | } 335 | 336 | /// Iterates over all zeros in the array using a custom [`Endian`] indexing 337 | /// 338 | /// # Examples 339 | /// 340 | /// ``` 341 | /// use bittle::{Bits, LittleEndian}; 342 | /// 343 | /// let mut a: [u8; 2] = bittle::set_le![4, 11, 14]; 344 | /// assert!(a.iter_zeros_in::().eq([0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 12, 13, 15])); 345 | /// ``` 346 | #[inline] 347 | fn iter_zeros_in(&self) -> Self::IterZerosIn<'_, E> 348 | where 349 | E: Endian, 350 | { 351 | IterZeros::new(self) 352 | } 353 | } 354 | 355 | impl BitsMut for [T; N] 356 | where 357 | T: Eq + BitsOwned, 358 | { 359 | #[inline] 360 | fn set_bit_in(&mut self, index: u32) 361 | where 362 | E: Endian, 363 | { 364 | self[(index / T::BITS) as usize % N].set_bit_in::(index % T::BITS); 365 | } 366 | 367 | #[inline] 368 | fn set_bit(&mut self, index: u32) { 369 | self.set_bit_in::(index); 370 | } 371 | 372 | #[inline] 373 | fn clear_bit_in(&mut self, index: u32) 374 | where 375 | E: Endian, 376 | { 377 | self[(index / T::BITS) as usize % N].clear_bit_in::(index % T::BITS); 378 | } 379 | 380 | #[inline] 381 | fn clear_bit(&mut self, index: u32) { 382 | self.clear_bit_in::(index); 383 | } 384 | 385 | #[inline] 386 | fn clear_bits(&mut self) { 387 | for b in self { 388 | b.clear_bits(); 389 | } 390 | } 391 | 392 | #[inline] 393 | fn union_assign(&mut self, other: &Self) { 394 | for (o, i) in self.iter_mut().zip(other) { 395 | o.union_assign(i); 396 | } 397 | } 398 | 399 | #[inline] 400 | fn conjunction_assign(&mut self, other: &Self) { 401 | for (o, i) in self.iter_mut().zip(other) { 402 | o.conjunction_assign(i); 403 | } 404 | } 405 | 406 | #[inline] 407 | fn difference_assign(&mut self, other: &Self) { 408 | for (o, i) in self.iter_mut().zip(other) { 409 | o.difference_assign(i); 410 | } 411 | } 412 | 413 | #[inline] 414 | fn symmetric_difference_assign(&mut self, other: &Self) { 415 | for (o, i) in self.iter_mut().zip(other) { 416 | o.symmetric_difference_assign(i); 417 | } 418 | } 419 | } 420 | 421 | impl_iter! { 422 | /// An owned iterator over the bits set to one in an array. 423 | {T, const N: usize, E} IntoIterOnes 424 | {into_iter_ones_in, T::IntoIterOnesIn, core::array::IntoIter} 425 | {array: [T; N] => N} 426 | {T: Clone} 427 | } 428 | 429 | impl_iter! { 430 | /// An owned iterator over the bits set to zero in an array. 431 | {T, const N: usize, E} IntoIterZeros 432 | {into_iter_zeros_in, T::IntoIterZerosIn, core::array::IntoIter} 433 | {array: [T; N] => N} 434 | {T: Clone} 435 | } 436 | -------------------------------------------------------------------------------- /src/number.rs: -------------------------------------------------------------------------------- 1 | //! [Bits] associated types for primitive numbers. 2 | 3 | use core::iter::FusedIterator; 4 | use core::marker::PhantomData; 5 | 6 | use crate::bits::Bits; 7 | use crate::bits_mut::BitsMut; 8 | use crate::bits_owned::BitsOwned; 9 | use crate::endian::{DefaultEndian, Endian}; 10 | 11 | mod sealed { 12 | use core::ops::{BitOrAssign, BitXorAssign}; 13 | 14 | /// Basic numerical trait for the plumbing of a bit set. This ensures that only 15 | /// primitive types can be used as the basis of a bit set backed by an array, 16 | /// like `[u64; 4]` and not `[[u32; 2]; 4]`. 17 | pub trait Number: Copy + BitXorAssign + BitOrAssign + Eq { 18 | const ZEROS: Self; 19 | const ONES: Self; 20 | const BITS: u32; 21 | const BIT_RIGHT: Self; 22 | const BIT_LEFT: Self; 23 | 24 | fn count_ones(self) -> u32; 25 | fn count_zeros(self) -> u32; 26 | fn trailing_ones(self) -> u32; 27 | fn leading_ones(self) -> u32; 28 | fn trailing_zeros(self) -> u32; 29 | fn leading_zeros(self) -> u32; 30 | fn wrapping_shl(self, index: u32) -> Self; 31 | fn wrapping_shr(self, index: u32) -> Self; 32 | } 33 | } 34 | 35 | pub(crate) use self::sealed::Number; 36 | 37 | macro_rules! number { 38 | ($ty:ty) => { 39 | impl Number for $ty { 40 | const ONES: $ty = !0; 41 | const ZEROS: $ty = 0; 42 | const BITS: u32 = <$ty>::BITS; 43 | const BIT_RIGHT: $ty = 1 as $ty; 44 | const BIT_LEFT: $ty = !(<$ty>::MAX >> 1); 45 | 46 | #[inline] 47 | fn count_ones(self) -> u32 { 48 | <$ty>::count_ones(self) 49 | } 50 | 51 | #[inline] 52 | fn count_zeros(self) -> u32 { 53 | <$ty>::count_zeros(self) 54 | } 55 | 56 | #[inline] 57 | fn trailing_ones(self) -> u32 { 58 | <$ty>::trailing_ones(self) 59 | } 60 | 61 | #[inline] 62 | fn leading_ones(self) -> u32 { 63 | <$ty>::leading_ones(self) 64 | } 65 | 66 | #[inline] 67 | fn trailing_zeros(self) -> u32 { 68 | <$ty>::trailing_zeros(self) 69 | } 70 | 71 | #[inline] 72 | fn leading_zeros(self) -> u32 { 73 | <$ty>::leading_zeros(self) 74 | } 75 | 76 | #[inline] 77 | fn wrapping_shl(self, index: u32) -> Self { 78 | <$ty>::wrapping_shl(self, index) 79 | } 80 | 81 | #[inline] 82 | fn wrapping_shr(self, index: u32) -> Self { 83 | <$ty>::wrapping_shr(self, index) 84 | } 85 | } 86 | 87 | impl crate::bits::Sealed for $ty {} 88 | 89 | impl Bits for $ty { 90 | type IterOnes<'a> 91 | = IterOnes 92 | where 93 | Self: 'a; 94 | type IterOnesIn<'a, E> 95 | = IterOnes 96 | where 97 | Self: 'a, 98 | E: Endian; 99 | type IterZeros<'a> 100 | = IterZeros 101 | where 102 | Self: 'a; 103 | type IterZerosIn<'a, E> 104 | = IterZeros 105 | where 106 | Self: 'a, 107 | E: Endian; 108 | 109 | #[inline] 110 | fn count_ones(&self) -> u32 { 111 | <$ty>::count_ones(*self) 112 | } 113 | 114 | #[inline] 115 | fn count_zeros(&self) -> u32 { 116 | <$ty>::count_zeros(*self) 117 | } 118 | 119 | #[inline] 120 | fn bits_capacity(&self) -> u32 { 121 | ::BITS 122 | } 123 | 124 | #[inline] 125 | fn all_zeros(&self) -> bool { 126 | <$ty as Number>::ZEROS == *self 127 | } 128 | 129 | #[inline] 130 | fn all_ones(&self) -> bool { 131 | <$ty as Number>::ONES == *self 132 | } 133 | 134 | #[inline] 135 | fn test_bit(&self, index: u32) -> bool { 136 | self.test_bit_in::(index) 137 | } 138 | 139 | #[inline] 140 | fn test_bit_in(&self, index: u32) -> bool 141 | where 142 | E: Endian, 143 | { 144 | *self & E::mask::(index) != 0 145 | } 146 | 147 | #[inline] 148 | fn iter_ones(&self) -> Self::IterOnes<'_> { 149 | IterOnes::new(*self) 150 | } 151 | 152 | #[inline] 153 | fn iter_ones_in(&self) -> Self::IterOnesIn<'_, E> 154 | where 155 | E: Endian, 156 | { 157 | IterOnes::new(*self) 158 | } 159 | 160 | #[inline] 161 | fn iter_zeros(&self) -> Self::IterZeros<'_> { 162 | IterZeros::new(*self) 163 | } 164 | 165 | #[inline] 166 | fn iter_zeros_in(&self) -> Self::IterZerosIn<'_, E> 167 | where 168 | E: Endian, 169 | { 170 | IterZeros::new(*self) 171 | } 172 | } 173 | 174 | impl BitsMut for $ty { 175 | #[inline] 176 | fn set_bit_in(&mut self, index: u32) 177 | where 178 | E: Endian, 179 | { 180 | *self |= E::mask::(index); 181 | } 182 | 183 | #[inline] 184 | fn set_bit(&mut self, index: u32) { 185 | self.set_bit_in::(index); 186 | } 187 | 188 | #[inline] 189 | fn clear_bit_in(&mut self, index: u32) 190 | where 191 | E: Endian, 192 | { 193 | *self &= !E::mask::(index); 194 | } 195 | 196 | #[inline] 197 | fn clear_bit(&mut self, index: u32) { 198 | self.clear_bit_in::(index); 199 | } 200 | 201 | #[inline] 202 | fn union_assign(&mut self, other: &Self) { 203 | *self |= other; 204 | } 205 | 206 | #[inline] 207 | fn conjunction_assign(&mut self, other: &Self) { 208 | *self &= other; 209 | } 210 | 211 | #[inline] 212 | fn difference_assign(&mut self, other: &Self) { 213 | *self &= !other; 214 | } 215 | 216 | #[inline] 217 | fn symmetric_difference_assign(&mut self, other: &Self) { 218 | *self ^= other; 219 | } 220 | 221 | #[inline] 222 | fn clear_bits(&mut self) { 223 | *self = <$ty as Number>::ZEROS; 224 | } 225 | } 226 | 227 | impl BitsOwned for $ty { 228 | const BITS: u32 = <$ty as Number>::BITS; 229 | const ZEROS: Self = <$ty as Number>::ZEROS; 230 | const ONES: Self = <$ty as Number>::ONES; 231 | 232 | type IntoIterOnes = IterOnes; 233 | type IntoIterOnesIn 234 | = IterOnes 235 | where 236 | E: Endian; 237 | type IntoIterZeros = IterZeros; 238 | type IntoIterZerosIn 239 | = IterZeros 240 | where 241 | E: Endian; 242 | 243 | #[inline] 244 | fn zeros() -> Self { 245 | <$ty as Number>::ZEROS 246 | } 247 | 248 | #[inline] 249 | fn ones() -> Self { 250 | <$ty as Number>::ONES 251 | } 252 | 253 | #[inline] 254 | fn with_bit(self, bit: u32) -> Self { 255 | self.with_bit_in::(bit) 256 | } 257 | 258 | #[inline] 259 | fn with_bit_in(self, bit: u32) -> Self 260 | where 261 | E: Endian, 262 | { 263 | self | E::mask::(bit) 264 | } 265 | 266 | #[inline] 267 | fn without_bit(self, bit: u32) -> Self { 268 | self.without_bit_in::(bit) 269 | } 270 | 271 | #[inline] 272 | fn without_bit_in(self, bit: u32) -> Self 273 | where 274 | E: Endian, 275 | { 276 | self & !E::mask::(bit) 277 | } 278 | 279 | #[inline] 280 | fn union(self, other: Self) -> Self { 281 | self | other 282 | } 283 | 284 | #[inline] 285 | fn conjunction(self, other: Self) -> Self { 286 | self & other 287 | } 288 | 289 | #[inline] 290 | fn difference(self, other: Self) -> Self { 291 | self & !other 292 | } 293 | 294 | #[inline] 295 | fn symmetric_difference(self, other: Self) -> Self { 296 | self ^ other 297 | } 298 | 299 | #[inline] 300 | fn into_iter_ones(self) -> Self::IntoIterOnes { 301 | IterOnes::new(self) 302 | } 303 | 304 | #[inline] 305 | fn into_iter_ones_in(self) -> Self::IntoIterOnesIn 306 | where 307 | E: Endian, 308 | { 309 | IterOnes::new(self) 310 | } 311 | 312 | #[inline] 313 | fn into_iter_zeros(self) -> Self::IntoIterZeros { 314 | IterZeros::new(self) 315 | } 316 | 317 | #[inline] 318 | fn into_iter_zeros_in(self) -> Self::IntoIterZerosIn 319 | where 320 | E: Endian, 321 | { 322 | IterZeros::new(self) 323 | } 324 | } 325 | }; 326 | } 327 | 328 | number!(usize); 329 | number!(isize); 330 | number!(u8); 331 | number!(i8); 332 | number!(u16); 333 | number!(i16); 334 | number!(u32); 335 | number!(i32); 336 | number!(u64); 337 | number!(i64); 338 | number!(u128); 339 | number!(i128); 340 | 341 | /// An iterator over ones in a primitive number. 342 | #[derive(Clone)] 343 | #[repr(transparent)] 344 | pub struct IterOnes { 345 | bits: T, 346 | _shift: PhantomData, 347 | } 348 | 349 | impl IterOnes { 350 | #[inline] 351 | const fn new(bits: T) -> Self { 352 | Self { 353 | bits, 354 | _shift: PhantomData, 355 | } 356 | } 357 | } 358 | 359 | /// Iterator over ones. 360 | /// 361 | /// # Examples 362 | /// 363 | /// ``` 364 | /// use bittle::Bits; 365 | /// 366 | /// let n: u8 = bittle::set![0, 4]; 367 | /// assert!(n.iter_ones().eq([0, 4])); 368 | /// ``` 369 | impl Iterator for IterOnes 370 | where 371 | T: Number, 372 | E: Endian, 373 | { 374 | type Item = u32; 375 | 376 | #[inline] 377 | fn next(&mut self) -> Option { 378 | if self.bits == T::ZEROS { 379 | return None; 380 | } 381 | 382 | let index = E::zeros(self.bits); 383 | self.bits ^= E::mask::(index); 384 | Some(index) 385 | } 386 | } 387 | 388 | /// Double ended iterator over ones. 389 | /// 390 | /// # Examples 391 | /// 392 | /// ``` 393 | /// use bittle::Bits; 394 | /// 395 | /// let n: u8 = bittle::set![0, 4]; 396 | /// assert!(n.iter_ones().rev().eq([4, 0])); 397 | /// ``` 398 | impl DoubleEndedIterator for IterOnes 399 | where 400 | T: Number, 401 | E: Endian, 402 | { 403 | #[inline] 404 | fn next_back(&mut self) -> Option { 405 | if self.bits == T::ZEROS { 406 | return None; 407 | } 408 | 409 | let index = T::BITS.wrapping_sub(E::zeros_rev(self.bits).wrapping_add(1)); 410 | 411 | self.bits ^= E::mask::(index); 412 | Some(index) 413 | } 414 | } 415 | 416 | /// Exact size iterator over ones. 417 | /// 418 | /// # Examples 419 | /// 420 | /// ``` 421 | /// use bittle::Bits; 422 | /// 423 | /// let n: u8 = bittle::set![0, 4]; 424 | /// assert_eq!(n.iter_ones().len(), 2); 425 | /// ``` 426 | impl ExactSizeIterator for IterOnes 427 | where 428 | T: Number, 429 | E: Endian, 430 | { 431 | #[inline] 432 | fn len(&self) -> usize { 433 | self.bits.count_ones() as usize 434 | } 435 | } 436 | 437 | impl FusedIterator for IterOnes 438 | where 439 | T: Number, 440 | E: Endian, 441 | { 442 | } 443 | 444 | /// An iterator over zeros in a primitive number. 445 | #[derive(Clone)] 446 | #[repr(transparent)] 447 | pub struct IterZeros { 448 | bits: T, 449 | _shift: PhantomData, 450 | } 451 | 452 | impl IterZeros { 453 | #[inline] 454 | const fn new(bits: T) -> Self { 455 | Self { 456 | bits, 457 | _shift: PhantomData, 458 | } 459 | } 460 | } 461 | 462 | /// Iterator over zeros. 463 | /// 464 | /// # Examples 465 | /// 466 | /// ``` 467 | /// use bittle::Bits; 468 | /// 469 | /// let n: u8 = bittle::set![0, 4, 6]; 470 | /// assert!(n.iter_zeros().eq([1, 2, 3, 5, 7])); 471 | /// ``` 472 | impl Iterator for IterZeros 473 | where 474 | T: Number, 475 | E: Endian, 476 | { 477 | type Item = u32; 478 | 479 | #[inline] 480 | fn next(&mut self) -> Option { 481 | if self.bits == T::ONES { 482 | return None; 483 | } 484 | 485 | let index = E::ones(self.bits); 486 | self.bits |= E::mask::(index); 487 | Some(index) 488 | } 489 | } 490 | 491 | /// Exact size iterator over zeros. 492 | /// 493 | /// # Examples 494 | /// 495 | /// ``` 496 | /// use bittle::Bits; 497 | /// 498 | /// let n: u8 = bittle::set![0, 4]; 499 | /// assert_eq!(n.iter_zeros().len(), 6); 500 | /// ``` 501 | impl ExactSizeIterator for IterZeros 502 | where 503 | T: Number, 504 | E: Endian, 505 | { 506 | #[inline] 507 | fn len(&self) -> usize { 508 | self.bits.count_zeros() as usize 509 | } 510 | } 511 | 512 | /// Double ended iterator over zeros. 513 | /// 514 | /// # Examples 515 | /// 516 | /// ``` 517 | /// use bittle::Bits; 518 | /// 519 | /// let n: u8 = bittle::set![0, 4, 6]; 520 | /// assert!(n.iter_zeros().rev().eq([7, 5, 3, 2, 1])); 521 | /// ``` 522 | impl DoubleEndedIterator for IterZeros 523 | where 524 | T: Number, 525 | E: Endian, 526 | { 527 | #[inline] 528 | fn next_back(&mut self) -> Option { 529 | if self.bits == T::ONES { 530 | return None; 531 | } 532 | 533 | let index = T::BITS.wrapping_sub(E::ones_rev(self.bits).wrapping_add(1)); 534 | self.bits |= E::mask::(index); 535 | Some(index) 536 | } 537 | } 538 | 539 | impl FusedIterator for IterZeros 540 | where 541 | T: Number, 542 | E: Endian, 543 | { 544 | } 545 | -------------------------------------------------------------------------------- /src/bits_mut.rs: -------------------------------------------------------------------------------- 1 | use crate::bits::Bits; 2 | use crate::endian::{BigEndian, Endian, LittleEndian}; 3 | 4 | /// Bitset mutable operations. 5 | /// 6 | /// This is implemented for primitive types such as: 7 | /// * [`usize`], [`u32`], [`u64`], and other signed numbers. 8 | /// * Arrays made up of numerical primitives, such as `[u32; 32]`. 9 | /// * Slices of numerical primitives, such as `&[u32]`. 10 | /// 11 | /// Also see the associated sibling traits: 12 | /// 13 | /// * [`Bits`] for immutable operations. 14 | /// * [`BitsOwned`] for owned operations. 15 | /// 16 | /// [`BitsOwned`]: crate::BitsOwned 17 | /// 18 | /// # Examples 19 | /// 20 | /// We can use the iterator of each set to compare bit sets of different kinds. 21 | /// The [`Bits::iter_ones`] iterator is guaranteed to iterate elements in the 22 | /// same order: 23 | /// 24 | /// ``` 25 | /// use bittle::{Bits, BitsMut}; 26 | /// 27 | /// let a: [u64; 2] = bittle::set![111]; 28 | /// let mut b = 0u128; 29 | /// 30 | /// assert!(!a.iter_ones().eq(b.iter_ones())); 31 | /// b.set_bit(111); 32 | /// assert!(a.iter_ones().eq(b.iter_ones())); 33 | /// ``` 34 | pub trait BitsMut: Bits { 35 | /// Set the given bit. 36 | /// 37 | /// Indexes which are out of bounds will wrap around in the bitset. 38 | fn set_bit_in(&mut self, index: u32) 39 | where 40 | E: Endian; 41 | 42 | /// Set the given bit using [`DefaultEndian`]. 43 | /// 44 | /// Indexes which are out of bounds will wrap around in the bitset. 45 | /// 46 | /// [`DefaultEndian`]: crate::DefaultEndian 47 | /// 48 | /// # Examples 49 | /// 50 | /// ``` 51 | /// use bittle::{Bits, BitsMut, BitsOwned}; 52 | /// 53 | /// let mut a = 0u32; 54 | /// assert!(!a.test_bit(32)); 55 | /// a.set_bit(0); 56 | /// assert!(a.test_bit(32)); 57 | /// a.clear_bit(32); 58 | /// assert!(!a.test_bit(0)); 59 | /// ``` 60 | /// 61 | /// Using a bigger set: 62 | /// 63 | /// ``` 64 | /// use bittle::{Bits, BitsMut, BitsOwned}; 65 | /// 66 | /// let mut a = u128::ones(); 67 | /// 68 | /// assert!(a.test_bit(0)); 69 | /// assert!(a.test_bit(1)); 70 | /// assert!(a.test_bit(127)); 71 | /// 72 | /// a.clear_bit(1); 73 | /// 74 | /// assert!(a.test_bit(0)); 75 | /// assert!(!a.test_bit(1)); 76 | /// assert!(a.test_bit(127)); 77 | /// 78 | /// a.set_bit(1); 79 | /// 80 | /// assert!(a.test_bit(0)); 81 | /// assert!(a.test_bit(1)); 82 | /// assert!(a.test_bit(127)); 83 | /// ``` 84 | fn set_bit(&mut self, index: u32); 85 | 86 | /// Set the given bit using [`LittleEndian`] indexing. 87 | /// 88 | /// Indexes which are out of bounds will wrap around in the bitset. 89 | /// 90 | /// # Examples 91 | /// 92 | /// ``` 93 | /// use bittle::{Bits, BitsMut, BitsOwned}; 94 | /// 95 | /// let mut a = 0u32; 96 | /// assert!(!a.test_bit_le(32)); 97 | /// a.set_bit_le(0); 98 | /// assert!(a.test_bit_le(32)); 99 | /// a.clear_bit_le(32); 100 | /// assert!(!a.test_bit_le(0)); 101 | /// ``` 102 | /// 103 | /// Using a bigger set: 104 | /// 105 | /// ``` 106 | /// use bittle::{Bits, BitsMut, BitsOwned}; 107 | /// 108 | /// let mut a = u128::ones(); 109 | /// 110 | /// assert!(a.test_bit_le(0)); 111 | /// assert!(a.test_bit_le(1)); 112 | /// assert!(a.test_bit_le(127)); 113 | /// 114 | /// a.clear_bit_le(1); 115 | /// 116 | /// assert!(a.test_bit_le(0)); 117 | /// assert!(!a.test_bit_le(1)); 118 | /// assert!(a.test_bit_le(127)); 119 | /// 120 | /// a.set_bit_le(1); 121 | /// 122 | /// assert!(a.test_bit_le(0)); 123 | /// assert!(a.test_bit_le(1)); 124 | /// assert!(a.test_bit_le(127)); 125 | /// ``` 126 | #[inline] 127 | fn set_bit_le(&mut self, index: u32) { 128 | self.set_bit_in::(index); 129 | } 130 | 131 | /// Set the given bit using [`LittleEndian`] indexing. 132 | /// 133 | /// Indexes which are out of bounds will wrap around in the bitset. 134 | /// 135 | /// # Examples 136 | /// 137 | /// ``` 138 | /// use bittle::{Bits, BitsMut, BitsOwned}; 139 | /// 140 | /// let mut a = 0u32; 141 | /// assert!(!a.test_bit_be(32)); 142 | /// a.set_bit_be(0); 143 | /// assert!(a.test_bit_be(32)); 144 | /// a.clear_bit_be(32); 145 | /// assert!(!a.test_bit_be(0)); 146 | /// ``` 147 | /// 148 | /// Using a bigger set: 149 | /// 150 | /// ``` 151 | /// use bittle::{Bits, BitsMut, BitsOwned}; 152 | /// 153 | /// let mut a = u128::ones(); 154 | /// 155 | /// assert!(a.test_bit_be(0)); 156 | /// assert!(a.test_bit_be(1)); 157 | /// assert!(a.test_bit_be(127)); 158 | /// 159 | /// a.clear_bit_be(1); 160 | /// 161 | /// assert!(a.test_bit_be(0)); 162 | /// assert!(!a.test_bit_be(1)); 163 | /// assert!(a.test_bit_be(127)); 164 | /// 165 | /// a.set_bit_be(1); 166 | /// 167 | /// assert!(a.test_bit_be(0)); 168 | /// assert!(a.test_bit_be(1)); 169 | /// assert!(a.test_bit_be(127)); 170 | /// ``` 171 | #[inline] 172 | fn set_bit_be(&mut self, index: u32) { 173 | self.set_bit_in::(index); 174 | } 175 | 176 | /// Clear the given bit with a custom [`Endian`] indexing. 177 | /// 178 | /// Indexes which are out of bounds will wrap around in the bitset. 179 | fn clear_bit_in(&mut self, index: u32) 180 | where 181 | E: Endian; 182 | 183 | /// Clear the given bit using [`DefaultEndian`]. 184 | /// 185 | /// Indexes which are out of bounds will wrap around in the bitset. 186 | /// 187 | /// [`DefaultEndian`]: crate::DefaultEndian 188 | /// 189 | /// # Examples 190 | /// 191 | /// ``` 192 | /// use bittle::{Bits, BitsMut, BitsOwned}; 193 | /// 194 | /// let mut a = 0u32; 195 | /// assert!(!a.test_bit(32)); 196 | /// a.set_bit(0); 197 | /// assert!(a.test_bit(32)); 198 | /// a.clear_bit(32); 199 | /// assert!(!a.test_bit(0)); 200 | /// ``` 201 | /// 202 | /// Example using array: 203 | /// 204 | /// ``` 205 | /// use bittle::{Bits, BitsMut, BitsOwned}; 206 | /// 207 | /// let mut a = u128::ones(); 208 | /// 209 | /// assert!(a.test_bit(0)); 210 | /// assert!(a.test_bit(1)); 211 | /// assert!(a.test_bit(127)); 212 | /// 213 | /// a.clear_bit(1); 214 | /// 215 | /// assert!(a.test_bit(0)); 216 | /// assert!(!a.test_bit(1)); 217 | /// assert!(a.test_bit(127)); 218 | /// 219 | /// a.set_bit(1); 220 | /// 221 | /// assert!(a.test_bit(0)); 222 | /// assert!(a.test_bit(1)); 223 | /// assert!(a.test_bit(127)); 224 | /// ``` 225 | fn clear_bit(&mut self, index: u32); 226 | 227 | /// Clear the given bit using [`LittleEndian`] indexing. 228 | /// 229 | /// Indexes which are out of bounds will wrap around in the bitset. 230 | /// 231 | /// # Examples 232 | /// 233 | /// ``` 234 | /// use bittle::{Bits, BitsMut, BitsOwned}; 235 | /// 236 | /// let mut a = 0u32; 237 | /// assert!(!a.test_bit_le(32)); 238 | /// a.set_bit_le(0); 239 | /// assert!(a.test_bit_le(32)); 240 | /// a.clear_bit_le(32); 241 | /// assert!(!a.test_bit_le(0)); 242 | /// ``` 243 | /// 244 | /// Example using array: 245 | /// 246 | /// ``` 247 | /// use bittle::{Bits, BitsMut, BitsOwned}; 248 | /// 249 | /// let mut a = u128::ones(); 250 | /// 251 | /// assert!(a.test_bit_le(0)); 252 | /// assert!(a.test_bit_le(1)); 253 | /// assert!(a.test_bit_le(127)); 254 | /// 255 | /// a.clear_bit_le(1); 256 | /// 257 | /// assert!(a.test_bit_le(0)); 258 | /// assert!(!a.test_bit_le(1)); 259 | /// assert!(a.test_bit_le(127)); 260 | /// 261 | /// a.set_bit_le(1); 262 | /// 263 | /// assert!(a.test_bit_le(0)); 264 | /// assert!(a.test_bit_le(1)); 265 | /// assert!(a.test_bit_le(127)); 266 | /// ``` 267 | #[inline] 268 | fn clear_bit_le(&mut self, index: u32) { 269 | self.clear_bit_in::(index); 270 | } 271 | 272 | /// Clear the given bit using [`BigEndian`] indexing. 273 | /// 274 | /// Indexes which are out of bounds will wrap around in the bitset. 275 | /// 276 | /// # Examples 277 | /// 278 | /// ``` 279 | /// use bittle::{Bits, BitsMut, BitsOwned}; 280 | /// 281 | /// let mut a = 0u32; 282 | /// assert!(!a.test_bit_be(32)); 283 | /// a.set_bit_be(0); 284 | /// assert!(a.test_bit_be(32)); 285 | /// a.clear_bit_be(32); 286 | /// assert!(!a.test_bit_be(0)); 287 | /// ``` 288 | /// 289 | /// Example using array: 290 | /// 291 | /// ``` 292 | /// use bittle::{Bits, BitsMut, BitsOwned}; 293 | /// 294 | /// let mut a = u128::ones(); 295 | /// 296 | /// assert!(a.test_bit_be(0)); 297 | /// assert!(a.test_bit_be(1)); 298 | /// assert!(a.test_bit_be(127)); 299 | /// 300 | /// a.clear_bit_be(1); 301 | /// 302 | /// assert!(a.test_bit_be(0)); 303 | /// assert!(!a.test_bit_be(1)); 304 | /// assert!(a.test_bit_be(127)); 305 | /// 306 | /// a.set_bit_be(1); 307 | /// 308 | /// assert!(a.test_bit_be(0)); 309 | /// assert!(a.test_bit_be(1)); 310 | /// assert!(a.test_bit_be(127)); 311 | /// ``` 312 | #[inline] 313 | fn clear_bit_be(&mut self, index: u32) { 314 | self.clear_bit_in::(index); 315 | } 316 | 317 | /// Clear the entire bit pattern. 318 | /// 319 | /// # Examples 320 | /// 321 | /// ``` 322 | /// use bittle::{Bits, BitsMut, BitsOwned}; 323 | /// 324 | /// let mut a = u128::ones(); 325 | /// 326 | /// assert!(a.test_bit(0)); 327 | /// assert!(a.test_bit(1)); 328 | /// assert!(a.test_bit(127)); 329 | /// 330 | /// a.clear_bits(); 331 | /// 332 | /// assert!(!a.test_bit(0)); 333 | /// assert!(!a.test_bit(1)); 334 | /// assert!(!a.test_bit(127)); 335 | /// 336 | /// a.set_bit(1); 337 | /// 338 | /// assert!(!a.test_bit(0)); 339 | /// assert!(a.test_bit(1)); 340 | /// assert!(!a.test_bit(127)); 341 | /// ``` 342 | fn clear_bits(&mut self); 343 | 344 | /// Modify the current set in place so that it becomes a union of this and 345 | /// another set. 346 | /// 347 | /// A union retains all elements from both sets. 348 | /// 349 | /// In terms of numerical operations, this is equivalent to 350 | /// [`BitOrAssign`][core::ops::BitOrAssign] or `a |= b`. 351 | /// 352 | /// # Examples 353 | /// 354 | /// ``` 355 | /// use bittle::{Bits, BitsMut}; 356 | /// 357 | /// let mut a: u128 = bittle::set![31, 67]; 358 | /// let b: u128 = bittle::set![31, 62]; 359 | /// 360 | /// a.union_assign(&b); 361 | /// 362 | /// assert!(a.iter_ones().eq([31, 62, 67])); 363 | /// ``` 364 | /// 365 | /// Using arrays: 366 | /// 367 | /// ``` 368 | /// use bittle::{Bits, BitsMut}; 369 | /// 370 | /// let mut a: [u32; 4] = bittle::set![31, 67]; 371 | /// let b: [u32; 4] = bittle::set![31, 62]; 372 | /// 373 | /// a.union_assign(&b); 374 | /// 375 | /// assert!(a.iter_ones().eq([31, 62, 67])); 376 | /// ``` 377 | fn union_assign(&mut self, other: &Self); 378 | 379 | /// Modify the current set in place so that it becomes a conjunction of this 380 | /// and another set. 381 | /// 382 | /// A conjunction keeps the elements which are in common between two sets. 383 | /// 384 | /// In terms of numerical operations, this is equivalent to 385 | /// [`BitAndAssign`][core::ops::BitAndAssign] or `a &= b`. 386 | /// 387 | /// # Examples 388 | /// 389 | /// ``` 390 | /// use bittle::{Bits, BitsMut}; 391 | /// 392 | /// let mut a: u128 = bittle::set![31, 67]; 393 | /// let b: u128 = bittle::set![31, 62]; 394 | /// 395 | /// a.conjunction_assign(&b); 396 | /// 397 | /// assert!(a.iter_ones().eq([31])); 398 | /// ``` 399 | /// 400 | /// Using arrays: 401 | /// 402 | /// ``` 403 | /// use bittle::{Bits, BitsMut}; 404 | /// 405 | /// let mut a: [u32; 4] = bittle::set![31, 67]; 406 | /// let b: [u32; 4] = bittle::set![31, 62]; 407 | /// 408 | /// a.conjunction_assign(&b); 409 | /// 410 | /// assert!(a.iter_ones().eq([31])); 411 | /// ``` 412 | fn conjunction_assign(&mut self, other: &Self); 413 | 414 | /// Modify the current set in place so that it becomes a difference of this 415 | /// and another set. 416 | /// 417 | /// This keeps the elements in the first set which are not part of the 418 | /// second. 419 | /// 420 | /// # Examples 421 | /// 422 | /// ``` 423 | /// use bittle::{Bits, BitsMut}; 424 | /// 425 | /// let a: u128 = bittle::set![31, 67]; 426 | /// let b: u128 = bittle::set![31, 62]; 427 | /// 428 | /// let mut c = a; 429 | /// c.difference_assign(&b); 430 | /// 431 | /// let mut d = b; 432 | /// d.difference_assign(&a); 433 | /// 434 | /// assert_ne!(c, d); 435 | /// 436 | /// assert!(c.iter_ones().eq([67])); 437 | /// assert!(d.iter_ones().eq([62])); 438 | /// ``` 439 | /// 440 | /// Using arrays: 441 | /// 442 | /// ``` 443 | /// use bittle::{Bits, BitsMut}; 444 | /// 445 | /// let a: [u32; 4] = bittle::set![31, 67]; 446 | /// let b: [u32; 4] = bittle::set![31, 62]; 447 | /// 448 | /// let mut c = a; 449 | /// c.difference_assign(&b); 450 | /// 451 | /// let mut d = b; 452 | /// d.difference_assign(&a); 453 | /// 454 | /// assert_ne!(c, d); 455 | /// 456 | /// assert!(c.iter_ones().eq([67])); 457 | /// assert!(d.iter_ones().eq([62])); 458 | /// ``` 459 | fn difference_assign(&mut self, other: &Self); 460 | 461 | /// Modify the current set in place so that it becomes a symmetric 462 | /// difference of this and another set. 463 | /// 464 | /// This retains elements which are unique to each set. 465 | /// 466 | /// In terms of numerical operations, this is equivalent to 467 | /// [`BitXorAssign`][core::ops::BitXorAssign] or `a ^= b`. 468 | /// 469 | /// # Examples 470 | /// 471 | /// ``` 472 | /// use bittle::{Bits, BitsMut}; 473 | /// 474 | /// let mut a: u128 = bittle::set![31, 67]; 475 | /// let b: u128 = bittle::set![31, 62]; 476 | /// 477 | /// a.symmetric_difference_assign(&b); 478 | /// 479 | /// assert!(a.iter_ones().eq([62, 67])); 480 | /// ``` 481 | /// 482 | /// Using arrays: 483 | /// 484 | /// ``` 485 | /// use bittle::{Bits, BitsMut}; 486 | /// 487 | /// let mut a: [u32; 4] = bittle::set![31, 67]; 488 | /// let b: [u32; 4] = bittle::set![31, 62]; 489 | /// 490 | /// a.symmetric_difference_assign(&b); 491 | /// 492 | /// assert!(a.iter_ones().eq([62, 67])); 493 | /// ``` 494 | fn symmetric_difference_assign(&mut self, other: &Self); 495 | } 496 | 497 | impl BitsMut for &mut T 498 | where 499 | T: ?Sized + BitsMut, 500 | { 501 | #[inline] 502 | fn set_bit_in(&mut self, index: u32) 503 | where 504 | E: Endian, 505 | { 506 | (**self).set_bit_in::(index); 507 | } 508 | 509 | #[inline] 510 | fn set_bit(&mut self, index: u32) { 511 | (**self).set_bit(index); 512 | } 513 | 514 | #[inline] 515 | fn clear_bit_in(&mut self, index: u32) 516 | where 517 | E: Endian, 518 | { 519 | (**self).clear_bit_in::(index); 520 | } 521 | 522 | #[inline] 523 | fn clear_bit(&mut self, index: u32) { 524 | (**self).clear_bit(index); 525 | } 526 | 527 | #[inline] 528 | fn clear_bits(&mut self) { 529 | (**self).clear_bits(); 530 | } 531 | 532 | #[inline] 533 | fn union_assign(&mut self, other: &Self) { 534 | (**self).union_assign(other); 535 | } 536 | 537 | #[inline] 538 | fn conjunction_assign(&mut self, other: &Self) { 539 | (**self).conjunction_assign(other); 540 | } 541 | 542 | #[inline] 543 | fn difference_assign(&mut self, other: &Self) { 544 | (**self).difference_assign(other); 545 | } 546 | 547 | #[inline] 548 | fn symmetric_difference_assign(&mut self, other: &Self) { 549 | (**self).symmetric_difference_assign(other); 550 | } 551 | } 552 | -------------------------------------------------------------------------------- /src/bits_owned.rs: -------------------------------------------------------------------------------- 1 | use crate::bits_mut::BitsMut; 2 | use crate::endian::{BigEndian, Endian, LittleEndian}; 3 | 4 | /// Bitset owned operations. 5 | /// 6 | /// This is implemented for primitive types such as: 7 | /// * [`usize`], [`u32`], [`u64`], and other signed numbers. 8 | /// * Arrays made up of numerical primitives, such as `[u32; 32]`. 9 | /// 10 | /// In contrast to [`Bits`] and [`BitsMut`] this is not implemented for slices 11 | /// such as `&[u32]`. This is since the operations provided here require 12 | /// ownership of the underlying data. 13 | /// 14 | /// Also see the associated sibling traits: 15 | /// 16 | /// * [`Bits`] for immutable operations. 17 | /// * [`BitsMut`] for mutable operations. 18 | /// 19 | /// [`Bits`]: crate::Bits 20 | /// 21 | /// # Examples 22 | /// 23 | /// ``` 24 | /// use bittle::{Bits, BitsOwned}; 25 | /// 26 | /// let a = u128::zeros(); 27 | /// let b = bittle::set!(77); 28 | /// 29 | /// assert!(!a.test_bit(77)); 30 | /// assert!(a.union(b).test_bit(77)); 31 | /// ``` 32 | /// 33 | /// The bit set can also use arrays as its backing storage. 34 | /// 35 | /// ``` 36 | /// use bittle::{Bits, BitsOwned}; 37 | /// 38 | /// let a = <[u32; 4]>::zeros(); 39 | /// let b = bittle::set!(77); 40 | /// 41 | /// assert!(!a.test_bit(77)); 42 | /// assert!(a.union(b).test_bit(77)); 43 | /// ``` 44 | pub trait BitsOwned: Sized + BitsMut { 45 | /// The number of bits in the bit set. 46 | /// 47 | /// # Examples 48 | /// 49 | /// ``` 50 | /// use bittle::BitsOwned; 51 | /// 52 | /// assert_eq!(u32::BITS, 32); 53 | /// assert_eq!(<[u32; 4]>::BITS, 128); 54 | /// ``` 55 | const BITS: u32; 56 | 57 | /// The bit pattern containing all zeros, or one that is empty. 58 | /// 59 | /// See [`BitsOwned::zeros`]. 60 | /// 61 | /// # Examples 62 | /// 63 | /// ``` 64 | /// use bittle::BitsOwned; 65 | /// 66 | /// assert_eq!(u32::ZEROS, 0); 67 | /// assert_eq!(<[u32; 4]>::ZEROS, [0, 0, 0, 0]); 68 | /// ``` 69 | const ZEROS: Self; 70 | 71 | /// The bit pattern containing all ones, or one that is full. 72 | /// 73 | /// See [`BitsOwned::ones`]. 74 | /// 75 | /// # Examples 76 | /// 77 | /// ``` 78 | /// use bittle::BitsOwned; 79 | /// 80 | /// assert_eq!(u32::ONES, u32::MAX); 81 | /// assert_eq!(<[u32; 4]>::ONES, [u32::MAX, u32::MAX, u32::MAX, u32::MAX]); 82 | /// ``` 83 | const ONES: Self; 84 | 85 | /// Owning iterator over bits set to ones using [`DefaultEndian`] indexing. 86 | /// 87 | /// See [`BitsOwned::into_iter_ones`]. 88 | /// 89 | /// [`DefaultEndian`]: crate::DefaultEndian 90 | type IntoIterOnes: Iterator; 91 | 92 | /// Owning iterator over bits set to ones using custom [`Endian`] indexing. 93 | /// 94 | /// See [`BitsOwned::into_iter_ones_in`]. 95 | type IntoIterOnesIn: Iterator 96 | where 97 | E: Endian; 98 | 99 | /// Owning iterator over bits set to zero using the [`DefaultEndian`] ordering. 100 | /// 101 | /// See [`BitsOwned::into_iter_zeros`]. 102 | /// 103 | /// [`DefaultEndian`]: crate::DefaultEndian 104 | type IntoIterZeros: Iterator; 105 | 106 | /// Owning iterator over bits set to zero using custom [`Endian`] ordering. 107 | /// 108 | /// See [`BitsOwned::into_iter_zeros_in`]. 109 | type IntoIterZerosIn: Iterator 110 | where 111 | E: Endian; 112 | 113 | /// Construct a bit set of all zeros, or one that is empty. 114 | /// 115 | /// # Examples 116 | /// 117 | /// ``` 118 | /// use bittle::{Bits, BitsOwned}; 119 | /// 120 | /// let set = u128::zeros(); 121 | /// assert!(set.all_zeros()); 122 | /// assert_eq!(set.iter_ones().count(), 0); 123 | /// ``` 124 | fn zeros() -> Self; 125 | 126 | /// Construct a bit set of all ones, or one that is full. 127 | /// 128 | /// # Examples 129 | /// 130 | /// ``` 131 | /// use bittle::{Bits, BitsOwned}; 132 | /// 133 | /// let set = u128::ones(); 134 | /// assert!(set.all_ones()); 135 | /// assert!(set.iter_ones().eq(0..128)) 136 | /// ``` 137 | fn ones() -> Self; 138 | 139 | /// Set the given bit and return the modified set. 140 | /// 141 | /// # Examples 142 | /// 143 | /// ``` 144 | /// use bittle::{Bits, BitsOwned, LittleEndian}; 145 | /// 146 | /// let set = u128::zeros().with_bit_in::(8).with_bit_in::(12); 147 | /// assert!(set.iter_ones_in::().eq([8, 12])) 148 | /// ``` 149 | /// 150 | /// Using a larger set: 151 | /// 152 | /// ``` 153 | /// use bittle::{Bits, BitsOwned, LittleEndian}; 154 | /// 155 | /// let set = <[u32; 4]>::zeros().with_bit_in::(8).with_bit_in::(12); 156 | /// assert!(set.iter_ones_in::().eq([8, 12])) 157 | /// ``` 158 | #[must_use] 159 | fn with_bit_in(self, bit: u32) -> Self 160 | where 161 | E: Endian; 162 | 163 | /// Set the given bit and return the modified set with the [`DefaultEndian`] 164 | /// indexing. 165 | /// 166 | /// [`DefaultEndian`]: crate::DefaultEndian 167 | /// 168 | /// # Examples 169 | /// 170 | /// ``` 171 | /// use bittle::{Bits, BitsOwned}; 172 | /// 173 | /// let set = u128::zeros().with_bit(8).with_bit(12); 174 | /// assert!(set.iter_ones().eq([8, 12])) 175 | /// ``` 176 | /// 177 | /// Using a larger set: 178 | /// 179 | /// ``` 180 | /// use bittle::{Bits, BitsOwned}; 181 | /// 182 | /// let set = <[u32; 4]>::zeros().with_bit(8).with_bit(12); 183 | /// assert!(set.iter_ones().eq([8, 12])) 184 | /// ``` 185 | #[must_use] 186 | fn with_bit(self, bit: u32) -> Self; 187 | 188 | /// Set the given bit and return the modified set with the [`LittleEndian`] indexing. 189 | /// 190 | /// # Examples 191 | /// 192 | /// ``` 193 | /// use bittle::{Bits, BitsOwned}; 194 | /// 195 | /// let set = u128::zeros().with_bit_le(8).with_bit_le(12); 196 | /// assert!(set.iter_ones_le().eq([8, 12])) 197 | /// ``` 198 | /// 199 | /// Using a larger set: 200 | /// 201 | /// ``` 202 | /// use bittle::{Bits, BitsOwned}; 203 | /// 204 | /// let set = <[u32; 4]>::zeros().with_bit_le(8).with_bit_le(12); 205 | /// assert!(set.iter_ones_le().eq([8, 12])) 206 | /// ``` 207 | #[must_use] 208 | #[inline] 209 | fn with_bit_le(self, bit: u32) -> Self { 210 | self.with_bit_in::(bit) 211 | } 212 | 213 | /// Set the given bit and return the modified set with the [`BigEndian`] 214 | /// indexing. 215 | /// 216 | /// # Examples 217 | /// 218 | /// ``` 219 | /// use bittle::{Bits, BitsOwned}; 220 | /// 221 | /// let set = u128::zeros().with_bit_be(8).with_bit_be(12); 222 | /// assert!(set.iter_ones_be().eq([8, 12])) 223 | /// ``` 224 | /// 225 | /// Using a larger set: 226 | /// 227 | /// ``` 228 | /// use bittle::{Bits, BitsOwned}; 229 | /// 230 | /// let set = <[u32; 4]>::zeros().with_bit_be(8).with_bit_be(12); 231 | /// assert!(set.iter_ones_be().eq([8, 12])) 232 | /// ``` 233 | #[must_use] 234 | #[inline] 235 | fn with_bit_be(self, bit: u32) -> Self { 236 | self.with_bit_in::(bit) 237 | } 238 | 239 | /// Set the given bit and return the modified set using custom [`Endian`] 240 | /// indexing. 241 | /// 242 | /// # Examples 243 | /// 244 | /// ``` 245 | /// use bittle::{Bits, BitsOwned, LittleEndian}; 246 | /// 247 | /// let set = u8::ones().without_bit_in::(2); 248 | /// assert!(set.iter_ones_in::().eq([0, 1, 3, 4, 5, 6, 7])) 249 | /// ``` 250 | /// 251 | /// Using a larger set: 252 | /// 253 | /// ``` 254 | /// use bittle::{Bits, BitsOwned, LittleEndian}; 255 | /// 256 | /// let set = <[u8; 2]>::ones().without_bit_in::(2).without_bit_in::(10); 257 | /// assert!(set.iter_ones_in::().eq([0, 1, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15])) 258 | /// ``` 259 | #[must_use] 260 | fn without_bit_in(self, bit: u32) -> Self 261 | where 262 | E: Endian; 263 | 264 | /// Set the given bit and return the modified set using [`DefaultEndian`] 265 | /// indexing. 266 | /// 267 | /// [`DefaultEndian`]: crate::DefaultEndian 268 | /// 269 | /// # Examples 270 | /// 271 | /// ``` 272 | /// use bittle::{Bits, BitsOwned}; 273 | /// 274 | /// let set = u8::ones().without_bit(2); 275 | /// assert!(set.iter_ones().eq([0, 1, 3, 4, 5, 6, 7])) 276 | /// ``` 277 | /// 278 | /// Using a larger set: 279 | /// 280 | /// ``` 281 | /// use bittle::{Bits, BitsOwned}; 282 | /// 283 | /// let set = <[u8; 2]>::ones().without_bit(2).without_bit(10); 284 | /// assert!(set.iter_ones().eq([0, 1, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15])) 285 | /// ``` 286 | #[must_use] 287 | fn without_bit(self, bit: u32) -> Self; 288 | 289 | /// Set the given bit and return the modified set using [`LittleEndian`] 290 | /// indexing. 291 | /// 292 | /// # Examples 293 | /// 294 | /// ``` 295 | /// use bittle::{Bits, BitsOwned}; 296 | /// 297 | /// let set = u8::ones().without_bit_le(2); 298 | /// assert!(set.iter_ones_le().eq([0, 1, 3, 4, 5, 6, 7])) 299 | /// ``` 300 | /// 301 | /// Using a larger set: 302 | /// 303 | /// ``` 304 | /// use bittle::{Bits, BitsOwned}; 305 | /// 306 | /// let set = <[u8; 2]>::ones().without_bit_le(2).without_bit_le(10); 307 | /// assert!(set.iter_ones_le().eq([0, 1, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15])) 308 | /// ``` 309 | #[must_use] 310 | #[inline] 311 | fn without_bit_le(self, bit: u32) -> Self { 312 | self.without_bit_in::(bit) 313 | } 314 | 315 | /// Set the given bit and return the modified set using [`BigEndian`] 316 | /// indexing. 317 | /// 318 | /// # Examples 319 | /// 320 | /// ``` 321 | /// use bittle::{Bits, BitsOwned}; 322 | /// 323 | /// let set = u8::ones().without_bit_be(2); 324 | /// assert!(set.iter_ones_be().eq([0, 1, 3, 4, 5, 6, 7])) 325 | /// ``` 326 | /// 327 | /// Using a larger set: 328 | /// 329 | /// ``` 330 | /// use bittle::{Bits, BitsOwned}; 331 | /// 332 | /// let set = <[u8; 2]>::ones().without_bit_be(2).without_bit_be(10); 333 | /// assert!(set.iter_ones_be().eq([0, 1, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15])) 334 | /// ``` 335 | #[must_use] 336 | #[inline] 337 | fn without_bit_be(self, bit: u32) -> Self { 338 | self.without_bit_in::(bit) 339 | } 340 | 341 | /// Construct the union between this and another set. 342 | /// 343 | /// A union retains all elements from both sets. 344 | /// 345 | /// In terms of numerical operations, this is equivalent to 346 | /// [`BitOr`][core::ops::BitOr] or `a | b`. 347 | /// 348 | /// # Examples 349 | /// 350 | /// ``` 351 | /// use bittle::{Bits, BitsOwned}; 352 | /// 353 | /// let a: u128 = bittle::set![31, 67]; 354 | /// let b: u128 = bittle::set![31, 62]; 355 | /// 356 | /// let c = a.union(b); 357 | /// assert!(c.iter_ones().eq([31, 62, 67])); 358 | /// ``` 359 | /// 360 | /// Using a larger set: 361 | /// 362 | /// ``` 363 | /// use bittle::{Bits, BitsOwned}; 364 | /// 365 | /// let a: [u32; 4] = bittle::set![31, 67]; 366 | /// let b: [u32; 4] = bittle::set![31, 62]; 367 | /// 368 | /// let c = a.union(b); 369 | /// assert!(c.iter_ones().eq([31, 62, 67])); 370 | /// ``` 371 | #[must_use] 372 | fn union(self, other: Self) -> Self; 373 | 374 | /// Construct a conjunction of this and another set. 375 | /// 376 | /// A conjunction keeps the elements which are in common between two sets. 377 | /// 378 | /// In terms of numerical operations, this is equivalent to 379 | /// [`BitAnd`][core::ops::BitAnd] or `a & b`. 380 | /// 381 | /// # Examples 382 | /// 383 | /// ``` 384 | /// use bittle::{Bits, BitsOwned}; 385 | /// 386 | /// let a: u128 = bittle::set![31, 67]; 387 | /// let b: u128 = bittle::set![31, 62]; 388 | /// 389 | /// let c = a.conjunction(b); 390 | /// assert!(c.iter_ones().eq([31])); 391 | /// ``` 392 | /// 393 | /// Using a larger set: 394 | /// 395 | /// ``` 396 | /// use bittle::{Bits, BitsOwned}; 397 | /// 398 | /// let a: [u32; 4] = bittle::set![31, 67]; 399 | /// let b: [u32; 4] = bittle::set![31, 62]; 400 | /// 401 | /// let c = a.conjunction(b); 402 | /// assert!(c.iter_ones().eq([31])); 403 | /// ``` 404 | #[must_use] 405 | fn conjunction(self, other: Self) -> Self; 406 | 407 | /// Construct the difference between this and another set. 408 | /// 409 | /// This returns the elements in the first set which are not part of the 410 | /// second. 411 | /// 412 | /// # Examples 413 | /// 414 | /// ``` 415 | /// use bittle::{Set, Bits, BitsOwned}; 416 | /// 417 | /// let a: u128 = bittle::set![31, 67]; 418 | /// let b: u128 = bittle::set![31, 62]; 419 | /// 420 | /// let c = a.difference(b); 421 | /// assert!(c.iter_ones().eq([67])); 422 | /// ``` 423 | /// 424 | /// Using a larger set: 425 | /// 426 | /// ``` 427 | /// use bittle::{Bits, BitsOwned}; 428 | /// 429 | /// let a: [u32; 4] = bittle::set![31, 67]; 430 | /// let b: [u32; 4] = bittle::set![31, 62]; 431 | /// 432 | /// let c = a.difference(b); 433 | /// assert!(c.iter_ones().eq([67])); 434 | /// ``` 435 | #[must_use] 436 | fn difference(self, other: Self) -> Self; 437 | 438 | /// Construct the symmetric difference between this and another set. 439 | /// 440 | /// This retains elements which are unique to each set. 441 | /// 442 | /// In terms of numerical operations, this is equivalent to 443 | /// [`BitXor`][core::ops::BitXor] or `a ^ b`. 444 | /// 445 | /// # Examples 446 | /// 447 | /// ``` 448 | /// use bittle::{Bits, BitsOwned}; 449 | /// 450 | /// let a: u128 = bittle::set![31, 67]; 451 | /// let b: u128 = bittle::set![31, 62]; 452 | /// 453 | /// let c = a.symmetric_difference(b); 454 | /// assert!(c.iter_ones().eq([62, 67])); 455 | /// ``` 456 | /// 457 | /// Using a larger set: 458 | /// 459 | /// ``` 460 | /// use bittle::{Bits, BitsOwned}; 461 | /// 462 | /// let a: [u32; 4] = bittle::set![31, 67]; 463 | /// let b: [u32; 4] = bittle::set![31, 62]; 464 | /// 465 | /// let c = a.symmetric_difference(b); 466 | /// assert!(c.iter_ones().eq([62, 67])); 467 | /// ``` 468 | #[must_use] 469 | fn symmetric_difference(self, other: Self) -> Self; 470 | 471 | /// Construct an owning iterator over all ones in a set using the 472 | /// [`DefaultEndian`] indexing. 473 | /// 474 | /// Will iterate through elements from smallest to largest index. 475 | /// 476 | /// [`DefaultEndian`]: crate::DefaultEndian 477 | /// 478 | /// # Examples 479 | /// 480 | /// ``` 481 | /// use bittle::{Bits, BitsOwned}; 482 | /// 483 | /// let a: u128 = bittle::set![3, 7]; 484 | /// assert!(a.into_iter_ones().eq([3, 7])); 485 | /// ``` 486 | /// 487 | /// Using a larger set: 488 | /// 489 | /// ``` 490 | /// use bittle::{Bits, BitsOwned}; 491 | /// 492 | /// let a: [u32; 4] = bittle::set![4, 63, 71, 127]; 493 | /// assert!(a.into_iter_ones().eq([4, 63, 71, 127])); 494 | /// assert!(a.into_iter_ones().rev().eq([127, 71, 63, 4])); 495 | /// ``` 496 | fn into_iter_ones(self) -> Self::IntoIterOnes; 497 | 498 | /// Construct an owning iterator over all ones in a set using custom 499 | /// [`Endian`] indexing. 500 | /// 501 | /// Will iterate through elements from smallest to largest index. 502 | /// 503 | /// # Examples 504 | /// 505 | /// ``` 506 | /// use bittle::{Bits, BitsOwned, LittleEndian}; 507 | /// 508 | /// let a: u128 = bittle::set_le![3, 7]; 509 | /// assert!(a.into_iter_ones_in::().eq([3, 7])); 510 | /// ``` 511 | /// 512 | /// Using a larger set: 513 | /// 514 | /// ``` 515 | /// use bittle::{Bits, BitsOwned, LittleEndian}; 516 | /// 517 | /// let a: [u32; 4] = bittle::set_le![4, 63, 71]; 518 | /// assert!(a.into_iter_ones_in::().eq([4, 63, 71])); 519 | /// ``` 520 | fn into_iter_ones_in(self) -> Self::IntoIterOnesIn 521 | where 522 | E: Endian; 523 | 524 | /// Construct an owning iterator over all ones in a set using 525 | /// [`LittleEndian`] indexing. 526 | /// 527 | /// Will iterate through elements from smallest to largest index. 528 | /// 529 | /// # Examples 530 | /// 531 | /// ``` 532 | /// use bittle::{Bits, BitsOwned}; 533 | /// 534 | /// let a: u128 = bittle::set_le![3, 7]; 535 | /// assert!(a.into_iter_ones_le().eq([3, 7])); 536 | /// ``` 537 | /// 538 | /// Using a larger set: 539 | /// 540 | /// ``` 541 | /// use bittle::{Bits, BitsOwned}; 542 | /// 543 | /// let a: [u32; 4] = bittle::set_le![4, 63, 71]; 544 | /// assert!(a.into_iter_ones_le().eq([4, 63, 71])); 545 | /// ``` 546 | #[inline] 547 | fn into_iter_ones_le(self) -> Self::IntoIterOnesIn { 548 | self.into_iter_ones_in() 549 | } 550 | 551 | /// Construct an owning iterator over all ones in a set using [`BigEndian`] 552 | /// indexing. 553 | /// 554 | /// Will iterate through elements from smallest to largest index. 555 | /// 556 | /// # Examples 557 | /// 558 | /// ``` 559 | /// use bittle::{Bits, BitsOwned}; 560 | /// 561 | /// let a: u128 = bittle::set![3, 7]; 562 | /// assert!(a.into_iter_ones_be().eq([3, 7])); 563 | /// ``` 564 | /// 565 | /// Using a larger set: 566 | /// 567 | /// ``` 568 | /// use bittle::{Bits, BitsOwned}; 569 | /// 570 | /// let a: [u32; 4] = bittle::set![4, 63, 71]; 571 | /// assert!(a.into_iter_ones_be().eq([4, 63, 71])); 572 | /// ``` 573 | #[inline] 574 | fn into_iter_ones_be(self) -> Self::IntoIterOnesIn { 575 | self.into_iter_ones_in() 576 | } 577 | 578 | /// Construct an owning iterator over all zeros in a set using 579 | /// [`DefaultEndian`] indexing. 580 | /// 581 | /// Will iterate through elements from smallest to largest index. 582 | /// 583 | /// [`DefaultEndian`]: crate::DefaultEndian 584 | /// 585 | /// # Examples 586 | /// 587 | /// ``` 588 | /// use bittle::{Bits, BitsOwned}; 589 | /// 590 | /// let a: u16 = bittle::set![4, 7, 10]; 591 | /// assert!(a.into_iter_zeros().eq([0, 1, 2, 3, 5, 6, 8, 9, 11, 12, 13, 14, 15])); 592 | /// ``` 593 | /// 594 | /// Using a larger set: 595 | /// 596 | /// ``` 597 | /// use bittle::{Bits, BitsOwned}; 598 | /// 599 | /// let a: [u8; 2] = bittle::set![4, 7, 10]; 600 | /// assert!(a.into_iter_zeros().eq([0, 1, 2, 3, 5, 6, 8, 9, 11, 12, 13, 14, 15])); 601 | /// assert!(a.into_iter_zeros().rev().eq([15, 14, 13, 12, 11, 9, 8, 6, 5, 3, 2, 1, 0])); 602 | /// ``` 603 | fn into_iter_zeros(self) -> Self::IntoIterZeros; 604 | 605 | /// Construct an owning iterator over all zeros in a set using custom 606 | /// [`Endian`] indexing. 607 | /// 608 | /// Will iterate through elements from smallest to largest index. 609 | /// 610 | /// # Examples 611 | /// 612 | /// ``` 613 | /// use bittle::{Bits, BitsOwned, LittleEndian}; 614 | /// 615 | /// let a: u16 = bittle::set_le![4, 7, 10]; 616 | /// assert!(a.into_iter_zeros_in::().eq([0, 1, 2, 3, 5, 6, 8, 9, 11, 12, 13, 14, 15])); 617 | /// ``` 618 | /// 619 | /// Using a larger set: 620 | /// 621 | /// ``` 622 | /// use bittle::{Bits, BitsOwned, LittleEndian}; 623 | /// 624 | /// let a: [u8; 2] = bittle::set_le![4, 7, 10]; 625 | /// assert!(a.into_iter_zeros_in::().eq([0, 1, 2, 3, 5, 6, 8, 9, 11, 12, 13, 14, 15])); 626 | /// ``` 627 | fn into_iter_zeros_in(self) -> Self::IntoIterZerosIn 628 | where 629 | E: Endian; 630 | 631 | /// Construct an owning iterator over all zeros in a set using 632 | /// [`LittleEndian`] indexing. 633 | /// 634 | /// Will iterate through elements from smallest to largest index. 635 | /// 636 | /// # Examples 637 | /// 638 | /// ``` 639 | /// use bittle::{Bits, BitsOwned}; 640 | /// 641 | /// let a: u16 = bittle::set_le![4, 7, 10]; 642 | /// assert!(a.into_iter_zeros_le().eq([0, 1, 2, 3, 5, 6, 8, 9, 11, 12, 13, 14, 15])); 643 | /// ``` 644 | /// 645 | /// Using a larger set: 646 | /// 647 | /// ``` 648 | /// use bittle::{Bits, BitsOwned}; 649 | /// 650 | /// let a: [u8; 2] = bittle::set_le![4, 7, 10]; 651 | /// assert!(a.into_iter_zeros_le().eq([0, 1, 2, 3, 5, 6, 8, 9, 11, 12, 13, 14, 15])); 652 | /// ``` 653 | #[inline] 654 | fn into_iter_zeros_le(self) -> Self::IntoIterZerosIn { 655 | self.into_iter_zeros_in() 656 | } 657 | 658 | /// Construct an owning iterator over all zeros in a set using [`BigEndian`] 659 | /// indexing. 660 | /// 661 | /// Will iterate through elements from smallest to largest index. 662 | /// 663 | /// # Examples 664 | /// 665 | /// ``` 666 | /// use bittle::{Bits, BitsOwned}; 667 | /// 668 | /// let a: u16 = bittle::set![4, 7, 10]; 669 | /// assert!(a.into_iter_zeros_be().eq([0, 1, 2, 3, 5, 6, 8, 9, 11, 12, 13, 14, 15])); 670 | /// ``` 671 | /// 672 | /// Using a larger set: 673 | /// 674 | /// ``` 675 | /// use bittle::{Bits, BitsOwned}; 676 | /// 677 | /// let a: [u8; 2] = bittle::set![4, 7, 10]; 678 | /// assert!(a.into_iter_zeros_be().eq([0, 1, 2, 3, 5, 6, 8, 9, 11, 12, 13, 14, 15])); 679 | /// ``` 680 | #[inline] 681 | fn into_iter_zeros_be(self) -> Self::IntoIterZerosIn { 682 | self.into_iter_zeros_in() 683 | } 684 | } 685 | -------------------------------------------------------------------------------- /src/bits.rs: -------------------------------------------------------------------------------- 1 | //! Traits which define the behaviors of a bit set. 2 | 3 | mod join_ones; 4 | use crate::endian::{BigEndian, DefaultEndian, Endian, LittleEndian}; 5 | 6 | pub use self::join_ones::JoinOnes; 7 | 8 | mod sealed { 9 | pub trait Sealed {} 10 | 11 | impl Sealed for &mut T where T: ?Sized + crate::Bits {} 12 | impl Sealed for &T where T: ?Sized + crate::Bits {} 13 | impl Sealed for [T] {} 14 | impl Sealed for [T; N] {} 15 | impl Sealed for crate::set::Set where T: ?Sized {} 16 | } 17 | 18 | pub(crate) use self::sealed::Sealed; 19 | 20 | /// Bitset immutable operations. 21 | /// 22 | /// This is implemented for primitive types such as: 23 | /// * [`usize`], [`u32`], [`u64`], and other signed numbers. 24 | /// * Arrays made up of numerical primitives, such as `[u32; 32]`. 25 | /// * Slices of numerical primitives, such as `&[u32]`. 26 | /// 27 | /// Also see the associated sibling traits: 28 | /// 29 | /// * [`BitsMut`] for mutable operations. 30 | /// * [`BitsOwned`] for owned operations. 31 | /// 32 | /// [`BitsMut`]: crate::BitsMut 33 | /// [`BitsOwned`]: crate::BitsOwned 34 | /// 35 | /// # Examples 36 | /// 37 | /// We can use the iterator of each set to compare bit sets of different kinds. 38 | /// The [`Bits::iter_ones`] iterator is guaranteed to iterate elements in the 39 | /// same order: 40 | /// 41 | /// ``` 42 | /// use bittle::{Bits, BitsMut}; 43 | /// 44 | /// let a: [u64; 2] = bittle::set![111]; 45 | /// let mut b = 0u128; 46 | /// 47 | /// assert!(!a.iter_ones().eq(b.iter_ones())); 48 | /// b.set_bit(111); 49 | /// assert!(a.iter_ones().eq(b.iter_ones())); 50 | /// ``` 51 | pub trait Bits: Sealed { 52 | /// The iterator over ones in this bit pattern using [`DefaultEndian`] 53 | /// indexing. 54 | /// 55 | /// See [`Bits::iter_ones`]. 56 | /// 57 | /// [`DefaultEndian`]: crate::DefaultEndian 58 | type IterOnes<'a>: Iterator 59 | where 60 | Self: 'a; 61 | 62 | /// The iterator over ones in this bit pattern using custom [`Endian`] 63 | /// indexing. 64 | /// 65 | /// See [`Bits::iter_ones_in`]. 66 | type IterOnesIn<'a, E>: Iterator 67 | where 68 | Self: 'a, 69 | E: Endian; 70 | 71 | /// The iterator over zeros in this bit pattern using [`DefaultEndian`] 72 | /// indexing. 73 | /// 74 | /// See [`Bits::iter_zeros`]. 75 | /// 76 | /// [`DefaultEndian`]: crate::DefaultEndian 77 | type IterZeros<'a>: Iterator 78 | where 79 | Self: 'a; 80 | 81 | /// The iterator over zeros in this bit pattern using custom [`Endian`] 82 | /// indexing. 83 | /// 84 | /// See [`Bits::iter_zeros_in`]. 85 | type IterZerosIn<'a, E>: Iterator 86 | where 87 | Self: 'a, 88 | E: Endian; 89 | 90 | /// Get the number of ones in the set. 91 | /// 92 | /// # Examples 93 | /// 94 | /// ``` 95 | /// use bittle::{Bits, BitsMut}; 96 | /// 97 | /// let mut a = 0u128; 98 | /// assert_eq!(a.count_ones(), 0); 99 | /// a.set_bit(4); 100 | /// assert_eq!(a.count_ones(), 1); 101 | /// ``` 102 | /// 103 | /// Using a larger set: 104 | /// 105 | /// ``` 106 | /// use bittle::{Bits, BitsMut}; 107 | /// 108 | /// let mut a = [0u128, 0]; 109 | /// assert_eq!(a.count_ones(), 0); 110 | /// a.set_bit(240); 111 | /// assert_eq!(a.count_ones(), 1); 112 | /// ``` 113 | fn count_ones(&self) -> u32; 114 | 115 | /// Get the number of ones in the set. 116 | /// 117 | /// # Examples 118 | /// 119 | /// ``` 120 | /// use bittle::{Bits, BitsMut}; 121 | /// 122 | /// let mut a = 0u128; 123 | /// assert_eq!(a.count_zeros(), 128); 124 | /// a.set_bit(4); 125 | /// assert_eq!(a.count_zeros(), 127); 126 | /// ``` 127 | /// 128 | /// Using a larger set: 129 | /// 130 | /// ``` 131 | /// use bittle::{Bits, BitsMut}; 132 | /// 133 | /// let mut a = [0u128, 0]; 134 | /// assert_eq!(a.count_zeros(), 256); 135 | /// a.set_bit(240); 136 | /// assert_eq!(a.count_zeros(), 255); 137 | /// ``` 138 | fn count_zeros(&self) -> u32; 139 | 140 | /// Get the capacity of the underlying set. 141 | /// 142 | /// # Examples 143 | /// 144 | /// ``` 145 | /// use bittle::Bits; 146 | /// 147 | /// let mut set = 0u128; 148 | /// assert_eq!(set.bits_capacity(), 128); 149 | /// ``` 150 | /// 151 | /// Using a larger set: 152 | /// 153 | /// ``` 154 | /// use bittle::Bits; 155 | /// 156 | /// let mut set = [0u128, 0]; 157 | /// assert_eq!(set.bits_capacity(), 256); 158 | /// ``` 159 | fn bits_capacity(&self) -> u32; 160 | 161 | /// Test if this set is empty, or all zeros. 162 | /// 163 | /// # Examples 164 | /// 165 | /// ``` 166 | /// use bittle::Bits; 167 | /// 168 | /// let a: u32 = bittle::set![]; 169 | /// assert!(a.all_zeros()); 170 | /// 171 | /// let a: u32 = bittle::set![1]; 172 | /// assert!(!a.all_zeros()); 173 | /// ``` 174 | /// 175 | /// Using a larger set: 176 | /// 177 | /// ``` 178 | /// use bittle::Bits; 179 | /// 180 | /// let a: [u32; 2] = bittle::set![]; 181 | /// assert!(a.all_zeros()); 182 | /// 183 | /// let a: [u32; 2] = bittle::set![55]; 184 | /// assert!(!a.all_zeros()); 185 | /// ``` 186 | fn all_zeros(&self) -> bool; 187 | 188 | /// Test if bit set is full, or all ones. 189 | /// 190 | /// # Examples 191 | /// 192 | /// ``` 193 | /// use bittle::{Bits, BitsMut, BitsOwned}; 194 | /// 195 | /// let mut set = u128::ones(); 196 | /// assert!(set.all_ones()); 197 | /// set.clear_bit(4); 198 | /// assert!(!set.all_ones()); 199 | /// ``` 200 | fn all_ones(&self) -> bool; 201 | 202 | /// Test if the given bit is set using [`DefaultEndian`] indexing. 203 | /// 204 | /// Indexes which are out of bounds will wrap around in the bitset. 205 | /// 206 | /// [`DefaultEndian`]: crate::DefaultEndian 207 | /// 208 | /// # Examples 209 | /// 210 | /// ``` 211 | /// use bittle::Bits; 212 | /// 213 | /// let a: u32 = bittle::set![]; 214 | /// assert!(!a.test_bit(32)); 215 | /// 216 | /// let a: u32 = bittle::set![32]; 217 | /// assert!(a.test_bit(32)); 218 | /// ``` 219 | /// 220 | /// Using a larger set: 221 | /// 222 | /// ``` 223 | /// use bittle::Bits; 224 | /// 225 | /// let a: [u32; 2] = bittle::set![]; 226 | /// assert!(!a.test_bit(55)); 227 | /// 228 | /// let a: [u32; 2] = bittle::set![55]; 229 | /// assert!(a.test_bit(55)); 230 | /// ``` 231 | fn test_bit(&self, index: u32) -> bool; 232 | 233 | /// Test if the given bit is set using custom [`Endian`] indexing. 234 | /// 235 | /// Indexes which are out of bounds will wrap around in the bitset. 236 | /// 237 | /// # Examples 238 | /// 239 | /// ``` 240 | /// use bittle::{Bits, LittleEndian}; 241 | /// 242 | /// let a: u32 = bittle::set_le![]; 243 | /// assert!(!a.test_bit_in::(32)); 244 | /// 245 | /// let a: u32 = bittle::set_le![32]; 246 | /// assert!(a.test_bit_in::(32)); 247 | /// ``` 248 | /// 249 | /// Using a larger set: 250 | /// 251 | /// ``` 252 | /// use bittle::{Bits, LittleEndian}; 253 | /// 254 | /// let a: [u32; 2] = bittle::set_le![]; 255 | /// assert!(!a.test_bit_in::(55)); 256 | /// 257 | /// let a: [u32; 2] = bittle::set_le![55]; 258 | /// assert!(a.test_bit_in::(55)); 259 | /// ``` 260 | fn test_bit_in(&self, index: u32) -> bool 261 | where 262 | E: Endian; 263 | 264 | /// Test if the given bit is set using [`LittleEndian`] indexing. 265 | /// 266 | /// Indexes which are out of bounds will wrap around in the bitset. 267 | /// 268 | /// # Examples 269 | /// 270 | /// ``` 271 | /// use bittle::Bits; 272 | /// 273 | /// let a: u32 = bittle::set_le![]; 274 | /// assert!(!a.test_bit_le(32)); 275 | /// 276 | /// let a: u32 = bittle::set_le![32]; 277 | /// assert!(a.test_bit_le(32)); 278 | /// ``` 279 | /// 280 | /// Using a larger set: 281 | /// 282 | /// ``` 283 | /// use bittle::Bits; 284 | /// 285 | /// let a: [u32; 2] = bittle::set_le![]; 286 | /// assert!(!a.test_bit_le(55)); 287 | /// 288 | /// let a: [u32; 2] = bittle::set_le![55]; 289 | /// assert!(a.test_bit_le(55)); 290 | /// ``` 291 | #[inline] 292 | fn test_bit_le(&self, index: u32) -> bool { 293 | self.test_bit_in::(index) 294 | } 295 | 296 | /// Test if the given bit is set using [`BigEndian`] indexing. 297 | /// 298 | /// Indexes which are out of bounds will wrap around in the bitset. 299 | /// 300 | /// # Examples 301 | /// 302 | /// ``` 303 | /// use bittle::Bits; 304 | /// 305 | /// let a: u32 = bittle::set_be![]; 306 | /// assert!(!a.test_bit_be(32)); 307 | /// 308 | /// let a: u32 = bittle::set_be![32]; 309 | /// assert!(a.test_bit_be(32)); 310 | /// ``` 311 | /// 312 | /// Using a larger set: 313 | /// 314 | /// ``` 315 | /// use bittle::Bits; 316 | /// 317 | /// let a: [u32; 2] = bittle::set_be![]; 318 | /// assert!(!a.test_bit_be(55)); 319 | /// 320 | /// let a: [u32; 2] = bittle::set_be![55]; 321 | /// assert!(a.test_bit_be(55)); 322 | /// ``` 323 | #[inline] 324 | fn test_bit_be(&self, index: u32) -> bool { 325 | self.test_bit_in::(index) 326 | } 327 | 328 | /// Construct an iterator over ones in the bit set using [`DefaultEndian`] 329 | /// indexing. 330 | /// 331 | /// Will iterate through elements from smallest to largest index. 332 | /// 333 | /// [`DefaultEndian`]: crate::DefaultEndian 334 | /// 335 | /// # Examples 336 | /// 337 | /// ``` 338 | /// use bittle::Bits; 339 | /// 340 | /// let set: u128 = bittle::set![3, 7]; 341 | /// assert!(set.iter_ones().eq([3, 7])); 342 | /// ``` 343 | /// 344 | /// A larger bit set: 345 | /// 346 | /// ``` 347 | /// use bittle::Bits; 348 | /// 349 | /// let set: [u32; 4] = bittle::set![4, 67, 71, 127]; 350 | /// assert!(set.iter_ones().eq([4, 67, 71, 127])); 351 | /// assert!(set.iter_ones().rev().eq([127, 71, 67, 4])); 352 | /// ``` 353 | fn iter_ones(&self) -> Self::IterOnes<'_>; 354 | 355 | /// Construct an iterator over ones in the bit set. 356 | /// 357 | /// Will iterate through elements from smallest to largest index. 358 | /// 359 | /// # Examples 360 | /// 361 | /// ``` 362 | /// use bittle::{Bits, LittleEndian}; 363 | /// 364 | /// let set: u128 = bittle::set_le![3, 7]; 365 | /// assert!(set.iter_ones_in::().eq([3, 7])); 366 | /// ``` 367 | /// 368 | /// A larger bit set: 369 | /// 370 | /// ``` 371 | /// use bittle::{Bits, LittleEndian}; 372 | /// 373 | /// let set: [u32; 4] = bittle::set_le![4, 67, 71]; 374 | /// assert!(set.iter_ones_in::().eq([4, 67, 71])); 375 | /// ``` 376 | fn iter_ones_in(&self) -> Self::IterOnesIn<'_, E> 377 | where 378 | E: Endian; 379 | 380 | /// Construct an iterator over ones in the bit set using [`LittleEndian`] 381 | /// indexing. 382 | /// 383 | /// Will iterate through elements from smallest to largest index. 384 | /// 385 | /// # Examples 386 | /// 387 | /// ``` 388 | /// use bittle::Bits; 389 | /// 390 | /// let set: u128 = bittle::set_le![3, 7]; 391 | /// assert!(set.iter_ones_le().eq([3, 7])); 392 | /// ``` 393 | /// 394 | /// A larger bit set: 395 | /// 396 | /// ``` 397 | /// use bittle::Bits; 398 | /// 399 | /// let set: [u32; 4] = bittle::set_le![4, 67, 71]; 400 | /// assert!(set.iter_ones_le().eq([4, 67, 71])); 401 | /// ``` 402 | #[inline] 403 | fn iter_ones_le(&self) -> Self::IterOnesIn<'_, LittleEndian> { 404 | self.iter_ones_in() 405 | } 406 | 407 | /// Construct an iterator over ones in the bit set using [`BigEndian`] 408 | /// indexing. 409 | /// 410 | /// Will iterate through elements from smallest to largest index. 411 | /// 412 | /// # Examples 413 | /// 414 | /// ``` 415 | /// use bittle::Bits; 416 | /// 417 | /// let set: u128 = bittle::set_be![3, 7]; 418 | /// assert!(set.iter_ones_be().eq([3, 7])); 419 | /// ``` 420 | /// 421 | /// A larger bit set: 422 | /// 423 | /// ``` 424 | /// use bittle::Bits; 425 | /// 426 | /// let set: [u32; 4] = bittle::set_be![4, 67, 71]; 427 | /// assert!(set.iter_ones_be().eq([4, 67, 71])); 428 | /// ``` 429 | #[inline] 430 | fn iter_ones_be(&self) -> Self::IterOnesIn<'_, BigEndian> { 431 | self.iter_ones_in() 432 | } 433 | 434 | /// Construct an iterator over zeros in the bit set using [`DefaultEndian`] 435 | /// indexing. 436 | /// 437 | /// Will iterate through elements from smallest to largest index. 438 | /// 439 | /// [`DefaultEndian`]: crate::DefaultEndian 440 | /// 441 | /// # Examples 442 | /// 443 | /// ``` 444 | /// use bittle::Bits; 445 | /// 446 | /// let set: u8 = bittle::set![3, 7]; 447 | /// assert!(set.iter_zeros().eq([0, 1, 2, 4, 5, 6])); 448 | /// ``` 449 | /// 450 | /// A larger bit set: 451 | /// 452 | /// ``` 453 | /// use bittle::Bits; 454 | /// 455 | /// let set: [u8; 2] = bittle::set![3, 7, 13, 14, 15]; 456 | /// assert!(set.iter_zeros().eq([0, 1, 2, 4, 5, 6, 8, 9, 10, 11, 12])); 457 | /// assert!(set.iter_zeros().rev().eq([12, 11, 10, 9, 8, 6, 5, 4, 2, 1, 0])); 458 | /// ``` 459 | fn iter_zeros(&self) -> Self::IterZeros<'_>; 460 | 461 | /// Construct an iterator over zeros in the bit set. 462 | /// 463 | /// Will iterate through elements from smallest to largest index. 464 | /// 465 | /// # Examples 466 | /// 467 | /// ``` 468 | /// use bittle::{Bits, LittleEndian}; 469 | /// 470 | /// let set: u8 = bittle::set_le![3, 7]; 471 | /// assert!(set.iter_zeros_in::().eq([0, 1, 2, 4, 5, 6])); 472 | /// ``` 473 | /// 474 | /// A larger bit set: 475 | /// 476 | /// ``` 477 | /// use bittle::{Bits, LittleEndian}; 478 | /// 479 | /// let set: [u8; 2] = bittle::set_le![3, 7, 13, 14, 15]; 480 | /// assert!(set.iter_zeros_in::().eq([0, 1, 2, 4, 5, 6, 8, 9, 10, 11, 12])); 481 | /// ``` 482 | fn iter_zeros_in(&self) -> Self::IterZerosIn<'_, E> 483 | where 484 | E: Endian; 485 | 486 | /// Construct an iterator over zeros in the bit set using [`LittleEndian`] indexing. 487 | /// 488 | /// Will iterate through elements from smallest to largest index. 489 | /// 490 | /// # Examples 491 | /// 492 | /// ``` 493 | /// use bittle::Bits; 494 | /// 495 | /// let set: u8 = bittle::set_le![3, 7]; 496 | /// assert!(set.iter_zeros_le().eq([0, 1, 2, 4, 5, 6])); 497 | /// ``` 498 | /// 499 | /// A larger bit set: 500 | /// 501 | /// ``` 502 | /// use bittle::Bits; 503 | /// 504 | /// let set: [u8; 2] = bittle::set_le![3, 7, 13, 14, 15]; 505 | /// assert!(set.iter_zeros_le().eq([0, 1, 2, 4, 5, 6, 8, 9, 10, 11, 12])); 506 | /// ``` 507 | #[inline] 508 | fn iter_zeros_le(&self) -> Self::IterZerosIn<'_, LittleEndian> { 509 | self.iter_zeros_in() 510 | } 511 | 512 | /// Construct an iterator over zeros in the bit set using [`BigEndian`] 513 | /// indexing. 514 | /// 515 | /// Will iterate through elements from smallest to largest index. 516 | /// 517 | /// # Examples 518 | /// 519 | /// ``` 520 | /// use bittle::Bits; 521 | /// 522 | /// let set: u8 = bittle::set_be![3, 7]; 523 | /// assert!(set.iter_zeros_be().eq([0, 1, 2, 4, 5, 6])); 524 | /// ``` 525 | /// 526 | /// A larger bit set: 527 | /// 528 | /// ``` 529 | /// use bittle::Bits; 530 | /// 531 | /// let set: [u8; 2] = bittle::set_be![3, 7, 13, 14, 15]; 532 | /// assert!(set.iter_zeros_be().eq([0, 1, 2, 4, 5, 6, 8, 9, 10, 11, 12])); 533 | /// ``` 534 | #[inline] 535 | fn iter_zeros_be(&self) -> Self::IterZerosIn<'_, BigEndian> { 536 | self.iter_zeros_in() 537 | } 538 | 539 | /// Join this bit set with an iterator, creating an iterator that only 540 | /// yields the elements which are set to ones using custom [`Endian`] 541 | /// indexing. 542 | /// 543 | /// The underlying iterator is advanced using [`Iterator::nth`] as 544 | /// appropriate. 545 | /// 546 | /// # Examples 547 | /// 548 | /// ``` 549 | /// use bittle::{Bits, LittleEndian}; 550 | /// 551 | /// let mask: u128 = bittle::set_le![0, 1, 3]; 552 | /// let mut values = vec![false, false, false, false]; 553 | /// 554 | /// for value in mask.join_ones_in::<_, LittleEndian>(values.iter_mut()) { 555 | /// *value = true; 556 | /// } 557 | /// 558 | /// assert_eq!(values, vec![true, true, false, true]); 559 | /// ``` 560 | fn join_ones_in(&self, iter: I) -> JoinOnes<'_, Self, E, I::IntoIter> 561 | where 562 | I: IntoIterator, 563 | E: Endian, 564 | { 565 | JoinOnes::new(self.iter_ones_in(), iter.into_iter()) 566 | } 567 | 568 | /// Join this bit set with an iterator, creating an iterator that only 569 | /// yields the elements which are set to ones using [`DefaultEndian`] 570 | /// indexing. 571 | /// 572 | /// The underlying iterator is advanced using [`Iterator::nth`] as 573 | /// appropriate. 574 | /// 575 | /// [`DefaultEndian`]: crate::DefaultEndian 576 | /// 577 | /// # Examples 578 | /// 579 | /// ``` 580 | /// use bittle::Bits; 581 | /// 582 | /// let mask: u128 = bittle::set![0, 1, 3]; 583 | /// let mut values = vec![false, false, false, false]; 584 | /// 585 | /// for value in mask.join_ones(values.iter_mut()) { 586 | /// *value = true; 587 | /// } 588 | /// 589 | /// assert_eq!(values, vec![true, true, false, true]); 590 | /// ``` 591 | #[inline] 592 | fn join_ones(&self, iter: I) -> JoinOnes<'_, Self, DefaultEndian, I::IntoIter> 593 | where 594 | I: IntoIterator, 595 | { 596 | JoinOnes::new(self.iter_ones_in(), iter.into_iter()) 597 | } 598 | 599 | /// Join this bit set with an iterator, creating an iterator that only 600 | /// yields the elements which are set to ones using [`LittleEndian`] indexing. 601 | /// 602 | /// The underlying iterator is advanced using [`Iterator::nth`] as 603 | /// appropriate. 604 | /// 605 | /// # Examples 606 | /// 607 | /// ``` 608 | /// use bittle::Bits; 609 | /// 610 | /// let mask: u8 = 0b11010000; 611 | /// let mut values = vec![false, false, false, false]; 612 | /// 613 | /// for value in mask.join_ones_le(values.iter_mut()) { 614 | /// *value = true; 615 | /// } 616 | /// 617 | /// assert_eq!(values, vec![true, true, false, true]); 618 | /// ``` 619 | #[inline] 620 | fn join_ones_le(&self, iter: I) -> JoinOnes<'_, Self, LittleEndian, I::IntoIter> 621 | where 622 | I: IntoIterator, 623 | { 624 | self.join_ones_in(iter) 625 | } 626 | 627 | /// Join this bit set with an iterator, creating an iterator that only 628 | /// yields the elements which are set to ones using [`BigEndian`] indexing. 629 | /// 630 | /// The underlying iterator is advanced using [`Iterator::nth`] as 631 | /// appropriate. 632 | /// 633 | /// # Examples 634 | /// 635 | /// ``` 636 | /// use bittle::Bits; 637 | /// 638 | /// let mask: u8 = 0b00001011; 639 | /// let mut values = vec![false, false, false, false]; 640 | /// 641 | /// for value in mask.join_ones_be(values.iter_mut()) { 642 | /// *value = true; 643 | /// } 644 | /// 645 | /// assert_eq!(values, vec![true, true, false, true]); 646 | /// ``` 647 | #[inline] 648 | fn join_ones_be(&self, iter: I) -> JoinOnes<'_, Self, BigEndian, I::IntoIter> 649 | where 650 | I: IntoIterator, 651 | { 652 | self.join_ones_in(iter) 653 | } 654 | } 655 | 656 | impl Bits for &T 657 | where 658 | T: ?Sized + Bits, 659 | { 660 | type IterOnes<'a> 661 | = T::IterOnes<'a> 662 | where 663 | Self: 'a; 664 | 665 | type IterOnesIn<'a, E> 666 | = T::IterOnesIn<'a, E> 667 | where 668 | Self: 'a, 669 | E: Endian; 670 | 671 | type IterZeros<'a> 672 | = T::IterZeros<'a> 673 | where 674 | Self: 'a; 675 | 676 | type IterZerosIn<'a, E> 677 | = T::IterZerosIn<'a, E> 678 | where 679 | Self: 'a, 680 | E: Endian; 681 | 682 | #[inline] 683 | fn count_ones(&self) -> u32 { 684 | (**self).count_ones() 685 | } 686 | 687 | #[inline] 688 | fn count_zeros(&self) -> u32 { 689 | (**self).count_zeros() 690 | } 691 | 692 | #[inline] 693 | fn bits_capacity(&self) -> u32 { 694 | (**self).bits_capacity() 695 | } 696 | 697 | #[inline] 698 | fn all_zeros(&self) -> bool { 699 | (**self).all_zeros() 700 | } 701 | 702 | #[inline] 703 | fn all_ones(&self) -> bool { 704 | (**self).all_ones() 705 | } 706 | 707 | #[inline] 708 | fn test_bit(&self, index: u32) -> bool { 709 | (**self).test_bit(index) 710 | } 711 | 712 | #[inline] 713 | fn test_bit_in(&self, index: u32) -> bool 714 | where 715 | E: Endian, 716 | { 717 | (**self).test_bit_in::(index) 718 | } 719 | 720 | #[inline] 721 | fn iter_ones(&self) -> Self::IterOnes<'_> { 722 | (**self).iter_ones() 723 | } 724 | 725 | #[inline] 726 | fn iter_ones_in(&self) -> Self::IterOnesIn<'_, E> 727 | where 728 | E: Endian, 729 | { 730 | (**self).iter_ones_in() 731 | } 732 | 733 | #[inline] 734 | fn iter_zeros(&self) -> Self::IterZeros<'_> { 735 | (**self).iter_zeros() 736 | } 737 | 738 | #[inline] 739 | fn iter_zeros_in(&self) -> Self::IterZerosIn<'_, E> 740 | where 741 | E: Endian, 742 | { 743 | (**self).iter_zeros_in() 744 | } 745 | } 746 | 747 | impl Bits for &mut T 748 | where 749 | T: ?Sized + Bits, 750 | { 751 | type IterOnesIn<'a, E> 752 | = T::IterOnesIn<'a, E> 753 | where 754 | Self: 'a, 755 | E: Endian; 756 | 757 | type IterOnes<'a> 758 | = T::IterOnes<'a> 759 | where 760 | Self: 'a; 761 | 762 | type IterZerosIn<'a, E> 763 | = T::IterZerosIn<'a, E> 764 | where 765 | Self: 'a, 766 | E: Endian; 767 | 768 | type IterZeros<'a> 769 | = T::IterZeros<'a> 770 | where 771 | Self: 'a; 772 | 773 | #[inline] 774 | fn count_ones(&self) -> u32 { 775 | (**self).count_ones() 776 | } 777 | 778 | #[inline] 779 | fn count_zeros(&self) -> u32 { 780 | (**self).count_zeros() 781 | } 782 | 783 | #[inline] 784 | fn bits_capacity(&self) -> u32 { 785 | (**self).bits_capacity() 786 | } 787 | 788 | #[inline] 789 | fn all_zeros(&self) -> bool { 790 | (**self).all_zeros() 791 | } 792 | 793 | #[inline] 794 | fn all_ones(&self) -> bool { 795 | (**self).all_ones() 796 | } 797 | 798 | #[inline] 799 | fn test_bit(&self, index: u32) -> bool { 800 | (**self).test_bit(index) 801 | } 802 | 803 | #[inline] 804 | fn test_bit_in(&self, index: u32) -> bool 805 | where 806 | E: Endian, 807 | { 808 | (**self).test_bit_in::(index) 809 | } 810 | 811 | #[inline] 812 | fn iter_ones(&self) -> Self::IterOnes<'_> { 813 | (**self).iter_ones() 814 | } 815 | 816 | #[inline] 817 | fn iter_ones_in(&self) -> Self::IterOnesIn<'_, E> 818 | where 819 | E: Endian, 820 | { 821 | (**self).iter_ones_in() 822 | } 823 | 824 | #[inline] 825 | fn iter_zeros(&self) -> Self::IterZeros<'_> { 826 | (**self).iter_zeros() 827 | } 828 | 829 | #[inline] 830 | fn iter_zeros_in(&self) -> Self::IterZerosIn<'_, E> 831 | where 832 | E: Endian, 833 | { 834 | (**self).iter_zeros_in() 835 | } 836 | } 837 | -------------------------------------------------------------------------------- /src/set.rs: -------------------------------------------------------------------------------- 1 | use core::cmp; 2 | use core::fmt; 3 | use core::hash::{Hash, Hasher}; 4 | use core::marker::PhantomData; 5 | use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Sub, SubAssign}; 6 | 7 | use crate::bits::Bits; 8 | use crate::bits_mut::BitsMut; 9 | use crate::bits_owned::BitsOwned; 10 | use crate::endian::{DefaultEndian, Endian, LittleEndian}; 11 | 12 | /// Convenience wrapper around bitsets providing the wrapped type with behaviors 13 | /// you'd expected out of a set-like container. 14 | /// 15 | ///
16 | /// 17 | /// ## Persistent endianness 18 | /// 19 | /// A set can be constructed with a custom endianness that it remembers, so that 20 | /// methods such as [`Bits::iter_ones`] uses the endianness in the type. 21 | /// 22 | /// ``` 23 | /// use bittle::{Bits, Set, LittleEndian}; 24 | /// 25 | /// let set: Set = bittle::set![1, 12]; 26 | /// assert!(set.iter_ones().eq([1, 12])); 27 | /// assert_eq!(set.into_bits(), 0b00010000_00000010); 28 | /// 29 | /// let set: Set = bittle::set![1, 12]; 30 | /// assert!(set.iter_ones().eq([1, 12])); 31 | /// assert_eq!(set.into_bits(), 0b01000000_00001000); 32 | /// ``` 33 | /// 34 | ///
35 | /// 36 | /// ## Debugging 37 | /// 38 | /// A reason one might want to use this wrapper is to have a [Debug][fmt::Debug] 39 | /// implementation which shows which bits are actually in use: 40 | /// 41 | /// ``` 42 | /// use bittle::{Bits, Set, LittleEndian}; 43 | /// 44 | /// let set: u128 = bittle::set![1, 14]; 45 | /// assert_eq!(format!("{set:?}"), "16386"); 46 | /// 47 | /// let set: Set = Set::new(set); 48 | /// assert_eq!(format!("{set:?}"), "{1, 14}"); 49 | /// 50 | /// let set: u128 = bittle::set_le![1, 14]; 51 | /// assert_eq!(format!("{set:?}"), "85080976323951685521100712850600493056"); 52 | /// 53 | /// let set: Set = Set::new_le(set); 54 | /// assert_eq!(format!("{set:?}"), "{1, 14}"); 55 | /// ``` 56 | /// 57 | ///
58 | /// 59 | /// ## Standard iteration 60 | /// 61 | /// This wrapper provides unambigious implementations of [`IntoIterator`] which 62 | /// delegates to [`Bits::iter_ones`], avoiding potential confusion when using an 63 | /// array as a set: 64 | /// 65 | /// ``` 66 | /// use bittle::Set; 67 | /// 68 | /// let array = [0b00001000u8, 0b0u8]; 69 | /// assert!(array.into_iter().eq([8, 0])); 70 | /// 71 | /// let set = Set::new(array); 72 | /// assert!(set.into_iter().eq([3])); 73 | /// ``` 74 | /// 75 | ///
76 | /// 77 | /// ## Standard operators 78 | /// 79 | /// Wrapping into a [Set] also provides us with [`BitOr`], [`BitAnd`], 80 | /// [`BitXor`], [`Sub`], and [`BitOrAssign`], [`BitAndAssign`], 81 | /// [`BitXorAssign`], [`SubAssign`] implementations to work with. They each 82 | /// refer to [`BitsOwned::union`], [`BitsOwned::conjunction`], 83 | /// [`BitsOwned::symmetric_difference`], and [`BitsOwned::difference`] 84 | /// respectively. 85 | /// 86 | /// ``` 87 | /// use bittle::{Bits, Set}; 88 | /// 89 | /// let set1: Set<[u32; 4]> = bittle::set![1, 65]; 90 | /// let set2: Set<[u32; 4]> = bittle::set![65, 110]; 91 | /// 92 | /// assert!((set1 | set2).iter_ones().eq([1, 65, 110])); 93 | /// assert!((set1 & set2).iter_ones().eq([65])); 94 | /// assert!((set1 ^ set2).iter_ones().eq([1, 110])); 95 | /// assert!((set1 - set2).iter_ones().eq([1])); 96 | /// 97 | /// let mut set3 = set1.clone(); 98 | /// set3 |= &set2; 99 | /// assert!(set3.iter_ones().eq([1, 65, 110])); 100 | /// 101 | /// let mut set3 = set1.clone(); 102 | /// set3 &= &set2; 103 | /// assert!(set3.iter_ones().eq([65])); 104 | /// 105 | /// let mut set3 = set1.clone(); 106 | /// set3 ^= &set2; 107 | /// assert!(set3.iter_ones().eq([1, 110])); 108 | /// 109 | /// let mut set3 = set1.clone(); 110 | /// set3 -= &set2; 111 | /// assert!(set3.iter_ones().eq([1])); 112 | /// ``` 113 | /// 114 | ///
115 | /// 116 | /// ## Equality and comparison 117 | /// 118 | /// It also ensures that for operations that can be, are generic over bitwise 119 | /// indexes, allowing different kinds of bitsets to be compared: 120 | /// 121 | /// ``` 122 | /// use bittle::Set; 123 | /// let a = 0b00000000_00000000_00000001_00010000u32; 124 | /// let b = 0b00000001_00010000u16; 125 | /// let c = vec![0b00010000u8, 0b00000001u8]; 126 | /// 127 | /// assert_eq!(Set::new(a), Set::new(b)); 128 | /// assert_eq!(Set::new(a), Set::from_ref(&c[..])); 129 | /// 130 | /// let d = 0b00000001_11111111u16; 131 | /// assert!(Set::new(d) < Set::new(a)); 132 | /// assert!(Set::new(d) < Set::new(b)); 133 | /// assert!(Set::new(d) < Set::from_ref(&c[..])); 134 | /// ``` 135 | /// 136 | /// Note that if this result seems peculiar to you, consider that a bitset is 137 | /// actually an *ordered set*, so it would mimic the behavior of 138 | /// [`BTreeSet`] where `u32` are the indexes in 139 | /// the set rather than a direct numerical comparison: 140 | /// 141 | /// ``` 142 | /// use std::collections::BTreeSet; 143 | /// let mut a = BTreeSet::new(); 144 | /// let mut b = BTreeSet::new(); 145 | /// 146 | /// a.insert(0u32); 147 | /// a.insert(1u32); 148 | /// b.insert(1u32); 149 | /// 150 | /// assert!(a < b); 151 | /// ``` 152 | /// 153 | /// [`BTreeSet`]: https://doc.rust-lang.org/std/collections/struct.BTreeSet.html 154 | /// 155 | /// # More Examples 156 | /// 157 | /// ``` 158 | /// use bittle::{Bits, BitsMut, BitsOwned, Set}; 159 | /// 160 | /// let mut a = Set::::zeros(); 161 | /// 162 | /// assert!(!a.test_bit(1)); 163 | /// a.set_bit(1); 164 | /// assert!(a.test_bit(1)); 165 | /// a.clear_bit(1); 166 | /// assert!(!a.test_bit(1)); 167 | /// ``` 168 | /// 169 | /// The bit set can also use arrays as its backing storage. 170 | /// 171 | /// ``` 172 | /// use bittle::{Bits, BitsMut, BitsOwned, Set}; 173 | /// 174 | /// let mut a = Set::<[u64; 16]>::zeros(); 175 | /// 176 | /// assert!(!a.test_bit(172)); 177 | /// a.set_bit(172); 178 | /// assert!(a.test_bit(172)); 179 | /// a.clear_bit(172); 180 | /// assert!(!a.test_bit(172)); 181 | /// ``` 182 | /// 183 | /// We can use the iterator of each set to compare bit sets of different kinds: 184 | /// 185 | /// ``` 186 | /// use bittle::{Bits, BitsMut, BitsOwned, Set}; 187 | /// 188 | /// let mut a = Set::<[u64; 2]>::zeros(); 189 | /// let mut b = Set::::zeros(); 190 | /// 191 | /// a.set_bit(111); 192 | /// assert!(!a.iter_ones().eq(b.iter_ones())); 193 | /// 194 | /// b.set_bit(111); 195 | /// assert!(a.iter_ones().eq(b.iter_ones())); 196 | /// ``` 197 | #[repr(transparent)] 198 | pub struct Set 199 | where 200 | T: ?Sized, 201 | { 202 | _shift: PhantomData, 203 | bits: T, 204 | } 205 | 206 | impl Set { 207 | /// Construct a set from its underlying bits using [`DefaultEndian`]. 208 | /// 209 | /// [`DefaultEndian`]: crate::DefaultEndian 210 | /// 211 | /// # Examples 212 | /// 213 | /// ``` 214 | /// use bittle::{Bits, Set}; 215 | /// 216 | /// let mut set = Set::new(0b00001001u32); 217 | /// assert!(set.iter_ones().eq([0, 3])); 218 | /// ``` 219 | #[inline] 220 | pub const fn new(bits: T) -> Self { 221 | Self::new_in(bits) 222 | } 223 | } 224 | 225 | impl Set { 226 | /// Construct a set from its underlying bits using [`LittleEndian`] indexing. 227 | /// 228 | /// # Examples 229 | /// 230 | /// ``` 231 | /// use bittle::{Bits, Set}; 232 | /// 233 | /// let mut set = Set::new_le(0b00001001u8); 234 | /// assert!(set.iter_ones().eq([4, 7])); 235 | /// ``` 236 | #[inline] 237 | pub const fn new_le(bits: T) -> Self { 238 | Self::new_in(bits) 239 | } 240 | } 241 | 242 | impl Set { 243 | /// Construct a set from its underlying bits using custom endianness. 244 | /// 245 | /// # Examples 246 | /// 247 | /// ``` 248 | /// use bittle::{Bits, Set, LittleEndian}; 249 | /// 250 | /// let mut set: Set = Set::new_in(0b00001001u8); 251 | /// assert!(set.iter_ones().eq([4, 7])); 252 | /// assert!(set.iter_ones_in::().eq([4, 7])); 253 | /// ``` 254 | #[inline] 255 | pub const fn new_in(bits: T) -> Self { 256 | Self { 257 | _shift: PhantomData, 258 | bits, 259 | } 260 | } 261 | 262 | /// Coerce into raw interior bits. 263 | /// 264 | /// # Examples 265 | /// 266 | /// ``` 267 | /// use bittle::{Bits, Set}; 268 | /// 269 | /// let mut set = Set::new(0b00001001u8); 270 | /// assert_eq!(set.into_bits(), 0b00001001u8); 271 | /// 272 | /// let mut set = Set::new_le(0b00001001u8); 273 | /// assert_eq!(set.into_bits(), 0b00001001u8); 274 | /// ``` 275 | #[inline] 276 | pub fn into_bits(self) -> T { 277 | self.bits 278 | } 279 | } 280 | 281 | impl Set 282 | where 283 | T: ?Sized, 284 | { 285 | /// Wrap a reference as a set using [`DefaultEndian`] indexing. 286 | /// 287 | /// # Examples 288 | /// 289 | /// ``` 290 | /// use bittle::{Bits, Set}; 291 | /// 292 | /// let mut set = Set::from_ref(&[0b00000001u8, 0b00010000u8]); 293 | /// assert!(set.iter_ones().eq([0, 12])); 294 | /// ``` 295 | #[inline] 296 | pub fn from_ref(bits: &U) -> &Self 297 | where 298 | U: ?Sized + AsRef, 299 | { 300 | // SAFETY: Reference provided as bits has the same memory layout as 301 | // &Set. 302 | unsafe { &*(bits.as_ref() as *const _ as *const _) } 303 | } 304 | 305 | /// Wrap a mutable reference as a set using [`DefaultEndian`] indexing. 306 | /// 307 | /// # Examples 308 | /// 309 | /// ``` 310 | /// use bittle::{Bits, BitsMut, Set}; 311 | /// 312 | /// let mut values = [0b00000001u8, 0b00010000u8]; 313 | /// 314 | /// let mut set = Set::from_mut(&mut values); 315 | /// assert!(set.iter_ones().eq([0, 12])); 316 | /// 317 | /// set.set_bit(4); 318 | /// assert_eq!(&values[..], &[0b00010001u8, 0b00010000u8]); 319 | /// ``` 320 | #[inline] 321 | pub fn from_mut(bits: &mut U) -> &mut Self 322 | where 323 | U: ?Sized + AsMut, 324 | { 325 | // SAFETY: Reference provided as bits has the same memory layout as &mut 326 | // Set. 327 | unsafe { &mut *(bits.as_mut() as *mut _ as *mut _) } 328 | } 329 | } 330 | 331 | impl Set 332 | where 333 | T: ?Sized, 334 | { 335 | /// Wrap a reference as a set using [`LittleEndian`] indexing. 336 | /// 337 | /// # Examples 338 | /// 339 | /// ``` 340 | /// use bittle::{Bits, Set}; 341 | /// 342 | /// let mut set = Set::from_ref_le(&[0b00000001u8, 0b00010000u8]); 343 | /// assert!(set.iter_ones_le().eq([7, 11])); 344 | /// ``` 345 | #[inline] 346 | pub fn from_ref_le(bits: &U) -> &Self 347 | where 348 | U: ?Sized + AsRef, 349 | { 350 | // SAFETY: Reference provided as bits has the same memory layout as 351 | // &Set. 352 | unsafe { &*(bits.as_ref() as *const _ as *const _) } 353 | } 354 | 355 | /// Wrap a mutable reference as a set using [`LittleEndian`] indexing. 356 | /// 357 | /// # Examples 358 | /// 359 | /// ``` 360 | /// use bittle::{Bits, BitsMut, Set, LittleEndian}; 361 | /// 362 | /// let mut values = [0b00000001u8, 0b00010000u8]; 363 | /// 364 | /// let mut set = Set::from_mut_le(&mut values); 365 | /// assert!(set.iter_ones_le().eq([7, 11])); 366 | /// 367 | /// set.set_bit(4); 368 | /// assert_eq!(&values[..], &[0b00001001u8, 0b00010000u8]); 369 | /// ``` 370 | #[inline] 371 | pub fn from_mut_le(bits: &mut U) -> &mut Self 372 | where 373 | U: ?Sized + AsMut, 374 | { 375 | // SAFETY: Reference provided as bits has the same memory layout as &mut 376 | // Set. 377 | unsafe { &mut *(bits.as_mut() as *mut _ as *mut _) } 378 | } 379 | } 380 | 381 | impl Default for Set 382 | where 383 | T: BitsOwned, 384 | E: Endian, 385 | { 386 | /// Construct a new empty set. 387 | /// 388 | /// # Examples 389 | /// 390 | /// ``` 391 | /// use bittle::{Bits, BitsMut, BitsOwned, Set}; 392 | /// 393 | /// let mut a = Set::::zeros(); 394 | /// 395 | /// assert!(a.all_zeros()); 396 | /// a.set_bit(0); 397 | /// assert!(!a.all_zeros()); 398 | /// ``` 399 | #[inline] 400 | fn default() -> Self { 401 | Self::new_in(T::ZEROS) 402 | } 403 | } 404 | 405 | impl Bits for Set 406 | where 407 | T: ?Sized + Bits, 408 | U: Endian, 409 | { 410 | type IterOnes<'a> 411 | = T::IterOnesIn<'a, U> 412 | where 413 | Self: 'a; 414 | 415 | type IterOnesIn<'a, E> 416 | = T::IterOnesIn<'a, E> 417 | where 418 | Self: 'a, 419 | E: Endian; 420 | 421 | type IterZeros<'a> 422 | = T::IterZerosIn<'a, U> 423 | where 424 | Self: 'a; 425 | 426 | type IterZerosIn<'a, E> 427 | = T::IterZerosIn<'a, E> 428 | where 429 | Self: 'a, 430 | E: Endian; 431 | 432 | #[inline] 433 | fn count_ones(&self) -> u32 { 434 | self.bits.count_ones() 435 | } 436 | 437 | #[inline] 438 | fn count_zeros(&self) -> u32 { 439 | self.bits.count_zeros() 440 | } 441 | 442 | #[inline] 443 | fn bits_capacity(&self) -> u32 { 444 | self.bits.bits_capacity() 445 | } 446 | 447 | #[inline] 448 | fn all_ones(&self) -> bool { 449 | self.bits.all_ones() 450 | } 451 | 452 | #[inline] 453 | fn all_zeros(&self) -> bool { 454 | self.bits.all_zeros() 455 | } 456 | 457 | #[inline] 458 | fn test_bit(&self, index: u32) -> bool { 459 | self.bits.test_bit_in::(index) 460 | } 461 | 462 | #[inline] 463 | fn test_bit_in(&self, index: u32) -> bool 464 | where 465 | E: Endian, 466 | { 467 | self.bits.test_bit_in::(index) 468 | } 469 | 470 | #[inline] 471 | fn iter_ones(&self) -> Self::IterOnes<'_> { 472 | self.bits.iter_ones_in() 473 | } 474 | 475 | #[inline] 476 | fn iter_ones_in(&self) -> Self::IterOnesIn<'_, E> 477 | where 478 | E: Endian, 479 | { 480 | self.bits.iter_ones_in() 481 | } 482 | 483 | #[inline] 484 | fn iter_zeros(&self) -> Self::IterZeros<'_> { 485 | self.bits.iter_zeros_in() 486 | } 487 | 488 | #[inline] 489 | fn iter_zeros_in(&self) -> Self::IterZerosIn<'_, E> 490 | where 491 | E: Endian, 492 | { 493 | self.bits.iter_zeros_in() 494 | } 495 | } 496 | 497 | impl BitsMut for Set 498 | where 499 | T: ?Sized + BitsMut, 500 | U: Endian, 501 | { 502 | #[inline] 503 | fn set_bit_in(&mut self, index: u32) 504 | where 505 | E: Endian, 506 | { 507 | self.bits.set_bit_in::(index); 508 | } 509 | 510 | #[inline] 511 | fn set_bit(&mut self, index: u32) { 512 | self.bits.set_bit_in::(index); 513 | } 514 | 515 | #[inline] 516 | fn clear_bit_in(&mut self, index: u32) 517 | where 518 | E: Endian, 519 | { 520 | self.bits.clear_bit_in::(index); 521 | } 522 | 523 | #[inline] 524 | fn clear_bit(&mut self, index: u32) { 525 | self.bits.clear_bit_in::(index); 526 | } 527 | 528 | #[inline] 529 | fn clear_bits(&mut self) { 530 | self.bits.clear_bits(); 531 | } 532 | 533 | #[inline] 534 | fn union_assign(&mut self, other: &Self) { 535 | self.bits.union_assign(&other.bits); 536 | } 537 | 538 | #[inline] 539 | fn conjunction_assign(&mut self, other: &Self) { 540 | self.bits.conjunction_assign(&other.bits); 541 | } 542 | 543 | #[inline] 544 | fn difference_assign(&mut self, other: &Self) { 545 | self.bits.difference_assign(&other.bits); 546 | } 547 | 548 | #[inline] 549 | fn symmetric_difference_assign(&mut self, other: &Self) { 550 | self.bits.symmetric_difference_assign(&other.bits); 551 | } 552 | } 553 | 554 | impl BitsOwned for Set 555 | where 556 | T: BitsOwned, 557 | U: Endian, 558 | { 559 | const BITS: u32 = T::BITS; 560 | const ONES: Self = Self::new_in(T::ONES); 561 | const ZEROS: Self = Self::new_in(T::ZEROS); 562 | 563 | type IntoIterOnes = T::IntoIterOnesIn; 564 | type IntoIterOnesIn 565 | = T::IntoIterOnesIn 566 | where 567 | E: Endian; 568 | type IntoIterZeros = T::IntoIterZerosIn; 569 | type IntoIterZerosIn 570 | = T::IntoIterZerosIn 571 | where 572 | E: Endian; 573 | 574 | #[inline] 575 | fn zeros() -> Self { 576 | Self::new_in(T::ZEROS) 577 | } 578 | 579 | #[inline] 580 | fn ones() -> Self { 581 | Self::new_in(T::ONES) 582 | } 583 | 584 | #[inline] 585 | fn with_bit_in(self, bit: u32) -> Self 586 | where 587 | E: Endian, 588 | { 589 | Self::new_in(self.bits.with_bit_in::(bit)) 590 | } 591 | 592 | #[inline] 593 | fn with_bit(self, bit: u32) -> Self { 594 | Self::new_in(self.bits.with_bit_in::(bit)) 595 | } 596 | 597 | #[inline] 598 | fn without_bit_in(self, bit: u32) -> Self 599 | where 600 | E: Endian, 601 | { 602 | Self::new_in(self.bits.without_bit_in::(bit)) 603 | } 604 | 605 | #[inline] 606 | fn without_bit(self, bit: u32) -> Self { 607 | Self::new_in(self.bits.without_bit_in::(bit)) 608 | } 609 | 610 | #[inline] 611 | fn union(self, other: Self) -> Self { 612 | Self::new_in(self.bits.union(other.bits)) 613 | } 614 | 615 | #[inline] 616 | fn conjunction(self, other: Self) -> Self { 617 | Self::new_in(self.bits.conjunction(other.bits)) 618 | } 619 | 620 | #[inline] 621 | fn difference(self, other: Self) -> Self { 622 | Self::new_in(self.bits.difference(other.bits)) 623 | } 624 | 625 | #[inline] 626 | fn symmetric_difference(self, other: Self) -> Self { 627 | Self::new_in(self.bits.symmetric_difference(other.bits)) 628 | } 629 | 630 | #[inline] 631 | fn into_iter_ones(self) -> Self::IntoIterOnes { 632 | self.bits.into_iter_ones_in() 633 | } 634 | 635 | #[inline] 636 | fn into_iter_ones_in(self) -> Self::IntoIterOnesIn 637 | where 638 | E: Endian, 639 | { 640 | self.bits.into_iter_ones_in() 641 | } 642 | 643 | #[inline] 644 | fn into_iter_zeros(self) -> Self::IntoIterZeros { 645 | self.bits.into_iter_zeros_in() 646 | } 647 | 648 | #[inline] 649 | fn into_iter_zeros_in(self) -> Self::IntoIterZerosIn 650 | where 651 | E: Endian, 652 | { 653 | self.bits.into_iter_zeros_in() 654 | } 655 | } 656 | 657 | impl Clone for Set 658 | where 659 | T: Clone, 660 | { 661 | #[inline] 662 | fn clone(&self) -> Self { 663 | Self::new_in(self.bits.clone()) 664 | } 665 | } 666 | 667 | impl Copy for Set where T: Copy {} 668 | 669 | impl fmt::Debug for Set 670 | where 671 | T: ?Sized + Bits, 672 | E: Endian, 673 | { 674 | #[inline] 675 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 676 | f.debug_set().entries(self.iter_ones_in::()).finish() 677 | } 678 | } 679 | 680 | /// Flexible [`PartialEq`] implementation which allows for comparisons between different kinds of sets. 681 | /// 682 | /// # Examples 683 | /// 684 | /// ``` 685 | /// use bittle::Set; 686 | /// 687 | /// let a = Set::new(0b00001000u8); 688 | /// let b = Set::new(0b00000000_00001000u16); 689 | /// let c = Set::new([0b00001000u8, 0]); 690 | /// 691 | /// assert_eq!(a, b); 692 | /// assert_eq!(a, c); 693 | /// ``` 694 | impl cmp::PartialEq for Set 695 | where 696 | T: ?Sized + Bits, 697 | U: ?Sized + Bits, 698 | E: Endian, 699 | { 700 | #[inline] 701 | fn eq(&self, other: &U) -> bool { 702 | self.iter_ones_in::().eq(other.iter_ones_in::()) 703 | } 704 | } 705 | 706 | impl cmp::Eq for Set 707 | where 708 | T: ?Sized + Bits, 709 | E: Endian, 710 | { 711 | } 712 | 713 | impl cmp::PartialOrd for Set 714 | where 715 | T: ?Sized + Bits, 716 | U: ?Sized + Bits, 717 | E: Endian, 718 | { 719 | #[inline] 720 | fn partial_cmp(&self, other: &U) -> Option { 721 | self.iter_ones_in::() 722 | .partial_cmp(other.iter_ones_in::()) 723 | } 724 | } 725 | 726 | impl cmp::Ord for Set 727 | where 728 | T: ?Sized + Bits, 729 | E: Endian, 730 | { 731 | #[inline] 732 | fn cmp(&self, other: &Self) -> cmp::Ordering { 733 | self.iter_ones_in::().cmp(other.iter_ones_in::()) 734 | } 735 | } 736 | 737 | impl Hash for Set 738 | where 739 | T: ?Sized + Hash, 740 | { 741 | #[inline] 742 | fn hash(&self, state: &mut H) { 743 | self.bits.hash(state); 744 | } 745 | } 746 | 747 | impl IntoIterator for Set 748 | where 749 | T: BitsOwned, 750 | E: Endian, 751 | { 752 | type IntoIter = T::IntoIterOnesIn; 753 | type Item = ::Item; 754 | 755 | #[inline] 756 | fn into_iter(self) -> Self::IntoIter { 757 | self.bits.into_iter_ones_in() 758 | } 759 | } 760 | 761 | impl<'a, T, E> IntoIterator for &'a Set 762 | where 763 | T: ?Sized + Bits, 764 | E: Endian, 765 | { 766 | type IntoIter = T::IterOnesIn<'a, E>; 767 | type Item = ::Item; 768 | 769 | #[inline] 770 | fn into_iter(self) -> Self::IntoIter { 771 | self.iter_ones_in::() 772 | } 773 | } 774 | 775 | impl From for Set 776 | where 777 | T: BitsOwned, 778 | E: Endian, 779 | { 780 | #[inline] 781 | fn from(bits: T) -> Self { 782 | Self::new_in(bits) 783 | } 784 | } 785 | 786 | macro_rules! owned_ops { 787 | ($trait:ident::$n:ident, $name:ident<$t:ident, $s:ident>, $fn:ident) => { 788 | impl<$t, $s> $trait<$name<$t, $s>> for $name<$t, $s> 789 | where 790 | $t: BitsOwned, 791 | $s: Endian, 792 | { 793 | type Output = $name<$t, $s>; 794 | 795 | #[inline] 796 | fn $n(self, rhs: $name<$t, $s>) -> Self::Output { 797 | $name::new_in(self.bits.$fn(rhs.bits)) 798 | } 799 | } 800 | 801 | impl<$t, $s> $trait<&$name<$t, $s>> for $name<$t, $s> 802 | where 803 | $t: Copy + BitsOwned, 804 | $s: Endian, 805 | { 806 | type Output = $name<$t, $s>; 807 | 808 | #[inline] 809 | fn $n(self, rhs: &$name<$t, $s>) -> Self::Output { 810 | $name::new_in(self.bits.$fn(rhs.bits)) 811 | } 812 | } 813 | 814 | impl<$t, $s> $trait<$name<$t, $s>> for &$name<$t, $s> 815 | where 816 | $t: Copy + BitsOwned, 817 | $s: Endian, 818 | { 819 | type Output = $name<$t, $s>; 820 | 821 | #[inline] 822 | fn $n(self, rhs: $name<$t, $s>) -> Self::Output { 823 | $name::new_in(self.bits.$fn(rhs.bits)) 824 | } 825 | } 826 | 827 | impl<$t, $s> $trait<&$name<$t, $s>> for &$name<$t, $s> 828 | where 829 | $t: Copy + BitsOwned, 830 | $s: Endian, 831 | { 832 | type Output = $name<$t, $s>; 833 | 834 | #[inline] 835 | fn $n(self, rhs: &$name<$t, $s>) -> Self::Output { 836 | $name::new_in(self.bits.$fn(rhs.bits)) 837 | } 838 | } 839 | }; 840 | } 841 | 842 | macro_rules! assign_ops { 843 | ($trait:ident::$n:ident, $name:ident<$t:ident, $s:ident>, $fn:ident) => { 844 | impl<$t, $s> $trait<$name<$t, $s>> for $name<$t, $s> 845 | where 846 | $t: BitsMut, 847 | { 848 | #[inline] 849 | fn $n(&mut self, rhs: $name<$t, $s>) { 850 | self.bits.$fn(&rhs.bits); 851 | } 852 | } 853 | 854 | impl<$t, $s> $trait<&$name<$t, $s>> for $name<$t, $s> 855 | where 856 | $t: BitsMut, 857 | { 858 | #[inline] 859 | fn $n(&mut self, rhs: &$name<$t, $s>) { 860 | self.bits.$fn(&rhs.bits); 861 | } 862 | } 863 | 864 | impl<$t, $s> $trait<&$name<$t, $s>> for &mut $name<$t, $s> 865 | where 866 | $t: BitsMut, 867 | { 868 | #[inline] 869 | fn $n(&mut self, rhs: &$name<$t, $s>) { 870 | self.bits.$fn(&rhs.bits); 871 | } 872 | } 873 | }; 874 | } 875 | 876 | owned_ops!(BitOr::bitor, Set, union); 877 | assign_ops!(BitOrAssign::bitor_assign, Set, union_assign); 878 | owned_ops!(BitAnd::bitand, Set, conjunction); 879 | assign_ops!(BitAndAssign::bitand_assign, Set, conjunction_assign); 880 | owned_ops!(BitXor::bitxor, Set, symmetric_difference); 881 | assign_ops!( 882 | BitXorAssign::bitxor_assign, 883 | Set, 884 | symmetric_difference_assign 885 | ); 886 | owned_ops!(Sub::sub, Set, difference); 887 | assign_ops!(SubAssign::sub_assign, Set, difference_assign); 888 | --------------------------------------------------------------------------------