Skip to content

Commit

Permalink
chore: improve Range
Browse files Browse the repository at this point in the history
  • Loading branch information
alexyakunin committed Oct 23, 2023
1 parent 5f3851f commit 3f778ae
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/Stl/Mathematics/Range.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public Range(T start, T end)
/// </summary>
/// <param name="start"><see cref="Start"/> property value.</param>
/// <param name="end"><see cref="End"/> property value.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Deconstruct(out T start, out T end)
{
start = Start;
Expand All @@ -74,16 +75,23 @@ public static Range<T> Parse(string value)
/// </summary>
/// <param name="source">Source tuple.</param>
/// <returns>A range constructed from a tuple.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Range<T>((T Start, T End) source)
=> new(source.Start, source.End);

// Misc. operations

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Range<T> Positive()
=> IsNegative ? new Range<T>(Start, Start) : this;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Range<T> Normalize()
=> IsNegative ? -this : this;

// Equality

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Range<T> other)
{
var equalityComparer = EqualityComparer<T>.Default;
Expand All @@ -103,6 +111,7 @@ public override int GetHashCode()
/// <param name="left">Left operand.</param>
/// <param name="right">Right operand.</param>
/// <returns><code>true</code> if two ranges are equal; otherwise, <code>false</code>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Range<T> left, Range<T> right)
=> left.Equals(right);

Expand All @@ -112,9 +121,11 @@ public override int GetHashCode()
/// <param name="left">Left operand.</param>
/// <param name="right">Right operand.</param>
/// <returns><code>true</code> if two ranges aren't equal; otherwise, <code>false</code>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Range<T> left, Range<T> right)
=> !left.Equals(right);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Range<T> operator -(Range<T> range)
=> new(range.End, range.Start);
}
6 changes: 5 additions & 1 deletion src/Stl/Mathematics/RangeExt.Double.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ public static bool Equals(this Range<double> range, Range<double> otherRange, do
=> Math.Abs(range.Start - otherRange.Start) < epsilon
&& Math.Abs(range.End - otherRange.End) < epsilon;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Size(this Range<double> range)
=> range.End - range.Start;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Range<double> Move(this Range<double> range, double offset)
=> new(range.Start + offset, range.End + offset);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Range<double> Expand(this Range<double> range, double offset)
=> new(range.Start - offset, range.End + offset);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Range<double> Resize(this Range<double> range, double size)
=> new(range.Start, range.Start + size);

Expand All @@ -32,6 +36,6 @@ public static Range<double> MinMaxWith(this Range<double> range, double point)
public static Range<double> IntersectWith(this Range<double> range, Range<double> other)
{
var result = new Range<double>(Math.Max(range.Start, other.Start), Math.Min(range.End, other.End));
return result.IsEmptyOrNegative ? default : result;
return result.Positive();
}
}
6 changes: 5 additions & 1 deletion src/Stl/Mathematics/RangeExt.Float.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ public static bool Equals(this Range<float> range, Range<float> otherRange, floa
=> Math.Abs(range.Start - otherRange.Start) < epsilon
&& Math.Abs(range.End - otherRange.End) < epsilon;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Size(this Range<float> range)
=> range.End - range.Start;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Range<float> Move(this Range<float> range, float offset)
=> new(range.Start + offset, range.End + offset);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Range<float> Expand(this Range<float> range, float offset)
=> new(range.Start - offset, range.End + offset);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Range<float> Resize(this Range<float> range, float size)
=> new(range.Start, range.Start + size);

Expand All @@ -32,6 +36,6 @@ public static Range<float> MinMaxWith(this Range<float> range, float point)
public static Range<float> IntersectWith(this Range<float> range, Range<float> other)
{
var result = new Range<float>(Math.Max(range.Start, other.Start), Math.Min(range.End, other.End));
return result.IsEmptyOrNegative ? default : result;
return result.Positive();
}
}
6 changes: 5 additions & 1 deletion src/Stl/Mathematics/RangeExt.Int.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ namespace Stl.Mathematics;

