aboutsummaryrefslogtreecommitdiff
path: root/src/map.rs
blob: 7099a96bc2782a38b4af5c62d6d547993913a93a (plain)
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
//! Enum Maps

pub mod axis {
    use crate::Axis;

    const LENGTH: usize = 4;

    pub struct Items<'a, T>
    where
        T: 'a,
    {
        map: &'a Map<T>,
        state: Option<Axis>,
    }

    impl<'a, T> Iterator for Items<'a, T> {
        type Item = (Axis, &'a T);

        fn next(&mut self) -> Option<(Axis, &'a T)> {
            while let Some(key) = self.state {
                self.state = key.next();

                if let Some(value) = self.map.get(key) {
                    return Some((key, value));
                }
            }

            None
        }
    }

    pub struct Map<T>([Option<T>; LENGTH]);

    impl<T> Default for Map<T> {
        fn default() -> Self {
            Self::new()
        }
    }

    impl<T> Map<T> {
        pub fn new() -> Map<T> {
            Map([None, None, None, None])
        }

        pub fn contains_key(&self, key: Axis) -> bool {
            self.0[key as usize].is_some()
        }

        pub fn get(&self, key: Axis) -> Option<&T> {
            self.0[key as usize].as_ref()
        }

        pub fn get_mut(&mut self, key: Axis) -> Option<&mut T> {
            self.0[key as usize].as_mut()
        }

        pub fn insert(&mut self, key: Axis, value: T) -> Option<T> {
            let key = key as usize;
            let old = self.0[key].take();

            self.0[key] = Some(value);

            old
        }

        pub fn iter(&self) -> Items<T> {
            Items {
                map: self,
                state: Some(Axis::BottomX),
            }
        }
    }

    impl<T> Clone for Map<T>
    where
        T: Clone,
    {
        fn clone(&self) -> Map<T> {
            Map([
                self.0[0].clone(),
                self.0[1].clone(),
                self.0[2].clone(),
                self.0[3].clone(),
            ])
        }
    }
}

pub mod grid {
    use crate::Grid;

    const LENGTH: usize = 2;

    pub struct Items<'a, T>
    where
        T: 'a,
    {
        map: &'a Map<T>,
        state: Option<Grid>,
    }

    impl<'a, T> Iterator for Items<'a, T> {
        type Item = (Grid, &'a T);

        fn next(&mut self) -> Option<(Grid, &'a T)> {
            while let Some(key) = self.state {
                self.state = key.next();

                if let Some(value) = self.map.get(key) {
                    return Some((key, value));
                }
            }

            None
        }
    }

    pub struct Map<T>([Option<T>; LENGTH]);

    impl<T> Map<T> {
        pub fn new() -> Map<T> {
            Map([None, None])
        }

        pub fn contains_key(&self, key: Grid) -> bool {
            self.0[key as usize].is_some()
        }

        pub fn get(&self, key: Grid) -> Option<&T> {
            self.0[key as usize].as_ref()
        }

        pub fn get_mut(&mut self, key: Grid) -> Option<&mut T> {
            self.0[key as usize].as_mut()
        }

        pub fn insert(&mut self, key: Grid, value: T) -> Option<T> {
            let key = key as usize;
            let old = self.0[key].take();

            self.0[key] = Some(value);

            old
        }

        pub fn iter(&self) -> Items<T> {
            Items {
                map: self,
                state: Some(Grid::Major),
            }
        }
    }

    impl<T> Clone for Map<T>
    where
        T: Clone,
    {
        fn clone(&self) -> Map<T> {
            Map([self.0[0].clone(), self.0[1].clone()])
        }
    }

    impl<T> Default for Map<T> {
        fn default() -> Self {
            Self::new()
        }
    }
}