Class Ranges<T>
Represents a set of intervals (ranges) for an ordered type T.
Provides set-like operations (union, intersection, difference, symmetric difference)
via operators:
| => Union,
& => Intersection,
^ => Symmetric Difference,
- => Difference (Except).
There is deliberately no complement operation: T is only constrained by
IComparable<T>, and IRange<T> carries no notion of a domain, a
minimum/maximum value, or infinite bounds, so a complement relative to "the entire domain" cannot
be computed in general (an empty Ranges<T> would not even have a domain to recover
it from). Types that explicitly model their complete domain, such as IntRange<T>,
can define their own well-defined complement instead.
The intervals are stored internally as disjoint, sorted Range<T> objects.
public class Ranges<T> : IFormattable, IEquatable<Ranges<T>>, ISubtractionOperators<Ranges<T>, Ranges<T>, Ranges<T>>, IEqualityOperators<Ranges<T>, Ranges<T>, bool> where T : IComparable<T>
Type Parameters
TA comparable type that supports ordering.
- Inheritance
-
Ranges<T>
- Implements
-
IEquatable<Ranges<T>>
- Derived
- Inherited Members
- Extension Methods
Constructors
Ranges()
Creates an empty set of intervals.
public Ranges()
Ranges(IEnumerable<IRange<T>>)
Creates a set of intervals from the provided collection of Range<T>. Overlapping intervals are merged into disjoint intervals.
public Ranges(IEnumerable<IRange<T>> intervals)
Parameters
intervalsIEnumerable<IRange<T>>
Ranges(IEnumerable<Range<T>>)
Creates a set of intervals from the provided collection of Range<T>. Overlapping intervals are merged into disjoint intervals.
public Ranges(IEnumerable<Range<T>> intervals)
Parameters
intervalsIEnumerable<Range<T>>
Ranges(params IRange<T>[])
Creates a set of intervals from one or more Range<T>. Overlapping intervals are merged into disjoint intervals.
public Ranges(params IRange<T>[] intervals)
Parameters
intervalsIRange<T>[]
Ranges(Ranges<T>)
Creates a new set of intervals by copying all intervals from other.
public Ranges(Ranges<T> other)
Parameters
otherRanges<T>
Ranges(params Range<T>[])
Creates a set of intervals from one or more Range<T>. Overlapping intervals are merged into disjoint intervals.
public Ranges(params Range<T>[] intervals)
Parameters
intervalsRange<T>[]
Properties
Count
Returns the total count of disjoint intervals stored.
public int Count { get; }
Property Value
Intervals
Returns a read-only snapshot of the underlying intervals (disjoint, sorted).
public IReadOnlyList<IRange<T>> Intervals { get; }
Property Value
- IReadOnlyList<IRange<T>>
Methods
Add(IRange<T>)
Adds a single interval, merging with existing intervals if they overlap or touch.
public void Add(IRange<T> interval)
Parameters
intervalIRange<T>
Add(T)
Adds a single interval, merging with existing intervals if they overlap or touch.
public void Add(T singleValue)
Parameters
singleValueT
Add(T, T, bool, bool)
Adds a single interval, merging with existing intervals if they overlap or touch.
public void Add(T start, T end, bool includeStart = true, bool includeEnd = true)
Parameters
AddAll(IEnumerable<IRange<T>>)
Adds multiple intervals, merging overlaps.
public void AddAll(IEnumerable<IRange<T>> intervals)
Parameters
intervalsIEnumerable<IRange<T>>
Clear()
Removes all intervals, resulting in an empty set.
public void Clear()
Contains(IRange<T>)
Checks if the entire range is contained in this set.
public bool Contains(IRange<T> range)
Parameters
rangeIRange<T>
Returns
Contains(T)
Checks if the specified value value is contained
in any of the intervals of this set.
public bool Contains(T value)
Parameters
valueT
Returns
Equals(object?)
Determines whether the specified object is equal to the current object.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current object.
Returns
Equals(Ranges<T>?)
Determines whether the specified Ranges<T> represents the same set of intervals.
public bool Equals(Ranges<T>? other)
Parameters
otherRanges<T>The other range collection to compare with.
Returns
Except(Ranges<T>, Ranges<T>)
Returns a new set representing all intervals in left that are not in right.
(Set difference).
public static Ranges<T> Except(Ranges<T> left, Ranges<T> right)
Parameters
Returns
- Ranges<T>
GetHashCode()
Serves as the default hash function.
public override int GetHashCode()
Returns
- int
A hash code for the current object.
InnerParse<T1>(string, string, IEnumerable<string>, Func<string, T1>)
protected static IEnumerable<IRange<T1>> InnerParse<T1>(string range, string itemSearchPattern, IEnumerable<string> separators, Func<string, T1> valueParser) where T1 : IComparable<T1>
Parameters
rangestringThe string containing the ranges.
itemSearchPatternstringThe regex pattern to match the elements in the range.
separatorsIEnumerable<string>The separator strings used to delimit the start and end values.
valueParserFunc<string, T1>A function to parse the string into type T1.
Returns
- IEnumerable<IRange<T1>>
An enumerable collection of parsed Range objects. An empty or whitespace-only
rangeis a legitimate representation of an empty set and yields no ranges without throwing.
Type Parameters
T1The type of the elements in the range.
Exceptions
- FormatException
Thrown when
rangecontains non-whitespace content that is not part of a recognized range expression (only whitespace is allowed between, before, or after matched ranges). This is a strict parser: it requires the entire input to be consumed, unlike an extraction API that would intentionally skip over surrounding text.
Intersect(Ranges<T>, Ranges<T>)
Returns a new set representing the intersection of left and right.
public static Ranges<T> Intersect(Ranges<T> left, Ranges<T> right)
Parameters
Returns
- Ranges<T>
Remove(IRange<T>)
Removes a single interval from this set, splitting or trimming intervals.
public void Remove(IRange<T> toRemove)
Parameters
toRemoveIRange<T>
Remove(T)
Removes a single interval from this set, splitting or trimming intervals.
public void Remove(T singleValue)
Parameters
singleValueT
Remove(T, T, bool, bool)
Removes a single interval from this set, splitting or trimming intervals.
public void Remove(T start, T end, bool includeStart = true, bool includeEnd = true)
Parameters
RemoveAll(IEnumerable<IRange<T>>)
Removes multiple intervals from this set.
public void RemoveAll(IEnumerable<IRange<T>> intervals)
Parameters
intervalsIEnumerable<IRange<T>>
SymmetricDifference(Ranges<T>, Ranges<T>)
Returns a new set representing the symmetric difference of left and right.
(Elements in left or right, but not both).
public static Ranges<T> SymmetricDifference(Ranges<T> left, Ranges<T> right)
Parameters
Returns
- Ranges<T>
ToString()
Converts the current Ranges<T> to its string representation using default formatting.
public override string ToString()
Returns
- string
A string representation of the stored ranges.
ToString(IFormatProvider?)
Converts the current Ranges<T> to its string representation using the provided format provider.
public string ToString(IFormatProvider? formatProvider)
Parameters
formatProviderIFormatProviderCulture used when formatting the numeric boundaries.
Returns
- string
A string representation of the stored ranges.
ToString(string?)
Converts the current Ranges<T> to its string representation using the provided format string.
public string ToString(string? format)
Parameters
formatstringCustom format applied to each interval boundary.
Returns
- string
A string representation of the stored ranges.
ToString(string?, IFormatProvider?)
Converts all stored intervals to string, joined by a union symbol " ∪ ". E.g. "[1..2] ∪ (3..5)"
public string ToString(string? format, IFormatProvider? formatProvider)
Parameters
formatstringformatProviderIFormatProvider
Returns
Union(Ranges<T>, Ranges<T>)
Returns a new set representing the union of left and right.
public static Ranges<T> Union(Ranges<T> left, Ranges<T> right)
Parameters
Returns
- Ranges<T>
Operators
operator &(Ranges<T>, Ranges<T>)
Intersection => bitwise AND operator.
public static Ranges<T> operator &(Ranges<T> left, Ranges<T> right)
Parameters
Returns
- Ranges<T>
operator |(Ranges<T>, Ranges<T>)
Union => bitwise OR operator.
public static Ranges<T> operator |(Ranges<T> left, Ranges<T> right)
Parameters
Returns
- Ranges<T>
operator ==(Ranges<T>?, Ranges<T>?)
Determines whether two Ranges<T> instances are equal.
public static bool operator ==(Ranges<T>? left, Ranges<T>? right)
Parameters
Returns
operator ^(Ranges<T>, Ranges<T>)
Symmetric Difference => bitwise XOR operator.
public static Ranges<T> operator ^(Ranges<T> left, Ranges<T> right)
Parameters
Returns
- Ranges<T>
operator !=(Ranges<T>?, Ranges<T>?)
Determines whether two Ranges<T> instances are not equal.
public static bool operator !=(Ranges<T>? left, Ranges<T>? right)
Parameters
Returns
operator -(Ranges<T>, Ranges<T>)
Difference => set except operator.
public static Ranges<T> operator -(Ranges<T> left, Ranges<T> right)
Parameters
Returns
- Ranges<T>