Skip to main content

IntervalTree

Struct IntervalTree 

Source
pub struct IntervalTree<T: Ord + Clone + Default, V, A: Allocator = Global> { /* private fields */ }
Available on crate feature interval-tree only.
Expand description

Am interval tree that supports O(log n) overlap queries.

Built on an augmented red-black tree where each subtree tracks the maximum upper bound (hi) of all intervals it contains. This enables efficient pruning during overlap queries.

§Type Parameters

  • T: The endpoint type. Must be Ord + Clone.
  • V: The value associated with each interval.
  • A: Allocator (defaults to Global]).

§Examples

use augmented_rbtree::interval_tree::{Interval, IntervalTree};

let mut tree = IntervalTree::new();
tree.insert(Interval::new(1, 5), "a");
tree.insert(Interval::new(3, 9), "b");
tree.insert(Interval::new(7, 10), "c");

let overlapping: Vec<_> = tree.query_overlap(4, 8).map(|(iv, v)| (*iv, *v)).collect();
assert_eq!(overlapping.len(), 3);

Implementations§

Source§

impl<T: Ord + Clone + Default, V> IntervalTree<T, V>

Source

pub fn new() -> Self

Creates a new, empty interval tree.

Source§

impl<T: Ord + Clone + Default, V, A: Allocator> IntervalTree<T, V, A>

Source

pub fn new_in(alloc: A) -> Self

Creates a new interval tree with the given allocator.

Source

pub fn inner_tree(&self) -> &AugmentedRBTree<Interval<T>, V, MaxHi<T>, A>

Returns a reference to the underlying augmented red-black tree. This can be used for advanced operations or visualization.

Source

pub fn insert(&mut self, interval: Interval<T>, value: V) -> Option<V>

Inserts an interval-value pair.

If the exact interval already exists, the old value is replaced and returned.

Source

pub fn remove(&mut self, interval: &Interval<T>) -> Option<V>

Removes an interval from the tree, returning its value if it existed.

Source

pub fn get(&self, interval: &Interval<T>) -> Option<&V>

Returns a reference to the value associated with interval, if present.

Source

pub fn contains(&self, interval: &Interval<T>) -> bool

Returns true if the tree contains the exact interval.

Source

pub fn len(&self) -> usize

Returns the number of intervals in the tree.

Source

pub fn is_empty(&self) -> bool

Returns true if the tree is empty.

Source

pub fn iter(&self) -> impl Iterator<Item = (&Interval<T>, &V)>

Returns an iterator over all (interval, value) pairs in sorted order (by lo, then hi).

Source

pub fn query_overlap<K>( &self, lo: K, hi: K, ) -> impl Iterator<Item = (&Interval<T>, &V)>
where K: Borrow<T>,

Returns an iterator over all intervals that overlap with [lo, hi].

Complexity: O(k log n) where k is the number of overlapping intervals.

§Examples
use augmented_rbtree::interval_tree::{Interval, IntervalTree};

let mut tree = IntervalTree::new();
tree.insert(Interval::new(1, 5), ());
tree.insert(Interval::new(6, 10), ());
tree.insert(Interval::new(3, 8), ());

let overlapping: Vec<_> = tree.query_overlap(4, 7).collect();
assert_eq!(overlapping.len(), 3); // [1,5], [3,8] and [6,10] all overlap [4,7]
Source

pub fn query_point<K>( &self, point: K, ) -> impl Iterator<Item = (&Interval<T>, &V)>
where K: Borrow<T> + Clone,

Returns an iterator over all intervals that contain the point p.

An interval [a, b] contains p iff a <= p <= b.

Complexity: O(k log n) where k is the number of matching intervals.

Source

pub fn any_overlaps<K>(&self, lo: K, hi: K) -> bool
where K: Borrow<T>,

Returns true if any interval in the tree overlaps with [lo, hi].

Complexity: O(log n).

Source

pub fn any_contains_point<K>(&self, point: K) -> bool
where K: Borrow<T> + Clone,

Returns true if any interval contains the given point.

Complexity: O(log n).

Source

pub fn first_overlap<K>(&self, lo: K, hi: K) -> Option<(&Interval<T>, &V)>
where K: Borrow<T>,

Returns the first overlapping interval with [lo, hi], if any.

When multiple intervals overlap, returns the one with the smallest lo.

Complexity: O(log n).

Trait Implementations§

Source§

impl<T: Ord + Clone + Default + Debug, V: Debug> Debug for IntervalTree<T, V>

Source§

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

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

impl<T: Ord + Clone + Default, V> Default for IntervalTree<T, V>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T, V, A> Freeze for IntervalTree<T, V, A>
where A: Freeze,

§

impl<T, V, A> RefUnwindSafe for IntervalTree<T, V, A>

§

impl<T, V, A> Send for IntervalTree<T, V, A>
where V: Send, T: Send, A: Send,

§

impl<T, V, A> Sync for IntervalTree<T, V, A>
where V: Sync, T: Sync, A: Sync,

§

impl<T, V, A> Unpin for IntervalTree<T, V, A>
where A: Unpin, V: Unpin, T: Unpin,

§

impl<T, V, A> UnsafeUnpin for IntervalTree<T, V, A>
where A: UnsafeUnpin,

§

impl<T, V, A> UnwindSafe for IntervalTree<T, V, A>

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.