IntervalTree

Struct IntervalTree 

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

An immutable data structure for storing and querying a collection of intervals

§Example

use std::ops::Bound::*;
use im_interval_tree::{IntervalTree, Interval};

// Construct a tree of intervals
let tree : IntervalTree<u8> = IntervalTree::new();
let tree = tree.insert(Interval::new(Included(1), Excluded(3)));
let tree = tree.insert(Interval::new(Included(2), Excluded(4)));
let tree = tree.insert(Interval::new(Included(5), Unbounded));
let tree = tree.insert(Interval::new(Excluded(7), Included(8)));

// Query for overlapping intervals
let query = tree.query_interval(&Interval::new(Included(3), Included(6)));
assert_eq!(
    query.collect::<Vec<Interval<u8>>>(),
    vec![
        Interval::new(Included(2), Excluded(4)),
        Interval::new(Included(5), Unbounded)
    ]
);

// Query for a specific point
let query = tree.query_point(&2);
assert_eq!(
    query.collect::<Vec<Interval<u8>>>(),
    vec![
        Interval::new(Included(2), Excluded(4)),
        Interval::new(Included(1), Excluded(3))
    ]
);

Implementations§

Source§

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

Source

pub fn new() -> IntervalTree<T>

Construct an empty IntervalTree

Source

pub fn insert(&self, interval: Interval<T>) -> IntervalTree<T>

Construct a new IntervalTree with the given Interval added

§Example
let tree : IntervalTree<u8> = IntervalTree::new();
let tree = tree.insert(Interval::new(Included(1), Included(2)));
assert_eq!(
    tree.iter().collect::<Vec<Interval<u8>>>(),
    vec![Interval::new(Included(1), Included(2))]
);
Source

pub fn remove(&self, interval: &Interval<T>) -> IntervalTree<T>

Construct a new IntervalTree minus the given Interval, if present

§Example
let tree : IntervalTree<u8> = IntervalTree::new();
let tree = tree.insert(Interval::new(Included(1), Included(2)));
let tree = tree.insert(Interval::new(Included(1), Included(3)));

let tree = tree.remove(&Interval::new(Included(1), Included(2)));
assert_eq!(
    tree.iter().collect::<Vec<Interval<u8>>>(),
    vec![Interval::new(Included(1), Included(3))]
);
Source

pub fn query_interval( &self, interval: &Interval<T>, ) -> impl Iterator<Item = Interval<T>> + '_

Return an Iterator over all the intervals in the tree that overlap with the given interval

§Example
let tree : IntervalTree<u8> = IntervalTree::new();
let tree = tree.insert(Interval::new(Included(1), Excluded(3)));
let tree = tree.insert(Interval::new(Included(5), Unbounded));

let query = tree.query_interval(&Interval::new(Included(3), Included(6)));
assert_eq!(
    query.collect::<Vec<Interval<u8>>>(),
    vec![Interval::new(Included(5), Unbounded)]
);
Source

pub fn query_point(&self, point: &T) -> impl Iterator<Item = Interval<T>> + '_

Return an Iterator over all the intervals in the tree that contain the given point

This is equivalent to tree.query_interval(Interval::new(Included(point), Included(point)))

§Example
let tree : IntervalTree<u8> = IntervalTree::new();
let tree = tree.insert(Interval::new(Included(1), Excluded(3)));
let tree = tree.insert(Interval::new(Included(5), Unbounded));

let query = tree.query_point(&2);
assert_eq!(
    query.collect::<Vec<Interval<u8>>>(),
    vec![Interval::new(Included(1), Excluded(3))]
);
Source

pub fn iter(&self) -> impl Iterator<Item = Interval<T>> + '_

Return an Iterator over all the intervals in the tree

This is equivalent to tree.query_interval(Unbounded, Unbounded)

§Example
let tree : IntervalTree<u8> = IntervalTree::new();
let tree = tree.insert(Interval::new(Included(2), Excluded(4)));
let tree = tree.insert(Interval::new(Included(5), Unbounded));

let iter = tree.iter();
assert_eq!(
    iter.collect::<Vec<Interval<u8>>>(),
    vec![
        Interval::new(Included(2), Excluded(4)),
        Interval::new(Included(5), Unbounded),
    ]
);

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> IntervalTree<T>

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<T: Hash + Ord + Clone> Hash for IntervalTree<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. 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> UnwindSafe for IntervalTree<T>
where T: RefUnwindSafe,

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