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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#![deny(missing_docs)]
//! A set for enum variants
//!
//! It is implemented as a wrapper over the `bit-set` crate,
//! which provides a set for integers values. We use the `EnumLike` trait
//! from the `enum_like` crate which allows for a conversion between enum
//! variant and integer value.
//!
//! Since an `EnumSet` is a wrapper over a `BitSet`, and a `BitSet` is
//! a wrapper over a `BitVec`, looking at the assembly generated by this crate
//! should be interesting.
extern crate enum_like;
extern crate bit_set;

use enum_like::EnumLike;
use bit_set::BitSet;
use std::marker::PhantomData;
use std::iter::FromIterator;
use std::fmt;

/// A `BitSet` indexed by an `EnumLike` type.
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct EnumSet<E: EnumLike> {
    inner: BitSet,
    _phantom: PhantomData<E>,
}

impl<E: EnumLike> EnumSet<E> {
    /// Returns the inner `BitSet`.
    pub fn into_bit_set(self) -> BitSet {
        self.inner
    }

    /// Returns a reference to the inner `BitSet`.
    pub fn get_ref(&self) -> &BitSet {
        &self.inner
    }

    /// Constructs an `EnumSet` from a `BitSet`.
    pub fn from_bit_set(inner: BitSet) -> Self {
        Self {
            inner,
            _phantom: PhantomData,
        }
    }

    /// Creates a new `EnumSet`.
    pub fn new() -> Self {
        Self {
            //inner: BitSet::with_capacity(E::NUM_VARIANTS),
            inner: BitSet::new(),
            _phantom: PhantomData,
        }
    }

    /// Attemps to minimalize the memory usage of the inner `BitSet`.
    pub fn shrink_to_fit(&mut self) {
        self.inner.shrink_to_fit()
    }

    /// Iterator over each element in the set.
    pub fn iter(&self) -> WrapIter<E, bit_set::Iter<'_, u32>> {
        WrapIter::<E, _>::new(self.inner.iter())
    }

    /*
    // Alternative not using WrapIter
    /// Iterator over each element in the set
    pub fn iter<'a>(&'a self) -> impl Iterator<Item=E> + 'a {
        self.inner.iter().map(|x| E::from_discr(x))
    }
    */

    /// Iterator over each element in `set || other`.
    pub fn union<'a>(&'a self, other: &'a Self) -> Union<'a, E> {
        WrapIter::<E, _>::new(self.inner.union(&other.inner))
    }

    /// Iterator over each element in `set && other`.
    pub fn intersection<'a>(&'a self, other: &'a Self) -> Intersection<'a, E> {
        WrapIter::<E, _>::new(self.inner.intersection(&other.inner))
    }

    /// Iterator over each element in `set - other`.
    pub fn difference<'a>(&'a self, other: &'a Self) -> Difference<'a, E> {
        WrapIter::<E, _>::new(self.inner.difference(&other.inner))
    }

    /// Iterator over each element in `set ^ other`.
    pub fn symmetric_difference<'a>(&'a self, other: &'a Self) -> SymmetricDifference<'a, E> {
        WrapIter::<E, _>::new(self.inner.symmetric_difference(&other.inner))
    }

    /// Computes the union with the other set, in-place.
    pub fn union_with(&mut self, other: &Self) {
        self.inner.union_with(&other.inner)
    }

    /// Computes the intersection with the other set, in-place.
    pub fn intersect_with(&mut self, other: &Self) {
        self.inner.intersect_with(&other.inner)
    }

    /// Computes the difference with the other set, in-place.
    pub fn difference_with(&mut self, other: &Self) {
        self.inner.difference_with(&other.inner)
    }

    /// Computes the symmetric difference with the other set, in-place.
    pub fn symmetric_difference_with(&mut self, other: &Self) {
        self.inner.symmetric_difference_with(&other.inner)
    }

    /// Returns the number of elements in the set.
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    /// Returns `true` if there are no elements in the set.
    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    /// Clears all elements in the set.
    pub fn clear(&mut self) {
        self.inner.clear()
    }

    /// Returns `true` if the set contains this value.
    pub fn contains(&self, value: E) -> bool {
        let d = value.to_discr();
        self.inner.contains(d)
    }

    /// Returns `true` if the set has no elements in common with `other`.
    pub fn is_disjoint(&self, other: &Self) -> bool {
        self.inner.is_disjoint(&other.inner)
    }

    /// Returns `true` if the set has no elements in common with `other`.
    pub fn is_subset(&self, other: &Self) -> bool {
        self.inner.is_subset(&other.inner)
    }

    /// Returns `true` if the set has no elements in common with `other`.
    pub fn is_superset(&self, other: &Self) -> bool {
        self.inner.is_superset(&other.inner)
    }

    /// Returns `true` if the value was not already present in the set
    pub fn insert(&mut self, value: E) -> bool {
        let d = value.to_discr();
        self.inner.insert(d)
    }

    /// Returns `true` if the value was already present in the set
    pub fn remove(&mut self, value: E) -> bool {
        let d = value.to_discr();
        self.inner.remove(d)
    }
}

