mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-22 23:58: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:
parent
ba2e34a755
commit
373313f07e
@ -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>
|
||||
|
31
X10D/src/EnumerableExtensions.cs
Normal file
31
X10D/src/EnumerableExtensions.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user