|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace itertools; |
|
4
|
|
|
|
|
5
|
|
|
use IteratorIterator; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class LookAheadIterator extends IteratorIterator |
|
9
|
|
|
{ |
|
10
|
|
|
protected $lookAheads; |
|
11
|
|
|
protected $skipAutoRewind; |
|
12
|
|
|
protected $skipNextNormalRewind; |
|
13
|
|
|
|
|
14
|
|
|
public static function newInstance($innerIterator) |
|
15
|
|
|
{ |
|
16
|
|
|
if($innerIterator instanceof self) { |
|
17
|
|
|
return $innerIterator; |
|
18
|
|
|
} |
|
19
|
|
|
return new self($innerIterator); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function __construct($innerIterator) |
|
23
|
|
|
{ |
|
24
|
|
|
parent::__construct(IterUtil::asIterator($innerIterator)); |
|
25
|
|
|
$this->lookAheads = new Queue(); |
|
26
|
|
|
$this->skipAutoRewind = false; |
|
27
|
|
|
$this->skipNextNormalRewind = false; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function next() |
|
31
|
|
|
{ |
|
32
|
|
|
if($this->lookAheads->isEmpty()) { |
|
33
|
|
|
return parent::next(); |
|
34
|
|
|
} |
|
35
|
|
|
$this->lookAheads->shift(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function valid() |
|
39
|
|
|
{ |
|
40
|
|
|
if(! $this->lookAheads->isEmpty()) { |
|
41
|
|
|
return true; |
|
42
|
|
|
} |
|
43
|
|
|
return parent::valid(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function key() |
|
47
|
|
|
{ |
|
48
|
|
|
if(! $this->lookAheads->isEmpty()) { |
|
49
|
|
|
return $this->lookAheads->bottom()->key; |
|
50
|
|
|
} |
|
51
|
|
|
return parent::key(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function current() |
|
55
|
|
|
{ |
|
56
|
|
|
if(! $this->lookAheads->isEmpty()) { |
|
57
|
|
|
return $this->lookAheads->bottom()->value; |
|
58
|
|
|
} |
|
59
|
|
|
return parent::current(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function prefetchUpTo($prefetchCount) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->autoRewind(); |
|
65
|
|
|
$prefetchCount = max(0, $prefetchCount - $this->lookAheads->count()); |
|
66
|
|
|
for($i = 0; $i < $prefetchCount; $i += 1) { |
|
67
|
|
|
if(! parent::valid()) { |
|
|
|
|
|
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
$this->lookAheads->push((object) array('value' => parent::current(), 'key' => parent::key())); |
|
|
|
|
|
|
71
|
|
|
parent::next(); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function getNext($n = 1) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->prefetchUpTo($n + 1); |
|
78
|
|
|
return $this->lookAheads->offsetGet($n)->value; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function hasAny() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->hasNext(0); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getNextKey($n = 1) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->prefetchUpTo($n + 1); |
|
89
|
|
|
return $this->lookAheads->offsetGet($n)->key; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function hasNext($n = 1) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->prefetchUpTo($n + 1); |
|
95
|
|
|
return $this->lookAheads->count() > $n; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function rewind() |
|
99
|
|
|
{ |
|
100
|
|
|
if($this->skipNextNormalRewind) { |
|
101
|
|
|
$this->skipNextNormalRewind = false; |
|
102
|
|
|
return; |
|
103
|
|
|
} |
|
104
|
|
|
parent::rewind(); |
|
105
|
|
|
$this->skipAutoRewind = true; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
protected function autoRewind() |
|
109
|
|
|
{ |
|
110
|
|
|
if(! $this->skipAutoRewind) { |
|
111
|
|
|
parent::rewind(); |
|
|
|
|
|
|
112
|
|
|
$this->skipNextNormalRewind = true; |
|
113
|
|
|
$this->skipAutoRewind = true; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.