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:
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
}
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.
Properties
Methods
Initializes the UniqueIteratorIterator.
Counts the number of elements exposed by the inner iterator.
Retrieves the normalized sequential key for the current unique element.
Advances to the next unique element.
Resets the iterator and clears the seen values.
Skips values that have already been encountered and stores the current unique value.
private
bool
$caseSensitive
=
true
private
int
$position
=
0
Description
the current position for the unique elements
private
array<int|string, mixed>
$seen
=
[]
Description
stores seen values to ensure uniqueness
private
bool
$strict
=
true
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)
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 normalized sequential key for the current unique element.
public
key() : int
Return values
Description
the zero-based position of the current unique value
Advances to the next unique element.
public
next() : void
Resets the iterator and clears the seen values.
public
rewind() : void
Skips values that have already been encountered and stores the current unique value.
private
skipDuplicates() : void