1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-23 00:18:47 +00:00

Add Wrap for built-in numeric types (#60)

This commit is contained in:
Oliver Booth 2023-02-27 17:18:44 +00:00
parent d1959f4ba6
commit db2def1eab
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
12 changed files with 391 additions and 1 deletions

View File

@ -26,6 +26,7 @@
- X10D: Added `IList<T>.Swap(IList<T>)` (#62)
- X10D: Added `IReadOnlyList<T>.IndexOf(T[, int[, int]])`
- X10D: Added `IReadOnlyList<T>.Slice(int[, int]])`
- X10D: Added `Wrap(T[, T])` for built-in numeric types (#60)
- X10D: Added `Nullable<T>.TryGetValue(out T)` (#61)
- X10D: Added `Point.IsOnLine(LineF)`, `Point.IsOnLine(PointF, PointF)`, and `Point.IsOnLine(Vector2, Vector2)`
- X10D: Added `PointF.IsOnLine(LineF)`, `PointF.IsOnLine(PointF, PointF)`, and `PointF.IsOnLine(Vector2, Vector2)`

View File

@ -143,4 +143,39 @@ public static class ByteExtensions
{
return ((long)value).MultiplicativePersistence();
}
/// <summary>
/// Wraps the current 8-bit unsigned integer between a low and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="low">The inclusive lower bound.</param>
/// <param name="high">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static byte Wrap(this byte value, byte low, byte high)
{
return (byte)((ulong)value).Wrap(low, high);
}
/// <summary>
/// Wraps the current 8-bit unsigned integer between 0 and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="length">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static byte Wrap(this byte value, byte length)
{
return (byte)((ulong)value).Wrap(length);
}
}

View File

@ -1,4 +1,4 @@
using System.Diagnostics.Contracts;
using System.Diagnostics.Contracts;
using System.Numerics;
using System.Runtime.CompilerServices;
@ -191,4 +191,40 @@ public static class DecimalExtensions
return current;
}
/// <summary>
/// Wraps the current decimal number between a low and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="low">The inclusive lower bound.</param>
/// <param name="high">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static decimal Wrap(this decimal value, decimal low, decimal high)
{
decimal difference = high - low;
return low + (((value - low) % difference) + difference) % difference;
}
/// <summary>
/// Wraps the current decimal number between 0 and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="length">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static decimal Wrap(this decimal value, decimal length)
{
return ((value % length) + length) % length;
}
}

View File

@ -494,4 +494,40 @@ public static class DoubleExtensions
{
return System.Math.Tanh(value);
}
/// <summary>
/// Wraps the current double-precision floating-point number between a low and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="low">The inclusive lower bound.</param>
/// <param name="high">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double Wrap(this double value, double low, double high)
{
double difference = high - low;
return low + (((value - low) % difference) + difference) % difference;
}
/// <summary>
/// Wraps the current double-precision floating-point number between 0 and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="length">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double Wrap(this double value, double length)
{
return ((value % length) + length) % length;
}
}

View File

@ -217,4 +217,39 @@ public static class Int16Extensions
{
return System.Math.Sign(value);
}
/// <summary>
/// Wraps the current 16-bit signed integer between a low and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="low">The inclusive lower bound.</param>
/// <param name="high">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static short Wrap(this short value, short low, short high)
{
return (short)((long)value).Wrap(low, high);
}
/// <summary>
/// Wraps the current 16-bit signed integer between 0 and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="length">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static short Wrap(this short value, short length)
{
return (short)((long)value).Wrap(length);
}
}

View File

@ -217,4 +217,39 @@ public static class Int32Extensions
{
return System.Math.Sign(value);
}
/// <summary>
/// Wraps the current 32-bit signed integer between a low and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="low">The inclusive lower bound.</param>
/// <param name="high">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int Wrap(this int value, int low, int high)
{
return (int)((long)value).Wrap(low, high);
}
/// <summary>
/// Wraps the current 32-bit signed integer between 0 and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="length">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int Wrap(this int value, int length)
{
return (int)((long)value).Wrap(length);
}
}

View File

@ -277,4 +277,40 @@ public static class Int64Extensions
{
return System.Math.Sign(value);
}
/// <summary>
/// Wraps the current 64-bit signed integer between a low and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="low">The inclusive lower bound.</param>
/// <param name="high">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static long Wrap(this long value, long low, long high)
{
long difference = high - low;
return low + (((value - low) % difference) + difference) % difference;
}
/// <summary>
/// Wraps the current 64-bit signed integer between 0 and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="length">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static long Wrap(this long value, long length)
{
return ((value % length) + length) % length;
}
}

View File

@ -218,4 +218,39 @@ public static class SByteExtensions
{
return System.Math.Sign(value);
}
/// <summary>
/// Wraps the current 8-bit signed integer between a low and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="low">The inclusive lower bound.</param>
/// <param name="high">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static sbyte Wrap(this sbyte value, sbyte low, sbyte high)
{
return (sbyte)((long)value).Wrap(low, high);
}
/// <summary>
/// Wraps the current 8-bit signed integer between 0 and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="length">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static sbyte Wrap(this sbyte value, sbyte length)
{
return (sbyte)((long)value).Wrap(length);
}
}

View File

@ -493,4 +493,39 @@ public static class SingleExtensions
{
return MathF.Tanh(value);
}
/// <summary>
/// Wraps the current single-precision floating-point number between a low and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="low">The inclusive lower bound.</param>
/// <param name="high">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float Wrap(this float value, float low, float high)
{
return (float)((double)value).Wrap(low, high);
}
/// <summary>
/// Wraps the current single-precision floating-point number between 0 and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="length">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float Wrap(this float value, float length)
{
return (float)((double)value).Wrap(length);
}
}

View File

@ -149,4 +149,39 @@ public static class UInt16Extensions
{
return ((ulong)value).MultiplicativePersistence();
}
/// <summary>
/// Wraps the current 16-bit unsigned integer between a low and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="low">The inclusive lower bound.</param>
/// <param name="high">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static ushort Wrap(this ushort value, ushort low, ushort high)
{
return (ushort)((ulong)value).Wrap(low, high);
}
/// <summary>
/// Wraps the current 16-bit unsigned integer between 0 and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="length">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static ushort Wrap(this ushort value, ushort length)
{
return (ushort)((ulong)value).Wrap(length);
}
}

View File

@ -149,4 +149,39 @@ public static class UInt32Extensions
{
return ((ulong)value).MultiplicativePersistence();
}
/// <summary>
/// Wraps the current 32-bit unsigned integer between a low and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="low">The inclusive lower bound.</param>
/// <param name="high">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static uint Wrap(this uint value, uint low, uint high)
{
return (uint)((ulong)value).Wrap(low, high);
}
/// <summary>
/// Wraps the current 32-bit unsigned integer between 0 and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="length">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static uint Wrap(this uint value, uint length)
{
return (uint)((ulong)value).Wrap(length);
}
}

View File

@ -209,4 +209,40 @@ public static class UInt64Extensions
return persistence;
}
/// <summary>
/// Wraps the current 64-bit unsigned integer between a low and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="low">The inclusive lower bound.</param>
/// <param name="high">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static ulong Wrap(this ulong value, ulong low, ulong high)
{
ulong difference = high - low;
return low + (((value - low) % difference) + difference) % difference;
}
/// <summary>
/// Wraps the current 64-bit unsigned integer between 0 and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="length">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static ulong Wrap(this ulong value, ulong length)
{
return ((value % length) + length) % length;
}
}