1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use crate::uint::Unsigned;
use crate::*;
use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Sub, SubAssign};

/// A set of values of type `T`, implemented using a bitmap.
///
/// # Example
///
/// ```
/// use cantor::{Finite, Set, BitmapSet};
///
/// #[derive(Finite, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
/// enum MyType {
///     A,
///     B(bool),
///     C(bool, bool)
/// }
///
/// let mut set = BitmapSet::none();
/// set.include(MyType::A);
/// set.include(MyType::B(false));
/// set.include(MyType::C(true, false));
/// assert_eq!(set.size(), 3);
/// assert!(set.contains(MyType::B(false)));
/// assert!(!set.contains(MyType::C(false, true)));
/// ```
pub struct BitmapSet<T: BitmapFinite>(T::Bitmap);

/// The trait required to use [`BitmapSet`] with a type.
///
/// This is automatically implemented on concrete types that derive [`Finite`]. It can also be
/// implemented on a particular concrete type using [`impl_concrete_finite`].
#[doc(hidden)]
#[allow(clippy::missing_safety_doc)] // Should never be manually implemented.
pub unsafe trait BitmapFinite: Finite {
    #[allow(missing_docs)]
    type Bitmap: Unsigned;
}

impl<T: BitmapFinite> BitmapSet<T> {
    /// Constructs a new [`BitmapSet`] with initial membership determined using the given function.
    ///
    /// # Example
    /// ```
    /// use cantor::{Finite, Set, BitmapSet};
    /// let set = BitmapSet::new(|x: bool| !x);
    /// assert_eq!(set.size(), 1);
    /// assert!(set.contains(false));
    /// ```
    pub fn new(mut f: impl FnMut(T) -> bool) -> Self {
        let mut bitmap = T::Bitmap::ZERO;
        for i in 0..T::COUNT {
            if f(unsafe { T::nth(i).unwrap_unchecked() }) {
                bitmap = bitmap | T::Bitmap::one_at(i);
            }
        }
        Self(bitmap)
    }

    /// The set of all possible values of `T`.
    pub fn all() -> Self {
        BitmapSet(T::Bitmap::ones(T::COUNT))
    }

    /// The empty set.
    pub fn none() -> Self {
        BitmapSet(T::Bitmap::ZERO)
    }

    /// The set consisting of only the given value.
    pub fn only(value: T) -> Self {
        BitmapSet(T::Bitmap::one_at(T::index_of(value)))
    }

    /// The number of values in this set.
    pub fn size(&self) -> usize {
        T::Bitmap::count_ones(self.0)
    }

    /// Determines whether this is the empty set.
    pub fn is_none(&self) -> bool {
        self.0 == T::Bitmap::ZERO
    }
}

impl<T: BitmapFinite> Default for BitmapSet<T> {
    fn default() -> Self {
        Self::none()
    }
}

/// A set of values of type `T`.
pub trait Set<T> {
    /// Determines whether the set contains the given value.
    fn contains(&self, value: T) -> bool;

    /// Ensures that the set includes the given value.
    fn include(&mut self, value: T);

    /// Ensures that the set excludes the given value.
    fn exclude(&mut self, value: T);
}

impl<T: BitmapFinite> Set<T> for BitmapSet<T> {
    fn contains(&self, value: T) -> bool {
        self.0 & T::Bitmap::one_at(T::index_of(value)) != T::Bitmap::ZERO
    }

    fn include(&mut self, value: T) {
        self.0 = self.0 | T::Bitmap::one_at(T::index_of(value));
    }

    fn exclude(&mut self, value: T) {
        self.0 = self.0 & !T::Bitmap::one_at(T::index_of(value));
    }
}

impl<T: CompressFinite + BitmapFinite> Set<Compress<T>> for BitmapSet<T> {
    fn contains(&self, value: Compress<T>) -> bool {
        self.0 & T::Bitmap::one_at(Compress::index_of(value)) != T::Bitmap::ZERO
    }

    fn include(&mut self, value: Compress<T>) {
        self.0 = self.0 | T::Bitmap::one_at(Compress::index_of(value));
    }

    fn exclude(&mut self, value: Compress<T>) {
        self.0 = self.0 & !T::Bitmap::one_at(Compress::index_of(value));
    }
}

