From 173f99748dbf6a73c88ed4a5fbab2a0f2a037d8c Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 28 May 2021 15:24:33 -0700 Subject: [PATCH] Rename context parameters to `cx` in sum_tree --- zed/src/sum_tree.rs | 90 +++++++++++++++++++------------------- zed/src/sum_tree/cursor.rs | 55 +++++++++++------------ 2 files changed, 70 insertions(+), 75 deletions(-) diff --git a/zed/src/sum_tree.rs b/zed/src/sum_tree.rs index 90cffaebd6..8a2f9eb475 100644 --- a/zed/src/sum_tree.rs +++ b/zed/src/sum_tree.rs @@ -25,7 +25,7 @@ pub trait KeyedItem: Item { pub trait Summary: Default + Clone + fmt::Debug { type Context; - fn add_summary(&mut self, summary: &Self, ctx: &Self::Context); + fn add_summary(&mut self, summary: &Self, cx: &Self::Context); } pub trait Dimension<'a, S: Summary>: Clone + fmt::Debug + Default { @@ -37,7 +37,7 @@ impl<'a, T: Summary> Dimension<'a, T> for () { } pub trait SeekDimension<'a, T: Summary>: Dimension<'a, T> { - fn cmp(&self, other: &Self, ctx: &T::Context) -> Ordering; + fn cmp(&self, other: &Self, cx: &T::Context) -> Ordering; } impl<'a, S: Summary, T: Dimension<'a, S> + Ord> SeekDimension<'a, S> for T { @@ -64,9 +64,9 @@ impl SumTree { })) } - pub fn from_item(item: T, ctx: &::Context) -> Self { + pub fn from_item(item: T, cx: &::Context) -> Self { let mut tree = Self::new(); - tree.push(item, ctx); + tree.push(item, cx); tree } @@ -101,14 +101,14 @@ impl SumTree { self.rightmost_leaf().0.items().last() } - pub fn update_last(&mut self, f: impl FnOnce(&mut T), ctx: &::Context) { - self.update_last_recursive(f, ctx); + pub fn update_last(&mut self, f: impl FnOnce(&mut T), cx: &::Context) { + self.update_last_recursive(f, cx); } fn update_last_recursive( &mut self, f: impl FnOnce(&mut T), - ctx: &::Context, + cx: &::Context, ) -> Option { match Arc::make_mut(&mut self.0) { Node::Internal { @@ -119,8 +119,8 @@ impl SumTree { } => { let last_summary = child_summaries.last_mut().unwrap(); let last_child = child_trees.last_mut().unwrap(); - *last_summary = last_child.update_last_recursive(f, ctx).unwrap(); - *summary = sum(child_summaries.iter(), ctx); + *last_summary = last_child.update_last_recursive(f, cx).unwrap(); + *summary = sum(child_summaries.iter(), cx); Some(summary.clone()) } Node::Leaf { @@ -132,7 +132,7 @@ impl SumTree { { (f)(item); *item_summary = item.summary(); - *summary = sum(item_summaries.iter(), ctx); + *summary = sum(item_summaries.iter(), cx); Some(summary.clone()) } else { None @@ -165,7 +165,7 @@ impl SumTree { } } - pub fn extend(&mut self, iter: I, ctx: &::Context) + pub fn extend(&mut self, iter: I, cx: &::Context) where I: IntoIterator, { @@ -173,7 +173,7 @@ impl SumTree { for item in iter { if leaf.is_some() && leaf.as_ref().unwrap().items().len() == 2 * TREE_BASE { - self.push_tree(SumTree(Arc::new(leaf.take().unwrap())), ctx); + self.push_tree(SumTree(Arc::new(leaf.take().unwrap())), cx); } if leaf.is_none() { @@ -191,7 +191,7 @@ impl SumTree { }) = leaf.as_mut() { let item_summary = item.summary(); - summary.add_summary(&item_summary, ctx); + summary.add_summary(&item_summary, cx); items.push(item); item_summaries.push(item_summary); } else { @@ -200,11 +200,11 @@ impl SumTree { } if leaf.is_some() { - self.push_tree(SumTree(Arc::new(leaf.take().unwrap())), ctx); + self.push_tree(SumTree(Arc::new(leaf.take().unwrap())), cx); } } - pub fn push(&mut self, item: T, ctx: &::Context) { + pub fn push(&mut self, item: T, cx: &::Context) { let summary = item.summary(); self.push_tree( SumTree(Arc::new(Node::Leaf { @@ -212,18 +212,18 @@ impl SumTree { items: ArrayVec::from_iter(Some(item)), item_summaries: ArrayVec::from_iter(Some(summary)), })), - ctx, + cx, ); } - pub fn push_tree(&mut self, other: Self, ctx: &::Context) { + pub fn push_tree(&mut self, other: Self, cx: &::Context) { if !other.0.is_leaf() || other.0.items().len() > 0 { if self.0.height() < other.0.height() { for tree in other.0.child_trees() { - self.push_tree(tree.clone(), ctx); + self.push_tree(tree.clone(), cx); } - } else if let Some(split_tree) = self.push_tree_recursive(other, ctx) { - *self = Self::from_child_trees(self.clone(), split_tree, ctx); + } else if let Some(split_tree) = self.push_tree_recursive(other, cx) { + *self = Self::from_child_trees(self.clone(), split_tree, cx); } } } @@ -231,7 +231,7 @@ impl SumTree { fn push_tree_recursive( &mut self, other: SumTree, - ctx: &::Context, + cx: &::Context, ) -> Option> { match Arc::make_mut(&mut self.0) { Node::Internal { @@ -242,7 +242,7 @@ impl SumTree { .. } => { let other_node = other.0.clone(); - summary.add_summary(other_node.summary(), ctx); + summary.add_summary(other_node.summary(), cx); let height_delta = *height - other_node.height(); let mut summaries_to_append = ArrayVec::<[T::Summary; 2 * TREE_BASE]>::new(); @@ -257,7 +257,7 @@ impl SumTree { let tree_to_append = child_trees .last_mut() .unwrap() - .push_tree_recursive(other, ctx); + .push_tree_recursive(other, cx); *child_summaries.last_mut().unwrap() = child_trees.last().unwrap().0.summary().clone(); @@ -287,13 +287,13 @@ impl SumTree { left_trees = all_trees.by_ref().take(midpoint).collect(); right_trees = all_trees.collect(); } - *summary = sum(left_summaries.iter(), ctx); + *summary = sum(left_summaries.iter(), cx); *child_summaries = left_summaries; *child_trees = left_trees; Some(SumTree(Arc::new(Node::Internal { height: *height, - summary: sum(right_summaries.iter(), ctx), + summary: sum(right_summaries.iter(), cx), child_summaries: right_summaries, child_trees: right_trees, }))) @@ -332,14 +332,14 @@ impl SumTree { } *items = left_items; *item_summaries = left_summaries; - *summary = sum(item_summaries.iter(), ctx); + *summary = sum(item_summaries.iter(), cx); Some(SumTree(Arc::new(Node::Leaf { items: right_items, - summary: sum(right_summaries.iter(), ctx), + summary: sum(right_summaries.iter(), cx), item_summaries: right_summaries, }))) } else { - summary.add_summary(other_node.summary(), ctx); + summary.add_summary(other_node.summary(), cx); items.extend(other_node.items().iter().cloned()); item_summaries.extend(other_node.child_summaries().iter().cloned()); None @@ -351,7 +351,7 @@ impl SumTree { fn from_child_trees( left: SumTree, right: SumTree, - ctx: &::Context, + cx: &::Context, ) -> Self { let height = left.0.height() + 1; let mut child_summaries = ArrayVec::new(); @@ -362,7 +362,7 @@ impl SumTree { child_trees.push(right); SumTree(Arc::new(Node::Internal { height, - summary: sum(child_summaries.iter(), ctx), + summary: sum(child_summaries.iter(), cx), child_summaries, child_trees, })) @@ -389,12 +389,12 @@ impl SumTree { impl SumTree { #[allow(unused)] - pub fn insert(&mut self, item: T, ctx: &::Context) { + pub fn insert(&mut self, item: T, cx: &::Context) { *self = { let mut cursor = self.cursor::(); - let mut new_tree = cursor.slice(&item.key(), SeekBias::Left, ctx); - new_tree.push(item, ctx); - new_tree.push_tree(cursor.suffix(ctx), ctx); + let mut new_tree = cursor.slice(&item.key(), SeekBias::Left, cx); + new_tree.push(item, cx); + new_tree.push_tree(cursor.suffix(cx), cx); new_tree }; } @@ -402,7 +402,7 @@ impl SumTree { pub fn edit( &mut self, mut edits: Vec>, - ctx: &::Context, + cx: &::Context, ) -> Vec { if edits.is_empty() { return Vec::new(); @@ -416,7 +416,7 @@ impl SumTree { let mut new_tree = SumTree::new(); let mut buffered_items = Vec::new(); - cursor.seek(&T::Key::default(), SeekBias::Left, ctx); + cursor.seek(&T::Key::default(), SeekBias::Left, cx); for edit in edits { let new_key = edit.key(); let mut old_item = cursor.item(); @@ -425,9 +425,9 @@ impl SumTree { .as_ref() .map_or(false, |old_item| old_item.key() < new_key) { - new_tree.extend(buffered_items.drain(..), ctx); - let slice = cursor.slice(&new_key, SeekBias::Left, ctx); - new_tree.push_tree(slice, ctx); + new_tree.extend(buffered_items.drain(..), cx); + let slice = cursor.slice(&new_key, SeekBias::Left, cx); + new_tree.push_tree(slice, cx); old_item = cursor.item(); } @@ -446,17 +446,17 @@ impl SumTree { } } - new_tree.extend(buffered_items, ctx); - new_tree.push_tree(cursor.suffix(ctx), ctx); + new_tree.extend(buffered_items, cx); + new_tree.push_tree(cursor.suffix(cx), cx); new_tree }; removed } - pub fn get(&self, key: &T::Key, ctx: &::Context) -> Option<&T> { + pub fn get(&self, key: &T::Key, cx: &::Context) -> Option<&T> { let mut cursor = self.cursor::(); - if cursor.seek(key, SeekBias::Left, ctx) { + if cursor.seek(key, SeekBias::Left, cx) { cursor.item() } else { None @@ -553,14 +553,14 @@ impl Edit { } } -fn sum<'a, T, I>(iter: I, ctx: &T::Context) -> T +fn sum<'a, T, I>(iter: I, cx: &T::Context) -> T where T: 'a + Summary, I: Iterator, { let mut sum = T::default(); for value in iter { - sum.add_summary(value, ctx); + sum.add_summary(value, cx); } sum } diff --git a/zed/src/sum_tree/cursor.rs b/zed/src/sum_tree/cursor.rs index 3d65d3f9c3..577b8eed36 100644 --- a/zed/src/sum_tree/cursor.rs +++ b/zed/src/sum_tree/cursor.rs @@ -342,33 +342,28 @@ where S: SeekDimension<'a, T::Summary>, U: Dimension<'a, T::Summary>, { - pub fn seek( - &mut self, - pos: &S, - bias: SeekBias, - ctx: &::Context, - ) -> bool { + pub fn seek(&mut self, pos: &S, bias: SeekBias, cx: &::Context) -> bool { self.reset(); - self.seek_internal::<()>(pos, bias, &mut SeekAggregate::None, ctx) + self.seek_internal::<()>(pos, bias, &mut SeekAggregate::None, cx) } pub fn seek_forward( &mut self, pos: &S, bias: SeekBias, - ctx: &::Context, + cx: &::Context, ) -> bool { - self.seek_internal::<()>(pos, bias, &mut SeekAggregate::None, ctx) + self.seek_internal::<()>(pos, bias, &mut SeekAggregate::None, cx) } pub fn slice( &mut self, end: &S, bias: SeekBias, - ctx: &::Context, + cx: &::Context, ) -> SumTree { let mut slice = SeekAggregate::Slice(SumTree::new()); - self.seek_internal::<()>(end, bias, &mut slice, ctx); + self.seek_internal::<()>(end, bias, &mut slice, cx); if let SeekAggregate::Slice(slice) = slice { slice } else { @@ -376,10 +371,10 @@ where } } - pub fn suffix(&mut self, ctx: &::Context) -> SumTree { + pub fn suffix(&mut self, cx: &::Context) -> SumTree { let extent = self.tree.extent::(); let mut slice = SeekAggregate::Slice(SumTree::new()); - self.seek_internal::<()>(&extent, SeekBias::Right, &mut slice, ctx); + self.seek_internal::<()>(&extent, SeekBias::Right, &mut slice, cx); if let SeekAggregate::Slice(slice) = slice { slice } else { @@ -391,13 +386,13 @@ where &mut self, end: &S, bias: SeekBias, - ctx: &::Context, + cx: &::Context, ) -> D where D: Dimension<'a, T::Summary>, { let mut summary = SeekAggregate::Summary(D::default()); - self.seek_internal(end, bias, &mut summary, ctx); + self.seek_internal(end, bias, &mut summary, cx); if let SeekAggregate::Summary(summary) = summary { summary } else { @@ -410,12 +405,12 @@ where target: &S, bias: SeekBias, aggregate: &mut SeekAggregate, - ctx: &::Context, + cx: &::Context, ) -> bool where D: Dimension<'a, T::Summary>, { - debug_assert!(target.cmp(&self.seek_dimension, ctx) >= Ordering::Equal); + debug_assert!(target.cmp(&self.seek_dimension, cx) >= Ordering::Equal); let mut containing_subtree = None; if self.did_seek { @@ -435,7 +430,7 @@ where let mut child_end = self.seek_dimension.clone(); child_end.add_summary(&child_summary); - let comparison = target.cmp(&child_end, ctx); + let comparison = target.cmp(&child_end, cx); if comparison == Ordering::Greater || (comparison == Ordering::Equal && bias == SeekBias::Right) { @@ -444,7 +439,7 @@ where match aggregate { SeekAggregate::None => {} SeekAggregate::Slice(slice) => { - slice.push_tree(child_tree.clone(), ctx); + slice.push_tree(child_tree.clone(), cx); } SeekAggregate::Summary(summary) => { summary.add_summary(child_summary); @@ -477,7 +472,7 @@ where let mut item_end = self.seek_dimension.clone(); item_end.add_summary(item_summary); - let comparison = target.cmp(&item_end, ctx); + let comparison = target.cmp(&item_end, cx); if comparison == Ordering::Greater || (comparison == Ordering::Equal && bias == SeekBias::Right) { @@ -491,7 +486,7 @@ where slice_items_summary .as_mut() .unwrap() - .add_summary(item_summary, ctx); + .add_summary(item_summary, cx); } SeekAggregate::Summary(summary) => { summary.add_summary(item_summary); @@ -506,7 +501,7 @@ where items: slice_items, item_summaries: slice_item_summaries, })), - ctx, + cx, ); } break 'outer; @@ -521,7 +516,7 @@ where items: slice_items, item_summaries: slice_item_summaries, })), - ctx, + cx, ); } } @@ -551,7 +546,7 @@ where let mut child_end = self.seek_dimension.clone(); child_end.add_summary(child_summary); - let comparison = target.cmp(&child_end, ctx); + let comparison = target.cmp(&child_end, cx); if comparison == Ordering::Greater || (comparison == Ordering::Equal && bias == SeekBias::Right) { @@ -560,7 +555,7 @@ where match aggregate { SeekAggregate::None => {} SeekAggregate::Slice(slice) => { - slice.push_tree(child_trees[index].clone(), ctx); + slice.push_tree(child_trees[index].clone(), cx); } SeekAggregate::Summary(summary) => { summary.add_summary(child_summary); @@ -597,7 +592,7 @@ where let mut child_end = self.seek_dimension.clone(); child_end.add_summary(item_summary); - let comparison = target.cmp(&child_end, ctx); + let comparison = target.cmp(&child_end, cx); if comparison == Ordering::Greater || (comparison == Ordering::Equal && bias == SeekBias::Right) { @@ -610,7 +605,7 @@ where slice_items_summary .as_mut() .unwrap() - .add_summary(item_summary, ctx); + .add_summary(item_summary, cx); slice_item_summaries.push(item_summary.clone()); } SeekAggregate::Summary(summary) => { @@ -636,7 +631,7 @@ where items: slice_items, item_summaries: slice_item_summaries, })), - ctx, + cx, ); } } @@ -658,9 +653,9 @@ where if let Some(summary) = self.item_summary() { end.add_summary(summary); } - target.cmp(&end, ctx) == Ordering::Equal + target.cmp(&end, cx) == Ordering::Equal } else { - target.cmp(&self.seek_dimension, ctx) == Ordering::Equal + target.cmp(&self.seek_dimension, cx) == Ordering::Equal } } }