Skip to content

SlidingWindowIteratorIterator

github-actions edited this page Mar 31, 2026 · 3 revisions

Class SlidingWindowIteratorIterator.

Provides a sliding window over an iterator with sequential keys.

This iterator returns overlapping windows of elements with keys starting from 0 and incrementing sequentially.


Methods

__construct

Initializes the SlidingWindowIteratorIterator.

public __construct(iterable $iterator, int $windowSize): mixed

Parameters:

Parameter Type Description
$iterator iterable the iterator containing values
$windowSize int the number of elements per window (must be >= 1)

Throws:

if $windowSize is less than 1


next

Advances to the next element, maintaining the sliding window.

public next(): void

current

Retrieves the current sliding window of elements.

public current(): array<int,mixed>

Return Value:

the current window of elements


key

Returns the current sequential key.

public key(): int

Return Value:

the current key, starting from 0


valid

Determines whether the current window is valid.

public valid(): bool

The iterator continues filling the window until the required size is met. If fewer elements than the window size exist, iteration stops.

Return Value:

true if a valid window exists, false otherwise


rewind

Resets the iterator, allowing re-iteration.

public rewind(): void

Inherited methods

count

Counts the number of elements in the iterable.

public count(): int

If the inner iterator implements Countable, it uses that. Otherwise, it counts the elements by iterating through them.

Return Value:

the number of elements in the iterable


Clone this wiki locally