Skip to content

Instantly share code, notes, and snippets.

@AnthonyMikh
Last active May 16, 2022 22:00
Show Gist options
  • Select an option

  • Save AnthonyMikh/a50a5a00c1844d9c5ad19c900b79d7f5 to your computer and use it in GitHub Desktop.

Select an option

Save AnthonyMikh/a50a5a00c1844d9c5ad19c900b79d7f5 to your computer and use it in GitHub Desktop.

Revisions

  1. AnthonyMikh revised this gist May 16, 2022. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion lib.rs
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ impl Bounds {
    put(row, col + 1);
    }
    if row + 1 < self.rows {
    put(row, col + 1);
    put(row + 1, col);
    }
    &buf[..i]
    }
    @@ -54,6 +54,7 @@ impl Bounds {
    put(row, col + 1);
    }
    if row + 1 < self.rows {
    let row = row + 1;
    if let Some(col) = col.checked_sub(1) {
    put(row, col);
    }
  2. AnthonyMikh revised this gist May 16, 2022. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions lib.rs
    Original file line number Diff line number Diff line change
    @@ -24,10 +24,10 @@ impl Bounds {
    put(row, col);
    }
    if col + 1 < self.cols {
    put(row, col);
    put(row, col + 1);
    }
    if row + 1 < self.rows {
    put(row, col);
    put(row, col + 1);
    }
    &buf[..i]
    }
    @@ -44,22 +44,22 @@ impl Bounds {
    }
    put(row, col);
    if col + 1 < self.cols {
    put(row, col);
    put(row, col + 1);
    }
    }
    if let Some(col) = col.checked_sub(1) {
    put(row, col);
    }
    if col + 1 < self.cols {
    put(row, col);
    put(row, col + 1);
    }
    if row + 1 < self.rows {
    if let Some(col) = col.checked_sub(1) {
    put(row, col);
    }
    put(row, col);
    if col + 1 < self.cols {
    put(row, col);
    put(row, col + 1);
    }
    }
    &buf[..i]
  3. AnthonyMikh created this gist Apr 29, 2022.
    67 changes: 67 additions & 0 deletions lib.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    #[derive(Clone, Copy)]
    pub struct Bounds {
    rows: usize,
    cols: usize,
    }

    type Pos = (usize, usize);

    impl Bounds {
    pub fn around_neumann(
    self,
    buf: &mut [Pos; 4],
    (row, col): Pos
    ) -> &[Pos] {
    let mut i = 0;
    let mut put = |r, c| {
    buf[i] = (r, c);
    i += 1;
    };
    if let Some(row) = row.checked_sub(1) {
    put(row, col);
    }
    if let Some(col) = col.checked_sub(1) {
    put(row, col);
    }
    if col + 1 < self.cols {
    put(row, col);
    }
    if row + 1 < self.rows {
    put(row, col);
    }
    &buf[..i]
    }

    pub fn around_moore(self, buf: &mut [Pos; 8], (row, col): Pos) -> &[Pos] {
    let mut i = 0;
    let mut put = |r, c| {
    buf[i] = (r, c);
    i += 1;
    };
    if let Some(row) = row.checked_sub(1) {
    if let Some(col) = col.checked_sub(1) {
    put(row, col);
    }
    put(row, col);
    if col + 1 < self.cols {
    put(row, col);
    }
    }
    if let Some(col) = col.checked_sub(1) {
    put(row, col);
    }
    if col + 1 < self.cols {
    put(row, col);
    }
    if row + 1 < self.rows {
    if let Some(col) = col.checked_sub(1) {
    put(row, col);
    }
    put(row, col);
    if col + 1 < self.cols {
    put(row, col);
    }
    }
    &buf[..i]
    }
    }