SlidingWindowIteratorIterator

Class

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.

Tags
since
1.0.0

Table of Contents

Properties

 : int
 : array<int, mixed>

Methods

__construct()

Initializes the SlidingWindowIteratorIterator.

 : mixed
count()

Counts the number of elements exposed by the inner iterator.

 : int
current()

Retrieves the current sliding window of elements.

 : array<int, mixed>
key()

Returns the current sequential key.

 : int
next()

Advances to the next element, maintaining the sliding window.

 : void
rewind()

Resets the iterator, allowing re-iteration.

 : void
valid()

Determines whether the current window is valid.

 : bool
Properties
Methods

__construct()

Public

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)

Tags
throws
InvalidArgumentException

Description

if $windowSize is less than 1

count()

Public

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
int

Description

the total number of elements available from the inner iterator

current()

Public

Retrieves the current sliding window of elements.

public current() : array<int, mixed>
Return values
array<int, mixed>

Description

the current window of elements

key()

Public

Returns the current sequential key.

public key() : int
Return values
int

Description

the current key, starting from 0

valid()

Public

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
bool

Description

true if a valid window exists, false otherwise