From 3f778aeef76c9018726e16c1a1bbc8adbeaaf656 Mon Sep 17 00:00:00 2001 From: Alex Yakunin Date: Mon, 23 Oct 2023 16:21:13 -0700 Subject: [PATCH] chore: improve Range --- src/Stl/Mathematics/Range.cs | 11 +++++++++++ src/Stl/Mathematics/RangeExt.Double.cs | 6 +++++- src/Stl/Mathematics/RangeExt.Float.cs | 6 +++++- src/Stl/Mathematics/RangeExt.Int.cs | 6 +++++- src/Stl/Mathematics/RangeExt.Long.cs | 6 +++++- src/Stl/Mathematics/RangeExt.Moment.cs | 6 +++++- src/Stl/Mathematics/RangeExt.TimeSpan.cs | 6 +++++- 7 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/Stl/Mathematics/Range.cs b/src/Stl/Mathematics/Range.cs index b2fb3a7f6..2d15c8f88 100644 --- a/src/Stl/Mathematics/Range.cs +++ b/src/Stl/Mathematics/Range.cs @@ -50,6 +50,7 @@ public Range(T start, T end) /// /// property value. /// property value. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Deconstruct(out T start, out T end) { start = Start; @@ -74,16 +75,23 @@ public static Range Parse(string value) /// /// Source tuple. /// A range constructed from a tuple. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator Range((T Start, T End) source) => new(source.Start, source.End); // Misc. operations + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Range Positive() + => IsNegative ? new Range(Start, Start) : this; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] public Range Normalize() => IsNegative ? -this : this; // Equality + [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Equals(Range other) { var equalityComparer = EqualityComparer.Default; @@ -103,6 +111,7 @@ public override int GetHashCode() /// Left operand. /// Right operand. /// true if two ranges are equal; otherwise, false. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(Range left, Range right) => left.Equals(right); @@ -112,9 +121,11 @@ public override int GetHashCode() /// Left operand. /// Right operand. /// true if two ranges aren't equal; otherwise, false. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator !=(Range left, Range right) => !left.Equals(right); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Range operator -(Range range) => new(range.End, range.Start); } diff --git a/src/Stl/Mathematics/RangeExt.Double.cs b/src/Stl/Mathematics/RangeExt.Double.cs index 5e58b6893..6c92dd565 100644 --- a/src/Stl/Mathematics/RangeExt.Double.cs +++ b/src/Stl/Mathematics/RangeExt.Double.cs @@ -6,13 +6,17 @@ public static bool Equals(this Range range, Range 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 range) => range.End - range.Start; + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Range Move(this Range range, double offset) => new(range.Start + offset, range.End + offset); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Range Expand(this Range range, double offset) => new(range.Start - offset, range.End + offset); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Range Resize(this Range range, double size) => new(range.Start, range.Start + size); @@ -32,6 +36,6 @@ public static Range MinMaxWith(this Range range, double point) public static Range IntersectWith(this Range range, Range other) { var result = new Range(Math.Max(range.Start, other.Start), Math.Min(range.End, other.End)); - return result.IsEmptyOrNegative ? default : result; + return result.Positive(); } } diff --git a/src/Stl/Mathematics/RangeExt.Float.cs b/src/Stl/Mathematics/RangeExt.Float.cs index 8d397716a..c750f1ade 100644 --- a/src/Stl/Mathematics/RangeExt.Float.cs +++ b/src/Stl/Mathematics/RangeExt.Float.cs @@ -6,13 +6,17 @@ public static bool Equals(this Range range, Range 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 range) => range.End - range.Start; + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Range Move(this Range range, float offset) => new(range.Start + offset, range.End + offset); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Range Expand(this Range range, float offset) => new(range.Start - offset, range.End + offset); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Range Resize(this Range range, float size) => new(range.Start, range.Start + size); @@ -32,6 +36,6 @@ public static Range MinMaxWith(this Range range, float point) public static Range IntersectWith(this Range range, Range other) { var result = new Range(Math.Max(range.Start, other.Start), Math.Min(range.End, other.End)); - return result.IsEmptyOrNegative ? default : result; + return result.Positive(); } } diff --git a/src/Stl/Mathematics/RangeExt.Int.cs b/src/Stl/Mathematics/RangeExt.Int.cs index f5cf339a8..b9dea2962 100644 --- a/src/Stl/Mathematics/RangeExt.Int.cs +++ b/src/Stl/Mathematics/RangeExt.Int.cs @@ -2,13 +2,17 @@ namespace Stl.Mathematics; public static partial class RangeExt { + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int Size(this Range range) => range.End - range.Start; + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Range Move(this Range range, int offset) => new(range.Start + offset, range.End + offset); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Range Expand(this Range range, int offset) => new(range.Start - offset, range.End + offset); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Range Resize(this Range range, int size) => new(range.Start, range.Start + size); @@ -28,6 +32,6 @@ public static Range MinMaxWith(this Range range, int point) public static Range IntersectWith(this Range range, Range other) { var result = new Range(Math.Max(range.Start, other.Start), Math.Min(range.End, other.End)); - return result.IsEmptyOrNegative ? default : result; + return result.Positive(); } } diff --git a/src/Stl/Mathematics/RangeExt.Long.cs b/src/Stl/Mathematics/RangeExt.Long.cs index ceb8b15a9..d1e2574c1 100644 --- a/src/Stl/Mathematics/RangeExt.Long.cs +++ b/src/Stl/Mathematics/RangeExt.Long.cs @@ -2,13 +2,17 @@ namespace Stl.Mathematics; public static partial class RangeExt { + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long Size(this Range range) => range.End - range.Start; + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Range Move(this Range range, long offset) => new(range.Start + offset, range.End + offset); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Range Expand(this Range range, long offset) => new(range.Start - offset, range.End + offset); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Range Resize(this Range range, long size) => new(range.Start, range.Start + size); @@ -28,6 +32,6 @@ public static Range MinMaxWith(this Range range, long point) public static Range IntersectWith(this Range range, Range other) { var result = new Range(Math.Max(range.Start, other.Start), Math.Min(range.End, other.End)); - return result.IsEmptyOrNegative ? default : result; + return result.Positive(); } } diff --git a/src/Stl/Mathematics/RangeExt.Moment.cs b/src/Stl/Mathematics/RangeExt.Moment.cs index 087e0b3e6..f1c8da79e 100644 --- a/src/Stl/Mathematics/RangeExt.Moment.cs +++ b/src/Stl/Mathematics/RangeExt.Moment.cs @@ -2,13 +2,17 @@ namespace Stl.Mathematics; public static partial class RangeExt { + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static TimeSpan Size(this Range range) => range.End - range.Start; + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Range Move(this Range range, TimeSpan offset) => new(range.Start + offset, range.End + offset); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Range Expand(this Range range, TimeSpan offset) => new(range.Start - offset, range.End + offset); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Range Resize(this Range range, TimeSpan size) => new(range.Start, range.Start + size); @@ -28,6 +32,6 @@ public static Range MinMaxWith(this Range range, Moment point) public static Range IntersectWith(this Range range, Range other) { var result = new Range(Moment.Max(range.Start, other.Start), Moment.Min(range.End, other.End)); - return result.Size() < TimeSpan.Zero ? default : result; + return result.Positive(); } } diff --git a/src/Stl/Mathematics/RangeExt.TimeSpan.cs b/src/Stl/Mathematics/RangeExt.TimeSpan.cs index 6f43e1ff1..ca9c36b12 100644 --- a/src/Stl/Mathematics/RangeExt.TimeSpan.cs +++ b/src/Stl/Mathematics/RangeExt.TimeSpan.cs @@ -2,13 +2,17 @@ namespace Stl.Mathematics; public static partial class RangeExt { + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static TimeSpan Size(this Range range) => range.End - range.Start; + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Range Move(this Range range, TimeSpan offset) => new(range.Start + offset, range.End + offset); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Range Expand(this Range range, TimeSpan offset) => new(range.Start - offset, range.End + offset); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Range Resize(this Range range, TimeSpan size) => new(range.Start, range.Start + size); @@ -28,6 +32,6 @@ public static Range MinMaxWith(this Range range, TimeSpan po public static Range IntersectWith(this Range range, Range other) { var result = new Range(TimeSpanExt.Max(range.Start, other.Start), TimeSpanExt.Min(range.End, other.End)); - return result.Size() < TimeSpan.Zero ? default : result; + return result.Positive(); } }