IntervalSet<T> Struct
Definition
- Assembly
- Bodu.Numerics.dll
- Package
- Bodu.Numerics 1.0.0
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
TThe 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
this[int]
Gets the piece at index, ordered lowest-first.
public Interval<T> this[int index] { get; }
Parameters
Property Value
- Interval<T>
The interval at the requested position.
Exceptions
- ArgumentOutOfRangeException
indexis 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
valueTThe value to test.
Returns
Encloses(Interval<T>)
Determines whether every value of interval is a member of the set.
public bool Encloses(Interval<T> interval)
Parameters
intervalInterval<T>The interval to test for enclosure.
Returns
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
otherIntervalSet<T>The set to compare against.
Returns
Equals(object?)
Determines whether this set equals the boxed obj.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare against.
Returns
- bool
true when
objis 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
otherIntervalSet<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
intervalInterval<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
intervalsIEnumerable<Interval<T>>The intervals to include; empty intervals are ignored.
Returns
- IntervalSet<T>
The normalized set.
Exceptions
- ArgumentNullException
intervalsis 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
otherIntervalSet<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
intervalInterval<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
intervalsInterval<T>[]The intervals to include; empty intervals are ignored.
Returns
- IntervalSet<T>
The normalized set.
Exceptions
- ArgumentNullException
intervalsis 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
otherIntervalSet<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
intervalInterval<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
leftIntervalSet<T>The first set.
rightIntervalSet<T>The second set.
Returns
operator !=(IntervalSet<T>, IntervalSet<T>)
Determines whether two sets are not equal.
public static bool operator !=(IntervalSet<T> left, IntervalSet<T> right)
Parameters
leftIntervalSet<T>The first set.
rightIntervalSet<T>The second set.
Returns
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |