-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Rewrite iterators and optimize things here and there.
- Loading branch information
Showing
12 changed files
with
300 additions
and
99 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
spec/loophp/collection/Iterator/ArrayCacheIteratorSpec.php
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,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\loophp\collection\Iterator; | ||
|
||
use ArrayIterator; | ||
use loophp\collection\Iterator\ArrayCacheIterator; | ||
use PhpSpec\Exception\Exception; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
class ArrayCacheIteratorSpec extends ObjectBehavior | ||
{ | ||
public function it_can_cache_an_iterator_of_type_generator() | ||
{ | ||
$generator = static function () { | ||
yield 'a'; | ||
|
||
yield 'b'; | ||
|
||
yield 'c'; | ||
|
||
yield 'd'; | ||
|
||
yield 'e'; | ||
}; | ||
|
||
$this->beConstructedWith($generator()); | ||
|
||
$this | ||
->valid() | ||
->shouldReturn(true); | ||
|
||
if (5 !== iterator_count($this->getWrappedObject())) { | ||
throw new Exception('The count is invalid.'); | ||
} | ||
|
||
$this | ||
->shouldIterateAs( | ||
range('a', 'e') | ||
); | ||
} | ||
|
||
public function it_is_initializable() | ||
{ | ||
$this->beConstructedWith(new ArrayIterator([])); | ||
|
||
$this->shouldHaveType(ArrayCacheIterator::class); | ||
} | ||
} |
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
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
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
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,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Iterator; | ||
|
||
use Iterator; | ||
|
||
use function array_key_exists; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @psalm-template TKey | ||
* @psalm-template TKey of array-key | ||
* @psalm-template T | ||
* | ||
* @extends ProxyIterator<TKey, T> | ||
*/ | ||
final class ArrayCacheIterator extends ProxyIterator | ||
{ | ||
/** | ||
* @psalm-var array<int, array{0: TKey, 1: T}> | ||
*/ | ||
private array $cache = []; | ||
|
||
private int $key = 0; | ||
|
||
/** | ||
* @psalm-param Iterator<TKey, T> $iterator | ||
*/ | ||
public function __construct(Iterator $iterator) | ||
{ | ||
$this->iterator = $iterator; | ||
} | ||
|
||
/** | ||
* @psalm-return T | ||
*/ | ||
public function current() | ||
{ | ||
/** @psalm-var array{TKey, T} $data */ | ||
$data = $this->getTupleFromCache($this->key); | ||
|
||
return $data[1]; | ||
} | ||
|
||
/** | ||
* @psalm-return TKey | ||
*/ | ||
public function key() | ||
{ | ||
/** @psalm-var array{TKey, T} $data */ | ||
$data = $this->getTupleFromCache($this->key); | ||
|
||
return $data[0]; | ||
} | ||
|
||
public function next(): void | ||
{ | ||
// This is mostly for iterator_count(). | ||
$this->getTupleFromCache($this->key++); | ||
|
||
parent::next(); | ||
} | ||
|
||
public function rewind(): void | ||
{ | ||
// No call to parent::rewind() because we do not know if the inner | ||
// iterator can be rewinded or not. | ||
$this->key = 0; | ||
} | ||
|
||
public function valid(): bool | ||
{ | ||
if (parent::valid()) { | ||
return true; | ||
} | ||
|
||
return array_key_exists($this->key, $this->cache); | ||
} | ||
|
||
/** | ||
* @psalm-return array{0: TKey, 1: T} | ||
*/ | ||
private function getTupleFromCache(int $key): array | ||
{ | ||
return $this->cache[$key] ??= [ | ||
parent::key(), | ||
parent::current(), | ||
]; | ||
} | ||
} |
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,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Iterator; | ||
|
||
use Generator; | ||
|
||
/** | ||
* @psalm-template TKey | ||
* @psalm-template T | ||
* | ||
* @extends ProxyIterator<TKey, T> | ||
*/ | ||
final class ClosureIterator extends ProxyIterator | ||
{ | ||
/** | ||
* @var array<int, mixed> | ||
* @psalm-var list<mixed> | ||
*/ | ||
private array $arguments; | ||
|
||
/** | ||
* @var callable | ||
* @psalm-var callable(mixed ...):Generator<TKey, T> | ||
*/ | ||
private $callable; | ||
|
||
/** | ||
* @param mixed ...$arguments | ||
* @psalm-param mixed ...$arguments | ||
* @psalm-param callable(mixed ...):Generator<TKey, T> $callable | ||
*/ | ||
public function __construct(callable $callable, ...$arguments) | ||
{ | ||
$this->callable = $callable; | ||
$this->arguments = $arguments; | ||
$this->iterator = $this->getGenerator(); | ||
} | ||
|
||
public function rewind(): void | ||
{ | ||
$this->iterator = $this->getGenerator(); | ||
} | ||
|
||
/** | ||
* @psalm-return Generator<TKey, T> | ||
*/ | ||
private function getGenerator(): Generator | ||
{ | ||
return yield from ($this->callable)(...$this->arguments); | ||
} | ||
} |
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
Oops, something went wrong.