SystemEX  Lacking
Additional generic collection types missing in .net
SystemEx.NumberRange< T > Class Template Reference

Represents a numeric range defined by a start and end value. Provides range validation, containment checks, normalization, intersection, adjacency, union, and enumeration. More...

Inheritance diagram for SystemEx.NumberRange< T >:
Collaboration diagram for SystemEx.NumberRange< T >:

Public Member Functions

 NumberRange ()
 Creates a new numeric range trom T.Zero to T.One.
 NumberRange (T start, T end)
 Creates a new numeric range with the specified start and end values.
override int GetHashCode ()
override bool Equals (object? obj)
bool Equals (NumberRange< T >? other)
 Determines equality based on start and end values.
bool Contains (T x)
 Determines whether the specified value lies within the range. Works for both ascending and descending ranges.
NumberRange< T > Normalize ()
 Returns a normalized version of the range where From ≤ To.
IRange< T > GetRange (T? tstart, T? tend)
 Creates a new range using optional override values for start and end.
IEnumerator< T > GetEnumerator ()
 Enumerates all values from From to To in increments of 1.
bool Overlaps (IRange< T > other)
 Determines whether this range overlaps with another range.
IRange< T >? Intersect (IRange< T > other)
 Computes the intersection of this range with another range.
bool IsAdjacent (IRange< T > other)
 Determines whether this range touches another range at exactly one boundary.
IRange< T >? Union (IRange< T > other)
 Computes the union of this range with another range if they overlap or are adjacent. Otherwise returns null.
override string ToString ()
 Returns a string representation of the range.

Properties

From [get, set]
 From value of the range.
To [get, set]
 End value of the range.
Length [get]
 Gets the length of the range (To - From).
bool IsValid [get]
 Indicates whether the range is valid (From ≤ To).
bool IsSame [get]
 Indicates whether the range represents a single value (From == To).
IForwardIterator< T > Begin [get]
 Returns an iterator positioned at the start of the range.
IForwardIterator< T > End [get]
 Returns an iterator positioned past the end of the range.

Detailed Description

Represents a numeric range defined by a start and end value. Provides range validation, containment checks, normalization, intersection, adjacency, union, and enumeration.

Template Parameters
TNumeric type implementing INumber<T>.
Type Constraints
T :INumber<T> 

Member Function Documentation

◆ Contains()

bool SystemEx.NumberRange< T >.Contains ( T x)

Determines whether the specified value lies within the range. Works for both ascending and descending ranges.

Parameters
xValue to test.
Returns
True if the value is inside the range.

Implements SystemEx.IRange< T >.

98=> (x - this.To) * (x - this.From) <= T.Zero;

◆ Equals() [1/2]

bool SystemEx.NumberRange< T >.Equals ( NumberRange< T >? other)

Determines equality based on start and end values.

Parameters
otherRange to compare with.
Returns
True if both ranges are equal.
87 =>
88 other is not null &&
89 From.Equals(other.From) &&
90 this.To.Equals(other.To);
Here is the call graph for this function:

◆ Equals() [2/2]

override bool SystemEx.NumberRange< T >.Equals ( object? obj)

79 =>
80 Equals(obj as NumberRange<T>);
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetEnumerator()

IEnumerator< T > SystemEx.NumberRange< T >.GetEnumerator ( )

Enumerates all values from From to To in increments of 1.

Returns
Sequence of values in the range.
126 {
127 for ( T i = From; i <= this.To; i += T.One )
128 yield return i;
129 }

◆ GetHashCode()

override int SystemEx.NumberRange< T >.GetHashCode ( )

75 =>
76 HashCode.Combine(this.From, this.To);

◆ GetRange()

IRange< T > SystemEx.NumberRange< T >.GetRange ( T? tstart,
T? tend )

Creates a new range using optional override values for start and end.

Parameters
tstartOptional new start value.
tendOptional new end value.
Returns
A new range with substituted boundaries.

Implements SystemEx.IRange< T >.

117 {
118 return new NumberRange<T>((tstart != null) ? tstart : this.From,
119 (tend != null) ? tend : this.To);
120 }
Here is the call graph for this function:

◆ Intersect()

IRange< T >? SystemEx.NumberRange< T >.Intersect ( IRange< T > other)

Computes the intersection of this range with another range.

Parameters
otherRange to intersect with.
Returns
A new range representing the intersection, or null if no overlap exists.

