diff options
Diffstat (limited to 'src/grid.rs')
-rw-r--r-- | src/grid.rs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/grid.rs b/src/grid.rs index b006882..63fab03 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -34,6 +34,13 @@ impl std::ops::Add<Row> for usize { } } +impl std::ops::Add<Row> for Row { + type Output = Row; + fn add(self, other: Row) -> Self::Output { + Row(self.0 + other.0) + } +} + impl std::ops::Add<usize> for Col { type Output = Self; fn add(self, other: usize) -> Self::Output { @@ -48,6 +55,13 @@ impl std::ops::Add<Col> for usize { } } +impl std::ops::Add<Col> for Col { + type Output = Col; + fn add(self, other: Col) -> Self::Output { + Col(self.0 + other.0) + } +} + impl std::ops::Sub<usize> for Row { type Output = Self; fn sub(self, other: usize) -> Self::Output { @@ -168,6 +182,13 @@ impl std::ops::Add<IRow> for isize { } } +impl std::ops::Add<IRow> for IRow { + type Output = IRow; + fn add(self, other: IRow) -> Self::Output { + IRow(self.0 + other.0) + } +} + impl std::ops::Add<isize> for ICol { type Output = Self; fn add(self, other: isize) -> Self::Output { @@ -182,6 +203,13 @@ impl std::ops::Add<ICol> for isize { } } +impl std::ops::Add<ICol> for ICol { + type Output = ICol; + fn add(self, other: ICol) -> Self::Output { + ICol(self.0 + other.0) + } +} + impl std::ops::Sub<isize> for IRow { type Output = Self; fn sub(self, other: isize) -> Self::Output { @@ -210,6 +238,34 @@ impl std::ops::Sub<ICol> for isize { } } +impl std::ops::Mul<isize> for IRow { + type Output = Self; + fn mul(self, other: isize) -> Self::Output { + Self(self.0 * other) + } +} + +impl std::ops::Mul<IRow> for isize { + type Output = IRow; + fn mul(self, other: IRow) -> Self::Output { + IRow(self * other.0) + } +} + +impl std::ops::Mul<isize> for ICol { + type Output = Self; + fn mul(self, other: isize) -> Self::Output { + Self(self.0 * other) + } +} + +impl std::ops::Mul<ICol> for isize { + type Output = ICol; + fn mul(self, other: ICol) -> Self::Output { + ICol(self * other.0) + } +} + #[derive(Default, Clone, Debug, Eq, PartialEq, Hash)] pub struct GridRow<T: Clone + Eq + PartialEq + std::hash::Hash> { cells: Vec<T>, @@ -330,6 +386,24 @@ impl<T: Clone + Eq + PartialEq + std::hash::Hash> Grid<T> { pos: 0, } } + + pub fn flood_fill( + &mut self, + row: Row, + col: Col, + fill: &T, + diagonal: bool, + ) { + let mut todo = vec![(row, col)]; + while let Some((row, col)) = todo.pop() { + self[row][col] = fill.clone(); + for (row, col) in self.adjacent(row, col, diagonal) { + if self[row][col] != *fill { + todo.push((row, col)); + } + } + } + } } impl<T: Default + Clone + Eq + PartialEq + std::hash::Hash> Grid<T> { |