Added To extension method (equivalent of Enumerable.Range)

This commit is contained in:
Oliver Booth 2022-04-21 17:22:29 +01:00
parent 28551f25e6
commit 881ccb0474
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
3 changed files with 241 additions and 1 deletions

View File

@ -1,4 +1,4 @@
using System.Diagnostics.Contracts;
using System.Diagnostics.Contracts;
using System.Net;
using System.Runtime.CompilerServices;
@ -213,6 +213,86 @@ public static class Int16Extensions
return Math.Sign(value);
}
/// <summary>
/// Returns an enumerable collection of 16-bit signed integers that range from the current value to a specified value.
/// </summary>
/// <param name="start">The value of the first integer in the sequence.</param>
/// <param name="end">The value of the last integer in the sequence.</param>
/// <returns>
/// <para>
/// An enumerable collection of 16-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in ascending order if <paramref name="start" /> is less than than <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection of 16-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in descending order if <paramref name="start" /> is greater than than
/// <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection that contains a single value if <paramref name="start" /> is equal to
/// <paramref name="end" />.
/// </para>
/// </returns>
public static IEnumerable<short> To(this short start, short end)
{
return start.To(end, 1);
}
/// <summary>
/// Returns an enumerable collection of 16-bit signed integers that range from the current value to a specified value.
/// </summary>
/// <param name="start">The value of the first integer in the sequence.</param>
/// <param name="end">The value of the last integer in the sequence.</param>
/// <param name="step">The increment by which to step.</param>
/// <returns>
/// <para>
/// An enumerable collection of 16-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in ascending order if <paramref name="start" /> is less than than <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection of 16-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in descending order if <paramref name="start" /> is greater than than
/// <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection that contains a single value if <paramref name="start" /> is equal to
/// <paramref name="end" />.
/// </para>
/// </returns>
public static IEnumerable<short> To(this short start, short end, short step)
{
if (step == 0)
{
throw new ArgumentOutOfRangeException(nameof(step), "Step cannot be zero.");
}
if (end == start)
{
yield return start;
yield break;
}
if (step > 0 || start < end)
{
for (short i = start; i <= end; i += step)
{
yield return i;
}
}
else
{
short absStep = Math.Abs(step);
for (short i = start; i >= end; i -= absStep)
{
yield return i;
}
}
}
/// <summary>
/// Converts an integer value from network byte order to host byte order.
/// </summary>

View File

@ -277,6 +277,86 @@ public static class Int32Extensions
return Math.Sign(value);
}
/// <summary>
/// Returns an enumerable collection of 32-bit signed integers that range from the current value to a specified value.
/// </summary>
/// <param name="start">The value of the first integer in the sequence.</param>
/// <param name="end">The value of the last integer in the sequence.</param>
/// <returns>
/// <para>
/// An enumerable collection of 32-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in ascending order if <paramref name="start" /> is less than than <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection of 32-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in descending order if <paramref name="start" /> is greater than than
/// <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection that contains a single value if <paramref name="start" /> is equal to
/// <paramref name="end" />.
/// </para>
/// </returns>
public static IEnumerable<int> To(this int start, int end)
{
return start.To(end, 1);
}
/// <summary>
/// Returns an enumerable collection of 32-bit signed integers that range from the current value to a specified value.
/// </summary>
/// <param name="start">The value of the first integer in the sequence.</param>
/// <param name="end">The value of the last integer in the sequence.</param>
/// <param name="step">The increment by which to step.</param>
/// <returns>
/// <para>
/// An enumerable collection of 32-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in ascending order if <paramref name="start" /> is less than than <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection of 32-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in descending order if <paramref name="start" /> is greater than than
/// <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection that contains a single value if <paramref name="start" /> is equal to
/// <paramref name="end" />.
/// </para>
/// </returns>
public static IEnumerable<int> To(this int start, int end, int step)
{
if (step == 0)
{
throw new ArgumentOutOfRangeException(nameof(step), "Step cannot be zero.");
}
if (end == start)
{
yield return start;
yield break;
}
if (step > 0 || start < end)
{
for (int i = start; i <= end; i += step)
{
yield return i;
}
}
else
{
int absStep = Math.Abs(step);
for (int i = start; i >= end; i -= absStep)
{
yield return i;
}
}
}
/// <summary>
/// Converts an integer value from network byte order to host byte order.
/// </summary>

View File

@ -238,6 +238,86 @@ public static class Int64Extensions
return Math.Sign(value);
}
/// <summary>
/// Returns an enumerable collection of 64-bit signed integers that range from the current value to a specified value.
/// </summary>
/// <param name="start">The value of the first integer in the sequence.</param>
/// <param name="end">The value of the last integer in the sequence.</param>
/// <returns>
/// <para>
/// An enumerable collection of 64-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in ascending order if <paramref name="start" /> is less than than <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection of 64-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in descending order if <paramref name="start" /> is greater than than
/// <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection that contains a single value if <paramref name="start" /> is equal to
/// <paramref name="end" />.
/// </para>
/// </returns>
public static IEnumerable<long> To(this long start, long end)
{
return start.To(end, 1);
}
/// <summary>
/// Returns an enumerable collection of 64-bit signed integers that range from the current value to a specified value.
/// </summary>
/// <param name="start">The value of the first integer in the sequence.</param>
/// <param name="end">The value of the last integer in the sequence.</param>
/// <param name="step">The increment by which to step.</param>
/// <returns>
/// <para>
/// An enumerable collection of 64-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in ascending order if <paramref name="start" /> is less than than <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection of 64-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in descending order if <paramref name="start" /> is greater than than
/// <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection that contains a single value if <paramref name="start" /> is equal to
/// <paramref name="end" />.
/// </para>
/// </returns>
public static IEnumerable<long> To(this long start, long end, long step)
{
if (step == 0)
{
throw new ArgumentOutOfRangeException(nameof(step), "Step cannot be zero.");
}
if (end == start)
{
yield return start;
yield break;
}
if (step > 0 || start < end)
{
for (long i = start; i <= end; i += step)
{
yield return i;
}
}
else
{
long absStep = Math.Abs(step);
for (long i = start; i >= end; i -= absStep)
{
yield return i;
}
}
}
/// <summary>
/// Converts an integer value from network byte order to host byte order.
/// </summary>