diff --git a/src/path.rs b/src/path.rs index 319ab24..51b5f30 100644 --- a/src/path.rs +++ b/src/path.rs @@ -17,12 +17,15 @@ use crate::{ const PESSIMAL: bool = false; -pub trait CreateContext: Clone { +pub trait PathContext { type Path; + + fn path_from_indices(&self, path: impl IntoIterator) -> Self::Path; fn len(&self) -> usize; fn node_indices(&self) -> Range { 0..self.len() } + fn dist(&self, nindex1: usize, nindex2: usize) -> f32; fn dist_path(&self, path: impl IntoIterator) -> f32 { path.into_iter() @@ -42,16 +45,40 @@ pub trait CreateContext: Clone { .expect("node_indices misbehaved") } fn send_path(&self, path: impl IntoIterator, progress: Option); +} + +pub trait CreateContext: PathContext + Clone { fn send_edges(&self, path: impl IntoIterator, progress: Option); - fn path_from_indices(&self, path: impl IntoIterator) -> Self::Path; fn rotate_left(self, index: usize) -> Self where Self: Sized; } -impl CreateContext for DistPathCreateContext { +pub trait ImproveContext: PathContext { + fn start_path(&self) -> graph::Path { + graph::Path::new(self.node_indices().collect()) + } + fn send_path_for_reactivity( + &self, + path: impl IntoIterator, + progress: Option, + ); + fn prefer_step(&self) -> bool; +} + +impl PathContext for DistPathCreateContext { type Path = dist_graph::Path; + fn path_from_indices(&self, path: impl IntoIterator) -> Self::Path { + dist_graph::Path::try_new( + path.into_iter() + .map(|idx| self.points[idx].clone()) + .collect(), + self.dim, + ) + .expect("invalid dimension") + } + fn len(&self) -> usize { self.points.len() } @@ -65,16 +92,6 @@ impl CreateContext for DistPathCreateContext { } } - fn path_from_indices(&self, path: impl IntoIterator) -> Self::Path { - dist_graph::Path::try_new( - path.into_iter() - .map(|idx| self.points[idx].clone()) - .collect(), - self.dim, - ) - .expect("invalid dimension") - } - fn send_path(&self, path: impl IntoIterator, progress: Option) { let mut dpc = DistPathCreation::from_path(self.path_from_indices(path)); if let Some(p) = progress { @@ -82,7 +99,9 @@ impl CreateContext for DistPathCreateContext { } self.action.send(dpc); } +} +impl CreateContext for DistPathCreateContext { fn send_edges(&self, edges: impl IntoIterator, progress: Option) { let mut dpc = DistPathCreation::from_edges( edges @@ -118,9 +137,13 @@ impl CreateContext for DistPathCreateContext { } } -impl CreateContext for PathCreateContext { +impl PathContext for PathCreateContext { type Path = graph::Path; + fn path_from_indices(&self, path: impl IntoIterator) -> Self::Path { + graph::Path::new(path.into_iter().collect()) + } + fn len(&self) -> usize { self.graph.size() } @@ -134,10 +157,6 @@ impl CreateContext for PathCreateContext { } } - fn path_from_indices(&self, path: impl IntoIterator) -> Self::Path { - graph::Path::new(path.into_iter().collect()) - } - fn send_path(&self, path: impl IntoIterator, progress: Option) { let mut pc = PathCreation::from_path(graph::Path::new(path.into_iter().collect_vec())); if let Some(p) = progress { @@ -145,7 +164,9 @@ impl CreateContext for PathCreateContext { } self.action.send(pc); } +} +impl CreateContext for PathCreateContext { fn send_edges(&self, path: impl IntoIterator, progress: Option) { let mut pc = PathCreation::from_edges( path.into_iter() @@ -170,42 +191,21 @@ impl CreateContext for PathCreateContext { } } -pub trait ImproveContext { - type Path; - fn len(&self) -> usize; - fn node_indices(&self) -> Range { - 0..self.len() - } - fn start_path(&self) -> graph::Path { - graph::Path::new(self.node_indices().collect()) - } - fn dist(&self, nindex1: usize, nindex2: usize) -> f32; - fn dist_path(&self, path: impl IntoIterator) -> f32 { - path.into_iter() - .tuple_windows() - .map(|(l, r)| self.dist(l, r)) - .sum() - } - fn cost(&self, path: &graph::Path) -> f32 { - self.dist_path(path.iter()).into() - } - fn send_path(&self, path: impl IntoIterator, progress: Option); - fn send_path_for_reactivity( - &self, - path: impl IntoIterator, - progress: Option, - ); - fn path_from_indices(&self, path: impl IntoIterator) -> Self::Path; - fn prefer_step(&self) -> bool; -} - -impl ImproveContext for DistPathImproveContext { +impl PathContext for DistPathImproveContext { type Path = dist_graph::Path; fn len(&self) -> usize { self.path.len() } + fn path_from_indices(&self, path: impl IntoIterator) -> Self::Path { + dist_graph::Path::try_new( + path.into_iter().map(|idx| self.path[idx].clone()).collect(), + self.dim, + ) + .expect("invalid dimension") + } + fn dist(&self, nindex1: usize, nindex2: usize) -> f32 { let value = self.graph.weight(nindex1, nindex2).into_inner(); if PESSIMAL { @@ -222,7 +222,9 @@ impl ImproveContext for DistPathImproveContext { } self.action.send(dpc); } +} +impl ImproveContext for DistPathImproveContext { fn send_path_for_reactivity( &self, path: impl IntoIterator, @@ -235,22 +237,18 @@ impl ImproveContext for DistPathImproveContext { self.action.send(dpc); } - fn path_from_indices(&self, path: impl IntoIterator) -> Self::Path { - dist_graph::Path::try_new( - path.into_iter().map(|idx| self.path[idx].clone()).collect(), - self.dim, - ) - .expect("invalid dimension") - } - fn prefer_step(&self) -> bool { self.prefer_step } } -impl ImproveContext for PathImproveContext { +impl PathContext for PathImproveContext { type Path = graph::Path; + fn path_from_indices(&self, path: impl IntoIterator) -> Self::Path { + graph::Path::new(path.into_iter().collect()) + } + fn len(&self) -> usize { self.graph.size() } @@ -264,10 +262,6 @@ impl ImproveContext for PathImproveContext { } } - fn path_from_indices(&self, path: impl IntoIterator) -> Self::Path { - graph::Path::new(path.into_iter().collect()) - } - fn send_path(&self, path: impl IntoIterator, progress: Option) { let mut pc = PathImprovement::from_path(graph::Path::new(path.into_iter().collect_vec())); if let Some(p) = progress { @@ -275,7 +269,9 @@ impl ImproveContext for PathImproveContext { } self.action.send(pc); } +} +impl ImproveContext for PathImproveContext { fn send_path_for_reactivity( &self, path: impl IntoIterator,