diff --git a/zed/src/sum_tree/mod.rs b/zed/src/sum_tree/mod.rs index 413f0e7ddc..03a9430ef5 100644 --- a/zed/src/sum_tree/mod.rs +++ b/zed/src/sum_tree/mod.rs @@ -337,7 +337,7 @@ impl SumTree { return Vec::new(); } - let mut replaced = Vec::new(); + let mut removed = Vec::new(); edits.sort_unstable_by_key(|item| item.key()); *self = { @@ -362,7 +362,7 @@ impl SumTree { if let Some(old_item) = old_item { if old_item.key() == new_key { - replaced.push(old_item.clone()); + removed.push(old_item.clone()); cursor.next(); } } @@ -380,7 +380,7 @@ impl SumTree { new_tree }; - replaced + removed } pub fn get(&self, key: &T::Key) -> Option<&T> { @@ -497,6 +497,7 @@ where #[cfg(test)] mod tests { use super::*; + use std::cmp; use std::ops::Add; #[test] @@ -780,11 +781,33 @@ mod tests { assert_eq!(cursor.slice(&Count(6), SeekBias::Right).items(), vec![6]); } + #[test] + fn test_edit() { + let mut tree = SumTree::::new(); + + let removed = tree.edit(vec![Edit::Insert(1), Edit::Insert(2), Edit::Insert(0)]); + assert_eq!(tree.items(), vec![0, 1, 2]); + assert_eq!(removed, Vec::::new()); + assert_eq!(tree.get(&0), Some(&0)); + assert_eq!(tree.get(&1), Some(&1)); + assert_eq!(tree.get(&2), Some(&2)); + assert_eq!(tree.get(&4), None); + + let removed = tree.edit(vec![Edit::Insert(2), Edit::Insert(4), Edit::Remove(0)]); + assert_eq!(tree.items(), vec![1, 2, 4]); + assert_eq!(removed, vec![0, 2]); + assert_eq!(tree.get(&0), None); + assert_eq!(tree.get(&1), Some(&1)); + assert_eq!(tree.get(&2), Some(&2)); + assert_eq!(tree.get(&4), Some(&4)); + } + #[derive(Clone, Default, Debug)] pub struct IntegersSummary { count: Count, sum: Sum, contains_even: bool, + max: u8, } #[derive(Ord, PartialOrd, Default, Eq, PartialEq, Clone, Debug)] @@ -801,15 +824,31 @@ mod tests { count: Count(1), sum: Sum(*self as usize), contains_even: (*self & 1) == 0, + max: *self, } } } + impl KeyedItem for u8 { + type Key = u8; + + fn key(&self) -> Self::Key { + *self + } + } + + impl<'a> Dimension<'a, IntegersSummary> for u8 { + fn add_summary(&mut self, summary: &IntegersSummary) { + *self = summary.max; + } + } + impl<'a> AddAssign<&'a Self> for IntegersSummary { fn add_assign(&mut self, other: &Self) { self.count.0 += &other.count.0; self.sum.0 += &other.sum.0; self.contains_even |= other.contains_even; + self.max = cmp::max(self.max, other.max); } } @@ -819,15 +858,6 @@ mod tests { } } - // impl<'a> Add<&'a Self> for Count { - // type Output = Self; - // - // fn add(mut self, other: &Self) -> Self { - // self.0 += other.0; - // self - // } - // } - impl<'a> Dimension<'a, IntegersSummary> for Sum { fn add_summary(&mut self, summary: &IntegersSummary) { self.0 += summary.sum.0;