Skip to content

Commit

Permalink
Reorganize traits: supertrait PathContext to reduce duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
leo848 committed Apr 6, 2024
1 parent eb114ec commit d9babeb
Showing 1 changed file with 57 additions and 61 deletions.
118 changes: 57 additions & 61 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = usize>) -> Self::Path;
fn len(&self) -> usize;
fn node_indices(&self) -> Range<usize> {
0..self.len()
}

fn dist(&self, nindex1: usize, nindex2: usize) -> f32;
fn dist_path(&self, path: impl IntoIterator<Item = usize>) -> f32 {
path.into_iter()
Expand All @@ -42,16 +45,40 @@ pub trait CreateContext: Clone {
.expect("node_indices misbehaved")
}
fn send_path(&self, path: impl IntoIterator<Item = usize>, progress: Option<f32>);
}

pub trait CreateContext: PathContext + Clone {
fn send_edges(&self, path: impl IntoIterator<Item = (usize, usize)>, progress: Option<f32>);
fn path_from_indices(&self, path: impl IntoIterator<Item = usize>) -> 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<Item = usize>,
progress: Option<f32>,
);
fn prefer_step(&self) -> bool;
}

impl PathContext for DistPathCreateContext {
type Path = dist_graph::Path;

fn path_from_indices(&self, path: impl IntoIterator<Item = usize>) -> 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()
}
Expand All @@ -65,24 +92,16 @@ impl CreateContext for DistPathCreateContext {
}
}

fn path_from_indices(&self, path: impl IntoIterator<Item = usize>) -> 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<Item = usize>, progress: Option<f32>) {
let mut dpc = DistPathCreation::from_path(self.path_from_indices(path));
if let Some(p) = progress {
dpc = dpc.progress(p)
}
self.action.send(dpc);
}
}

impl CreateContext for DistPathCreateContext {
fn send_edges(&self, edges: impl IntoIterator<Item = (usize, usize)>, progress: Option<f32>) {
let mut dpc = DistPathCreation::from_edges(
edges
Expand Down Expand Up @@ -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<Item = usize>) -> Self::Path {
graph::Path::new(path.into_iter().collect())
}

fn len(&self) -> usize {
self.graph.size()
}
Expand All @@ -134,18 +157,16 @@ impl CreateContext for PathCreateContext {
}
}

fn path_from_indices(&self, path: impl IntoIterator<Item = usize>) -> Self::Path {
graph::Path::new(path.into_iter().collect())
}

fn send_path(&self, path: impl IntoIterator<Item = usize>, progress: Option<f32>) {
let mut pc = PathCreation::from_path(graph::Path::new(path.into_iter().collect_vec()));
if let Some(p) = progress {
pc = pc.progress(p);
}
self.action.send(pc);
}
}

impl CreateContext for PathCreateContext {
fn send_edges(&self, path: impl IntoIterator<Item = (usize, usize)>, progress: Option<f32>) {
let mut pc = PathCreation::from_edges(
path.into_iter()
Expand All @@ -170,42 +191,21 @@ impl CreateContext for PathCreateContext {
}
}

pub trait ImproveContext {
type Path;
fn len(&self) -> usize;
fn node_indices(&self) -> Range<usize> {
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<Item = usize>) -> 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<Item = usize>, progress: Option<f32>);
fn send_path_for_reactivity(
&self,
path: impl IntoIterator<Item = usize>,
progress: Option<f32>,
);
fn path_from_indices(&self, path: impl IntoIterator<Item = usize>) -> 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<Item = usize>) -> 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 {
Expand All @@ -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<Item = usize>,
Expand All @@ -235,22 +237,18 @@ impl ImproveContext for DistPathImproveContext {
self.action.send(dpc);
}

fn path_from_indices(&self, path: impl IntoIterator<Item = usize>) -> 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<Item = usize>) -> Self::Path {
graph::Path::new(path.into_iter().collect())
}

fn len(&self) -> usize {
self.graph.size()
}
Expand All @@ -264,18 +262,16 @@ impl ImproveContext for PathImproveContext {
}
}

fn path_from_indices(&self, path: impl IntoIterator<Item = usize>) -> Self::Path {
graph::Path::new(path.into_iter().collect())
}

fn send_path(&self, path: impl IntoIterator<Item = usize>, progress: Option<f32>) {
let mut pc = PathImprovement::from_path(graph::Path::new(path.into_iter().collect_vec()));
if let Some(p) = progress {
pc = pc.progress(p);
}
self.action.send(pc);
}
}

impl ImproveContext for PathImproveContext {
fn send_path_for_reactivity(
&self,
path: impl IntoIterator<Item = usize>,
Expand Down

0 comments on commit d9babeb

Please sign in to comment.