fixed_map/set/storage/
boolean.rs

1// Iterators are confusing if they impl `Copy`.
2#![allow(missing_copy_implementations)]
3
4use core::mem;
5
6use crate::set::SetStorage;
7
8const TRUE_BIT: u8 = 0b10;
9const FALSE_BIT: u8 = 0b01;
10
11/// [`SetStorage`] for [`bool`] types.
12///
13/// # Examples
14///
15/// ```
16/// use fixed_map::{Key, Set};
17///
18/// #[derive(Debug, Clone, Copy, PartialEq, Key)]
19/// enum MyKey {
20///     First(bool),
21///     Second,
22/// }
23///
24/// let mut a = Set::new();
25/// a.insert(MyKey::First(false));
26///
27/// assert!(!a.contains(MyKey::First(true)));
28/// assert!(a.contains(MyKey::First(false)));
29/// assert!(!a.contains(MyKey::Second));
30///
31/// assert!(a.iter().eq([MyKey::First(false)]));
32/// ```
33///
34/// Iterator over boolean set:
35///
36/// ```
37/// use fixed_map::{Key, Set};
38///
39/// #[derive(Debug, Clone, Copy, PartialEq, Key)]
40/// enum MyKey {
41///     Bool(bool),
42///     Other,
43/// }
44///
45/// let mut a = Set::new();
46/// a.insert(MyKey::Bool(true));
47/// a.insert(MyKey::Bool(false));
48///
49/// assert!(a.iter().eq([MyKey::Bool(true), MyKey::Bool(false)]));
50/// assert_eq!(a.iter().rev().collect::<Vec<_>>(), vec![MyKey::Bool(false), MyKey::Bool(true)]);
51/// ```
52#[derive(Clone, Copy, PartialEq, Eq)]
53pub struct BooleanSetStorage {
54    bits: u8,
55}
56
57/// See [`BooleanSetStorage::iter`].
58pub struct Iter {
59    bits: u8,
60}
61
62impl Clone for Iter {
63    #[inline]
64    fn clone(&self) -> Iter {
65        Iter { bits: self.bits }
66    }
67}
68
69impl Iterator for Iter {
70    type Item = bool;
71
72    #[inline]
73    fn next(&mut self) -> Option<Self::Item> {
74        if self.bits & TRUE_BIT != 0 {
75            self.bits &= !TRUE_BIT;
76            return Some(true);
77        }
78
79        if self.bits & FALSE_BIT != 0 {
80            self.bits &= !FALSE_BIT;
81            return Some(false);
82        }
83
84        None
85    }
86
87    #[inline]
88    fn size_hint(&self) -> (usize, Option<usize>) {
89        let len = self.bits.count_ones() as usize;
90        (len, Some(len))
91    }
92}
93
94impl DoubleEndedIterator for Iter {
95    #[inline]
96    fn next_back(&mut self) -> Option<Self::Item> {
97        if self.bits & FALSE_BIT != 0 {
98            self.bits &= !FALSE_BIT;
99            return Some(false);
100        }
101
102        if self.bits & TRUE_BIT != 0 {
103            self.bits &= !TRUE_BIT;
104            return Some(true);
105        }
106
107        None
108    }
109}
110
111impl ExactSizeIterator for Iter {
112    #[inline]
113    fn len(&self) -> usize {
114        self.bits.count_ones() as usize
115    }
116}
117
118impl SetStorage<bool> for BooleanSetStorage {
119    type Iter<'this> = Iter;
120    type IntoIter = Iter;
121
122    #[inline]
123    fn empty() -> Self {
124        Self { bits: 0 }
125    }
126
127    #[inline]
128    fn len(&self) -> usize {
129        usize::from(self.bits & TRUE_BIT != 0)
130            .saturating_add(usize::from(self.bits & FALSE_BIT != 0))
131    }
132
133    #[inline]
134    fn is_empty(&self) -> bool {
135        self.bits == 0
136    }
137
138    #[inline]
139    fn insert(&mut self, value: bool) -> bool {
140        let update = self.bits | to_bits(value);
141        test(mem::replace(&mut self.bits, update), value)
142    }
143
144    #[inline]
145    fn contains(&self, value: bool) -> bool {
146        test(self.bits, value)
147    }
148
149    #[inline]
150    fn remove(&mut self, value: bool) -> bool {
151        let value = to_bits(value);
152        let update = self.bits & !value;
153        mem::replace(&mut self.bits, update) != 0
154    }
155
156    #[inline]
157    fn retain<F>(&mut self, mut f: F)
158    where
159        F: FnMut(bool) -> bool,
160    {
161        if test(self.bits, true) && !f(true) {
162            self.bits &= !TRUE_BIT;
163        }
164
165        if test(self.bits, false) && !f(false) {
166            self.bits &= !FALSE_BIT;
167        }
168    }
169
170    #[inline]
171    fn clear(&mut self) {
172        self.bits = 0;
173    }
174
175    #[inline]
176    fn iter(&self) -> Self::Iter<'_> {
177        Iter { bits: self.bits }
178    }
179
180    #[inline]
181    fn into_iter(self) -> Self::IntoIter {
182        Iter { bits: self.bits }
183    }
184}
185
186#[inline]
187const fn test(bits: u8, value: bool) -> bool {
188    bits & to_bits(value) != 0
189}
190
191#[inline]
192const fn to_bits(value: bool) -> u8 {
193    if value {
194        TRUE_BIT
195    } else {
196        FALSE_BIT
197    }
198}