Module

Temporal.Interval.Set

Package
purescript-temporal
Repository
philippedev101/purescript-temporal

Sorted, non-overlapping interval collections.

An IntervalSet maintains the invariant that its intervals are sorted by start, non-overlapping, non-adjacent, and non-empty. All operations preserve this invariant.

Use IntervalSet for scheduling, calendar free/busy queries, and timeline operations.

#IntervalSet Source

newtype IntervalSet a

A sorted collection of non-overlapping, non-adjacent, non-empty half-open intervals.

Instances

#empty Source

empty :: forall a. IntervalSet a

The empty interval set.

#singleton Source

singleton :: forall a. Interval a -> IntervalSet a

An interval set containing a single interval.

#fromIntervals Source

fromIntervals :: forall a. Ord a => Array (Interval a) -> IntervalSet a

Build an interval set from an array of intervals, merging any that overlap or are adjacent. O(n log n).

#toArray Source

toArray :: forall a. IntervalSet a -> Array (Interval a)

Extract the sorted array of non-overlapping intervals.

#isEmpty Source

isEmpty :: forall a. IntervalSet a -> Boolean

Whether the set contains no intervals.

#span Source

span :: forall a. Ord a => IntervalSet a -> Maybe (Interval a)

The bounding interval (from the earliest start to the latest end). Returns Nothing for an empty set.

#member Source

member :: forall a. Ord a => a -> IntervalSet a -> Boolean

Whether a point is contained in any interval in the set. O(log n).

#findContaining Source

findContaining :: forall a. Ord a => a -> IntervalSet a -> Maybe (Interval a)

Find the interval containing the given point, if any. O(log n).

#gaps Source

gaps :: forall a. Ord a => IntervalSet a -> IntervalSet a

The gaps (free time) between intervals within the set's span. Returns an empty set if the set has fewer than 2 intervals.

#insert Source

insert :: forall a. Ord a => Interval a -> IntervalSet a -> IntervalSet a

Insert an interval into the set, merging with any overlapping or adjacent existing intervals. O(n).

#remove Source

remove :: forall a. Ord a => Interval a -> IntervalSet a -> IntervalSet a

Remove an interval from the set, punching holes as needed. O(n).

#unionSets Source

unionSets :: forall a. Ord a => IntervalSet a -> IntervalSet a -> IntervalSet a

Union of two interval sets. O(n + m).

#intersectSets Source

intersectSets :: forall a. Ord a => IntervalSet a -> IntervalSet a -> IntervalSet a

Intersection of two interval sets — the time present in both. O(n + m).

#differenceSets Source

differenceSets :: forall a. Ord a => IntervalSet a -> IntervalSet a -> IntervalSet a

Difference of two interval sets — time in the first but not the second. O(n + m).

#complement Source

complement :: forall a. Ord a => Interval a -> IntervalSet a -> IntervalSet a

The complement of an interval set within a bounding interval — the gaps relative to the bound.

complement [0, 100) { [10, 20), [50, 60) }
-- { [0, 10), [20, 50), [60, 100) }