Skip to main content

IntervalTree

Struct IntervalTree 

Source
pub struct IntervalTree<T: Clone> { /* private fields */ }
Expand description

A interval tree using red-black tree, whereas keys are intervals, and values are plists in elisp.

All intervals are half-opened intervals, i.e. I = [start, end).These intervals are sorted by their starting point, then their ending point.

NOTE When inserting an interval I, if I intersects with some existing interval J but I != J, then we split & merge I and J into sub-intervals. Thus all intervals inside a interval tree will not overlap. Adjacant intervals with identical props should be merged afterwards, maybe during redisplay.

Implementations§

Source§

impl<T: Clone> IntervalTree<T>

Source

pub fn new() -> Self

Creates an empty interval tree.

Source

pub fn insert<'a, F: Fn(T, T) -> T>( &'a mut self, key: impl Into<TextRange>, val: T, merge_fn: F, ) -> Option<&'a mut Box<Node<T>>>

Inserts a new interval with the specified key and val into the interval tree.

If the interval key is degenerate (i.e., its start equals its end), the function returns None as such intervals are not allowed in the tree. Otherwise, it delegates the insertion to the underlying node structure.

§Arguments
  • key - The text range representing the interval to insert.
  • val - The value associated with the interval.
  • merge - A closure that specifies how to merge intervals if they overlap
§Returns

An optional mutable reference to the newly inserted node, or None if the interval is degenerate.

Source

pub fn get(&self, key: impl Into<TextRange>) -> Option<T>

Finds the node with key key in the tree and returns its value if found.

§Arguments
  • key - The text range representing the interval to search for.
§Returns

An optional value associated with the node if it exists, None otherwise.

Source

pub fn delete(&mut self, key: impl Into<TextRange>) -> MaybeNode<T>

Delete the node with key key from the tree.

If the root node is the only black node, then we have to make it red before deleting. Otherwise, the tree would become unbalanced.

After deleting, we make sure the root node is black again.

Source

pub fn delete_min(&mut self) -> MaybeNode<T>

Deletes the node with the minimum key from the interval tree.

If the root node is the only black node, it is temporarily colored red to maintain tree balance during deletion. After deletion, the root node is recolored black to ensure the red-black tree properties are preserved.

§Returns

An optional Node<T> representing the removed node, or None if the tree is empty.

Source

pub fn delete_max(&mut self) -> MaybeNode<T>

Source

pub fn advance(&mut self, position: usize, length: usize)

Advances all intervals in the tree by length, starting at position. This is typically used to implement operations that insert or delete text in a buffer.

Source

pub fn find(&self, position: usize) -> Option<&Node<T>>

Find the node whose interval contains the given position. If no interval contains the position, returns None.

Source

pub fn find_intersects(&self, range: TextRange) -> Vec<&Node<T>>

Find all nodes in the tree whose intervals intersect the given range. The result is a vector of references to the found nodes.

Source

pub fn min(&self) -> Option<&Node<T>>

Return the minimum node in the tree, or None if the tree is empty.

Source

pub fn merge<F: Fn(&T, &T) -> bool>(&mut self, equal: F)

Merges adjacent intervals in the tree that have equal properties.

This function iterates over the nodes in the interval tree, starting from the minimum node. It checks if the current node’s end equals the next node’s start and if their values are considered equal by the provided equal function. If both conditions are met, it merges the intervals by extending the current node’s end to the next node’s end and deletes the next node.

§Arguments
  • equal - A closure that takes references to two values and returns true if they are considered equal, false otherwise.
Source

pub fn apply<F: FnMut(&T)>(&self, f: &mut F)

Source

pub fn apply_mut<F: FnMut(&mut Node<T>)>(&mut self, f: &mut F)

Source§

impl<T: Clone + Debug> IntervalTree<T>

Source

pub fn print(&self)

Recursively print out the tree, for debugging purposes. The output format is not guaranteed to be stable.

Trait Implementations§

Source§

impl<T: Clone + Debug> Debug for IntervalTree<T>

Source§

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

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

impl<T: Default + Clone> Default for IntervalTree<T>

Source§

fn default() -> IntervalTree<T>

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

Auto Trait Implementations§

§

impl<T> Freeze for IntervalTree<T>

§

impl<T> RefUnwindSafe for IntervalTree<T>
where T: RefUnwindSafe,

§

impl<T> !Send for IntervalTree<T>

§

impl<T> !Sync for IntervalTree<T>

§

impl<T> Unpin for IntervalTree<T>

§

impl<T> UnsafeUnpin for IntervalTree<T>

§

impl<T> UnwindSafe for IntervalTree<T>

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> 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, 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.