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
use direction::*;
use grid_2d::*;
use num_traits::Zero;

#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy)]
pub struct DistanceMapCell<Cost> {
    pub(crate) seen: u64,
    pub(crate) visited: u64,
    pub(crate) cost: Cost,
    pub(crate) direction: Direction,
    pub(crate) coord: Coord,
}

impl<Cost: Zero> DistanceMapCell<Cost> {
    fn new(coord: Coord) -> Self {
        Self {
            seen: 0,
            visited: 0,
            cost: Zero::zero(),
            direction: Direction::North,
            coord,
        }
    }
}

impl<Cost> DistanceMapCell<Cost>
where
    Cost: Copy,
{
    pub fn cost(&self) -> Cost {
        self.cost
    }
    pub fn direction(&self) -> Direction {
        self.direction
    }
}

#[derive(Debug, Clone)]
pub enum DistanceMapEntry<'a, Cost: 'a> {
    Origin,
    Unvisited,
    Outside,
    Cell(&'a DistanceMapCell<Cost>),
}

impl<'a, Cost> DistanceMapEntry<'a, Cost> {
    pub fn cell(self) -> Option<&'a DistanceMapCell<Cost>> {
        match self {
            DistanceMapEntry::Cell(c) => Some(c),
            _ => None,
        }
    }
    pub fn is_origin(self) -> bool {
        match self {
            DistanceMapEntry::Origin => true,
            _ => false,
        }
    }
    pub fn is_unvisited(self) -> bool {
        match self {
            DistanceMapEntry::Unvisited => true,
            _ => false,
        }
    }
    pub fn is_outside(self) -> bool {
        match self {
            DistanceMapEntry::Outside => true,
            _ => false,
        }
    }
}

#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct DistanceMap<Cost> {
    pub(crate) seq: u64,
    pub(crate) grid: Grid<DistanceMapCell<Cost>>,
    pub(crate) origin: Coord,
}

impl<Cost> DistanceMap<Cost>
where
    Cost: Zero + Copy,
{
    pub fn new(size: Size) -> Self {
        Self {
            seq: 1,
            grid: Grid::new_fn(size, DistanceMapCell::new),
            origin: Coord::new(0, 0),
        }
    }

    pub fn width(&self) -> u32 {
        self.grid.width()
    }

    pub fn height(&self) -> u32 {
        self.grid.height()
    }

    pub fn size(&self) -> Size {
        self.grid.size()
    }

    pub fn get(&self, coord: Coord) -> DistanceMapEntry<Cost> {
        if let Some(cell) = self.grid.get(coord) {
            if cell.seen == self.seq {
                if coord == self.origin {
                    DistanceMapEntry::Origin
                } else {
                    DistanceMapEntry::Cell(cell)
                }
            } else {
                DistanceMapEntry::Unvisited
            }
        } else {
            DistanceMapEntry::Outside
        }
    }

    pub fn cost(&self, coord: Coord) -> Option<Cost> {
        match self.get(coord) {
            DistanceMapEntry::Cell(cell) => Some(cell.cost),
            DistanceMapEntry::Origin => Some(Zero::zero()),
            DistanceMapEntry::Unvisited => None,
            DistanceMapEntry::Outside => None,
        }
    }
}

#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct UniformDistanceMap<Cost, Directions> {
    pub(crate) distance_map: DistanceMap<Cost>,
    pub(crate) directions: Directions,
}

impl<Cost, IntoDirection, Directions> UniformDistanceMap<Cost, Directions>
where
    Cost: Zero + Copy,
    IntoDirection: Into<Direction>,
    Directions: Copy + IntoIterator<Item = IntoDirection>,
{
    pub fn new(size: Size, directions: Directions) -> Self {
        let distance_map = DistanceMap::new(size);
        Self {
            distance_map,
            directions,
        }
    }

    pub fn width(&self) -> u32 {
        self.distance_map.width()
    }

    pub fn height(&self) -> u32 {
        self.distance_map.height()
    }

    pub fn size(&self) -> Size {
        self.distance_map.size()
    }

    pub fn get(&self, coord: Coord) -> DistanceMapEntry<Cost> {
        self.distance_map.get(coord)
    }

    pub fn cost(&self, coord: Coord) -> Option<Cost> {
        self.distance_map.cost(coord)
    }
}