mirror of https://codeberg.org/topola/topola.git
refactor(geometry/edit): "merge" instead of "apply" edit on another edit
This is just a small terminological distinction.
This commit is contained in:
parent
e92864d58b
commit
8647df026d
|
|
@ -773,8 +773,9 @@ impl<CW: Clone, Cel: Copy, R: AccessRules> Drawing<CW, Cel, R> {
|
|||
) -> Result<(), DrawingException> {
|
||||
let mut temp_recorder = DrawingEdit::new();
|
||||
let ret = self.update_this_and_outward_bows_intern(&mut temp_recorder, around);
|
||||
|
||||
if ret.is_ok() {
|
||||
recorder.apply(&temp_recorder);
|
||||
recorder.merge(temp_recorder);
|
||||
} else {
|
||||
temp_recorder.reverse_inplace();
|
||||
self.recording_geometry_with_rtree.apply(&temp_recorder);
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
//
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::{btree_map::Entry, BTreeMap};
|
||||
|
||||
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)>)>,
|
||||
}
|
||||
|
||||
fn swap_tuple_inplace<D>(x: &mut (D, D)) {
|
||||
core::mem::swap(&mut x.0, &mut x.1);
|
||||
}
|
||||
|
||||
impl<
|
||||
DW: AccessDotWeight + GetLayer,
|
||||
SW: AccessSegWeight + GetLayer,
|
||||
|
|
@ -57,25 +53,17 @@ 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 merge(&mut self, edit: GeometryEdit<DW, SW, BW, CW, Cel, PI, DI, SI, BI>) {
|
||||
Self::merge_btmap(&mut self.dots, &edit.dots);
|
||||
Self::merge_btmap(&mut self.segs, &edit.segs);
|
||||
Self::merge_btmap(&mut self.bends, &edit.bends);
|
||||
Self::merge_btmap(&mut self.compounds, &edit.compounds);
|
||||
}
|
||||
|
||||
pub fn reverse(&self) -> Self {
|
||||
let mut rev = self.clone();
|
||||
rev.reverse_inplace();
|
||||
rev
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_btmap<I: Copy + Eq + Ord, D: Clone>(
|
||||
fn merge_btmap<I: Copy + Eq + Ord, D: Clone>(
|
||||
main: &mut 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 {
|
||||
match main.entry(*index) {
|
||||
Entry::Vacant(vac) => {
|
||||
|
|
@ -88,23 +76,22 @@ fn apply_btmap<I: Copy + Eq + Ord, D: Clone>(
|
|||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
DW: AccessDotWeight + GetLayer,
|
||||
SW: AccessSegWeight + GetLayer,
|
||||
BW: AccessBendWeight + GetLayer,
|
||||
CW: Clone,
|
||||
Cel: 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<DW, SW, BW, CW, Cel, PI, DI, SI, BI>
|
||||
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>) {
|
||||
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);
|
||||
pub fn reverse_inplace(&mut self) {
|
||||
self.dots.values_mut().for_each(Self::swap_tuple_inplace);
|
||||
self.segs.values_mut().for_each(Self::swap_tuple_inplace);
|
||||
self.bends.values_mut().for_each(Self::swap_tuple_inplace);
|
||||
self.compounds
|
||||
.values_mut()
|
||||
.for_each(Self::swap_tuple_inplace);
|
||||
}
|
||||
|
||||
fn swap_tuple_inplace<D>(x: &mut (D, D)) {
|
||||
core::mem::swap(&mut x.0, &mut x.1);
|
||||
}
|
||||
|
||||
pub fn reverse(&self) -> Self {
|
||||
let mut rev = self.clone();
|
||||
rev.reverse_inplace();
|
||||
rev
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue