BTree

Struct BTree 

Source
pub struct BTree<B: BTreeTrait> { /* private fields */ }

Implementations§

Source§

impl<B: BTreeTrait> BTree<B>

Source

pub fn new() -> Self

Source

pub fn node_len(&self) -> usize

Get the number of nodes in the tree. It includes all the internal nodes and the leaf nodes.

Source

pub fn insert<Q>( &mut self, q: &Q::QueryArg, data: B::Elem, ) -> (Cursor, SplittedLeaves)
where Q: Query<B>,

Insert new element to the tree.

Returns (insert_pos, splitted_leaves)

Source

pub fn insert_by_path( &mut self, cursor: Cursor, data: B::Elem, ) -> (Cursor, SplittedLeaves)

Source

pub fn insert_many_by_cursor( &mut self, cursor: Option<Cursor>, data_iter: impl Iterator<Item = B::Elem>, )

Insert many elements into the tree at once

It will invoke [BTreeTrait::insert_batch]

Source

pub fn shift_path_by_one_offset(&self, path: Cursor) -> Option<Cursor>
where B::Elem: HasLength,

Shift by offset 1.

It will not stay on empty spans but scan forward

Source

pub fn query<Q>(&self, query: &Q::QueryArg) -> Option<QueryResult>
where Q: Query<B>,

Query the tree by custom query type

Return None if the tree is empty

Source

pub fn query_with_finder_return<Q>( &self, query: &Q::QueryArg, ) -> (Option<QueryResult>, Q)
where Q: Query<B>,

Source

pub fn get_elem_mut(&mut self, leaf: LeafIndex) -> Option<&mut B::Elem>

Source

pub fn get_elem(&self, leaf: LeafIndex) -> Option<&<B as BTreeTrait>::Elem>

Source

pub fn remove_leaf(&mut self, path: Cursor) -> Option<B::Elem>

Remove leaf node from the tree

If it’s already removed, this method will return None

Source

pub fn update<F>(&mut self, range: Range<Cursor>, f: &mut F) -> SplittedLeaves
where F: FnMut(&mut B::Elem) -> Option<B::CacheDiff>,

Update the elements in place.

If the range.start or range.end is in the middle of a leaf node, the leaf node will be splitted into two leaf nodes. The new leaf nodes will be returned.

F should returns Some(cache_diff) if cache needs to be updated. Otherwise, returns None.

If the given range has zero length, f will still be called, and the slice will have same start and end field

TODO: need better test coverage

Source

pub fn prefer_right(&self, path: Cursor) -> Option<Cursor>

Prefer begin of the next leaf node than end of the current leaf node

When path.offset == leaf.rle_len(), this method will return the next leaf node with offset 0

Source

pub fn prefer_left(&self, path: Cursor) -> Option<Cursor>

Prefer end of the previous leaf node than begin of the current leaf node

When path.offset == 0, this method will return the previous leaf node with offset leaf.rle_len()

Update leaf node’s elements.

f returns Option<(cache_diff, new_insert_1, new_insert2)>

  • If returned value is None, the cache will not be updated.
  • If leaf_node.can_remove(), it will be removed from the tree.

Returns (path, splitted_leaves), if is is still valid after this method. (If the leaf node is removed, the path will be None)

Source

pub fn update_leaf( &mut self, node_idx: LeafIndex, f: impl FnOnce(&mut B::Elem) -> (bool, Option<B::Elem>, Option<B::Elem>), ) -> (bool, SplittedLeaves)

Update leaf node’s elements, return true if cache need to be updated

f returns (is_cache_updated, cache_diff, new_insert_1, new_insert2)

  • If leaf_node.can_remove(), it will be removed from the tree.

Returns true if the node_idx is still valid. (If the leaf node is removed, it will return false).

Source

pub fn update_leaves_with_arg_in_ranges<A: Debug>( &mut self, args: Vec<(LeafIndex, Range<usize>, A)>, f: impl FnMut(&mut B::Elem, &A), ) -> Vec<LeafIndex>

Update the given leaves with the given function in the given range.

  • The range descibes the range inside the leaf node.
  • There can be multiple ranges in the same leaf node.
  • The cahce will be recalculated for each affected node
  • It doesn’t guarantee the applying order

Currently, the time complexity is O(m^2) for each leaf node, where m is the number of ranges inside the same leaf node. If we have a really large m, this function need to be optimized.

Source

pub fn iter(&self) -> impl Iterator<Item = &B::Elem> + '_

Source

pub fn drain(&mut self, range: Range<QueryResult>) -> Drain<'_, B>

Source