impl<T: BitmapFinite> Iterator for BitmapSet<T> {
    type Item = T;
    fn next(&mut self) -> Option<Self::Item> {
        if let Some(index) = self.0.first_one() {
            self.0 = self.0 & !T::Bitmap::one_at(index);
            Some(unsafe { T::nth(index).unwrap_unchecked() })
        } else {
            None
        }
    }
}

impl<T: BitmapFinite> DoubleEndedIterator for BitmapSet<T> {
    fn next_back(&mut self) -> Option<Self::Item> {
        if let Some(index) = self.0.last_one() {
            self.0 = self.0 & !T::Bitmap::one_at(index);
            Some(unsafe { T::nth(index).unwrap_unchecked() })
        } else {
            None
        }
    }
}

impl<T: BitmapFinite> BitAnd<BitmapSet<T>> for BitmapSet<T> {
    type Output = BitmapSet<T>;
    fn bitand(self, rhs: BitmapSet<T>) -> Self::Output {
        BitmapSet(self.0 & rhs.0)
    }
}

impl<T: BitmapFinite> BitOr<BitmapSet<T>> for BitmapSet<T> {
    type Output = BitmapSet<T>;
    fn bitor(self, rhs: BitmapSet<T>) -> Self::Output {
        BitmapSet(self.0 | rhs.0)
    }
}

impl<T: BitmapFinite> BitXor<BitmapSet<T>> for BitmapSet<T> {
    type Output = BitmapSet<T>;
    fn bitxor(self, rhs: BitmapSet<T>) -> Self::Output {
        BitmapSet(self.0 ^ rhs.0)
    }
}

impl<T: BitmapFinite> Sub<BitmapSet<T>> for BitmapSet<T> {
    type Output = BitmapSet<T>;
    fn sub(self, rhs: BitmapSet<T>) -> Self::Output {
        BitmapSet(self.0 & !rhs.0)
    }
}

impl<T: BitmapFinite> BitOrAssign<BitmapSet<T>> for BitmapSet<T> {
    fn bitor_assign(&mut self, rhs: BitmapSet<T>) {
        *self = *self | rhs;
    }
}

impl<T: BitmapFinite> BitAndAssign<BitmapSet<T>> for BitmapSet<T> {
    fn bitand_assign(&mut self, rhs: BitmapSet<T>) {
        *self = *self & rhs;
    }
}

impl<T: BitmapFinite> BitXorAssign<BitmapSet<T>> for BitmapSet<T> {
    fn bitxor_assign(&mut self, rhs: BitmapSet<T>) {
        *self = *self ^ rhs;
    }
}

impl<T: BitmapFinite> SubAssign<BitmapSet<T>> for BitmapSet<T> {
    fn sub_assign(&mut self, rhs: BitmapSet<T>) {
        *self = *self - rhs;
    }
}

impl<T: BitmapFinite> Clone for BitmapSet<T> {
    fn clone(&self) -> Self {
        Self(self.0)
    }
}

impl<T: BitmapFinite> Copy for BitmapSet<T> {}

impl<T: BitmapFinite> PartialEq for BitmapSet<T> {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}

impl<T: BitmapFinite> Eq for BitmapSet<T> {}

impl<T: BitmapFinite> PartialOrd for BitmapSet<T> {
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        self.0.partial_cmp(&other.0)
    }
}

impl<T: BitmapFinite> Ord for BitmapSet<T> {
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.0.cmp(&other.0)
    }
}

unsafe impl<T: BitmapFinite> Finite for BitmapSet<T> {
    const COUNT: usize = 1 << T::COUNT;

    fn index_of(value: Self) -> usize {
        value.0.to_usize()
    }

    fn nth(index: usize) -> Option<Self> {
        if index < Self::COUNT {
            Some(Self(T::Bitmap::from_usize_unchecked(index)))
        } else {
            None
        }
    }
}

impl<T: core::fmt::Debug + BitmapFinite> core::fmt::Debug for BitmapSet<T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_set().entries(*self).finish()
    }
}

#[test]
fn test_debug() {
    extern crate alloc;
    let mut set = BitmapSet::none();
    set.include(false);
    set.include(true);
    assert_eq!(alloc::format!("{:?}", set), "{false, true}");
}