Add a Summary trait in preparation of passing a context struct

This commit is contained in:
Antonio Scandurra
2021-05-06 09:32:14 +02:00
parent f7d8b6b4c0
commit 7fb7a6adfc
7 changed files with 56 additions and 45 deletions
+11 -12
View File
@@ -475,17 +475,17 @@ impl Buffer {
if let Some(fragment) = cursor.item() {
let summary_start = cmp::max(*cursor.start(), range.start) - cursor.start();
let summary_end = cmp::min(range.end - cursor.start(), fragment.len());
summary += &fragment.text.slice(summary_start..summary_end).summary();
summary += fragment.text.slice(summary_start..summary_end).summary();
cursor.next();
}
if range.end > *cursor.start() {
summary += &cursor.summary::<TextSummary>(&range.end, SeekBias::Right);
summary += cursor.summary::<TextSummary>(&range.end, SeekBias::Right);
if let Some(fragment) = cursor.item() {
let summary_start = cmp::max(*cursor.start(), range.start) - cursor.start();
let summary_end = cmp::min(range.end - cursor.start(), fragment.len());
summary += &fragment.text.slice(summary_start..summary_end).summary();
summary += fragment.text.slice(summary_start..summary_end).summary();
}
}
@@ -520,17 +520,17 @@ impl Buffer {
if let Some(fragment) = cursor.item() {
let summary_start = cmp::max(*cursor.start(), range.start) - cursor.start();
let summary_end = cmp::min(range.end - cursor.start(), fragment.len());
summary += &fragment.text.slice(summary_start..summary_end).summary();
summary += fragment.text.slice(summary_start..summary_end).summary();
cursor.next();
}
if range.end > *cursor.start() {
summary += &cursor.summary::<TextSummary>(&range.end, SeekBias::Right);
summary += cursor.summary::<TextSummary>(&range.end, SeekBias::Right);
if let Some(fragment) = cursor.item() {
let summary_start = cmp::max(*cursor.start(), range.start) - cursor.start();
let summary_end = cmp::min(range.end - cursor.start(), fragment.len());
summary += &fragment.text.slice(summary_start..summary_end).summary();
summary += fragment.text.slice(summary_start..summary_end).summary();
}
}
@@ -2102,8 +2102,8 @@ impl sum_tree::Item for Fragment {
}
}
impl<'a> AddAssign<&'a FragmentSummary> for FragmentSummary {
fn add_assign(&mut self, other: &Self) {
impl sum_tree::Summary for FragmentSummary {
fn add_summary(&mut self, other: &Self) {
self.text_summary += &other.text_summary;
debug_assert!(self.max_fragment_id <= other.max_fragment_id);
self.max_fragment_id = other.max_fragment_id.clone();
@@ -2166,8 +2166,8 @@ impl sum_tree::Item for InsertionSplit {
}
}
impl<'a> AddAssign<&'a InsertionSplitSummary> for InsertionSplitSummary {
fn add_assign(&mut self, other: &Self) {
impl sum_tree::Summary for InsertionSplitSummary {
fn add_summary(&mut self, other: &Self) {
self.extent += other.extent;
}
}
@@ -3071,8 +3071,7 @@ mod tests {
let mut buffers = Vec::new();
let mut network = Network::new();
for i in 0..PEERS {
let buffer =
ctx.add_model(|_| Buffer::new(i as ReplicaId, base_text.as_str()));
let buffer = ctx.add_model(|_| Buffer::new(i as ReplicaId, base_text.as_str()));
buffers.push(buffer);
replica_ids.push(i as u16);
network.add_peer(i as u16);
+8 -2
View File
@@ -58,6 +58,12 @@ pub struct TextSummary {
pub rightmost_point: Point,
}
impl sum_tree::Summary for TextSummary {
fn add_summary(&mut self, other: &Self) {
*self += other;
}
}
impl<'a> std::ops::AddAssign<&'a Self> for TextSummary {
fn add_assign(&mut self, other: &'a Self) {
let joined_line_len = self.lines.column + other.first_line_len;
@@ -85,8 +91,8 @@ impl std::ops::AddAssign<Self> for TextSummary {
}
impl<'a> sum_tree::Dimension<'a, TextSummary> for TextSummary {
fn add_summary(&mut self, summary: &TextSummary) {
*self += summary;
fn add_summary(&mut self, other: &TextSummary) {
*self += other;
}
}
+3 -3
View File
@@ -421,8 +421,8 @@ impl sum_tree::Item for Transform {
}
}
impl<'a> std::ops::AddAssign<&'a Self> for TransformSummary {
fn add_assign(&mut self, other: &'a Self) {
impl sum_tree::Summary for TransformSummary {
fn add_summary(&mut self, other: &Self) {
self.buffer += &other.buffer;
self.display += &other.display;
}
@@ -430,7 +430,7 @@ impl<'a> std::ops::AddAssign<&'a Self> for TransformSummary {
impl<'a> Dimension<'a, TransformSummary> for TransformSummary {
fn add_summary(&mut self, summary: &'a TransformSummary) {
*self += summary;
sum_tree::Summary::add_summary(self, summary);
}
}
+4 -7
View File
@@ -1,11 +1,8 @@
use crate::{
sum_tree::{Cursor, Dimension, Edit, Item, KeyedItem, SumTree},
sum_tree::{Cursor, Dimension, Edit, Item, KeyedItem, SumTree, Summary},
time,
};
use std::{
fmt::Debug,
ops::{Add, AddAssign},
};
use std::{fmt::Debug, ops::Add};
pub trait Operation: Clone + Debug + Eq {
fn timestamp(&self) -> time::Lamport;
@@ -68,8 +65,8 @@ impl<T: Operation> KeyedItem for T {
}
}
impl<'a> AddAssign<&'a Self> for OperationSummary {
fn add_assign(&mut self, other: &Self) {
impl Summary for OperationSummary {
fn add_summary(&mut self, other: &Self) {
assert!(self.key < other.key);
self.key = other.key;
self.len += other.len;
+8 -2
View File
@@ -511,7 +511,10 @@ where
SeekAggregate::Slice(_) => {
slice_items.push(item.clone());
slice_item_summaries.push(item_summary.clone());
*slice_items_summary.as_mut().unwrap() += item_summary;
slice_items_summary
.as_mut()
.unwrap()
.add_summary(item_summary);
}
SeekAggregate::Summary(summary) => {
summary.add_summary(item_summary);
@@ -621,7 +624,10 @@ where
SeekAggregate::None => {}
SeekAggregate::Slice(_) => {
slice_items.push(item.clone());
*slice_items_summary.as_mut().unwrap() += item_summary;
slice_items_summary
.as_mut()
.unwrap()
.add_summary(item_summary);
slice_item_summaries.push(item_summary.clone());
}
SeekAggregate::Summary(summary) => {
+19 -16
View File
@@ -3,7 +3,7 @@ mod cursor;
use arrayvec::ArrayVec;
pub use cursor::Cursor;
pub use cursor::FilterCursor;
use std::{fmt, iter::FromIterator, ops::AddAssign, sync::Arc};
use std::{fmt, iter::FromIterator, sync::Arc};
#[cfg(test)]
const TREE_BASE: usize = 2;
@@ -11,7 +11,7 @@ const TREE_BASE: usize = 2;
const TREE_BASE: usize = 6;
pub trait Item: Clone + fmt::Debug {
type Summary: for<'a> AddAssign<&'a Self::Summary> + Default + Clone + fmt::Debug;
type Summary: Summary;
fn summary(&self) -> Self::Summary;
}
@@ -22,6 +22,10 @@ pub trait KeyedItem: Item {
fn key(&self) -> Self::Key;
}
pub trait Summary: Default + Clone + fmt::Debug {
fn add_summary(&mut self, summary: &Self);
}
pub trait Dimension<'a, Summary: Default>: Clone + fmt::Debug + Default {
fn add_summary(&mut self, summary: &'a Summary);
}
@@ -136,7 +140,7 @@ impl<T: Item> SumTree<T> {
}) = leaf.as_mut()
{
let item_summary = item.summary();
*summary += &item_summary;
summary.add_summary(&item_summary);
items.push(item);
item_summaries.push(item_summary);
} else {
@@ -183,7 +187,7 @@ impl<T: Item> SumTree<T> {
..
} => {
let other_node = other.0.clone();
*summary += other_node.summary();
summary.add_summary(other_node.summary());
let height_delta = *height - other_node.height();
let mut summaries_to_append = ArrayVec::<[T::Summary; 2 * TREE_BASE]>::new();
@@ -277,7 +281,7 @@ impl<T: Item> SumTree<T> {
item_summaries: right_summaries,
})))
} else {
*summary += other_node.summary();
summary.add_summary(other_node.summary());
items.extend(other_node.items().iter().cloned());
item_summaries.extend(other_node.child_summaries().iter().cloned());
None
@@ -484,12 +488,12 @@ impl<T: KeyedItem> Edit<T> {
fn sum<'a, T, I>(iter: I) -> T
where
T: 'a + Default + AddAssign<&'a T>,
T: 'a + Summary,
I: Iterator<Item = &'a T>,
{
let mut sum = T::default();
for value in iter {
sum += value;
sum.add_summary(value);
}
sum
}
@@ -836,15 +840,8 @@ mod tests {
*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) {
impl Summary for IntegersSummary {
fn add_summary(&mut self, other: &Self) {
self.count.0 += &other.count.0;
self.sum.0 += &other.sum.0;
self.contains_even |= other.contains_even;
@@ -852,6 +849,12 @@ mod tests {
}
}
impl<'a> Dimension<'a, IntegersSummary> for u8 {
fn add_summary(&mut self, summary: &IntegersSummary) {
*self = summary.max;
}
}
impl<'a> Dimension<'a, IntegersSummary> for Count {
fn add_summary(&mut self, summary: &IntegersSummary) {
self.0 += summary.count.0;
+3 -3
View File
@@ -24,7 +24,7 @@ use std::{
fmt, fs,
future::Future,
io::{self, Read, Write},
ops::{AddAssign, Deref},
ops::Deref,
os::unix::{ffi::OsStrExt, fs::MetadataExt},
path::{Path, PathBuf},
sync::{Arc, Weak},
@@ -535,8 +535,8 @@ impl Default for EntrySummary {
}
}
impl<'a> AddAssign<&'a EntrySummary> for EntrySummary {
fn add_assign(&mut self, rhs: &'a EntrySummary) {
impl sum_tree::Summary for EntrySummary {
fn add_summary(&mut self, rhs: &Self) {
self.max_path = rhs.max_path.clone();
self.file_count += rhs.file_count;
self.visible_file_count += rhs.visible_file_count;