Add type for Pathfinder input

This commit is contained in:
hal8174 2025-01-29 23:05:07 +01:00
parent 08eb0a8e11
commit 00eda50872
4 changed files with 29 additions and 12 deletions

View file

@ -6,6 +6,11 @@ pub mod graph;
pub mod misc;
pub mod priority_queue;
pub struct PathInput<'c, M> {
connections: &'c [Connection],
map: M,
}
pub struct Connection {
pub start_pos: Position,
pub start_dir: Direction,
@ -23,7 +28,12 @@ pub trait Map {
}
pub trait Pathfinder {
fn find_paths(&self, input: &[Connection], map: impl Map) -> Option<Vec<Vec<PathField>>>;
fn find_paths<M: Map>(&self, input: PathInput<'_, M>) -> Option<Vec<Vec<PathField>>>;
}
pub struct SinglePathInput<'c, M> {
connections: &'c [SingleConnection],
map: M,
}
struct SingleConnection {
@ -35,15 +45,16 @@ struct SingleConnection {
}
trait SinglePathfinder {
fn find_paths(&self, input: &[SingleConnection], map: impl Map) -> Option<Vec<Vec<PathField>>>;
fn find_paths<M: Map>(&self, input: SinglePathInput<'_, M>) -> Option<Vec<Vec<PathField>>>;
}
impl<P> Pathfinder for P
where
P: SinglePathfinder,
{
fn find_paths(&self, input: &[Connection], map: impl Map) -> Option<Vec<Vec<PathField>>> {
fn find_paths<M: Map>(&self, input: PathInput<'_, M>) -> Option<Vec<Vec<PathField>>> {
let inner_input = input
.connections
.iter()
.flat_map(|c| {
(0..c.lanes).map(|i| SingleConnection {
@ -60,12 +71,16 @@ where
})
.collect::<Vec<_>>();
let inner_result = self.find_paths(&inner_input, map)?;
let inner_result = self.find_paths(SinglePathInput {
connections: &inner_input,
map: input.map,
})?;
let mut inner_iterator = inner_result.into_iter();
Some(
input
.connections
.iter()
.map(|i| inner_iterator.by_ref().take(i.lanes).flatten().collect())
.collect(),