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};
12pub use tree_map::{MapSeekTarget, TreeMap, TreeSet};
13use ztracing::instrument;
14
15#[cfg(test)]
16pub const TREE_BASE: usize = 2;
17#[cfg(not(test))]
18pub const TREE_BASE: usize = 6;
19
20// Helper for when we cannot use ArrayVec::<T>::push().unwrap() as T doesn't impl Debug
21trait 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
31/// An item that can be stored in a [`SumTree`]
32///
33/// Must be summarized by a type that implements [`Summary`]
34pub trait Item: Clone {
35 type Summary: Summary;
36
37 fn summary(&self, cx: <Self::Summary as Summary>::Context<'_>) -> Self::Summary;
38}
39
40/// An [`Item`] whose summary has a specific key that can be used to identify it
41pub trait KeyedItem: Item {
42 type Key: for<'a> Dimension<'a, Self::Summary> + Ord;
43
44 fn key(&self) -> Self::Key;
45}
46
47/// A type that describes the Sum of all [`Item`]s in a subtree of the [`SumTree`]
48///
49/// Each Summary type can have multiple [`Dimension`]s that it measures,
50/// which can be used to navigate the tree
51pub 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
77/// Catch-all implementation for when you need something that implements [`Summary`] without a specific type.
78/// We implement it on a `NoSummary` instead of re-using `()`, as that avoids blanket impl collisions with `impl<T: Summary> Dimension for T`
79/// (as we also need unit type to be a fill-in dimension)
80impl ContextLessSummary for NoSummary {
81 fn zero() -> Self {
82 NoSummary
83 }
84
85 fn add_summary(&mut self, _: &Self) {}
86}
87
88/// Each [`Summary`] type can have more than one [`Dimension`] type that it measures.
89///
90/// You can use dimensions to seek to a specific location in the [`SumTree`]
91///
92/// # Example:
93/// Zed's rope has a `TextSummary` type that summarizes lines, characters, and bytes.
94/// Each of these are different dimensions we may want to seek to
95pub 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/// Bias is used to settle ambiguities when determining positions in an ordered sequence.
168///
169/// The primary use case is for text, where Bias influences
170/// which character an offset or anchor is associated with.
171///
172/// # Examples
173/// Given the buffer `AˇBCD`:
174/// - The offset of the cursor is 1
175/// - [Bias::Left] would attach the cursor to the character `A`
176/// - [Bias::Right] would attach the cursor to the character `B`
177///
178/// Given the buffer `A«BCˇ»D`:
179/// - The offset of the cursor is 3, and the selection is from 1 to 3
180/// - The left anchor of the selection has [Bias::Right], attaching it to the character `B`
181/// - The right anchor of the selection has [Bias::Left], attaching it to the character `C`
182///
183/// Given the buffer `{ˇ<...>`, where `<...>` is a folded region:
184/// - The display offset of the cursor is 1, but the offset in the buffer is determined by the bias
185/// - [Bias::Left] would attach the cursor to the character `{`, with a buffer offset of 1
186/// - [Bias::Right] would attach the cursor to the first character of the folded region,
187/// and the buffer offset would be the offset of the first character of the folded region
188#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Debug, Hash, Default)]
189pub enum Bias {
190 /// Attach to the character on the left
191 #[default]
192 Left,
193 /// Attach to the character on the right
194 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/// A B+ tree in which each leaf node contains `Item`s of type `T` and a `Summary`s for each `Item`.
207/// Each internal node contains a `Summary` of the items in its subtree.
208///
209/// The maximum number of items per node is `TREE_BASE * 2`.
210///
211/// Any [`Dimension`] supported by the [`Summary`] type can be used to seek to a specific location in the tree.
212#[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 /// 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.
235 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 /// A more efficient version of `Cursor::new()` + `Cursor::seek()` + `Cursor::item()`.
397 ///
398 /// Only returns the item that exactly has the target match.
399 #[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 /// A more efficient version of `Cursor::new()` + `Cursor::seek()` + `Cursor::item()`
425 #[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 /// A more efficient version of `Cursor::new()` + `Cursor::seek()` + `Cursor::item()`
510 #[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 /// Note: If the summary type requires a non `()` context, then the filter cursor
608 /// that is returned cannot be used with Rust's iterators.
609 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 // appends the `large` tree to a `small` tree, assumes small.height() <= large.height()
921 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 // Merge two nodes into `large`.
984 //
985 // `large` will contain the contents of `small` followed by its own data.
986 // If the combined data exceed the node capacity, returns a new node that
987 // holds the first half of the merged items and `large` is left with the
988 // second half
989 //
990 // The nodes must be on the same height
991 // It only makes sense to call this when `small` is underflowing
992 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]
1400 fn init_logger() {
1401 zlog::init_test();
1402 }
1403
1404 #[test]
1405 fn test_extend_and_push_tree() {
1406 let mut tree1 = SumTree::default();
1407 tree1.extend(0..20, ());
1408
1409 let mut tree2 = SumTree::default();
1410 tree2.extend(50..100, ());
1411
1412 tree1.append(tree2, ());
1413 assert_eq!(tree1.items(()), (0..20).chain(50..100).collect::<Vec<u8>>());
1414 }
1415
1416 #[test]
1417 fn test_random() {
1418 let mut starting_seed = 0;
1419 if let Ok(value) = std::env::var("SEED") {
1420 starting_seed = value.parse().expect("invalid SEED variable");
1421 }
1422 let mut num_iterations = 100;
1423 if let Ok(value) = std::env::var("ITERATIONS") {
1424 num_iterations = value.parse().expect("invalid ITERATIONS variable");
1425 }
1426 let num_operations = std::env::var("OPERATIONS")
1427 .map_or(5, |o| o.parse().expect("invalid OPERATIONS variable"));
1428
1429 for seed in starting_seed..(starting_seed + num_iterations) {
1430 eprintln!("seed = {}", seed);
1431 let mut rng = StdRng::seed_from_u64(seed);
1432
1433 let rng = &mut rng;
1434 let mut tree = SumTree::<u8>::default();
1435 let count = rng.random_range(0..10);
1436 if rng.random() {
1437 tree.extend(rng.sample_iter(StandardUniform).take(count), ());
1438 } else {
1439 let items = rng
1440 .sample_iter(StandardUniform)
1441 .take(count)
1442 .collect::<Vec<_>>();
1443 tree.par_extend(items, ());
1444 }
1445
1446 for _ in 0..num_operations {
1447 let splice_end = rng.random_range(0..tree.extent::<Count>(()).0 + 1);
1448 let splice_start = rng.random_range(0..splice_end + 1);
1449 let count = rng.random_range(0..10);
1450 let tree_end = tree.extent::<Count>(());
1451 let new_items = rng
1452 .sample_iter(StandardUniform)
1453 .take(count)
1454 .collect::<Vec<u8>>();
1455
1456 let mut reference_items = tree.items(());
1457 reference_items.splice(splice_start..splice_end, new_items.clone());
1458
1459 tree = {
1460 let mut cursor = tree.cursor::<Count>(());
1461 let mut new_tree = cursor.slice(&Count(splice_start), Bias::Right);
1462 if rng.random() {
1463 new_tree.extend(new_items, ());
1464 } else {
1465 new_tree.par_extend(new_items, ());
1466 }
1467 cursor.seek(&Count(splice_end), Bias::Right);
1468 new_tree.append(cursor.slice(&tree_end, Bias::Right), ());
1469 new_tree
1470 };
1471
1472 assert_eq!(tree.items(()), reference_items);
1473 assert_eq!(
1474 tree.iter().collect::<Vec<_>>(),
1475 tree.cursor::<()>(()).collect::<Vec<_>>()
1476 );
1477
1478 log::info!("tree items: {:?}", tree.items(()));
1479
1480 let mut filter_cursor =
1481 tree.filter::<_, Count>((), |summary| summary.contains_even);
1482 let expected_filtered_items = tree
1483 .items(())
1484 .into_iter()
1485 .enumerate()
1486 .filter(|(_, item)| (item & 1) == 0)
1487 .collect::<Vec<_>>();
1488
1489 let mut item_ix = if rng.random() {
1490 filter_cursor.next();
1491 0
1492 } else {
1493 filter_cursor.prev();
1494 expected_filtered_items.len().saturating_sub(1)
1495 };
1496 while item_ix < expected_filtered_items.len() {
1497 log::info!("filter_cursor, item_ix: {}", item_ix);
1498 let actual_item = filter_cursor.item().unwrap();
1499 let (reference_index, reference_item) = expected_filtered_items[item_ix];
1500 assert_eq!(actual_item, &reference_item);
1501 assert_eq!(filter_cursor.start().0, reference_index);
1502 log::info!("next");
1503 filter_cursor.next();
1504 item_ix += 1;
1505
1506 while item_ix > 0 && rng.random_bool(0.2) {
1507 log::info!("prev");
1508 filter_cursor.prev();
1509 item_ix -= 1;
1510
1511 if item_ix == 0 && rng.random_bool(0.2) {
1512 filter_cursor.prev();
1513 assert_eq!(filter_cursor.item(), None);
1514 assert_eq!(filter_cursor.start().0, 0);
1515 filter_cursor.next();
1516 }
1517 }
1518 }
1519 assert_eq!(filter_cursor.item(), None);
1520
1521 let mut before_start = false;
1522 let mut cursor = tree.cursor::<Count>(());
1523 let start_pos = rng.random_range(0..=reference_items.len());
1524 cursor.seek(&Count(start_pos), Bias::Right);
1525 let mut pos = rng.random_range(start_pos..=reference_items.len());
1526 cursor.seek_forward(&Count(pos), Bias::Right);
1527
1528 for i in 0..10 {
1529 assert_eq!(cursor.start().0, pos);
1530
1531 if pos > 0 {
1532 assert_eq!(cursor.prev_item().unwrap(), &reference_items[pos - 1]);
1533 } else {
1534 assert_eq!(cursor.prev_item(), None);
1535 }
1536
1537 if pos < reference_items.len() && !before_start {
1538 assert_eq!(cursor.item().unwrap(), &reference_items[pos]);
1539 } else {
1540 assert_eq!(cursor.item(), None);
1541 }
1542
1543 if before_start {
1544 assert_eq!(cursor.next_item(), reference_items.first());
1545 } else if pos + 1 < reference_items.len() {
1546 assert_eq!(cursor.next_item().unwrap(), &reference_items[pos + 1]);
1547 } else {
1548 assert_eq!(cursor.next_item(), None);
1549 }
1550
1551 if i < 5 {
1552 cursor.next();
1553 if pos < reference_items.len() {
1554 pos += 1;
1555 before_start = false;
1556 }
1557 } else {
1558 cursor.prev();
1559 if pos == 0 {
1560 before_start = true;
1561 }
1562 pos = pos.saturating_sub(1);
1563 }
1564 }
1565 }
1566
1567 for _ in 0..10 {
1568 let end = rng.random_range(0..tree.extent::<Count>(()).0 + 1);
1569 let start = rng.random_range(0..end + 1);
1570 let start_bias = if rng.random() {
1571 Bias::Left
1572 } else {
1573 Bias::Right
1574 };
1575 let end_bias = if rng.random() {
1576 Bias::Left
1577 } else {
1578 Bias::Right
1579 };
1580
1581 let mut cursor = tree.cursor::<Count>(());
1582 cursor.seek(&Count(start), start_bias);
1583 let slice = cursor.slice(&Count(end), end_bias);
1584
1585 cursor.seek(&Count(start), start_bias);
1586 let summary = cursor.summary::<_, Sum>(&Count(end), end_bias);
1587
1588 assert_eq!(summary.0, slice.summary().sum);
1589 }
1590 }
1591 }
1592
1593 #[test]
1594 fn test_cursor() {
1595 // Empty tree
1596 let tree = SumTree::<u8>::default();
1597 let mut cursor = tree.cursor::<IntegersSummary>(());
1598 assert_eq!(
1599 cursor.slice(&Count(0), Bias::Right).items(()),
1600 Vec::<u8>::new()
1601 );
1602 assert_eq!(cursor.item(), None);
1603 assert_eq!(cursor.prev_item(), None);
1604 assert_eq!(cursor.next_item(), None);
1605 assert_eq!(cursor.start().sum, 0);
1606 cursor.prev();
1607 assert_eq!(cursor.item(), None);
1608 assert_eq!(cursor.prev_item(), None);
1609 assert_eq!(cursor.next_item(), None);
1610 assert_eq!(cursor.start().sum, 0);
1611 cursor.next();
1612 assert_eq!(cursor.item(), None);
1613 assert_eq!(cursor.prev_item(), None);
1614 assert_eq!(cursor.next_item(), None);
1615 assert_eq!(cursor.start().sum, 0);
1616
1617 // Single-element tree
1618 let mut tree = SumTree::<u8>::default();
1619 tree.extend(vec![1], ());
1620 let mut cursor = tree.cursor::<IntegersSummary>(());
1621 assert_eq!(
1622 cursor.slice(&Count(0), Bias::Right).items(()),
1623 Vec::<u8>::new()
1624 );
1625 assert_eq!(cursor.item(), Some(&1));
1626 assert_eq!(cursor.prev_item(), None);
1627 assert_eq!(cursor.next_item(), None);
1628 assert_eq!(cursor.start().sum, 0);
1629
1630 cursor.next();
1631 assert_eq!(cursor.item(), None);
1632 assert_eq!(cursor.prev_item(), Some(&1));
1633 assert_eq!(cursor.next_item(), None);
1634 assert_eq!(cursor.start().sum, 1);
1635
1636 cursor.prev();
1637 assert_eq!(cursor.item(), Some(&1));
1638 assert_eq!(cursor.prev_item(), None);
1639 assert_eq!(cursor.next_item(), None);
1640 assert_eq!(cursor.start().sum, 0);
1641
1642 let mut cursor = tree.cursor::<IntegersSummary>(());
1643 assert_eq!(cursor.slice(&Count(1), Bias::Right).items(()), [1]);
1644 assert_eq!(cursor.item(), None);
1645 assert_eq!(cursor.prev_item(), Some(&1));
1646 assert_eq!(cursor.next_item(), None);
1647 assert_eq!(cursor.start().sum, 1);
1648
1649 cursor.seek(&Count(0), Bias::Right);
1650 assert_eq!(
1651 cursor
1652 .slice(&tree.extent::<Count>(()), Bias::Right)
1653 .items(()),
1654 [1]
1655 );
1656 assert_eq!(cursor.item(), None);
1657 assert_eq!(cursor.prev_item(), Some(&1));
1658 assert_eq!(cursor.next_item(), None);
1659 assert_eq!(cursor.start().sum, 1);
1660
1661 // Multiple-element tree
1662 let mut tree = SumTree::default();
1663 tree.extend(vec![1, 2, 3, 4, 5, 6], ());
1664 let mut cursor = tree.cursor::<IntegersSummary>(());
1665
1666 assert_eq!(cursor.slice(&Count(2), Bias::Right).items(()), [1, 2]);
1667 assert_eq!(cursor.item(), Some(&3));
1668 assert_eq!(cursor.prev_item(), Some(&2));
1669 assert_eq!(cursor.next_item(), Some(&4));
1670 assert_eq!(cursor.start().sum, 3);
1671
1672 cursor.next();
1673 assert_eq!(cursor.item(), Some(&4));
1674 assert_eq!(cursor.prev_item(), Some(&3));
1675 assert_eq!(cursor.next_item(), Some(&5));
1676 assert_eq!(cursor.start().sum, 6);
1677
1678 cursor.next();
1679 assert_eq!(cursor.item(), Some(&5));
1680 assert_eq!(cursor.prev_item(), Some(&4));
1681 assert_eq!(cursor.next_item(), Some(&6));
1682 assert_eq!(cursor.start().sum, 10);
1683
1684 cursor.next();
1685 assert_eq!(cursor.item(), Some(&6));
1686 assert_eq!(cursor.prev_item(), Some(&5));
1687 assert_eq!(cursor.next_item(), None);
1688 assert_eq!(cursor.start().sum, 15);
1689
1690 cursor.next();
1691 cursor.next();
1692 assert_eq!(cursor.item(), None);
1693 assert_eq!(cursor.prev_item(), Some(&6));
1694 assert_eq!(cursor.next_item(), None);
1695 assert_eq!(cursor.start().sum, 21);
1696
1697 cursor.prev();
1698 assert_eq!(cursor.item(), Some(&6));
1699 assert_eq!(cursor.prev_item(), Some(&5));
1700 assert_eq!(cursor.next_item(), None);
1701 assert_eq!(cursor.start().sum, 15);
1702
1703 cursor.prev();
1704 assert_eq!(cursor.item(), Some(&5));
1705 assert_eq!(cursor.prev_item(), Some(&4));
1706 assert_eq!(cursor.next_item(), Some(&6));
1707 assert_eq!(cursor.start().sum, 10);
1708
1709 cursor.prev();
1710 assert_eq!(cursor.item(), Some(&4));
1711 assert_eq!(cursor.prev_item(), Some(&3));
1712 assert_eq!(cursor.next_item(), Some(&5));
1713 assert_eq!(cursor.start().sum, 6);
1714
1715 cursor.prev();
1716 assert_eq!(cursor.item(), Some(&3));
1717 assert_eq!(cursor.prev_item(), Some(&2));
1718 assert_eq!(cursor.next_item(), Some(&4));
1719 assert_eq!(cursor.start().sum, 3);
1720
1721 cursor.prev();
1722 assert_eq!(cursor.item(), Some(&2));
1723 assert_eq!(cursor.prev_item(), Some(&1));
1724 assert_eq!(cursor.next_item(), Some(&3));
1725 assert_eq!(cursor.start().sum, 1);
1726
1727 cursor.prev();
1728 assert_eq!(cursor.item(), Some(&1));
1729 assert_eq!(cursor.prev_item(), None);
1730 assert_eq!(cursor.next_item(), Some(&2));
1731 assert_eq!(cursor.start().sum, 0);
1732
1733 cursor.prev();
1734 assert_eq!(cursor.item(), None);
1735 assert_eq!(cursor.prev_item(), None);
1736 assert_eq!(cursor.next_item(), Some(&1));
1737 assert_eq!(cursor.start().sum, 0);
1738
1739 cursor.next();
1740 assert_eq!(cursor.item(), Some(&1));
1741 assert_eq!(cursor.prev_item(), None);
1742 assert_eq!(cursor.next_item(), Some(&2));
1743 assert_eq!(cursor.start().sum, 0);
1744
1745 let mut cursor = tree.cursor::<IntegersSummary>(());
1746 assert_eq!(
1747 cursor
1748 .slice(&tree.extent::<Count>(()), Bias::Right)
1749 .items(()),
1750 tree.items(())
1751 );
1752 assert_eq!(cursor.item(), None);
1753 assert_eq!(cursor.prev_item(), Some(&6));
1754 assert_eq!(cursor.next_item(), None);
1755 assert_eq!(cursor.start().sum, 21);
1756
1757 cursor.seek(&Count(3), Bias::Right);
1758 assert_eq!(
1759 cursor
1760 .slice(&tree.extent::<Count>(()), Bias::Right)
1761 .items(()),
1762 [4, 5, 6]
1763 );
1764 assert_eq!(cursor.item(), None);
1765 assert_eq!(cursor.prev_item(), Some(&6));
1766 assert_eq!(cursor.next_item(), None);
1767 assert_eq!(cursor.start().sum, 21);
1768
1769 // Seeking can bias left or right
1770 cursor.seek(&Count(1), Bias::Left);
1771 assert_eq!(cursor.item(), Some(&1));
1772 cursor.seek(&Count(1), Bias::Right);
1773 assert_eq!(cursor.item(), Some(&2));
1774
1775 // Slicing without resetting starts from where the cursor is parked at.
1776 cursor.seek(&Count(1), Bias::Right);
1777 assert_eq!(cursor.slice(&Count(3), Bias::Right).items(()), vec![2, 3]);
1778 assert_eq!(cursor.slice(&Count(6), Bias::Left).items(()), vec![4, 5]);
1779 assert_eq!(cursor.slice(&Count(6), Bias::Right).items(()), vec![6]);
1780 }
1781
1782 #[test]
1783 fn test_edit() {
1784 let mut tree = SumTree::<u8>::default();
1785
1786 let removed = tree.edit(vec![Edit::Insert(1), Edit::Insert(2), Edit::Insert(0)], ());
1787 assert_eq!(tree.items(()), vec![0, 1, 2]);
1788 assert_eq!(removed, Vec::<u8>::new());
1789 assert_eq!(tree.get(&0, ()), Some(&0));
1790 assert_eq!(tree.get(&1, ()), Some(&1));
1791 assert_eq!(tree.get(&2, ()), Some(&2));
1792 assert_eq!(tree.get(&4, ()), None);
1793
1794 let removed = tree.edit(vec![Edit::Insert(2), Edit::Insert(4), Edit::Remove(0)], ());
1795 assert_eq!(tree.items(()), vec![1, 2, 4]);
1796 assert_eq!(removed, vec![0, 2]);
1797 assert_eq!(tree.get(&0, ()), None);
1798 assert_eq!(tree.get(&1, ()), Some(&1));
1799 assert_eq!(tree.get(&2, ()), Some(&2));
1800 assert_eq!(tree.get(&4, ()), Some(&4));
1801 }
1802
1803 #[test]
1804 fn test_from_iter() {
1805 assert_eq!(
1806 SumTree::from_iter(0..100, ()).items(()),
1807 (0..100).collect::<Vec<_>>()
1808 );
1809
1810 // Ensure `from_iter` works correctly when the given iterator restarts
1811 // after calling `next` if `None` was already returned.
1812 let mut ix = 0;
1813 let iterator = std::iter::from_fn(|| {
1814 ix = (ix + 1) % 2;
1815 if ix == 1 { Some(1) } else { None }
1816 });
1817 assert_eq!(SumTree::from_iter(iterator, ()).items(()), vec![1]);
1818 }
1819
1820 #[derive(Clone, Default, Debug)]
1821 pub struct IntegersSummary {
1822 count: usize,
1823 sum: usize,
1824 contains_even: bool,
1825 max: u8,
1826 }
1827
1828 #[derive(Ord, PartialOrd, Default, Eq, PartialEq, Clone, Debug)]
1829 struct Count(usize);
1830
1831 #[derive(Ord, PartialOrd, Default, Eq, PartialEq, Clone, Debug)]
1832 struct Sum(usize);
1833
1834 impl Item for u8 {
1835 type Summary = IntegersSummary;
1836
1837 fn summary(&self, _cx: ()) -> Self::Summary {
1838 IntegersSummary {
1839 count: 1,
1840 sum: *self as usize,
1841 contains_even: (*self & 1) == 0,
1842 max: *self,
1843 }
1844 }
1845 }
1846
1847 impl KeyedItem for u8 {
1848 type Key = u8;
1849
1850 fn key(&self) -> Self::Key {
1851 *self
1852 }
1853 }
1854
1855 impl ContextLessSummary for IntegersSummary {
1856 fn zero() -> Self {
1857 Default::default()
1858 }
1859
1860 fn add_summary(&mut self, other: &Self) {
1861 self.count += other.count;
1862 self.sum += other.sum;
1863 self.contains_even |= other.contains_even;
1864 self.max = cmp::max(self.max, other.max);
1865 }
1866 }
1867
1868 impl Dimension<'_, IntegersSummary> for u8 {
1869 fn zero(_cx: ()) -> Self {
1870 Default::default()
1871 }
1872
1873 fn add_summary(&mut self, summary: &IntegersSummary, _: ()) {
1874 *self = summary.max;
1875 }
1876 }
1877
1878 impl Dimension<'_, IntegersSummary> for Count {
1879 fn zero(_cx: ()) -> Self {
1880 Default::default()
1881 }
1882
1883 fn add_summary(&mut self, summary: &IntegersSummary, _: ()) {
1884 self.0 += summary.count;
1885 }
1886 }
1887
1888 impl SeekTarget<'_, IntegersSummary, IntegersSummary> for Count {
1889 fn cmp(&self, cursor_location: &IntegersSummary, _: ()) -> Ordering {
1890 self.0.cmp(&cursor_location.count)
1891 }
1892 }
1893
1894 impl Dimension<'_, IntegersSummary> for Sum {
1895 fn zero(_cx: ()) -> Self {
1896 Default::default()
1897 }
1898
1899 fn add_summary(&mut self, summary: &IntegersSummary, _: ()) {
1900 self.0 += summary.sum;
1901 }
1902 }
1903}