Table of Contents

Class WindowSequence

Namespace
PatternKit.Behavioral.Iterator
Assembly
PatternKit.Core.dll

Sliding / striding window iterator over an IEnumerable<T> that yields immutable (or buffer-reused) window views. Demonstrates creating a custom enumerator with additional semantics while still presenting standard IEnumerable API.

public static class WindowSequence
Inheritance
WindowSequence
Inherited Members

Remarks

Use Windows<T>(IEnumerable<T>, int, int, bool, bool) to produce fixed-size windows with an optional stride and partial inclusion.

Design goals: clarity and extensibility over micro-optimizations; showcases custom enumerator + reusable buffer option.

Methods

Windows<T>(IEnumerable<T>, int, int, bool, bool)

Produces a sequence of sliding (or striding) windows from source.

public static IEnumerable<WindowSequence.Window<T>> Windows<T>(this IEnumerable<T> source, int size, int stride = 1, bool includePartial = false, bool reuseBuffer = false)

Parameters

source IEnumerable<T>

Underlying source sequence (enumerated exactly once).

size int

Window size (> 0).

stride int

Elements to advance between windows (default 1; must be > 0).

includePartial bool

When true, a trailing window smaller than size is yielded.

reuseBuffer bool

When true, the same underlying array is reused for each full window (call ToArray() if you need a snapshot).

Returns

IEnumerable<WindowSequence.Window<T>>

An IEnumerable<T> of WindowSequence.Window<T> values.

Type Parameters

T

Element type.

Remarks

Reuse buffer trade-off: Setting reuseBuffer reduces allocations for hot paths; consumers MUST copy data they intend to keep.

The implementation is intentionally allocation-light (queue nodes only) and simple; a ring buffer would be faster but more complex.

Exceptions

ArgumentOutOfRangeException

Thrown when size or stride are not positive.