GeneratorRewindableIterator

Class

An iterator that allows rewinding over a generator by caching its values.

Description

This class wraps a generator or a closure returning a generator, enabling multiple iterations over the generated sequence by caching its results.

Usage Example:

Tags
example

Description

Using a Generator

use FastForward\Iterator\GeneratorRewindableIterator;

$iterator = new GeneratorRewindableIterator(
    (function () {
        yield 'A';
        yield 'B';
        yield 'C';
    })()
);

foreach ($iterator as $value) {
    echo $value; // Outputs: A B C
}

foreach ($iterator as $value) {
    echo $value; // Outputs: A B C (rewound)
}
example

Description

Using a Callable that Returns a Generator

use FastForward\Iterator\GeneratorRewindableIterator;

$iterator = new GeneratorRewindableIterator(fn () => (function () {
    yield 1;
    yield 2;
    yield 3;
})());

foreach ($iterator as $value) {
    echo $value; // Outputs: 1 2 3
}

foreach ($iterator as $value) {
    echo $value; // Outputs: 1 2 3 (rewound)
}

Note: This implementation ensures that the generator can be rewound by caching its results using GeneratorCachingIteratorAggregate.

since
1.0.0

Table of Contents

Properties

 : IteratorIterator
 : IteratorAggregate

Methods

__construct()

Initializes the GeneratorRewindableIterator with a generator or a closure returning a generator.

 : mixed
count()

Counts the number of elements available in the iterator.

 : int
current()

Retrieves the current element from the iterator.

 : mixed
key()

Retrieves the key of the current element.

 : mixed
next()

Advances the iterator to the next element.

 : void
rewind()

Rewinds the iterator to the beginning.

 : void
valid()

Checks if the current iterator position is valid.

 : bool
Properties

$innerIterator

Private
private IteratorIterator $innerIterator

Description

the internal innerIterator iterator used for iteration

$iteratorAggregate

Private Read-only
private IteratorAggregate $iteratorAggregate

Description

the iterator aggregate that caches generated values

Methods

__construct()

Public

Initializes the GeneratorRewindableIterator with a generator or a closure returning a generator.

public __construct(Closure|Generator  $generator) : mixed
Parameters
$generator : Closure|Generator

Description

a generator instance or a callable that returns a generator

count()

Public

Counts the number of elements available in the iterator.

public count() : int

Description

This method MUST count the elements by iterating over a clone of the current iterator instance so that the active iterator state of the original object is not modified during the counting process. Concrete implementations SHOULD therefore remain safely cloneable whenever this behavior is expected to be used.

Return values
int

Description

the total number of elements exposed by the iterator

current()

Public

Retrieves the current element from the iterator.

public current() : mixed
Return values

Description

the current element

key()

Public

Retrieves the key of the current element.

public key() : mixed
Return values

Description

the key associated with the current element

rewind()

Public

Rewinds the iterator to the beginning.

public rewind() : void

Description

This method creates a new IteratorIterator instance wrapping the GeneratorCachingIteratorAggregate, ensuring that the generator can be reused.

valid()

Public

Checks if the current iterator position is valid.

public valid() : bool
Return values
bool

Description

true if the current position is valid, false otherwise