| @@ 5-79 (lines=75) @@ | ||
| 2 | ||
| 3 | namespace Collections\Iterator; |
|
| 4 | ||
| 5 | class LazySliceIterator implements \Iterator |
|
| 6 | { |
|
| 7 | /** |
|
| 8 | * @var \Iterator |
|
| 9 | */ |
|
| 10 | private $it; |
|
| 11 | ||
| 12 | /** |
|
| 13 | * @var int |
|
| 14 | */ |
|
| 15 | private $start; |
|
| 16 | ||
| 17 | /** |
|
| 18 | * @var int |
|
| 19 | */ |
|
| 20 | private $len; |
|
| 21 | ||
| 22 | /** |
|
| 23 | * @var int |
|
| 24 | */ |
|
| 25 | private $currentLen; |
|
| 26 | ||
| 27 | public function __construct($it, $start, $len) |
|
| 28 | { |
|
| 29 | $this->it = $it; |
|
| 30 | $this->start = $start; |
|
| 31 | $this->len = $len; |
|
| 32 | $this->currentLen = $len; |
|
| 33 | while ($start !== 0 && $it->valid()) { |
|
| 34 | $it->next(); |
|
| 35 | --$start; |
|
| 36 | } |
|
| 37 | } |
|
| 38 | ||
| 39 | public function __clone() |
|
| 40 | { |
|
| 41 | $this->it = clone $this->it; |
|
| 42 | } |
|
| 43 | ||
| 44 | public function rewind() |
|
| 45 | { |
|
| 46 | $it = $this->it; |
|
| 47 | $start = $this->start; |
|
| 48 | $len = $this->len; |
|
| 49 | $it->rewind(); |
|
| 50 | $this->currentLen = $len; |
|
| 51 | while ($start !== 0 && $it->valid()) { |
|
| 52 | $it->next(); |
|
| 53 | --$start; |
|
| 54 | } |
|
| 55 | } |
|
| 56 | ||
| 57 | public function valid() |
|
| 58 | { |
|
| 59 | return $this->it->valid() && $this->currentLen !== 0; |
|
| 60 | } |
|
| 61 | ||
| 62 | public function next() |
|
| 63 | { |
|
| 64 | $this->it->next(); |
|
| 65 | if ($this->currentLen !== 0) { |
|
| 66 | --$this->currentLen; |
|
| 67 | } |
|
| 68 | } |
|
| 69 | ||
| 70 | public function key() |
|
| 71 | { |
|
| 72 | return $this->it->key(); |
|
| 73 | } |
|
| 74 | ||
| 75 | public function current() |
|
| 76 | { |
|
| 77 | return $this->it->current(); |
|
| 78 | } |
|
| 79 | } |
|
| @@ 7-81 (lines=75) @@ | ||
| 4 | ||
| 5 | use Collections\KeyedIterator; |
|
| 6 | ||
| 7 | class LazySliceKeyedIterator implements KeyedIterator |
|
| 8 | { |
|
| 9 | /** |
|
| 10 | * @var KeyedIterator |
|
| 11 | */ |
|
| 12 | private $it; |
|
| 13 | ||
| 14 | /** |
|
| 15 | * @var int |
|
| 16 | */ |
|
| 17 | private $start; |
|
| 18 | ||
| 19 | /** |
|
| 20 | * @var int |
|
| 21 | */ |
|
| 22 | private $len; |
|
| 23 | ||
| 24 | /** |
|
| 25 | * @var int |
|
| 26 | */ |
|
| 27 | private $currentLen; |
|
| 28 | ||
| 29 | public function __construct($it, $start, $len) |
|
| 30 | { |
|
| 31 | $this->it = $it; |
|
| 32 | $this->start = $start; |
|
| 33 | $this->len = $len; |
|
| 34 | $this->currentLen = $len; |
|
| 35 | while ($start !== 0 && $it->valid()) { |
|
| 36 | $it->next(); |
|
| 37 | --$start; |
|
| 38 | } |
|
| 39 | } |
|
| 40 | ||
| 41 | public function __clone() |
|
| 42 | { |
|
| 43 | $this->it = clone $this->it; |
|
| 44 | } |
|
| 45 | ||
| 46 | public function rewind() |
|
| 47 | { |
|
| 48 | $it = $this->it; |
|
| 49 | $start = $this->start; |
|
| 50 | $len = $this->len; |
|
| 51 | $it->rewind(); |
|
| 52 | $this->currentLen = $len; |
|
| 53 | while ($start !== 0 && $it->valid()) { |
|
| 54 | $it->next(); |
|
| 55 | --$start; |
|
| 56 | } |
|
| 57 | } |
|
| 58 | ||
| 59 | public function valid() |
|
| 60 | { |
|
| 61 | return $this->it->valid() && $this->currentLen !== 0; |
|
| 62 | } |
|
| 63 | ||
| 64 | public function next() |
|
| 65 | { |
|
| 66 | $this->it->next(); |
|
| 67 | if ($this->currentLen !== 0) { |
|
| 68 | --$this->currentLen; |
|
| 69 | } |
|
| 70 | } |
|
| 71 | ||
| 72 | public function key() |
|
| 73 | { |
|
| 74 | return $this->it->key(); |
|
| 75 | } |
|
| 76 | ||
| 77 | public function current() |
|
| 78 | { |
|
| 79 | return $this->it->current(); |
|
| 80 | } |
|
| 81 | } |
|
| 82 | ||