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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
//! A set for enumerations backed by an array.
use core::{fmt, marker::PhantomData};
use crate::{map, Enum, EnumMap};
/// A set implemented as a [`EnumMap`] where the value is `()`.
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct EnumSet<const LENGTH: usize, E: Enum<LENGTH>>(EnumMap<LENGTH, E, ()>);
impl<const LENGTH: usize, E: Enum<LENGTH>> EnumSet<LENGTH, E> {
/// Creates an empty `EnumSet`.
///
/// With `debug_assertions` enabled, the constructor verifies the implementation
/// of the [`Enum`] trait.
pub fn new() -> Self {
Self(EnumMap::new())
}
/// Clears the set, removing all values.
///
/// # Examples
///
/// ```
/// # enumap::enumap! { #[derive(Debug, PartialEq)] enum Fruit { Orange, Banana, Grape, } }
/// use enumap::{Enum, EnumSet};
///
/// let mut a = EnumSet::from([Fruit::Orange, Fruit::Banana]);
///
/// assert!(!a.is_empty());
/// a.clear();
/// assert!(a.is_empty());
/// ```
pub fn clear(&mut self) {
self.0.clear()
}
/// Returns true if the set contains a value.
///
/// # Examples
///
/// ```
/// # enumap::enumap! { #[derive(Debug, PartialEq)] enum Fruit { Orange, Banana, Grape, } }
/// use enumap::{Enum, EnumSet};
///
/// let a = EnumSet::from([Fruit::Orange, Fruit::Banana]);
///
/// assert!(a.contains(Fruit::Orange));
/// assert!(a.contains(Fruit::Banana));
/// assert!(!a.contains(Fruit::Grape));
/// ```
pub fn contains(&self, value: E) -> bool {
self.0.contains_key(value)
}
/// Visits the values representing the difference, i.e., the values that are in self but not in other.
///
/// # Examples
///
/// ```
/// # enumap::enumap! { #[derive(Debug, PartialEq)] enum Fruit { Orange, Banana, Grape, Apple } }
/// use enumap::{Enum, EnumSet};
///
/// let a = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Apple]);
/// let b = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Grape]);
///
/// // Can be seen as `a - b`.
/// for x in a.difference(&b) {
/// println!("{x:?}"); // Print Apple
/// }
///
/// let diff: Vec<Fruit> = a.difference(&b).collect();
/// assert_eq!(diff, vec![Fruit::Apple]);
///
/// // Note that difference is not symmetric,
/// // and `b - a` means something else:
/// let diff: Vec<Fruit> = b.difference(&a).collect();
/// assert_eq!(diff, vec![Fruit::Grape]);
/// ```
pub fn difference<'a>(&'a self, other: &'a EnumSet<LENGTH, E>) -> Difference<'a, LENGTH, E> {
Difference {
this: self.0.as_slice(),
other: other.0.as_slice(),
index: 0,
_enum: PhantomData,
}
}
/// Adds a value to the set.
///
/// Returns whether the value was newly inserted. That is:
///
/// If the set did not previously contain this value, true is returned.
/// If the set already contained this value, false is returned.
///
/// # Examples
///
/// ```
/// # enumap::enumap! { enum Fruit { Orange, Banana, Grape, } }
/// use enumap::EnumSet;
///
/// let mut set = EnumSet::new();
///
/// assert_eq!(set.insert(Fruit::Orange), true);
/// assert_eq!(set.insert(Fruit::Orange), false);
/// assert_eq!(set.len(), 1);
/// ```
pub fn insert(&mut self, value: E) -> bool {
self.0.insert(value, ()).is_none()
}
/// Visits the values representing the intersection, i.e., the values that are both in self and other.
///
/// # Examples
///
/// ```
/// # enumap::enumap! { #[derive(Debug, PartialEq)] enum Fruit { Orange, Banana, Grape, Apple } }
/// use enumap::{Enum, EnumSet};
///
/// let a = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Apple]);
/// let b = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Grape]);
///
/// // Print Orange, Banana in order.
/// for x in a.intersection(&b) {
/// println!("{x:?}");
/// }
///
/// let intersection: Vec<Fruit> = a.intersection(&b).collect();
/// assert_eq!(intersection, vec![Fruit::Orange, Fruit::Banana]);
/// ```
pub fn intersection<'a>(
&'a self,
other: &'a EnumSet<LENGTH, E>,
) -> Intersection<'a, LENGTH, E> {
Intersection {
this: self.0.as_slice(),
other: other.0.as_slice(),
index: 0,
_enum: PhantomData,
}
}
/// Returns true if self has no elements in common with other.
/// This is equivalent to checking for an empty intersection.
///
/// # Examples
///
/// ```
/// # enumap::enumap! { #[derive(Debug, PartialEq)] enum Fruit { Orange, Banana, Grape, Apple } }
/// use enumap::{Enum, EnumSet};
///
/// let a = EnumSet::from([Fruit::Orange, Fruit::Banana]);
/// let mut b = EnumSet::new();
///
/// assert_eq!(a.is_disjoint(&b), true);
/// b.insert(Fruit::Grape);
/// assert_eq!(a.is_disjoint(&b), true);
/// b.insert(Fruit::Orange);
/// assert_eq!(a.is_disjoint(&b), false);
/// ```
pub fn is_disjoint(&self, other: &EnumSet<LENGTH, E>) -> bool {
self.intersection(other).next().is_none()
}
/// Returns true if the set contains no elements.
///
/// # Examples
///
/// ```
/// # enumap::enumap! { enum Fruit { Orange, Banana, Grape, } }
/// use enumap::EnumSet;
///
/// let mut set = EnumSet::new();
/// assert!(set.is_empty());
/// set.insert(Fruit::Orange);
/// assert!(!set.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// Returns true if the set is a subset of another, i.e.,
/// other contains at least all the values in self.
///
/// # Examples
///
/// ```
/// # enumap::enumap! { #[derive(Debug, PartialEq)] enum Fruit { Orange, Banana, Grape, Apple } }
/// use enumap::{Enum, EnumSet};
///
/// let sup = EnumSet::from([Fruit::Orange, Fruit::Banana]);
/// let mut set = EnumSet::new();
///
/// assert_eq!(set.is_subset(&sup), true);
/// set.insert(Fruit::Orange);
/// assert_eq!(set.is_subset(&sup), true);
/// set.insert(Fruit::Grape);
/// assert_eq!(set.is_subset(&sup), false);
/// ```
pub fn is_subset(&self, other: &EnumSet<LENGTH, E>) -> bool {
self.difference(other).next().is_none()
}
/// Returns true if the set is a superset of another, i.e.,
/// self contains at least all the values in other.
///
/// # Examples
///
/// ```
/// # enumap::enumap! { #[derive(Debug, PartialEq)] enum Fruit { Orange, Banana, Grape, Apple } }
/// use enumap::{Enum, EnumSet};
///
/// let sub = EnumSet::from([Fruit::Orange, Fruit::Banana]);
/// let mut set = EnumSet::new();
///
/// assert_eq!(set.is_superset(&sub), false);
/// set.insert(Fruit::Orange);
/// assert_eq!(set.is_superset(&sub), false);
/// set.insert(Fruit::Banana);
/// assert_eq!(set.is_superset(&sub), true);
/// set.insert(Fruit::Grape);
/// assert_eq!(set.is_superset(&sub), true);
/// ```
pub fn is_superset(&self, other: &EnumSet<LENGTH, E>) -> bool {
other.difference(self).next().is_none()
}
/// An iterator visiting all elements in order. The iterator element type is `E`.
///
/// # Examples
///
/// ```
/// # enumap::enumap! { #[derive(Debug)] enum Fruit { Orange, Banana, Grape, } }
/// use enumap::EnumSet;
///
/// let mut set = EnumSet::from([
/// Fruit::Orange,
/// Fruit::Grape,
/// ]);
///
/// for value in set.iter() {
/// println!("{value:?}");
/// }
/// # let mut iter = set.iter();
/// # assert!(matches!(iter.next(), Some(Fruit::Orange)));
/// # assert!(matches!(iter.next(), Some(Fruit::Grape)));
/// # assert!(iter.next().is_none());
/// ```
pub fn iter(&self) -> Iter<'_, LENGTH, E> {
Iter {
inner: self.0.keys(),
}
}
/// Returns the number of elements in the set.
///
/// # Examples
///
/// ```
/// # enumap::enumap! { enum Fruit { Orange, Banana, Grape, } }
/// use enumap::EnumSet;
///
/// let mut set = EnumSet::new();
/// assert_eq!(set.len(), 0);
/// set.insert(Fruit::Grape);
/// assert_eq!(set.len(), 1);
/// ```
pub fn len(&self) -> usize {
self.0.len()
}
/// Removes a value from the set. Returns whether the value was present in the set.
///
/// # Examples
///
/// ```
/// # enumap::enumap! { enum Fruit { Orange, Banana, Grape, } }
/// use enumap::EnumSet;
///
/// let mut set = EnumSet::new();
///
/// set.insert(Fruit::Orange);
/// assert_eq!(set.remove(Fruit::Orange), true);
/// assert_eq!(set.remove(Fruit::Orange), false);
/// assert!(set.is_empty());
/// ```
pub fn remove(&mut self, value: E) -> bool {
self.0.remove(value).is_some()
}
/// Visits the values representing the union, i.e.,
/// all the values in self or other, without duplicates.
///
/// # Examples
///
/// ```
/// # enumap::enumap! { #[derive(Debug, PartialEq)] enum Fruit { Orange, Banana, Grape, Apple } }
/// use enumap::{Enum, EnumSet};
///
/// let a = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Apple]);
/// let b = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Grape]);
///
/// // Print Orange, Banana, Grape, Apple in order.
/// for x in a.union(&b) {
/// println!("{x:?}");
/// }
///
/// let union: Vec<Fruit> = a.union(&b).collect();
/// assert_eq!(union, vec![Fruit::Orange, Fruit::Banana, Fruit::Grape, Fruit::Apple]);
/// ```
pub fn union<'a>(&'a self, other: &'a EnumSet<LENGTH, E>) -> Union<'a, LENGTH, E> {
Union {
this: self.0.as_slice(),
other: other.0.as_slice(),
index: 0,
_enum: PhantomData,
}
}
/// Visits the values representing the symmetric difference, i.e.,
/// the values that are in self or in other but not in both.
///
/// # Examples
///
/// ```
/// # enumap::enumap! { #[derive(Debug, PartialEq)] enum Fruit { Orange, Banana, Grape, Apple } }
/// use enumap::{Enum, EnumSet};
///
/// let a = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Apple]);
/// let b = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Grape]);
///
/// // Print Grape, Apple in order.
/// for x in a.symmetric_difference(&b) {
/// println!("{x:?}");
/// }
///
/// let diff1: Vec<Fruit> = a.symmetric_difference(&b).collect();
/// let diff2: Vec<Fruit> = b.symmetric_difference(&a).collect();
/// assert_eq!(diff1, diff2);
/// assert_eq!(diff1, vec![Fruit::Grape, Fruit::Apple]);
/// ```
pub fn symmetric_difference<'a>(
&'a self,
other: &'a EnumSet<LENGTH, E>,
) -> SymmetricDifference<'a, LENGTH, E> {
SymmetricDifference {
this: self.0.as_slice(),
other: other.0.as_slice(),
index: 0,
_enum: PhantomData,
}
}
}
impl<const LENGTH: usize, E: Enum<LENGTH>> Default for EnumSet<LENGTH, E> {
fn default() -> Self {
Self::new()
}
}
impl<const LENGTH: usize, E: Enum<LENGTH>> fmt::Debug for EnumSet<LENGTH, E>
where
E: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_set().entries(self.iter()).finish()
}
}
impl<const LENGTH: usize, E: Enum<LENGTH>, const N: usize> From<[E; N]> for EnumSet<LENGTH, E> {
fn from(value: [E; N]) -> Self {
Self::from_iter(value)
}
}
impl<const LENGTH: usize, E: Enum<LENGTH>, V> From<EnumMap<LENGTH, E, V>> for EnumSet<LENGTH, E> {
/// Converts an `EnumMap` into an `EnumSet` containing all of the map's keys.
///
/// # Examples
///
/// ```
/// # enumap::enumap! { #[derive(Debug, PartialEq)] enum Fruit { Orange, Banana, Grape, } }
/// use enumap::{EnumMap, EnumSet};
///
/// let map = EnumMap::from([(Fruit::Banana, 100), (Fruit::Grape, 200)]);
/// let set = EnumSet::from(map);
///
/// assert!(set.contains(Fruit::Banana));
/// assert!(set.contains(Fruit::Grape));
/// assert!(!set.contains(Fruit::Orange));
/// ```
fn from(value: EnumMap<LENGTH, E, V>) -> Self {
let data: [_; LENGTH] = value.into();
Self(data.map(|v| v.map(|_| ())).into())
}
}
impl<const LENGTH: usize, E: Enum<LENGTH>, V> From<&EnumMap<LENGTH, E, V>> for EnumSet<LENGTH, E> {
/// Converts an `&EnumMap` into an `EnumSet` containing all of the map's keys.
///
/// # Examples
///
/// ```
/// # enumap::enumap! { #[derive(Debug, PartialEq)] enum Fruit { Orange, Banana, Grape, } }
/// use enumap::{EnumMap, EnumSet};
///
/// let map = EnumMap::from([(Fruit::Banana, 100), (Fruit::Grape, 200)]);
/// let set = EnumSet::from(&map);
///
/// assert_eq!(set.len(), map.len());
/// for fruit in set {
/// assert!(map.contains_key(fruit));
/// }
/// ```
fn from(value: &EnumMap<LENGTH, E, V>) -> Self {
let data = value.as_slice();
Self(core::array::from_fn(|i| data[i].is_some().then_some(())).into())
}
}
impl<const LENGTH: usize, E: Enum<LENGTH>> FromIterator<E> for EnumSet<LENGTH, E> {
fn from_iter<T: IntoIterator<Item = E>>(iter: T) -> Self {
let mut set = Self::new();
set.extend(iter);
set
}
}
impl<'a, const LENGTH: usize, E: Enum<LENGTH>> FromIterator<&'a E> for EnumSet<LENGTH, E> {
fn from_iter<T: IntoIterator<Item = &'a E>>(iter: T) -> Self {
let mut set = Self::new();
set.extend(iter);
set
}
}
impl<const LENGTH: usize, E: Enum<LENGTH>> Extend<E> for EnumSet<LENGTH, E> {
fn extend<T: IntoIterator<Item = E>>(&mut self, iter: T) {
for value in iter {
self.insert(value);
}
}
}
impl<'a, const LENGTH: usize, E: Enum<LENGTH>> Extend<&'a E> for EnumSet<LENGTH, E> {
fn extend<T: IntoIterator<Item = &'a E>>(&mut self, iter: T) {
for value in iter {
self.insert(*value);
}
}
}
impl<const LENGTH: usize, E: Enum<LENGTH>> IntoIterator for EnumSet<LENGTH, E> {
type Item = E;
type IntoIter = IntoIter<LENGTH, E>;
fn into_iter(self) -> Self::IntoIter {
IntoIter {
inner: self.0.into_iter(),
}
}
}
impl<'a, const LENGTH: usize, E: Enum<LENGTH>> IntoIterator for &'a EnumSet<LENGTH, E> {
type Item = E;
type IntoIter = Iter<'a, LENGTH, E>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<const LENGTH: usize, E: Enum<LENGTH>> core::ops::BitAnd<&EnumSet<LENGTH, E>>
for &EnumSet<LENGTH, E>
{
type Output = EnumSet<LENGTH, E>;
/// Returns the intersection of `self` and `rhs` as a new `EnumSet<LENGTH, E>`.
///
/// # Examples
///
/// ```
/// # enumap::enumap! { #[derive(Debug, PartialEq)] enum Fruit { Orange, Banana, Grape, Apple } }
/// use enumap::{Enum, EnumSet};
///
/// let a = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Apple]);
/// let b = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Grape]);
///
/// let set = &a & &b;
/// assert_eq!(set, EnumSet::from([Fruit::Orange, Fruit::Banana]));
/// ```
fn bitand(self, rhs: &EnumSet<LENGTH, E>) -> Self::Output {
self.intersection(rhs).collect()
}
}
impl<const LENGTH: usize, E: Enum<LENGTH>> core::ops::BitAnd<E> for EnumSet<LENGTH, E> {
type Output = EnumSet<LENGTH, E>;
/// Returns the intersection of `self` and `rhs` as a new `EnumSet<LENGTH, E>`.
///
/// # Examples
///
/// ```
/// # enumap::enumap! { #[derive(Debug, PartialEq)] enum Fruit { Orange, Banana, Grape, Apple } }
/// use enumap::{Enum, EnumSet};
///
/// let set = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Apple]);
///
/// let set = set & Fruit::Apple;
/// assert_eq!(set, EnumSet::from([Fruit::Apple]));
///
/// let set = set & Fruit::Grape;
/// assert_eq!(set, EnumSet::new());
/// ```
fn bitand(mut self, rhs: E) -> Self::Output {
if self.contains(rhs) {
self.clear();
self.insert(rhs);
} else {
self.clear();
}
self
}
}
impl<const LENGTH: usize, E: Enum<LENGTH>> core::ops::BitOr<&EnumSet<LENGTH, E>>
for &EnumSet<LENGTH, E>
{
type Output = EnumSet<LENGTH, E>;
/// Returns the union of `self` and `rhs` as a new `EnumSet<LENGTH, E>`.
///
/// # Examples
///
/// ```
/// # enumap::enumap! { #[derive(Debug, PartialEq)] enum Fruit { Orange, Banana, Grape, Apple } }
/// use enumap::{Enum, EnumSet};
///
/// let a = EnumSet::from([Fruit::Orange, Fruit::Apple]);
/// let b = EnumSet::from([Fruit::Orange, Fruit::Grape]);
///
/// let set = &a | &b;
/// assert_eq!(set, EnumSet::from([Fruit::Orange, Fruit::Grape, Fruit::Apple]));
/// ```
fn bitor(self, rhs: &EnumSet<LENGTH, E>) -> Self::Output {
self.union(rhs).collect()
}
}
impl<const LENGTH: usize, E: Enum<LENGTH>> core::ops::BitOr<E> for EnumSet<LENGTH, E> {
type Output = EnumSet<LENGTH, E>;
/// Returns the union of `self` and `rhs` as a new `EnumSet<LENGTH, E>`.
///
/// # Examples
///
/// ```
/// # enumap::enumap! { #[derive(Debug, PartialEq)] enum Fruit { Orange, Banana, Grape, Apple } }
/// use enumap::{Enum, EnumSet};
///
/// let set = EnumSet::from([Fruit::Orange, Fruit::Apple]);
///
/// let set = set | Fruit::Banana | Fruit::Orange;
/// assert_eq!(set, EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Apple]));
/// ```
fn bitor(mut self, rhs: E) -> Self::Output {
self.insert(rhs);
self
}
}
impl<const LENGTH: usize, E: Enum<LENGTH>> core::ops::BitXor<&EnumSet<LENGTH, E>>
for &EnumSet<LENGTH, E>
{
type Output = EnumSet<LENGTH, E>;
/// Returns the symmetric difference of `self` and `rhs` as a new `EnumSet<LENGTH, E>`.
///
/// # Examples
///
/// ```
/// # enumap::enumap! { #[derive(Debug, PartialEq)] enum Fruit { Orange, Banana, Grape, Apple } }
/// use enumap::{Enum, EnumSet};
///
/// let a = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Apple]);
/// let b = EnumSet::from([Fruit::Orange, Fruit::Grape]);
///
/// let set = &a ^ &b;
/// assert_eq!(set, EnumSet::from([Fruit::Banana, Fruit::Grape, Fruit::Apple]));
/// ```
fn bitxor(self, rhs: &EnumSet<LENGTH, E>) -> Self::Output {
self.symmetric_difference(rhs).collect()
}
}
impl<const LENGTH: usize, E: Enum<LENGTH>> core::ops::BitXor<E> for EnumSet<LENGTH, E> {
type Output = EnumSet<LENGTH, E>;
/// Returns the symmetric difference of `self` and `rhs` as a new `EnumSet<LENGTH, E>`.
///
/// # Examples
///
/// ```
/// # enumap::enumap! { #[derive(Debug, PartialEq)] enum Fruit { Orange, Banana, Grape, } }
/// use enumap::{Enum, EnumSet};
///
/// let set = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Grape]);
///
/// let set = set ^ Fruit::Banana;
/// assert_eq!(set, EnumSet::from([Fruit::Orange, Fruit::Grape]));
///
/// let set = set ^ Fruit::Banana;
/// assert_eq!(set, EnumSet::from([Fruit::Banana]));
/// ```
fn bitxor(mut self, rhs: E) -> Self::Output {
if !self.remove(rhs) {
EnumSet::from([rhs])
} else {
self
}
}
}
impl<const LENGTH: usize, E: Enum<LENGTH>> core::ops::Sub<&EnumSet<LENGTH, E>>
for &EnumSet<LENGTH, E>
{
type Output = EnumSet<LENGTH, E>;
/// Returns the difference of `self` and `rhs` as a new `EnumSet<LENGTH, E>`.
///
/// # Examples
///
/// ```
/// # enumap::enumap! { #[derive(Debug, PartialEq)] enum Fruit { Orange, Banana, Grape, Apple } }
/// use enumap::{Enum, EnumSet};
///
/// let a = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Apple]);
/// let b = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Grape]);
///
/// let set = &a - &b;
/// assert_eq!(set, EnumSet::from([Fruit::Apple]));
/// ```
fn sub(self, rhs: &EnumSet<LENGTH, E>) -> Self::Output {
self.difference(rhs).collect()
}
}
/// Iterator returned from [`EnumSet::iter`].
pub struct Iter<'a, const LENGTH: usize, E: Enum<LENGTH>> {
inner: map::Keys<'a, LENGTH, E, ()>,
}
impl<'a, const LENGTH: usize, E: Enum<LENGTH>> Iterator for Iter<'a, LENGTH, E> {
type Item = E;
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
}
/// Iterator returned from [`EnumSet::into_iter`].
pub struct IntoIter<const LENGTH: usize, E: Enum<LENGTH>> {
inner: map::IntoIter<LENGTH, E, ()>,
}
impl<const LENGTH: usize, E: Enum<LENGTH>> Iterator for IntoIter<LENGTH, E> {
type Item = E;
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|(v, _)| v)
}
}
/// Iterator returned from [`EnumSet::difference`].
pub struct Difference<'a, const LENGTH: usize, E: Enum<LENGTH>> {
this: &'a [Option<()>; LENGTH],
other: &'a [Option<()>; LENGTH],
index: usize,
_enum: PhantomData<E>,
}
impl<'a, const LENGTH: usize, E: Enum<LENGTH>> Iterator for Difference<'a, LENGTH, E> {
type Item = E;
fn next(&mut self) -> Option<Self::Item> {
while self.index < LENGTH {
let index = self.index;
self.index += 1;
if self.this[index].is_some() && self.other[index].is_none() {
return E::from_index(index);
}
}
None
}
}
/// Iterator returned from [`EnumSet::intersection`].
pub struct Intersection<'a, const LENGTH: usize, E: Enum<LENGTH>> {
this: &'a [Option<()>; LENGTH],
other: &'a [Option<()>; LENGTH],
index: usize,
_enum: PhantomData<E>,
}
impl<'a, const LENGTH: usize, E: Enum<LENGTH>> Iterator for Intersection<'a, LENGTH, E> {
type Item = E;
fn next(&mut self) -> Option<Self::Item> {
while self.index < LENGTH {
let index = self.index;
self.index += 1;
if self.this[index].is_some() && self.other[index].is_some() {
return E::from_index(index);
}
}
None
}
}
/// Iterator returned from [`EnumSet::union`].
pub struct Union<'a, const LENGTH: usize, E: Enum<LENGTH>> {
this: &'a [Option<()>; LENGTH],
other: &'a [Option<()>; LENGTH],
index: usize,
_enum: PhantomData<E>,
}
impl<'a, const LENGTH: usize, E: Enum<LENGTH>> Iterator for Union<'a, LENGTH, E> {
type Item = E;
fn next(&mut self) -> Option<Self::Item> {
while self.index < LENGTH {
let index = self.index;
self.index += 1;
if self.this[index].is_some() || self.other[index].is_some() {
return E::from_index(index);
}
}
None
}
}
/// Iterator returned from [`EnumSet::symmetric_difference`].
pub struct SymmetricDifference<'a, const LENGTH: usize, E: Enum<LENGTH>> {
this: &'a [Option<()>; LENGTH],
other: &'a [Option<()>; LENGTH],
index: usize,
_enum: PhantomData<E>,
}
impl<'a, const LENGTH: usize, E: Enum<LENGTH>> Iterator for SymmetricDifference<'a, LENGTH, E> {
type Item = E;
fn next(&mut self) -> Option<Self::Item> {
while self.index < LENGTH {
let index = self.index;
self.index += 1;
if self.this[index].is_some() ^ self.other[index].is_some() {
return E::from_index(index);
}
}
None
}
}