Simplify shared Grid

This commit is contained in:
Timothy Warren 2022-12-20 09:39:55 -05:00
parent 6941e0024c
commit d432d855e6
3 changed files with 32 additions and 48 deletions

View File

@ -6,34 +6,52 @@ pub struct Grid<T> {
pub vec: Vec<T>, pub vec: Vec<T>,
} }
pub trait Grid2d<T> { impl<T> Grid<T> {
fn new(width: usize) -> Self; pub fn new(width: usize) -> Self {
Grid {
width,
vec: Vec::new(),
}
}
fn len(&self) -> usize; pub fn len(&self) -> usize {
self.vec.len()
}
fn num_cols(&self) -> usize; pub fn num_cols(&self) -> usize {
self.width
}
fn num_rows(&self) -> usize { pub fn num_rows(&self) -> usize {
self.len() / self.num_cols() self.len() / self.num_cols()
} }
// Convert x,y coordinate into linear array index // Convert x,y coordinate into linear array index
fn xy_idx(&self, x: usize, y: usize) -> usize { pub fn xy_idx(&self, x: usize, y: usize) -> usize {
(y * self.num_cols()) + x (y * self.num_cols()) + x
} }
/// Convert linear array index to x,y coordinate /// Convert linear array index to x,y coordinate
fn idx_xy(&self, idx: usize) -> (usize, usize) { pub fn idx_xy(&self, idx: usize) -> (usize, usize) {
(idx % self.num_cols(), idx / self.num_cols()) (idx % self.num_cols(), idx / self.num_cols())
} }
fn get(&self, idx: usize) -> Option<&T>; pub fn get(&self, idx: usize) -> Option<&T> {
self.vec.get(idx)
}
fn get_mut(&mut self, idx: usize) -> Option<&mut T>; pub fn get_mut(&mut self, idx: usize) -> Option<&mut T> {
self.vec.get_mut(idx)
}
fn get_row(&mut self, row_num: usize) -> &mut [T]; pub fn get_row(&mut self, row_num: usize) -> &mut [T] {
let start = self.row_first_idx(row_num);
let end = self.row_last_idx(row_num);
fn row_first_idx(&self, row: usize) -> usize { &mut self.vec[start..=end]
}
pub fn row_first_idx(&self, row: usize) -> usize {
let idx = row * self.num_cols(); let idx = row * self.num_cols();
if idx < self.len() { if idx < self.len() {
@ -43,7 +61,7 @@ pub trait Grid2d<T> {
} }
} }
fn row_last_idx(&self, row: usize) -> usize { pub fn row_last_idx(&self, row: usize) -> usize {
if (row + 1) > self.num_rows() { if (row + 1) > self.num_rows() {
return self.len(); return self.len();
} }
@ -51,11 +69,11 @@ pub trait Grid2d<T> {
self.row_first_idx(row + 1) - 1 self.row_first_idx(row + 1) - 1
} }
fn get_row_indexes(&self, row_num: usize) -> Vec<usize> { pub fn get_row_indexes(&self, row_num: usize) -> Vec<usize> {
(self.row_first_idx(row_num)..=self.row_last_idx(row_num)).collect() (self.row_first_idx(row_num)..=self.row_last_idx(row_num)).collect()
} }
fn get_column_indexes(&self, col_num: usize) -> Vec<usize> { pub fn get_column_indexes(&self, col_num: usize) -> Vec<usize> {
let mut indexes = Vec::new(); let mut indexes = Vec::new();
if col_num >= self.num_cols() { if col_num >= self.num_cols() {
@ -75,38 +93,6 @@ pub trait Grid2d<T> {
} }
} }
impl<T> Grid2d<T> for Grid<T> {
fn new(width: usize) -> Self {
Grid {
width,
vec: Vec::new(),
}
}
fn len(&self) -> usize {
self.vec.len()
}
fn num_cols(&self) -> usize {
self.width
}
fn get(&self, idx: usize) -> Option<&T> {
self.vec.get(idx)
}
fn get_mut(&mut self, idx: usize) -> Option<&mut T> {
self.vec.get_mut(idx)
}
fn get_row(&mut self, row_num: usize) -> &mut [T] {
let start = self.row_first_idx(row_num);
let end = self.row_last_idx(row_num);
&mut self.vec[start..=end]
}
}
#[macro_export] #[macro_export]
/// Simplifies newtype wrapping of the `Grid` struct /// Simplifies newtype wrapping of the `Grid` struct
macro_rules! impl_grid_newtype { macro_rules! impl_grid_newtype {

View File

@ -1,6 +1,5 @@
use aoc_shared::enums::Direction; use aoc_shared::enums::Direction;
use aoc_shared::grid::Grid as BaseGrid; use aoc_shared::grid::Grid as BaseGrid;
use aoc_shared::grid::Grid2d;
use aoc_shared::impl_grid_newtype; use aoc_shared::impl_grid_newtype;
#[derive(Debug)] #[derive(Debug)]

View File

@ -1,6 +1,5 @@
use aoc_shared::impl_grid_newtype; use aoc_shared::impl_grid_newtype;
use aoc_shared::grid::Grid as BaseGrid; use aoc_shared::grid::Grid as BaseGrid;
use aoc_shared::grid::Grid2d;
use std::collections::HashSet; use std::collections::HashSet;
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Eq, Hash)] #[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Eq, Hash)]