refactor(autorouter/autoroute): Move permuting to new trait

This commit is contained in:
Mikolaj Wielgus 2025-09-01 15:17:48 +02:00
parent 8fb94c96ff
commit 3cc2315ebe
2 changed files with 30 additions and 17 deletions

View File

@ -20,7 +20,7 @@ use crate::{
router::{ router::{
navcord::Navcord, navmesh::Navmesh, thetastar::ThetastarStepper, RouteStepper, Router, navcord::Navcord, navmesh::Navmesh, thetastar::ThetastarStepper, RouteStepper, Router,
}, },
stepper::{Abort, EstimateProgress, Step}, stepper::{Abort, EstimateProgress, Permute, Step},
}; };
use super::{ use super::{
@ -87,22 +87,6 @@ impl AutorouteExecutionStepper {
}) })
} }
fn permute(
&mut self,
autorouter: &mut Autorouter<impl AccessMesadata>,
permutation: Vec<RatlineIndex>,
) -> Result<(), AutorouterError> {
let new_index = permutation
.iter()
.zip(self.ratlines.iter())
.position(|(permuted, original)| *permuted != *original)
.unwrap();
self.ratlines = permutation;
self.backtrace_to_index(autorouter, new_index)?;
Ok(())
}
fn backtrace_to_index( fn backtrace_to_index(
&mut self, &mut self,
autorouter: &mut Autorouter<impl AccessMesadata>, autorouter: &mut Autorouter<impl AccessMesadata>,
@ -236,6 +220,27 @@ impl<M: AccessMesadata> Abort<Autorouter<M>> for AutorouteExecutionStepper {
} }
} }
impl<M: AccessMesadata> Permute<Autorouter<M>> for AutorouteExecutionStepper {
type Index = RatlineIndex;
type Output = Result<(), AutorouterError>;
fn permute(
&mut self,
autorouter: &mut Autorouter<M>,
permutation: Vec<RatlineIndex>,
) -> Result<(), AutorouterError> {
let new_index = permutation
.iter()
.zip(self.ratlines.iter())
.position(|(permuted, original)| *permuted != *original)
.unwrap();
self.ratlines = permutation;
self.backtrace_to_index(autorouter, new_index)?;
Ok(())
}
}
impl EstimateProgress for AutorouteExecutionStepper { impl EstimateProgress for AutorouteExecutionStepper {
type Value = f64; type Value = f64;

View File

@ -53,6 +53,14 @@ pub trait Abort<Ctx> {
fn abort(&mut self, context: &mut Ctx); fn abort(&mut self, context: &mut Ctx);
} }
/// Some steppers may be permuted from their initial order.
pub trait Permute<Ctx> {
type Index;
type Output;
fn permute(&mut self, context: &mut Ctx, ordering: Vec<Self::Index>) -> Self::Output;
}
/// Steppers that can receive discrete events and act on them implement this /// Steppers that can receive discrete events and act on them implement this
/// trait. /// trait.
// XXX: Doesn't this violate the rule that stepper's future states are // XXX: Doesn't this violate the rule that stepper's future states are