| @@ 5-60 (lines=56) @@ | ||
| 2 | ||
| 3 | namespace Collections\Iterator; |
|
| 4 | ||
| 5 | class LazyTakeIterator implements \Iterator |
|
| 6 | { |
|
| 7 | /** |
|
| 8 | * @var \Iterator |
|
| 9 | */ |
|
| 10 | private $it; |
|
| 11 | ||
| 12 | /** |
|
| 13 | * @var int |
|
| 14 | */ |
|
| 15 | private $n; |
|
| 16 | ||
| 17 | /** |
|
| 18 | * @var int |
|
| 19 | */ |
|
| 20 | private $numLeft; |
|
| 21 | ||
| 22 | public function __construct($it, $n) |
|
| 23 | { |
|
| 24 | $this->it = $it; |
|
| 25 | $this->n = $n; |
|
| 26 | $this->numLeft = $n; |
|
| 27 | } |
|
| 28 | ||
| 29 | public function __clone() |
|
| 30 | { |
|
| 31 | $this->it = clone $this->it; |
|
| 32 | } |
|
| 33 | ||
| 34 | public function rewind() |
|
| 35 | { |
|
| 36 | $this->it->rewind(); |
|
| 37 | $this->numLeft = $this->n; |
|
| 38 | } |
|
| 39 | ||
| 40 | public function valid() |
|
| 41 | { |
|
| 42 | return ($this->numLeft > 0 && $this->it->valid()); |
|
| 43 | } |
|
| 44 | ||
| 45 | public function next() |
|
| 46 | { |
|
| 47 | $this->it->next(); |
|
| 48 | --$this->numLeft; |
|
| 49 | } |
|
| 50 | ||
| 51 | public function key() |
|
| 52 | { |
|
| 53 | return $this->it->key(); |
|
| 54 | } |
|
| 55 | ||
| 56 | public function current() |
|
| 57 | { |
|
| 58 | return $this->it->current(); |
|
| 59 | } |
|
| 60 | } |
|
| 61 | ||
| @@ 7-62 (lines=56) @@ | ||
| 4 | ||
| 5 | use Collections\KeyedIterator; |
|
| 6 | ||
| 7 | class LazyTakeKeyedIterator implements KeyedIterator |
|
| 8 | { |
|
| 9 | /** |
|
| 10 | * @var KeyedIterator |
|
| 11 | */ |
|
| 12 | private $it; |
|
| 13 | ||
| 14 | /** |
|
| 15 | * @var int |
|
| 16 | */ |
|
| 17 | private $n; |
|
| 18 | ||
| 19 | /** |
|
| 20 | * @var int |
|
| 21 | */ |
|
| 22 | private $numLeft; |
|
| 23 | ||
| 24 | public function __construct($it, $n) |
|
| 25 | { |
|
| 26 | $this->it = $it; |
|
| 27 | $this->n = $n; |
|
| 28 | $this->numLeft = $n; |
|
| 29 | } |
|
| 30 | ||
| 31 | public function __clone() |
|
| 32 | { |
|
| 33 | $this->it = clone $this->it; |
|
| 34 | } |
|
| 35 | ||
| 36 | public function rewind() |
|
| 37 | { |
|
| 38 | $this->it->rewind(); |
|
| 39 | $this->numLeft = $this->n; |
|
| 40 | } |
|
| 41 | ||
| 42 | public function valid() |
|
| 43 | { |
|
| 44 | return ($this->numLeft > 0 && $this->it->valid()); |
|
| 45 | } |
|
| 46 | ||
| 47 | public function next() |
|
| 48 | { |
|
| 49 | $this->it->next(); |
|
| 50 | --$this->numLeft; |
|
| 51 | } |
|
| 52 | ||
| 53 | public function key() |
|
| 54 | { |
|
| 55 | return $this->it->key(); |
|
| 56 | } |
|
| 57 | ||
| 58 | public function current() |
|
| 59 | { |
|
| 60 | return $this->it->current(); |
|
| 61 | } |
|
| 62 | } |
|