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
/// General purpose coordinate
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Default)]
pub struct Coord {
    pub x: i32,
    pub y: i32,
}

impl Coord {
    pub fn new(x: i32, y: i32) -> Self {
        Self { x, y }
    }
}

impl From<(i32, i32)> for Coord {
    fn from((x, y): (i32, i32)) -> Self {
        Coord::new(x, y)
    }
}

impl From<[i32; 2]> for Coord {
    fn from(array: [i32; 2]) -> Self {
        Coord::new(array[0], array[1])
    }
}

/// A size cannot be created which would contain un-addressable cells.
/// That is, the maximum size has a width and height of one greater than the maximum `i32`.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Default)]
pub struct Size {
    x: u32,
    y: u32,
}

impl Size {
    /// Creates a new `Size`.
    /// Panics if `x` or `y` is greater than `::std::i32::MAX as u32 + 1`.
    pub fn new(x: u32, y: u32) -> Self {
        const SIZE_MAX: u32 = ::std::i32::MAX as u32 + 1;
        if x > SIZE_MAX || y > SIZE_MAX {
            panic!("Size is too big: ({}, {})", x, y);
        }
        Self { x, y }
    }

    /// Returns the width.
    pub fn x(&self) -> u32 {
        self.x
    }

    /// Returns the height.
    pub fn y(&self) -> u32 {
        self.y
    }

    /// Returns an iterator over all the coordinates within
    /// a rectangle of this size.
    pub fn coords(&self) -> CoordIter {
        CoordIter::new(*self)
    }

    /// Suppose an array is used to implement a 2D grid of this size,
    /// where traversing the array from start to end is equivalent
    /// to traversing the 2D grid top to bottom, traversing left
    /// to right within each row. If a given coordinate is valid
    /// for such a grid, this function returns the index into the
    /// array corresponding to that coordinate.
    pub fn index(&self, coord: Coord) -> Option<usize> {
        if coord.x < 0 || coord.y < 0 {
            return None;
        }

        let x = coord.x as u32;
        let y = coord.y as u32;

        if x >= self.x || y >= self.y {
            return None;
        }

        Some((y * self.x + x) as usize)
    }

    pub fn coord(&self, index: usize) -> Option<Coord> {
        let y = index / self.x as usize;

        if y >= self.y as usize {
            return None;
        }

        let x = index % self.x as usize;

        Some(Coord::new(x as i32, y as i32))
    }

    /// Return the number of cells in a 2D grid of this size.
    pub fn count(&self) -> usize {
        (self.x * self.y) as usize
    }
}

impl From<(u32, u32)> for Size {
    fn from((x, y): (u32, u32)) -> Self {
        Size::new(x, y)
    }
}

impl From<[u32; 2]> for Size {
    fn from(array: [u32; 2]) -> Self {
        Size::new(array[0], array[1])
    }
}

/// Iterates over all the coordinates in a grid from
/// top to bottom, and left to right within each row.
pub struct CoordIter {
    size: Size,
    coord: Coord,
}

impl CoordIter {
    pub fn new(size: Size) -> Self {
        Self {
            size,
            coord: Coord::new(0, 0),
        }
    }
}

impl Iterator for CoordIter {
    type Item = Coord;
    fn next(&mut self) -> Option<Self::Item> {
        if self.coord.y as u32 == self.size.y {
            return None;
        }

        let coord = self.coord;

        self.coord.x += 1;
        if self.coord.x as u32 == self.size.x {
            self.coord.x = 0;
            self.coord.y += 1;
        }

        Some(coord)
    }
}

#[cfg(test)]
mod test {
    use super::{Coord, Size};

    #[test]
    fn coord_to_index() {
        let size = Size::new(4, 3);

        assert_eq!(size.coord(12), None);
        assert_eq!(size.coord(11), Some(Coord::new(3, 2)));
        assert_eq!(size.coord(0), Some(Coord::new(0, 0)));
        assert_eq!(size.index(Coord::new(0, 0)), Some(0));
    }
}