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