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
//! Provides an interator which produces indexes along with the values of an iterator over
//! [`CellMap`].

// ------------------------------------------------------------------------------------------------
// IMPORTS
// ------------------------------------------------------------------------------------------------

use std::marker::PhantomData;

use nalgebra::Vector2;

use crate::{iterators::CellMapIter, Layer};

use super::Layered;

// ------------------------------------------------------------------------------------------------
// STRUCTS
// ------------------------------------------------------------------------------------------------

/// Modified the wrapped interator to return the index of the cell as well as the cell itself.
pub struct Indexed<L, T, I>
where
    L: Layer,
    I: CellMapIter<L, T>,
{
    pub(crate) iter: I,

    pub(crate) _phantom: PhantomData<(L, T)>,
}

// ------------------------------------------------------------------------------------------------
// IMPLS
// ------------------------------------------------------------------------------------------------

impl<L, T, I> Iterator for Indexed<L, T, I>
where
    L: Layer,
    I: CellMapIter<L, T>,
{
    type Item = ((L, Vector2<usize>), I::Item);

    fn next(&mut self) -> Option<Self::Item> {
        // Gotta access the index first, as next should increment after yeilding. Must use the
        // get_layer_checked function instead of get layer to avoid panics once the iterator has
        // reached the end of the map.
        let index = (
            self.get_layer_checked()?,
            Vector2::new(self.iter.get_x(), self.iter.get_y()),
        );

        if let Some(next) = self.iter.next() {
            Some((index, next))
        } else {
            return None;
        }
    }
}

impl<L, T, I> CellMapIter<L, T> for Indexed<L, T, I>
where
    L: Layer,
    I: CellMapIter<L, T>,
{
    fn limit_layers(&mut self, layers: &[L]) {
        self.iter.limit_layers(layers)
    }

    fn get_layer_checked(&self) -> Option<L> {
        self.iter.get_layer_checked()
    }

    fn get_layer(&self) -> L {
        self.iter.get_layer()
    }

    fn get_x(&self) -> usize {
        self.iter.get_x()
    }

    fn get_y(&self) -> usize {
        self.iter.get_y()
    }
}

impl<L, T, I> Indexed<L, T, I>
where
    L: Layer,
    I: CellMapIter<L, T>,
{
    /// Modifies this iterator to only produce the cells in the given layer.
    pub fn layer(mut self, layer: L) -> Layered<L, T, Self> {
        self.limit_layers(&[layer]);
        Layered {
            iter: self,
            _phantom: PhantomData,
        }
    }

    /// Modifies this iterator to only produce the cells in the given layers.
    pub fn layers(mut self, layers: &[L]) -> Layered<L, T, Self> {
        self.limit_layers(layers);
        Layered {
            iter: self,
            _phantom: PhantomData,
        }
    }
}

impl<L, T, I> Clone for Indexed<L, T, I>
where
    L: Layer,
    I: CellMapIter<L, T> + Clone,
{
    fn clone(&self) -> Self {
        Self {
            iter: self.iter.clone(),
            _phantom: PhantomData,
        }
    }
}

// ------------------------------------------------------------------------------------------------
// TESTS
// ------------------------------------------------------------------------------------------------

#[cfg(test)]
mod tests {

    use nalgebra::Vector2;
    use ndarray::arr2;

    use crate::{CellMap, CellMapParams, Layer};

    #[derive(Clone, Copy, Eq, PartialEq, Debug)]
    #[allow(dead_code)]
    enum MyLayers {
        Layer0,
        Layer1,
        Layer2,
    }

    // Have to do a manual impl because the derive doesn't like working inside this crate, for some
    // reason
    impl Layer for MyLayers {
        const NUM_LAYERS: usize = 3;
        const FIRST: Self = Self::Layer0;
        fn to_index(&self) -> usize {
            match self {
                Self::Layer0 => 0,
                Self::Layer1 => 1,
                Self::Layer2 => 2,
            }
        }

        fn from_index(index: usize) -> Self {
            match index {
                0 => Self::Layer0,
                1 => Self::Layer1,
                2 => Self::Layer2,
                _ => panic!(
                    "Got a layer index of {} but there are only {} layers",
                    index,
                    Self::NUM_LAYERS
                ),
            }
        }
    }

    #[test]
    fn index_correct() {
        // Create dummy map
        let mut map = CellMap::<MyLayers, usize>::new_from_elem(
            CellMapParams {
                cell_size: Vector2::new(1.0, 1.0),
                num_cells: Vector2::new(3, 3),
                centre: Vector2::new(0.0, 0.0),
            },
            0,
        );

        // Set each element in the map to the sum of it's indices
        map.iter_mut().indexed().for_each(|((layer, cell), value)| {
            *value = layer.to_index() + cell.x + cell.y;
        });

        // Check that each cell is now set correctly
        assert_eq!(
            map[MyLayers::Layer0],
            arr2(&[[0, 1, 2], [1, 2, 3], [2, 3, 4]])
        );
        assert_eq!(
            map[MyLayers::Layer1],
            arr2(&[[1, 2, 3], [2, 3, 4], [3, 4, 5]])
        );
        assert_eq!(
            map[MyLayers::Layer2],
            arr2(&[[2, 3, 4], [3, 4, 5], [4, 5, 6]])
        );
    }
}