#[repr(transparent)]pub struct Set<T, E = DefaultEndian>where
T: ?Sized,{ /* private fields */ }
Expand description
Convenience wrapper around bitsets providing the wrapped type with behaviors you’d expected out of a set-like container.
Persistent endianness
A set can be constructed with a custom endianness that it remembers, so that
methods such as Bits::iter_ones
uses the endianness in the type.
use bittle::{Bits, Set, LittleEndian};
let set: Set<u16> = bittle::set![1, 12];
assert!(set.iter_ones().eq([1, 12]));
assert_eq!(set.into_bits(), 0b00010000_00000010);
let set: Set<u16, LittleEndian> = bittle::set![1, 12];
assert!(set.iter_ones().eq([1, 12]));
assert_eq!(set.into_bits(), 0b01000000_00001000);
Debugging
A reason one might want to use this wrapper is to have a Debug implementation which shows which bits are actually in use:
use bittle::{Bits, Set, LittleEndian};
let set: u128 = bittle::set![1, 14];
assert_eq!(format!("{set:?}"), "16386");
let set: Set<u128> = Set::new(set);
assert_eq!(format!("{set:?}"), "{1, 14}");
let set: u128 = bittle::set_le![1, 14];
assert_eq!(format!("{set:?}"), "85080976323951685521100712850600493056");
let set: Set<u128, LittleEndian> = Set::new_le(set);
assert_eq!(format!("{set:?}"), "{1, 14}");
Standard iteration
This wrapper provides unambigious implementations of IntoIterator
which
delegates to Bits::iter_ones
, avoiding potential confusion when using an
array as a set:
use bittle::Set;
let array = [0b00001000u8, 0b0u8];
assert!(array.into_iter().eq([8, 0]));
let set = Set::new(array);
assert!(set.into_iter().eq([3]));
Standard operators
Wrapping into a Set also provides us with BitOr
, BitAnd
,
BitXor
, Sub
, and BitOrAssign
, BitAndAssign
,
BitXorAssign
, SubAssign
implementations to work with. They each
refer to BitsOwned::union
, BitsOwned::conjunction
,
BitsOwned::symmetric_difference
, and BitsOwned::difference
respectively.
use bittle::{Bits, Set};
let set1: Set<[u32; 4]> = bittle::set![1, 65];
let set2: Set<[u32; 4]> = bittle::set![65, 110];
assert!((set1 | set2).iter_ones().eq([1, 65, 110]));
assert!((set1 & set2).iter_ones().eq([65]));
assert!((set1 ^ set2).iter_ones().eq([1, 110]));
assert!((set1 - set2).iter_ones().eq([1]));
let mut set3 = set1.clone();
set3 |= &set2;
assert!(set3.iter_ones().eq([1, 65, 110]));
let mut set3 = set1.clone();
set3 &= &set2;
assert!(set3.iter_ones().eq([65]));
let mut set3 = set1.clone();
set3 ^= &set2;
assert!(set3.iter_ones().eq([1, 110]));
let mut set3 = set1.clone();
set3 -= &set2;
assert!(set3.iter_ones().eq([1]));
Equality and comparison
It also ensures that for operations that can be, are generic over bitwise indexes, allowing different kinds of bitsets to be compared:
use bittle::Set;
let a = 0b00000000_00000000_00000001_00010000u32;
let b = 0b00000001_00010000u16;
let c = vec![0b00010000u8, 0b00000001u8];
assert_eq!(Set::new(a), Set::new(b));
assert_eq!(Set::new(a), Set::from_ref(&c[..]));
let d = 0b00000001_11111111u16;
assert!(Set::new(d) < Set::new(a));
assert!(Set::new(d) < Set::new(b));
assert!(Set::new(d) < Set::from_ref(&c[..]));
Note that if this result seems peculiar to you, consider that a bitset is
actually an ordered set, so it would mimic the behavior of
BTreeSet<u32>
where u32
are the indexes in
the set rather than a direct numerical comparison:
use std::collections::BTreeSet;
let mut a = BTreeSet::new();
let mut b = BTreeSet::new();
a.insert(0u32);
a.insert(1u32);
b.insert(1u32);
assert!(a < b);
More Examples
use bittle::{Bits, BitsMut, BitsOwned, Set};
let mut a = Set::<u128>::zeros();
assert!(!a.test_bit(1));
a.set_bit(1);
assert!(a.test_bit(1));
a.clear_bit(1);
assert!(!a.test_bit(1));
The bit set can also use arrays as its backing storage.
use bittle::{Bits, BitsMut, BitsOwned, Set};
let mut a = Set::<[u64; 16]>::zeros();
assert!(!a.test_bit(172));
a.set_bit(172);
assert!(a.test_bit(172));
a.clear_bit(172);
assert!(!a.test_bit(172));
We can use the iterator of each set to compare bit sets of different kinds:
use bittle::{Bits, BitsMut, BitsOwned, Set};
let mut a = Set::<[u64; 2]>::zeros();
let mut b = Set::<u128>::zeros();
a.set_bit(111);
assert!(!a.iter_ones().eq(b.iter_ones()));
b.set_bit(111);
assert!(a.iter_ones().eq(b.iter_ones()));
Implementations§
source§impl<T> Set<T>
impl<T> Set<T>
sourcepub const fn new(bits: T) -> Self
pub const fn new(bits: T) -> Self
Construct a set from its underlying bits using DefaultEndian
.
Examples
use bittle::{Bits, Set};
let mut set = Set::new(0b00001001u32);
assert!(set.iter_ones().eq([0, 3]));
source§impl<T> Set<T, LittleEndian>
impl<T> Set<T, LittleEndian>
sourcepub const fn new_le(bits: T) -> Self
pub const fn new_le(bits: T) -> Self
Construct a set from its underlying bits using LittleEndian
indexing.
Examples
use bittle::{Bits, Set};
let mut set = Set::new_le(0b00001001u8);
assert!(set.iter_ones().eq([4, 7]));
source§impl<T, E> Set<T, E>
impl<T, E> Set<T, E>
sourcepub const fn new_in(bits: T) -> Self
pub const fn new_in(bits: T) -> Self
Construct a set from its underlying bits using custom endianness.
Examples
use bittle::{Bits, Set, LittleEndian};
let mut set: Set<u8, LittleEndian> = Set::new_in(0b00001001u8);
assert!(set.iter_ones().eq([4, 7]));
assert!(set.iter_ones_in::<LittleEndian>().eq([4, 7]));
source§impl<T> Set<T>where
T: ?Sized,
impl<T> Set<T>where T: ?Sized,
sourcepub fn from_ref<U>(bits: &U) -> &Selfwhere
U: ?Sized + AsRef<T>,
pub fn from_ref<U>(bits: &U) -> &Selfwhere U: ?Sized + AsRef<T>,
Wrap a reference as a set using DefaultEndian
indexing.
Examples
use bittle::{Bits, Set};
let mut set = Set::from_ref(&[0b00000001u8, 0b00010000u8]);
assert!(set.iter_ones().eq([0, 12]));
sourcepub fn from_mut<U>(bits: &mut U) -> &mut Selfwhere
U: ?Sized + AsMut<T>,
pub fn from_mut<U>(bits: &mut U) -> &mut Selfwhere U: ?Sized + AsMut<T>,
Wrap a mutable reference as a set using DefaultEndian
indexing.
Examples
use bittle::{Bits, BitsMut, Set};
let mut values = [0b00000001u8, 0b00010000u8];
let mut set = Set::from_mut(&mut values);
assert!(set.iter_ones().eq([0, 12]));
set.set_bit(4);
assert_eq!(&values[..], &[0b00010001u8, 0b00010000u8]);
source§impl<T> Set<T, LittleEndian>where
T: ?Sized,
impl<T> Set<T, LittleEndian>where T: ?Sized,
sourcepub fn from_ref_le<U>(bits: &U) -> &Selfwhere
U: ?Sized + AsRef<T>,
pub fn from_ref_le<U>(bits: &U) -> &Selfwhere U: ?Sized + AsRef<T>,
Wrap a reference as a set using LittleEndian
indexing.
Examples
use bittle::{Bits, Set};
let mut set = Set::from_ref_le(&[0b00000001u8, 0b00010000u8]);
assert!(set.iter_ones_le().eq([7, 11]));
sourcepub fn from_mut_le<U>(bits: &mut U) -> &mut Selfwhere
U: ?Sized + AsMut<T>,
pub fn from_mut_le<U>(bits: &mut U) -> &mut Selfwhere U: ?Sized + AsMut<T>,
Wrap a mutable reference as a set using LittleEndian
indexing.
Examples
use bittle::{Bits, BitsMut, Set, LittleEndian};
let mut values = [0b00000001u8, 0b00010000u8];
let mut set = Set::from_mut_le(&mut values);
assert!(set.iter_ones_le().eq([7, 11]));
set.set_bit(4);
assert_eq!(&values[..], &[0b00001001u8, 0b00010000u8]);
Trait Implementations§
source§impl<T, E> BitAndAssign<&Set<T, E>> for &mut Set<T, E>where
T: BitsMut,
impl<T, E> BitAndAssign<&Set<T, E>> for &mut Set<T, E>where T: BitsMut,
source§fn bitand_assign(&mut self, rhs: &Set<T, E>)
fn bitand_assign(&mut self, rhs: &Set<T, E>)
&=
operation. Read moresource§impl<T, E> BitAndAssign<&Set<T, E>> for Set<T, E>where
T: BitsMut,
impl<T, E> BitAndAssign<&Set<T, E>> for Set<T, E>where T: BitsMut,
source§fn bitand_assign(&mut self, rhs: &Set<T, E>)
fn bitand_assign(&mut self, rhs: &Set<T, E>)
&=
operation. Read moresource§impl<T, E> BitAndAssign<Set<T, E>> for Set<T, E>where
T: BitsMut,
impl<T, E> BitAndAssign<Set<T, E>> for Set<T, E>where T: BitsMut,
source§fn bitand_assign(&mut self, rhs: Set<T, E>)
fn bitand_assign(&mut self, rhs: Set<T, E>)
&=
operation. Read moresource§impl<T, E> BitOrAssign<&Set<T, E>> for &mut Set<T, E>where
T: BitsMut,
impl<T, E> BitOrAssign<&Set<T, E>> for &mut Set<T, E>where T: BitsMut,
source§fn bitor_assign(&mut self, rhs: &Set<T, E>)
fn bitor_assign(&mut self, rhs: &Set<T, E>)
|=
operation. Read moresource§impl<T, E> BitOrAssign<&Set<T, E>> for Set<T, E>where
T: BitsMut,
impl<T, E> BitOrAssign<&Set<T, E>> for Set<T, E>where T: BitsMut,
source§fn bitor_assign(&mut self, rhs: &Set<T, E>)
fn bitor_assign(&mut self, rhs: &Set<T, E>)
|=
operation. Read moresource§impl<T, E> BitOrAssign<Set<T, E>> for Set<T, E>where
T: BitsMut,
impl<T, E> BitOrAssign<Set<T, E>> for Set<T, E>where T: BitsMut,
source§fn bitor_assign(&mut self, rhs: Set<T, E>)
fn bitor_assign(&mut self, rhs: Set<T, E>)
|=
operation. Read moresource§impl<T, E> BitXorAssign<&Set<T, E>> for &mut Set<T, E>where
T: BitsMut,
impl<T, E> BitXorAssign<&Set<T, E>> for &mut Set<T, E>where T: BitsMut,
source§fn bitxor_assign(&mut self, rhs: &Set<T, E>)
fn bitxor_assign(&mut self, rhs: &Set<T, E>)
^=
operation. Read moresource§impl<T, E> BitXorAssign<&Set<T, E>> for Set<T, E>where
T: BitsMut,
impl<T, E> BitXorAssign<&Set<T, E>> for Set<T, E>where T: BitsMut,
source§fn bitxor_assign(&mut self, rhs: &Set<T, E>)
fn bitxor_assign(&mut self, rhs: &Set<T, E>)
^=
operation. Read moresource§impl<T, E> BitXorAssign<Set<T, E>> for Set<T, E>where
T: BitsMut,
impl<T, E> BitXorAssign<Set<T, E>> for Set<T, E>where T: BitsMut,
source§fn bitxor_assign(&mut self, rhs: Set<T, E>)
fn bitxor_assign(&mut self, rhs: Set<T, E>)
^=
operation. Read moresource§impl<T, U> Bits for Set<T, U>where
T: ?Sized + Bits,
U: Endian,
impl<T, U> Bits for Set<T, U>where T: ?Sized + Bits, U: Endian,
§type IterOnes<'a>
where
Self: 'a = <T as Bits>::IterOnesIn<'a, U>
type IterOnes<'a> where Self: 'a = <T as Bits>::IterOnesIn<'a, U>
DefaultEndian
indexing. Read more§type IterOnesIn<'a, E>
where
Self: 'a,
E: Endian = <T as Bits>::IterOnesIn<'a, E>
type IterOnesIn<'a, E> where Self: 'a, E: Endian = <T as Bits>::IterOnesIn<'a, E>
§type IterZeros<'a>
where
Self: 'a = <T as Bits>::IterZerosIn<'a, U>
type IterZeros<'a> where Self: 'a = <T as Bits>::IterZerosIn<'a, U>
DefaultEndian
indexing. Read more§type IterZerosIn<'a, E>
where
Self: 'a,
E: Endian = <T as Bits>::IterZerosIn<'a, E>
type IterZerosIn<'a, E> where Self: 'a, E: Endian = <T as Bits>::IterZerosIn<'a, E>
source§fn count_ones(&self) -> u32
fn count_ones(&self) -> u32
source§fn count_zeros(&self) -> u32
fn count_zeros(&self) -> u32
source§fn bits_capacity(&self) -> u32
fn bits_capacity(&self) -> u32
source§fn test_bit(&self, index: u32) -> bool
fn test_bit(&self, index: u32) -> bool
DefaultEndian
indexing. Read moresource§fn iter_ones(&self) -> Self::IterOnes<'_>
fn iter_ones(&self) -> Self::IterOnes<'_>
DefaultEndian
indexing. Read moresource§fn iter_ones_in<E>(&self) -> Self::IterOnesIn<'_, E>where
E: Endian,
fn iter_ones_in<E>(&self) -> Self::IterOnesIn<'_, E>where E: Endian,
source§fn iter_zeros(&self) -> Self::IterZeros<'_>
fn iter_zeros(&self) -> Self::IterZeros<'_>
DefaultEndian
indexing. Read moresource§fn iter_zeros_in<E>(&self) -> Self::IterZerosIn<'_, E>where
E: Endian,
fn iter_zeros_in<E>(&self) -> Self::IterZerosIn<'_, E>where E: Endian,
source§fn test_bit_le(&self, index: u32) -> bool
fn test_bit_le(&self, index: u32) -> bool
LittleEndian
indexing. Read moresource§fn test_bit_be(&self, index: u32) -> bool
fn test_bit_be(&self, index: u32) -> bool
source§fn iter_ones_le(&self) -> Self::IterOnesIn<'_, LittleEndian>
fn iter_ones_le(&self) -> Self::IterOnesIn<'_, LittleEndian>
LittleEndian
indexing. Read moresource§fn iter_ones_be(&self) -> Self::IterOnesIn<'_, BigEndian>
fn iter_ones_be(&self) -> Self::IterOnesIn<'_, BigEndian>
source§fn iter_zeros_le(&self) -> Self::IterZerosIn<'_, LittleEndian>
fn iter_zeros_le(&self) -> Self::IterZerosIn<'_, LittleEndian>
LittleEndian
indexing. Read moresource§fn iter_zeros_be(&self) -> Self::IterZerosIn<'_, BigEndian>
fn iter_zeros_be(&self) -> Self::IterZerosIn<'_, BigEndian>
source§impl<T, U> BitsMut for Set<T, U>where
T: ?Sized + BitsMut,
U: Endian,
impl<T, U> BitsMut for Set<T, U>where T: ?Sized + BitsMut, U: Endian,
source§fn clear_bit_in<E>(&mut self, index: u32)where
E: Endian,
fn clear_bit_in<E>(&mut self, index: u32)where E: Endian,
source§fn clear_bits(&mut self)
fn clear_bits(&mut self)
source§fn union_assign(&mut self, other: &Self)
fn union_assign(&mut self, other: &Self)
source§fn conjunction_assign(&mut self, other: &Self)
fn conjunction_assign(&mut self, other: &Self)
source§fn difference_assign(&mut self, other: &Self)
fn difference_assign(&mut self, other: &Self)
source§fn symmetric_difference_assign(&mut self, other: &Self)
fn symmetric_difference_assign(&mut self, other: &Self)
source§fn set_bit_le(&mut self, index: u32)
fn set_bit_le(&mut self, index: u32)
LittleEndian
indexing. Read moresource§fn set_bit_be(&mut self, index: u32)
fn set_bit_be(&mut self, index: u32)
LittleEndian
indexing. Read moresource§fn clear_bit_le(&mut self, index: u32)
fn clear_bit_le(&mut self, index: u32)
LittleEndian
indexing. Read moresource§impl<T, U> BitsOwned for Set<T, U>where
T: BitsOwned,
U: Endian,
impl<T, U> BitsOwned for Set<T, U>where T: BitsOwned, U: Endian,
§type IntoIterOnes = <T as BitsOwned>::IntoIterOnesIn<U>
type IntoIterOnes = <T as BitsOwned>::IntoIterOnesIn<U>
DefaultEndian
indexing. Read more§type IntoIterOnesIn<E>
where
E: Endian = <T as BitsOwned>::IntoIterOnesIn<E>
type IntoIterOnesIn<E> where E: Endian = <T as BitsOwned>::IntoIterOnesIn<E>
§type IntoIterZeros = <T as BitsOwned>::IntoIterZerosIn<U>
type IntoIterZeros = <T as BitsOwned>::IntoIterZerosIn<U>
DefaultEndian
ordering. Read more§type IntoIterZerosIn<E>
where
E: Endian = <T as BitsOwned>::IntoIterZerosIn<E>
type IntoIterZerosIn<E> where E: Endian = <T as BitsOwned>::IntoIterZerosIn<E>
source§fn with_bit_in<E>(self, bit: u32) -> Selfwhere
E: Endian,
fn with_bit_in<E>(self, bit: u32) -> Selfwhere E: Endian,
source§fn with_bit(self, bit: u32) -> Self
fn with_bit(self, bit: u32) -> Self
DefaultEndian
indexing. Read moresource§fn without_bit_in<E>(self, bit: u32) -> Selfwhere
E: Endian,
fn without_bit_in<E>(self, bit: u32) -> Selfwhere E: Endian,
source§fn without_bit(self, bit: u32) -> Self
fn without_bit(self, bit: u32) -> Self
DefaultEndian
indexing. Read moresource§fn union(self, other: Self) -> Self
fn union(self, other: Self) -> Self
source§fn conjunction(self, other: Self) -> Self
fn conjunction(self, other: Self) -> Self
source§fn difference(self, other: Self) -> Self
fn difference(self, other: Self) -> Self
source§fn symmetric_difference(self, other: Self) -> Self
fn symmetric_difference(self, other: Self) -> Self
source§fn into_iter_ones(self) -> Self::IntoIterOnes
fn into_iter_ones(self) -> Self::IntoIterOnes
DefaultEndian
indexing. Read moresource§fn into_iter_ones_in<E>(self) -> Self::IntoIterOnesIn<E>where
E: Endian,
fn into_iter_ones_in<E>(self) -> Self::IntoIterOnesIn<E>where E: Endian,
source§fn into_iter_zeros(self) -> Self::IntoIterZeros
fn into_iter_zeros(self) -> Self::IntoIterZeros
DefaultEndian
indexing. Read moresource§fn into_iter_zeros_in<E>(self) -> Self::IntoIterZerosIn<E>where
E: Endian,
fn into_iter_zeros_in<E>(self) -> Self::IntoIterZerosIn<E>where E: Endian,
source§fn with_bit_le(self, bit: u32) -> Selfwhere
Self: Sized,
fn with_bit_le(self, bit: u32) -> Selfwhere Self: Sized,
LittleEndian
indexing. Read moresource§fn with_bit_be(self, bit: u32) -> Selfwhere
Self: Sized,
fn with_bit_be(self, bit: u32) -> Selfwhere Self: Sized,
source§fn without_bit_le(self, bit: u32) -> Selfwhere
Self: Sized,
fn without_bit_le(self, bit: u32) -> Selfwhere Self: Sized,
LittleEndian
indexing. Read moresource§fn without_bit_be(self, bit: u32) -> Selfwhere
Self: Sized,
fn without_bit_be(self, bit: u32) -> Selfwhere Self: Sized,
source§fn into_iter_ones_le(self) -> Self::IntoIterOnesIn<LittleEndian>where
Self: Sized,
fn into_iter_ones_le(self) -> Self::IntoIterOnesIn<LittleEndian>where Self: Sized,
LittleEndian
indexing. Read moresource§fn into_iter_ones_be(self) -> Self::IntoIterOnesIn<BigEndian>where
Self: Sized,
fn into_iter_ones_be(self) -> Self::IntoIterOnesIn<BigEndian>where Self: Sized,
source§fn into_iter_zeros_le(self) -> Self::IntoIterZerosIn<LittleEndian>where
Self: Sized,
fn into_iter_zeros_le(self) -> Self::IntoIterZerosIn<LittleEndian>where Self: Sized,
LittleEndian
indexing. Read moresource§fn into_iter_zeros_be(self) -> Self::IntoIterZerosIn<BigEndian>where
Self: Sized,
fn into_iter_zeros_be(self) -> Self::IntoIterZerosIn<BigEndian>where Self: Sized,
source§impl<'a, T, E> IntoIterator for &'a Set<T, E>where
T: ?Sized + Bits,
E: Endian,
impl<'a, T, E> IntoIterator for &'a Set<T, E>where T: ?Sized + Bits, E: Endian,
source§impl<T, E> IntoIterator for Set<T, E>where
T: BitsOwned,
E: Endian,
impl<T, E> IntoIterator for Set<T, E>where T: BitsOwned, E: Endian,
source§impl<T, U, E> PartialEq<U> for Set<T, E>where
T: ?Sized + Bits,
U: ?Sized + Bits,
E: Endian,
impl<T, U, E> PartialEq<U> for Set<T, E>where T: ?Sized + Bits, U: ?Sized + Bits, E: Endian,
source§impl<T, U, E> PartialOrd<U> for Set<T, E>where
T: ?Sized + Bits,
U: ?Sized + Bits,
E: Endian,
impl<T, U, E> PartialOrd<U> for Set<T, E>where T: ?Sized + Bits, U: ?Sized + Bits, E: Endian,
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<T, E> SubAssign<&Set<T, E>> for &mut Set<T, E>where
T: BitsMut,
impl<T, E> SubAssign<&Set<T, E>> for &mut Set<T, E>where T: BitsMut,
source§fn sub_assign(&mut self, rhs: &Set<T, E>)
fn sub_assign(&mut self, rhs: &Set<T, E>)
-=
operation. Read moresource§impl<T, E> SubAssign<&Set<T, E>> for Set<T, E>where
T: BitsMut,
impl<T, E> SubAssign<&Set<T, E>> for Set<T, E>where T: BitsMut,
source§fn sub_assign(&mut self, rhs: &Set<T, E>)
fn sub_assign(&mut self, rhs: &Set<T, E>)
-=
operation. Read moresource§impl<T, E> SubAssign<Set<T, E>> for Set<T, E>where
T: BitsMut,
impl<T, E> SubAssign<Set<T, E>> for Set<T, E>where T: BitsMut,
source§fn sub_assign(&mut self, rhs: Set<T, E>)
fn sub_assign(&mut self, rhs: Set<T, E>)
-=
operation. Read more