sum_tree.rs

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