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
use crate::{Direction, Sim, SquareGrid, TakeMoveDirection, TakeMoveNeighbors};
use std::iter::{once, Chain, Once};
use std::mem::transmute_copy;
use std::ops::{Index, IndexMut};
use NeumannDirection::*;

use crate::{GetNeighbors, Neighborhood};

#[derive(Copy, Clone, Debug, PartialEq, Eq, EnumIterator)]
pub enum NeumannDirection {
    Right,
    UpRight,
    Up,
    UpLeft,
    Left,
    DownLeft,
    Down,
    DownRight,
}

impl Direction for NeumannDirection {
    type Directions = NeumannDirectionEnumIterator;

    #[inline]
    fn directions() -> Self::Directions {
        NeumannDirection::iter_variants()
    }
}

impl NeumannDirection {
    #[inline]
    pub fn delta(self) -> (isize, isize) {
        match self {
            Right => (1, 0),
            UpRight => (1, -1),
            Up => (0, -1),
            UpLeft => (-1, -1),
            Left => (-1, 0),
            DownLeft => (-1, 1),
            Down => (0, 1),
            DownRight => (1, 1),
        }
    }
}

impl From<usize> for NeumannDirection {
    fn from(n: usize) -> Self {
        match n {
            0 => Right,
            1 => UpRight,
            2 => Up,
            3 => UpLeft,
            4 => Left,
            5 => DownLeft,
            6 => Down,
            7 => DownRight,
            _ => panic!("invalid integer conversion to Direction"),
        }
    }
}

impl Into<usize> for NeumannDirection {
    fn into(self) -> usize {
        match self {
            Right => 0,
            UpRight => 1,
            Up => 2,
            UpLeft => 3,
            Left => 4,
            DownLeft => 5,
            Down => 6,
            DownRight => 7,
        }
    }
}

#[derive(Copy, Clone, Debug, Default)]
pub struct NeumannNeighbors<T> {
    pub right: T,
    pub up_right: T,
    pub up: T,
    pub up_left: T,
    pub left: T,
    pub down_left: T,
    pub down: T,
    pub down_right: T,
}

impl<T> NeumannNeighbors<T> {
    pub fn as_ref(&self) -> NeumannNeighbors<&T> {
        NeumannNeighbors {
            right: &self.right,
            up_right: &self.up_right,
            up: &self.up,
            up_left: &self.up_left,
            left: &self.left,
            down_left: &self.down_left,
            down: &self.down,
            down_right: &self.down_right,
        }
    }
}

impl NeumannNeighbors<bool> {
    /// Must provide an input iterator with length of the number of directions for the `Direction` impl
    /// which contains sigmoid outputs in the range of (0.0, 1.0). For each direction this will instantiate
    /// a bool that indicates whether that direction was above 0.5.
    #[inline]
    pub fn chooser(sigmoids: impl Iterator<Item = f32>) -> Self {
        sigmoids.map(|value| value > 0.5).collect()
    }

    #[inline]
    pub fn chooser_slice(sigmoids: &[f32]) -> Self {
        Self::chooser(sigmoids.iter().cloned())
    }
}

impl<T> std::iter::FromIterator<T> for NeumannNeighbors<T> {
    fn from_iter<I>(iter: I) -> Self
    where
        I: IntoIterator<Item = T>,
    {
        let mut iter = iter.into_iter();
        Self {
            right: iter.next().unwrap(),
            up_right: iter.next().unwrap(),
            up: iter.next().unwrap(),
            up_left: iter.next().unwrap(),
            left: iter.next().unwrap(),
            down_left: iter.next().unwrap(),
            down: iter.next().unwrap(),
            down_right: iter.next().unwrap(),
        }
    }
}

impl<T> Index<NeumannDirection> for NeumannNeighbors<T> {
    type Output = T;
    #[inline]
    fn index(&self, ix: NeumannDirection) -> &T {
        match ix {
            Right => &self.right,
            UpRight => &self.up_right,
            Up => &self.up,
            UpLeft => &self.up_left,
            Left => &self.left,
            DownLeft => &self.down_left,
            Down => &self.down,
            DownRight => &self.down_right,
        }
    }
}

