Table of Contents

IntervalSet<T> Struct

Definition

Namespace
Bodu.Numerics
Assembly
Bodu.Numerics.dll
Package
Bodu.Numerics 1.0.0
Source
IntervalSet{T}.SetOperations.cs

Represents an immutable set of values as a normalized collection of disjoint, non-adjacent Interval<T> pieces in ascending order - the general result of interval algebra that can produce a disconnected range.

public readonly struct IntervalSet<T> : IEquatable<IntervalSet<T>> where T : INumber<T>

Type Parameters

T

The numeric type used for the intervals' endpoints.

Implements
Inherited Members
Extension Methods

Examples

var set = IntervalSet<int>.Of(Interval<int>.Closed(1, 3), Interval<int>.Closed(2, 5), Interval<int>.Closed(8, 9));
set.Count;                 // 2 - [1, 5] and [8, 9] (the first two coalesced)
set.Contains(4);           // True
set.Complement();          // (-∞, 1) ∪ (5, 8) ∪ (9, +∞)

Remarks

Where Interval<T> models a single connected range and IntervalPair<T> the at-most-two pieces of a binary difference, IntervalSet<T> models an arbitrary union of ranges. Overlapping and adjacent inputs are coalesced at construction, so the pieces are always disjoint, non-adjacent, sorted, and free of empty entries - a canonical form in which set equality is piecewise equality.

The type is the home for the operations a single interval cannot express as one value: N-ary Union(Interval<T>), Except(Interval<T>), and Complement() (the values not in the set, over the whole line). It composes with Interval<T> throughout.

Properties

Count

Gets the number of disjoint pieces in the set.

public int Count { get; }

Property Value

int

The count of normalized intervals.

Empty

Gets the empty set - the set containing no values.

public static IntervalSet<T> Empty { get; }

Property Value

IntervalSet<T>

An IntervalSet<T> whose Count is zero.

IsEmpty

Gets a value indicating whether the set contains no values.

public bool IsEmpty { get; }

Property Value

bool

true when the set has no pieces; otherwise false.

this[int]

Gets the piece at index, ordered lowest-first.

public Interval<T> this[int index] { get; }

Parameters

index int

The zero-based index, less than Count.

Property Value

Interval<T>

The interval at the requested position.

Exceptions

ArgumentOutOfRangeException

index is out of range.

Methods

Complement()

Returns the complement of this set - every value not in the set, over the whole line (-∞, +∞).

public IntervalSet<T> Complement()

Returns

IntervalSet<T>

The complement set.

Examples

IntervalSet<int>.Of(Interval<int>.Closed(1, 5)).Complement();   // (-∞, 1) ∪ (5, +∞)

Contains(T)

Determines whether value is a member of any piece of the set.

public bool Contains(T value)

Parameters

value T

The value to test.

Returns

bool

true when the set contains value; otherwise false.

Encloses(Interval<T>)

Determines whether every value of interval is a member of the set.

public bool Encloses(Interval<T> interval)

Parameters

interval Interval<T>

The interval to test for enclosure.

Returns

bool

true when the set is a superset of interval; otherwise false.

Remarks

Because the pieces are disjoint and non-adjacent, an enclosed interval must lie entirely within one piece.

Equals(IntervalSet<T>)

Determines whether this set equals other - the same normalized pieces in the same order.

public bool Equals(IntervalSet<T> other)

Parameters

other IntervalSet<T>

The set to compare against.

Returns

bool

true when the two describe the same set of values; otherwise false.

Equals(object?)

Determines whether this set equals the boxed obj.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare against.

Returns

bool

true when obj is an equal IntervalSet<T>; otherwise false.

Except(IntervalSet<T>)

Returns this set with the members of other removed - the set difference.

public IntervalSet<T> Except(IntervalSet<T> other)

Parameters

other IntervalSet<T>

The set to subtract.

Returns

IntervalSet<T>

The normalized difference.

Except(Interval<T>)

