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
use enum_map::{Enum, EnumArray, MaybeUninit};
use itertools::enumerate;
pub mod enum_map_ext;
pub trait EnumSequence: EnumArray<Self> {
type Iter: Iterator<Item = Self>;
fn first() -> Self;
fn last() -> Self;
fn next(self) -> Option<Self>;
fn previous(self) -> Option<Self>;
fn follows(self, other: Self) -> bool
where
Self: PartialEq<Self>;
fn precedes(self, other: Self) -> bool
where
Self: PartialEq<Self>;
fn values_iter() -> Self::Iter;
fn values_array() -> <Self as EnumArray<Self>>::Array;
}
impl<E, const N: usize> EnumSequence for E
where
Self: EnumArray<Self, Array = [Self; N]>,
{
type Iter = impl Iterator<Item = Self>
+ DoubleEndedIterator<Item = Self>
+ ExactSizeIterator<Item = Self>;
fn first() -> Self {
E::from_usize(0)
}
fn last() -> Self {
E::from_usize(E::LENGTH - 1)
}
fn next(self) -> Option<Self>
where
E: Enum,
{
let identifier = self.into_usize();
(identifier < E::LENGTH - 1).then(|| E::from_usize(identifier + 1))
}
fn previous(self) -> Option<Self>
where
E: Enum,
{
let identifier = self.into_usize();
(identifier > 0).then(|| E::from_usize(identifier - 1))
}
fn follows(self, other: Self) -> bool
where
Self: PartialEq<Self>,
{
Some(self) == other.next()
}
fn precedes(self, other: Self) -> bool
where
Self: PartialEq<Self>,
{
Some(self) == other.previous()
}
fn values_iter() -> Self::Iter {
(0..E::LENGTH).map(E::from_usize)
}
fn values_array() -> <Self as EnumArray<Self>>::Array {
let mut values: [MaybeUninit<Self>; N] = MaybeUninit::uninit_array();
for (index, uninit_value) in enumerate(&mut values) {
uninit_value.write(Self::from_usize(index));
}
unsafe { MaybeUninit::array_assume_init(values) }
}
}