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§
Source§impl<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§
Source§impl<T: Ord> Ord for Interval<T>
impl<T: Ord> Ord for Interval<T>
Source§impl<T: Ord> PartialOrd for Interval<T>
impl<T: Ord> PartialOrd for Interval<T>
impl<T: Ord> Eq for Interval<T>
Auto Trait Implementations§
impl<T> Freeze for Interval<T>
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§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more