-
-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Nette Framework (https://nette.org) | ||
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nette\Iterators; | ||
|
||
|
||
/** | ||
* MemoizingIterator wraps around another iterator and caches its keys and values during iteration. | ||
* This allows the data to be re-iterated multiple times. | ||
* @template K | ||
* @template V | ||
* @implements \Iterator<K, V> | ||
*/ | ||
class MemoizingIterator implements \Iterator | ||
{ | ||
private array $cache = []; | ||
private int $index = 0; | ||
|
||
|
||
/** | ||
* @param \Iterator<K, V> $inner | ||
*/ | ||
public function __construct( | ||
private readonly \Iterator $inner, | ||
) { | ||
} | ||
|
||
|
||
public function rewind(): void | ||
{ | ||
if (!$this->cache) { | ||
$this->inner->rewind(); | ||
} | ||
$this->index = 0; | ||
} | ||
|
||
|
||
/** | ||
* @return V | ||
*/ | ||
public function current(): mixed | ||
{ | ||
return $this->cache[$this->index][1] ?? null; | ||
} | ||
|
||
|
||
/** | ||
* @return K | ||
*/ | ||
public function key(): mixed | ||
{ | ||
return $this->cache[$this->index][0] ?? null; | ||
} | ||
|
||
|
||
public function next(): void | ||
{ | ||
if (!isset($this->cache[++$this->index])) { | ||
$this->inner->next(); | ||
} | ||
} | ||
|
||
|
||
public function valid(): bool | ||
{ | ||
if (!isset($this->cache[$this->index]) && $this->inner->valid()) { | ||
$this->cache[$this->index] = [$this->inner->key(), $this->inner->current()]; | ||
} | ||
return isset($this->cache[$this->index]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
/** | ||
* Test: Nette\Iterators\MemoizingIterator | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\Iterators\MemoizingIterator; | ||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
function iterator(): Generator | ||
{ | ||
yield 'a' => 'apple'; | ||
yield ['b'] => ['banana']; | ||
yield 'c' => 'cherry'; | ||
} | ||
|
||
|
||
test('iteration', function () { | ||
$iterator = new MemoizingIterator(iterator()); | ||
|
||
$pairs = []; | ||
foreach ($iterator as $key => $value) { | ||
$pairs[] = [$key, $value]; | ||
} | ||
Assert::same( | ||
[ | ||
['a', 'apple'], | ||
[['b'], ['banana']], | ||
['c', 'cherry'], | ||
], | ||
$pairs, | ||
); | ||
}); | ||
|
||
|
||
test('re-iteration', function () { | ||
$iterator = new MemoizingIterator(iterator()); | ||
|
||
foreach ($iterator as $value); | ||
|
||
$pairs = []; | ||
foreach ($iterator as $key => $value) { | ||
$pairs[] = [$key, $value]; | ||
} | ||
Assert::same( | ||
[ | ||
['a', 'apple'], | ||
[['b'], ['banana']], | ||
['c', 'cherry'], | ||
], | ||
$pairs, | ||
); | ||
}); | ||
|
||
|
||
test('nested re-iteration', function () { // nefunguje | ||
$iterator = new MemoizingIterator(iterator()); | ||
|
||
$pairs = []; | ||
foreach ($iterator as $key => $value) { | ||
$pairs[] = [$key, $value]; | ||
foreach ($iterator as $value); | ||
} | ||
Assert::same( | ||
[ | ||
['a', 'apple'], | ||
[['b'], ['banana']], | ||
['c', 'cherry'], | ||
], | ||
$pairs, | ||
); | ||
}); |