Struct IntervalTree

Source
pub struct IntervalTree<T, D>
where T: IntervalType,
{ /* private fields */ }
Expand description

An Interval Tree.

Implementations§

Source§

impl<T, D> IntervalTree<T, D>
where T: IntervalType,

Source

pub fn new_from_entry<I>(entry: I) -> Self
where I: Into<IntervalTreeEntry<T, D>>,

Creates a new IntervalTree from a root entry.

§Parameters
  • entry - The first entry.
§Example
use space_partitioning::IntervalTree;
let tree = IntervalTree::new_from_entry((15..=20, "data"));
assert_eq!(tree.len(), 1);
Source

pub fn insert<I>(&mut self, entry: I) -> &Self
where I: Into<IntervalTreeEntry<T, D>>,

Inserts a new entry to the IntervalTree.

§Parameters
  • entry - The entry to insert.
§Example
use space_partitioning::IntervalTree;
let mut tree = IntervalTree::default();
tree.insert((15..=20, "data"));
assert_eq!(tree.len(), 1);
Source

pub fn len(&self) -> usize

Returns the number of elements in the IntervalTree.

§Example
use space_partitioning::IntervalTree;
let tree = IntervalTree::new_from_entry((15..=20, "data"));
assert_eq!(tree.len(), 1);
Source

pub fn is_empty(&self) -> bool

Returns whether the tree is empty, i.e., whether it has no elements.

§Example
use space_partitioning::IntervalTree;
let tree = IntervalTree::<i32, ()>::default();
assert!(tree.is_empty());

Queries the tree for overlaps with the specified interval.

/// # Parameters

  • interval - The interval to query for.
§Example
use space_partitioning::IntervalTree;
use space_partitioning::interval_tree::Interval;

let mut tree = IntervalTree::new_from_entry((15..=20, "A"));
tree.insert((100..=101, "B"));

let matched_a = tree.overlap_search(&(18..=25).into()).unwrap();
assert_eq!(matched_a.interval.start, 15);
assert_eq!(matched_a.interval.end, 20);
assert_eq!(matched_a.data, "A");

let matched_b = tree.overlap_search(&(100..=100).into()).unwrap();
assert_eq!(matched_b.interval.start, 100);
assert_eq!(matched_b.interval.end, 101);
assert_eq!(matched_b.data, "B");

let no_match = tree.overlap_search(0..=5);
assert!(no_match.is_none());
Source

pub fn iter_inorder(&self) -> InorderIterator<'_, T, D>

Returns an InorderIterator<T, D> that iterates the tree elements in order of their interval starts.

§Example
use space_partitioning::IntervalTree;
use std::iter::FromIterator;

let tree = IntervalTree::from_iter([(18..=25, "abc"), (0..=20, "xyz")]);
let mut iter = tree.iter_inorder();

let first = iter.next().unwrap();
assert_eq!(first.interval.start, 0);
assert_eq!(first.interval.end, 20);
assert_eq!(first.data, "xyz");

let second = iter.next().unwrap();
assert_eq!(second.interval.start, 18);
assert_eq!(second.interval.end, 25);
assert_eq!(second.data, "abc");

assert!(iter.next().is_none());

Trait Implementations§

Source§

impl<T, D> Debug for IntervalTree<T, D>
where T: Debug + IntervalType,

Source§

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

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

impl<T, D> Default for IntervalTree<T, D>
where T: IntervalType,

Source§

fn default() -> Self

Returns an empty IntervalTree<T, D>.

§Example
use space_partitioning::IntervalTree;
let tree = IntervalTree::<i32, ()>::default();
assert_eq!(tree.len(), 0);
Source§

impl<T, D> From<(Interval<T>, D)> for IntervalTree<T, D>
where T: IntervalType,

Source§

fn from(value: (Interval<T>, D)) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Interval<T>> for IntervalTree<T, ()>
where T: IntervalType,

Source§

fn from(interval: Interval<T>) -> Self

Converts to this type from the input type.
Source§

impl<I, T, D> FromIterator<I> for IntervalTree<T, D>
where I: Into<IntervalTreeEntry<T, D>>, T: IntervalType,

Source§

fn from_iter<Iter>(iter: Iter) -> Self
where Iter: IntoIterator<Item = I>,

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<T, D> Freeze for IntervalTree<T, D>
where T: Freeze, D: Freeze,

§

impl<T, D> RefUnwindSafe for IntervalTree<T, D>

§

impl<T, D> Send for IntervalTree<T, D>
where T: Send, D: Send,

§

impl<T, D> Sync for IntervalTree<T, D>
where T: Sync, D: Sync,

§

impl<T, D> Unpin for IntervalTree<T, D>
where T: Unpin, D: Unpin,

§

impl<T, D> UnwindSafe for IntervalTree<T, D>
where T: UnwindSafe, D: UnwindSafe,

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.