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