Returns this set with the members of interval removed - the set difference.

public IntervalSet<T> Except(Interval<T> interval)

Parameters

interval Interval<T>

The interval to subtract.

Returns

IntervalSet<T>

The normalized difference.

Examples

IntervalSet<int>.Of(Interval<int>.Closed(1, 10))
    .Except(Interval<int>.Closed(3, 5));   // [1, 3) ∪ (5, 10]

From(IEnumerable<Interval<T>>)

Creates a set from a sequence of intervals, coalescing overlapping and adjacent pieces into a normalized form.

public static IntervalSet<T> From(IEnumerable<Interval<T>> intervals)

Parameters

intervals IEnumerable<Interval<T>>

The intervals to include; empty intervals are ignored.

Returns

IntervalSet<T>

The normalized set.

Exceptions

ArgumentNullException

intervals is null.

GetEnumerator()

Returns an enumerator over the disjoint pieces in ascending order.

public IntervalSet<T>.Enumerator GetEnumerator()

Returns

IntervalSet<T>.Enumerator

A struct enumerator.

GetHashCode()

Returns a hash code consistent with Equals(IntervalSet<T>).

public override int GetHashCode()

Returns

int

A 32-bit hash code.

Intersect(IntervalSet<T>)

Returns the intersection of this set with other - the members in both.

public IntervalSet<T> Intersect(IntervalSet<T> other)

Parameters

other IntervalSet<T>

The set to intersect with.

Returns

IntervalSet<T>

The normalized intersection.

Intersect(Interval<T>)

Returns the intersection of this set with interval - the members shared with it.

public IntervalSet<T> Intersect(Interval<T> interval)

Parameters

interval Interval<T>

The interval to intersect with.

Returns

IntervalSet<T>

The normalized intersection.

Examples

IntervalSet<int>.Of(Interval<int>.Closed(1, 5), Interval<int>.Closed(8, 12))
    .Intersect(Interval<int>.Closed(4, 10));   // [4, 5] ∪ [8, 10]

Of(params Interval<T>[])

Creates a set from the supplied intervals, coalescing overlapping and adjacent pieces into a normalized form.

public static IntervalSet<T> Of(params Interval<T>[] intervals)

Parameters

intervals Interval<T>[]

The intervals to include; empty intervals are ignored.

Returns

IntervalSet<T>

The normalized set.

Exceptions

ArgumentNullException

intervals is null.

ToString()

Returns a set-notation string representation - the pieces joined by the union symbol, or the empty-set glyph.

public override string ToString()

Returns

string

The formatted set.

Union(IntervalSet<T>)

Returns the union of this set with other.

public IntervalSet<T> Union(IntervalSet<T> other)

Parameters

other IntervalSet<T>

The set to add.

Returns

IntervalSet<T>

The normalized union.

Union(Interval<T>)

Returns the union of this set with interval.

public IntervalSet<T> Union(Interval<T> interval)

Parameters

interval Interval<T>

The interval to add.

Returns

IntervalSet<T>

The normalized union.

Examples

IntervalSet<int>.Of(Interval<int>.Closed(1, 3), Interval<int>.Closed(8, 9))
    .Union(Interval<int>.Closed(2, 5));   // [1, 5] ∪ [8, 9]

Operators

operator ==(IntervalSet<T>, IntervalSet<T>)

Determines whether two sets are equal.

public static bool operator ==(IntervalSet<T> left, IntervalSet<T> right)

Parameters

left IntervalSet<T>

The first set.

right IntervalSet<T>

The second set.

Returns

bool

true when equal; otherwise false.

operator !=(IntervalSet<T>, IntervalSet<T>)

Determines whether two sets are not equal.

public static bool operator !=(IntervalSet<T> left, IntervalSet<T> right)

Parameters

left IntervalSet<T>

The first set.

right IntervalSet<T>

The second set.

Returns

bool

true when they differ; otherwise false.

Applies to

ProductVersions
.NET8, 10