Skip to content

GetBoundingInterval()

The GetBoundingInterval() extension method returns an Option<BasicMeasuredInterval<TBoundary, TLength>> containing the minimum interval that completely encloses all intervals contained within an IDisjointIntervalSet<TBoundary, TLength>, or Option.None if the set is empty.

Method Signature

public static Option<BasicMeasuredInterval<TBoundary, TLength>> GetBoundingInterval<TBoundary, TLength>(
    this IDisjointIntervalSet<TBoundary, TLength> s)
    where TBoundary : notnull, IComparable<TBoundary>;

How It Works

GetBoundingInterval() computes the smallest contiguous interval spanning from the earliest Start boundary to the latest End boundary across all intervals in the set: - If the set is empty (s.Count == 0), it returns Option.None<BasicMeasuredInterval<TBoundary, TLength>>(). - Otherwise, the start boundary is s.Start, with inclusion determined by s.Covers(s.Start). - The end boundary is s.End, with inclusion determined by s.Covers(s.End). - The returned interval is a BasicMeasuredInterval<TBoundary, TLength> measured by the set's LengthOperator, wrapped in Option.Some.

Real Line:  0---1---2---3---4---5---6---7---8---9---10

Set S:              [=======]       [=======]  ([2, 4] and [6, 8])

S.GetBoundingInterval() ->
                    [=======================]  (Some([2, 8]))

Example

using System;
using Marsop.Ephemeral.Core;
using Marsop.Ephemeral.Numerics;
using Optional;

var lengthOperator = IntDefaultLengthOperator.Instance;

// Set with two disjoint intervals separated by a gap
var set = new DisjointIntervalSet<int, int>(
    lengthOperator,
    IntInterval.CreateClosed(2, 4),
    IntInterval.CreateClosed(6, 8)
);

Option<BasicMeasuredInterval<int, int>> result = set.GetBoundingInterval();

result.MatchSome(bounds =>
{
    Console.WriteLine(bounds.Start); // 2
    Console.WriteLine(bounds.End);   // 8
    Console.WriteLine(bounds);       // [2, 8]

    // Because it is a measured interval, you can measure its total span
    int totalSpan = bounds.Measure(lengthOperator); // 6
});

See Also