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