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