-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAccumulate.php
149 lines (128 loc) · 3.83 KB
/
Accumulate.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
<?php
declare(strict_types = 1);
namespace Innmind\Immutable;
/**
* Simple iterator to cache the results of a generator so it can be iterated
* over multiple times
*
* @template S
* @implements \Iterator<S>
* @internal Do not use this in your code
* @psalm-immutable Not really immutable but to simplify declaring immutability of other structures
*/
final class Accumulate implements \Iterator
{
/** @var \Generator<S> */
private \Generator $generator;
/** @var list<S> */
private array $values = [];
/** @var list<int<0, max>> */
private array $cursors = [];
private bool $started = false;
/**
* @param \Generator<S> $generator
*/
public function __construct(\Generator $generator)
{
$this->generator = $generator;
}
/**
* @return S
*/
#[\Override]
public function current(): mixed
{
/** @psalm-suppress InaccessibleProperty */
$this->started = true;
/** @psalm-suppress UnusedMethodCall */
$this->pop();
/** @var S */
return \current($this->values);
}
/**
* @return int<0, max>|null
*/
#[\Override]
public function key(): ?int
{
/** @psalm-suppress InaccessibleProperty */
$this->started = true;
/** @psalm-suppress UnusedMethodCall */
$this->pop();
return \key($this->values);
}
#[\Override]
public function next(): void
{
/** @psalm-suppress InaccessibleProperty */
$this->started = true;
/** @psalm-suppress InaccessibleProperty */
\next($this->values);
if ($this->reachedCacheEnd()) {
/** @psalm-suppress ImpureMethodCall */
$this->generator->next();
}
}
#[\Override]
public function rewind(): void
{
if ($this->started && !\is_null($key = $this->key())) {
/** @psalm-suppress InaccessibleProperty */
$this->cursors[] = $key;
}
/** @psalm-suppress InaccessibleProperty */
$this->started = true;
/** @psalm-suppress InaccessibleProperty */
\reset($this->values);
}
#[\Override]
public function valid(): bool
{
/** @psalm-suppress InaccessibleProperty */
$this->started = true;
/** @psalm-suppress ImpureMethodCall */
$valid = !$this->reachedCacheEnd() || $this->generator->valid();
if (!$valid) {
// once the "true" end has been reached we automatically rewind this
// iterator so it is always in a clean state
$this->cleanup();
}
return $valid;
}
public function started(): bool
{
return $this->started;
}
public function cleanup(): void
{
/** @psalm-suppress InaccessibleProperty */
$previousCursor = \array_pop($this->cursors);
if (\is_null($previousCursor)) {
return;
}
// Re-position the cursor to the previous position before entering a new
// loop. It only iterate over the cached values because the previous
// cursor must be in the cache.
/** @psalm-suppress InaccessibleProperty */
\reset($this->values);
while (\is_int(\key($this->values)) && \key($this->values) !== $previousCursor) {
/** @psalm-suppress InaccessibleProperty */
\next($this->values);
}
}
private function reachedCacheEnd(): bool
{
return \key($this->values) === null;
}
private function pop(): void
{
/** @psalm-suppress ImpureMethodCall */
if ($this->reachedCacheEnd() && $this->generator->valid()) {
/**
* @psalm-suppress InaccessibleProperty
* @psalm-suppress ImpureMethodCall
*/
$this->values[] = $this->generator->current();
}
}
}