1mod cursor;
2mod tree_map;
3
4use arrayvec::ArrayVec;
5pub use cursor::{Cursor, FilterCursor, Iter};
6use rayon::iter::{IndexedParallelIterator, IntoParallelIterator, ParallelIterator as _};
7use std::marker::PhantomData;
8use std::mem;
9use std::{cmp::Ordering, fmt, iter::FromIterator, sync::Arc};
10pub use tree_map::{MapSeekTarget, TreeMap, TreeSet};
11use ztracing::instrument;
12
13#[cfg(test)]
14pub const TREE_BASE: usize = 2;
15#[cfg(not(test))]
16pub const TREE_BASE: usize = 6;
17
18/// An item that can be stored in a [`SumTree`]
19///
20/// Must be summarized by a type that implements [`Summary`]
21pub trait Item: Clone {
22 type Summary: Summary;
23
24 fn summary(&self, cx: <Self::Summary as Summary>::Context<'_>) -> Self::Summary;
25}
26
27/// An [`Item`] whose summary has a specific key that can be used to identify it
28pub trait KeyedItem: Item {
29 type Key: for<'a> Dimension<'a, Self::Summary> + Ord;
30
31 fn key(&self) -> Self::Key;
32}
33
34/// A type that describes the Sum of all [`Item`]s in a subtree of the [`SumTree`]
35///
36/// Each Summary type can have multiple [`Dimension`]s that it measures,
37/// which can be used to navigate the tree
38pub trait Summary: Clone {
39 type Context<'a>: Copy;
40 fn zero<'a>(cx: Self::Context<'a>) -> Self;
41 fn add_summary<'a>(&mut self, summary: &Self, cx: Self::Context<'a>);
42}
43
44pub trait ContextLessSummary: Clone {
45 fn zero() -> Self;
46 fn add_summary(&mut self, summary: &Self);
47}
48
49impl<T: ContextLessSummary> Summary for T {
50 type Context<'a> = ();
51
52 fn zero<'a>((): ()) -> Self {
53 T::zero()
54 }
55
56 fn add_summary<'a>(&mut self, summary: &Self, (): ()) {
57 T::add_summary(self, summary)
58 }
59}
60
61#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
62pub struct NoSummary;
63
64/// Catch-all implementation for when you need something that implements [`Summary`] without a specific type.
65/// We implement it on a `NoSummary` instead of re-using `()`, as that avoids blanket impl collisions with `impl<T: Summary> Dimension for T`
66/// (as we also need unit type to be a fill-in dimension)
67impl ContextLessSummary for NoSummary {
68 fn zero() -> Self {
69 NoSummary
70 }
71
72 fn add_summary(&mut self, _: &Self) {}
73}
74
75/// Each [`Summary`] type can have more than one [`Dimension`] type that it measures.
76///
77/// You can use dimensions to seek to a specific location in the [`SumTree`]
78///
79/// # Example:
80/// Zed's rope has a `TextSummary` type that summarizes lines, characters, and bytes.
81/// Each of these are different dimensions we may want to seek to
82pub trait Dimension<'a, S: Summary>: Clone {
83 fn zero(cx: S::Context<'_>) -> Self;
84
85 fn add_summary(&mut self, summary: &'a S, cx: S::Context<'_>);
86 #[must_use]
87 fn with_added_summary(mut self, summary: &'a S, cx: S::Context<'_>) -> Self {
88 self.add_summary(summary, cx);
89 self
90 }
91
92 fn from_summary(summary: &'a S, cx: S::Context<'_>) -> Self {
93 let mut dimension = Self::zero(cx);
94 dimension.add_summary(summary, cx);
95 dimension
96 }
97}
98
99impl<'a, T: Summary> Dimension<'a, T> for T {
100 fn zero(cx: T::Context<'_>) -> Self {
101 Summary::zero(cx)
102 }
103
104 fn add_summary(&mut self, summary: &'a T, cx: T::Context<'_>) {
105 Summary::add_summary(self, summary, cx);
106 }
107}
108
109pub trait SeekTarget<'a, S: Summary, D: Dimension<'a, S>> {
110 fn cmp(&self, cursor_location: &D, cx: S::Context<'_>) -> Ordering;
111}
112
113impl<'a, S: Summary, D: Dimension<'a, S> + Ord> SeekTarget<'a, S, D> for D {
114 fn cmp(&self, cursor_location: &Self, _: S::Context<'_>) -> Ordering {
115 Ord::cmp(self, cursor_location)
116 }
117}
118
119impl<'a, T: Summary> Dimension<'a, T> for () {
120 fn zero(_: T::Context<'_>) -> Self {}
121
122 fn add_summary(&mut self, _: &'a T, _: T::Context<'_>) {}
123}
124
125#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
126pub struct Dimensions<D1, D2, D3 = ()>(pub D1, pub D2, pub D3);
127
128impl<'a, T: Summary, D1: Dimension<'a, T>, D2: Dimension<'a, T>, D3: Dimension<'a, T>>
129 Dimension<'a, T> for Dimensions<D1, D2, D3>
130{
131 fn zero(cx: T::Context<'_>) -> Self {
132 Dimensions(D1::zero(cx), D2::zero(cx), D3::zero(cx))
133 }
134
135 fn add_summary(&mut self, summary: &'a T, cx: T::Context<'_>) {
136 self.0.add_summary(summary, cx);
137 self.1.add_summary(summary, cx);
138 self.2.add_summary(summary, cx);
139 }
140}
141
142impl<'a, S, D1, D2, D3> SeekTarget<'a, S, Dimensions<D1, D2, D3>> for D1
143where
144 S: Summary,
145 D1: SeekTarget<'a, S, D1> + Dimension<'a, S>,
146 D2: Dimension<'a, S>,
147 D3: Dimension<'a, S>,
148{
149 fn cmp(&self, cursor_location: &Dimensions<D1, D2, D3>, cx: S::Context<'_>) -> Ordering {
150 self.cmp(&cursor_location.0, cx)
151 }
152}
153
154/// Bias is used to settle ambiguities when determining positions in an ordered sequence.
155///
156/// The primary use case is for text, where Bias influences
157/// which character an offset or anchor is associated with.
158///
159/// # Examples
160/// Given the buffer `AˇBCD`:
161/// - The offset of the cursor is 1
162/// - [Bias::Left] would attach the cursor to the character `A`
163/// - [Bias::Right] would attach the cursor to the character `B`
164///
165/// Given the buffer `A«BCˇ»D`:
166/// - The offset of the cursor is 3, and the selection is from 1 to 3
167/// - The left anchor of the selection has [Bias::Right], attaching it to the character `B`
168/// - The right anchor of the selection has [Bias::Left], attaching it to the character `C`
169///
170/// Given the buffer `{ˇ<...>`, where `<...>` is a folded region:
171/// - The display offset of the cursor is 1, but the offset in the buffer is determined by the bias
172/// - [Bias::Left] would attach the cursor to the character `{`, with a buffer offset of 1
173/// - [Bias::Right] would attach the cursor to the first character of the folded region,
174/// and the buffer offset would be the offset of the first character of the folded region
175#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Debug, Hash, Default)]
176pub enum Bias {
177 /// Attach to the character on the left
178 #[default]
179 Left,
180 /// Attach to the character on the right
181 Right,
182}
183
184impl Bias {
185 pub fn invert(self) -> Self {
186 match self {
187 Self::Left => Self::Right,
188 Self::Right => Self::Left,
189 }
190 }
191}
192
193/// A B+ tree in which each leaf node contains `Item`s of type `T` and a `Summary`s for each `Item`.
194/// Each internal node contains a `Summary` of the items in its subtree.
195///
196/// The maximum number of items per node is `TREE_BASE * 2`.
197///
198/// Any [`Dimension`] supported by the [`Summary`] type can be used to seek to a specific location in the tree.
199#[derive(Clone)]
200pub struct SumTree<T: Item>(Arc<Node<T>>);
201
202impl<T> fmt::Debug for SumTree<T>
203where
204 T: fmt::Debug + Item,
205 T::Summary: fmt::Debug,
206{
207 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
208 f.debug_tuple("SumTree").field(&self.0).finish()
209 }
210}
211
212impl<T: Item> SumTree<T> {
213 pub fn new(cx: <T::Summary as Summary>::Context<'_>) -> Self {
214 SumTree(Arc::new(Node::Leaf {
215 summary: <T::Summary as Summary>::zero(cx),
216 items: ArrayVec::new(),
217 item_summaries: ArrayVec::new(),
218 }))
219 }
220
221 /// Useful in cases where the item type has a non-trivial context type, but the zero value of the summary type doesn't depend on that context.
222 pub fn from_summary(summary: T::Summary) -> Self {
223 SumTree(Arc::new(Node::Leaf {
224 summary,
225 items: ArrayVec::new(),
226 item_summaries: ArrayVec::new(),
227 }))
228 }
229
230 pub fn from_item(item: T, cx: <T::Summary as Summary>::Context<'_>) -> Self {
231 let mut tree = Self::new(cx);
232 tree.push(item, cx);
233 tree
234 }
235
236 pub fn from_iter<I: IntoIterator<Item = T>>(
237 iter: I,
238 cx: <T::Summary as Summary>::Context<'_>,
239 ) -> Self {
240 let mut nodes = Vec::new();
241
242 let mut iter = iter.into_iter().fuse().peekable();
243 while iter.peek().is_some() {
244 let items: ArrayVec<T, { 2 * TREE_BASE }> = iter.by_ref().take(2 * TREE_BASE).collect();
245 let item_summaries: ArrayVec<T::Summary, { 2 * TREE_BASE }> =
246 items.iter().map(|item| item.summary(cx)).collect();
247
248 let mut summary = item_summaries[0].clone();
249 for item_summary in &item_summaries[1..] {
250 <T::Summary as Summary>::add_summary(&mut summary, item_summary, cx);
251 }
252
253 nodes.push(SumTree(Arc::new(Node::Leaf {
254 summary,
255 items,
256 item_summaries,
257 })));
258 }
259
260 let mut parent_nodes = Vec::new();
261 let mut height = 0;
262 while nodes.len() > 1 {
263 height += 1;
264 let mut current_parent_node = None;
265 for child_node in nodes.drain(..) {
266 let parent_node = current_parent_node.get_or_insert_with(|| {
267 SumTree(Arc::new(Node::Internal {
268 summary: <T::Summary as Summary>::zero(cx),
269 height,
270 child_summaries: ArrayVec::new(),
271 child_trees: ArrayVec::new(),
272 }))
273 });
274 let Node::Internal {
275 summary,
276 child_summaries,
277 child_trees,
278 ..
279 } = Arc::get_mut(&mut parent_node.0).unwrap()
280 else {
281 unreachable!()
282 };
283 let child_summary = child_node.summary();
284 <T::Summary as Summary>::add_summary(summary, child_summary, cx);
285 child_summaries.push(child_summary.clone());
286 child_trees.push(child_node);
287
288 if child_trees.len() == 2 * TREE_BASE {
289 parent_nodes.extend(current_parent_node.take());
290 }
291 }
292 parent_nodes.extend(current_parent_node.take());
293 mem::swap(&mut nodes, &mut parent_nodes);
294 }
295
296 if nodes.is_empty() {
297 Self::new(cx)
298 } else {
299 debug_assert_eq!(nodes.len(), 1);
300 nodes.pop().unwrap()
301 }
302 }
303
304 pub fn from_par_iter<I, Iter>(iter: I, cx: <T::Summary as Summary>::Context<'_>) -> Self
305 where
306 I: IntoParallelIterator<Iter = Iter>,
307 Iter: IndexedParallelIterator<Item = T>,
308 T: Send + Sync,
309 T::Summary: Send + Sync,
310 for<'a> <T::Summary as Summary>::Context<'a>: Sync,
311 {
312 let mut nodes = iter
313 .into_par_iter()
314 .chunks(2 * TREE_BASE)
315 .map(|items| {
316 let items: ArrayVec<T, { 2 * TREE_BASE }> = items.into_iter().collect();
317 let item_summaries: ArrayVec<T::Summary, { 2 * TREE_BASE }> =
318 items.iter().map(|item| item.summary(cx)).collect();
319 let mut summary = item_summaries[0].clone();
320 for item_summary in &item_summaries[1..] {
321 <T::Summary as Summary>::add_summary(&mut summary, item_summary, cx);
322 }
323 SumTree(Arc::new(Node::Leaf {
324 summary,
325 items,
326 item_summaries,
327 }))
328 })
329 .collect::<Vec<_>>();
330
331 let mut height = 0;
332 while nodes.len() > 1 {
333 height += 1;
334 nodes = nodes
335 .into_par_iter()
336 .chunks(2 * TREE_BASE)
337 .map(|child_nodes| {
338 let child_trees: ArrayVec<SumTree<T>, { 2 * TREE_BASE }> =
339 child_nodes.into_iter().collect();
340 let child_summaries: ArrayVec<T::Summary, { 2 * TREE_BASE }> = child_trees
341 .iter()
342 .map(|child_tree| child_tree.summary().clone())
343 .collect();
344 let mut summary = child_summaries[0].clone();
345 for child_summary in &child_summaries[1..] {
346 <T::Summary as Summary>::add_summary(&mut summary, child_summary, cx);
347 }
348 SumTree(Arc::new(Node::Internal {
349 height,
350 summary,
351 child_summaries,
352 child_trees,
353 }))
354 })
355 .collect::<Vec<_>>();
356 }
357
358 if nodes.is_empty() {
359 Self::new(cx)
360 } else {
361 debug_assert_eq!(nodes.len(), 1);
362 nodes.pop().unwrap()
363 }
364 }
365
366 #[allow(unused)]
367 pub fn items<'a>(&'a self, cx: <T::Summary as Summary>::Context<'a>) -> Vec<T> {
368 let mut items = Vec::new();
369 let mut cursor = self.cursor::<()>(cx);
370 cursor.next();
371 while let Some(item) = cursor.item() {
372 items.push(item.clone());
373 cursor.next();
374 }
375 items
376 }
377
378 pub fn iter(&self) -> Iter<'_, T> {
379 Iter::new(self)
380 }
381
382 /// A more efficient version of `Cursor::new()` + `Cursor::seek()` + `Cursor::item()`.
383 ///
384 /// Only returns the item that exactly has the target match.
385 #[instrument(skip_all)]
386 pub fn find_exact<'a, 'slf, D, Target>(
387 &'slf self,
388 cx: <T::Summary as Summary>::Context<'a>,
389 target: &Target,
390 bias: Bias,
391 ) -> (D, D, Option<&'slf T>)
392 where
393 D: Dimension<'slf, T::Summary>,
394 Target: SeekTarget<'slf, T::Summary, D>,
395 {
396 let tree_end = D::zero(cx).with_added_summary(self.summary(), cx);
397 let comparison = target.cmp(&tree_end, cx);
398 if comparison == Ordering::Greater || (comparison == Ordering::Equal && bias == Bias::Right)
399 {
400 return (tree_end.clone(), tree_end, None);
401 }
402
403 let mut pos = D::zero(cx);
404 return match Self::find_iterate::<_, _, true>(cx, target, bias, &mut pos, self) {
405 Some((item, end)) => (pos, end, Some(item)),
406 None => (pos.clone(), pos, None),
407 };
408 }
409
410 /// A more efficient version of `Cursor::new()` + `Cursor::seek()` + `Cursor::item()`
411 #[instrument(skip_all)]
412 pub fn find<'a, 'slf, D, Target>(
413 &'slf self,
414 cx: <T::Summary as Summary>::Context<'a>,
415 target: &Target,
416 bias: Bias,
417 ) -> (D, D, Option<&'slf T>)
418 where
419 D: Dimension<'slf, T::Summary>,
420 Target: SeekTarget<'slf, T::Summary, D>,
421 {
422 let tree_end = D::zero(cx).with_added_summary(self.summary(), cx);
423 let comparison = target.cmp(&tree_end, cx);
424 if comparison == Ordering::Greater || (comparison == Ordering::Equal && bias == Bias::Right)
425 {
426 return (tree_end.clone(), tree_end, None);
427 }
428
429 let mut pos = D::zero(cx);
430 return match Self::find_iterate::<_, _, false>(cx, target, bias, &mut pos, self) {
431 Some((item, end)) => (pos, end, Some(item)),
432 None => (pos.clone(), pos, None),
433 };
434 }
435
436 fn find_iterate<'tree, 'a, D, Target, const EXACT: bool>(
437 cx: <T::Summary as Summary>::Context<'a>,
438 target: &Target,
439 bias: Bias,
440 position: &mut D,
441 mut this: &'tree SumTree<T>,
442 ) -> Option<(&'tree T, D)>
443 where
444 D: Dimension<'tree, T::Summary>,
445 Target: SeekTarget<'tree, T::Summary, D>,
446 {
447 'iterate: loop {
448 match &*this.0 {
449 Node::Internal {
450 child_summaries,
451 child_trees,
452 ..
453 } => {
454 for (child_tree, child_summary) in child_trees.iter().zip(child_summaries) {
455 let child_end = position.clone().with_added_summary(child_summary, cx);
456
457 let comparison = target.cmp(&child_end, cx);
458 let target_in_child = comparison == Ordering::Less
459 || (comparison == Ordering::Equal && bias == Bias::Left);
460 if target_in_child {
461 this = child_tree;
462 continue 'iterate;
463 }
464 *position = child_end;
465 }
466 }
467 Node::Leaf {
468 items,
469 item_summaries,
470 ..
471 } => {
472 for (item, item_summary) in items.iter().zip(item_summaries) {
473 let mut child_end = position.clone();
474 child_end.add_summary(item_summary, cx);
475
476 let comparison = target.cmp(&child_end, cx);
477 let entry_found = if EXACT {
478 comparison == Ordering::Equal
479 } else {
480 comparison == Ordering::Less
481 || (comparison == Ordering::Equal && bias == Bias::Left)
482 };
483 if entry_found {
484 return Some((item, child_end));
485 }
486
487 *position = child_end;
488 }
489 }
490 }
491 return None;
492 }
493 }
494
495 /// A more efficient version of `Cursor::new()` + `Cursor::seek()` + `Cursor::item()`
496 #[instrument(skip_all)]
497 pub fn find_with_prev<'a, 'slf, D, Target>(
498 &'slf self,
499 cx: <T::Summary as Summary>::Context<'a>,
500 target: &Target,
501 bias: Bias,
502 ) -> (D, D, Option<(Option<&'slf T>, &'slf T)>)
503 where
504 D: Dimension<'slf, T::Summary>,
505 Target: SeekTarget<'slf, T::Summary, D>,
506 {
507 let tree_end = D::zero(cx).with_added_summary(self.summary(), cx);
508 let comparison = target.cmp(&tree_end, cx);
509 if comparison == Ordering::Greater || (comparison == Ordering::Equal && bias == Bias::Right)
510 {
511 return (tree_end.clone(), tree_end, None);
512 }
513
514 let mut pos = D::zero(cx);
515 return match Self::find_with_prev_iterate::<_, _, false>(cx, target, bias, &mut pos, self) {
516 Some((prev, item, end)) => (pos, end, Some((prev, item))),
517 None => (pos.clone(), pos, None),
518 };
519 }
520
521 fn find_with_prev_iterate<'tree, 'a, D, Target, const EXACT: bool>(
522 cx: <T::Summary as Summary>::Context<'a>,
523 target: &Target,
524 bias: Bias,
525 position: &mut D,
526 mut this: &'tree SumTree<T>,
527 ) -> Option<(Option<&'tree T>, &'tree T, D)>
528 where
529 D: Dimension<'tree, T::Summary>,
530 Target: SeekTarget<'tree, T::Summary, D>,
531 {
532 let mut prev = None;
533 'iterate: loop {
534 match &*this.0 {
535 Node::Internal {
536 child_summaries,
537 child_trees,
538 ..
539 } => {
540 for (child_tree, child_summary) in child_trees.iter().zip(child_summaries) {
541 let child_end = position.clone().with_added_summary(child_summary, cx);
542
543 let comparison = target.cmp(&child_end, cx);
544 let target_in_child = comparison == Ordering::Less
545 || (comparison == Ordering::Equal && bias == Bias::Left);
546 if target_in_child {
547 this = child_tree;
548 continue 'iterate;
549 }
550 prev = child_tree.last();
551 *position = child_end;
552 }
553 }
554 Node::Leaf {
555 items,
556 item_summaries,
557 ..
558 } => {
559 for (item, item_summary) in items.iter().zip(item_summaries) {
560 let mut child_end = position.clone();
561 child_end.add_summary(item_summary, cx);
562
563 let comparison = target.cmp(&child_end, cx);
564 let entry_found = if EXACT {
565 comparison == Ordering::Equal
566 } else {
567 comparison == Ordering::Less
568 || (comparison == Ordering::Equal && bias == Bias::Left)
569 };
570 if entry_found {
571 return Some((prev, item, child_end));
572 }
573
574 prev = Some(item);
575 *position = child_end;
576 }
577 }
578 }
579 return None;
580 }
581 }
582
583 pub fn cursor<'a, 'b, D>(
584 &'a self,
585 cx: <T::Summary as Summary>::Context<'b>,
586 ) -> Cursor<'a, 'b, T, D>
587 where
588 D: Dimension<'a, T::Summary>,
589 {
590 Cursor::new(self, cx)
591 }
592
593 /// Note: If the summary type requires a non `()` context, then the filter cursor
594 /// that is returned cannot be used with Rust's iterators.
595 pub fn filter<'a, 'b, F, U>(
596 &'a self,
597 cx: <T::Summary as Summary>::Context<'b>,
598 filter_node: F,
599 ) -> FilterCursor<'a, 'b, F, T, U>
600 where
601 F: FnMut(&T::Summary) -> bool,
602 U: Dimension<'a, T::Summary>,
603 {
604 FilterCursor::new(self, cx, filter_node)
605 }
606
607 #[allow(dead_code)]
608 pub fn first(&self) -> Option<&T> {
609 self.leftmost_leaf().0.items().first()
610 }
611
612 pub fn last(&self) -> Option<&T> {
613 self.rightmost_leaf().0.items().last()
614 }
615
616 pub fn last_summary(&self) -> Option<&T::Summary> {
617 self.rightmost_leaf().0.child_summaries().last()
618 }
619
620 pub fn update_last(
621 &mut self,
622 f: impl FnOnce(&mut T),
623 cx: <T::Summary as Summary>::Context<'_>,
624 ) {
625 self.update_last_recursive(f, cx);
626 }
627
628 fn update_last_recursive(
629 &mut self,
630 f: impl FnOnce(&mut T),
631 cx: <T::Summary as Summary>::Context<'_>,
632 ) -> Option<T::Summary> {
633 match Arc::make_mut(&mut self.0) {
634 Node::Internal {
635 summary,
636 child_summaries,
637 child_trees,
638 ..
639 } => {
640 let last_summary = child_summaries.last_mut().unwrap();
641 let last_child = child_trees.last_mut().unwrap();
642 *last_summary = last_child.update_last_recursive(f, cx).unwrap();
643 *summary = sum(child_summaries.iter(), cx);
644 Some(summary.clone())
645 }
646 Node::Leaf {
647 summary,
648 items,
649 item_summaries,
650 } => {
651 if let Some((item, item_summary)) = items.last_mut().zip(item_summaries.last_mut())
652 {
653 (f)(item);
654 *item_summary = item.summary(cx);
655 *summary = sum(item_summaries.iter(), cx);
656 Some(summary.clone())
657 } else {
658 None
659 }
660 }
661 }
662 }
663
664 pub fn update_first(
665 &mut self,
666 f: impl FnOnce(&mut T),
667 cx: <T::Summary as Summary>::Context<'_>,
668 ) {
669 self.update_first_recursive(f, cx);
670 }
671
672 fn update_first_recursive(
673 &mut self,
674 f: impl FnOnce(&mut T),
675 cx: <T::Summary as Summary>::Context<'_>,
676 ) -> Option<T::Summary> {
677 match Arc::make_mut(&mut self.0) {
678 Node::Internal {
679 summary,
680 child_summaries,
681 child_trees,
682 ..
683 } => {
684 let first_summary = child_summaries.first_mut().unwrap();
685 let first_child = child_trees.first_mut().unwrap();
686 *first_summary = first_child.update_first_recursive(f, cx).unwrap();
687 *summary = sum(child_summaries.iter(), cx);
688 Some(summary.clone())
689 }
690 Node::Leaf {
691 summary,
692 items,
693 item_summaries,
694 } => {
695 if let Some((item, item_summary)) =
696 items.first_mut().zip(item_summaries.first_mut())
697 {
698 (f)(item);
699 *item_summary = item.summary(cx);
700 *summary = sum(item_summaries.iter(), cx);
701 Some(summary.clone())
702 } else {
703 None
704 }
705 }
706 }
707 }
708
709 pub fn extent<'a, D: Dimension<'a, T::Summary>>(
710 &'a self,
711 cx: <T::Summary as Summary>::Context<'_>,
712 ) -> D {
713 let mut extent = D::zero(cx);
714 match self.0.as_ref() {
715 Node::Internal { summary, .. } | Node::Leaf { summary, .. } => {
716 extent.add_summary(summary, cx);
717 }
718 }
719 extent
720 }
721
722 pub fn summary(&self) -> &T::Summary {
723 match self.0.as_ref() {
724 Node::Internal { summary, .. } => summary,
725 Node::Leaf { summary, .. } => summary,
726 }
727 }
728
729 pub fn is_empty(&self) -> bool {
730 match self.0.as_ref() {
731 Node::Internal { .. } => false,
732 Node::Leaf { items, .. } => items.is_empty(),
733 }
734 }
735
736 pub fn extend<I>(&mut self, iter: I, cx: <T::Summary as Summary>::Context<'_>)
737 where
738 I: IntoIterator<Item = T>,
739 {
740 self.append(Self::from_iter(iter, cx), cx);
741 }
742
743 pub fn par_extend<I, Iter>(&mut self, iter: I, cx: <T::Summary as Summary>::Context<'_>)
744 where
745 I: IntoParallelIterator<Iter = Iter>,
746 Iter: IndexedParallelIterator<Item = T>,
747 T: Send + Sync,
748 T::Summary: Send + Sync,
749 for<'a> <T::Summary as Summary>::Context<'a>: Sync,
750 {
751 self.append(Self::from_par_iter(iter, cx), cx);
752 }
753
754 pub fn push(&mut self, item: T, cx: <T::Summary as Summary>::Context<'_>) {
755 let summary = item.summary(cx);
756 self.append(
757 SumTree(Arc::new(Node::Leaf {
758 summary: summary.clone(),
759 items: ArrayVec::from_iter(Some(item)),
760 item_summaries: ArrayVec::from_iter(Some(summary)),
761 })),
762 cx,
763 );
764 }
765
766 pub fn append(&mut self, mut other: Self, cx: <T::Summary as Summary>::Context<'_>) {
767 if self.is_empty() {
768 *self = other;
769 } else if !other.0.is_leaf() || !other.0.items().is_empty() {
770 if self.0.height() < other.0.height() {
771 if let Some(tree) = Self::append_large(self.clone(), &mut other, cx) {
772 *self = Self::from_child_trees(tree, other, cx);
773 } else {
774 *self = other;
775 }
776 } else if let Some(split_tree) = self.push_tree_recursive(other, cx) {
777 *self = Self::from_child_trees(self.clone(), split_tree, cx);
778 }
779 }
780 }
781
782 fn push_tree_recursive(
783 &mut self,
784 other: SumTree<T>,
785 cx: <T::Summary as Summary>::Context<'_>,
786 ) -> Option<SumTree<T>> {
787 match Arc::make_mut(&mut self.0) {
788 Node::Internal {
789 height,
790 summary,
791 child_summaries,
792 child_trees,
793 ..
794 } => {
795 let other_node = other.0.clone();
796 <T::Summary as Summary>::add_summary(summary, other_node.summary(), cx);
797
798 let height_delta = *height - other_node.height();
799 let mut summaries_to_append = ArrayVec::<T::Summary, { 2 * TREE_BASE }>::new();
800 let mut trees_to_append = ArrayVec::<SumTree<T>, { 2 * TREE_BASE }>::new();
801 if height_delta == 0 {
802 summaries_to_append.extend(other_node.child_summaries().iter().cloned());
803 trees_to_append.extend(other_node.child_trees().iter().cloned());
804 } else if height_delta == 1 && !other_node.is_underflowing() {
805 summaries_to_append.push(other_node.summary().clone());
806 trees_to_append.push(other)
807 } else {
808 let tree_to_append = child_trees
809 .last_mut()
810 .unwrap()
811 .push_tree_recursive(other, cx);
812 *child_summaries.last_mut().unwrap() =
813 child_trees.last().unwrap().0.summary().clone();
814
815 if let Some(split_tree) = tree_to_append {
816 summaries_to_append.push(split_tree.0.summary().clone());
817 trees_to_append.push(split_tree);
818 }
819 }
820
821 let child_count = child_trees.len() + trees_to_append.len();
822 if child_count > 2 * TREE_BASE {
823 let left_summaries: ArrayVec<_, { 2 * TREE_BASE }>;
824 let right_summaries: ArrayVec<_, { 2 * TREE_BASE }>;
825 let left_trees;
826 let right_trees;
827
828 let midpoint = (child_count + child_count % 2) / 2;
829 {
830 let mut all_summaries = child_summaries
831 .iter()
832 .chain(summaries_to_append.iter())
833 .cloned();
834 left_summaries = all_summaries.by_ref().take(midpoint).collect();
835 right_summaries = all_summaries.collect();
836 let mut all_trees =
837 child_trees.iter().chain(trees_to_append.iter()).cloned();
838 left_trees = all_trees.by_ref().take(midpoint).collect();
839 right_trees = all_trees.collect();
840 }
841 *summary = sum(left_summaries.iter(), cx);
842 *child_summaries = left_summaries;
843 *child_trees = left_trees;
844
845 Some(SumTree(Arc::new(Node::Internal {
846 height: *height,
847 summary: sum(right_summaries.iter(), cx),
848 child_summaries: right_summaries,
849 child_trees: right_trees,
850 })))
851 } else {
852 child_summaries.extend(summaries_to_append);
853 child_trees.extend(trees_to_append);
854 None
855 }
856 }
857 Node::Leaf {
858 summary,
859 items,
860 item_summaries,
861 } => {
862 let other_node = other.0;
863
864 let child_count = items.len() + other_node.items().len();
865 if child_count > 2 * TREE_BASE {
866 let left_items;
867 let right_items;
868 let left_summaries;
869 let right_summaries: ArrayVec<T::Summary, { 2 * TREE_BASE }>;
870
871 let midpoint = (child_count + child_count % 2) / 2;
872 {
873 let mut all_items = items.iter().chain(other_node.items().iter()).cloned();
874 left_items = all_items.by_ref().take(midpoint).collect();
875 right_items = all_items.collect();
876
877 let mut all_summaries = item_summaries
878 .iter()
879 .chain(other_node.child_summaries())
880 .cloned();
881 left_summaries = all_summaries.by_ref().take(midpoint).collect();
882 right_summaries = all_summaries.collect();
883 }
884 *items = left_items;
885 *item_summaries = left_summaries;
886 *summary = sum(item_summaries.iter(), cx);
887 Some(SumTree(Arc::new(Node::Leaf {
888 items: right_items,
889 summary: sum(right_summaries.iter(), cx),
890 item_summaries: right_summaries,
891 })))
892 } else {
893 <T::Summary as Summary>::add_summary(summary, other_node.summary(), cx);
894 items.extend(other_node.items().iter().cloned());
895 item_summaries.extend(other_node.child_summaries().iter().cloned());
896 None
897 }
898 }
899 }
900 }
901
902 // appends the `large` tree to a `small` tree, assumes small.height() <= large.height()
903 fn append_large(
904 small: Self,
905 large: &mut Self,
906 cx: <T::Summary as Summary>::Context<'_>,
907 ) -> Option<Self> {
908 if small.0.height() == large.0.height() {
909 if !small.0.is_underflowing() {
910 Some(small)
911 } else {
912 Self::merge_into_right(small, large, cx)
913 }
914 } else {
915 debug_assert!(small.0.height() < large.0.height());
916 let Node::Internal {
917 height,
918 summary,
919 child_summaries,
920 child_trees,
921 } = Arc::make_mut(&mut large.0)
922 else {
923 unreachable!();
924 };
925 let mut full_summary = small.summary().clone();
926 Summary::add_summary(&mut full_summary, summary, cx);
927 *summary = full_summary;
928
929 let first = child_trees.first_mut().unwrap();
930 let res = Self::append_large(small, first, cx);
931 *child_summaries.first_mut().unwrap() = first.summary().clone();
932 if let Some(tree) = res {
933 if child_trees.len() < 2 * TREE_BASE {
934 child_summaries.insert(0, tree.summary().clone());
935 child_trees.insert(0, tree);
936 None
937 } else {
938 let new_child_summaries = {
939 let mut res = ArrayVec::from_iter([tree.summary().clone()]);
940 res.extend(child_summaries.drain(..TREE_BASE));
941 res
942 };
943 let tree = SumTree(Arc::new(Node::Internal {
944 height: *height,
945 summary: sum(new_child_summaries.iter(), cx),
946 child_summaries: new_child_summaries,
947 child_trees: {
948 let mut res = ArrayVec::from_iter([tree]);
949 res.extend(child_trees.drain(..TREE_BASE));
950 res
951 },
952 }));
953
954 *summary = sum(child_summaries.iter(), cx);
955 Some(tree)
956 }
957 } else {
958 None
959 }
960 }
961 }
962
963 // Merge two nodes into `large`.
964 //
965 // `large` will contain the contents of `small` followed by its own data.
966 // If the combined data exceed the node capacity, returns a new node that
967 // holds the first half of the merged items and `large` is left with the
968 // second half
969 //
970 // The nodes must be on the same height
971 // It only makes sense to call this when `small` is underflowing
972 fn merge_into_right(
973 small: Self,
974 large: &mut Self,
975 cx: <<T as Item>::Summary as Summary>::Context<'_>,
976 ) -> Option<SumTree<T>> {
977 debug_assert_eq!(small.0.height(), large.0.height());
978 match (small.0.as_ref(), Arc::make_mut(&mut large.0)) {
979 (
980 Node::Internal {
981 summary: small_summary,
982 child_summaries: small_child_summaries,
983 child_trees: small_child_trees,
984 ..
985 },
986 Node::Internal {
987 summary,
988 child_summaries,
989 child_trees,
990 height,
991 },
992 ) => {
993 let total_child_count = child_trees.len() + small_child_trees.len();
994 if total_child_count <= 2 * TREE_BASE {
995 let mut all_trees = small_child_trees.clone();
996 all_trees.extend(child_trees.drain(..));
997 *child_trees = all_trees;
998
999 let mut all_summaries = small_child_summaries.clone();
1000 all_summaries.extend(child_summaries.drain(..));
1001 *child_summaries = all_summaries;
1002
1003 let mut full_summary = small_summary.clone();
1004 Summary::add_summary(&mut full_summary, summary, cx);
1005 *summary = full_summary;
1006 None
1007 } else {
1008 let midpoint = total_child_count.div_ceil(2);
1009 let mut all_trees = small_child_trees.iter().chain(child_trees.iter()).cloned();
1010 let left_trees = all_trees.by_ref().take(midpoint).collect();
1011 *child_trees = all_trees.collect();
1012
1013 let mut all_summaries = small_child_summaries
1014 .iter()
1015 .chain(child_summaries.iter())
1016 .cloned();
1017 let left_summaries: ArrayVec<_, { 2 * TREE_BASE }> =
1018 all_summaries.by_ref().take(midpoint).collect();
1019 *child_summaries = all_summaries.collect();
1020
1021 *summary = sum(child_summaries.iter(), cx);
1022 Some(SumTree(Arc::new(Node::Internal {
1023 height: *height,
1024 summary: sum(left_summaries.iter(), cx),
1025 child_summaries: left_summaries,
1026 child_trees: left_trees,
1027 })))
1028 }
1029 }
1030 (
1031 Node::Leaf {
1032 summary: small_summary,
1033 items: small_items,
1034 item_summaries: small_item_summaries,
1035 },
1036 Node::Leaf {
1037 summary,
1038 items,
1039 item_summaries,
1040 },
1041 ) => {
1042 let total_child_count = small_items.len() + items.len();
1043 if total_child_count <= 2 * TREE_BASE {
1044 let mut all_items = small_items.clone();
1045 all_items.extend(items.drain(..));
1046 *items = all_items;
1047
1048 let mut all_summaries = small_item_summaries.clone();
1049 all_summaries.extend(item_summaries.drain(..));
1050 *item_summaries = all_summaries;
1051
1052 let mut full_summary = small_summary.clone();
1053 Summary::add_summary(&mut full_summary, summary, cx);
1054 *summary = full_summary;
1055 None
1056 } else {
1057 let midpoint = total_child_count.div_ceil(2);
1058 let mut all_items = small_items.iter().chain(items.iter()).cloned();
1059 let left_items = all_items.by_ref().take(midpoint).collect();
1060 *items = all_items.collect();
1061
1062 let mut all_summaries = small_item_summaries
1063 .iter()
1064 .chain(item_summaries.iter())
1065 .cloned();
1066 let left_summaries: ArrayVec<_, { 2 * TREE_BASE }> =
1067 all_summaries.by_ref().take(midpoint).collect();
1068 *item_summaries = all_summaries.collect();
1069
1070 *summary = sum(item_summaries.iter(), cx);
1071 Some(SumTree(Arc::new(Node::Leaf {
1072 items: left_items,
1073 summary: sum(left_summaries.iter(), cx),
1074 item_summaries: left_summaries,
1075 })))
1076 }
1077 }
1078 _ => unreachable!(),
1079 }
1080 }
1081
1082 fn from_child_trees(
1083 left: SumTree<T>,
1084 right: SumTree<T>,
1085 cx: <T::Summary as Summary>::Context<'_>,
1086 ) -> Self {
1087 let height = left.0.height() + 1;
1088 let mut child_summaries = ArrayVec::new();
1089 child_summaries.push(left.0.summary().clone());
1090 child_summaries.push(right.0.summary().clone());
1091 let mut child_trees = ArrayVec::new();
1092 child_trees.push(left);
1093 child_trees.push(right);
1094 SumTree(Arc::new(Node::Internal {
1095 height,
1096 summary: sum(child_summaries.iter(), cx),
1097 child_summaries,
1098 child_trees,
1099 }))
1100 }
1101
1102 fn leftmost_leaf(&self) -> &Self {
1103 match *self.0 {
1104 Node::Leaf { .. } => self,
1105 Node::Internal {
1106 ref child_trees, ..
1107 } => child_trees.first().unwrap().leftmost_leaf(),
1108 }
1109 }
1110
1111 fn rightmost_leaf(&self) -> &Self {
1112 match *self.0 {
1113 Node::Leaf { .. } => self,
1114 Node::Internal {
1115 ref child_trees, ..
1116 } => child_trees.last().unwrap().rightmost_leaf(),
1117 }
1118 }
1119}
1120
1121impl<T: Item + PartialEq> PartialEq for SumTree<T> {
1122 fn eq(&self, other: &Self) -> bool {
1123 self.iter().eq(other.iter())
1124 }
1125}
1126
1127impl<T: Item + Eq> Eq for SumTree<T> {}
1128
1129impl<T: KeyedItem> SumTree<T> {
1130 pub fn insert_or_replace<'a, 'b>(
1131 &'a mut self,
1132 item: T,
1133 cx: <T::Summary as Summary>::Context<'b>,
1134 ) -> Option<T> {
1135 let mut replaced = None;
1136 {
1137 let mut cursor = self.cursor::<T::Key>(cx);
1138 let mut new_tree = cursor.slice(&item.key(), Bias::Left);
1139 if let Some(cursor_item) = cursor.item()
1140 && cursor_item.key() == item.key()
1141 {
1142 replaced = Some(cursor_item.clone());
1143 cursor.next();
1144 }
1145 new_tree.push(item, cx);
1146 new_tree.append(cursor.suffix(), cx);
1147 drop(cursor);
1148 *self = new_tree
1149 };
1150 replaced
1151 }
1152
1153 pub fn remove(&mut self, key: &T::Key, cx: <T::Summary as Summary>::Context<'_>) -> Option<T> {
1154 let mut removed = None;
1155 *self = {
1156 let mut cursor = self.cursor::<T::Key>(cx);
1157 let mut new_tree = cursor.slice(key, Bias::Left);
1158 if let Some(item) = cursor.item()
1159 && item.key() == *key
1160 {
1161 removed = Some(item.clone());
1162 cursor.next();
1163 }
1164 new_tree.append(cursor.suffix(), cx);
1165 new_tree
1166 };
1167 removed
1168 }
1169
1170 pub fn edit(
1171 &mut self,
1172 mut edits: Vec<Edit<T>>,
1173 cx: <T::Summary as Summary>::Context<'_>,
1174 ) -> Vec<T> {
1175 if edits.is_empty() {
1176 return Vec::new();
1177 }
1178
1179 let mut removed = Vec::new();
1180 edits.sort_unstable_by_key(|item| item.key());
1181
1182 *self = {
1183 let mut cursor = self.cursor::<T::Key>(cx);
1184 let mut new_tree = SumTree::new(cx);
1185 let mut buffered_items = Vec::new();
1186
1187 cursor.seek(&T::Key::zero(cx), Bias::Left);
1188 for edit in edits {
1189 let new_key = edit.key();
1190 let mut old_item = cursor.item();
1191
1192 if old_item
1193 .as_ref()
1194 .is_some_and(|old_item| old_item.key() < new_key)
1195 {
1196 new_tree.extend(buffered_items.drain(..), cx);
1197 let slice = cursor.slice(&new_key, Bias::Left);
1198 new_tree.append(slice, cx);
1199 old_item = cursor.item();
1200 }
1201
1202 if let Some(old_item) = old_item
1203 && old_item.key() == new_key
1204 {
1205 removed.push(old_item.clone());
1206 cursor.next();
1207 }
1208
1209 match edit {
1210 Edit::Insert(item) => {
1211 buffered_items.push(item);
1212 }
1213 Edit::Remove(_) => {}
1214 }
1215 }
1216
1217 new_tree.extend(buffered_items, cx);
1218 new_tree.append(cursor.suffix(), cx);
1219 new_tree
1220 };
1221
1222 removed
1223 }
1224
1225 pub fn get<'a>(
1226 &'a self,
1227 key: &T::Key,
1228 cx: <T::Summary as Summary>::Context<'a>,
1229 ) -> Option<&'a T> {
1230 if let (_, _, Some(item)) = self.find_exact::<T::Key, _>(cx, key, Bias::Left) {
1231 Some(item)
1232 } else {
1233 None
1234 }
1235 }
1236}
1237
1238impl<T, S> Default for SumTree<T>
1239where
1240 T: Item<Summary = S>,
1241 S: for<'a> Summary<Context<'a> = ()>,
1242{
1243 fn default() -> Self {
1244 Self::new(())
1245 }
1246}
1247
1248#[derive(Clone)]
1249pub enum Node<T: Item> {
1250 Internal {
1251 height: u8,
1252 summary: T::Summary,
1253 child_summaries: ArrayVec<T::Summary, { 2 * TREE_BASE }>,
1254 child_trees: ArrayVec<SumTree<T>, { 2 * TREE_BASE }>,
1255 },
1256 Leaf {
1257 summary: T::Summary,
1258 items: ArrayVec<T, { 2 * TREE_BASE }>,
1259 item_summaries: ArrayVec<T::Summary, { 2 * TREE_BASE }>,
1260 },
1261}
1262
1263impl<T> fmt::Debug for Node<T>
1264where
1265 T: Item + fmt::Debug,
1266 T::Summary: fmt::Debug,
1267{
1268 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1269 match self {
1270 Node::Internal {
1271 height,
1272 summary,
1273 child_summaries,
1274 child_trees,
1275 } => f
1276 .debug_struct("Internal")
1277 .field("height", height)
1278 .field("summary", summary)
1279 .field("child_summaries", child_summaries)
1280 .field("child_trees", child_trees)
1281 .finish(),
1282 Node::Leaf {
1283 summary,
1284 items,
1285 item_summaries,
1286 } => f
1287 .debug_struct("Leaf")
1288 .field("summary", summary)
1289 .field("items", items)
1290 .field("item_summaries", item_summaries)
1291 .finish(),
1292 }
1293 }
1294}
1295
1296impl<T: Item> Node<T> {
1297 fn is_leaf(&self) -> bool {
1298 matches!(self, Node::Leaf { .. })
1299 }
1300
1301 fn height(&self) -> u8 {
1302 match self {
1303 Node::Internal { height, .. } => *height,
1304 Node::Leaf { .. } => 0,
1305 }
1306 }
1307
1308 fn summary(&self) -> &T::Summary {
1309 match self {
1310 Node::Internal { summary, .. } => summary,
1311 Node::Leaf { summary, .. } => summary,
1312 }
1313 }
1314
1315 fn child_summaries(&self) -> &[T::Summary] {
1316 match self {
1317 Node::Internal {
1318 child_summaries, ..
1319 } => child_summaries.as_slice(),
1320 Node::Leaf { item_summaries, .. } => item_summaries.as_slice(),
1321 }
1322 }
1323
1324 fn child_trees(&self) -> &ArrayVec<SumTree<T>, { 2 * TREE_BASE }> {
1325 match self {
1326 Node::Internal { child_trees, .. } => child_trees,
1327 Node::Leaf { .. } => panic!("Leaf nodes have no child trees"),
1328 }
1329 }
1330
1331 fn items(&self) -> &ArrayVec<T, { 2 * TREE_BASE }> {
1332 match self {
1333 Node::Leaf { items, .. } => items,
1334 Node::Internal { .. } => panic!("Internal nodes have no items"),
1335 }
1336 }
1337
1338 fn is_underflowing(&self) -> bool {
1339 match self {
1340 Node::Internal { child_trees, .. } => child_trees.len() < TREE_BASE,
1341 Node::Leaf { items, .. } => items.len() < TREE_BASE,
1342 }
1343 }
1344}
1345
1346#[derive(Debug)]
1347pub enum Edit<T: KeyedItem> {
1348 Insert(T),
1349 Remove(T::Key),
1350}
1351
1352impl<T: KeyedItem> Edit<T> {
1353 fn key(&self) -> T::Key {
1354 match self {
1355 Edit::Insert(item) => item.key(),
1356 Edit::Remove(key) => key.clone(),
1357 }
1358 }
1359}
1360
1361fn sum<'a, T, I>(iter: I, cx: T::Context<'_>) -> T
1362where
1363 T: 'a + Summary,
1364 I: Iterator<Item = &'a T>,
1365{
1366 let mut sum = T::zero(cx);
1367 for value in iter {
1368 sum.add_summary(value, cx);
1369 }
1370 sum
1371}
1372
1373#[cfg(test)]
1374mod tests {
1375 use super::*;
1376 use rand::{distr::StandardUniform, prelude::*};
1377 use std::cmp;
1378
1379 #[ctor::ctor]
1380 fn init_logger() {
1381 zlog::init_test();
1382 }
1383
1384 #[test]
1385 fn test_extend_and_push_tree() {
1386 let mut tree1 = SumTree::default();
1387 tree1.extend(0..20, ());
1388
1389 let mut tree2 = SumTree::default();
1390 tree2.extend(50..100, ());
1391
1392 tree1.append(tree2, ());
1393 assert_eq!(tree1.items(()), (0..20).chain(50..100).collect::<Vec<u8>>());
1394 }
1395
1396 #[test]
1397 fn test_random() {
1398 let mut starting_seed = 0;
1399 if let Ok(value) = std::env::var("SEED") {
1400 starting_seed = value.parse().expect("invalid SEED variable");
1401 }
1402 let mut num_iterations = 100;
1403 if let Ok(value) = std::env::var("ITERATIONS") {
1404 num_iterations = value.parse().expect("invalid ITERATIONS variable");
1405 }
1406 let num_operations = std::env::var("OPERATIONS")
1407 .map_or(5, |o| o.parse().expect("invalid OPERATIONS variable"));
1408
1409 for seed in starting_seed..(starting_seed + num_iterations) {
1410 eprintln!("seed = {}", seed);
1411 let mut rng = StdRng::seed_from_u64(seed);
1412
1413 let rng = &mut rng;
1414 let mut tree = SumTree::<u8>::default();
1415 let count = rng.random_range(0..10);
1416 if rng.random() {
1417 tree.extend(rng.sample_iter(StandardUniform).take(count), ());
1418 } else {
1419 let items = rng
1420 .sample_iter(StandardUniform)
1421 .take(count)
1422 .collect::<Vec<_>>();
1423 tree.par_extend(items, ());
1424 }
1425
1426 for _ in 0..num_operations {
1427 let splice_end = rng.random_range(0..tree.extent::<Count>(()).0 + 1);
1428 let splice_start = rng.random_range(0..splice_end + 1);
1429 let count = rng.random_range(0..10);
1430 let tree_end = tree.extent::<Count>(());
1431 let new_items = rng
1432 .sample_iter(StandardUniform)
1433 .take(count)
1434 .collect::<Vec<u8>>();
1435
1436 let mut reference_items = tree.items(());
1437 reference_items.splice(splice_start..splice_end, new_items.clone());
1438
1439 tree = {
1440 let mut cursor = tree.cursor::<Count>(());
1441 let mut new_tree = cursor.slice(&Count(splice_start), Bias::Right);
1442 if rng.random() {
1443 new_tree.extend(new_items, ());
1444 } else {
1445 new_tree.par_extend(new_items, ());
1446 }
1447 cursor.seek(&Count(splice_end), Bias::Right);
1448 new_tree.append(cursor.slice(&tree_end, Bias::Right), ());
1449 new_tree
1450 };
1451
1452 assert_eq!(tree.items(()), reference_items);
1453 assert_eq!(
1454 tree.iter().collect::<Vec<_>>(),
1455 tree.cursor::<()>(()).collect::<Vec<_>>()
1456 );
1457
1458 log::info!("tree items: {:?}", tree.items(()));
1459
1460 let mut filter_cursor =
1461 tree.filter::<_, Count>((), |summary| summary.contains_even);
1462 let expected_filtered_items = tree
1463 .items(())
1464 .into_iter()
1465 .enumerate()
1466 .filter(|(_, item)| (item & 1) == 0)
1467 .collect::<Vec<_>>();
1468
1469 let mut item_ix = if rng.random() {
1470 filter_cursor.next();
1471 0
1472 } else {
1473 filter_cursor.prev();
1474 expected_filtered_items.len().saturating_sub(1)
1475 };
1476 while item_ix < expected_filtered_items.len() {
1477 log::info!("filter_cursor, item_ix: {}", item_ix);
1478 let actual_item = filter_cursor.item().unwrap();
1479 let (reference_index, reference_item) = expected_filtered_items[item_ix];
1480 assert_eq!(actual_item, &reference_item);
1481 assert_eq!(filter_cursor.start().0, reference_index);
1482 log::info!("next");
1483 filter_cursor.next();
1484 item_ix += 1;
1485
1486 while item_ix > 0 && rng.random_bool(0.2) {
1487 log::info!("prev");
1488 filter_cursor.prev();
1489 item_ix -= 1;
1490
1491 if item_ix == 0 && rng.random_bool(0.2) {
1492 filter_cursor.prev();
1493 assert_eq!(filter_cursor.item(), None);
1494 assert_eq!(filter_cursor.start().0, 0);
1495 filter_cursor.next();
1496 }
1497 }
1498 }
1499 assert_eq!(filter_cursor.item(), None);
1500
1501 let mut before_start = false;
1502 let mut cursor = tree.cursor::<Count>(());
1503 let start_pos = rng.random_range(0..=reference_items.len());
1504 cursor.seek(&Count(start_pos), Bias::Right);
1505 let mut pos = rng.random_range(start_pos..=reference_items.len());
1506 cursor.seek_forward(&Count(pos), Bias::Right);
1507
1508 for i in 0..10 {
1509 assert_eq!(cursor.start().0, pos);
1510
1511 if pos > 0 {
1512 assert_eq!(cursor.prev_item().unwrap(), &reference_items[pos - 1]);
1513 } else {
1514 assert_eq!(cursor.prev_item(), None);
1515 }
1516
1517 if pos < reference_items.len() && !before_start {
1518 assert_eq!(cursor.item().unwrap(), &reference_items[pos]);
1519 } else {
1520 assert_eq!(cursor.item(), None);
1521 }
1522
1523 if before_start {
1524 assert_eq!(cursor.next_item(), reference_items.first());
1525 } else if pos + 1 < reference_items.len() {
1526 assert_eq!(cursor.next_item().unwrap(), &reference_items[pos + 1]);
1527 } else {
1528 assert_eq!(cursor.next_item(), None);
1529 }
1530
1531 if i < 5 {
1532 cursor.next();
1533 if pos < reference_items.len() {
1534 pos += 1;
1535 before_start = false;
1536 }
1537 } else {
1538 cursor.prev();
1539 if pos == 0 {
1540 before_start = true;
1541 }
1542 pos = pos.saturating_sub(1);
1543 }
1544 }
1545 }
1546
1547 for _ in 0..10 {
1548 let end = rng.random_range(0..tree.extent::<Count>(()).0 + 1);
1549 let start = rng.random_range(0..end + 1);
1550 let start_bias = if rng.random() {
1551 Bias::Left
1552 } else {
1553 Bias::Right
1554 };
1555 let end_bias = if rng.random() {
1556 Bias::Left
1557 } else {
1558 Bias::Right
1559 };
1560
1561 let mut cursor = tree.cursor::<Count>(());
1562 cursor.seek(&Count(start), start_bias);
1563 let slice = cursor.slice(&Count(end), end_bias);
1564
1565 cursor.seek(&Count(start), start_bias);
1566 let summary = cursor.summary::<_, Sum>(&Count(end), end_bias);
1567
1568 assert_eq!(summary.0, slice.summary().sum);
1569 }
1570 }
1571 }
1572
1573 #[test]
1574 fn test_cursor() {
1575 // Empty tree
1576 let tree = SumTree::<u8>::default();
1577 let mut cursor = tree.cursor::<IntegersSummary>(());
1578 assert_eq!(
1579 cursor.slice(&Count(0), Bias::Right).items(()),
1580 Vec::<u8>::new()
1581 );
1582 assert_eq!(cursor.item(), None);
1583 assert_eq!(cursor.prev_item(), None);
1584 assert_eq!(cursor.next_item(), None);
1585 assert_eq!(cursor.start().sum, 0);
1586 cursor.prev();
1587 assert_eq!(cursor.item(), None);
1588 assert_eq!(cursor.prev_item(), None);
1589 assert_eq!(cursor.next_item(), None);
1590 assert_eq!(cursor.start().sum, 0);
1591 cursor.next();
1592 assert_eq!(cursor.item(), None);
1593 assert_eq!(cursor.prev_item(), None);
1594 assert_eq!(cursor.next_item(), None);
1595 assert_eq!(cursor.start().sum, 0);
1596
1597 // Single-element tree
1598 let mut tree = SumTree::<u8>::default();
1599 tree.extend(vec![1], ());
1600 let mut cursor = tree.cursor::<IntegersSummary>(());
1601 assert_eq!(
1602 cursor.slice(&Count(0), Bias::Right).items(()),
1603 Vec::<u8>::new()
1604 );
1605 assert_eq!(cursor.item(), Some(&1));
1606 assert_eq!(cursor.prev_item(), None);
1607 assert_eq!(cursor.next_item(), None);
1608 assert_eq!(cursor.start().sum, 0);
1609
1610 cursor.next();
1611 assert_eq!(cursor.item(), None);
1612 assert_eq!(cursor.prev_item(), Some(&1));
1613 assert_eq!(cursor.next_item(), None);
1614 assert_eq!(cursor.start().sum, 1);
1615
1616 cursor.prev();
1617 assert_eq!(cursor.item(), Some(&1));
1618 assert_eq!(cursor.prev_item(), None);
1619 assert_eq!(cursor.next_item(), None);
1620 assert_eq!(cursor.start().sum, 0);
1621
1622 let mut cursor = tree.cursor::<IntegersSummary>(());
1623 assert_eq!(cursor.slice(&Count(1), Bias::Right).items(()), [1]);
1624 assert_eq!(cursor.item(), None);
1625 assert_eq!(cursor.prev_item(), Some(&1));
1626 assert_eq!(cursor.next_item(), None);
1627 assert_eq!(cursor.start().sum, 1);
1628
1629 cursor.seek(&Count(0), Bias::Right);
1630 assert_eq!(
1631 cursor
1632 .slice(&tree.extent::<Count>(()), Bias::Right)
1633 .items(()),
1634 [1]
1635 );
1636 assert_eq!(cursor.item(), None);
1637 assert_eq!(cursor.prev_item(), Some(&1));
1638 assert_eq!(cursor.next_item(), None);
1639 assert_eq!(cursor.start().sum, 1);
1640
1641 // Multiple-element tree
1642 let mut tree = SumTree::default();
1643 tree.extend(vec![1, 2, 3, 4, 5, 6], ());
1644 let mut cursor = tree.cursor::<IntegersSummary>(());
1645
1646 assert_eq!(cursor.slice(&Count(2), Bias::Right).items(()), [1, 2]);
1647 assert_eq!(cursor.item(), Some(&3));
1648 assert_eq!(cursor.prev_item(), Some(&2));
1649 assert_eq!(cursor.next_item(), Some(&4));
1650 assert_eq!(cursor.start().sum, 3);
1651
1652 cursor.next();
1653 assert_eq!(cursor.item(), Some(&4));
1654 assert_eq!(cursor.prev_item(), Some(&3));
1655 assert_eq!(cursor.next_item(), Some(&5));
1656 assert_eq!(cursor.start().sum, 6);
1657
1658 cursor.next();
1659 assert_eq!(cursor.item(), Some(&5));
1660 assert_eq!(cursor.prev_item(), Some(&4));
1661 assert_eq!(cursor.next_item(), Some(&6));
1662 assert_eq!(cursor.start().sum, 10);
1663
1664 cursor.next();
1665 assert_eq!(cursor.item(), Some(&6));
1666 assert_eq!(cursor.prev_item(), Some(&5));
1667 assert_eq!(cursor.next_item(), None);
1668 assert_eq!(cursor.start().sum, 15);
1669
1670 cursor.next();
1671 cursor.next();
1672 assert_eq!(cursor.item(), None);
1673 assert_eq!(cursor.prev_item(), Some(&6));
1674 assert_eq!(cursor.next_item(), None);
1675 assert_eq!(cursor.start().sum, 21);
1676
1677 cursor.prev();
1678 assert_eq!(cursor.item(), Some(&6));
1679 assert_eq!(cursor.prev_item(), Some(&5));
1680 assert_eq!(cursor.next_item(), None);
1681 assert_eq!(cursor.start().sum, 15);
1682
1683 cursor.prev();
1684 assert_eq!(cursor.item(), Some(&5));
1685 assert_eq!(cursor.prev_item(), Some(&4));
1686 assert_eq!(cursor.next_item(), Some(&6));
1687 assert_eq!(cursor.start().sum, 10);
1688
1689 cursor.prev();
1690 assert_eq!(cursor.item(), Some(&4));
1691 assert_eq!(cursor.prev_item(), Some(&3));
1692 assert_eq!(cursor.next_item(), Some(&5));
1693 assert_eq!(cursor.start().sum, 6);
1694
1695 cursor.prev();
1696 assert_eq!(cursor.item(), Some(&3));
1697 assert_eq!(cursor.prev_item(), Some(&2));
1698 assert_eq!(cursor.next_item(), Some(&4));
1699 assert_eq!(cursor.start().sum, 3);
1700
1701 cursor.prev();
1702 assert_eq!(cursor.item(), Some(&2));
1703 assert_eq!(cursor.prev_item(), Some(&1));
1704 assert_eq!(cursor.next_item(), Some(&3));
1705 assert_eq!(cursor.start().sum, 1);
1706
1707 cursor.prev();
1708 assert_eq!(cursor.item(), Some(&1));
1709 assert_eq!(cursor.prev_item(), None);
1710 assert_eq!(cursor.next_item(), Some(&2));
1711 assert_eq!(cursor.start().sum, 0);
1712
1713 cursor.prev();
1714 assert_eq!(cursor.item(), None);
1715 assert_eq!(cursor.prev_item(), None);
1716 assert_eq!(cursor.next_item(), Some(&1));
1717 assert_eq!(cursor.start().sum, 0);
1718
1719 cursor.next();
1720 assert_eq!(cursor.item(), Some(&1));
1721 assert_eq!(cursor.prev_item(), None);
1722 assert_eq!(cursor.next_item(), Some(&2));
1723 assert_eq!(cursor.start().sum, 0);
1724
1725 let mut cursor = tree.cursor::<IntegersSummary>(());
1726 assert_eq!(
1727 cursor
1728 .slice(&tree.extent::<Count>(()), Bias::Right)
1729 .items(()),
1730 tree.items(())
1731 );
1732 assert_eq!(cursor.item(), None);
1733 assert_eq!(cursor.prev_item(), Some(&6));
1734 assert_eq!(cursor.next_item(), None);
1735 assert_eq!(cursor.start().sum, 21);
1736
1737 cursor.seek(&Count(3), Bias::Right);
1738 assert_eq!(
1739 cursor
1740 .slice(&tree.extent::<Count>(()), Bias::Right)
1741 .items(()),
1742 [4, 5, 6]
1743 );
1744 assert_eq!(cursor.item(), None);
1745 assert_eq!(cursor.prev_item(), Some(&6));
1746 assert_eq!(cursor.next_item(), None);
1747 assert_eq!(cursor.start().sum, 21);
1748
1749 // Seeking can bias left or right
1750 cursor.seek(&Count(1), Bias::Left);
1751 assert_eq!(cursor.item(), Some(&1));
1752 cursor.seek(&Count(1), Bias::Right);
1753 assert_eq!(cursor.item(), Some(&2));
1754
1755 // Slicing without resetting starts from where the cursor is parked at.
1756 cursor.seek(&Count(1), Bias::Right);
1757 assert_eq!(cursor.slice(&Count(3), Bias::Right).items(()), vec![2, 3]);
1758 assert_eq!(cursor.slice(&Count(6), Bias::Left).items(()), vec![4, 5]);
1759 assert_eq!(cursor.slice(&Count(6), Bias::Right).items(()), vec![6]);
1760 }
1761
1762 #[test]
1763 fn test_edit() {
1764 let mut tree = SumTree::<u8>::default();
1765
1766 let removed = tree.edit(vec![Edit::Insert(1), Edit::Insert(2), Edit::Insert(0)], ());
1767 assert_eq!(tree.items(()), vec![0, 1, 2]);
1768 assert_eq!(removed, Vec::<u8>::new());
1769 assert_eq!(tree.get(&0, ()), Some(&0));
1770 assert_eq!(tree.get(&1, ()), Some(&1));
1771 assert_eq!(tree.get(&2, ()), Some(&2));
1772 assert_eq!(tree.get(&4, ()), None);
1773
1774 let removed = tree.edit(vec![Edit::Insert(2), Edit::Insert(4), Edit::Remove(0)], ());
1775 assert_eq!(tree.items(()), vec![1, 2, 4]);
1776 assert_eq!(removed, vec![0, 2]);
1777 assert_eq!(tree.get(&0, ()), None);
1778 assert_eq!(tree.get(&1, ()), Some(&1));
1779 assert_eq!(tree.get(&2, ()), Some(&2));
1780 assert_eq!(tree.get(&4, ()), Some(&4));
1781 }
1782
1783 #[test]
1784 fn test_from_iter() {
1785 assert_eq!(
1786 SumTree::from_iter(0..100, ()).items(()),
1787 (0..100).collect::<Vec<_>>()
1788 );
1789
1790 // Ensure `from_iter` works correctly when the given iterator restarts
1791 // after calling `next` if `None` was already returned.
1792 let mut ix = 0;
1793 let iterator = std::iter::from_fn(|| {
1794 ix = (ix + 1) % 2;
1795 if ix == 1 { Some(1) } else { None }
1796 });
1797 assert_eq!(SumTree::from_iter(iterator, ()).items(()), vec![1]);
1798 }
1799
1800 #[derive(Clone, Default, Debug)]
1801 pub struct IntegersSummary {
1802 count: usize,
1803 sum: usize,
1804 contains_even: bool,
1805 max: u8,
1806 }
1807
1808 #[derive(Ord, PartialOrd, Default, Eq, PartialEq, Clone, Debug)]
1809 struct Count(usize);
1810
1811 #[derive(Ord, PartialOrd, Default, Eq, PartialEq, Clone, Debug)]
1812 struct Sum(usize);
1813
1814 impl Item for u8 {
1815 type Summary = IntegersSummary;
1816
1817 fn summary(&self, _cx: ()) -> Self::Summary {
1818 IntegersSummary {
1819 count: 1,
1820 sum: *self as usize,
1821 contains_even: (*self & 1) == 0,
1822 max: *self,
1823 }
1824 }
1825 }
1826
1827 impl KeyedItem for u8 {
1828 type Key = u8;
1829
1830 fn key(&self) -> Self::Key {
1831 *self
1832 }
1833 }
1834
1835 impl ContextLessSummary for IntegersSummary {
1836 fn zero() -> Self {
1837 Default::default()
1838 }
1839
1840 fn add_summary(&mut self, other: &Self) {
1841 self.count += other.count;
1842 self.sum += other.sum;
1843 self.contains_even |= other.contains_even;
1844 self.max = cmp::max(self.max, other.max);
1845 }
1846 }
1847
1848 impl Dimension<'_, IntegersSummary> for u8 {
1849 fn zero(_cx: ()) -> Self {
1850 Default::default()
1851 }
1852
1853 fn add_summary(&mut self, summary: &IntegersSummary, _: ()) {
1854 *self = summary.max;
1855 }
1856 }
1857
1858 impl Dimension<'_, IntegersSummary> for Count {
1859 fn zero(_cx: ()) -> Self {
1860 Default::default()
1861 }
1862
1863 fn add_summary(&mut self, summary: &IntegersSummary, _: ()) {
1864 self.0 += summary.count;
1865 }
1866 }
1867
1868 impl SeekTarget<'_, IntegersSummary, IntegersSummary> for Count {
1869 fn cmp(&self, cursor_location: &IntegersSummary, _: ()) -> Ordering {
1870 self.0.cmp(&cursor_location.count)
1871 }
1872 }
1873
1874 impl Dimension<'_, IntegersSummary> for Sum {
1875 fn zero(_cx: ()) -> Self {
1876 Default::default()
1877 }
1878
1879 fn add_summary(&mut self, summary: &IntegersSummary, _: ()) {
1880 self.0 += summary.sum;
1881 }
1882 }
1883}