Provides a sliding window over an iterator with sequential keys.
Description
This iterator returns overlapping windows of elements with keys
starting from 0 and incrementing sequentially.
Properties
Methods
Initializes the SlidingWindowIteratorIterator.
Counts the number of elements exposed by the inner iterator.
Retrieves the current sliding window of elements.
Returns the current sequential key.
Advances to the next element, maintaining the sliding window.
Resets the iterator, allowing re-iteration.
Determines whether the current window is valid.
private
int
$key
=
0
Description
the current sequential key for the iterator
private
array<int, mixed>
$window
=
[]
Description
the buffer holding the current window of elements
private
int
$windowSize
Description
the fixed size of each sliding window
Initializes the SlidingWindowIteratorIterator.
public
__construct(
iterable<string|int, mixed>
$iterator,
int
$windowSize) : mixed
Parameters
$iterator
:
iterable<string|int, mixed>
Description
the iterator containing values
$windowSize
:
int
Description
the number of elements per window (must be >= 1)
Description
if $windowSize is less than 1
Counts the number of elements exposed by the inner iterator.
public
count() : int
Description
If the inner iterator implements 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 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 values
Description
the total number of elements available from the inner iterator
Retrieves the current sliding window of elements.
public
current() : array<int, mixed>
Return values
Description
the current window of elements
Returns the current sequential key.
public
key() : int
Return values
Description
the current key, starting from 0
Advances to the next element, maintaining the sliding window.
public
next() : void
Resets the iterator, allowing re-iteration.
public
rewind() : void
Determines whether the current window is valid.
public
valid() : bool
Description
The iterator continues filling the window until the required size is met. If fewer elements than the window size exist, iteration stops.
Return values
Description
true if a valid window exists, false otherwise