aoclib/geometry/map/
edge.rs

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
use crate::geometry::{Direction, Point};

/// Iterator over points on the edge of a [`Map`].
///
/// Created by the [`Map::edge`] function. See there for more details.
pub struct Edge {
    pub(crate) from: Point,
    pub(crate) to: Point,
    pub(crate) direction: Direction,
    pub(crate) done: bool,
}

impl Iterator for Edge {
    type Item = Point;

    fn next(&mut self) -> Option<Self::Item> {
        if self.done {
            return None;
        }

        let next = self.from;
        self.from += self.direction;
        self.done = next == self.to;

        Some(next)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let size = (self.to - self.from).manhattan() as usize + 1;
        (size, Some(size))
    }
}

impl std::iter::ExactSizeIterator for Edge {}

impl std::iter::DoubleEndedIterator for Edge {
    fn next_back(&mut self) -> Option<Self::Item> {
        if self.done {
            return None;
        }

        let next = self.to;
        self.to += self.direction.reverse();
        self.done = next == self.from;

        Some(next)
    }
}