1mod cursor;
2mod tree_map;
3
4use arrayvec::ArrayVec;
5pub use cursor::{Cursor, FilterCursor, Iter};
6use rayon::prelude::*;
7use std::marker::PhantomData;
8use std::mem;
9use std::{cmp::Ordering, fmt, iter::FromIterator, sync::Arc};
10pub use tree_map::{MapSeekTarget, TreeMap, TreeSet};
11
12#[cfg(test)]
13pub const TREE_BASE: usize = 2;
14#[cfg(not(test))]
15pub const TREE_BASE: usize = 6;
16
17/// An item that can be stored in a [`SumTree`]
18///
19/// Must be summarized by a type that implements [`Summary`]
20pub trait Item: Clone {
21 type Summary: Summary;
22
23 fn summary(&self) -> Self::Summary;
24}
25
26/// An [`Item`] whose summary has a specific key that can be used to identify it
27pub trait KeyedItem: Item {
28 type Key: for<'a> Dimension<'a, Self::Summary> + Ord;
29
30 fn key(&self) -> Self::Key;
31}
32
33/// A type that describes the Sum of all [`Item`]s in a subtree of the [`SumTree`]
34///
35/// Each Summary type can have multiple [`Dimensions`] that it measures,
36/// which can be used to navigate the tree
37pub trait Summary: Default + Clone + fmt::Debug {
38 type Context;
39
40 fn add_summary(&mut self, summary: &Self, cx: &Self::Context);
41}
42
43/// Each [`Summary`] type can have more than one [`Dimension`] type that it measures.
44///
45/// You can use dimensions to seek to a specific location in the [`SumTree`]
46///
47/// # Example:
48/// Zed's rope has a `TextSummary` type that summarizes lines, characters, and bytes.
49/// Each of these are different dimensions we may want to seek to
50pub trait Dimension<'a, S: Summary>: Clone + fmt::Debug + Default {
51 fn add_summary(&mut self, _summary: &'a S, _: &S::Context);
52
53 fn from_summary(summary: &'a S, cx: &S::Context) -> Self {
54 let mut dimension = Self::default();
55 dimension.add_summary(summary, cx);
56 dimension
57 }
58}
59
60impl<'a, T: Summary> Dimension<'a, T> for T {
61 fn add_summary(&mut self, summary: &'a T, cx: &T::Context) {
62 Summary::add_summary(self, summary, cx);
63 }
64}
65
66pub trait SeekTarget<'a, S: Summary, D: Dimension<'a, S>>: fmt::Debug {
67 fn cmp(&self, cursor_location: &D, cx: &S::Context) -> Ordering;
68}
69
70impl<'a, S: Summary, D: Dimension<'a, S> + Ord> SeekTarget<'a, S, D> for D {
71 fn cmp(&self, cursor_location: &Self, _: &S::Context) -> Ordering {
72 Ord::cmp(self, cursor_location)
73 }
74}
75
76impl<'a, T: Summary> Dimension<'a, T> for () {
77 fn add_summary(&mut self, _: &'a T, _: &T::Context) {}
78}
79
80impl<'a, T: Summary, D1: Dimension<'a, T>, D2: Dimension<'a, T>> Dimension<'a, T> for (D1, D2) {
81 fn add_summary(&mut self, summary: &'a T, cx: &T::Context) {
82 self.0.add_summary(summary, cx);
83 self.1.add_summary(summary, cx);
84 }
85}
86
87impl<'a, S: Summary, D1: SeekTarget<'a, S, D1> + Dimension<'a, S>, D2: Dimension<'a, S>>
88 SeekTarget<'a, S, (D1, D2)> for D1
89{
90 fn cmp(&self, cursor_location: &(D1, D2), cx: &S::Context) -> Ordering {
91 self.cmp(&cursor_location.0, cx)
92 }
93}
94
95struct End<D>(PhantomData<D>);
96
97impl<D> End<D> {
98 fn new() -> Self {
99 Self(PhantomData)
100 }
101}
102
103impl<'a, S: Summary, D: Dimension<'a, S>> SeekTarget<'a, S, D> for End<D> {
104 fn cmp(&self, _: &D, _: &S::Context) -> Ordering {
105 Ordering::Greater
106 }
107}
108
109impl<D> fmt::Debug for End<D> {
110 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111 f.debug_tuple("End").finish()
112 }
113}
114
115/// Bias is used to settle ambiguities when determining positions in an ordered sequence.
116///
117/// The primary use case is for text, where Bias influences
118/// which character an offset or anchor is associated with.
119///
120/// # Examples
121/// Given the buffer `AˇBCD`:
122/// - The offset of the cursor is 1
123/// - [Bias::Left] would attach the cursor to the character `A`
124/// - [Bias::Right] would attach the cursor to the character `B`
125///
126/// Given the buffer `A«BCˇ»D`:
127/// - The offset of the cursor is 3, and the selection is from 1 to 3
128/// - The left anchor of the selection has [Bias::Right], attaching it to the character `B`
129/// - The right anchor of the selection has [Bias::Left], attaching it to the character `C`
130///
131/// Given the buffer `{ˇ<...>`, where `<...>` is a folded region:
132/// - The display offset of the cursor is 1, but the offset in the buffer is determined by the bias
133/// - [Bias::Left] would attach the cursor to the character `{`, with a buffer offset of 1
134/// - [Bias::Right] would attach the cursor to the first character of the folded region,
135/// and the buffer offset would be the offset of the first character of the folded region
136#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Debug, Hash, Default)]
137pub enum Bias {
138 /// Attach to the character on the left
139 #[default]
140 Left,
141 /// Attach to the character on the right
142 Right,
143}
144
145impl Bias {
146 pub fn invert(self) -> Self {
147 match self {
148 Self::Left => Self::Right,
149 Self::Right => Self::Left,
150 }
151 }
152}
153
154/// A B-tree where each leaf node contains an [`Item`] of type `T`,
155/// and each internal node contains a [`Summary`] of the items in its subtree.
156///
157/// Any [`Dimension`] supported by the [`Summary`] type can be used to seek to a specific location in the tree.
158#[derive(Debug, Clone)]
159pub struct SumTree<T: Item>(Arc<Node<T>>);
160
161impl<T: Item> SumTree<T> {
162 pub fn new() -> Self {
163 SumTree(Arc::new(Node::Leaf {
164 summary: T::Summary::default(),
165 items: ArrayVec::new(),
166 item_summaries: ArrayVec::new(),
167 }))
168 }
169
170 pub fn from_item(item: T, cx: &<T::Summary as Summary>::Context) -> Self {
171 let mut tree = Self::new();
172 tree.push(item, cx);
173 tree
174 }
175
176 pub fn from_iter<I: IntoIterator<Item = T>>(
177 iter: I,
178 cx: &<T::Summary as Summary>::Context,
179 ) -> Self {
180 let mut nodes = Vec::new();
181
182 let mut iter = iter.into_iter().fuse().peekable();
183 while iter.peek().is_some() {
184 let items: ArrayVec<T, { 2 * TREE_BASE }> = iter.by_ref().take(2 * TREE_BASE).collect();
185 let item_summaries: ArrayVec<T::Summary, { 2 * TREE_BASE }> =
186 items.iter().map(|item| item.summary()).collect();
187
188 let mut summary = item_summaries[0].clone();
189 for item_summary in &item_summaries[1..] {
190 <T::Summary as Summary>::add_summary(&mut summary, item_summary, cx);
191 }
192
193 nodes.push(Node::Leaf {
194 summary,
195 items,
196 item_summaries,
197 });
198 }
199
200 let mut parent_nodes = Vec::new();
201 let mut height = 0;
202 while nodes.len() > 1 {
203 height += 1;
204 let mut current_parent_node = None;
205 for child_node in nodes.drain(..) {
206 let parent_node = current_parent_node.get_or_insert_with(|| Node::Internal {
207 summary: T::Summary::default(),
208 height,
209 child_summaries: ArrayVec::new(),
210 child_trees: ArrayVec::new(),
211 });
212 let Node::Internal {
213 summary,
214 child_summaries,
215 child_trees,
216 ..
217 } = parent_node
218 else {
219 unreachable!()
220 };
221 let child_summary = child_node.summary();
222 <T::Summary as Summary>::add_summary(summary, child_summary, cx);
223 child_summaries.push(child_summary.clone());
224 child_trees.push(Self(Arc::new(child_node)));
225
226 if child_trees.len() == 2 * TREE_BASE {
227 parent_nodes.extend(current_parent_node.take());
228 }
229 }
230 parent_nodes.extend(current_parent_node.take());
231 mem::swap(&mut nodes, &mut parent_nodes);
232 }
233
234 if nodes.is_empty() {
235 Self::new()
236 } else {
237 debug_assert_eq!(nodes.len(), 1);
238 Self(Arc::new(nodes.pop().unwrap()))
239 }
240 }
241
242 pub fn from_par_iter<I, Iter>(iter: I, cx: &<T::Summary as Summary>::Context) -> Self
243 where
244 I: IntoParallelIterator<Iter = Iter>,
245 Iter: IndexedParallelIterator<Item = T>,
246 T: Send + Sync,
247 T::Summary: Send + Sync,
248 <T::Summary as Summary>::Context: Sync,
249 {
250 let mut nodes = iter
251 .into_par_iter()
252 .chunks(2 * TREE_BASE)
253 .map(|items| {
254 let items: ArrayVec<T, { 2 * TREE_BASE }> = items.into_iter().collect();
255 let item_summaries: ArrayVec<T::Summary, { 2 * TREE_BASE }> =
256 items.iter().map(|item| item.summary()).collect();
257 let mut summary = item_summaries[0].clone();
258 for item_summary in &item_summaries[1..] {
259 <T::Summary as Summary>::add_summary(&mut summary, item_summary, cx);
260 }
261 SumTree(Arc::new(Node::Leaf {
262 summary,
263 items,
264 item_summaries,
265 }))
266 })
267 .collect::<Vec<_>>();
268
269 let mut height = 0;
270 while nodes.len() > 1 {
271 height += 1;
272 nodes = nodes
273 .into_par_iter()
274 .chunks(2 * TREE_BASE)
275 .map(|child_nodes| {
276 let child_trees: ArrayVec<SumTree<T>, { 2 * TREE_BASE }> =
277 child_nodes.into_iter().collect();
278 let child_summaries: ArrayVec<T::Summary, { 2 * TREE_BASE }> = child_trees
279 .iter()
280 .map(|child_tree| child_tree.summary().clone())
281 .collect();
282 let mut summary = child_summaries[0].clone();
283 for child_summary in &child_summaries[1..] {
284 <T::Summary as Summary>::add_summary(&mut summary, child_summary, cx);
285 }
286 SumTree(Arc::new(Node::Internal {
287 height,
288 summary,
289 child_summaries,
290 child_trees,
291 }))
292 })
293 .collect::<Vec<_>>();
294 }
295
296 if nodes.is_empty() {
297 Self::new()
298 } else {
299 debug_assert_eq!(nodes.len(), 1);
300 nodes.pop().unwrap()
301 }
302 }
303
304 #[allow(unused)]
305 pub fn items(&self, cx: &<T::Summary as Summary>::Context) -> Vec<T> {
306 let mut items = Vec::new();
307 let mut cursor = self.cursor::<()>();
308 cursor.next(cx);
309 while let Some(item) = cursor.item() {
310 items.push(item.clone());
311 cursor.next(cx);
312 }
313 items
314 }
315
316 pub fn iter(&self) -> Iter<T> {
317 Iter::new(self)
318 }
319
320 pub fn cursor<'a, S>(&'a self) -> Cursor<T, S>
321 where
322 S: Dimension<'a, T::Summary>,
323 {
324 Cursor::new(self)
325 }
326
327 /// Note: If the summary type requires a non `()` context, then the filter cursor
328 /// that is returned cannot be used with Rust's iterators.
329 pub fn filter<'a, F, U>(&'a self, filter_node: F) -> FilterCursor<F, T, U>
330 where
331 F: FnMut(&T::Summary) -> bool,
332 U: Dimension<'a, T::Summary>,
333 {
334 FilterCursor::new(self, filter_node)
335 }
336
337 #[allow(dead_code)]
338 pub fn first(&self) -> Option<&T> {
339 self.leftmost_leaf().0.items().first()
340 }
341
342 pub fn last(&self) -> Option<&T> {
343 self.rightmost_leaf().0.items().last()
344 }
345
346 pub fn update_last(&mut self, f: impl FnOnce(&mut T), cx: &<T::Summary as Summary>::Context) {
347 self.update_last_recursive(f, cx);
348 }
349
350 fn update_last_recursive(
351 &mut self,
352 f: impl FnOnce(&mut T),
353 cx: &<T::Summary as Summary>::Context,
354 ) -> Option<T::Summary> {
355 match Arc::make_mut(&mut self.0) {
356 Node::Internal {
357 summary,
358 child_summaries,
359 child_trees,
360 ..
361 } => {
362 let last_summary = child_summaries.last_mut().unwrap();
363 let last_child = child_trees.last_mut().unwrap();
364 *last_summary = last_child.update_last_recursive(f, cx).unwrap();
365 *summary = sum(child_summaries.iter(), cx);
366 Some(summary.clone())
367 }
368 Node::Leaf {
369 summary,
370 items,
371 item_summaries,
372 } => {
373 if let Some((item, item_summary)) = items.last_mut().zip(item_summaries.last_mut())
374 {
375 (f)(item);
376 *item_summary = item.summary();
377 *summary = sum(item_summaries.iter(), cx);
378 Some(summary.clone())
379 } else {
380 None
381 }
382 }
383 }
384 }
385
386 pub fn extent<'a, D: Dimension<'a, T::Summary>>(
387 &'a self,
388 cx: &<T::Summary as Summary>::Context,
389 ) -> D {
390 let mut extent = D::default();
391 match self.0.as_ref() {
392 Node::Internal { summary, .. } | Node::Leaf { summary, .. } => {
393 extent.add_summary(summary, cx);
394 }
395 }
396 extent
397 }
398
399 pub fn summary(&self) -> &T::Summary {
400 match self.0.as_ref() {
401 Node::Internal { summary, .. } => summary,
402 Node::Leaf { summary, .. } => summary,
403 }
404 }
405
406 pub fn is_empty(&self) -> bool {
407 match self.0.as_ref() {
408 Node::Internal { .. } => false,
409 Node::Leaf { items, .. } => items.is_empty(),
410 }
411 }
412
413 pub fn extend<I>(&mut self, iter: I, cx: &<T::Summary as Summary>::Context)
414 where
415 I: IntoIterator<Item = T>,
416 {
417 self.append(Self::from_iter(iter, cx), cx);
418 }
419
420 pub fn par_extend<I, Iter>(&mut self, iter: I, cx: &<T::Summary as Summary>::Context)
421 where
422 I: IntoParallelIterator<Iter = Iter>,
423 Iter: IndexedParallelIterator<Item = T>,
424 T: Send + Sync,
425 T::Summary: Send + Sync,
426 <T::Summary as Summary>::Context: Sync,
427 {
428 self.append(Self::from_par_iter(iter, cx), cx);
429 }
430
431 pub fn push(&mut self, item: T, cx: &<T::Summary as Summary>::Context) {
432 let summary = item.summary();
433 self.append(
434 SumTree(Arc::new(Node::Leaf {
435 summary: summary.clone(),
436 items: ArrayVec::from_iter(Some(item)),
437 item_summaries: ArrayVec::from_iter(Some(summary)),
438 })),
439 cx,
440 );
441 }
442
443 pub fn append(&mut self, other: Self, cx: &<T::Summary as Summary>::Context) {
444 if self.is_empty() {
445 *self = other;
446 } else if !other.0.is_leaf() || !other.0.items().is_empty() {
447 if self.0.height() < other.0.height() {
448 for tree in other.0.child_trees() {
449 self.append(tree.clone(), cx);
450 }
451 } else if let Some(split_tree) = self.push_tree_recursive(other, cx) {
452 *self = Self::from_child_trees(self.clone(), split_tree, cx);
453 }
454 }
455 }
456
457 fn push_tree_recursive(
458 &mut self,
459 other: SumTree<T>,
460 cx: &<T::Summary as Summary>::Context,
461 ) -> Option<SumTree<T>> {
462 match Arc::make_mut(&mut self.0) {
463 Node::Internal {
464 height,
465 summary,
466 child_summaries,
467 child_trees,
468 ..
469 } => {
470 let other_node = other.0.clone();
471 <T::Summary as Summary>::add_summary(summary, other_node.summary(), cx);
472
473 let height_delta = *height - other_node.height();
474 let mut summaries_to_append = ArrayVec::<T::Summary, { 2 * TREE_BASE }>::new();
475 let mut trees_to_append = ArrayVec::<SumTree<T>, { 2 * TREE_BASE }>::new();
476 if height_delta == 0 {
477 summaries_to_append.extend(other_node.child_summaries().iter().cloned());
478 trees_to_append.extend(other_node.child_trees().iter().cloned());
479 } else if height_delta == 1 && !other_node.is_underflowing() {
480 summaries_to_append.push(other_node.summary().clone());
481 trees_to_append.push(other)
482 } else {
483 let tree_to_append = child_trees
484 .last_mut()
485 .unwrap()
486 .push_tree_recursive(other, cx);
487 *child_summaries.last_mut().unwrap() =
488 child_trees.last().unwrap().0.summary().clone();
489
490 if let Some(split_tree) = tree_to_append {
491 summaries_to_append.push(split_tree.0.summary().clone());
492 trees_to_append.push(split_tree);
493 }
494 }
495
496 let child_count = child_trees.len() + trees_to_append.len();
497 if child_count > 2 * TREE_BASE {
498 let left_summaries: ArrayVec<_, { 2 * TREE_BASE }>;
499 let right_summaries: ArrayVec<_, { 2 * TREE_BASE }>;
500 let left_trees;
501 let right_trees;
502
503 let midpoint = (child_count + child_count % 2) / 2;
504 {
505 let mut all_summaries = child_summaries
506 .iter()
507 .chain(summaries_to_append.iter())
508 .cloned();
509 left_summaries = all_summaries.by_ref().take(midpoint).collect();
510 right_summaries = all_summaries.collect();
511 let mut all_trees =
512 child_trees.iter().chain(trees_to_append.iter()).cloned();
513 left_trees = all_trees.by_ref().take(midpoint).collect();
514 right_trees = all_trees.collect();
515 }
516 *summary = sum(left_summaries.iter(), cx);
517 *child_summaries = left_summaries;
518 *child_trees = left_trees;
519
520 Some(SumTree(Arc::new(Node::Internal {
521 height: *height,
522 summary: sum(right_summaries.iter(), cx),
523 child_summaries: right_summaries,
524 child_trees: right_trees,
525 })))
526 } else {
527 child_summaries.extend(summaries_to_append);
528 child_trees.extend(trees_to_append);
529 None
530 }
531 }
532 Node::Leaf {
533 summary,
534 items,
535 item_summaries,
536 } => {
537 let other_node = other.0;
538
539 let child_count = items.len() + other_node.items().len();
540 if child_count > 2 * TREE_BASE {
541 let left_items;
542 let right_items;
543 let left_summaries;
544 let right_summaries: ArrayVec<T::Summary, { 2 * TREE_BASE }>;
545
546 let midpoint = (child_count + child_count % 2) / 2;
547 {
548 let mut all_items = items.iter().chain(other_node.items().iter()).cloned();
549 left_items = all_items.by_ref().take(midpoint).collect();
550 right_items = all_items.collect();
551
552 let mut all_summaries = item_summaries
553 .iter()
554 .chain(other_node.child_summaries())
555 .cloned();
556 left_summaries = all_summaries.by_ref().take(midpoint).collect();
557 right_summaries = all_summaries.collect();
558 }
559 *items = left_items;
560 *item_summaries = left_summaries;
561 *summary = sum(item_summaries.iter(), cx);
562 Some(SumTree(Arc::new(Node::Leaf {
563 items: right_items,
564 summary: sum(right_summaries.iter(), cx),
565 item_summaries: right_summaries,
566 })))
567 } else {
568 <T::Summary as Summary>::add_summary(summary, other_node.summary(), cx);
569 items.extend(other_node.items().iter().cloned());
570 item_summaries.extend(other_node.child_summaries().iter().cloned());
571 None
572 }
573 }
574 }
575 }
576
577 fn from_child_trees(
578 left: SumTree<T>,
579 right: SumTree<T>,
580 cx: &<T::Summary as Summary>::Context,
581 ) -> Self {
582 let height = left.0.height() + 1;
583 let mut child_summaries = ArrayVec::new();
584 child_summaries.push(left.0.summary().clone());
585 child_summaries.push(right.0.summary().clone());
586 let mut child_trees = ArrayVec::new();
587 child_trees.push(left);
588 child_trees.push(right);
589 SumTree(Arc::new(Node::Internal {
590 height,
591 summary: sum(child_summaries.iter(), cx),
592 child_summaries,
593 child_trees,
594 }))
595 }
596
597 fn leftmost_leaf(&self) -> &Self {
598 match *self.0 {
599 Node::Leaf { .. } => self,
600 Node::Internal {
601 ref child_trees, ..
602 } => child_trees.first().unwrap().leftmost_leaf(),
603 }
604 }
605
606 fn rightmost_leaf(&self) -> &Self {
607 match *self.0 {
608 Node::Leaf { .. } => self,
609 Node::Internal {
610 ref child_trees, ..
611 } => child_trees.last().unwrap().rightmost_leaf(),
612 }
613 }
614
615 #[cfg(debug_assertions)]
616 pub fn _debug_entries(&self) -> Vec<&T> {
617 self.iter().collect::<Vec<_>>()
618 }
619}
620
621impl<T: Item + PartialEq> PartialEq for SumTree<T> {
622 fn eq(&self, other: &Self) -> bool {
623 self.iter().eq(other.iter())
624 }
625}
626
627impl<T: Item + Eq> Eq for SumTree<T> {}
628
629impl<T: KeyedItem> SumTree<T> {
630 pub fn insert_or_replace(
631 &mut self,
632 item: T,
633 cx: &<T::Summary as Summary>::Context,
634 ) -> Option<T> {
635 let mut replaced = None;
636 *self = {
637 let mut cursor = self.cursor::<T::Key>();
638 let mut new_tree = cursor.slice(&item.key(), Bias::Left, cx);
639 if let Some(cursor_item) = cursor.item() {
640 if cursor_item.key() == item.key() {
641 replaced = Some(cursor_item.clone());
642 cursor.next(cx);
643 }
644 }
645 new_tree.push(item, cx);
646 new_tree.append(cursor.suffix(cx), cx);
647 new_tree
648 };
649 replaced
650 }
651
652 pub fn remove(&mut self, key: &T::Key, cx: &<T::Summary as Summary>::Context) -> Option<T> {
653 let mut removed = None;
654 *self = {
655 let mut cursor = self.cursor::<T::Key>();
656 let mut new_tree = cursor.slice(key, Bias::Left, cx);
657 if let Some(item) = cursor.item() {
658 if item.key() == *key {
659 removed = Some(item.clone());
660 cursor.next(cx);
661 }
662 }
663 new_tree.append(cursor.suffix(cx), cx);
664 new_tree
665 };
666 removed
667 }
668
669 pub fn edit(
670 &mut self,
671 mut edits: Vec<Edit<T>>,
672 cx: &<T::Summary as Summary>::Context,
673 ) -> Vec<T> {
674 if edits.is_empty() {
675 return Vec::new();
676 }
677
678 let mut removed = Vec::new();
679 edits.sort_unstable_by_key(|item| item.key());
680
681 *self = {
682 let mut cursor = self.cursor::<T::Key>();
683 let mut new_tree = SumTree::new();
684 let mut buffered_items = Vec::new();
685
686 cursor.seek(&T::Key::default(), Bias::Left, cx);
687 for edit in edits {
688 let new_key = edit.key();
689 let mut old_item = cursor.item();
690
691 if old_item
692 .as_ref()
693 .map_or(false, |old_item| old_item.key() < new_key)
694 {
695 new_tree.extend(buffered_items.drain(..), cx);
696 let slice = cursor.slice(&new_key, Bias::Left, cx);
697 new_tree.append(slice, cx);
698 old_item = cursor.item();
699 }
700
701 if let Some(old_item) = old_item {
702 if old_item.key() == new_key {
703 removed.push(old_item.clone());
704 cursor.next(cx);
705 }
706 }
707
708 match edit {
709 Edit::Insert(item) => {
710 buffered_items.push(item);
711 }
712 Edit::Remove(_) => {}
713 }
714 }
715
716 new_tree.extend(buffered_items, cx);
717 new_tree.append(cursor.suffix(cx), cx);
718 new_tree
719 };
720
721 removed
722 }
723
724 pub fn get(&self, key: &T::Key, cx: &<T::Summary as Summary>::Context) -> Option<&T> {
725 let mut cursor = self.cursor::<T::Key>();
726 if cursor.seek(key, Bias::Left, cx) {
727 cursor.item()
728 } else {
729 None
730 }
731 }
732}
733
734impl<T: Item> Default for SumTree<T> {
735 fn default() -> Self {
736 Self::new()
737 }
738}
739
740#[derive(Clone, Debug)]
741pub enum Node<T: Item> {
742 Internal {
743 height: u8,
744 summary: T::Summary,
745 child_summaries: ArrayVec<T::Summary, { 2 * TREE_BASE }>,
746 child_trees: ArrayVec<SumTree<T>, { 2 * TREE_BASE }>,
747 },
748 Leaf {
749 summary: T::Summary,
750 items: ArrayVec<T, { 2 * TREE_BASE }>,
751 item_summaries: ArrayVec<T::Summary, { 2 * TREE_BASE }>,
752 },
753}
754
755impl<T: Item> Node<T> {
756 fn is_leaf(&self) -> bool {
757 matches!(self, Node::Leaf { .. })
758 }
759
760 fn height(&self) -> u8 {
761 match self {
762 Node::Internal { height, .. } => *height,
763 Node::Leaf { .. } => 0,
764 }
765 }
766
767 fn summary(&self) -> &T::Summary {
768 match self {
769 Node::Internal { summary, .. } => summary,
770 Node::Leaf { summary, .. } => summary,
771 }
772 }
773
774 fn child_summaries(&self) -> &[T::Summary] {
775 match self {
776 Node::Internal {
777 child_summaries, ..
778 } => child_summaries.as_slice(),
779 Node::Leaf { item_summaries, .. } => item_summaries.as_slice(),
780 }
781 }
782
783 fn child_trees(&self) -> &ArrayVec<SumTree<T>, { 2 * TREE_BASE }> {
784 match self {
785 Node::Internal { child_trees, .. } => child_trees,
786 Node::Leaf { .. } => panic!("Leaf nodes have no child trees"),
787 }
788 }
789
790 fn items(&self) -> &ArrayVec<T, { 2 * TREE_BASE }> {
791 match self {
792 Node::Leaf { items, .. } => items,
793 Node::Internal { .. } => panic!("Internal nodes have no items"),
794 }
795 }
796
797 fn is_underflowing(&self) -> bool {
798 match self {
799 Node::Internal { child_trees, .. } => child_trees.len() < TREE_BASE,
800 Node::Leaf { items, .. } => items.len() < TREE_BASE,
801 }
802 }
803}
804
805#[derive(Debug)]
806pub enum Edit<T: KeyedItem> {
807 Insert(T),
808 Remove(T::Key),
809}
810
811impl<T: KeyedItem> Edit<T> {
812 fn key(&self) -> T::Key {
813 match self {
814 Edit::Insert(item) => item.key(),
815 Edit::Remove(key) => key.clone(),
816 }
817 }
818}
819
820fn sum<'a, T, I>(iter: I, cx: &T::Context) -> T
821where
822 T: 'a + Summary,
823 I: Iterator<Item = &'a T>,
824{
825 let mut sum = T::default();
826 for value in iter {
827 sum.add_summary(value, cx);
828 }
829 sum
830}
831
832#[cfg(test)]
833mod tests {
834 use super::*;
835 use rand::{distributions, prelude::*};
836 use std::cmp;
837
838 #[ctor::ctor]
839 fn init_logger() {
840 if std::env::var("RUST_LOG").is_ok() {
841 env_logger::init();
842 }
843 }
844
845 #[test]
846 fn test_extend_and_push_tree() {
847 let mut tree1 = SumTree::new();
848 tree1.extend(0..20, &());
849
850 let mut tree2 = SumTree::new();
851 tree2.extend(50..100, &());
852
853 tree1.append(tree2, &());
854 assert_eq!(
855 tree1.items(&()),
856 (0..20).chain(50..100).collect::<Vec<u8>>()
857 );
858 }
859
860 #[test]
861 fn test_random() {
862 let mut starting_seed = 0;
863 if let Ok(value) = std::env::var("SEED") {
864 starting_seed = value.parse().expect("invalid SEED variable");
865 }
866 let mut num_iterations = 100;
867 if let Ok(value) = std::env::var("ITERATIONS") {
868 num_iterations = value.parse().expect("invalid ITERATIONS variable");
869 }
870 let num_operations = std::env::var("OPERATIONS")
871 .map_or(5, |o| o.parse().expect("invalid OPERATIONS variable"));
872
873 for seed in starting_seed..(starting_seed + num_iterations) {
874 eprintln!("seed = {}", seed);
875 let mut rng = StdRng::seed_from_u64(seed);
876
877 let rng = &mut rng;
878 let mut tree = SumTree::<u8>::new();
879 let count = rng.gen_range(0..10);
880 if rng.gen() {
881 tree.extend(rng.sample_iter(distributions::Standard).take(count), &());
882 } else {
883 let items = rng
884 .sample_iter(distributions::Standard)
885 .take(count)
886 .collect::<Vec<_>>();
887 tree.par_extend(items, &());
888 }
889
890 for _ in 0..num_operations {
891 let splice_end = rng.gen_range(0..tree.extent::<Count>(&()).0 + 1);
892 let splice_start = rng.gen_range(0..splice_end + 1);
893 let count = rng.gen_range(0..10);
894 let tree_end = tree.extent::<Count>(&());
895 let new_items = rng
896 .sample_iter(distributions::Standard)
897 .take(count)
898 .collect::<Vec<u8>>();
899
900 let mut reference_items = tree.items(&());
901 reference_items.splice(splice_start..splice_end, new_items.clone());
902
903 tree = {
904 let mut cursor = tree.cursor::<Count>();
905 let mut new_tree = cursor.slice(&Count(splice_start), Bias::Right, &());
906 if rng.gen() {
907 new_tree.extend(new_items, &());
908 } else {
909 new_tree.par_extend(new_items, &());
910 }
911 cursor.seek(&Count(splice_end), Bias::Right, &());
912 new_tree.append(cursor.slice(&tree_end, Bias::Right, &()), &());
913 new_tree
914 };
915
916 assert_eq!(tree.items(&()), reference_items);
917 assert_eq!(
918 tree.iter().collect::<Vec<_>>(),
919 tree.cursor::<()>().collect::<Vec<_>>()
920 );
921
922 log::info!("tree items: {:?}", tree.items(&()));
923
924 let mut filter_cursor = tree.filter::<_, Count>(|summary| summary.contains_even);
925 let expected_filtered_items = tree
926 .items(&())
927 .into_iter()
928 .enumerate()
929 .filter(|(_, item)| (item & 1) == 0)
930 .collect::<Vec<_>>();
931
932 let mut item_ix = if rng.gen() {
933 filter_cursor.next(&());
934 0
935 } else {
936 filter_cursor.prev(&());
937 expected_filtered_items.len().saturating_sub(1)
938 };
939 while item_ix < expected_filtered_items.len() {
940 log::info!("filter_cursor, item_ix: {}", item_ix);
941 let actual_item = filter_cursor.item().unwrap();
942 let (reference_index, reference_item) = expected_filtered_items[item_ix];
943 assert_eq!(actual_item, &reference_item);
944 assert_eq!(filter_cursor.start().0, reference_index);
945 log::info!("next");
946 filter_cursor.next(&());
947 item_ix += 1;
948
949 while item_ix > 0 && rng.gen_bool(0.2) {
950 log::info!("prev");
951 filter_cursor.prev(&());
952 item_ix -= 1;
953
954 if item_ix == 0 && rng.gen_bool(0.2) {
955 filter_cursor.prev(&());
956 assert_eq!(filter_cursor.item(), None);
957 assert_eq!(filter_cursor.start().0, 0);
958 filter_cursor.next(&());
959 }
960 }
961 }
962 assert_eq!(filter_cursor.item(), None);
963
964 let mut before_start = false;
965 let mut cursor = tree.cursor::<Count>();
966 let start_pos = rng.gen_range(0..=reference_items.len());
967 cursor.seek(&Count(start_pos), Bias::Right, &());
968 let mut pos = rng.gen_range(start_pos..=reference_items.len());
969 cursor.seek_forward(&Count(pos), Bias::Right, &());
970
971 for i in 0..10 {
972 assert_eq!(cursor.start().0, pos);
973
974 if pos > 0 {
975 assert_eq!(cursor.prev_item().unwrap(), &reference_items[pos - 1]);
976 } else {
977 assert_eq!(cursor.prev_item(), None);
978 }
979
980 if pos < reference_items.len() && !before_start {
981 assert_eq!(cursor.item().unwrap(), &reference_items[pos]);
982 } else {
983 assert_eq!(cursor.item(), None);
984 }
985
986 if before_start {
987 assert_eq!(cursor.next_item(), reference_items.get(0));
988 } else if pos + 1 < reference_items.len() {
989 assert_eq!(cursor.next_item().unwrap(), &reference_items[pos + 1]);
990 } else {
991 assert_eq!(cursor.next_item(), None);
992 }
993
994 if i < 5 {
995 cursor.next(&());
996 if pos < reference_items.len() {
997 pos += 1;
998 before_start = false;
999 }
1000 } else {
1001 cursor.prev(&());
1002 if pos == 0 {
1003 before_start = true;
1004 }
1005 pos = pos.saturating_sub(1);
1006 }
1007 }
1008 }
1009
1010 for _ in 0..10 {
1011 let end = rng.gen_range(0..tree.extent::<Count>(&()).0 + 1);
1012 let start = rng.gen_range(0..end + 1);
1013 let start_bias = if rng.gen() { Bias::Left } else { Bias::Right };
1014 let end_bias = if rng.gen() { Bias::Left } else { Bias::Right };
1015
1016 let mut cursor = tree.cursor::<Count>();
1017 cursor.seek(&Count(start), start_bias, &());
1018 let slice = cursor.slice(&Count(end), end_bias, &());
1019
1020 cursor.seek(&Count(start), start_bias, &());
1021 let summary = cursor.summary::<_, Sum>(&Count(end), end_bias, &());
1022
1023 assert_eq!(summary.0, slice.summary().sum);
1024 }
1025 }
1026 }
1027
1028 #[test]
1029 fn test_cursor() {
1030 // Empty tree
1031 let tree = SumTree::<u8>::new();
1032 let mut cursor = tree.cursor::<IntegersSummary>();
1033 assert_eq!(
1034 cursor.slice(&Count(0), Bias::Right, &()).items(&()),
1035 Vec::<u8>::new()
1036 );
1037 assert_eq!(cursor.item(), None);
1038 assert_eq!(cursor.prev_item(), None);
1039 assert_eq!(cursor.next_item(), None);
1040 assert_eq!(cursor.start().sum, 0);
1041 cursor.prev(&());
1042 assert_eq!(cursor.item(), None);
1043 assert_eq!(cursor.prev_item(), None);
1044 assert_eq!(cursor.next_item(), None);
1045 assert_eq!(cursor.start().sum, 0);
1046 cursor.next(&());
1047 assert_eq!(cursor.item(), None);
1048 assert_eq!(cursor.prev_item(), None);
1049 assert_eq!(cursor.next_item(), None);
1050 assert_eq!(cursor.start().sum, 0);
1051
1052 // Single-element tree
1053 let mut tree = SumTree::<u8>::new();
1054 tree.extend(vec![1], &());
1055 let mut cursor = tree.cursor::<IntegersSummary>();
1056 assert_eq!(
1057 cursor.slice(&Count(0), Bias::Right, &()).items(&()),
1058 Vec::<u8>::new()
1059 );
1060 assert_eq!(cursor.item(), Some(&1));
1061 assert_eq!(cursor.prev_item(), None);
1062 assert_eq!(cursor.next_item(), None);
1063 assert_eq!(cursor.start().sum, 0);
1064
1065 cursor.next(&());
1066 assert_eq!(cursor.item(), None);
1067 assert_eq!(cursor.prev_item(), Some(&1));
1068 assert_eq!(cursor.next_item(), None);
1069 assert_eq!(cursor.start().sum, 1);
1070
1071 cursor.prev(&());
1072 assert_eq!(cursor.item(), Some(&1));
1073 assert_eq!(cursor.prev_item(), None);
1074 assert_eq!(cursor.next_item(), None);
1075 assert_eq!(cursor.start().sum, 0);
1076
1077 let mut cursor = tree.cursor::<IntegersSummary>();
1078 assert_eq!(cursor.slice(&Count(1), Bias::Right, &()).items(&()), [1]);
1079 assert_eq!(cursor.item(), None);
1080 assert_eq!(cursor.prev_item(), Some(&1));
1081 assert_eq!(cursor.next_item(), None);
1082 assert_eq!(cursor.start().sum, 1);
1083
1084 cursor.seek(&Count(0), Bias::Right, &());
1085 assert_eq!(
1086 cursor
1087 .slice(&tree.extent::<Count>(&()), Bias::Right, &())
1088 .items(&()),
1089 [1]
1090 );
1091 assert_eq!(cursor.item(), None);
1092 assert_eq!(cursor.prev_item(), Some(&1));
1093 assert_eq!(cursor.next_item(), None);
1094 assert_eq!(cursor.start().sum, 1);
1095
1096 // Multiple-element tree
1097 let mut tree = SumTree::new();
1098 tree.extend(vec![1, 2, 3, 4, 5, 6], &());
1099 let mut cursor = tree.cursor::<IntegersSummary>();
1100
1101 assert_eq!(cursor.slice(&Count(2), Bias::Right, &()).items(&()), [1, 2]);
1102 assert_eq!(cursor.item(), Some(&3));
1103 assert_eq!(cursor.prev_item(), Some(&2));
1104 assert_eq!(cursor.next_item(), Some(&4));
1105 assert_eq!(cursor.start().sum, 3);
1106
1107 cursor.next(&());
1108 assert_eq!(cursor.item(), Some(&4));
1109 assert_eq!(cursor.prev_item(), Some(&3));
1110 assert_eq!(cursor.next_item(), Some(&5));
1111 assert_eq!(cursor.start().sum, 6);
1112
1113 cursor.next(&());
1114 assert_eq!(cursor.item(), Some(&5));
1115 assert_eq!(cursor.prev_item(), Some(&4));
1116 assert_eq!(cursor.next_item(), Some(&6));
1117 assert_eq!(cursor.start().sum, 10);
1118
1119 cursor.next(&());
1120 assert_eq!(cursor.item(), Some(&6));
1121 assert_eq!(cursor.prev_item(), Some(&5));
1122 assert_eq!(cursor.next_item(), None);
1123 assert_eq!(cursor.start().sum, 15);
1124
1125 cursor.next(&());
1126 cursor.next(&());
1127 assert_eq!(cursor.item(), None);
1128 assert_eq!(cursor.prev_item(), Some(&6));
1129 assert_eq!(cursor.next_item(), None);
1130 assert_eq!(cursor.start().sum, 21);
1131
1132 cursor.prev(&());
1133 assert_eq!(cursor.item(), Some(&6));
1134 assert_eq!(cursor.prev_item(), Some(&5));
1135 assert_eq!(cursor.next_item(), None);
1136 assert_eq!(cursor.start().sum, 15);
1137
1138 cursor.prev(&());
1139 assert_eq!(cursor.item(), Some(&5));
1140 assert_eq!(cursor.prev_item(), Some(&4));
1141 assert_eq!(cursor.next_item(), Some(&6));
1142 assert_eq!(cursor.start().sum, 10);
1143
1144 cursor.prev(&());
1145 assert_eq!(cursor.item(), Some(&4));
1146 assert_eq!(cursor.prev_item(), Some(&3));
1147 assert_eq!(cursor.next_item(), Some(&5));
1148 assert_eq!(cursor.start().sum, 6);
1149
1150 cursor.prev(&());
1151 assert_eq!(cursor.item(), Some(&3));
1152 assert_eq!(cursor.prev_item(), Some(&2));
1153 assert_eq!(cursor.next_item(), Some(&4));
1154 assert_eq!(cursor.start().sum, 3);
1155
1156 cursor.prev(&());
1157 assert_eq!(cursor.item(), Some(&2));
1158 assert_eq!(cursor.prev_item(), Some(&1));
1159 assert_eq!(cursor.next_item(), Some(&3));
1160 assert_eq!(cursor.start().sum, 1);
1161
1162 cursor.prev(&());
1163 assert_eq!(cursor.item(), Some(&1));
1164 assert_eq!(cursor.prev_item(), None);
1165 assert_eq!(cursor.next_item(), Some(&2));
1166 assert_eq!(cursor.start().sum, 0);
1167
1168 cursor.prev(&());
1169 assert_eq!(cursor.item(), None);
1170 assert_eq!(cursor.prev_item(), None);
1171 assert_eq!(cursor.next_item(), Some(&1));
1172 assert_eq!(cursor.start().sum, 0);
1173
1174 cursor.next(&());
1175 assert_eq!(cursor.item(), Some(&1));
1176 assert_eq!(cursor.prev_item(), None);
1177 assert_eq!(cursor.next_item(), Some(&2));
1178 assert_eq!(cursor.start().sum, 0);
1179
1180 let mut cursor = tree.cursor::<IntegersSummary>();
1181 assert_eq!(
1182 cursor
1183 .slice(&tree.extent::<Count>(&()), Bias::Right, &())
1184 .items(&()),
1185 tree.items(&())
1186 );
1187 assert_eq!(cursor.item(), None);
1188 assert_eq!(cursor.prev_item(), Some(&6));
1189 assert_eq!(cursor.next_item(), None);
1190 assert_eq!(cursor.start().sum, 21);
1191
1192 cursor.seek(&Count(3), Bias::Right, &());
1193 assert_eq!(
1194 cursor
1195 .slice(&tree.extent::<Count>(&()), Bias::Right, &())
1196 .items(&()),
1197 [4, 5, 6]
1198 );
1199 assert_eq!(cursor.item(), None);
1200 assert_eq!(cursor.prev_item(), Some(&6));
1201 assert_eq!(cursor.next_item(), None);
1202 assert_eq!(cursor.start().sum, 21);
1203
1204 // Seeking can bias left or right
1205 cursor.seek(&Count(1), Bias::Left, &());
1206 assert_eq!(cursor.item(), Some(&1));
1207 cursor.seek(&Count(1), Bias::Right, &());
1208 assert_eq!(cursor.item(), Some(&2));
1209
1210 // Slicing without resetting starts from where the cursor is parked at.
1211 cursor.seek(&Count(1), Bias::Right, &());
1212 assert_eq!(
1213 cursor.slice(&Count(3), Bias::Right, &()).items(&()),
1214 vec![2, 3]
1215 );
1216 assert_eq!(
1217 cursor.slice(&Count(6), Bias::Left, &()).items(&()),
1218 vec![4, 5]
1219 );
1220 assert_eq!(
1221 cursor.slice(&Count(6), Bias::Right, &()).items(&()),
1222 vec![6]
1223 );
1224 }
1225
1226 #[test]
1227 fn test_edit() {
1228 let mut tree = SumTree::<u8>::new();
1229
1230 let removed = tree.edit(vec![Edit::Insert(1), Edit::Insert(2), Edit::Insert(0)], &());
1231 assert_eq!(tree.items(&()), vec![0, 1, 2]);
1232 assert_eq!(removed, Vec::<u8>::new());
1233 assert_eq!(tree.get(&0, &()), Some(&0));
1234 assert_eq!(tree.get(&1, &()), Some(&1));
1235 assert_eq!(tree.get(&2, &()), Some(&2));
1236 assert_eq!(tree.get(&4, &()), None);
1237
1238 let removed = tree.edit(vec![Edit::Insert(2), Edit::Insert(4), Edit::Remove(0)], &());
1239 assert_eq!(tree.items(&()), vec![1, 2, 4]);
1240 assert_eq!(removed, vec![0, 2]);
1241 assert_eq!(tree.get(&0, &()), None);
1242 assert_eq!(tree.get(&1, &()), Some(&1));
1243 assert_eq!(tree.get(&2, &()), Some(&2));
1244 assert_eq!(tree.get(&4, &()), Some(&4));
1245 }
1246
1247 #[test]
1248 fn test_from_iter() {
1249 assert_eq!(
1250 SumTree::from_iter(0..100, &()).items(&()),
1251 (0..100).collect::<Vec<_>>()
1252 );
1253
1254 // Ensure `from_iter` works correctly when the given iterator restarts
1255 // after calling `next` if `None` was already returned.
1256 let mut ix = 0;
1257 let iterator = std::iter::from_fn(|| {
1258 ix = (ix + 1) % 2;
1259 if ix == 1 {
1260 Some(1)
1261 } else {
1262 None
1263 }
1264 });
1265 assert_eq!(SumTree::from_iter(iterator, &()).items(&()), vec![1]);
1266 }
1267
1268 #[derive(Clone, Default, Debug)]
1269 pub struct IntegersSummary {
1270 count: usize,
1271 sum: usize,
1272 contains_even: bool,
1273 max: u8,
1274 }
1275
1276 #[derive(Ord, PartialOrd, Default, Eq, PartialEq, Clone, Debug)]
1277 struct Count(usize);
1278
1279 #[derive(Ord, PartialOrd, Default, Eq, PartialEq, Clone, Debug)]
1280 struct Sum(usize);
1281
1282 impl Item for u8 {
1283 type Summary = IntegersSummary;
1284
1285 fn summary(&self) -> Self::Summary {
1286 IntegersSummary {
1287 count: 1,
1288 sum: *self as usize,
1289 contains_even: (*self & 1) == 0,
1290 max: *self,
1291 }
1292 }
1293 }
1294
1295 impl KeyedItem for u8 {
1296 type Key = u8;
1297
1298 fn key(&self) -> Self::Key {
1299 *self
1300 }
1301 }
1302
1303 impl Summary for IntegersSummary {
1304 type Context = ();
1305
1306 fn add_summary(&mut self, other: &Self, _: &()) {
1307 self.count += other.count;
1308 self.sum += other.sum;
1309 self.contains_even |= other.contains_even;
1310 self.max = cmp::max(self.max, other.max);
1311 }
1312 }
1313
1314 impl<'a> Dimension<'a, IntegersSummary> for u8 {
1315 fn add_summary(&mut self, summary: &IntegersSummary, _: &()) {
1316 *self = summary.max;
1317 }
1318 }
1319
1320 impl<'a> Dimension<'a, IntegersSummary> for Count {
1321 fn add_summary(&mut self, summary: &IntegersSummary, _: &()) {
1322 self.0 += summary.count;
1323 }
1324 }
1325
1326 impl<'a> SeekTarget<'a, IntegersSummary, IntegersSummary> for Count {
1327 fn cmp(&self, cursor_location: &IntegersSummary, _: &()) -> Ordering {
1328 self.0.cmp(&cursor_location.count)
1329 }
1330 }
1331
1332 impl<'a> Dimension<'a, IntegersSummary> for Sum {
1333 fn add_summary(&mut self, summary: &IntegersSummary, _: &()) {
1334 self.0 += summary.sum;
1335 }
1336 }
1337}