Struct store_interval_tree::Interval
source · pub struct Interval<T: Ord> { /* private fields */ }
Expand description
A utility data structure to represent intervals. It supports open, close and unbounded intervals
Examples
use store_interval_tree::Interval;
use std::ops::Bound::*;
// initialize interval [2,4]
let interval1 = Interval::new(Included(2), Included(4));
// initialize interval [2,4)
let interval2 = Interval::new(Included(2), Excluded(4));
// initialize point [4,4]
let point1 = Interval::point(4);
// compare intervals
// first, lower bounds are compared. if they're equal, higher bounds will be compared
assert!(interval2 < interval1);
// check if two intervals overlap
assert!(Interval::overlaps(&interval1, &interval2));
// check if one point and an interval overlap
assert!(Interval::overlaps(&interval1, &point1));
assert!(!Interval::overlaps(&interval2, &point1));
// check if one interval contains another interval
assert!(Interval::contains(&interval1, &interval2));
// get overlapped interval between two intervals
assert!(Interval::get_overlap(&interval1, &interval2).unwrap() == interval2);
Implementations
sourceimpl<T: Ord> Interval<T>
impl<T: Ord> Interval<T>
sourcepub fn new(low: Bound<T>, high: Bound<T>) -> Interval<T>
pub fn new(low: Bound<T>, high: Bound<T>) -> Interval<T>
Creates a new interval
Arguments
low
: lower bound of the intervalhigh
: higher bound of the interval
Panics
- panics if
low
>high
.low
==high
is acceptable if interval is closed at both sides: [low, high]
Example
use store_interval_tree::Interval;
use std::ops::Bound::*;
// create the interval [2,4)
let interval1 = Interval::new(Included(2), Excluded(4));
// create the interval (-inf,4)
let interval2 = Interval::new(Unbounded, Excluded(4));
// create the interval (1,+inf)
let interval3 = Interval::new(Excluded(1), Unbounded);
sourcepub fn duplicate(&self) -> Interval<T>
pub fn duplicate(&self) -> Interval<T>
Creates a duplicate of the interval
Examples
use store_interval_tree::Interval;
use std::ops::Bound::*;
let interval = Interval::new(Included(2), Unbounded);
let duplicate = interval.duplicate();
assert!(interval == duplicate);
sourcepub fn overlaps(first: &Interval<T>, second: &Interval<T>) -> bool
pub fn overlaps(first: &Interval<T>, second: &Interval<T>) -> bool
Returns true if first
and second
intervals overlap, false otherwise
Examples
use store_interval_tree::Interval;
use std::ops::Bound::*;
let interval1 = Interval::new(Included(2), Included(4));
let interval2 = Interval::new(Included(2), Excluded(4));
let point1 = Interval::point(4);
assert!(Interval::overlaps(&interval1, &interval2));
assert!(Interval::overlaps(&interval1, &point1));
assert!(!Interval::overlaps(&interval2, &point1));
sourcepub fn contains(first: &Interval<T>, second: &Interval<T>) -> bool
pub fn contains(first: &Interval<T>, second: &Interval<T>) -> bool
Returns true if second
is a sub-interval of first
, false otherwise
Examples
use store_interval_tree::Interval;
use std::ops::Bound::*;
let interval1 = Interval::new(Included(2), Included(4));
let interval2 = Interval::new(Included(2), Excluded(4));
assert!(Interval::contains(&interval1, &interval2));
sourcepub fn get_overlap(
first: &Interval<T>,
second: &Interval<T>
) -> Option<Interval<T>>
pub fn get_overlap(
first: &Interval<T>,
second: &Interval<T>
) -> Option<Interval<T>>
Get overlapped interval of first
and second
, None
otherwise
Examples
use store_interval_tree::Interval;
use std::ops::Bound::*;
// initialize intervals
let interval1 = Interval::new(Included(2), Included(4));
let interval2 = Interval::new(Included(2), Excluded(4));
assert!(Interval::get_overlap(&interval1, &interval2).unwrap() == interval2);
Trait Implementations
sourceimpl<T: Ord> Ord for Interval<T>
impl<T: Ord> Ord for Interval<T>
1.21.0 · sourcefn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
1.21.0 · sourcefn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the minimum of two values. Read more
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Selfwhere
Self: Sized + PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: Sized + PartialOrd<Self>,
Restrict a value to a certain interval. Read more
sourceimpl<T: Ord> PartialOrd<Interval<T>> for Interval<T>
impl<T: Ord> PartialOrd<Interval<T>> for Interval<T>
sourcefn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moreimpl<T: Ord> Eq for Interval<T>
Auto Trait Implementations
impl<T> RefUnwindSafe for Interval<T>where
T: RefUnwindSafe,
impl<T> !Send for Interval<T>
impl<T> !Sync for Interval<T>
impl<T> Unpin for Interval<T>
impl<T> UnwindSafe for Interval<T>where
T: RefUnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more