impl<T> IndexMut<NeumannDirection> for NeumannNeighbors<T> {
    #[inline]
    fn index_mut(&mut self, ix: NeumannDirection) -> &mut T {
        match ix {
            Right => &mut self.right,
            UpRight => &mut self.up_right,
            Up => &mut self.up,
            UpLeft => &mut self.up_left,
            Left => &mut self.left,
            DownLeft => &mut self.down_left,
            Down => &mut self.down,
            DownRight => &mut self.down_right,
        }
    }
}

type NeighborhoodIter<T> = Chain<
    Chain<
        Chain<Chain<Chain<Chain<Chain<Once<T>, Once<T>>, Once<T>>, Once<T>>, Once<T>>, Once<T>>,
        Once<T>,
    >,
    Once<T>,
>;

impl<T> Neighborhood<T> for NeumannNeighbors<T> {
    type Direction = NeumannDirection;
    type Iter = NeighborhoodIter<T>;
    type DirIter = NeighborhoodIter<(NeumannDirection, T)>;

    #[inline]
    fn new<F: FnMut(NeumannDirection) -> T>(mut f: F) -> NeumannNeighbors<T> {
        NeumannNeighbors {
            right: f(Right),
            up_right: f(UpRight),
            up: f(Up),
            up_left: f(UpLeft),
            left: f(Left),
            down_left: f(DownLeft),
            down: f(Down),
            down_right: f(DownRight),
        }
    }

    #[inline]
    fn iter(self) -> Self::Iter {
        once(self.right)
            .chain(once(self.up_right))
            .chain(once(self.up))
            .chain(once(self.up_left))
            .chain(once(self.left))
            .chain(once(self.down_left))
            .chain(once(self.down))
            .chain(once(self.down_right))
    }

    #[inline]
    fn dir_iter(self) -> Self::DirIter {
        once((Right, self.right))
            .chain(once((UpRight, self.up_right)))
            .chain(once((Up, self.up)))
            .chain(once((UpLeft, self.up_left)))
            .chain(once((Left, self.left)))
            .chain(once((DownLeft, self.down_left)))
            .chain(once((Down, self.down)))
            .chain(once((DownRight, self.down_right)))
    }
}

impl<'a, T> From<NeumannNeighbors<&'a T>> for NeumannNeighbors<T>
where
    T: Clone,
{
    #[inline]
    fn from(f: NeumannNeighbors<&'a T>) -> Self {
        NeumannNeighbors::new(|dir| f[dir].clone())
    }
}

impl<'a, C, S> GetNeighbors<'a, usize, NeumannNeighbors<&'a C>> for SquareGrid<'a, S>
where
    S: Sim<'a, Cell = C>,
{
    #[inline]
    fn get_neighbors(&'a self, ix: usize) -> NeumannNeighbors<&'a C> {
        NeumannNeighbors::new(|dir| unsafe {
            self.get_cell_unchecked(self.delta_index(ix, dir.delta()))
        })
    }
}

impl<'a, S, M> TakeMoveDirection<usize, NeumannDirection, M> for SquareGrid<'a, S>
where
    S: Sim<'a, Move = M, MoveNeighbors = NeumannNeighbors<M>>,
{
    #[inline]
    unsafe fn take_move_direction(&self, ix: usize, dir: NeumannDirection) -> M {
        transmute_copy(&self.get_move_neighbors(ix)[dir])
    }
}

impl<'a, S, M> TakeMoveNeighbors<usize, NeumannNeighbors<M>> for SquareGrid<'a, S>
where
    S: Sim<'a, Move = M, MoveNeighbors = NeumannNeighbors<M>>,
{
    #[inline]
    unsafe fn take_move_neighbors(&self, ix: usize) -> NeumannNeighbors<M> {
        NeumannNeighbors::new(|dir| {
            self.take_move_direction(self.delta_index(ix, dir.delta()), dir.inv())
        })
    }
}