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

(#7) Move Int64Extensions to child namespace

This commit is contained in:
Oliver Booth 2021-03-04 01:37:02 +00:00
parent f2b37962d6
commit 88244def34
2 changed files with 113 additions and 241 deletions

View File

@ -1,241 +0,0 @@
using System;
namespace X10D
{
/// <summary>
/// Extension methods for <see cref="long" />.
/// </summary>
public static class Int64Extensions
{
/// <summary>
/// Clamps a value between a minimum and a maximum value.
/// </summary>
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>
/// Returns <paramref name="max" /> if <paramref name="value" /> is greater than it,
/// <paramref name="min" /> if <paramref name="value" /> is less than it,
/// or <paramref name="value" /> itself otherwise.
/// </returns>
public static long Clamp(this long value, long min, long max)
{
return Math.Min(Math.Max(value, min), max);
}
/// <summary>
/// Clamps a value between a minimum and a maximum value.
/// </summary>
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>
/// Returns <paramref name="max" /> if <paramref name="value" /> is greater than it,
/// <paramref name="min" /> if <paramref name="value" /> is less than it,
/// or <paramref name="value" /> itself otherwise.
/// </returns>
[CLSCompliant(false)]
public static ulong Clamp(this ulong value, ulong min, ulong max)
{
return Math.Min(Math.Max(value, min), max);
}
/// <summary>
/// Converts the <see cref="long" /> to a <see cref="DateTime" /> treating it as a Unix timestamp.
/// </summary>
/// <param name="timestamp">The timestamp.</param>
/// <param name="isMillis">
/// Optional. Whether or not the input value should be treated as milliseconds. Defaults
/// to <see langword="false" />.
/// </param>
/// <returns>
/// Returns a <see cref="DateTime" /> representing <paramref name="timestamp" /> seconds since the Unix
/// epoch.
/// </returns>
public static DateTime FromUnixTimestamp(this long timestamp, bool isMillis = false)
{
var offset = isMillis
? DateTimeOffset.FromUnixTimeMilliseconds(timestamp)
: DateTimeOffset.FromUnixTimeSeconds(timestamp);
return offset.DateTime;
}
/// <summary>
/// Converts the <see cref="ulong" /> to a <see cref="byte" />[].
/// </summary>
/// <param name="number">The number to convert.</param>
/// <returns>Returns a <see cref="byte" />[].</returns>
[CLSCompliant(false)]
public static byte[] GetBytes(this ulong number)
{
return BitConverter.GetBytes(number);
}
/// <summary>
/// Converts the <see cref="long" /> to a <see cref="byte" />[].
/// </summary>
/// <param name="number">The number to convert.</param>
/// <returns>Returns a <see cref="byte" />[].</returns>
public static byte[] GetBytes(this long number)
{
return BitConverter.GetBytes(number);
}
/// <summary>
/// Determines if the <see cref="long" /> is even.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is even, <see langword="false" />
/// otherwise.
/// </returns>
public static bool IsEven(this long number)
{
return Math.Abs(number % 2.0) < double.Epsilon;
}
/// <summary>
/// Determines if the <see cref="ulong" /> is even.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is even, <see langword="false" />
/// otherwise.
/// </returns>
[CLSCompliant(false)]
public static bool IsEven(this ulong number)
{
return Math.Abs(number % 2.0) < double.Epsilon;
}
/// <summary>
/// Determines if the <see cref="long" /> is odd.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is odd, <see langword="false" />
/// otherwise.
/// </returns>
public static bool IsOdd(this long number)
{
return !IsEven(number);
}
/// <summary>
/// Determines if the <see cref="ulong" /> is odd.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is odd, <see langword="false" />
/// otherwise.
/// </returns>
[CLSCompliant(false)]
public static bool IsOdd(this ulong number)
{
return !IsEven(number);
}
/// <summary>
/// Determines if the <see cref="long" /> is a prime number.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is prime, <see langword="false" />
/// otherwise.
/// </returns>
public static bool IsPrime(this long number)
{
if (number <= 1)
{
return false;
}
if (number == 2)
{
return true;
}
if (number % 2 == 0)
{
return false;
}
var boundary = (long)Math.Floor(Math.Sqrt(number));
for (var i = 3; i <= boundary; i += 2)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
/// <summary>
/// Determines if the <see cref="ulong" /> is a prime number.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>
/// Returns <see langword="true" /> if <paramref name="number" /> is prime, <see langword="false" />
/// otherwise.
/// </returns>
[CLSCompliant(false)]
public static bool IsPrime(this ulong number)
{
if (number <= 1)
{
return false;
}
if (number == 2)
{
return true;
}
if (number % 2 == 0)
{
return false;
}
var boundary = (ulong)Math.Floor(Math.Sqrt(number));
for (uint i = 3; i <= boundary; i += 2)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
/// <summary>
/// Gets an boolean value that represents this integer.
/// </summary>
/// <param name="value">The integer.</param>
/// <returns>
/// Returns <see langword="false" /> if <paramref name="value" /> is 0,
/// <see langword="true" /> otherwise.
/// </returns>
public static bool ToBoolean(this long value)
{
return value != 0;
}
/// <summary>
/// Gets an boolean value that represents this integer.
/// </summary>
/// <param name="value">The integer.</param>
/// <returns>
/// Returns <see langword="false" /> if <paramref name="value" /> is 0,
/// <see langword="true" /> otherwise.
/// </returns>
[CLSCompliant(false)]
public static bool ToBoolean(this ulong value)
{
return value != 0;
}
}
}

View File

@ -0,0 +1,113 @@
using System;
namespace X10D.Int64Extensions
{
/// <summary>
/// Extension methods for <see cref="long" />.
/// </summary>
public static class Int64Extensions
{
/// <summary>
/// Converts a Unix time expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z to a
/// <see cref="DateTimeOffset" /> value.
/// </summary>
/// <param name="value">
/// A Unix time, expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z (January 1,
/// 1970, at 12:00 AM UTC). For Unix times before this date, its value is negative.
/// </param>
/// <returns>A date and time value that represents the same moment in time as the Unix time.</returns>
public static DateTimeOffset FromUnixTimeMilliseconds(this long value)
{
return DateTimeOffset.FromUnixTimeMilliseconds(value);
}
/// <summary>
/// Converts a Unix time expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z to a
/// <see cref="DateTimeOffset" /> value.
/// </summary>
/// <param name="value">
/// A Unix time, expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z (January 1, 1970, at
/// 12:00 AM UTC). For Unix times before this date, its value is negative.
/// </param>
/// <returns>A date and time value that represents the same moment in time as the Unix time.</returns>
public static DateTimeOffset FromUnixTimeSeconds(this long value)
{
return DateTimeOffset.FromUnixTimeSeconds(value);
}
/// <summary>
/// Returns the current 64-bit signed integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 8.</returns>
public static byte[] GetBytes(this long value)
{
return BitConverter.GetBytes(value);
}
/// <summary>
/// Returns a value indicating whether the current 64-bit signed integer is even.
/// </summary>
/// <param name="value">The number to check.</param>
/// <returns><see langword="true" /> if <paramref name="value" /> is even, or <see langword="false" /> otherwise.</returns>
public static bool IsEven(this long value)
{
return value % 2 == 0;
}
/// <summary>
/// Returns a value indicating whether the current 64-bit signed integer is odd.
/// </summary>
/// <param name="value">The number to check.</param>
/// <returns><see langword="true" /> if <paramref name="value" /> is odd, or <see langword="false" /> otherwise.</returns>
public static bool IsOdd(this long value)
{
return !value.IsEven();
}
/// <summary>
/// Returns a value indicating whether the current 64-bit signed integer is prime.
/// </summary>
/// <param name="value">The number to check.</param>
/// <returns><see langword="true" /> if <paramref name="value" /> is prime, or <see langword="false" /> otherwise.</returns>
public static bool IsPrime(this long value)
{
switch (value)
{
case < 2: return false;
case 2:
case 3: return true;
}
if (value % 2 == 0 || value % 3 == 0)
{
return false;
}
if ((value + 1) % 6 != 0 && (value - 1) % 6 != 0)
{
return false;
}
for (var iterator = 5L; iterator * iterator <= value; iterator += 6)
{
if (value % iterator == 0 || value % (iterator + 2) == 0)
{
return false;
}
}
return true;
}
/// <summary>
/// Converts the value of the current 64-bit signed integer to an equivalent <see cref="bool" /> value.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <returns><see langword="true" /> if <paramref name="value" /> is not zero, or <see langword="false" /> otherwise.</returns>
public static bool ToBoolean(this long value)
{
return value != 0;
}
}
}