sum_tree.rs

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