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