Implements SystemEx.IRange< T >.

152 {
153 NumberRange<T>? _ret = null;
154
155 if ( Overlaps(other) ) {
156 _ret = new NumberRange<T>(T.Max(this.From, other.From),
157 T.Min(this.To, other.To));
158 }
159
160 return _ret;
161 }
Here is the call graph for this function:

◆ IsAdjacent()

bool SystemEx.NumberRange< T >.IsAdjacent ( IRange< T > other)

Determines whether this range touches another range at exactly one boundary.

Parameters
otherRange to test.
Returns
True if the ranges are adjacent.

Implements SystemEx.IRange< T >.

168 {
169 bool result = false;
170
171 if ( IsValid && other.IsValid )
172 result =
173 this.To == other.From ||
174 other.To == this.From;
175
176 return result;
177 }
Here is the caller graph for this function:

◆ Normalize()

NumberRange< T > SystemEx.NumberRange< T >.Normalize ( )

Returns a normalized version of the range where From ≤ To.

Returns
A normalized range.
104 =>
105 (this.From <= this.To) ? this : new NumberRange<T>(this.To, this.From);
Here is the call graph for this function:
Here is the caller graph for this function:

◆ NumberRange() [1/2]

SystemEx.NumberRange< T >.NumberRange ( )

Creates a new numeric range trom T.Zero to T.One.

40 {
41 this.From = T.Zero;
42 this.To = T.One;
43 }
Here is the caller graph for this function:

◆ NumberRange() [2/2]

SystemEx.NumberRange< T >.NumberRange ( T start,
T end )

Creates a new numeric range with the specified start and end values.

Parameters
startFrom value.
endTo value.
49 {
50 this.From = start;
51 this.To = end;
52 }

◆ Overlaps()

bool SystemEx.NumberRange< T >.Overlaps ( IRange< T > other)

Determines whether this range overlaps with another range.

Parameters
otherRange to test.
Returns
True if the ranges overlap.

Implements SystemEx.IRange< T >.

136 {
137 bool result = false;
138
139 if ( IsValid && other.IsValid )
140 result = !(other.To < From || other.From > this.To);
141
142 return result;
143 }
Here is the caller graph for this function:

◆ ToString()

override string SystemEx.NumberRange< T >.ToString ( )

Returns a string representation of the range.

200 {
201 return (IsValid) ?
202 string.Create(null, stackalloc char[256], $"Range: [{this.From} ... $[{this.To}]") :
203 string.Create(null, stackalloc char[256], $"Not Valid Range: [{this.From} ... $[{this.To}]");
204 }

◆ Union()

IRange< T >? SystemEx.NumberRange< T >.Union ( IRange< T > other)

Computes the union of this range with another range if they overlap or are adjacent. Otherwise returns null.

Parameters
otherRange to merge with.
Returns
The merged range, or null if no merge is possible.

Implements SystemEx.IRange< T >.

185 {
186 IRange<T>? result = null;
187
188 if ( Overlaps(other) || IsAdjacent(other) ) {
189 T start = T.Min(this.From, other.From);
190 T end = T.Max(this.To, other.To);
191 result = new NumberRange<T>(start, end);
192 }
193
194 return result;
195 }
Here is the call graph for this function:

Property Documentation

◆ Begin

IForwardIterator<T> SystemEx.NumberRange< T >.Begin
get

Returns an iterator positioned at the start of the range.

Implements SystemEx.IRange< T >.

◆ End

IForwardIterator<T> SystemEx.NumberRange< T >.End
get

Returns an iterator positioned past the end of the range.

Implements SystemEx.IRange< T >.

◆ From

T SystemEx.NumberRange< T >.From
getset

From value of the range.

Implements SystemEx.IRange< T >.

33{ get; set; }

◆ IsSame

bool SystemEx.NumberRange< T >.IsSame
get

Indicates whether the range represents a single value (From == To).

Implements SystemEx.IRange< T >.

◆ IsValid

bool SystemEx.NumberRange< T >.IsValid
get

Indicates whether the range is valid (From ≤ To).

Implements SystemEx.IRange< T >.

◆ Length

T SystemEx.NumberRange< T >.Length
get

Gets the length of the range (To - From).

◆ To

T SystemEx.NumberRange< T >.To
getset

End value of the range.

Implements SystemEx.IRange< T >.

35{ get; set; }

The documentation for this class was generated from the following file:
  • NumberRange.cs