public static partial class RangeExt
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Size(this Range<int> range)
=> range.End - range.Start;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Range<int> Move(this Range<int> range, int offset)
=> new(range.Start + offset, range.End + offset);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Range<int> Expand(this Range<int> range, int offset)
=> new(range.Start - offset, range.End + offset);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Range<int> Resize(this Range<int> range, int size)
=> new(range.Start, range.Start + size);

Expand All @@ -28,6 +32,6 @@ public static Range<int> MinMaxWith(this Range<int> range, int point)
public static Range<int> IntersectWith(this Range<int> range, Range<int> other)
{
var result = new Range<int>(Math.Max(range.Start, other.Start), Math.Min(range.End, other.End));
return result.IsEmptyOrNegative ? default : result;
return result.Positive();
}
}
6 changes: 5 additions & 1 deletion src/Stl/Mathematics/RangeExt.Long.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ namespace Stl.Mathematics;

public static partial class RangeExt
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long Size(this Range<long> range)
=> range.End - range.Start;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Range<long> Move(this Range<long> range, long offset)
=> new(range.Start + offset, range.End + offset);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Range<long> Expand(this Range<long> range, long offset)
=> new(range.Start - offset, range.End + offset);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Range<long> Resize(this Range<long> range, long size)
=> new(range.Start, range.Start + size);

Expand All @@ -28,6 +32,6 @@ public static Range<long> MinMaxWith(this Range<long> range, long point)
public static Range<long> IntersectWith(this Range<long> range, Range<long> other)
{
var result = new Range<long>(Math.Max(range.Start, other.Start), Math.Min(range.End, other.End));
return result.IsEmptyOrNegative ? default : result;
return result.Positive();
}
}
6 changes: 5 additions & 1 deletion src/Stl/Mathematics/RangeExt.Moment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ namespace Stl.Mathematics;

public static partial class RangeExt
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TimeSpan Size(this Range<Moment> range)
=> range.End - range.Start;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Range<Moment> Move(this Range<Moment> range, TimeSpan offset)
=> new(range.Start + offset, range.End + offset);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Range<Moment> Expand(this Range<Moment> range, TimeSpan offset)
=> new(range.Start - offset, range.End + offset);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Range<Moment> Resize(this Range<Moment> range, TimeSpan size)
=> new(range.Start, range.Start + size);

Expand All @@ -28,6 +32,6 @@ public static Range<Moment> MinMaxWith(this Range<Moment> range, Moment point)
public static Range<Moment> IntersectWith(this Range<Moment> range, Range<Moment> other)
{
var result = new Range<Moment>(Moment.Max(range.Start, other.Start), Moment.Min(range.End, other.End));
return result.Size() < TimeSpan.Zero ? default : result;
return result.Positive();
}
}
6 changes: 5 additions & 1 deletion src/Stl/Mathematics/RangeExt.TimeSpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ namespace Stl.Mathematics;

public static partial class RangeExt
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TimeSpan Size(this Range<TimeSpan> range)
=> range.End - range.Start;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Range<TimeSpan> Move(this Range<TimeSpan> range, TimeSpan offset)
=> new(range.Start + offset, range.End + offset);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Range<TimeSpan> Expand(this Range<TimeSpan> range, TimeSpan offset)
=> new(range.Start - offset, range.End + offset);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Range<TimeSpan> Resize(this Range<TimeSpan> range, TimeSpan size)
=> new(range.Start, range.Start + size);

Expand All @@ -28,6 +32,6 @@ public static Range<TimeSpan> MinMaxWith(this Range<TimeSpan> range, TimeSpan po
public static Range<TimeSpan> IntersectWith(this Range<TimeSpan> range, Range<TimeSpan> other)
{
var result = new Range<TimeSpan>(TimeSpanExt.Max(range.Start, other.Start), TimeSpanExt.Min(range.End, other.End));
return result.Size() < TimeSpan.Zero ? default : result;
return result.Positive();
}
}

0 comments on commit 3f778ae

Please sign in to comment.