mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-01-11 19:44:55 +00:00
Extension method for padding a sequence
This commit is contained in:
@@ -267,6 +267,23 @@ namespace ICD.Common.Utils.Tests.Extensions
|
||||
Assert.AreEqual(6, values[5]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PadRightTest()
|
||||
{
|
||||
int[] items = new[] {1, 2, 3}.PadRight(5).ToArray();
|
||||
|
||||
Assert.AreEqual(5, items.Length);
|
||||
Assert.AreEqual(1, items[0]);
|
||||
Assert.AreEqual(2, items[1]);
|
||||
Assert.AreEqual(3, items[2]);
|
||||
Assert.AreEqual(0, items[3]);
|
||||
Assert.AreEqual(0, items[4]);
|
||||
|
||||
items = items.PadRight(2).ToArray();
|
||||
|
||||
Assert.AreEqual(5, items.Length);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void OrderTest()
|
||||
{
|
||||
|
||||
@@ -472,6 +472,31 @@ namespace ICD.Common.Utils.Extensions
|
||||
yield return item;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pads the given sequence to the given count size with default items.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="count"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static IEnumerable<T> PadRight<T>(this IEnumerable<T> extends, int count)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
int index = 0;
|
||||
|
||||
foreach (T item in extends)
|
||||
{
|
||||
yield return item;
|
||||
index++;
|
||||
}
|
||||
|
||||
for (; index < count; index++)
|
||||
yield return default(T);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Default ordering for the items in the sequence.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user