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