pub struct Interval<T>where
T: Copy + PartialOrd,{
pub start: T,
pub end: T,
}Expand description
Half-open time interval [start, end).
Half-open intervals are convenient for period arithmetic because adjacent
intervals such as [a, b) and [b, c) touch without overlapping.
Fields§
§start: TInclusive lower bound.
end: TExclusive upper bound.
Implementations§
Source§impl<T> Interval<T>where
T: Copy + PartialOrd,
impl<T> Interval<T>where
T: Copy + PartialOrd,
Sourcepub fn new<S, E>(start: S, end: E) -> Interval<T>
pub fn new<S, E>(start: S, end: E) -> Interval<T>
Construct without validation.
This accepts any start/end pair, including reversed or NaN-like
endpoints. Prefer try_new when the bounds come from
computation or external input.
Sourcepub fn try_new<S, E>(
start: S,
end: E,
) -> Result<Interval<T>, InvalidIntervalError>
pub fn try_new<S, E>( start: S, end: E, ) -> Result<Interval<T>, InvalidIntervalError>
Validating constructor: rejects start > end and NaN endpoints.
Zero-length intervals where start == end are allowed.
Sourcepub fn intersection(&self, other: &Interval<T>) -> Option<Interval<T>>
pub fn intersection(&self, other: &Interval<T>) -> Option<Interval<T>>
Overlap as a half-open range. Returns None if the intervals touch
only at a point or do not overlap.
Sourcepub fn union(&self, other: &Interval<T>) -> Vec<Interval<T>>
pub fn union(&self, other: &Interval<T>) -> Vec<Interval<T>>
Smallest interval covering both self and other, together with any
gap between them.
Returns one interval when they overlap or touch, two when they are strictly disjoint (sorted by start time).
This pairwise API preserves disjointness instead of forcing a merged interval that would invent coverage through the gap.
Sourcepub fn complement(&self, periods: &[Interval<T>]) -> Vec<Interval<T>>
pub fn complement(&self, periods: &[Interval<T>]) -> Vec<Interval<T>>
Gaps inside self that are not covered by any interval in periods.
periods must be sorted and non-overlapping
(see Interval::validate). Intervals outside self are effectively
ignored except for how they advance the internal cursor.
Sourcepub fn try_complement(
&self,
periods: &[Interval<T>],
) -> Result<Vec<Interval<T>>, PeriodListError>
pub fn try_complement( &self, periods: &[Interval<T>], ) -> Result<Vec<Interval<T>>, PeriodListError>
Checked variant of complement.
Validates that self is well ordered and that periods is sorted,
non-overlapping, and internally valid before computing the complement.
Sourcepub fn validate(periods: &[Interval<T>]) -> Result<(), PeriodListError>
pub fn validate(periods: &[Interval<T>]) -> Result<(), PeriodListError>
Check that a list is sorted, non-overlapping, and each start <= end.
Touching intervals such as [a, b) followed by [b, c) are valid.
Sourcepub fn intersect_many(a: &[Interval<T>], b: &[Interval<T>]) -> Vec<Interval<T>>
pub fn intersect_many(a: &[Interval<T>], b: &[Interval<T>]) -> Vec<Interval<T>>
Intersection of two sorted, non-overlapping period lists. O(n + m).
Sourcepub fn try_intersect_many(
a: &[Interval<T>],
b: &[Interval<T>],
) -> Result<Vec<Interval<T>>, PeriodListError>
pub fn try_intersect_many( a: &[Interval<T>], b: &[Interval<T>], ) -> Result<Vec<Interval<T>>, PeriodListError>
Checked variant of intersect_many.
Validates both input lists before computing their intersection.
Trait Implementations§
Source§impl<T> PartialEq for Interval<T>
impl<T> PartialEq for Interval<T>
impl<T> Copy for Interval<T>where
T: Copy + PartialOrd,
impl<T> StructuralPartialEq for Interval<T>where
T: Copy + PartialOrd,
Auto Trait Implementations§
impl<T> Freeze for Interval<T>where
T: Freeze,
impl<T> RefUnwindSafe for Interval<T>where
T: RefUnwindSafe,
impl<T> Send for Interval<T>where
T: Send,
impl<T> Sync for Interval<T>where
T: Sync,
impl<T> Unpin for Interval<T>where
T: Unpin,
impl<T> UnsafeUnpin for Interval<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for Interval<T>where
T: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.