pub struct Interval<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: Copy + PartialOrd> Interval<T>
impl<T: Copy + PartialOrd> Interval<T>
Sourcepub fn new<S: Into<T>, E: Into<T>>(start: S, end: E) -> Self
pub fn new<S: Into<T>, E: Into<T>>(start: S, end: E) -> Self
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: Into<T>, E: Into<T>>(
start: S,
end: E,
) -> Result<Self, InvalidIntervalError>
pub fn try_new<S: Into<T>, E: Into<T>>( start: S, end: E, ) -> Result<Self, InvalidIntervalError>
Validating constructor: rejects !(start <= end) (e.g. reversed bounds or unordered floats such as NaN).
Zero-length intervals where start == end are allowed.
Sourcepub fn intersection(&self, other: &Self) -> Option<Self>
pub fn intersection(&self, other: &Self) -> Option<Self>
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: &Self) -> Vec<Self>
pub fn union(&self, other: &Self) -> Vec<Self>
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: &[Self]) -> Vec<Self>
pub fn complement(&self, periods: &[Self]) -> Vec<Self>
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: &[Self],
) -> Result<Vec<Self>, PeriodListError>
pub fn try_complement( &self, periods: &[Self], ) -> Result<Vec<Self>, 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: &[Self]) -> Result<(), PeriodListError>
pub fn validate(periods: &[Self]) -> 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: &[Self], b: &[Self]) -> Vec<Self>
pub fn intersect_many(a: &[Self], b: &[Self]) -> Vec<Self>
Intersection of two sorted, non-overlapping period lists. O(n + m).
Sourcepub fn try_intersect_many(
a: &[Self],
b: &[Self],
) -> Result<Vec<Self>, PeriodListError>
pub fn try_intersect_many( a: &[Self], b: &[Self], ) -> Result<Vec<Self>, PeriodListError>
Checked variant of intersect_many.
Validates both input lists before computing their intersection.
Sourcepub fn union_many(a: &[Self], b: &[Self]) -> Vec<Self>
pub fn union_many(a: &[Self], b: &[Self]) -> Vec<Self>
Union of two sorted period lists.
Overlapping and adjacent intervals are merged. The result is sorted and non-overlapping.
Sourcepub fn normalize(periods: &[Self]) -> Vec<Self>
pub fn normalize(periods: &[Self]) -> Vec<Self>
Sort and merge overlapping or adjacent intervals.
This is useful for normalizing hand-built or concatenated period lists before later set operations.