Skip to content

SlidingWindowIteratorIterator

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

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 exposed by the inner iterator.

public count(): int

If the inner iterator implements

  • See: \Countable, this method SHALL return the value provided by that implementation. Otherwise, it MUST count elements by iterating over the iterator. If the inner iterator is not cloneable, this method SHALL wrap the current object in an

  • See: \IteratorIterator instance and count through that wrapper to avoid performing an invalid clone operation. If the inner iterator is cloneable, this method SHOULD count over a clone so that the original iterator state is preserved as much as possible.

Return Value:

the total number of elements available from the inner iterator


Clone this wiki locally