refactor(geometry/edit): "merge" instead of "apply" edit on another edit

This is just a small terminological distinction.
This commit is contained in:
Mikolaj Wielgus 2025-05-25 18:39:49 +02:00
parent e92864d58b
commit 8647df026d
2 changed files with 36 additions and 48 deletions

View File

@ -773,8 +773,9 @@ impl<CW: Clone, Cel: Copy, R: AccessRules> Drawing<CW, Cel, R> {
) -> Result<(), DrawingException> { ) -> Result<(), DrawingException> {
let mut temp_recorder = DrawingEdit::new(); let mut temp_recorder = DrawingEdit::new();
let ret = self.update_this_and_outward_bows_intern(&mut temp_recorder, around); let ret = self.update_this_and_outward_bows_intern(&mut temp_recorder, around);
if ret.is_ok() { if ret.is_ok() {
recorder.apply(&temp_recorder); recorder.merge(temp_recorder);
} else { } else {
temp_recorder.reverse_inplace(); temp_recorder.reverse_inplace();
self.recording_geometry_with_rtree.apply(&temp_recorder); self.recording_geometry_with_rtree.apply(&temp_recorder);

View File

@ -2,7 +2,7 @@
// //
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
use std::collections::BTreeMap; use std::collections::{btree_map::Entry, BTreeMap};
use crate::graph::{GenericIndex, GetPetgraphIndex}; use crate::graph::{GenericIndex, GetPetgraphIndex};
@ -32,10 +32,6 @@ pub struct GeometryEdit<DW, SW, BW, CW, Cel, PI, DI, SI, BI> {
BTreeMap<GenericIndex<CW>, (Option<(Vec<(Cel, PI)>, CW)>, Option<(Vec<(Cel, PI)>, CW)>)>, BTreeMap<GenericIndex<CW>, (Option<(Vec<(Cel, PI)>, CW)>, Option<(Vec<(Cel, PI)>, CW)>)>,
} }
fn swap_tuple_inplace<D>(x: &mut (D, D)) {
core::mem::swap(&mut x.0, &mut x.1);
}
impl< impl<
DW: AccessDotWeight + GetLayer, DW: AccessDotWeight + GetLayer,
SW: AccessSegWeight + GetLayer, SW: AccessSegWeight + GetLayer,
@ -57,25 +53,17 @@ impl<
} }
} }
pub fn reverse_inplace(&mut self) { pub fn merge(&mut self, edit: GeometryEdit<DW, SW, BW, CW, Cel, PI, DI, SI, BI>) {
self.dots.values_mut().for_each(swap_tuple_inplace); Self::merge_btmap(&mut self.dots, &edit.dots);
self.segs.values_mut().for_each(swap_tuple_inplace); Self::merge_btmap(&mut self.segs, &edit.segs);
self.bends.values_mut().for_each(swap_tuple_inplace); Self::merge_btmap(&mut self.bends, &edit.bends);
self.compounds.values_mut().for_each(swap_tuple_inplace); Self::merge_btmap(&mut self.compounds, &edit.compounds);
} }
pub fn reverse(&self) -> Self { fn merge_btmap<I: Copy + Eq + Ord, D: Clone>(
let mut rev = self.clone();
rev.reverse_inplace();
rev
}
}
fn apply_btmap<I: Copy + Eq + Ord, D: Clone>(
main: &mut BTreeMap<I, (Option<D>, Option<D>)>, main: &mut BTreeMap<I, (Option<D>, Option<D>)>,
edit: &BTreeMap<I, (Option<D>, Option<D>)>, edit: &BTreeMap<I, (Option<D>, Option<D>)>,
) { ) {
use std::collections::btree_map::Entry;
for (index, (old, new)) in edit { for (index, (old, new)) in edit {
match main.entry(*index) { match main.entry(*index) {
Entry::Vacant(vac) => { Entry::Vacant(vac) => {
@ -88,23 +76,22 @@ fn apply_btmap<I: Copy + Eq + Ord, D: Clone>(
} }
} }
impl< pub fn reverse_inplace(&mut self) {
DW: AccessDotWeight + GetLayer, self.dots.values_mut().for_each(Self::swap_tuple_inplace);
SW: AccessSegWeight + GetLayer, self.segs.values_mut().for_each(Self::swap_tuple_inplace);
BW: AccessBendWeight + GetLayer, self.bends.values_mut().for_each(Self::swap_tuple_inplace);
CW: Clone, self.compounds
Cel: Copy, .values_mut()
PI: GetPetgraphIndex + TryInto<DI> + TryInto<SI> + TryInto<BI> + Eq + Ord + Copy, .for_each(Self::swap_tuple_inplace);
DI: GetPetgraphIndex + Into<PI> + Eq + Ord + Copy, }
SI: GetPetgraphIndex + Into<PI> + Eq + Ord + Copy,
BI: GetPetgraphIndex + Into<PI> + Eq + Ord + Copy, fn swap_tuple_inplace<D>(x: &mut (D, D)) {
> ApplyGeometryEdit<DW, SW, BW, CW, Cel, PI, DI, SI, BI> core::mem::swap(&mut x.0, &mut x.1);
for GeometryEdit<DW, SW, BW, CW, Cel, PI, DI, SI, BI> }
{
fn apply(&mut self, edit: &GeometryEdit<DW, SW, BW, CW, Cel, PI, DI, SI, BI>) { pub fn reverse(&self) -> Self {
apply_btmap(&mut self.dots, &edit.dots); let mut rev = self.clone();
apply_btmap(&mut self.segs, &edit.segs); rev.reverse_inplace();
apply_btmap(&mut self.bends, &edit.bends); rev
apply_btmap(&mut self.compounds, &edit.compounds);
} }
} }