1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-22 19:28:48 +00:00

Change IE<byte>.Chunkify to IE<T>.Split

Thereby adding support for any IEnumerable<T>, and naming
convention is consistent with .NET and removes warnings of a
non-existent word
This commit is contained in:
Oliver Booth 2020-04-18 14:31:29 +01:00
parent ba2e34a755
commit 373313f07e
No known key found for this signature in database
GPG Key ID: 0D7F2EF1C8D2B9C0
2 changed files with 31 additions and 22 deletions

View File

@ -20,28 +20,6 @@
return BitConverter.ToString(bytes.ToArray());
}
/// <summary>
/// Splits <paramref name="bytes"/> into chunks of size <paramref name="chunkSize"/>.
/// </summary>
/// <param name="bytes">The collection to split.</param>
/// <param name="chunkSize">The maximum length of the nested <see cref="byte"/> collection.</param>
/// <returns>Returns an <see cref="IEnumerable{T}"/> of <see cref="IEnumerable{T}"/> of <see cref="byte"/>
/// values.</returns>
public static IEnumerable<IEnumerable<byte>> Chunkify(this IEnumerable<byte> bytes, int chunkSize)
{
IEnumerable<byte> enumerable = bytes as byte[] ?? bytes.ToArray();
long count = enumerable.LongCount();
List<IEnumerable<byte>> chunks = new List<IEnumerable<byte>>();
chunkSize = chunkSize.Clamp(1, enumerable.Count());
for (int i = 0; i < (int) (count / chunkSize); i++)
{
chunks.Add(enumerable.Skip(i * chunkSize).Take(chunkSize));
}
return chunks.ToArray();
}
/// <summary>
/// Converts the <see cref="byte"/>[] to an <see cref="short"/>.
/// </summary>

View File

@ -0,0 +1,31 @@
namespace X10D
{
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// Extension methods for <see cref="IEnumerable{T}"/>.
/// </summary>
public static class EnumerableExtensions
{
/// <summary>
/// Splits <paramref name="value"/> into chunks of size <paramref name="chunkSize"/>.
/// </summary>
/// <typeparam name="T">Any type.</typeparam>
/// <param name="value">The collection to split.</param>
/// <param name="chunkSize">The maximum length of the nested <see cref="byte"/> collection.</param>
/// <returns>Returns an <see cref="IEnumerable{T}"/> of <see cref="IEnumerable{T}"/> of <see cref="byte"/>
/// values.</returns>
public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> value, int chunkSize)
{
T[] enumerable = value.ToArray();
var count = enumerable.LongCount();
chunkSize = chunkSize.Clamp(1, enumerable.Length);
for (var i = 0; i < (int)(count / chunkSize); i++)
{
yield return enumerable.Skip(i * chunkSize).Take(chunkSize);
}
}
}
}