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
use std::iter::{once, Chain, Once}; use std::ops::{Index, IndexMut}; use {Sim, SquareGrid, TakeMoveDirection, TakeMoveNeighbors}; use std::mem::transmute_copy; use {GetNeighbors, Neighborhood}; #[derive(Copy, Clone, Debug, PartialEq, Eq, EnumIterator)] pub enum Direction { Right, Up, Left, Down, } impl super::Direction for Direction { type Directions = DirectionEnumIterator; #[inline] fn inv(self) -> Direction { use self::Direction::*; match self { Right => Left, Up => Down, Left => Right, Down => Up, } } #[inline] fn directions() -> Self::Directions { Direction::iter_variants() } } impl Direction { #[inline] fn delta(self) -> (isize, isize) { use self::Direction::*; match self { Right => (1, 0), Up => (0, -1), Left => (-1, 0), Down => (0, 1), } } #[inline] pub fn left(self) -> Self { use self::Direction::*; match self { Right => Up, Up => Left, Left => Down, Down => Right, } } #[inline] pub fn right(self) -> Self { use self::Direction::*; match self { Right => Down, Up => Right, Left => Up, Down => Left, } } } #[derive(Copy, Clone, Debug, Default)] pub struct Neighbors<T> { pub right: T, pub up: T, pub left: T, pub down: T, } impl<T> Index<Direction> for Neighbors<T> { type Output = T; #[inline] fn index(&self, ix: Direction) -> &T { use self::Direction::*; match ix { Right => &self.right, Up => &self.up, Left => &self.left, Down => &self.down, } } } impl<T> IndexMut<Direction> for Neighbors<T> { #[inline] fn index_mut(&mut self, ix: Direction) -> &mut T { use self::Direction::*; match ix { Right => &mut self.right, Up => &mut self.up, Left => &mut self.left, Down => &mut self.down, } } } type NeighborhoodIter<T> = Chain<Chain<Chain<Once<T>, Once<T>>, Once<T>>, Once<T>>; impl<T> Neighborhood<T> for Neighbors<T> { type Direction = Direction; type Iter = NeighborhoodIter<T>; type DirIter = NeighborhoodIter<(Direction, T)>; #[inline] fn new<F: FnMut(Direction) -> T>(mut f: F) -> Neighbors<T> { use self::Direction::*; Neighbors { right: f(Right), up: f(Up), left: f(Left), down: f(Down), } } #[inline] fn iter(self) -> Self::Iter { once(self.right) .chain(once(self.up)) .chain(once(self.left)) .chain(once(self.down)) } #[inline] fn dir_iter(self) -> Self::DirIter { use self::Direction::*; once((Right, self.right)) .chain(once((Up, self.up))) .chain(once((Left, self.left))) .chain(once((Down, self.down))) } } impl<'a, T> From<Neighbors<&'a T>> for Neighbors<T> where T: Clone, { #[inline] fn from(f: Neighbors<&'a T>) -> Self { Neighbors::new(|dir| f[dir].clone()) } } impl<'a, C, S> GetNeighbors<'a, usize, Neighbors<&'a C>> for SquareGrid<'a, S> where S: Sim<'a, Cell = C>, { #[inline] fn get_neighbors(&'a self, ix: usize) -> Neighbors<&'a C> { Neighbors::new(|dir| unsafe { self.get_cell_unchecked(self.delta_index(ix, dir.delta())) }) } } impl<'a, S, M> TakeMoveDirection<usize, Direction, M> for SquareGrid<'a, S> where S: Sim<'a, Move = M, MoveNeighbors = Neighbors<M>>, { #[inline] unsafe fn take_move_direction(&self, ix: usize, dir: Direction) -> M { transmute_copy(&self.get_move_neighbors(ix)[dir]) } } impl<'a, S, M> TakeMoveNeighbors<usize, Neighbors<M>> for SquareGrid<'a, S> where S: Sim<'a, Move = M, MoveNeighbors = Neighbors<M>>, { #[inline] unsafe fn take_move_neighbors(&self, ix: usize) -> Neighbors<M> { use Direction; Neighbors::new(|dir| self.take_move_direction(self.delta_index(ix, dir.delta()), dir.inv())) } }