Table of Contents

IntervalPair<T> Struct

Definition

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

Represents the result of a binary interval set operation as zero, one, or two disjoint intervals in ascending order - the maximum number of pieces that subtracting or symmetric-differencing two intervals can produce.

public readonly struct IntervalPair<T> where T : INumber<T>

Type Parameters

T

The numeric type used for the intervals' endpoints.

Inherited Members
Extension Methods

Examples

IntervalPair<int> diff = Interval<int>.Closed(0, 10).Difference(Interval<int>.Closed(3, 5));
diff.Count;          // 2
foreach (var piece in diff)
    Console.WriteLine(piece);   // "[0, 3)" then "(5, 10]"

Remarks

Difference(Interval<T>) and SymmetricDifference(Interval<T>) return this type. Because removing one interval from another leaves at most a left and a right remainder, the result never needs more than two pieces, so this allocation-free value type stores them inline rather than in a heap collection. When fewer than two pieces are present the surplus slots are the empty interval and are not enumerated.

The pieces are ordered lowest-first and are guaranteed disjoint and non-adjacent. Enumerate with a foreach loop, index them via this[int] (0-based, bounded by Count), or read First and Second directly.

Properties

Count

Gets the number of disjoint intervals in the result (0, 1, or 2).

public int Count { get; }

Property Value

int

The count of non-empty pieces.

Empty

Gets the empty result - zero pieces.

public static IntervalPair<T> Empty { get; }

Property Value

IntervalPair<T>

An IntervalPair<T> whose Count is zero.

First

Gets the first (lower) piece, or Empty when the result is empty.

public Interval<T> First { get; }

Property Value

Interval<T>

The lowest interval in the result.

IsEmpty

Gets a value indicating whether the result contains no intervals.

public bool IsEmpty { get; }

Property Value

bool

true when Count is zero; 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 negative or not less than Count.

Second

Gets the second (upper) piece, or Empty when the result has fewer than two pieces.

public Interval<T> Second { get; }

Property Value

Interval<T>

The higher interval in the result.

Methods

GetEnumerator()

Returns an enumerator that iterates the non-empty pieces in ascending order.

public IntervalPair<T>.Enumerator GetEnumerator()

Returns

IntervalPair<T>.Enumerator

A struct enumerator over the pieces.

ToIntervalSet()

Returns an IntervalSet<T> containing the pieces of this result.

public IntervalSet<T> ToIntervalSet()

Returns

IntervalSet<T>

The equivalent normalized set - empty, or the one or two disjoint pieces this pair holds.

Remarks

IntervalPair<T> is the allocation-free result of a binary Difference(Interval<T>) or SymmetricDifference(Interval<T>) and holds at most two pieces; IntervalSet<T> is the general disconnected-range model. Lift a transient pair into a set when the result must compose with further N-ary set algebra or be persisted. Empty slots are dropped, so the result has Count pieces.

ToString()

Returns a set-notation string representation of the result - the empty-set glyph when empty, otherwise the pieces joined by the union symbol.

public override string ToString()

Returns

string

The formatted result.

Applies to

ProductVersions
.NET8, 10