Table of Contents

Class IntRange<T>

Namespace
Utils.Range
Assembly
Utils.dll

Represents a collection of integer intervals (ranges), each of which may include negative or positive infinity as an endpoint.

Bitwise-like operators for set operations: | (Union), & (Intersection), ^ (Symmetric Difference), ~ (Complement).

Internally stores multiple non-overlapping intervals sorted by their (Min, Max). A null Minimum => -∞. A null Maximum => +∞.

public sealed class IntRange<T> : IEnumerable<T>, IEnumerable, IBitwiseOperators<IntRange<T>, IntRange<T>, IntRange<T>>, ISubtractionOperators<IntRange<T>, IntRange<T>, IntRange<T>>, IFormattable where T : struct, IBinaryInteger<T>, IComparable<T>, IMinMaxValue<T>

Type Parameters

T
Inheritance
IntRange<T>
Implements
Inherited Members
Extension Methods

Constructors

IntRange()

Creates an empty set of intervals.

public IntRange()

IntRange(string, CultureInfo)

Creates a set of intervals from a string. Expects forms like: "∞-∞" => all integers "∞-5" => [-∞..5] "5-∞" => [5..+∞] "2-5" => [2..5] "42" => single value [42..42] And possibly multiple intervals separated by commas or semicolons, e.g. "∞-0, 5-10, 42".

public IntRange(string input, CultureInfo cultureInfo)

Parameters

input string
cultureInfo CultureInfo

IntRange(string, TextInfo)

Creates a set of intervals from a string. Expects forms like: "∞-∞" => all integers "∞-5" => [-∞..5] "5-∞" => [5..+∞] "2-5" => [2..5] "42" => single value [42..42] And possibly multiple intervals separated by commas or semicolons, e.g. "∞-0, 5-10, 42".

public IntRange(string input, TextInfo textInfo)

Parameters

input string
textInfo TextInfo

IntRange(string, params string[])

Creates a set of intervals from a string. Expects forms like: "∞-∞" => all integers "∞-5" => [-∞..5] "5-∞" => [5..+∞] "2-5" => [2..5] "42" => single value [42..42] And possibly multiple intervals separated by commas or semicolons, e.g. "∞-0, 5-10, 42".

public IntRange(string input, params string[] separators)

Parameters

input string
separators string[]

Properties

FullRange

Returns an IntRange representing all integers from -∞ to +∞. (A single interval with null-min, null-max).

public static IntRange<T> FullRange { get; }

Property Value

IntRange<T>

Methods

Complement()

Computes the complement with respect to the full integer domain (-∞ to +∞). That is, all values in [-∞..+∞] that are not in this IntRange<T>.

public IntRange<T> Complement()

Returns

IntRange<T>

Contains(BigInteger)

Checks whether a BigInteger value is contained in any of the intervals. Converts each finite bound to BigInteger for an exact comparison, so values outside the range of T (e.g. above MaxValue) are evaluated correctly against open or closed bounds.

public bool Contains(BigInteger value)

Parameters

value BigInteger

Returns

bool

Contains(T)

Checks whether an integer value is contained in any of the intervals.

public bool Contains(T value)

Parameters

value T

Returns

bool

Except(IntRange<T>, IntRange<T>)

Produces the difference of two sets: all values in left that are not in right.

public static IntRange<T> Except(IntRange<T> left, IntRange<T> right)

Parameters

left IntRange<T>
right IntRange<T>

Returns

IntRange<T>

GetEnumerator()

Enumerates all integers in the stored intervals. If you have intervals that go to ±∞, enumerating them is infinite! For safety, you might want to throw if the range is unbounded.

public IEnumerator<T> GetEnumerator()

Returns

IEnumerator<T>

Intersect(IntRange<T>, IntRange<T>)

Produces the intersection of two IntRange sets (&).

public static IntRange<T> Intersect(IntRange<T> left, IntRange<T> right)

Parameters

left IntRange<T>
right IntRange<T>

Returns

IntRange<T>

SymmetricDifference(IntRange<T>, IntRange<T>)

Symmetric difference => (A \ B) ∪ (B \ A).

public static IntRange<T> SymmetricDifference(IntRange<T> left, IntRange<T> right)

Parameters

left IntRange<T>
right IntRange<T>

Returns

IntRange<T>

ToString()

Converts the current IntRange<T> to its string representation using default formatting.

public override string ToString()

Returns

string

A string that represents the current range.

ToString(IFormatProvider)

Converts the current IntRange<T> to its string representation using the provided format provider.

public string ToString(IFormatProvider formatProvider)

Parameters

formatProvider IFormatProvider

Culture used when formatting the numeric boundaries.

Returns

string

A string that represents the current range.

ToString(string?)

Converts the current IntRange<T> to its string representation using the provided format string.

public string ToString(string? format)

Parameters

format string

Custom format applied to the numeric boundaries.

Returns

string

A string that represents the current range.

ToString(string?, IFormatProvider?)

Joins each range with the current culture's list separator (or the formatProvider's culture). Each range is printed as "∞-∞", "∞-5", "5-∞", etc.

public string? ToString(string? format, IFormatProvider? formatProvider)

Parameters

format string
formatProvider IFormatProvider

Returns

string

Union(IntRange<T>, IntRange<T>)

Produces the union of two IntRange sets (|).

public static IntRange<T> Union(IntRange<T> left, IntRange<T> right)

Parameters

left IntRange<T>
right IntRange<T>

Returns

IntRange<T>

Operators

operator &(IntRange<T>, IntRange<T>)

Bitwise AND => Intersection

public static IntRange<T> operator &(IntRange<T> left, IntRange<T> right)

Parameters

left IntRange<T>
right IntRange<T>

Returns

IntRange<T>

operator |(IntRange<T>, IntRange<T>)

Bitwise OR => Union

public static IntRange<T> operator |(IntRange<T> left, IntRange<T> right)

Parameters

left IntRange<T>
right IntRange<T>

Returns

IntRange<T>

operator ^(IntRange<T>, IntRange<T>)

Bitwise XOR => Symmetric Difference

public static IntRange<T> operator ^(IntRange<T> left, IntRange<T> right)

Parameters

left IntRange<T>
right IntRange<T>

Returns

IntRange<T>

operator ~(IntRange<T>)

Bitwise NOT => Complement (everything in [-∞..+∞] not in this range).

public static IntRange<T> operator ~(IntRange<T> range)

Parameters

range IntRange<T>

Returns

IntRange<T>

operator -(IntRange<T>, IntRange<T>)

Substraction => Except

public static IntRange<T> operator -(IntRange<T> left, IntRange<T> right)

Parameters

left IntRange<T>
right IntRange<T>

Returns

IntRange<T>