Table of Contents

DiscreteIntervalPair<T> Struct

Definition

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

Represents the result of a binary DiscreteInterval<T> set operation as zero, one, or two disjoint, non-adjacent intervals in ascending order - the discrete counterpart to IntervalPair<T>.

public readonly struct DiscreteIntervalPair<T> where T : IBinaryInteger<T>

Type Parameters

T

The integer type used for the intervals' endpoints.

Inherited Members
Extension Methods

Examples

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

Remarks

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

The runs 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 DiscreteIntervalPair<T> Empty { get; }

Property Value

DiscreteIntervalPair<T>

A DiscreteIntervalPair<T> whose Count is zero.

First

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

public DiscreteInterval<T> First { get; }

Property Value

DiscreteInterval<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 DiscreteInterval<T> this[int index] { get; }

Parameters

index int

The zero-based index, less than Count.

Property Value

DiscreteInterval<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 DiscreteInterval<T> Second { get; }

Property Value

DiscreteInterval<T>

The higher interval in the result.

Methods

GetEnumerator()

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

public DiscreteIntervalPair<T>.Enumerator GetEnumerator()

Returns

DiscreteIntervalPair<T>.Enumerator

A struct enumerator over the pieces.

ToIntervalSet()

Returns an IntervalSet<T> covering the same integers as the pieces of this result.

public IntervalSet<T> ToIntervalSet()

Returns

IntervalSet<T>

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

Remarks

There is deliberately no DiscreteIntervalSet<T>; a discrete result is lifted into the continuous IntervalSet<T> through each run's ToInterval() so it composes with the single N-ary set model. The continuous pieces cover the same integers plus the coordinates between them, so use this as a set of integer runs rather than for continuous membership tests. Empty slots are dropped.

ToString()

Returns a set-notation string representation of the result.

public override string ToString()

Returns

string

The formatted result.

Applies to

ProductVersions
.NET8, 10