Skip to content

Commit 8d818a6

Browse files
committed
.
1 parent e74d34a commit 8d818a6

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

packages/solver-r/src/_test_grid_samples.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn grid_from_ascii(ascii: &str) -> Grid<Color> {
88
let width = rows.iter().fold(0, |max_len, r| max_len.max(r.len()));
99
let height = rows.len();
1010

11-
let mut grid = Grid::create(width as u8, height as u8, Color::Empty);
11+
let mut grid = Grid::<Color>::create(width as u8, height as u8);
1212

1313
for x in 0..width {
1414
for y in 0..height {
@@ -118,7 +118,7 @@ pub fn get_grid_sample(g: SampleGrid) -> Grid<Color> {
118118
),
119119

120120
SampleGrid::RandomPack => {
121-
let mut grid = Grid::create(52, 7, Color::Empty);
121+
let mut grid = Grid::<Color>::create(52, 7);
122122
randomly_fill_grid(
123123
&mut grid,
124124
&[Color::Empty, Color::Empty, Color::Color1, Color::Color4],
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use crate::grid::{Color, Grid};
2+
3+
pub fn get_exit_cost_grid(color_grid: &Grid<Color>) -> Grid<u8> {
4+
let mut exit_cost_grid = Grid::<u8>::create(color_grid.width, color_grid.height);
5+
6+
exit_cost_grid
7+
}

packages/solver-r/src/exit_cost_map.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/solver-r/src/grid.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,22 @@ pub enum Color {
3030
Color4 = 4,
3131
}
3232

33+
impl Default for Color {
34+
fn default() -> Self {
35+
Color::Empty
36+
}
37+
}
38+
3339
#[derive(Clone)]
3440
pub struct Grid<T: Copy> {
3541
pub width: u8,
3642
pub height: u8,
3743
pub cells: Vec<T>,
3844
}
39-
impl<T: Copy> Grid<T> {
40-
pub fn create(width: u8, height: u8, item: T) -> Grid<T> {
45+
impl<T: Copy + Default> Grid<T> {
46+
pub fn create(width: u8, height: u8) -> Grid<T> {
4147
let n = (width as usize) * (height as usize);
42-
let cells = (0..n).map(|_| item).collect();
48+
let cells = (0..n).map(|_| T::default()).collect();
4349

4450
Grid {
4551
width,
@@ -51,6 +57,13 @@ impl<T: Copy> Grid<T> {
5157
fn get_index(&self, x: i8, y: i8) -> usize {
5258
return (x as usize) * (self.height as usize) + (y as usize);
5359
}
60+
61+
pub fn fill(&mut self, value: T) -> () {
62+
let n = (self.width as usize) * (self.height as usize);
63+
for i in (0..n) {
64+
self.cells[i] = value;
65+
}
66+
}
5467
pub fn get(&self, p: &Point) -> T {
5568
let i = self.get_index(p.x, p.y);
5669
return self.cells[i];
@@ -95,23 +108,23 @@ fn it_should_sort_cell() {
95108
}
96109
#[test]
97110
fn it_should_grid_create() {
98-
let grid = Grid::create(30, 10, Color::Empty);
111+
let grid = Grid::<Color>::create(30, 10);
99112

100113
assert_eq!(grid.width, 30);
101114
assert_eq!(grid.height, 10);
102115
assert_eq!(grid.get(&Point { x: 2, y: 3 }), Color::Empty);
103116
}
104117
#[test]
105118
fn it_should_grid_setter() {
106-
let mut grid = Grid::create(20, 10, Color::Empty);
119+
let mut grid = Grid::<Color>::create(20, 10);
107120

108121
grid.set(&Point { x: 12, y: 3 }, Color::Color1);
109122

110123
assert_eq!(grid.get(&Point { x: 12, y: 3 }), Color::Color1);
111124
}
112125
#[test]
113126
fn it_should_iterate() {
114-
let grid = Grid::create(2, 2, Color::Empty);
127+
let grid = Grid::<Color>::create(2, 2);
115128

116129
assert_eq!(
117130
grid.iter().collect::<HashSet<_>>(),

packages/solver-r/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod _test_grid_samples;
22
mod astar;
33
mod astar_snake;
4+
mod exit_cost_grid;
45
mod exitable;
56
mod grid;
67
mod snake;

0 commit comments

Comments
 (0)