criterion_plot/
map.rs

1//! Enum Maps
2
3pub mod axis {
4    use crate::Axis;
5
6    const LENGTH: usize = 4;
7
8    pub struct Items<'a, T>
9    where
10        T: 'a,
11    {
12        map: &'a Map<T>,
13        state: Option<Axis>,
14    }
15
16    impl<'a, T> Iterator for Items<'a, T> {
17        type Item = (Axis, &'a T);
18
19        fn next(&mut self) -> Option<(Axis, &'a T)> {
20            while let Some(key) = self.state {
21                self.state = key.next();
22
23                if let Some(value) = self.map.get(key) {
24                    return Some((key, value));
25                }
26            }
27
28            None
29        }
30    }
31
32    pub struct Map<T>([Option<T>; LENGTH]);
33
34    impl<T> Default for Map<T> {
35        fn default() -> Self {
36            Self::new()
37        }
38    }
39
40    impl<T> Map<T> {
41        pub fn new() -> Map<T> {
42            Map([None, None, None, None])
43        }
44
45        pub fn contains_key(&self, key: Axis) -> bool {
46            self.0[key as usize].is_some()
47        }
48
49        pub fn get(&self, key: Axis) -> Option<&T> {
50            self.0[key as usize].as_ref()
51        }
52
53        pub fn get_mut(&mut self, key: Axis) -> Option<&mut T> {
54            self.0[key as usize].as_mut()
55        }
56
57        pub fn insert(&mut self, key: Axis, value: T) -> Option<T> {
58            let key = key as usize;
59            let old = self.0[key].take();
60
61            self.0[key] = Some(value);
62
63            old
64        }
65
66        pub fn iter(&self) -> Items<T> {
67            Items {
68                map: self,
69                state: Some(Axis::BottomX),
70            }
71        }
72    }
73
74    impl<T> Clone for Map<T>
75    where
76        T: Clone,
77    {
78        fn clone(&self) -> Map<T> {
79            Map([
80                self.0[0].clone(),
81                self.0[1].clone(),
82                self.0[2].clone(),
83                self.0[3].clone(),
84            ])
85        }
86    }
87}
88
89pub mod grid {
90    use crate::Grid;
91
92    const LENGTH: usize = 2;
93
94    pub struct Items<'a, T>
95    where
96        T: 'a,
97    {
98        map: &'a Map<T>,
99        state: Option<Grid>,
100    }
101
102    impl<'a, T> Iterator for Items<'a, T> {
103        type Item = (Grid, &'a T);
104
105        fn next(&mut self) -> Option<(Grid, &'a T)> {
106            while let Some(key) = self.state {
107                self.state = key.next();
108
109                if let Some(value) = self.map.get(key) {
110                    return Some((key, value));
111                }
112            }
113
114            None
115        }
116    }
117
118    pub struct Map<T>([Option<T>; LENGTH]);
119
120    impl<T> Map<T> {
121        pub fn new() -> Map<T> {
122            Map([None, None])
123        }
124
125        pub fn contains_key(&self, key: Grid) -> bool {
126            self.0[key as usize].is_some()
127        }
128
129        pub fn get(&self, key: Grid) -> Option<&T> {
130            self.0[key as usize].as_ref()
131        }
132
133        pub fn get_mut(&mut self, key: Grid) -> Option<&mut T> {
134            self.0[key as usize].as_mut()
135        }
136
137        pub fn insert(&mut self, key: Grid, value: T) -> Option<T> {
138            let key = key as usize;
139            let old = self.0[key].take();
140
141            self.0[key] = Some(value);
142
143            old
144        }
145
146        pub fn iter(&self) -> Items<T> {
147            Items {
148                map: self,
149                state: Some(Grid::Major),
150            }
151        }
152    }
153
154    impl<T> Clone for Map<T>
155    where
156        T: Clone,
157    {
158        fn clone(&self) -> Map<T> {
159            Map([self.0[0].clone(), self.0[1].clone()])
160        }
161    }
162
163    impl<T> Default for Map<T> {
164        fn default() -> Self {
165            Self::new()
166        }
167    }
168}