-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathIterator.php
241 lines (204 loc) · 5.31 KB
/
Iterator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
declare(strict_types=1);
namespace Psl\Iter;
use Closure;
use Countable;
use Generator;
use SeekableIterator;
use function array_key_exists;
/**
* @template Tk
* @template Tv
*
* @implements SeekableIterator<Tk, Tv>
*/
final class Iterator implements Countable, SeekableIterator
{
/**
* @var null|Generator<Tk, Tv, mixed, mixed>
*/
private ?Generator $generator;
/**
* @var array<int, array{0: Tk, 1: Tv}>
*/
private array $entries = [];
/**
* Whether or not the current value/key pair has been added to the local entries.
*/
private bool $saved = true;
/**
* Current cursor position for the local entries.
*/
private int $position = 0;
/**
* The size of the generator.
*
* @var null|int<0, max>
*/
private ?int $count = null;
/**
* @param Generator<Tk, Tv, mixed, mixed> $generator
*/
public function __construct(Generator $generator)
{
$this->generator = $generator;
}
/**
* Create an iterator from a factory.
*
* @template Tsk
* @template Tsv
*
* @param (Closure(): iterable<Tsk, Tsv>) $factory
*
* @return Iterator<Tsk, Tsv>
*/
public static function from(Closure $factory): Iterator
{
return self::create($factory());
}
/**
* Create an iterator from an iterable.
*
* @psalm-suppress DocblockTypeContradiction - Psalm gets lost on Generator, which is a perfectly valid iterable type.
*
* @template Tsk
* @template Tsv
*
* @param iterable<Tsk, Tsv> $iterable
*
* @return Iterator<Tsk, Tsv>
*/
public static function create(iterable $iterable): Iterator
{
if ($iterable instanceof Generator) {
return new self($iterable);
}
/**
* @var (Closure(): Generator<Tsk, Tsv, mixed, void>) $factory
*/
$factory = static fn(): Generator => yield from $iterable;
return new self($factory());
}
/**
* Return the current element.
*
* @return Tv
*/
public function current(): mixed
{
$this->save();
return $this->entries[$this->position][1];
}
/**
* Checks if current position is valid.
*/
public function valid(): bool
{
if (array_key_exists($this->position, $this->entries)) {
return true;
}
if (null !== $this->generator && $this->generator->valid()) {
return true;
}
$this->generator = null;
return false;
}
/**
* @psalm-suppress PossiblyNullReference
*/
private function save(): void
{
if ($this->generator) {
if ($this->entries === []) {
$this->saved = false;
}
if (!$this->saved && $this->generator->valid()) {
$this->saved = true;
$this->entries[] = [$this->generator->key(), $this->generator->current()];
}
}
}
/**
* Return the key of the current element.
*
* @return Tk
*/
public function key(): mixed
{
$this->save();
return $this->entries[$this->position][0];
}
/**
* Rewind the Iterator to the first element.
*/
public function rewind(): void
{
$this->position = 0;
}
/**
* Seek to the given position.
*
* @param int<0, max> $position
*
* @throws Exception\OutOfBoundsException If $position is out-of-bounds.
*/
public function seek(int $position): void
{
if ($position <= $this->position) {
$this->position = $position;
return;
}
if ($this->generator) {
do {
$this->save();
$this->next();
/** @psalm-suppress PossiblyNullReference - ->next() and ->save() don't mutate ->generator. */
if (!$this->generator->valid()) {
$this->generator = null;
throw new Exception\OutOfBoundsException('Position is out-of-bounds.');
}
} while ($this->position < $position);
return;
}
if ($position >= $this->count()) {
throw new Exception\OutOfBoundsException('Position is out-of-bounds.');
}
$this->position = $position;
}
/**
* Move forward to the next element.
*/
public function next(): void
{
$this->position++;
if (array_key_exists($this->position, $this->entries) || null === $this->generator || !$this->generator->valid()) {
return;
}
$this->generator->next();
$this->saved = false;
}
/**
* @return int<0, max>
*
* @psalm-suppress PossiblyNullReference
*/
public function count(): int
{
if ($this->generator) {
$previous = $this->position;
do {
$this->save();
$this->next();
} while ($this->generator->valid());
$this->position = $previous;
$this->generator = null;
}
if (null !== $this->count) {
return $this->count;
}
/** @var int<0, max> */
$this->count = count($this->entries);
return $this->count;
}
}