Fix subtracting

This commit is contained in:
Austen Adler 2022-08-09 17:51:32 -04:00
parent 8a3a77df55
commit d10a02573b

View File

@ -30,46 +30,46 @@ pub struct SelectionWithSubselections {
/// A selection desc that spans only one row /// A selection desc that spans only one row
/// ///
/// This type is required when doing operations that involve multiple lines, but logic cannot exist to see if, for example, 2 selection descs with row:1 col:1-10 and row:2 col:0-1 is adjacent /// This type is required when doing operations that involve multiple lines, but logic cannot exist to see if, for example, 2 selection descs with row:1 col:1-10 and row:2 col:0-1 is adjacent
#[derive(Copy, Clone, PartialEq, PartialOrd, Ord, Eq, Debug)] // #[derive(Copy, Clone, PartialEq, PartialOrd, Ord, Eq, Debug)]
pub struct RowSelectionDesc { // pub struct RowSelectionDesc {
pub row: usize, // pub row: usize,
pub left_col: usize, // pub left_col: usize,
pub right_col: usize, // pub right_col: usize,
} // }
// impl RowSelectionDesc { // impl RowSelectionDesc {
// pub fn // pub fn
// } // }
impl TryFrom<SelectionDesc> for RowSelectionDesc { // impl TryFrom<SelectionDesc> for RowSelectionDesc {
type Error = KakError; // type Error = KakError;
fn try_from(sd: SelectionDesc) -> Result<Self, Self::Error> { // fn try_from(sd: SelectionDesc) -> Result<Self, Self::Error> {
if sd.left.row == sd.right.row { // if sd.left.row == sd.right.row {
Ok(Self { // Ok(Self {
row: sd.left.row, // row: sd.left.row,
left_col: sd.left.col, // left_col: sd.left.col,
right_col: sd.right.col, // right_col: sd.right.col,
}) // })
} else { // } else {
Err(KakError::MultiRowSelectionNotSupported) // Err(KakError::MultiRowSelectionNotSupported)
} // }
} // }
} // }
impl Into<SelectionDesc> for RowSelectionDesc { // impl Into<SelectionDesc> for RowSelectionDesc {
fn into(self) -> SelectionDesc { // fn into(self) -> SelectionDesc {
SelectionDesc { // SelectionDesc {
left: AnchorPosition { // left: AnchorPosition {
row: self.row, // row: self.row,
col: self.left_col, // col: self.left_col,
}, // },
right: AnchorPosition { // right: AnchorPosition {
row: self.row, // row: self.row,
col: self.right_col, // col: self.right_col,
}, // },
} // }
} // }
} // }
#[derive(Clone, PartialEq, PartialOrd, Ord, Eq, Debug)] #[derive(Clone, PartialEq, PartialOrd, Ord, Eq, Debug)]
pub struct SelectionDesc { pub struct SelectionDesc {
@ -162,23 +162,27 @@ impl SelectionDesc {
#[must_use] #[must_use]
pub fn subtract(&self, b: &Self) -> MaybeSplit<Self> { pub fn subtract(&self, b: &Self) -> MaybeSplit<Self> {
let sorted_self = self.sort();
let sorted_b = b.sort(); let sorted_b = b.sort();
match (sorted_b.contains(&self.left), sorted_b.contains(&self.right), self.contains(&sorted_b)) { match (
sorted_b.contains(&sorted_self.left),
sorted_b.contains(&sorted_self.right),
sorted_self.contains(&sorted_b),
) {
(true, true, _) => { (true, true, _) => {
// self is contained by sorted_b // sorted_self is contained by sorted_b
MaybeSplit::Nothing MaybeSplit::Nothing
} }
(false, false, false) => { (false, false, false) => {
// There is no intersection // There is no intersection
// TODO: Why can't I clone myself? MaybeSplit::Just(sorted_self)
MaybeSplit::Just(self.clone())
} }
(false, false, true) => { (false, false, true) => {
// B is contained and it does not intersect with left or right // B is contained and it does not intersect with left or right
MaybeSplit::JustTwo( MaybeSplit::JustTwo(
Self { Self {
left: self.left, left: sorted_self.left,
right: AnchorPosition { right: AnchorPosition {
row: sorted_b.left.row, row: sorted_b.left.row,
col: sorted_b.left.col.saturating_sub(1), col: sorted_b.left.col.saturating_sub(1),
@ -189,24 +193,24 @@ impl SelectionDesc {
row: sorted_b.right.row, row: sorted_b.right.row,
col: sorted_b.right.col.saturating_add(1), col: sorted_b.right.col.saturating_add(1),
}, },
right: self.right, right: sorted_self.right,
}, },
) )
} }
(true, false, _) => { (true, false, _) => {
// Only self's left is contained // Only sorted_self's left is contained
MaybeSplit::Just(Self { MaybeSplit::Just(Self {
left: AnchorPosition { left: AnchorPosition {
row: sorted_b.right.row, row: sorted_b.right.row,
col: sorted_b.right.col.saturating_add(1), col: sorted_b.right.col.saturating_add(1),
}, },
right: self.right, right: sorted_self.right,
}) })
} }
(false, true, _) => { (false, true, _) => {
// Only self's right is contained // Only sorted_self's right is contained
MaybeSplit::Just(Self { MaybeSplit::Just(Self {
left: self.left, left: sorted_self.left,
right: AnchorPosition { right: AnchorPosition {
row: sorted_b.left.row, row: sorted_b.left.row,
col: sorted_b.left.col.saturating_sub(1), col: sorted_b.left.col.saturating_sub(1),
@ -825,73 +829,90 @@ mod test {
// 01234567 // 01234567
// a: ^_^ // a: ^_^
// b: ^____^ // b: ^____^
assert_eq!(sd!(1, 3).subtract(&sd!(0, 5)), MaybeSplit::Nothing); mixed_test!((1, 3), subtract, (0, 5), MaybeSplit::Nothing);
assert_eq!(sd!(1, 3).subtract(&sdr!(0, 5)), MaybeSplit::Nothing); // assert_eq!(sd!(1, 3).subtract(&sd!(0, 5)), MaybeSplit::Nothing);
// assert_eq!(sd!(1, 3).subtract(&sdr!(0, 5)), MaybeSplit::Nothing);
// 01234567 // 01234567
// a: ^__^ // a: ^__^
// b: ^____^ // b: ^____^
assert_eq!(sd!(0, 3).subtract(&sd!(0, 5)), MaybeSplit::Nothing); mixed_test!((0, 3), subtract, (0, 5), MaybeSplit::Nothing);
assert_eq!(sd!(0, 3).subtract(&sdr!(0, 5)), MaybeSplit::Nothing); // assert_eq!(sd!(0, 3).subtract(&sd!(0, 5)), MaybeSplit::Nothing);
// assert_eq!(sd!(0, 3).subtract(&sdr!(0, 5)), MaybeSplit::Nothing);
// 01234567 // 01234567
// a: ^___^ // a: ^___^
// b: ^___^ // b: ^___^
assert_eq!(sd!(1, 5).subtract(&sd!(1, 5)), MaybeSplit::Nothing); mixed_test!((1, 5), subtract, (1, 5), MaybeSplit::Nothing);
assert_eq!(sd!(1, 5).subtract(&sdr!(1, 5)), MaybeSplit::Nothing); // assert_eq!(sd!(1, 5).subtract(&sd!(1, 5)), MaybeSplit::Nothing);
// assert_eq!(sd!(1, 5).subtract(&sdr!(1, 5)), MaybeSplit::Nothing);
// 01234567 // 01234567
// a: ^_____^ // a: ^_____^
// b: ^____^ // b: ^____^
assert_eq!(sd!(0, 6).subtract(&sd!(0, 5)), MaybeSplit::Just(sd!(6, 6))); mixed_test!((0, 6), subtract, (0, 5), MaybeSplit::Just(sd!(6, 6)));
assert_eq!(sd!(0, 6).subtract(&sdr!(0, 5)), MaybeSplit::Just(sd!(6, 6))); // assert_eq!(sd!(0, 6).subtract(&sd!(0, 5)), MaybeSplit::Just(sd!(6, 6)));
// assert_eq!(sd!(0, 6).subtract(&sdr!(0, 5)), MaybeSplit::Just(sd!(6, 6)));
// 01234567 // 01234567
// a: ^____^ // a: ^____^
// b: ^____^ // b: ^____^
assert_eq!(sd!(1, 6).subtract(&sd!(0, 5)), MaybeSplit::Just(sd!(6, 6))); mixed_test!((1, 6), subtract, (0, 5), MaybeSplit::Just(sd!(6, 6)));
assert_eq!(sd!(1, 6).subtract(&sdr!(0, 5)), MaybeSplit::Just(sd!(6, 6))); // assert_eq!(sd!(1, 6).subtract(&sd!(0, 5)), MaybeSplit::Just(sd!(6, 6)));
// assert_eq!(sd!(1, 6).subtract(&sdr!(0, 5)), MaybeSplit::Just(sd!(6, 6)));
// 01234567 // 01234567
// a: ^____^ // a: ^____^
// b: ^____^ // b: ^____^
assert_eq!(sd!(0, 5).subtract(&sd!(1, 6)), MaybeSplit::Just(sd!(0, 0))); mixed_test!((0, 5), subtract, (1, 6), MaybeSplit::Just(sd!(0, 0)));
assert_eq!(sd!(0, 5).subtract(&sdr!(1, 6)), MaybeSplit::Just(sd!(0, 0))); // assert_eq!(sd!(0, 5).subtract(&sd!(1, 6)), MaybeSplit::Just(sd!(0, 0)));
// assert_eq!(sd!(0, 5).subtract(&sdr!(1, 6)), MaybeSplit::Just(sd!(0, 0)));
// 01234567 // 01234567
// a: ^______^ // a: ^______^
// b: ^____^ // b: ^____^
assert_eq!(sd! (0, 7).subtract(&sd!(1, 6)), MaybeSplit::JustTwo(sd!(0, 0), sd!(7, 7)) ); mixed_test!(
assert_eq!(sd! (0, 7).subtract(&sdr!(1, 6)), MaybeSplit::JustTwo(sd!(0, 0), sd!(7, 7)) ); (0, 7),
subtract,
(1, 6),
MaybeSplit::JustTwo(sd!(0, 0), sd!(7, 7))
);
// assert_eq!(sd!(0, 7).subtract(&sd!(1, 6)),MaybeSplit::JustTwo(sd!(0, 0), sd!(7, 7)));
// assert_eq!(sd!(0, 7).subtract(&sdr!(1, 6)),MaybeSplit::JustTwo(sd!(0, 0), sd!(7, 7)));
// 01234567 // 01234567
// a: ^ // a: ^
// b: ^____^ // b: ^____^
assert_eq!(sd!(3, 3).subtract(&sd!(0, 5)), MaybeSplit::Nothing); mixed_test!((3, 3), subtract, (0, 5), MaybeSplit::Nothing);
assert_eq!(sd!(3, 3).subtract(&sdr!(0, 5)), MaybeSplit::Nothing); // assert_eq!(sd!(3, 3).subtract(&sd!(0, 5)), MaybeSplit::Nothing);
// assert_eq!(sd!(3, 3).subtract(&sdr!(0, 5)), MaybeSplit::Nothing);
// 01234567 // 01234567
// a: ^ // a: ^
// b: ^____^ // b: ^____^
assert_eq!(sd!(0, 0).subtract(&sd!(0, 5)), MaybeSplit::Nothing); mixed_test!((0, 0), subtract, (0, 5), MaybeSplit::Nothing);
assert_eq!(sd!(0, 0).subtract(&sdr!(0, 5)), MaybeSplit::Nothing); // assert_eq!(sd!(0, 0).subtract(&sd!(0, 5)), MaybeSplit::Nothing);
// assert_eq!(sd!(0, 0).subtract(&sdr!(0, 5)), MaybeSplit::Nothing);
// 01234567 // 01234567
// a: ^ // a: ^
// b: ^____^ // b: ^____^
assert_eq!(sd!(0, 0).subtract(&sd!(1, 6)), MaybeSplit::Just(sd!(0, 0))); mixed_test!((0, 0), subtract, (1, 6), MaybeSplit::Just(sd!(0, 0)));
assert_eq!(sd!(0, 0).subtract(&sdr!(1, 6)), MaybeSplit::Just(sd!(0, 0))); // assert_eq!(sd!(0, 0).subtract(&sd!(1, 6)), MaybeSplit::Just(sd!(0, 0)));
// assert_eq!(sd!(0, 0).subtract(&sdr!(1, 6)), MaybeSplit::Just(sd!(0, 0)));
// 01234567 // 01234567
// a: ^ // a: ^
// b: ^____^ // b: ^____^
assert_eq!(sd!(5, 5).subtract(&sd!(0, 5)), MaybeSplit::Nothing); mixed_test!((5, 5), subtract, (0, 5), MaybeSplit::Nothing);
assert_eq!(sd!(5, 5).subtract(&sdr!(0, 5)), MaybeSplit::Nothing); // assert_eq!(sd!(5, 5).subtract(&sd!(0, 5)), MaybeSplit::Nothing);
// assert_eq!(sd!(5, 5).subtract(&sdr!(0, 5)), MaybeSplit::Nothing);
// 01234567 // 01234567
// a: ^ // a: ^
// b: ^____^ // b: ^____^
assert_eq!(sd!(6, 6).subtract(&sd!(0, 5)), MaybeSplit::Just(sd!(6, 6))); mixed_test!((6, 6), subtract, (0, 5), MaybeSplit::Just(sd!(6, 6)));
assert_eq!(sd!(6, 6).subtract(&sdr!(0, 5)), MaybeSplit::Just(sd!(6, 6))); // assert_eq!(sd!(6, 6).subtract(&sd!(0, 5)), MaybeSplit::Just(sd!(6, 6)));
// assert_eq!(sd!(6, 6).subtract(&sdr!(0, 5)), MaybeSplit::Just(sd!(6, 6)));
} }
} }