pub fn drain_by_query<Q: Query<B>>( &mut self, range: Range<Q::QueryArg>, ) -> Drain<'_, B>

Source

pub fn first_leaf(&self) -> Option<LeafIndex>

Source

pub fn last_leaf(&self) -> Option<LeafIndex>

Source

pub fn range<Q>(&self, range: Range<Q::QueryArg>) -> Option<Range<QueryResult>>
where Q: Query<B>,

Source

pub fn iter_range( &self, range: impl RangeBounds<Cursor>, ) -> impl Iterator<Item = ElemSlice<'_, B::Elem>> + '_

Source

pub fn start_cursor(&self) -> Option<Cursor>

Source

pub fn end_cursor(&self) -> Option<Cursor>

Source

pub fn get_internal_mut(&mut self, index: ArenaIndex) -> &mut Node<B>

Source

pub fn get_leaf_mut(&mut self, index: ArenaIndex) -> &mut LeafNode<B::Elem>

Source

pub fn get_internal(&self, index: ArenaIndex) -> &Node<B>

§Panic

If the given index is not valid or deleted

Source

pub fn get_leaf(&self, index: ArenaIndex) -> &LeafNode<B::Elem>

Source

pub fn recursive_update_cache( &mut self, node_idx: ArenaIndex, can_use_diff: bool, cache_diff: Option<B::CacheDiff>, )

Sometimes we cannot use diff because no only the given node is changed, but also its siblings. For example, after delete a range of nodes, we cannot use the diff from child to infer the diff of parent.

Source

pub fn next_elem(&self, path: Cursor) -> Option<Cursor>

find the next element in the tree

Source

pub fn prev_elem(&self, path: Cursor) -> Option<Cursor>

Source

pub fn root_cache(&self) -> &B::Cache

Source

pub fn clear(&mut self)

This method will release the memory back to OS. Currently, it’s just *self = Self::new()

Source

pub fn is_empty(&self) -> bool

Source

pub fn push(&mut self, elem: B::Elem) -> Cursor

Source

pub fn prepend(&mut self, elem: B::Elem) -> Cursor

Source

pub fn compare_pos(&self, a: Cursor, b: Cursor) -> Ordering

compare the position of a and b

Source

pub fn visit_previous_caches<F>(&self, cursor: Cursor, f: F)
where F: FnMut(PreviousCache<'_, B>),

Iterate the caches of previous nodes/elements. This method will visit as less caches as possible. For example, if all nodes in a subtree need to be visited, we will only visit the root cache.

f: (node_cache, previous_sibling_elem, (this_elem, offset))

Source

pub fn diagnose_balance(&self)

Source

pub fn iter_with_filter<'a, R: Default + Copy + AddAssign + 'a>( &'a self, f: impl FnMut(&B::Cache) -> (bool, R) + 'a, ) -> impl Iterator<Item = (R, &B::Elem)> + '_

Iterate over the leaf elements in the tree if the filter returns true for all its ancestors’ caches, including its own cache.

Source

pub fn update_cache_and_elem_with_filter<'a>( &'a mut self, f: impl FnMut(&mut B::Cache) -> bool + 'a, g: impl FnMut(&mut B::Elem) + 'a, )

This method allows users to update the caches and the elements with a filter.

If f returns true for a node, it will drill down into the subtree whose root is the node.

It’s the caller’s responsibility to ensure the invariance of caches being up to date.

Source

pub fn depth(&self) -> usize

Source

pub fn internal_avg_children_num(&self) -> f64

Source§

impl<B: BTreeTrait> BTree<B>

Source

pub fn check(&self)

Trait Implementations§

Source§

impl<Elem: Clone, B: BTreeTrait<Elem = Elem>> Clone for BTree<B>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Cache: Debug, Elem: Debug, B: BTreeTrait<Elem = Elem, Cache = Cache>> Debug for BTree<B>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<B: BTreeTrait> Default for BTree<B>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<B: BTreeTrait, T: Into<B::Elem>> FromIterator<T> for BTree<B>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<B> Freeze for BTree<B>
where <B as BTreeTrait>::Cache: Freeze,

§

impl<B> RefUnwindSafe for BTree<B>

§

impl<B> Send for BTree<B>
where <B as BTreeTrait>::Cache: Send, <B as BTreeTrait>::Elem: Send,

§

impl<B> Sync for BTree<B>
where <B as BTreeTrait>::Cache: Sync, <B as BTreeTrait>::Elem: Sync,

§

impl<B> Unpin for BTree<B>
where <B as BTreeTrait>::Cache: Unpin, <B as BTreeTrait>::Elem: Unpin,

§

impl<B> UnwindSafe for BTree<B>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.