sum_tree.rs

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