use indicatrix_cut_core::Edit;
use std::collections::BTreeSet;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Shift {
Insert(usize),
Remove(usize),
Move { from: usize, to: usize },
}
impl Shift {
const fn apply(self, index: usize) -> Option<usize> {
match self {
Self::Insert(at) => Some(if index >= at { index + 1 } else { index }),
Self::Remove(at) => {
if index == at {
None
} else if index > at {
Some(index - 1)
} else {
Some(index)
}
}
Self::Move { from, to } => {
if index == from {
return Some(to);
}
let without_moved = if index > from { index - 1 } else { index };
Some(if without_moved >= to {
without_moved + 1
} else {
without_moved
})
}
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct TierIndexMap {
shifts: Vec<Shift>,
}
impl TierIndexMap {
#[must_use]
pub fn of(edit: &Edit) -> Self {
let mut shifts = Vec::new();
collect_shifts(edit, &mut shifts);
Self { shifts }
}
#[must_use]
pub fn map(&self, index: usize) -> Option<usize> {
self.shifts
.iter()
.try_fold(index, |current, shift| shift.apply(current))
}
#[must_use]
pub fn remap(&self, selection: &BTreeSet<usize>) -> BTreeSet<usize> {
selection
.iter()
.filter_map(|&index| self.map(index))
.collect()
}
}
fn collect_shifts(edit: &Edit, shifts: &mut Vec<Shift>) {
match edit {
Edit::AddTier { index, .. } => shifts.push(Shift::Insert(*index)),
Edit::RemoveTier { index } => shifts.push(Shift::Remove(*index)),
Edit::MoveTier { from, to } => shifts.push(Shift::Move {
from: *from,
to: *to,
}),
Edit::Batch(edits) => {
for sub_edit in edits {
collect_shifts(sub_edit, shifts);
}
}
_ => {}
}
}
#[cfg(test)]
mod tests {
use super::*;
use indicatrix::geometry::meet_solver::MeetConstraint;
use indicatrix_cut_core::ConstraintTier;
fn tier() -> ConstraintTier {
ConstraintTier {
angle_deg: -40.0,
name: String::new(),
indices: Vec::new(),
constraint: MeetConstraint::MeetExisting,
imported_meet: None,
original_notes: None,
detached: Vec::new(),
}
}
#[test]
fn an_insertion_pushes_the_rows_at_and_after_it_down() {
let map = TierIndexMap::of(&Edit::AddTier {
index: 2,
tier: tier(),
});
assert_eq!(
[0, 1, 2, 3].map(|index| map.map(index)),
[Some(0), Some(1), Some(3), Some(4)]
);
}
#[test]
fn a_removal_drops_its_row_and_pulls_later_rows_up() {
let map = TierIndexMap::of(&Edit::RemoveTier { index: 1 });
assert_eq!(
[0, 1, 2, 3].map(|index| map.map(index)),
[Some(0), None, Some(1), Some(2)]
);
}
#[test]
fn a_move_follows_remove_then_insert() {
let down = TierIndexMap::of(&Edit::MoveTier { from: 0, to: 2 });
assert_eq!(
[0, 1, 2, 3, 4].map(|index| down.map(index)),
[Some(2), Some(0), Some(1), Some(3), Some(4)]
);
let up = TierIndexMap::of(&Edit::MoveTier { from: 3, to: 1 });
assert_eq!(
[0, 1, 2, 3, 4].map(|index| up.map(index)),
[Some(0), Some(2), Some(3), Some(1), Some(4)]
);
}
#[test]
fn a_batch_composes_its_parts_in_order() {
let undo_of_removal = Edit::Batch(vec![
Edit::AddTier {
index: 1,
tier: tier(),
},
Edit::RestoreTierId {
index: 1,
id: indicatrix_cut_core::TierId(9),
},
]);
let map = TierIndexMap::of(&undo_of_removal);
assert_eq!(
[0, 1, 2].map(|index| map.map(index)),
[Some(0), Some(2), Some(3)]
);
}
#[test]
fn edits_that_do_not_move_rows_leave_the_selection_alone() {
let map = TierIndexMap::of(&Edit::RetargetAngles {
changes: vec![(1, -40.0, -41.0)],
});
let selection = BTreeSet::from([0, 2, 4]);
assert_eq!(map.remap(&selection), selection);
}
#[test]
fn remapping_a_selection_drops_removed_rows_and_renumbers_the_rest() {
let map = TierIndexMap::of(&Edit::RemoveTier { index: 0 });
assert_eq!(
map.remap(&BTreeSet::from([0, 1])),
BTreeSet::from([0]),
"row 0 is gone and row 1 is now row 0"
);
}
}