Added a sliding window extension for IEnumerable
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
|
||||
namespace AdventOfCode.Extensions;
|
||||
|
||||
@@ -144,6 +145,18 @@ public static class EnumerableExtensions
|
||||
}
|
||||
}
|
||||
|
||||
private static TValue[] GetCombination<TValue>(int[] innerIndices, TValue[] innerPool) =>
|
||||
innerIndices.Select(i => innerPool[i]).ToArray();
|
||||
private static TValue[] GetCombination<TValue>(int[] indices, TValue[] values) =>
|
||||
indices.Select(i => values[i]).ToArray();
|
||||
|
||||
public static IEnumerable<TValue[]> SlidingWindow<TValue>(this IEnumerable<TValue> values, int count)
|
||||
{
|
||||
var queue = new Queue<TValue>(count);
|
||||
foreach (var value in values)
|
||||
{
|
||||
queue.Enqueue(value);
|
||||
if(queue.Count > count)
|
||||
queue.Dequeue();
|
||||
yield return queue.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user