default_vec2/
bit_set.rs

1use crate::default_vec::DefaultVec;
2use core::fmt::{Debug, Formatter};
3use core::iter;
4use core::marker::PhantomData;
5use core::ops::{BitAndAssign, BitOrAssign, BitXorAssign, SubAssign};
6
7type Elt = u32;
8
9/// A simple unbounded bitset that fits in 2 `usize`s worth of memory
10///
11/// It resizes its heap allocation whenever a number that wouldn't otherwise fit in memory is added
12/// and doesn't ever shrink its memory so it could end of wasting memory if a very large element
13/// is added and then removed
14#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
15pub struct BitSet<I = usize>(DefaultVec<Elt>, PhantomData<I>);
16
17impl<I> Default for BitSet<I> {
18    fn default() -> Self {
19        BitSet(DefaultVec::default(), PhantomData)
20    }
21}
22
23impl<I> Clone for BitSet<I> {
24    fn clone(&self) -> Self {
25        BitSet(self.0.clone(), PhantomData)
26    }
27
28    fn clone_from(&mut self, source: &Self) {
29        self.0.clone_from(&source.0)
30    }
31}
32
33impl<I> PartialEq<Self> for BitSet<I> {
34    fn eq(&self, other: &Self) -> bool {
35        self.0 == other.0
36    }
37}
38
39impl<I> Eq for BitSet<I> {}
40
41#[inline]
42fn split(x: usize) -> (usize, Elt, u32) {
43    let offset = (x % Elt::BITS as usize) as u32;
44    (x / Elt::BITS as usize, 1 << offset, offset)
45}
46
47impl<I: Into<usize>> BitSet<I> {
48    /// Adds an element to the set
49    ///
50    /// If the set did not have this value present, true is returned.
51    ///
52    /// If the set did have this value present, false is returned.
53    ///
54    /// ```rust
55    /// use default_vec2::BitSet;
56    /// let mut s: BitSet<usize> = BitSet::default();
57    /// assert!(s.insert(0));
58    /// assert!(!s.insert(0));
59    /// ```
60    pub fn insert(&mut self, x: I) -> bool {
61        let (chunk_idx, mask, _) = split(x.into());
62        let chunk = self.0.get_mut(chunk_idx);
63        let res = (*chunk & mask) == 0;
64        *chunk |= mask;
65        res
66    }
67
68    /// Removes an element form the set.
69    ///
70    /// Returns whether the value was present in the set.
71    ///
72    /// ```rust
73    /// use default_vec2::BitSet;
74    /// let mut s: BitSet<usize> = BitSet::default();
75    /// assert!(!s.remove(0));
76    /// s.insert(0);
77    /// assert!(s.remove(0));
78    /// assert!(!s.remove(0))
79    /// ```
80    pub fn remove(&mut self, x: I) -> bool {
81        let (chunk_idx, mask, _) = split(x.into());
82        let chunk = self.0.get_mut(chunk_idx);
83        let res = (*chunk & mask) != 0;
84        *chunk &= !mask;
85        res
86    }
87
88    /// Inserts `x` if `v` is true, or removes it otherwise.
89    ///
90    /// Returns whether `self` used to contain `x`
91    ///
92    /// ```rust
93    /// use default_vec2::BitSet;
94    /// let mut s: BitSet<usize> = BitSet::default();
95    /// assert!(!s.set(0, false));
96    /// assert!(!s.set(0, true));
97    /// assert!(s.set(0, true));
98    /// assert!(s.set(0, false));
99    /// ```
100    pub fn set(&mut self, x: I, v: bool) -> bool {
101        let (chunk_idx, mask, shift) = split(x.into());
102        let chunk = self.0.get_mut(chunk_idx);
103        let res = (*chunk & mask) != 0;
104        *chunk &= !mask;
105        *chunk |= (v as Elt) << shift;
106        res
107    }
108
109    /// Checks if the set contains an element
110    pub fn contains(&self, x: I) -> bool {
111        let (chunk_idx, mask, _) = split(x.into());
112        let chunk = self.0.get(chunk_idx);
113        (chunk & mask) != 0
114    }
115
116    /// Same as contains but already reserves space for `x`
117    pub fn contains_mut(&mut self, x: I) -> bool {
118        let (chunk_idx, mask, _) = split(x.into());
119        let chunk = *self.0.get_mut(chunk_idx);
120        (chunk & mask) != 0
121    }
122
123    /// Removes all elements from the set
124    pub fn clear(&mut self) {
125        self.0.clear()
126    }
127}
128
129impl<I: From<usize> + Into<usize> + Copy> BitSet<I> {
130    /// Iterate over all elements in the set
131    ///
132    /// Run time is proportional to the largest element that has ever been in the set
133    pub fn iter(&self) -> impl Iterator<Item = I> + '_ {
134        let max = self.0.capacity() * (Elt::BITS as usize);
135        (0..max).map(I::from).filter(|x| self.contains(*x))
136    }
137}
138
139impl<'a, I> BitAndAssign<&'a BitSet> for BitSet<I> {
140    /// Sets `*self` to the intersection of `self` and `other`
141    ///
142    /// ### Example:
143    /// ```
144    /// use default_vec2::BitSet;
145    /// let mut s1: BitSet<usize> = BitSet::from_iter([0, 1]);
146    /// let s2 = BitSet::from_iter([0, 42]);
147    /// s1 &= &s2;
148    ///
149    /// assert_eq!(vec![0], s1.iter().collect::<Vec<_>>());
150    /// ```
151    fn bitand_assign(&mut self, rhs: &'a BitSet) {
152        for (this, other) in self
153            .0
154            .iter_mut()
155            .zip(rhs.0.iter().copied().chain(iter::repeat(0)))
156        {
157            *this &= other
158        }
159    }
160}
161
162impl<'a, I> BitOrAssign<&'a BitSet> for BitSet<I> {
163    /// Sets `*self` to the intersection of `self` and `other`
164    ///
165    /// ### Example:
166    /// ```
167    /// use default_vec2::BitSet;
168    /// let mut s1: BitSet<usize> = BitSet::from_iter([0, 1]);
169    /// let s2 = BitSet::from_iter([0, 42]);
170    /// s1 |= &s2;
171    ///
172    /// assert_eq!(vec![0, 1, 42], s1.iter().collect::<Vec<_>>());
173    /// ```
174    fn bitor_assign(&mut self, rhs: &'a BitSet) {
175        if rhs.0.capacity() > self.0.capacity() {
176            self.0.reserve(rhs.0.capacity())
177        }
178        for (this, other) in self.0.iter_mut().zip(rhs.0.iter().copied()) {
179            *this |= other
180        }
181    }
182}
183
184impl<'a, I> SubAssign<&'a BitSet> for BitSet<I> {
185    /// Sets `*self` to the set difference of `self` and `other`
186    ///
187    /// ### Example:
188    /// ```
189    /// use default_vec2::BitSet;
190    /// let mut s1: BitSet<usize> = BitSet::from_iter([0, 1]);
191    /// let s2 = BitSet::from_iter([0, 42]);
192    /// s1 -= &s2;
193    ///
194    /// assert_eq!(vec![1], s1.iter().collect::<Vec<_>>());
195    /// ```
196    fn sub_assign(&mut self, rhs: &'a BitSet) {
197        for (this, other) in self.0.iter_mut().zip(rhs.0.iter().copied()) {
198            *this &= !other
199        }
200    }
201}
202
203impl<'a, I> BitXorAssign<&'a BitSet> for BitSet<I> {
204    /// Sets `*self` to the set symmetric difference of `self` and `other`
205    ///
206    /// ### Example:
207    /// ```
208    /// use default_vec2::BitSet;
209    /// let mut s1: BitSet<usize> = BitSet::from_iter([0, 1]);
210    /// let s2 = BitSet::from_iter([0, 42]);
211    /// s1 ^= &s2;
212    ///
213    /// assert_eq!(vec![1, 42], s1.iter().collect::<Vec<_>>());
214    /// ```
215    fn bitxor_assign(&mut self, rhs: &'a BitSet) {
216        if rhs.0.capacity() > self.0.capacity() {
217            self.0.reserve(rhs.0.capacity())
218        }
219        for (this, other) in self.0.iter_mut().zip(rhs.0.iter().copied()) {
220            *this ^= other
221        }
222    }
223}
224
225impl<I: Into<usize>> Extend<I> for BitSet<I> {
226    fn extend<T: IntoIterator<Item = I>>(&mut self, iter: T) {
227        iter.into_iter().for_each(|x| {
228            self.insert(x);
229        })
230    }
231}
232
233impl<I: Into<usize>> FromIterator<I> for BitSet<I> {
234    fn from_iter<T: IntoIterator<Item = I>>(iter: T) -> Self {
235        let mut res = Self::default();
236        res.extend(iter);
237        res
238    }
239}
240
241impl<I: From<usize> + Into<usize> + Copy + Debug> Debug for BitSet<I> {
242    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
243        f.debug_set().entries(self.iter()).finish()
244    }
245}