UniqueIteratorIterator

Class

Filters duplicate values from an iterator, ensuring uniqueness.

Description

This iterator allows traversing an iterable while maintaining a record of seen values, returning only the first occurrence of each unique value. Subsequent occurrences are skipped.

Usage Example:

Tags
example

Description

Removing duplicate values

use FastForward\Iterator\UniqueIteratorIterator;
use ArrayIterator;

$data = new ArrayIterator([1, 2, 2, 3, 4, 4, 5]);
$uniqueIterator = new UniqueIteratorIterator($data);

foreach ($uniqueIterator as $num) {
    echo $num . ' '; // Outputs: 1 2 3 4 5
}
example

Description

Case-insensitive uniqueness

use FastForward\Iterator\UniqueIteratorIterator;
use ArrayIterator;

$data = new ArrayIterator(['a', 'A', 'b', 'B', 'a']);
$uniqueIterator = new UniqueIteratorIterator($data, false);

foreach ($uniqueIterator as $char) {
    echo $char . ' '; // Outputs: a A b B a (case-sensitive comparison disabled)
}

Note: This iterator preserves the order of first occurrences.

since
1.0.0

Table of Contents

Properties

 : int
 : array<int|string, mixed>
 : bool

Methods

__construct()

Initializes the UniqueIteratorIterator.

 : mixed
count()

Counts the number of elements exposed by the inner iterator.

 : int
key()

Retrieves the normalized sequential key for the current unique element.

 : int
next()

Advances to the next unique element.

 : void
rewind()

Resets the iterator and clears the seen values.

 : void
skipDuplicates()

Skips values that have already been encountered and stores the current unique value.

 : void
Properties

$position

Private
private int $position = 0

Description

the current position for the unique elements

$seen

Private
private array<int|string, mixed> $seen = []

Description

stores seen values to ensure uniqueness

Methods

__construct()

Public

Initializes the UniqueIteratorIterator.

public __construct( iterable<string|int, mixed>  $iterator[, bool  $strict = true][, bool  $caseSensitive = true]) : mixed
Parameters
$iterator : iterable<string|int, mixed>

Description

the iterator to filter for unique values

$strict : bool = true

Description

whether to use strict comparison (default: true)

$caseSensitive : bool = true

Description

whether to use case-sensitive comparison (default: true)

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

key()

Public

Retrieves the normalized sequential key for the current unique element.

public key() : int
Return values
int

Description

the zero-based position of the current unique value

skipDuplicates()

Private

Skips values that have already been encountered and stores the current unique value.

private skipDuplicates() : void