astar

[Done] Astar Pathing Library for Rust
git clone https://git.jojolepro.com/astar.git
Log | Files | Refs | LICENSE

commit 9d4580bd9509729784cac8d271949fdf193891dc
parent d831d7aa0d2c2cdfabbaef631c0f0f4276aab93a
Author: Joël Lupien (Jojolepro) <jojolepromain@gmail.com>
Date:   Wed, 15 May 2019 12:52:29 -0400

Revert "without generic"

This reverts commit d831d7aa0d2c2cdfabbaef631c0f0f4276aab93a.

Diffstat:
Msrc/lib.rs | 20+++++++++-----------
1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/src/lib.rs b/src/lib.rs @@ -1,5 +1,3 @@ -//! Library providing A* pathfinding utilities. - use hibitset::BitSet; /// A position in the grid. @@ -27,35 +25,35 @@ impl Weighted for f32 { /// # Generics /// B: The type of square of which the map in composed. See the `Weighted` trait for more /// information. -pub struct Map { - map: Vec<f32>, +pub struct Map<B: Weighted> { + map: Vec<B>, size: (usize, usize), } -impl Map { +impl<B: Weighted + Default + Clone> Map<B> { /// Create a new map of the specified size. pub fn new(size_x: usize, size_y: usize) -> Self { Map { - map: vec![1.0; size_x * size_y], + map: vec![B::default(); size_x * size_y], size: (size_x, size_y), } } /// Get the grid at the specified position. - pub fn get_grid(&self, x: usize, y: usize) -> &f32 { + pub fn get_grid(&self, x: usize, y: usize) -> &B { let idx = self.get_index_for_position(x, y); self.map.get(idx).unwrap() } /// Get the grid mutably at the specified position. - pub fn get_grid_mut(&mut self, x: usize, y: usize) -> &mut f32 { + pub fn get_grid_mut(&mut self, x: usize, y: usize) -> &mut B { let idx = self.get_index_for_position(x, y); self.map.get_mut(idx).unwrap() } /// Inserts a grid element at the specified position. - pub fn insert_grid(&mut self, x: usize, y: usize, grid: f32) { + pub fn insert_grid(&mut self, x: usize, y: usize, grid: B) { let idx = self.get_index_for_position(x, y); *self.map.get_mut(idx).unwrap() = grid; } @@ -203,7 +201,7 @@ mod tests { #[test] fn astar() { - let map = Map::new(20, 20); + let map = Map::<f32>::new(20, 20); let from = (2, 4); let to = (2, 7); let shortest = map.shortest_path(from, to).unwrap(); @@ -213,7 +211,7 @@ mod tests { #[should_panic] #[test] fn out_of_bounds() { - let map = Map::new(20, 20); + let map = Map::<f32>::new(20, 20); let from = (2, 4); let to = (2, 77); map.shortest_path(from, to).unwrap();