impl<E: EnumLike> Default for EnumSet<E> {
    fn default() -> Self {
        Self::new()
    }
}

impl<E: EnumLike> FromIterator<E> for EnumSet<E> {
    fn from_iter<I: IntoIterator<Item = E>>(iter: I) -> Self {
        let mut ret = Self::default();
        ret.extend(iter);
        ret
    }
}

impl<E: EnumLike> Extend<E> for EnumSet<E> {
    fn extend<I: IntoIterator<Item = E>>(&mut self, iter: I) {
        for i in iter {
            self.insert(i);
        }
    }
}

impl<E: EnumLike + fmt::Debug> fmt::Debug for EnumSet<E> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_set().entries(self.iter()).finish()
    }
}

// Iterators

/// Wraps an iterator from the `bit-set` crate, mapping the output from
/// `usize` to `E: EnumLike`.
#[derive(Debug)]
pub struct WrapIter<E: EnumLike, I: Iterator<Item=usize>> {
    inner: I,
    _phantom: PhantomData<E>,
}

/// Iterator over the `&EnumSet`
pub type Iter<'a, E> = WrapIter<E, bit_set::Iter<'a, u32>>;
/// Iterator over the `&EnumSet`
pub type Union<'a, E> = WrapIter<E, bit_set::Union<'a, u32>>;
/// Iterator over the `&EnumSet`
pub type Intersection<'a, E> = WrapIter<E, bit_set::Intersection<'a, u32>>;
/// Iterator over the `&EnumSet`
pub type Difference<'a, E> = WrapIter<E, bit_set::Difference<'a, u32>>;
/// Iterator over the `&EnumSet`
pub type SymmetricDifference<'a, E> = WrapIter<E, bit_set::SymmetricDifference<'a, u32>>;

impl<E: EnumLike, I: Iterator<Item=usize>> WrapIter<E, I> {
    fn new(inner: I) -> Self {
        Self { inner, _phantom: PhantomData }
    }
}

impl<E: EnumLike, I: Iterator<Item=usize>> Iterator for WrapIter<E, I> {
    type Item = E;
    fn next(&mut self) -> Option<E> {
        self.inner.next().map(|x| E::from_discr(x))
    }
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.inner.size_hint()
    }
}

impl<'a, E: EnumLike> IntoIterator for &'a EnumSet<E> {
    type Item = E;
    type IntoIter = WrapIter<E, bit_set::Iter<'a, u32>>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}


/*
/// Make all the other methods of `BitVec` available without having to
/// rewrite them.
/// Now that I think about it, this may be a bad idea.
impl<E: EnumLike> Deref for EnumSet<E> {
    type Target = BitSet;
    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl<E: EnumLike> DerefMut for EnumSet<E> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}
*/

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
    enum ABC {
        A,
        B,
        C,
    }

    unsafe impl EnumLike for ABC {
        const NUM_VARIANTS: usize = 3;
        fn to_discr(self) -> usize {
            //self as usize
            // ^this may not work if the enum has variants with values, like A = 100:
            // so instead, we do the long form:
            match self {
                ABC::A => 0,
                ABC::B => 1,
                ABC::C => 2,
            }
        }
        fn from_discr(x: usize) -> Self {
            match x {
                0 => ABC::A,
                1 => ABC::B,
                2 => ABC::C,
                _ => panic!("Enum ABC has no variant {}", x),
            }
        }
    }

    #[test]
    fn create() {
        let mut e = EnumSet::new();
        assert_eq!(e.contains(ABC::A), false);
        assert_eq!(e.insert(ABC::A), true);
        assert_eq!(e.insert(ABC::A), false);
        assert_eq!(e.contains(ABC::A), true);
        assert_eq!(e.remove(ABC::A), true);
        assert_eq!(e.remove(ABC::A), false);
        assert_eq!(e.contains(ABC::A), false);
    }

    // As for now, this crate is assumed to work because of its simplicity.
}