From 88244def341b12a38cf3b396de3d60203efcec03 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Thu, 4 Mar 2021 01:37:02 +0000 Subject: [PATCH] (#7) Move Int64Extensions to child namespace --- X10D/src/Int64Extensions.cs | 241 -------------------- X10D/src/Int64Extensions/Int64Extensions.cs | 113 +++++++++ 2 files changed, 113 insertions(+), 241 deletions(-) delete mode 100644 X10D/src/Int64Extensions.cs create mode 100644 X10D/src/Int64Extensions/Int64Extensions.cs diff --git a/X10D/src/Int64Extensions.cs b/X10D/src/Int64Extensions.cs deleted file mode 100644 index 1d5401b..0000000 --- a/X10D/src/Int64Extensions.cs +++ /dev/null @@ -1,241 +0,0 @@ -using System; - -namespace X10D -{ - /// - /// Extension methods for . - /// - public static class Int64Extensions - { - /// - /// Clamps a value between a minimum and a maximum value. - /// - /// The value to clamp. - /// The minimum value. - /// The maximum value. - /// - /// Returns if is greater than it, - /// if is less than it, - /// or itself otherwise. - /// - public static long Clamp(this long value, long min, long max) - { - return Math.Min(Math.Max(value, min), max); - } - - /// - /// Clamps a value between a minimum and a maximum value. - /// - /// The value to clamp. - /// The minimum value. - /// The maximum value. - /// - /// Returns if is greater than it, - /// if is less than it, - /// or itself otherwise. - /// - [CLSCompliant(false)] - public static ulong Clamp(this ulong value, ulong min, ulong max) - { - return Math.Min(Math.Max(value, min), max); - } - - /// - /// Converts the to a treating it as a Unix timestamp. - /// - /// The timestamp. - /// - /// Optional. Whether or not the input value should be treated as milliseconds. Defaults - /// to . - /// - /// - /// Returns a representing seconds since the Unix - /// epoch. - /// - public static DateTime FromUnixTimestamp(this long timestamp, bool isMillis = false) - { - var offset = isMillis - ? DateTimeOffset.FromUnixTimeMilliseconds(timestamp) - : DateTimeOffset.FromUnixTimeSeconds(timestamp); - - return offset.DateTime; - } - - /// - /// Converts the to a []. - /// - /// The number to convert. - /// Returns a []. - [CLSCompliant(false)] - public static byte[] GetBytes(this ulong number) - { - return BitConverter.GetBytes(number); - } - - /// - /// Converts the to a []. - /// - /// The number to convert. - /// Returns a []. - public static byte[] GetBytes(this long number) - { - return BitConverter.GetBytes(number); - } - - /// - /// Determines if the is even. - /// - /// The number. - /// - /// Returns if is even, - /// otherwise. - /// - public static bool IsEven(this long number) - { - return Math.Abs(number % 2.0) < double.Epsilon; - } - - /// - /// Determines if the is even. - /// - /// The number. - /// - /// Returns if is even, - /// otherwise. - /// - [CLSCompliant(false)] - public static bool IsEven(this ulong number) - { - return Math.Abs(number % 2.0) < double.Epsilon; - } - - /// - /// Determines if the is odd. - /// - /// The number. - /// - /// Returns if is odd, - /// otherwise. - /// - public static bool IsOdd(this long number) - { - return !IsEven(number); - } - - /// - /// Determines if the is odd. - /// - /// The number. - /// - /// Returns if is odd, - /// otherwise. - /// - [CLSCompliant(false)] - public static bool IsOdd(this ulong number) - { - return !IsEven(number); - } - - /// - /// Determines if the is a prime number. - /// - /// The number. - /// - /// Returns if is prime, - /// otherwise. - /// - 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; - } - - /// - /// Determines if the is a prime number. - /// - /// The number. - /// - /// Returns if is prime, - /// otherwise. - /// - [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; - } - - /// - /// Gets an boolean value that represents this integer. - /// - /// The integer. - /// - /// Returns if is 0, - /// otherwise. - /// - public static bool ToBoolean(this long value) - { - return value != 0; - } - - /// - /// Gets an boolean value that represents this integer. - /// - /// The integer. - /// - /// Returns if is 0, - /// otherwise. - /// - [CLSCompliant(false)] - public static bool ToBoolean(this ulong value) - { - return value != 0; - } - } -} diff --git a/X10D/src/Int64Extensions/Int64Extensions.cs b/X10D/src/Int64Extensions/Int64Extensions.cs new file mode 100644 index 0000000..464ae8a --- /dev/null +++ b/X10D/src/Int64Extensions/Int64Extensions.cs @@ -0,0 +1,113 @@ +using System; + +namespace X10D.Int64Extensions +{ + /// + /// Extension methods for . + /// + public static class Int64Extensions + { + /// + /// Converts a Unix time expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z to a + /// 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. + /// + /// A date and time value that represents the same moment in time as the Unix time. + public static DateTimeOffset FromUnixTimeMilliseconds(this long value) + { + return DateTimeOffset.FromUnixTimeMilliseconds(value); + } + + /// + /// Converts a Unix time expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z to a + /// 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. + /// + /// A date and time value that represents the same moment in time as the Unix time. + public static DateTimeOffset FromUnixTimeSeconds(this long value) + { + return DateTimeOffset.FromUnixTimeSeconds(value); + } + + /// + /// Returns the current 64-bit signed integer value as an array of bytes. + /// + /// The number to convert. + /// An array of bytes with length 8. + public static byte[] GetBytes(this long value) + { + return BitConverter.GetBytes(value); + } + + /// + /// Returns a value indicating whether the current 64-bit signed integer is even. + /// + /// The number to check. + /// if is even, or otherwise. + public static bool IsEven(this long value) + { + return value % 2 == 0; + } + + /// + /// Returns a value indicating whether the current 64-bit signed integer is odd. + /// + /// The number to check. + /// if is odd, or otherwise. + public static bool IsOdd(this long value) + { + return !value.IsEven(); + } + + /// + /// Returns a value indicating whether the current 64-bit signed integer is prime. + /// + /// The number to check. + /// if is prime, or otherwise. + 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; + } + + /// + /// Converts the value of the current 64-bit signed integer to an equivalent value. + /// + /// The value to convert. + /// if is not zero, or otherwise. + public static bool ToBoolean(this long value) + { + return value != 0; + } + } +}