feat(geometry/edit): impl ApplyGeometryEdit for GeometryEdit

This commit is contained in:
Ellen Emilia Anna Zscheile 2025-03-24 14:19:50 +01:00 committed by mikolaj
parent e20c9aa3d6
commit cdee22d63e
1 changed files with 51 additions and 26 deletions

View File

@ -33,6 +33,10 @@ pub struct GeometryEdit<PW, DW, SW, BW, CW, PI, DI, SI, BI> {
primitive_weight_marker: PhantomData<PW>, primitive_weight_marker: PhantomData<PW>,
} }
fn swap_tuple_inplace<D>(x: &mut (D, D)) {
core::mem::swap(&mut x.0, &mut x.1);
}
impl< impl<
PW: GetWidth + GetLayer + TryInto<DW> + TryInto<SW> + TryInto<BW> + Retag<Index = PI> + Copy, PW: GetWidth + GetLayer + TryInto<DW> + TryInto<SW> + TryInto<BW> + Retag<Index = PI> + Copy,
DW: AccessDotWeight + Into<PW> + GetLayer, DW: AccessDotWeight + Into<PW> + GetLayer,
@ -55,33 +59,54 @@ impl<
} }
} }
pub fn reverse_inplace(&mut self) {
self.dots.values_mut().for_each(swap_tuple_inplace);
self.segs.values_mut().for_each(swap_tuple_inplace);
self.bends.values_mut().for_each(swap_tuple_inplace);
self.compounds.values_mut().for_each(swap_tuple_inplace);
}
pub fn reverse(&self) -> Self { pub fn reverse(&self) -> Self {
Self { let mut rev = self.clone();
dots: self rev.reverse_inplace();
.dots rev
.clone() }
.into_iter() }
.map(|(k, v)| (k, (v.1, v.0)))
.collect(), fn apply_btmap<I: Copy + Eq + Ord, D: Clone>(
segs: self main: &mut BTreeMap<I, (Option<D>, Option<D>)>,
.segs edit: &BTreeMap<I, (Option<D>, Option<D>)>,
.clone() ) {
.into_iter() use std::collections::btree_map::Entry;
.map(|(k, v)| (k, (v.1, v.0))) for (index, (old, new)) in edit {
.collect(), match main.entry(*index) {
bends: self Entry::Vacant(vac) => {
.bends vac.insert((old.clone(), new.clone()));
.clone() }
.into_iter() Entry::Occupied(mut occ) => {
.map(|(k, v)| (k, (v.1, v.0))) occ.get_mut().1 = new.clone();
.collect(), }
compounds: self
.compounds
.clone()
.into_iter()
.map(|(k, v)| (k, (v.1, v.0)))
.collect(),
primitive_weight_marker: PhantomData,
} }
} }
} }
impl<
PW: GetWidth + GetLayer + TryInto<DW> + TryInto<SW> + TryInto<BW> + Retag<Index = PI> + Copy,
DW: AccessDotWeight + Into<PW> + GetLayer,
SW: AccessSegWeight + Into<PW> + GetLayer,
BW: AccessBendWeight + Into<PW> + GetLayer,
CW: Copy,
PI: GetPetgraphIndex + TryInto<DI> + TryInto<SI> + TryInto<BI> + Eq + Ord + Copy,
DI: GetPetgraphIndex + Into<PI> + Eq + Ord + Copy,
SI: GetPetgraphIndex + Into<PI> + Eq + Ord + Copy,
BI: GetPetgraphIndex + Into<PI> + Eq + Ord + Copy,
> ApplyGeometryEdit<PW, DW, SW, BW, CW, PI, DI, SI, BI>
for GeometryEdit<PW, DW, SW, BW, CW, PI, DI, SI, BI>
{
fn apply(&mut self, edit: &GeometryEdit<PW, DW, SW, BW, CW, PI, DI, SI, BI>) {
apply_btmap(&mut self.dots, &edit.dots);
apply_btmap(&mut self.segs, &edit.segs);
apply_btmap(&mut self.bends, &edit.bends);
apply_btmap(&mut self.compounds, &edit.compounds);
}
}