1 | <?php |
||
2 | declare(strict_types = 1); |
||
3 | |||
4 | namespace Innmind\Immutable\Map; |
||
5 | |||
6 | use Innmind\Immutable\{ |
||
7 | Map, |
||
8 | Str, |
||
9 | Sequence, |
||
10 | Set, |
||
11 | Pair, |
||
12 | Maybe, |
||
13 | SideEffect, |
||
14 | }; |
||
15 | |||
16 | /** |
||
17 | * @template T |
||
18 | * @template S |
||
19 | * @psalm-immutable |
||
20 | */ |
||
21 | final class DoubleIndex implements Implementation |
||
22 | { |
||
23 | /** @var Sequence\Implementation<Pair<T, S>> */ |
||
24 | private Sequence\Implementation $pairs; |
||
25 | |||
26 | /** |
||
27 | * @param Sequence\Implementation<Pair<T, S>> $pairs |
||
28 | */ |
||
29 | public function __construct(Sequence\Implementation $pairs = null) |
||
30 | { |
||
31 | /** @var Sequence\Implementation<Pair<T, S>> */ |
||
32 | $this->pairs = $pairs ?? new Sequence\Primitive; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
33 | } |
||
34 | |||
35 | /** |
||
36 | 84 | * @param T $key |
|
0 ignored issues
–
show
The type
Innmind\Immutable\Map\T was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
37 | * @param S $value |
||
0 ignored issues
–
show
The type
Innmind\Immutable\Map\S was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
38 | 84 | * |
|
39 | 84 | * @return self<T, S> |
|
40 | 84 | */ |
|
41 | 84 | public function __invoke($key, $value): self |
|
42 | 84 | { |
|
43 | 84 | $map = $this->remove($key); |
|
44 | |||
45 | 84 | return new self(($map->pairs)(new Pair($key, $value))); |
|
46 | 84 | } |
|
47 | |||
48 | 16 | /** |
|
49 | * @template A |
||
50 | 16 | * @template B |
|
51 | * @psalm-pure |
||
52 | * |
||
53 | 16 | * @param A $key |
|
0 ignored issues
–
show
The type
Innmind\Immutable\Map\A was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
54 | * @param B $value |
||
0 ignored issues
–
show
The type
Innmind\Immutable\Map\B was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
55 | 16 | * |
|
56 | * @return self<A, B> |
||
57 | */ |
||
58 | 4 | public static function of($key, $value): self |
|
59 | { |
||
60 | 4 | /** @psalm-suppress MixedArgumentTypeCoercion */ |
|
61 | return new self(new Sequence\Primitive([new Pair($key, $value)])); |
||
62 | } |
||
63 | 6 | ||
64 | public function size(): int |
||
65 | 6 | { |
|
66 | return $this->pairs->size(); |
||
67 | } |
||
68 | |||
69 | public function count(): int |
||
70 | { |
||
71 | return $this->size(); |
||
72 | } |
||
73 | |||
74 | 35 | /** |
|
75 | * @param T $key |
||
76 | 35 | * |
|
77 | 34 | * @return Maybe<S> |
|
78 | */ |
||
79 | 33 | public function get($key): Maybe |
|
80 | { |
||
81 | 33 | return $this |
|
82 | 3 | ->pairs |
|
83 | 3 | ->find(static fn($pair) => $pair->key() === $key) |
|
84 | 3 | ->map(static fn($pair) => $pair->value()); |
|
85 | 3 | } |
|
86 | |||
87 | 3 | /** |
|
88 | 3 | * @param T $key |
|
89 | 3 | */ |
|
90 | public function contains($key): bool |
||
91 | { |
||
92 | 33 | return $this |
|
0 ignored issues
–
show
|
|||
93 | 33 | ->pairs |
|
94 | ->find(static fn($pair) => $pair->key() === $key) |
||
95 | 33 | ->match( |
|
96 | static fn() => true, |
||
97 | static fn() => false, |
||
98 | 33 | ); |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * @return self<T, S> |
||
103 | */ |
||
104 | public function clear(): self |
||
105 | { |
||
106 | return new self($this->pairs->clear()); |
||
107 | } |
||
108 | 18 | ||
109 | /** |
||
110 | 18 | * @param Implementation<T, S> $map |
|
111 | 1 | */ |
|
112 | public function equals(Implementation $map): bool |
||
113 | { |
||
114 | 17 | if (!$map->keys()->equals($this->keys())) { |
|
115 | 17 | return false; |
|
116 | } |
||
117 | |||
118 | foreach ($this->pairs->iterator() as $pair) { |
||
119 | $equals = $map |
||
120 | ->get($pair->key()) |
||
121 | ->filter(static fn($value) => $value === $pair->value()) |
||
122 | 3 | ->match( |
|
123 | static fn() => true, |
||
124 | 3 | static fn() => false, |
|
125 | ); |
||
126 | |||
127 | if (!$equals) { |
||
128 | return false; |
||
129 | } |
||
130 | 5 | } |
|
131 | |||
132 | 5 | return true; |
|
133 | 5 | } |
|
134 | 5 | ||
135 | 5 | /** |
|
136 | * @param callable(T, S): bool $predicate |
||
137 | 5 | * |
|
138 | * @return self<T, S> |
||
139 | */ |
||
140 | public function filter(callable $predicate): self |
||
141 | { |
||
142 | return new self($this->pairs->filter( |
||
143 | 2 | static fn($pair) => $predicate($pair->key(), $pair->value()), |
|
144 | )); |
||
145 | 2 | } |
|
146 | 1 | ||
147 | /** |
||
148 | * @param callable(T, S): void $function |
||
149 | 2 | */ |
|
150 | 2 | public function foreach(callable $function): SideEffect |
|
151 | 2 | { |
|
152 | foreach ($this->pairs->iterator() as $pair) { |
||
153 | $function($pair->key(), $pair->value()); |
||
154 | } |
||
155 | 1 | ||
156 | return new SideEffect; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @template D |
||
161 | * |
||
162 | * @param callable(T, S): D $discriminator |
||
163 | 1 | * |
|
164 | * @return Map<D, Map<T, S>> |
||
165 | 1 | */ |
|
166 | public function groupBy(callable $discriminator): Map |
||
167 | 1 | { |
|
168 | 1 | /** @var Map<D, Map<T, S>> */ |
|
169 | $groups = Map::of(); |
||
170 | 1 | ||
171 | foreach ($this->pairs->iterator() as $pair) { |
||
172 | 1 | $key = $discriminator($pair->key(), $pair->value()); |
|
173 | |||
174 | $group = $groups->get($key)->match( |
||
175 | static fn($group) => $group, |
||
176 | fn() => $this->clearMap(), |
||
177 | 1 | ); |
|
178 | $groups = ($groups)($key, ($group)($pair->key(), $pair->value())); |
||
179 | } |
||
180 | |||
181 | 1 | /** @var Map<D, Map<T, S>> */ |
|
182 | return $groups; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @return Set<T> |
||
187 | 1 | */ |
|
188 | public function keys(): Set |
||
189 | 1 | { |
|
190 | 1 | return $this |
|
191 | ->pairs |
||
192 | 1 | ->map(static fn($pair) => $pair->key()) |
|
193 | ->toSet(); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @return Sequence<S> |
||
198 | */ |
||
199 | public function values(): Sequence |
||
200 | { |
||
201 | return $this |
||
202 | 2 | ->pairs |
|
203 | ->map(static fn($pair) => $pair->value()) |
||
204 | 2 | ->toSequence(); |
|
205 | 1 | } |
|
206 | |||
207 | /** |
||
208 | 1 | * @template B |
|
209 | * |
||
210 | 1 | * @param callable(T, S): B $function |
|
211 | 1 | * |
|
212 | * @return self<T, B> |
||
213 | 1 | */ |
|
214 | public function map(callable $function): self |
||
215 | 1 | { |
|
216 | 1 | return new self($this->pairs->map(static fn($pair) => new Pair( |
|
217 | 1 | $pair->key(), |
|
218 | $function($pair->key(), $pair->value()), |
||
219 | ))); |
||
220 | } |
||
221 | 1 | ||
222 | /** |
||
223 | 1 | * @param T $key |
|
224 | * |
||
225 | 1 | * @return self<T, S> |
|
226 | */ |
||
227 | 1 | public function remove($key): self |
|
228 | { |
||
229 | return new self($this->pairs->filter( |
||
230 | 1 | static fn($pair) => $pair->key() !== $key, |
|
231 | )); |
||
232 | 1 | } |
|
233 | |||
234 | /** |
||
235 | * @param Implementation<T, S> $map |
||
236 | * |
||
237 | 1 | * @return self<T, S> |
|
238 | */ |
||
239 | public function merge(Implementation $map): self |
||
240 | { |
||
241 | /** @psalm-suppress MixedArgument For some reason it no longer recognize templates for $key and $value */ |
||
242 | return $map->reduce( |
||
243 | 10 | $this, |
|
244 | static fn(self $carry, $key, $value): self => ($carry)($key, $value), |
||
245 | 10 | ); |
|
246 | } |
||
247 | |||
248 | /** |
||
249 | * @param callable(T, S): bool $predicate |
||
250 | * |
||
251 | 5 | * @return Map<bool, Map<T, S>> |
|
252 | */ |
||
253 | 5 | public function partition(callable $predicate): Map |
|
254 | { |
||
255 | $truthy = $this->clearMap(); |
||
256 | $falsy = $this->clearMap(); |
||
257 | |||
258 | foreach ($this->pairs->iterator() as $pair) { |
||
259 | $key = $pair->key(); |
||
260 | $value = $pair->value(); |
||
261 | 3 | ||
262 | $return = $predicate($key, $value); |
||
263 | 3 | ||
264 | if ($return === true) { |
||
265 | 3 | $truthy = ($truthy)($key, $value); |
|
266 | 3 | } else { |
|
267 | $falsy = ($falsy)($key, $value); |
||
268 | 3 | } |
|
269 | } |
||
270 | 2 | ||
271 | return Map::of([true, $truthy], [false, $falsy]); |
||
272 | 2 | } |
|
273 | |||
274 | 2 | /** |
|
275 | 2 | * @template R |
|
276 | * @param R $carry |
||
0 ignored issues
–
show
The type
Innmind\Immutable\Map\R was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
277 | * @param callable(R, T, S): R $reducer |
||
278 | 3 | * |
|
279 | * @return R |
||
280 | */ |
||
281 | 1 | public function reduce($carry, callable $reducer) |
|
282 | { |
||
283 | foreach ($this->pairs->iterator() as $pair) { |
||
284 | $carry = $reducer($carry, $pair->key(), $pair->value()); |
||
285 | } |
||
286 | |||
287 | return $carry; |
||
288 | } |
||
289 | 1 | ||
290 | public function empty(): bool |
||
291 | 1 | { |
|
292 | 1 | return $this->pairs->empty(); |
|
293 | } |
||
294 | |||
295 | 1 | public function find(callable $predicate): Maybe |
|
296 | 1 | { |
|
297 | 1 | return $this->pairs->find(static fn($pair) => $predicate($pair->key(), $pair->value())); |
|
298 | 1 | } |
|
299 | 1 | ||
300 | 1 | /** |
|
301 | 1 | * @return Map<T, S> |
|
302 | 1 | */ |
|
303 | 1 | private function clearMap(): Map |
|
304 | 1 | { |
|
305 | return Map::of(); |
||
306 | 1 | } |
|
307 | } |
||
308 |