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