1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ds; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use Countable; |
7
|
|
|
use Ds; |
8
|
|
|
use Error; |
9
|
|
|
use IteratorAggregate; |
10
|
|
|
use JsonSerializable; |
11
|
|
|
use OutOfBoundsException; |
12
|
|
|
use OutOfRangeException; |
13
|
|
|
use Traversable; |
14
|
|
|
use UnderflowException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Collection is the base interface which covers functionality common to all the |
18
|
|
|
* data structures in this library. It guarantees that all structures are |
19
|
|
|
* traversable, countable, and can be converted to json using json_encode(). |
20
|
|
|
* |
21
|
|
|
* @package Ds |
22
|
|
|
*/ |
23
|
|
|
interface Collection extends Traversable, Countable, JsonSerializable |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Removes all values from the collection. |
27
|
|
|
*/ |
28
|
|
|
public function clear(); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Returns the size of the collection. |
32
|
|
|
* |
33
|
|
|
* @return int |
34
|
|
|
*/ |
35
|
|
|
public function count(): int; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Returns a shallow copy of the collection. |
39
|
|
|
* |
40
|
|
|
* @return Collection a copy of the collection. |
41
|
|
|
*/ |
42
|
|
|
public function copy(): Collection; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns whether the collection is empty. |
46
|
|
|
* |
47
|
|
|
* This should be equivalent to a count of zero, but is not required. |
48
|
|
|
* Implementations should define what empty means in their own context. |
49
|
|
|
* |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
public function isEmpty(): bool; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns an array representation of the collection. |
56
|
|
|
* |
57
|
|
|
* The format of the returned array is implementation-dependent. |
58
|
|
|
* Some implementations may throw an exception if an array representation |
59
|
|
|
* could not be created. |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public function toArray(): array; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* A Deque (pronounced "deck") is a sequence of values in a contiguous buffer |
68
|
|
|
* that grows and shrinks automatically. The name is a common abbreviation of |
69
|
|
|
* "double-ended queue". |
70
|
|
|
* |
71
|
|
|
* While a Deque is very similar to a Vector, it offers constant time operations |
72
|
|
|
* at both ends of the buffer, ie. shift, unshift, push and pop are all O(1). |
73
|
|
|
* |
74
|
|
|
* @package Ds |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
final class Deque implements IteratorAggregate, ArrayAccess, Sequence |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
const MIN_CAPACITY = 8; |
79
|
|
|
|
80
|
|
|
// BEGIN GenericCollection Trait |
81
|
|
|
/** |
82
|
|
|
* Returns whether the collection is empty. |
83
|
|
|
* |
84
|
|
|
* This should be equivalent to a count of zero, but is not required. |
85
|
|
|
* Implementations should define what empty means in their own context. |
86
|
|
|
* |
87
|
|
|
* @return bool whether the collection is empty. |
88
|
|
|
*/ |
89
|
|
|
public function isEmpty(): bool |
90
|
|
|
{ |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Returns a representation that can be natively converted to JSON, which is |
95
|
|
|
* called when invoking json_encode. |
96
|
|
|
* |
97
|
|
|
* @return mixed the data to be JSON encoded. |
98
|
|
|
* |
99
|
|
|
* @see JsonSerializable |
100
|
|
|
*/ |
101
|
|
|
public function jsonSerialize() |
102
|
|
|
{ |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Creates a shallow copy of the collection. |
107
|
|
|
* |
108
|
|
|
* @return Collection a shallow copy of the collection. |
109
|
|
|
*/ |
110
|
|
|
public function copy(): Collection |
111
|
|
|
{ |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Invoked when calling var_dump. |
116
|
|
|
* |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
public function __debugInfo(): array |
120
|
|
|
{ |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Returns a string representation of the collection, which is invoked when |
125
|
|
|
* the collection is converted to a string. |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
public function __toString(): string |
130
|
|
|
{ |
131
|
|
|
} |
132
|
|
|
// END GenericCollection Trait |
133
|
|
|
|
134
|
|
|
// BEGIN GenericSequence Trait |
135
|
|
|
/** |
136
|
|
|
* @inheritDoc |
137
|
|
|
*/ |
138
|
|
|
public function __construct($values = null) |
139
|
|
|
{ |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns an array representation of the collection. |
144
|
|
|
* |
145
|
|
|
* The format of the returned array is implementation-dependent. Some |
146
|
|
|
* implementations may throw an exception if an array representation |
147
|
|
|
* could not be created (for example when object are used as keys). |
148
|
|
|
* |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
|
public function toArray(): array |
152
|
|
|
{ |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @inheritdoc |
157
|
|
|
*/ |
158
|
|
|
public function apply(callable $callback) |
159
|
|
|
{ |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @inheritdoc |
164
|
|
|
*/ |
165
|
|
|
public function merge($values): Sequence |
166
|
|
|
{ |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @inheritdoc |
171
|
|
|
*/ |
172
|
|
|
public function count(): int |
173
|
|
|
{ |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @inheritDoc |
178
|
|
|
*/ |
179
|
|
|
public function contains(...$values): bool |
180
|
|
|
{ |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @inheritDoc |
185
|
|
|
*/ |
186
|
|
|
public function filter(callable $callback = null): Sequence |
187
|
|
|
{ |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @inheritDoc |
192
|
|
|
*/ |
193
|
|
|
public function find($value) |
194
|
|
|
{ |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @inheritDoc |
199
|
|
|
*/ |
200
|
|
|
public function first() |
201
|
|
|
{ |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @inheritDoc |
206
|
|
|
*/ |
207
|
|
|
public function get(int $index) |
208
|
|
|
{ |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @inheritDoc |
213
|
|
|
*/ |
214
|
|
|
public function insert(int $index, ...$values) |
215
|
|
|
{ |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @inheritDoc |
220
|
|
|
*/ |
221
|
|
|
public function join(string $glue = null): string |
222
|
|
|
{ |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @inheritDoc |
227
|
|
|
*/ |
228
|
|
|
public function last() |
229
|
|
|
{ |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @inheritDoc |
234
|
|
|
*/ |
235
|
|
|
public function map(callable $callback): Sequence |
236
|
|
|
{ |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @inheritDoc |
241
|
|
|
*/ |
242
|
|
|
public function pop() |
243
|
|
|
{ |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @inheritDoc |
248
|
|
|
*/ |
249
|
|
|
public function push(...$values) |
250
|
|
|
{ |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @inheritDoc |
255
|
|
|
*/ |
256
|
|
|
public function reduce(callable $callback, $initial = null) |
257
|
|
|
{ |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @inheritDoc |
262
|
|
|
*/ |
263
|
|
|
public function remove(int $index) |
264
|
|
|
{ |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @inheritDoc |
269
|
|
|
*/ |
270
|
|
|
public function reverse() |
271
|
|
|
{ |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @inheritDoc |
276
|
|
|
*/ |
277
|
|
|
public function reversed(): Sequence |
278
|
|
|
{ |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @inheritDoc |
283
|
|
|
*/ |
284
|
|
|
public function rotate(int $rotations) |
285
|
|
|
{ |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @inheritDoc |
290
|
|
|
*/ |
291
|
|
|
public function set(int $index, $value) |
292
|
|
|
{ |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @inheritDoc |
297
|
|
|
*/ |
298
|
|
|
public function shift() |
299
|
|
|
{ |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @inheritDoc |
304
|
|
|
*/ |
305
|
|
|
public function slice(int $offset, int $length = null): Sequence |
306
|
|
|
{ |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @inheritDoc |
311
|
|
|
*/ |
312
|
|
|
public function sort(callable $comparator = null) |
313
|
|
|
{ |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @inheritDoc |
318
|
|
|
*/ |
319
|
|
|
public function sorted(callable $comparator = null): Sequence |
320
|
|
|
{ |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @inheritDoc |
325
|
|
|
*/ |
326
|
|
|
public function sum() |
327
|
|
|
{ |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @inheritDoc |
332
|
|
|
*/ |
333
|
|
|
public function unshift(...$values) |
334
|
|
|
{ |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* |
339
|
|
|
*/ |
340
|
|
|
public function getIterator() |
341
|
|
|
{ |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* @inheritdoc |
346
|
|
|
*/ |
347
|
|
|
public function clear() |
348
|
|
|
{ |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @inheritdoc |
353
|
|
|
*/ |
354
|
|
|
public function offsetSet($offset, $value) |
355
|
|
|
{ |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* @inheritdoc |
360
|
|
|
*/ |
361
|
|
|
public function &offsetGet($offset) |
362
|
|
|
{ |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* @inheritdoc |
367
|
|
|
*/ |
368
|
|
|
public function offsetUnset($offset) |
369
|
|
|
{ |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* @inheritdoc |
374
|
|
|
*/ |
375
|
|
|
public function offsetExists($offset) |
376
|
|
|
{ |
377
|
|
|
} |
378
|
|
|
// END GenericSequence Trait |
379
|
|
|
|
380
|
|
|
// BEGIN SquaredCapacityTrait |
381
|
|
|
// BEGIN Capacity Trait |
382
|
|
|
/** |
383
|
|
|
* Returns the current capacity. |
384
|
|
|
* |
385
|
|
|
* @return int |
386
|
|
|
*/ |
387
|
|
|
public function capacity(): int |
388
|
|
|
{ |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Ensures that enough memory is allocated for a specified capacity. This |
393
|
|
|
* potentially reduces the number of reallocations as the size increases. |
394
|
|
|
* |
395
|
|
|
* @param int $capacity The number of values for which capacity should be |
396
|
|
|
* allocated. Capacity will stay the same if this value |
397
|
|
|
* is less than or equal to the current capacity. |
398
|
|
|
*/ |
399
|
|
|
public function allocate(int $capacity) |
400
|
|
|
{ |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* @return float the structures growth factor. |
405
|
|
|
*/ |
406
|
|
|
protected function getGrowthFactor(): float |
407
|
|
|
{ |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* @return float to multiply by when decreasing capacity. |
412
|
|
|
*/ |
413
|
|
|
protected function getDecayFactor(): float |
414
|
|
|
{ |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Checks and adjusts capacity if required. |
419
|
|
|
*/ |
420
|
|
|
protected function checkCapacity() |
421
|
|
|
{ |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
/** |
425
|
|
|
* Called when capacity should be decrease if it drops below a threshold. |
426
|
|
|
*/ |
427
|
|
|
protected function decreaseCapacity() |
428
|
|
|
{ |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* @return bool whether capacity should be increased. |
433
|
|
|
*/ |
434
|
|
|
protected function shouldDecreaseCapacity(): bool |
435
|
|
|
{ |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* @return bool whether capacity should be increased. |
440
|
|
|
*/ |
441
|
|
|
protected function shouldIncreaseCapacity(): bool |
442
|
|
|
{ |
443
|
|
|
} |
444
|
|
|
// END Capacity Trait |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* Called when capacity should be increased to accommodate new values. |
448
|
|
|
*/ |
449
|
|
|
protected function increaseCapacity() |
450
|
|
|
{ |
451
|
|
|
} |
452
|
|
|
// END SquaredCapacityTrait |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* Hashable is an interface which allows objects to be used as keys. |
457
|
|
|
* |
458
|
|
|
* It’s an alternative to spl_object_hash(), which determines an object’s hash |
459
|
|
|
* based on its handle: this means that two objects that are considered equal |
460
|
|
|
* by an implicit definition would not treated as equal because they are not |
461
|
|
|
* the same instance. |
462
|
|
|
* |
463
|
|
|
* @package Ds |
464
|
|
|
*/ |
465
|
|
|
interface Hashable |
|
|
|
|
466
|
|
|
{ |
467
|
|
|
/** |
468
|
|
|
* Produces a scalar value to be used as the object's hash, which determines |
469
|
|
|
* where it goes in the hash table. While this value does not have to be |
470
|
|
|
* unique, objects which are equal must have the same hash value. |
471
|
|
|
* |
472
|
|
|
* @return mixed |
473
|
|
|
*/ |
474
|
|
|
public function hash(); |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* Determines if two objects should be considered equal. Both objects will |
478
|
|
|
* be instances of the same class but may not be the same instance. |
479
|
|
|
* |
480
|
|
|
* @param $obj self An instance of the same class to compare to. |
481
|
|
|
* |
482
|
|
|
* @return bool |
483
|
|
|
*/ |
484
|
|
|
public function equals($obj): bool; |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
/** |
488
|
|
|
* A Map is a sequential collection of key-value pairs, almost identical to an |
489
|
|
|
* array used in a similar context. Keys can be any type, but must be unique. |
490
|
|
|
* |
491
|
|
|
* @package Ds |
492
|
|
|
*/ |
493
|
|
|
final class Map implements IteratorAggregate, ArrayAccess, Collection |
|
|
|
|
494
|
|
|
{ |
495
|
|
|
const MIN_CAPACITY = 8; |
496
|
|
|
|
497
|
|
|
// BEGIN GenericCollection Trait |
498
|
|
|
/** |
499
|
|
|
* Returns whether the collection is empty. |
500
|
|
|
* |
501
|
|
|
* This should be equivalent to a count of zero, but is not required. |
502
|
|
|
* Implementations should define what empty means in their own context. |
503
|
|
|
* |
504
|
|
|
* @return bool whether the collection is empty. |
505
|
|
|
*/ |
506
|
|
|
public function isEmpty(): bool |
507
|
|
|
{ |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
/** |
511
|
|
|
* Returns a representation that can be natively converted to JSON, which is |
512
|
|
|
* called when invoking json_encode. |
513
|
|
|
* |
514
|
|
|
* @return mixed the data to be JSON encoded. |
515
|
|
|
* |
516
|
|
|
* @see JsonSerializable |
517
|
|
|
*/ |
518
|
|
|
public function jsonSerialize() |
519
|
|
|
{ |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
/** |
523
|
|
|
* Creates a shallow copy of the collection. |
524
|
|
|
* |
525
|
|
|
* @return Collection a shallow copy of the collection. |
526
|
|
|
*/ |
527
|
|
|
public function copy(): Collection |
528
|
|
|
{ |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
/** |
532
|
|
|
* Returns a string representation of the collection, which is invoked when |
533
|
|
|
* the collection is converted to a string. |
534
|
|
|
* |
535
|
|
|
* @return string |
536
|
|
|
*/ |
537
|
|
|
public function __toString(): string |
538
|
|
|
{ |
539
|
|
|
} |
540
|
|
|
// END GenericCollection Trait |
541
|
|
|
|
542
|
|
|
// BEGIN SquaredCapacityTrait |
543
|
|
|
// BEGIN Capacity Trait |
544
|
|
|
/** |
545
|
|
|
* Returns the current capacity. |
546
|
|
|
* |
547
|
|
|
* @return int |
548
|
|
|
*/ |
549
|
|
|
public function capacity(): int |
550
|
|
|
{ |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
/** |
554
|
|
|
* Ensures that enough memory is allocated for a specified capacity. This |
555
|
|
|
* potentially reduces the number of reallocations as the size increases. |
556
|
|
|
* |
557
|
|
|
* @param int $capacity The number of values for which capacity should be |
558
|
|
|
* allocated. Capacity will stay the same if this value |
559
|
|
|
* is less than or equal to the current capacity. |
560
|
|
|
*/ |
561
|
|
|
public function allocate(int $capacity) |
562
|
|
|
{ |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
/** |
566
|
|
|
* @return float the structures growth factor. |
567
|
|
|
*/ |
568
|
|
|
protected function getGrowthFactor(): float |
569
|
|
|
{ |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
/** |
573
|
|
|
* @return float to multiply by when decreasing capacity. |
574
|
|
|
*/ |
575
|
|
|
protected function getDecayFactor(): float |
576
|
|
|
{ |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
/** |
580
|
|
|
* Checks and adjusts capacity if required. |
581
|
|
|
*/ |
582
|
|
|
protected function checkCapacity() |
583
|
|
|
{ |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
/** |
587
|
|
|
* Called when capacity should be decrease if it drops below a threshold. |
588
|
|
|
*/ |
589
|
|
|
protected function decreaseCapacity() |
590
|
|
|
{ |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
/** |
594
|
|
|
* @return bool whether capacity should be increased. |
595
|
|
|
*/ |
596
|
|
|
protected function shouldDecreaseCapacity(): bool |
597
|
|
|
{ |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
/** |
601
|
|
|
* @return bool whether capacity should be increased. |
602
|
|
|
*/ |
603
|
|
|
protected function shouldIncreaseCapacity(): bool |
604
|
|
|
{ |
605
|
|
|
} |
606
|
|
|
// END Capacity Trait |
607
|
|
|
|
608
|
|
|
/** |
609
|
|
|
* Called when capacity should be increased to accommodate new values. |
610
|
|
|
*/ |
611
|
|
|
protected function increaseCapacity() |
612
|
|
|
{ |
613
|
|
|
} |
614
|
|
|
// END SquaredCapacityTrait |
615
|
|
|
|
616
|
|
|
/** |
617
|
|
|
* Creates a new instance. |
618
|
|
|
* |
619
|
|
|
* @param array|Traversable|null $values |
620
|
|
|
*/ |
621
|
|
|
public function __construct($values = null) |
622
|
|
|
{ |
623
|
|
|
} |
624
|
|
|
|
625
|
|
|
/** |
626
|
|
|
* Updates all values by applying a callback function to each value. |
627
|
|
|
* |
628
|
|
|
* @param callable $callback Accepts two arguments: key and value, should |
629
|
|
|
* return what the updated value will be. |
630
|
|
|
*/ |
631
|
|
|
public function apply(callable $callback) |
632
|
|
|
{ |
633
|
|
|
} |
634
|
|
|
|
635
|
|
|
/** |
636
|
|
|
* @inheritDoc |
637
|
|
|
*/ |
638
|
|
|
public function clear() |
639
|
|
|
{ |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
/** |
643
|
|
|
* Return the first Pair from the Map |
644
|
|
|
* |
645
|
|
|
* @return Pair |
646
|
|
|
* |
647
|
|
|
* @throws UnderflowException |
648
|
|
|
*/ |
649
|
|
|
public function first(): Pair |
650
|
|
|
{ |
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
/** |
654
|
|
|
* Return the last Pair from the Map |
655
|
|
|
* |
656
|
|
|
* @return Pair |
657
|
|
|
* |
658
|
|
|
* @throws UnderflowException |
659
|
|
|
*/ |
660
|
|
|
public function last(): Pair |
661
|
|
|
{ |
662
|
|
|
} |
663
|
|
|
|
664
|
|
|
/** |
665
|
|
|
* Return the pair at a specified position in the Map |
666
|
|
|
* |
667
|
|
|
* @param int $position |
668
|
|
|
* |
669
|
|
|
* @return Pair |
670
|
|
|
* |
671
|
|
|
* @throws OutOfRangeException |
672
|
|
|
*/ |
673
|
|
|
public function skip(int $position): Pair |
674
|
|
|
{ |
675
|
|
|
} |
676
|
|
|
|
677
|
|
|
/** |
678
|
|
|
* Returns the result of associating all keys of a given traversable object |
679
|
|
|
* or array with their corresponding values, as well as those of this map. |
680
|
|
|
* |
681
|
|
|
* @param array|Traversable $values |
682
|
|
|
* |
683
|
|
|
* @return Map |
684
|
|
|
*/ |
685
|
|
|
public function merge($values): Map |
686
|
|
|
{ |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
/** |
690
|
|
|
* Creates a new map containing the pairs of the current instance whose keys |
691
|
|
|
* are also present in the given map. In other words, returns a copy of the |
692
|
|
|
* current map with all keys removed that are not also in the other map. |
693
|
|
|
* |
694
|
|
|
* @param Map $map The other map. |
695
|
|
|
* |
696
|
|
|
* @return Map A new map containing the pairs of the current instance |
697
|
|
|
* whose keys are also present in the given map. In other |
698
|
|
|
* words, returns a copy of the current map with all keys |
699
|
|
|
* removed that are not also in the other map. |
700
|
|
|
*/ |
701
|
|
|
public function intersect(Map $map): Map |
702
|
|
|
{ |
703
|
|
|
} |
704
|
|
|
|
705
|
|
|
/** |
706
|
|
|
* Returns the result of removing all keys from the current instance that |
707
|
|
|
* are present in a given map. |
708
|
|
|
* |
709
|
|
|
* @param Map $map The map containing the keys to exclude. |
710
|
|
|
* |
711
|
|
|
* @return Map The result of removing all keys from the current instance |
712
|
|
|
* that are present in a given map. |
713
|
|
|
*/ |
714
|
|
|
public function diff(Map $map): Map |
715
|
|
|
{ |
716
|
|
|
} |
717
|
|
|
|
718
|
|
|
/** |
719
|
|
|
* Determines whether two keys are equal. |
720
|
|
|
* |
721
|
|
|
* @param mixed $a |
722
|
|
|
* @param mixed $b |
723
|
|
|
* |
724
|
|
|
* @return bool |
725
|
|
|
*/ |
726
|
|
|
private function keysAreEqual($a, $b): bool |
727
|
|
|
{ |
728
|
|
|
} |
729
|
|
|
|
730
|
|
|
/** |
731
|
|
|
* Attempts to look up a key in the table. |
732
|
|
|
* |
733
|
|
|
* @param $key |
734
|
|
|
* |
735
|
|
|
* @return Pair|null |
736
|
|
|
*/ |
737
|
|
|
private function lookupKey($key) |
738
|
|
|
{ |
739
|
|
|
} |
740
|
|
|
|
741
|
|
|
/** |
742
|
|
|
* Attempts to look up a key in the table. |
743
|
|
|
* |
744
|
|
|
* @param $value |
745
|
|
|
* |
746
|
|
|
* @return Pair|null |
747
|
|
|
*/ |
748
|
|
|
private function lookupValue($value) |
749
|
|
|
{ |
750
|
|
|
foreach ($this->pairs as $pair) { |
751
|
|
|
if ($pair->value === $value) { |
752
|
|
|
return $pair; |
753
|
|
|
} |
754
|
|
|
} |
755
|
|
|
} |
756
|
|
|
|
757
|
|
|
/** |
758
|
|
|
* Returns whether an association a given key exists. |
759
|
|
|
* |
760
|
|
|
* @param mixed $key |
761
|
|
|
* |
762
|
|
|
* @return bool |
763
|
|
|
*/ |
764
|
|
|
public function hasKey($key): bool |
765
|
|
|
{ |
766
|
|
|
return $this->lookupKey($key) !== null; |
767
|
|
|
} |
768
|
|
|
|
769
|
|
|
/** |
770
|
|
|
* Returns whether an association for a given value exists. |
771
|
|
|
* |
772
|
|
|
* @param mixed $value |
773
|
|
|
* |
774
|
|
|
* @return bool |
775
|
|
|
*/ |
776
|
|
|
public function hasValue($value): bool |
777
|
|
|
{ |
778
|
|
|
return $this->lookupValue($value) !== null; |
779
|
|
|
} |
780
|
|
|
|
781
|
|
|
/** |
782
|
|
|
* @inheritDoc |
783
|
|
|
*/ |
784
|
|
|
public function count(): int |
785
|
|
|
{ |
786
|
|
|
return count($this->pairs); |
787
|
|
|
} |
788
|
|
|
|
789
|
|
|
/** |
790
|
|
|
* Returns a new map containing only the values for which a predicate |
791
|
|
|
* returns true. A boolean test will be used if a predicate is not provided. |
792
|
|
|
* |
793
|
|
|
* @param callable|null $callback Accepts a key and a value, and returns: |
794
|
|
|
* true : include the value, |
795
|
|
|
* false: skip the value. |
796
|
|
|
* |
797
|
|
|
* @return Map |
798
|
|
|
*/ |
799
|
|
View Code Duplication |
public function filter(callable $callback = null): Map |
|
|
|
|
800
|
|
|
{ |
801
|
|
|
$filtered = new self(); |
802
|
|
|
|
803
|
|
|
foreach ($this as $key => $value) { |
804
|
|
|
if ($callback ? $callback($key, $value) : $value) { |
805
|
|
|
$filtered->put($key, $value); |
806
|
|
|
} |
807
|
|
|
} |
808
|
|
|
|
809
|
|
|
return $filtered; |
810
|
|
|
} |
811
|
|
|
|
812
|
|
|
/** |
813
|
|
|
* Returns the value associated with a key, or an optional default if the |
814
|
|
|
* key is not associated with a value. |
815
|
|
|
* |
816
|
|
|
* @param mixed $key |
817
|
|
|
* @param mixed $default |
818
|
|
|
* |
819
|
|
|
* @return mixed The associated value or fallback default if provided. |
820
|
|
|
* |
821
|
|
|
* @throws OutOfBoundsException if no default was provided and the key is |
822
|
|
|
* not associated with a value. |
823
|
|
|
*/ |
824
|
|
View Code Duplication |
public function get($key, $default = null) |
|
|
|
|
825
|
|
|
{ |
826
|
|
|
if (($pair = $this->lookupKey($key))) { |
827
|
|
|
return $pair->value; |
828
|
|
|
} |
829
|
|
|
|
830
|
|
|
// Check if a default was provided. |
831
|
|
|
if (func_num_args() === 1) { |
832
|
|
|
throw new OutOfBoundsException(); |
833
|
|
|
} |
834
|
|
|
|
835
|
|
|
return $default; |
836
|
|
|
} |
837
|
|
|
|
838
|
|
|
/** |
839
|
|
|
* Returns a set of all the keys in the map. |
840
|
|
|
* |
841
|
|
|
* @return Set |
842
|
|
|
*/ |
843
|
|
|
public function keys(): Set |
844
|
|
|
{ |
845
|
|
|
$key = function($pair) { |
846
|
|
|
return $pair->key; |
847
|
|
|
}; |
848
|
|
|
|
849
|
|
|
return new Set(array_map($key, $this->pairs)); |
850
|
|
|
} |
851
|
|
|
|
852
|
|
|
/** |
853
|
|
|
* Returns a new map using the results of applying a callback to each value. |
854
|
|
|
* |
855
|
|
|
* The keys will be equal in both maps. |
856
|
|
|
* |
857
|
|
|
* @param callable $callback Accepts two arguments: key and value, should |
858
|
|
|
* return what the updated value will be. |
859
|
|
|
* |
860
|
|
|
* @return Map |
861
|
|
|
*/ |
862
|
|
View Code Duplication |
public function map(callable $callback): Map |
|
|
|
|
863
|
|
|
{ |
864
|
|
|
$apply = function($pair) use ($callback) { |
865
|
|
|
return $callback($pair->key, $pair->value); |
866
|
|
|
}; |
867
|
|
|
|
868
|
|
|
return new self(array_map($apply, $this->pairs)); |
869
|
|
|
} |
870
|
|
|
|
871
|
|
|
/** |
872
|
|
|
* Returns a sequence of pairs representing all associations. |
873
|
|
|
* |
874
|
|
|
* @return Sequence |
875
|
|
|
*/ |
876
|
|
|
public function pairs(): Sequence |
877
|
|
|
{ |
878
|
|
|
$copy = function($pair) { |
879
|
|
|
return $pair->copy(); |
880
|
|
|
}; |
881
|
|
|
|
882
|
|
|
return new Vector(array_map($copy, $this->pairs)); |
883
|
|
|
} |
884
|
|
|
|
885
|
|
|
/** |
886
|
|
|
* Associates a key with a value, replacing a previous association if there |
887
|
|
|
* was one. |
888
|
|
|
* |
889
|
|
|
* @param mixed $key |
890
|
|
|
* @param mixed $value |
891
|
|
|
*/ |
892
|
|
View Code Duplication |
public function put($key, $value) |
|
|
|
|
893
|
|
|
{ |
894
|
|
|
$pair = $this->lookupKey($key); |
895
|
|
|
|
896
|
|
|
if ($pair) { |
897
|
|
|
$pair->value = $value; |
898
|
|
|
|
899
|
|
|
} else { |
900
|
|
|
$this->checkCapacity(); |
901
|
|
|
$this->pairs[] = new Pair($key, $value); |
902
|
|
|
} |
903
|
|
|
} |
904
|
|
|
|
905
|
|
|
/** |
906
|
|
|
* Creates associations for all keys and corresponding values of either an |
907
|
|
|
* array or iterable object. |
908
|
|
|
* |
909
|
|
|
* @param Traversable|array $values |
910
|
|
|
*/ |
911
|
|
|
public function putAll($values) |
912
|
|
|
{ |
913
|
|
|
} |
914
|
|
|
|
915
|
|
|
/** |
916
|
|
|
* Iteratively reduces the map to a single value using a callback. |
917
|
|
|
* |
918
|
|
|
* @param callable $callback Accepts the carry, key, and value, and |
919
|
|
|
* returns an updated carry value. |
920
|
|
|
* |
921
|
|
|
* @param mixed|null $initial Optional initial carry value. |
922
|
|
|
* |
923
|
|
|
* @return mixed The carry value of the final iteration, or the initial |
924
|
|
|
* value if the map was empty. |
925
|
|
|
*/ |
926
|
|
|
public function reduce(callable $callback, $initial = null) |
927
|
|
|
{ |
928
|
|
|
} |
929
|
|
|
|
930
|
|
|
/** |
931
|
|
|
* Completely removes a pair from the internal array by position. It is |
932
|
|
|
* important to remove it from the array and not just use 'unset'. |
933
|
|
|
* |
934
|
|
|
* @param int $position |
935
|
|
|
* |
936
|
|
|
* @return mixed |
937
|
|
|
*/ |
938
|
|
|
private function delete(int $position) |
939
|
|
|
{ |
940
|
|
|
} |
941
|
|
|
|
942
|
|
|
/** |
943
|
|
|
* Removes a key's association from the map and returns the associated value |
944
|
|
|
* or a provided default if provided. |
945
|
|
|
* |
946
|
|
|
* @param mixed $key |
947
|
|
|
* @param mixed $default |
948
|
|
|
* |
949
|
|
|
* @return mixed The associated value or fallback default if provided. |
950
|
|
|
* |
951
|
|
|
* @throws \OutOfBoundsException if no default was provided and the key is |
952
|
|
|
* not associated with a value. |
953
|
|
|
*/ |
954
|
|
|
public function remove($key, $default = null) |
955
|
|
|
{ |
956
|
|
|
} |
957
|
|
|
|
958
|
|
|
/** |
959
|
|
|
* Sorts the map into the reversed order. |
960
|
|
|
*/ |
961
|
|
|
public function reverse() |
962
|
|
|
{ |
963
|
|
|
} |
964
|
|
|
|
965
|
|
|
/** |
966
|
|
|
* Returns a reversed copy of the map. |
967
|
|
|
* |
968
|
|
|
* @return Map |
969
|
|
|
*/ |
970
|
|
|
public function reversed(): Map |
971
|
|
|
{ |
972
|
|
|
} |
973
|
|
|
|
974
|
|
|
/** |
975
|
|
|
* Returns a sub-sequence of a given length starting at a specified offset. |
976
|
|
|
* |
977
|
|
|
* @param int $offset If the offset is non-negative, the map will |
978
|
|
|
* start at that offset in the map. If offset is |
979
|
|
|
* negative, the map will start that far from the |
980
|
|
|
* end. |
981
|
|
|
* |
982
|
|
|
* @param int|null $length If a length is given and is positive, the |
983
|
|
|
* resulting set will have up to that many pairs in |
984
|
|
|
* it. If the requested length results in an |
985
|
|
|
* overflow, only pairs up to the end of the map |
986
|
|
|
* will be included. |
987
|
|
|
* |
988
|
|
|
* If a length is given and is negative, the map |
989
|
|
|
* will stop that many pairs from the end. |
990
|
|
|
* |
991
|
|
|
* If a length is not provided, the resulting map |
992
|
|
|
* will contains all pairs between the offset and |
993
|
|
|
* the end of the map. |
994
|
|
|
* |
995
|
|
|
* @return Map |
996
|
|
|
*/ |
997
|
|
|
public function slice(int $offset, int $length = null): Map |
998
|
|
|
{ |
999
|
|
|
} |
1000
|
|
|
|
1001
|
|
|
/** |
1002
|
|
|
* Sorts the map in-place, based on an optional callable comparator. |
1003
|
|
|
* |
1004
|
|
|
* The map will be sorted by value. |
1005
|
|
|
* |
1006
|
|
|
* @param callable|null $comparator Accepts two values to be compared. |
1007
|
|
|
*/ |
1008
|
|
|
public function sort(callable $comparator = null) |
1009
|
|
|
{ |
1010
|
|
|
} |
1011
|
|
|
|
1012
|
|
|
/** |
1013
|
|
|
* Returns a sorted copy of the map, based on an optional callable |
1014
|
|
|
* comparator. The map will be sorted by value. |
1015
|
|
|
* |
1016
|
|
|
* @param callable|null $comparator Accepts two values to be compared. |
1017
|
|
|
* |
1018
|
|
|
* @return Map |
1019
|
|
|
*/ |
1020
|
|
|
public function sorted(callable $comparator = null): Map |
1021
|
|
|
{ |
1022
|
|
|
} |
1023
|
|
|
|
1024
|
|
|
/** |
1025
|
|
|
* Sorts the map in-place, based on an optional callable comparator. |
1026
|
|
|
* |
1027
|
|
|
* The map will be sorted by key. |
1028
|
|
|
* |
1029
|
|
|
* @param callable|null $comparator Accepts two keys to be compared. |
1030
|
|
|
*/ |
1031
|
|
|
public function ksort(callable $comparator = null) |
1032
|
|
|
{ |
1033
|
|
|
} |
1034
|
|
|
|
1035
|
|
|
/** |
1036
|
|
|
* Returns a sorted copy of the map, based on an optional callable |
1037
|
|
|
* comparator. The map will be sorted by key. |
1038
|
|
|
* |
1039
|
|
|
* @param callable|null $comparator Accepts two keys to be compared. |
1040
|
|
|
* |
1041
|
|
|
* @return Map |
1042
|
|
|
*/ |
1043
|
|
|
public function ksorted(callable $comparator = null): Map |
1044
|
|
|
{ |
1045
|
|
|
} |
1046
|
|
|
|
1047
|
|
|
/** |
1048
|
|
|
* Returns the sum of all values in the map. |
1049
|
|
|
* |
1050
|
|
|
* @return int|float The sum of all the values in the map. |
1051
|
|
|
*/ |
1052
|
|
|
public function sum() |
1053
|
|
|
{ |
1054
|
|
|
} |
1055
|
|
|
|
1056
|
|
|
/** |
1057
|
|
|
* Returns an array representation of the collection. |
1058
|
|
|
* |
1059
|
|
|
* The format of the returned array is implementation-dependent. Some |
1060
|
|
|
* implementations may throw an exception if an array representation |
1061
|
|
|
* could not be created (for example when object are used as keys). |
1062
|
|
|
* |
1063
|
|
|
* @return array |
1064
|
|
|
*/ |
1065
|
|
|
public function toArray(): array |
1066
|
|
|
{ |
1067
|
|
|
} |
1068
|
|
|
|
1069
|
|
|
/** |
1070
|
|
|
* Returns a sequence of all the associated values in the Map. |
1071
|
|
|
* |
1072
|
|
|
* @return Sequence |
1073
|
|
|
*/ |
1074
|
|
|
public function values(): Sequence |
1075
|
|
|
{ |
1076
|
|
|
} |
1077
|
|
|
|
1078
|
|
|
/** |
1079
|
|
|
* Creates a new map that contains the pairs of the current instance as well |
1080
|
|
|
* as the pairs of another map. |
1081
|
|
|
* |
1082
|
|
|
* @param Map $map The other map, to combine with the current instance. |
1083
|
|
|
* |
1084
|
|
|
* @return Map A new map containing all the pairs of the current |
1085
|
|
|
* instance as well as another map. |
1086
|
|
|
*/ |
1087
|
|
|
public function union(Map $map): Map |
1088
|
|
|
{ |
1089
|
|
|
} |
1090
|
|
|
|
1091
|
|
|
/** |
1092
|
|
|
* Creates a new map using keys of either the current instance or of another |
1093
|
|
|
* map, but not of both. |
1094
|
|
|
* |
1095
|
|
|
* @param Map $map |
1096
|
|
|
* |
1097
|
|
|
* @return Map A new map containing keys in the current instance as well |
1098
|
|
|
* as another map, but not in both. |
1099
|
|
|
*/ |
1100
|
|
|
public function xor(Map $map): Map |
1101
|
|
|
{ |
1102
|
|
|
} |
1103
|
|
|
|
1104
|
|
|
/** |
1105
|
|
|
* @inheritDoc |
1106
|
|
|
*/ |
1107
|
|
|
public function getIterator() |
1108
|
|
|
{ |
1109
|
|
|
} |
1110
|
|
|
|
1111
|
|
|
/** |
1112
|
|
|
* Returns a representation to be used for var_dump and print_r. |
1113
|
|
|
*/ |
1114
|
|
|
public function __debugInfo() |
1115
|
|
|
{ |
1116
|
|
|
} |
1117
|
|
|
|
1118
|
|
|
/** |
1119
|
|
|
* @inheritdoc |
1120
|
|
|
*/ |
1121
|
|
|
public function offsetSet($offset, $value) |
1122
|
|
|
{ |
1123
|
|
|
} |
1124
|
|
|
|
1125
|
|
|
/** |
1126
|
|
|
* @inheritdoc |
1127
|
|
|
* |
1128
|
|
|
* @throws OutOfBoundsException |
1129
|
|
|
*/ |
1130
|
|
|
public function &offsetGet($offset) |
1131
|
|
|
{ |
1132
|
|
|
} |
1133
|
|
|
|
1134
|
|
|
/** |
1135
|
|
|
* @inheritdoc |
1136
|
|
|
*/ |
1137
|
|
|
public function offsetUnset($offset) |
1138
|
|
|
{ |
1139
|
|
|
} |
1140
|
|
|
|
1141
|
|
|
/** |
1142
|
|
|
* @inheritdoc |
1143
|
|
|
*/ |
1144
|
|
|
public function offsetExists($offset) |
1145
|
|
|
{ |
1146
|
|
|
} |
1147
|
|
|
} |
1148
|
|
|
|
1149
|
|
|
/** |
1150
|
|
|
* A pair which represents a key and an associated value. |
1151
|
|
|
* |
1152
|
|
|
* @package Ds |
1153
|
|
|
*/ |
1154
|
|
|
final class Pair implements JsonSerializable |
|
|
|
|
1155
|
|
|
{ |
1156
|
|
|
/** |
1157
|
|
|
* @param mixed $key The pair's key |
1158
|
|
|
*/ |
1159
|
|
|
public $key; |
1160
|
|
|
|
1161
|
|
|
/** |
1162
|
|
|
* @param mixed $value The pair's value |
1163
|
|
|
*/ |
1164
|
|
|
public $value; |
1165
|
|
|
|
1166
|
|
|
/** |
1167
|
|
|
* Creates a new instance. |
1168
|
|
|
* |
1169
|
|
|
* @param mixed $key |
1170
|
|
|
* @param mixed $value |
1171
|
|
|
*/ |
1172
|
|
|
public function __construct($key = null, $value = null) |
1173
|
|
|
{ |
1174
|
|
|
} |
1175
|
|
|
|
1176
|
|
|
/** |
1177
|
|
|
* This allows unset($pair->key) to not completely remove the property, |
1178
|
|
|
* but be set to null instead. |
1179
|
|
|
* |
1180
|
|
|
* @param mixed $name |
1181
|
|
|
* |
1182
|
|
|
* @return mixed|null |
1183
|
|
|
*/ |
1184
|
|
|
public function __get($name) |
1185
|
|
|
{ |
1186
|
|
|
} |
1187
|
|
|
|
1188
|
|
|
/** |
1189
|
|
|
* Returns a copy of the Pair |
1190
|
|
|
* |
1191
|
|
|
* @return Pair |
1192
|
|
|
*/ |
1193
|
|
|
public function copy(): Pair |
1194
|
|
|
{ |
1195
|
|
|
} |
1196
|
|
|
|
1197
|
|
|
/** |
1198
|
|
|
* Returns a representation to be used for var_dump and print_r. |
1199
|
|
|
* |
1200
|
|
|
* @return array |
1201
|
|
|
*/ |
1202
|
|
|
public function __debugInfo() |
1203
|
|
|
{ |
1204
|
|
|
} |
1205
|
|
|
|
1206
|
|
|
/** |
1207
|
|
|
* @inheritDoc |
1208
|
|
|
*/ |
1209
|
|
|
public function toArray(): array |
1210
|
|
|
{ |
1211
|
|
|
} |
1212
|
|
|
|
1213
|
|
|
/** |
1214
|
|
|
* @inheritDoc |
1215
|
|
|
*/ |
1216
|
|
|
public function jsonSerialize() |
1217
|
|
|
{ |
1218
|
|
|
} |
1219
|
|
|
|
1220
|
|
|
/** |
1221
|
|
|
* Returns a string representation of the pair. |
1222
|
|
|
* |
1223
|
|
|
* @return string a string representation of the pair. |
1224
|
|
|
*/ |
1225
|
|
|
public function __toString(): string |
1226
|
|
|
{ |
1227
|
|
|
} |
1228
|
|
|
} |
1229
|
|
|
|
1230
|
|
|
/** |
1231
|
|
|
* A PriorityQueue is very similar to a Queue. Values are pushed into the queue |
1232
|
|
|
* with an assigned priority, and the value with the highest priority will |
1233
|
|
|
* always be at the front of the queue. |
1234
|
|
|
* |
1235
|
|
|
* @package Ds |
1236
|
|
|
*/ |
1237
|
|
|
final class PriorityQueue implements IteratorAggregate, Collection |
|
|
|
|
1238
|
|
|
{ |
1239
|
|
|
/** @var int */ |
1240
|
|
|
const MIN_CAPACITY = 8; |
1241
|
|
|
|
1242
|
|
|
// BEGIN GenericCollection Trait |
1243
|
|
|
/** |
1244
|
|
|
* Returns whether the collection is empty. |
1245
|
|
|
* |
1246
|
|
|
* This should be equivalent to a count of zero, but is not required. |
1247
|
|
|
* Implementations should define what empty means in their own context. |
1248
|
|
|
* |
1249
|
|
|
* @return bool whether the collection is empty. |
1250
|
|
|
*/ |
1251
|
|
|
public function isEmpty(): bool |
1252
|
|
|
{ |
1253
|
|
|
} |
1254
|
|
|
|
1255
|
|
|
/** |
1256
|
|
|
* Returns a representation that can be natively converted to JSON, which is |
1257
|
|
|
* called when invoking json_encode. |
1258
|
|
|
* |
1259
|
|
|
* @return mixed the data to be JSON encoded. |
1260
|
|
|
* |
1261
|
|
|
* @see JsonSerializable |
1262
|
|
|
*/ |
1263
|
|
|
public function jsonSerialize() |
1264
|
|
|
{ |
1265
|
|
|
} |
1266
|
|
|
|
1267
|
|
|
/** |
1268
|
|
|
* Creates a shallow copy of the collection. |
1269
|
|
|
* |
1270
|
|
|
* @return Collection a shallow copy of the collection. |
1271
|
|
|
*/ |
1272
|
|
|
public function copy(): Collection |
1273
|
|
|
{ |
1274
|
|
|
} |
1275
|
|
|
|
1276
|
|
|
/** |
1277
|
|
|
* Invoked when calling var_dump. |
1278
|
|
|
* |
1279
|
|
|
* @return array |
1280
|
|
|
*/ |
1281
|
|
|
public function __debugInfo(): array |
1282
|
|
|
{ |
1283
|
|
|
} |
1284
|
|
|
|
1285
|
|
|
/** |
1286
|
|
|
* Returns a string representation of the collection, which is invoked when |
1287
|
|
|
* the collection is converted to a string. |
1288
|
|
|
* |
1289
|
|
|
* @return string |
1290
|
|
|
*/ |
1291
|
|
|
public function __toString(): string |
1292
|
|
|
{ |
1293
|
|
|
} |
1294
|
|
|
// END GenericCollection Trait |
1295
|
|
|
|
1296
|
|
|
// BEGIN SquaredCapacityTrait |
1297
|
|
|
// BEGIN Capacity Trait |
1298
|
|
|
/** |
1299
|
|
|
* Returns the current capacity. |
1300
|
|
|
* |
1301
|
|
|
* @return int |
1302
|
|
|
*/ |
1303
|
|
|
public function capacity(): int |
1304
|
|
|
{ |
1305
|
|
|
} |
1306
|
|
|
|
1307
|
|
|
/** |
1308
|
|
|
* Ensures that enough memory is allocated for a specified capacity. This |
1309
|
|
|
* potentially reduces the number of reallocations as the size increases. |
1310
|
|
|
* |
1311
|
|
|
* @param int $capacity The number of values for which capacity should be |
1312
|
|
|
* allocated. Capacity will stay the same if this value |
1313
|
|
|
* is less than or equal to the current capacity. |
1314
|
|
|
*/ |
1315
|
|
|
public function allocate(int $capacity) |
1316
|
|
|
{ |
1317
|
|
|
} |
1318
|
|
|
|
1319
|
|
|
/** |
1320
|
|
|
* @return float the structures growth factor. |
1321
|
|
|
*/ |
1322
|
|
|
protected function getGrowthFactor(): float |
1323
|
|
|
{ |
1324
|
|
|
} |
1325
|
|
|
|
1326
|
|
|
/** |
1327
|
|
|
* @return float to multiply by when decreasing capacity. |
1328
|
|
|
*/ |
1329
|
|
|
protected function getDecayFactor(): float |
1330
|
|
|
{ |
1331
|
|
|
} |
1332
|
|
|
|
1333
|
|
|
/** |
1334
|
|
|
* Checks and adjusts capacity if required. |
1335
|
|
|
*/ |
1336
|
|
|
protected function checkCapacity() |
1337
|
|
|
{ |
1338
|
|
|
} |
1339
|
|
|
|
1340
|
|
|
/** |
1341
|
|
|
* Called when capacity should be decrease if it drops below a threshold. |
1342
|
|
|
*/ |
1343
|
|
|
protected function decreaseCapacity() |
1344
|
|
|
{ |
1345
|
|
|
} |
1346
|
|
|
|
1347
|
|
|
/** |
1348
|
|
|
* @return bool whether capacity should be increased. |
1349
|
|
|
*/ |
1350
|
|
|
protected function shouldDecreaseCapacity(): bool |
1351
|
|
|
{ |
1352
|
|
|
} |
1353
|
|
|
|
1354
|
|
|
/** |
1355
|
|
|
* @return bool whether capacity should be increased. |
1356
|
|
|
*/ |
1357
|
|
|
protected function shouldIncreaseCapacity(): bool |
1358
|
|
|
{ |
1359
|
|
|
} |
1360
|
|
|
// END Capacity Trait |
1361
|
|
|
|
1362
|
|
|
/** |
1363
|
|
|
* Called when capacity should be increased to accommodate new values. |
1364
|
|
|
*/ |
1365
|
|
|
protected function increaseCapacity() |
1366
|
|
|
{ |
1367
|
|
|
} |
1368
|
|
|
// END SquaredCapacityTrait |
1369
|
|
|
|
1370
|
|
|
/** |
1371
|
|
|
* @inheritDoc |
1372
|
|
|
*/ |
1373
|
|
|
public function clear() |
1374
|
|
|
{ |
1375
|
|
|
} |
1376
|
|
|
|
1377
|
|
|
/** |
1378
|
|
|
* @inheritDoc |
1379
|
|
|
*/ |
1380
|
|
|
public function count(): int |
1381
|
|
|
{ |
1382
|
|
|
} |
1383
|
|
|
|
1384
|
|
|
/** |
1385
|
|
|
* Returns the value with the highest priority in the priority queue. |
1386
|
|
|
* |
1387
|
|
|
* @return mixed |
1388
|
|
|
* |
1389
|
|
|
* @throw UnderflowException |
1390
|
|
|
*/ |
1391
|
|
|
public function peek() |
1392
|
|
|
{ |
1393
|
|
|
} |
1394
|
|
|
|
1395
|
|
|
/** |
1396
|
|
|
* Returns and removes the value with the highest priority in the queue. |
1397
|
|
|
* |
1398
|
|
|
* @return mixed |
1399
|
|
|
*/ |
1400
|
|
|
public function pop() |
1401
|
|
|
{ |
1402
|
|
|
} |
1403
|
|
|
|
1404
|
|
|
/** |
1405
|
|
|
* Pushes a value into the queue, with a specified priority. |
1406
|
|
|
* |
1407
|
|
|
* @param mixed $value |
1408
|
|
|
* @param int $priority |
1409
|
|
|
*/ |
1410
|
|
|
public function push($value, int $priority) |
1411
|
|
|
{ |
1412
|
|
|
} |
1413
|
|
|
|
1414
|
|
|
/** |
1415
|
|
|
* Returns an array representation of the collection. |
1416
|
|
|
* |
1417
|
|
|
* The format of the returned array is implementation-dependent. Some |
1418
|
|
|
* implementations may throw an exception if an array representation |
1419
|
|
|
* could not be created (for example when object are used as keys). |
1420
|
|
|
* |
1421
|
|
|
* @return array |
1422
|
|
|
*/ |
1423
|
|
|
public function toArray(): array |
1424
|
|
|
{ |
1425
|
|
|
} |
1426
|
|
|
|
1427
|
|
|
/** |
1428
|
|
|
* @inheritDoc |
1429
|
|
|
*/ |
1430
|
|
|
public function getIterator() |
1431
|
|
|
{ |
1432
|
|
|
} |
1433
|
|
|
} |
1434
|
|
|
|
1435
|
|
|
/** |
1436
|
|
|
* A “first in, first out” or “FIFO” collection that only allows access to the |
1437
|
|
|
* value at the front of the queue and iterates in that order, destructively. |
1438
|
|
|
* |
1439
|
|
|
* @package Ds |
1440
|
|
|
*/ |
1441
|
|
|
final class Queue implements IteratorAggregate, ArrayAccess, Collection |
|
|
|
|
1442
|
|
|
{ |
1443
|
|
|
const MIN_CAPACITY = 8; |
1444
|
|
|
|
1445
|
|
|
// BEGIN GenericCollection Trait |
1446
|
|
|
/** |
1447
|
|
|
* Returns whether the collection is empty. |
1448
|
|
|
* |
1449
|
|
|
* This should be equivalent to a count of zero, but is not required. |
1450
|
|
|
* Implementations should define what empty means in their own context. |
1451
|
|
|
* |
1452
|
|
|
* @return bool whether the collection is empty. |
1453
|
|
|
*/ |
1454
|
|
|
public function isEmpty(): bool |
1455
|
|
|
{ |
1456
|
|
|
} |
1457
|
|
|
|
1458
|
|
|
/** |
1459
|
|
|
* Returns a representation that can be natively converted to JSON, which is |
1460
|
|
|
* called when invoking json_encode. |
1461
|
|
|
* |
1462
|
|
|
* @return mixed the data to be JSON encoded. |
1463
|
|
|
* |
1464
|
|
|
* @see JsonSerializable |
1465
|
|
|
*/ |
1466
|
|
|
public function jsonSerialize() |
1467
|
|
|
{ |
1468
|
|
|
} |
1469
|
|
|
|
1470
|
|
|
/** |
1471
|
|
|
* Creates a shallow copy of the collection. |
1472
|
|
|
* |
1473
|
|
|
* @return Collection a shallow copy of the collection. |
1474
|
|
|
*/ |
1475
|
|
|
public function copy(): Collection |
1476
|
|
|
{ |
1477
|
|
|
} |
1478
|
|
|
|
1479
|
|
|
/** |
1480
|
|
|
* Invoked when calling var_dump. |
1481
|
|
|
* |
1482
|
|
|
* @return array |
1483
|
|
|
*/ |
1484
|
|
|
public function __debugInfo(): array |
1485
|
|
|
{ |
1486
|
|
|
} |
1487
|
|
|
|
1488
|
|
|
/** |
1489
|
|
|
* Returns a string representation of the collection, which is invoked when |
1490
|
|
|
* the collection is converted to a string. |
1491
|
|
|
* |
1492
|
|
|
* @return string |
1493
|
|
|
*/ |
1494
|
|
|
public function __toString(): string |
1495
|
|
|
{ |
1496
|
|
|
} |
1497
|
|
|
// END GenericCollection Trait |
1498
|
|
|
|
1499
|
|
|
/** |
1500
|
|
|
* Creates an instance using the values of an array or Traversable object. |
1501
|
|
|
* |
1502
|
|
|
* @param array|Traversable $values |
|
|
|
|
1503
|
|
|
*/ |
1504
|
|
|
public function __construct($values = null) |
1505
|
|
|
{ |
1506
|
|
|
} |
1507
|
|
|
|
1508
|
|
|
/** |
1509
|
|
|
* Ensures that enough memory is allocated for a specified capacity. This |
1510
|
|
|
* potentially reduces the number of reallocations as the size increases. |
1511
|
|
|
* |
1512
|
|
|
* @param int $capacity The number of values for which capacity should be |
1513
|
|
|
* allocated. Capacity will stay the same if this value |
1514
|
|
|
* is less than or equal to the current capacity. |
1515
|
|
|
*/ |
1516
|
|
|
public function allocate(int $capacity) |
1517
|
|
|
{ |
1518
|
|
|
} |
1519
|
|
|
|
1520
|
|
|
/** |
1521
|
|
|
* Returns the current capacity of the queue. |
1522
|
|
|
* |
1523
|
|
|
* @return int |
1524
|
|
|
*/ |
1525
|
|
|
public function capacity(): int |
1526
|
|
|
{ |
1527
|
|
|
} |
1528
|
|
|
|
1529
|
|
|
/** |
1530
|
|
|
* @inheritDoc |
1531
|
|
|
*/ |
1532
|
|
|
public function clear() |
1533
|
|
|
{ |
1534
|
|
|
} |
1535
|
|
|
|
1536
|
|
|
/** |
1537
|
|
|
* @inheritDoc |
1538
|
|
|
*/ |
1539
|
|
|
public function count(): int |
1540
|
|
|
{ |
1541
|
|
|
} |
1542
|
|
|
|
1543
|
|
|
/** |
1544
|
|
|
* Returns the value at the front of the queue without removing it. |
1545
|
|
|
* |
1546
|
|
|
* @return mixed |
1547
|
|
|
*/ |
1548
|
|
|
public function peek() |
1549
|
|
|
{ |
1550
|
|
|
} |
1551
|
|
|
|
1552
|
|
|
/** |
1553
|
|
|
* Returns and removes the value at the front of the Queue. |
1554
|
|
|
* |
1555
|
|
|
* @return mixed |
1556
|
|
|
*/ |
1557
|
|
|
public function pop() |
1558
|
|
|
{ |
1559
|
|
|
} |
1560
|
|
|
|
1561
|
|
|
/** |
1562
|
|
|
* Pushes zero or more values into the front of the queue. |
1563
|
|
|
* |
1564
|
|
|
* @param mixed ...$values |
1565
|
|
|
*/ |
|
|
|
|
1566
|
|
|
public function push(...$values) |
1567
|
|
|
{ |
1568
|
|
|
} |
1569
|
|
|
|
1570
|
|
|
/** |
1571
|
|
|
* Returns an array representation of the collection. |
1572
|
|
|
* |
1573
|
|
|
* The format of the returned array is implementation-dependent. Some |
1574
|
|
|
* implementations may throw an exception if an array representation |
1575
|
|
|
* could not be created (for example when object are used as keys). |
1576
|
|
|
* |
1577
|
|
|
* @return array |
1578
|
|
|
*/ |
1579
|
|
|
public function toArray(): array |
1580
|
|
|
{ |
1581
|
|
|
} |
1582
|
|
|
|
1583
|
|
|
/** |
1584
|
|
|
* Get iterator |
1585
|
|
|
*/ |
1586
|
|
|
public function getIterator() |
1587
|
|
|
{ |
1588
|
|
|
} |
1589
|
|
|
|
1590
|
|
|
/** |
1591
|
|
|
* @inheritdoc |
1592
|
|
|
* |
1593
|
|
|
* @throws OutOfBoundsException |
1594
|
|
|
*/ |
1595
|
|
|
public function offsetSet($offset, $value) |
1596
|
|
|
{ |
1597
|
|
|
} |
1598
|
|
|
|
1599
|
|
|
/** |
1600
|
|
|
* @inheritdoc |
1601
|
|
|
* |
1602
|
|
|
* @throws Error |
1603
|
|
|
*/ |
1604
|
|
|
public function offsetGet($offset) |
1605
|
|
|
{ |
1606
|
|
|
} |
1607
|
|
|
|
1608
|
|
|
/** |
1609
|
|
|
* @inheritdoc |
1610
|
|
|
* |
1611
|
|
|
* @throws Error |
1612
|
|
|
*/ |
1613
|
|
|
public function offsetUnset($offset) |
1614
|
|
|
{ |
1615
|
|
|
} |
1616
|
|
|
|
1617
|
|
|
/** |
1618
|
|
|
* @inheritdoc |
1619
|
|
|
* |
1620
|
|
|
* @throws Error |
1621
|
|
|
*/ |
1622
|
|
|
public function offsetExists($offset) |
1623
|
|
|
{ |
1624
|
|
|
} |
1625
|
|
|
} |
1626
|
|
|
|
1627
|
|
|
/** |
1628
|
|
|
* Describes the behaviour of values arranged in a single, linear dimension. |
1629
|
|
|
* Some languages refer to this as a "List". It’s similar to an array that uses |
1630
|
|
|
* incremental integer keys, with the exception of a few characteristics: |
1631
|
|
|
* |
1632
|
|
|
* - Values will always be indexed as [0, 1, 2, …, size - 1]. |
1633
|
|
|
* - Only allowed to access values by index in the range [0, size - 1]. |
1634
|
|
|
* |
1635
|
|
|
* @package Ds |
1636
|
|
|
*/ |
1637
|
|
|
interface Sequence extends Collection |
|
|
|
|
1638
|
|
|
{ |
1639
|
|
|
/** |
1640
|
|
|
* Ensures that enough memory is allocated for a required capacity. |
1641
|
|
|
* |
1642
|
|
|
* @param int $capacity The number of values for which capacity should be |
1643
|
|
|
* allocated. Capacity will stay the same if this value |
1644
|
|
|
* is less than or equal to the current capacity. |
1645
|
|
|
*/ |
1646
|
|
|
public function allocate(int $capacity); |
1647
|
|
|
|
1648
|
|
|
/** |
1649
|
|
|
* Updates every value in the sequence by applying a callback, using the |
1650
|
|
|
* return value as the new value. |
1651
|
|
|
* |
1652
|
|
|
* @param callable $callback Accepts the value, returns the new value. |
1653
|
|
|
*/ |
1654
|
|
|
public function apply(callable $callback); |
1655
|
|
|
|
1656
|
|
|
/** |
1657
|
|
|
* Returns the current capacity of the sequence. |
1658
|
|
|
* |
1659
|
|
|
* @return int |
1660
|
|
|
*/ |
1661
|
|
|
public function capacity(): int; |
1662
|
|
|
|
1663
|
|
|
/** |
1664
|
|
|
* Determines whether the sequence contains all of zero or more values. |
1665
|
|
|
* |
1666
|
|
|
* @param mixed ...$values |
1667
|
|
|
* |
1668
|
|
|
* @return bool true if at least one value was provided and the sequence |
1669
|
|
|
* contains all given values, false otherwise. |
1670
|
|
|
*/ |
|
|
|
|
1671
|
|
|
public function contains(...$values): bool; |
1672
|
|
|
|
1673
|
|
|
/** |
1674
|
|
|
* Returns a new sequence containing only the values for which a callback |
1675
|
|
|
* returns true. A boolean test will be used if a callback is not provided. |
1676
|
|
|
* |
1677
|
|
|
* @param callable|null $callback Accepts a value, returns a boolean result: |
1678
|
|
|
* true : include the value, |
1679
|
|
|
* false: skip the value. |
1680
|
|
|
* |
1681
|
|
|
* @return Sequence |
1682
|
|
|
*/ |
1683
|
|
|
public function filter(callable $callback = null): Sequence; |
1684
|
|
|
|
1685
|
|
|
/** |
1686
|
|
|
* Returns the index of a given value, or false if it could not be found. |
1687
|
|
|
* |
1688
|
|
|
* @param mixed $value |
1689
|
|
|
* |
1690
|
|
|
* @return int|bool |
1691
|
|
|
*/ |
1692
|
|
|
public function find($value); |
1693
|
|
|
|
1694
|
|
|
/** |
1695
|
|
|
* Returns the first value in the sequence. |
1696
|
|
|
* |
1697
|
|
|
* @return mixed |
1698
|
|
|
* |
1699
|
|
|
* @throws \UnderflowException if the sequence is empty. |
1700
|
|
|
*/ |
1701
|
|
|
public function first(); |
1702
|
|
|
|
1703
|
|
|
/** |
1704
|
|
|
* Returns the value at a given index (position) in the sequence. |
1705
|
|
|
* |
1706
|
|
|
* @param int $index |
1707
|
|
|
* |
1708
|
|
|
* @return mixed |
1709
|
|
|
* |
1710
|
|
|
* @throws \OutOfRangeException if the index is not in the range [0, size-1] |
1711
|
|
|
*/ |
1712
|
|
|
public function get(int $index); |
1713
|
|
|
|
1714
|
|
|
/** |
1715
|
|
|
* Inserts zero or more values at a given index. |
1716
|
|
|
* |
1717
|
|
|
* Each value after the index will be moved one position to the right. |
1718
|
|
|
* Values may be inserted at an index equal to the size of the sequence. |
1719
|
|
|
* |
1720
|
|
|
* @param int $index |
1721
|
|
|
* @param mixed ...$values |
1722
|
|
|
* |
1723
|
|
|
* @throws \OutOfRangeException if the index is not in the range [0, n] |
1724
|
|
|
*/ |
|
|
|
|
1725
|
|
|
public function insert(int $index, ...$values); |
1726
|
|
|
|
1727
|
|
|
/** |
1728
|
|
|
* Joins all values of the sequence into a string, adding an optional 'glue' |
1729
|
|
|
* between them. Returns an empty string if the sequence is empty. |
1730
|
|
|
* |
1731
|
|
|
* @param string $glue |
|
|
|
|
1732
|
|
|
* |
1733
|
|
|
* @return string |
1734
|
|
|
*/ |
1735
|
|
|
public function join(string $glue = null): string; |
1736
|
|
|
|
1737
|
|
|
/** |
1738
|
|
|
* Returns the last value in the sequence. |
1739
|
|
|
* |
1740
|
|
|
* @return mixed |
1741
|
|
|
* |
1742
|
|
|
* @throws \UnderflowException if the sequence is empty. |
1743
|
|
|
*/ |
1744
|
|
|
public function last(); |
1745
|
|
|
|
1746
|
|
|
/** |
1747
|
|
|
* Returns a new sequence using the results of applying a callback to each |
1748
|
|
|
* value. |
1749
|
|
|
* |
1750
|
|
|
* @param callable $callback |
1751
|
|
|
* |
1752
|
|
|
* @return Sequence |
1753
|
|
|
*/ |
1754
|
|
|
public function map(callable $callback): Sequence; |
1755
|
|
|
|
1756
|
|
|
/** |
1757
|
|
|
* Returns the result of adding all given values to the sequence. |
1758
|
|
|
* |
1759
|
|
|
* @param array|Traversable $values |
1760
|
|
|
* |
1761
|
|
|
* @return Sequence |
1762
|
|
|
*/ |
1763
|
|
|
public function merge($values): Sequence; |
1764
|
|
|
|
1765
|
|
|
/** |
1766
|
|
|
* Removes the last value in the sequence, and returns it. |
1767
|
|
|
* |
1768
|
|
|
* @return mixed what was the last value in the sequence. |
1769
|
|
|
* |
1770
|
|
|
* @throws \UnderflowException if the sequence is empty. |
1771
|
|
|
*/ |
1772
|
|
|
public function pop(); |
1773
|
|
|
|
1774
|
|
|
/** |
1775
|
|
|
* Adds zero or more values to the end of the sequence. |
1776
|
|
|
* |
1777
|
|
|
* @param mixed ...$values |
1778
|
|
|
*/ |
|
|
|
|
1779
|
|
|
public function push(...$values); |
1780
|
|
|
|
1781
|
|
|
/** |
1782
|
|
|
* Iteratively reduces the sequence to a single value using a callback. |
1783
|
|
|
* |
1784
|
|
|
* @param callable $callback Accepts the carry and current value, and |
1785
|
|
|
* returns an updated carry value. |
1786
|
|
|
* |
1787
|
|
|
* @param mixed|null $initial Optional initial carry value. |
1788
|
|
|
* |
1789
|
|
|
* @return mixed The carry value of the final iteration, or the initial |
1790
|
|
|
* value if the sequence was empty. |
1791
|
|
|
*/ |
1792
|
|
|
public function reduce(callable $callback, $initial = null); |
1793
|
|
|
|
1794
|
|
|
/** |
1795
|
|
|
* Removes and returns the value at a given index in the sequence. |
1796
|
|
|
* |
1797
|
|
|
* @param int $index this index to remove. |
1798
|
|
|
* |
1799
|
|
|
* @return mixed the removed value. |
1800
|
|
|
* |
1801
|
|
|
* @throws \OutOfRangeException if the index is not in the range [0, size-1] |
1802
|
|
|
*/ |
1803
|
|
|
public function remove(int $index); |
1804
|
|
|
|
1805
|
|
|
/** |
1806
|
|
|
* Reverses the sequence in-place. |
1807
|
|
|
*/ |
1808
|
|
|
public function reverse(); |
1809
|
|
|
|
1810
|
|
|
/** |
1811
|
|
|
* Returns a reversed copy of the sequence. |
1812
|
|
|
* |
1813
|
|
|
* @return Sequence |
1814
|
|
|
*/ |
1815
|
|
|
public function reversed(); |
1816
|
|
|
|
1817
|
|
|
/** |
1818
|
|
|
* Rotates the sequence by a given number of rotations, which is equivalent |
1819
|
|
|
* to successive calls to 'shift' and 'push' if the number of rotations is |
1820
|
|
|
* positive, or 'pop' and 'unshift' if negative. |
1821
|
|
|
* |
1822
|
|
|
* @param int $rotations The number of rotations (can be negative). |
1823
|
|
|
*/ |
1824
|
|
|
public function rotate(int $rotations); |
1825
|
|
|
|
1826
|
|
|
/** |
1827
|
|
|
* Replaces the value at a given index in the sequence with a new value. |
1828
|
|
|
* |
1829
|
|
|
* @param int $index |
1830
|
|
|
* @param mixed $value |
1831
|
|
|
* |
1832
|
|
|
* @throws \OutOfRangeException if the index is not in the range [0, size-1] |
1833
|
|
|
*/ |
1834
|
|
|
public function set(int $index, $value); |
1835
|
|
|
|
1836
|
|
|
/** |
1837
|
|
|
* Removes and returns the first value in the sequence. |
1838
|
|
|
* |
1839
|
|
|
* @return mixed what was the first value in the sequence. |
1840
|
|
|
* |
1841
|
|
|
* @throws \UnderflowException if the sequence was empty. |
1842
|
|
|
*/ |
1843
|
|
|
public function shift(); |
1844
|
|
|
|
1845
|
|
|
/** |
1846
|
|
|
* Returns a sub-sequence of a given length starting at a specified index. |
1847
|
|
|
* |
1848
|
|
|
* @param int $index If the index is positive, the sequence will start |
1849
|
|
|
* at that index in the sequence. If index is negative, |
1850
|
|
|
* the sequence will start that far from the end. |
1851
|
|
|
* |
1852
|
|
|
* @param int $length If a length is given and is positive, the resulting |
|
|
|
|
1853
|
|
|
* sequence will have up to that many values in it. |
1854
|
|
|
* If the length results in an overflow, only values |
1855
|
|
|
* up to the end of the sequence will be included. |
1856
|
|
|
* |
1857
|
|
|
* If a length is given and is negative, the sequence |
1858
|
|
|
* will stop that many values from the end. |
1859
|
|
|
* |
1860
|
|
|
* If a length is not provided, the resulting sequence |
1861
|
|
|
* will contain all values between the index and the |
1862
|
|
|
* end of the sequence. |
1863
|
|
|
* |
1864
|
|
|
* @return Sequence |
1865
|
|
|
*/ |
1866
|
|
|
public function slice(int $index, int $length = null): Sequence; |
1867
|
|
|
|
1868
|
|
|
/** |
1869
|
|
|
* Sorts the sequence in-place, based on an optional callable comparator. |
1870
|
|
|
* |
1871
|
|
|
* @param callable|null $comparator Accepts two values to be compared. |
1872
|
|
|
* Should return the result of a <=> b. |
1873
|
|
|
*/ |
1874
|
|
|
public function sort(callable $comparator = null); |
1875
|
|
|
|
1876
|
|
|
/** |
1877
|
|
|
* Returns a sorted copy of the sequence, based on an optional callable |
1878
|
|
|
* comparator. Natural ordering will be used if a comparator is not given. |
1879
|
|
|
* |
1880
|
|
|
* @param callable|null $comparator Accepts two values to be compared. |
1881
|
|
|
* Should return the result of a <=> b. |
1882
|
|
|
* |
1883
|
|
|
* @return Sequence |
1884
|
|
|
*/ |
1885
|
|
|
public function sorted(callable $comparator = null): Sequence; |
1886
|
|
|
|
1887
|
|
|
/** |
1888
|
|
|
* Returns the sum of all values in the sequence. |
1889
|
|
|
* |
1890
|
|
|
* @return int|float The sum of all the values in the sequence. |
1891
|
|
|
*/ |
1892
|
|
|
public function sum(); |
1893
|
|
|
|
1894
|
|
|
/** |
1895
|
|
|
* Adds zero or more values to the front of the sequence. |
1896
|
|
|
* |
1897
|
|
|
* @param mixed ...$values |
1898
|
|
|
*/ |
|
|
|
|
1899
|
|
|
public function unshift(...$values); |
1900
|
|
|
} |
1901
|
|
|
|
1902
|
|
|
/** |
1903
|
|
|
* A sequence of unique values. |
1904
|
|
|
* |
1905
|
|
|
* @package Ds |
1906
|
|
|
*/ |
1907
|
|
|
final class Set implements IteratorAggregate, ArrayAccess, Collection |
|
|
|
|
1908
|
|
|
{ |
1909
|
|
|
const MIN_CAPACITY = Map::MIN_CAPACITY; |
1910
|
|
|
|
1911
|
|
|
// BEGIN GenericCollection Trait |
1912
|
|
|
/** |
1913
|
|
|
* Returns whether the collection is empty. |
1914
|
|
|
* |
1915
|
|
|
* This should be equivalent to a count of zero, but is not required. |
1916
|
|
|
* Implementations should define what empty means in their own context. |
1917
|
|
|
* |
1918
|
|
|
* @return bool whether the collection is empty. |
1919
|
|
|
*/ |
1920
|
|
|
public function isEmpty(): bool |
1921
|
|
|
{ |
1922
|
|
|
} |
1923
|
|
|
|
1924
|
|
|
/** |
1925
|
|
|
* Returns a representation that can be natively converted to JSON, which is |
1926
|
|
|
* called when invoking json_encode. |
1927
|
|
|
* |
1928
|
|
|
* @return mixed the data to be JSON encoded. |
1929
|
|
|
* |
1930
|
|
|
* @see JsonSerializable |
1931
|
|
|
*/ |
1932
|
|
|
public function jsonSerialize() |
1933
|
|
|
{ |
1934
|
|
|
} |
1935
|
|
|
|
1936
|
|
|
/** |
1937
|
|
|
* Creates a shallow copy of the collection. |
1938
|
|
|
* |
1939
|
|
|
* @return Collection a shallow copy of the collection. |
1940
|
|
|
*/ |
1941
|
|
|
public function copy(): Collection |
1942
|
|
|
{ |
1943
|
|
|
} |
1944
|
|
|
|
1945
|
|
|
/** |
1946
|
|
|
* Invoked when calling var_dump. |
1947
|
|
|
* |
1948
|
|
|
* @return array |
1949
|
|
|
*/ |
1950
|
|
|
public function __debugInfo(): array |
1951
|
|
|
{ |
1952
|
|
|
} |
1953
|
|
|
|
1954
|
|
|
/** |
1955
|
|
|
* Returns a string representation of the collection, which is invoked when |
1956
|
|
|
* the collection is converted to a string. |
1957
|
|
|
* |
1958
|
|
|
* @return string |
1959
|
|
|
*/ |
1960
|
|
|
public function __toString(): string |
1961
|
|
|
{ |
1962
|
|
|
} |
1963
|
|
|
// END GenericCollection Trait |
1964
|
|
|
|
1965
|
|
|
/** |
1966
|
|
|
* Creates a new set using the values of an array or Traversable object. |
1967
|
|
|
* The keys of either will not be preserved. |
1968
|
|
|
* |
1969
|
|
|
* @param array|Traversable|null $values |
1970
|
|
|
*/ |
1971
|
|
|
public function __construct($values = null) |
1972
|
|
|
{ |
1973
|
|
|
} |
1974
|
|
|
|
1975
|
|
|
/** |
1976
|
|
|
* Adds zero or more values to the set. |
1977
|
|
|
* |
1978
|
|
|
* @param mixed ...$values |
1979
|
|
|
*/ |
|
|
|
|
1980
|
|
|
public function add(...$values) |
1981
|
|
|
{ |
1982
|
|
|
} |
1983
|
|
|
|
1984
|
|
|
/** |
1985
|
|
|
* Ensures that enough memory is allocated for a specified capacity. This |
1986
|
|
|
* potentially reduces the number of reallocations as the size increases. |
1987
|
|
|
* |
1988
|
|
|
* @param int $capacity The number of values for which capacity should be |
1989
|
|
|
* allocated. Capacity will stay the same if this value |
1990
|
|
|
* is less than or equal to the current capacity. |
1991
|
|
|
*/ |
1992
|
|
|
public function allocate(int $capacity) |
1993
|
|
|
{ |
1994
|
|
|
} |
1995
|
|
|
|
1996
|
|
|
/** |
1997
|
|
|
* Returns the current capacity of the set. |
1998
|
|
|
* |
1999
|
|
|
* @return int |
2000
|
|
|
*/ |
2001
|
|
|
public function capacity(): int |
2002
|
|
|
{ |
2003
|
|
|
} |
2004
|
|
|
|
2005
|
|
|
/** |
2006
|
|
|
* Clear all elements in the Set |
2007
|
|
|
*/ |
2008
|
|
|
public function clear() |
2009
|
|
|
{ |
2010
|
|
|
} |
2011
|
|
|
|
2012
|
|
|
/** |
2013
|
|
|
* Determines whether the set contains all of zero or more values. |
2014
|
|
|
* |
2015
|
|
|
* @param mixed ...$values |
2016
|
|
|
* |
2017
|
|
|
* @return bool true if at least one value was provided and the set |
2018
|
|
|
* contains all given values, false otherwise. |
2019
|
|
|
*/ |
|
|
|
|
2020
|
|
|
public function contains(...$values): bool |
2021
|
|
|
{ |
2022
|
|
|
} |
2023
|
|
|
|
2024
|
|
|
/** |
2025
|
|
|
* Returns the number of elements in the Stack |
2026
|
|
|
* |
2027
|
|
|
* @return int |
2028
|
|
|
*/ |
2029
|
|
|
public function count(): int |
2030
|
|
|
{ |
2031
|
|
|
} |
2032
|
|
|
|
2033
|
|
|
/** |
2034
|
|
|
* Creates a new set using values from this set that aren't in another set. |
2035
|
|
|
* |
2036
|
|
|
* Formally: A \ B = {x ∈ A | x ∉ B} |
2037
|
|
|
* |
2038
|
|
|
* @param Set $set |
2039
|
|
|
* |
2040
|
|
|
* @return Set |
2041
|
|
|
*/ |
2042
|
|
|
public function diff(Set $set): Set |
2043
|
|
|
{ |
2044
|
|
|
} |
2045
|
|
|
|
2046
|
|
|
/** |
2047
|
|
|
* Creates a new set using values in either this set or in another set, |
2048
|
|
|
* but not in both. |
2049
|
|
|
* |
2050
|
|
|
* Formally: A ⊖ B = {x : x ∈ (A \ B) ∪ (B \ A)} |
2051
|
|
|
* |
2052
|
|
|
* @param Set $set |
2053
|
|
|
* |
2054
|
|
|
* @return Set |
2055
|
|
|
*/ |
2056
|
|
|
public function xor(Set $set): Set |
2057
|
|
|
{ |
2058
|
|
|
} |
2059
|
|
|
|
2060
|
|
|
/** |
2061
|
|
|
* Returns a new set containing only the values for which a callback |
2062
|
|
|
* returns true. A boolean test will be used if a callback is not provided. |
2063
|
|
|
* |
2064
|
|
|
* @param callable|null $callback Accepts a value, returns a boolean: |
2065
|
|
|
* true : include the value, |
2066
|
|
|
* false: skip the value. |
2067
|
|
|
* |
2068
|
|
|
* @return Set |
2069
|
|
|
*/ |
2070
|
|
|
public function filter(callable $callback = null): Set |
2071
|
|
|
{ |
2072
|
|
|
} |
2073
|
|
|
|
2074
|
|
|
/** |
2075
|
|
|
* Returns the first value in the set. |
2076
|
|
|
* |
2077
|
|
|
* @return mixed the first value in the set. |
2078
|
|
|
*/ |
2079
|
|
|
public function first() |
2080
|
|
|
{ |
2081
|
|
|
} |
2082
|
|
|
|
2083
|
|
|
/** |
2084
|
|
|
* Returns the value at a specified position in the set. |
2085
|
|
|
* |
2086
|
|
|
* @param int $position |
2087
|
|
|
* |
2088
|
|
|
* @return mixed|null |
2089
|
|
|
* |
2090
|
|
|
* @throws OutOfRangeException |
2091
|
|
|
*/ |
2092
|
|
|
public function get(int $position) |
2093
|
|
|
{ |
2094
|
|
|
} |
2095
|
|
|
|
2096
|
|
|
/** |
2097
|
|
|
* Creates a new set using values common to both this set and another set. |
2098
|
|
|
* |
2099
|
|
|
* In other words, returns a copy of this set with all values removed that |
2100
|
|
|
* aren't in the other set. |
2101
|
|
|
* |
2102
|
|
|
* Formally: A ∩ B = {x : x ∈ A ∧ x ∈ B} |
2103
|
|
|
* |
2104
|
|
|
* @param Set $set |
2105
|
|
|
* |
2106
|
|
|
* @return Set |
2107
|
|
|
*/ |
2108
|
|
|
public function intersect(Set $set): Set |
2109
|
|
|
{ |
2110
|
|
|
} |
2111
|
|
|
|
2112
|
|
|
/** |
2113
|
|
|
* Joins all values of the set into a string, adding an optional 'glue' |
2114
|
|
|
* between them. Returns an empty string if the set is empty. |
2115
|
|
|
* |
2116
|
|
|
* @param string $glue |
|
|
|
|
2117
|
|
|
* |
2118
|
|
|
* @return string |
2119
|
|
|
*/ |
2120
|
|
|
public function join(string $glue = null): string |
2121
|
|
|
{ |
2122
|
|
|
} |
2123
|
|
|
|
2124
|
|
|
/** |
2125
|
|
|
* Returns the last value in the set. |
2126
|
|
|
* |
2127
|
|
|
* @return mixed the last value in the set. |
2128
|
|
|
*/ |
2129
|
|
|
public function last() |
2130
|
|
|
{ |
2131
|
|
|
} |
2132
|
|
|
|
2133
|
|
|
/** |
2134
|
|
|
* Iteratively reduces the set to a single value using a callback. |
2135
|
|
|
* |
2136
|
|
|
* @param callable $callback Accepts the carry and current value, and |
2137
|
|
|
* returns an updated carry value. |
2138
|
|
|
* |
2139
|
|
|
* @param mixed|null $initial Optional initial carry value. |
2140
|
|
|
* |
2141
|
|
|
* @return mixed The carry value of the final iteration, or the initial |
2142
|
|
|
* value if the set was empty. |
2143
|
|
|
*/ |
2144
|
|
|
public function reduce(callable $callback, $initial = null) |
2145
|
|
|
{ |
2146
|
|
|
} |
2147
|
|
|
|
2148
|
|
|
/** |
2149
|
|
|
* Removes zero or more values from the set. |
2150
|
|
|
* |
2151
|
|
|
* @param mixed ...$values |
2152
|
|
|
*/ |
|
|
|
|
2153
|
|
|
public function remove(...$values) |
2154
|
|
|
{ |
2155
|
|
|
} |
2156
|
|
|
|
2157
|
|
|
/** |
2158
|
|
|
* Reverses the set in-place. |
2159
|
|
|
*/ |
2160
|
|
|
public function reverse() |
2161
|
|
|
{ |
2162
|
|
|
} |
2163
|
|
|
|
2164
|
|
|
/** |
2165
|
|
|
* Returns a reversed copy of the set. |
2166
|
|
|
* |
2167
|
|
|
* @return Set |
2168
|
|
|
*/ |
2169
|
|
|
public function reversed(): Set |
2170
|
|
|
{ |
2171
|
|
|
} |
2172
|
|
|
|
2173
|
|
|
/** |
2174
|
|
|
* Returns a subset of a given length starting at a specified offset. |
2175
|
|
|
* |
2176
|
|
|
* @param int $offset If the offset is non-negative, the set will start |
2177
|
|
|
* at that offset in the set. If offset is negative, |
2178
|
|
|
* the set will start that far from the end. |
2179
|
|
|
* |
2180
|
|
|
* @param int $length If a length is given and is positive, the resulting |
|
|
|
|
2181
|
|
|
* set will have up to that many values in it. |
2182
|
|
|
* If the requested length results in an overflow, only |
2183
|
|
|
* values up to the end of the set will be included. |
2184
|
|
|
* |
2185
|
|
|
* If a length is given and is negative, the set |
2186
|
|
|
* will stop that many values from the end. |
2187
|
|
|
* |
2188
|
|
|
* If a length is not provided, the resulting set |
2189
|
|
|
* will contains all values between the offset and the |
2190
|
|
|
* end of the set. |
2191
|
|
|
* |
2192
|
|
|
* @return Set |
2193
|
|
|
*/ |
2194
|
|
|
public function slice(int $offset, int $length = null): Set |
2195
|
|
|
{ |
2196
|
|
|
} |
2197
|
|
|
|
2198
|
|
|
/** |
2199
|
|
|
* Sorts the set in-place, based on an optional callable comparator. |
2200
|
|
|
* |
2201
|
|
|
* @param callable|null $comparator Accepts two values to be compared. |
2202
|
|
|
* Should return the result of a <=> b. |
2203
|
|
|
*/ |
2204
|
|
|
public function sort(callable $comparator = null) |
2205
|
|
|
{ |
2206
|
|
|
} |
2207
|
|
|
|
2208
|
|
|
/** |
2209
|
|
|
* Returns a sorted copy of the set, based on an optional callable |
2210
|
|
|
* comparator. Natural ordering will be used if a comparator is not given. |
2211
|
|
|
* |
2212
|
|
|
* @param callable|null $comparator Accepts two values to be compared. |
2213
|
|
|
* Should return the result of a <=> b. |
2214
|
|
|
* |
2215
|
|
|
* @return Set |
2216
|
|
|
*/ |
2217
|
|
|
public function sorted(callable $comparator = null): Set |
2218
|
|
|
{ |
2219
|
|
|
} |
2220
|
|
|
|
2221
|
|
|
/** |
2222
|
|
|
* Returns the result of adding all given values to the set. |
2223
|
|
|
* |
2224
|
|
|
* @param array|Traversable $values |
2225
|
|
|
* |
2226
|
|
|
* @return \Ds\Set |
2227
|
|
|
*/ |
2228
|
|
|
public function merge($values): Set |
2229
|
|
|
{ |
2230
|
|
|
} |
2231
|
|
|
|
2232
|
|
|
/** |
2233
|
|
|
* Returns an array representation of the collection. |
2234
|
|
|
* |
2235
|
|
|
* The format of the returned array is implementation-dependent. Some |
2236
|
|
|
* implementations may throw an exception if an array representation |
2237
|
|
|
* could not be created (for example when object are used as keys). |
2238
|
|
|
* |
2239
|
|
|
* @return array |
2240
|
|
|
*/ |
2241
|
|
|
public function toArray(): array |
2242
|
|
|
{ |
2243
|
|
|
} |
2244
|
|
|
|
2245
|
|
|
/** |
2246
|
|
|
* Returns the sum of all values in the set. |
2247
|
|
|
* |
2248
|
|
|
* @return int|float The sum of all the values in the set. |
2249
|
|
|
*/ |
2250
|
|
|
public function sum() |
2251
|
|
|
{ |
2252
|
|
|
} |
2253
|
|
|
|
2254
|
|
|
/** |
2255
|
|
|
* Creates a new set that contains the values of this set as well as the |
2256
|
|
|
* values of another set. |
2257
|
|
|
* |
2258
|
|
|
* Formally: A ∪ B = {x: x ∈ A ∨ x ∈ B} |
2259
|
|
|
* |
2260
|
|
|
* @param Set $set |
2261
|
|
|
* |
2262
|
|
|
* @return Set |
2263
|
|
|
*/ |
2264
|
|
|
public function union(Set $set): Set |
2265
|
|
|
{ |
2266
|
|
|
} |
2267
|
|
|
|
2268
|
|
|
/** |
2269
|
|
|
* Get iterator |
2270
|
|
|
*/ |
2271
|
|
|
public function getIterator() |
2272
|
|
|
{ |
2273
|
|
|
} |
2274
|
|
|
|
2275
|
|
|
/** |
2276
|
|
|
* @inheritdoc |
2277
|
|
|
* |
2278
|
|
|
* @throws OutOfBoundsException |
2279
|
|
|
*/ |
2280
|
|
|
public function offsetSet($offset, $value) |
2281
|
|
|
{ |
2282
|
|
|
} |
2283
|
|
|
|
2284
|
|
|
/** |
2285
|
|
|
* @inheritdoc |
2286
|
|
|
*/ |
2287
|
|
|
public function offsetGet($offset) |
2288
|
|
|
{ |
2289
|
|
|
} |
2290
|
|
|
|
2291
|
|
|
/** |
2292
|
|
|
* @inheritdoc |
2293
|
|
|
* |
2294
|
|
|
* @throws Error |
2295
|
|
|
*/ |
2296
|
|
|
public function offsetExists($offset) |
2297
|
|
|
{ |
2298
|
|
|
} |
2299
|
|
|
|
2300
|
|
|
/** |
2301
|
|
|
* @inheritdoc |
2302
|
|
|
* |
2303
|
|
|
* @throws Error |
2304
|
|
|
*/ |
2305
|
|
|
public function offsetUnset($offset) |
2306
|
|
|
{ |
2307
|
|
|
} |
2308
|
|
|
} |
2309
|
|
|
|
2310
|
|
|
/** |
2311
|
|
|
* A “last in, first out” or “LIFO” collection that only allows access to the |
2312
|
|
|
* value at the top of the structure and iterates in that order, destructively. |
2313
|
|
|
* |
2314
|
|
|
* @package Ds |
2315
|
|
|
*/ |
2316
|
|
|
final class Stack implements IteratorAggregate, ArrayAccess, Collection |
|
|
|
|
2317
|
|
|
{ |
2318
|
|
|
// BEGIN GenericCollection Trait |
2319
|
|
|
/** |
2320
|
|
|
* Returns whether the collection is empty. |
2321
|
|
|
* |
2322
|
|
|
* This should be equivalent to a count of zero, but is not required. |
2323
|
|
|
* Implementations should define what empty means in their own context. |
2324
|
|
|
* |
2325
|
|
|
* @return bool whether the collection is empty. |
2326
|
|
|
*/ |
2327
|
|
|
public function isEmpty(): bool |
2328
|
|
|
{ |
2329
|
|
|
} |
2330
|
|
|
|
2331
|
|
|
/** |
2332
|
|
|
* Returns a representation that can be natively converted to JSON, which is |
2333
|
|
|
* called when invoking json_encode. |
2334
|
|
|
* |
2335
|
|
|
* @return mixed the data to be JSON encoded. |
2336
|
|
|
* |
2337
|
|
|
* @see JsonSerializable |
2338
|
|
|
*/ |
2339
|
|
|
public function jsonSerialize() |
2340
|
|
|
{ |
2341
|
|
|
} |
2342
|
|
|
|
2343
|
|
|
/** |
2344
|
|
|
* Creates a shallow copy of the collection. |
2345
|
|
|
* |
2346
|
|
|
* @return Collection a shallow copy of the collection. |
2347
|
|
|
*/ |
2348
|
|
|
public function copy(): Collection |
2349
|
|
|
{ |
2350
|
|
|
} |
2351
|
|
|
|
2352
|
|
|
/** |
2353
|
|
|
* Invoked when calling var_dump. |
2354
|
|
|
* |
2355
|
|
|
* @return array |
2356
|
|
|
*/ |
2357
|
|
|
public function __debugInfo(): array |
2358
|
|
|
{ |
2359
|
|
|
} |
2360
|
|
|
|
2361
|
|
|
/** |
2362
|
|
|
* Returns a string representation of the collection, which is invoked when |
2363
|
|
|
* the collection is converted to a string. |
2364
|
|
|
* |
2365
|
|
|
* @return string |
2366
|
|
|
*/ |
2367
|
|
|
public function __toString(): string |
2368
|
|
|
{ |
2369
|
|
|
} |
2370
|
|
|
// END GenericCollection Trait |
2371
|
|
|
|
2372
|
|
|
/** |
2373
|
|
|
* Creates an instance using the values of an array or Traversable object. |
2374
|
|
|
* |
2375
|
|
|
* @param array|Traversable $values |
|
|
|
|
2376
|
|
|
*/ |
2377
|
|
|
public function __construct($values = null) |
2378
|
|
|
{ |
2379
|
|
|
$this->vector = new Vector($values ?: []); |
2380
|
|
|
} |
2381
|
|
|
|
2382
|
|
|
/** |
2383
|
|
|
* Clear all elements in the Stack |
2384
|
|
|
*/ |
2385
|
|
|
public function clear() |
2386
|
|
|
{ |
2387
|
|
|
$this->vector->clear(); |
2388
|
|
|
} |
2389
|
|
|
|
2390
|
|
|
/** |
2391
|
|
|
* Returns the number of elements in the Stack |
2392
|
|
|
* |
2393
|
|
|
* @return int |
2394
|
|
|
*/ |
2395
|
|
|
public function count(): int |
2396
|
|
|
{ |
2397
|
|
|
return count($this->vector); |
2398
|
|
|
} |
2399
|
|
|
|
2400
|
|
|
/** |
2401
|
|
|
* Ensures that enough memory is allocated for a specified capacity. This |
2402
|
|
|
* potentially reduces the number of reallocations as the size increases. |
2403
|
|
|
* |
2404
|
|
|
* @param int $capacity The number of values for which capacity should be |
2405
|
|
|
* allocated. Capacity will stay the same if this value |
2406
|
|
|
* is less than or equal to the current capacity. |
2407
|
|
|
*/ |
2408
|
|
|
public function allocate(int $capacity) |
2409
|
|
|
{ |
2410
|
|
|
$this->vector->allocate($capacity); |
2411
|
|
|
} |
2412
|
|
|
|
2413
|
|
|
/** |
2414
|
|
|
* Returns the current capacity of the stack. |
2415
|
|
|
* |
2416
|
|
|
* @return int |
2417
|
|
|
*/ |
2418
|
|
|
public function capacity(): int |
2419
|
|
|
{ |
2420
|
|
|
return $this->vector->capacity(); |
2421
|
|
|
} |
2422
|
|
|
|
2423
|
|
|
/** |
2424
|
|
|
* Returns the value at the top of the stack without removing it. |
2425
|
|
|
* |
2426
|
|
|
* @return mixed |
2427
|
|
|
* |
2428
|
|
|
* @throws \UnderflowException if the stack is empty. |
2429
|
|
|
*/ |
2430
|
|
|
public function peek() |
2431
|
|
|
{ |
2432
|
|
|
return $this->vector->last(); |
2433
|
|
|
} |
2434
|
|
|
|
2435
|
|
|
/** |
2436
|
|
|
* Returns and removes the value at the top of the stack. |
2437
|
|
|
* |
2438
|
|
|
* @return mixed |
2439
|
|
|
* |
2440
|
|
|
* @throws \UnderflowException if the stack is empty. |
2441
|
|
|
*/ |
2442
|
|
|
public function pop() |
2443
|
|
|
{ |
2444
|
|
|
return $this->vector->pop(); |
2445
|
|
|
} |
2446
|
|
|
|
2447
|
|
|
/** |
2448
|
|
|
* Pushes zero or more values onto the top of the stack. |
2449
|
|
|
* |
2450
|
|
|
* @param mixed ...$values |
2451
|
|
|
*/ |
|
|
|
|
2452
|
|
|
public function push(...$values) |
2453
|
|
|
{ |
2454
|
|
|
$this->vector->push(...$values); |
2455
|
|
|
} |
2456
|
|
|
|
2457
|
|
|
/** |
2458
|
|
|
* Returns an array representation of the collection. |
2459
|
|
|
* |
2460
|
|
|
* The format of the returned array is implementation-dependent. Some |
2461
|
|
|
* implementations may throw an exception if an array representation |
2462
|
|
|
* could not be created (for example when object are used as keys). |
2463
|
|
|
* |
2464
|
|
|
* @return array |
2465
|
|
|
*/ |
2466
|
|
|
public function toArray(): array |
2467
|
|
|
{ |
2468
|
|
|
return array_reverse($this->vector->toArray()); |
2469
|
|
|
} |
2470
|
|
|
|
2471
|
|
|
/** |
2472
|
|
|
* |
2473
|
|
|
*/ |
2474
|
|
|
public function getIterator() |
2475
|
|
|
{ |
2476
|
|
|
while ( ! $this->isEmpty()) { |
2477
|
|
|
yield $this->pop(); |
2478
|
|
|
} |
2479
|
|
|
} |
2480
|
|
|
|
2481
|
|
|
/** |
2482
|
|
|
* @inheritdoc |
2483
|
|
|
* |
2484
|
|
|
* @throws OutOfBoundsException |
2485
|
|
|
*/ |
2486
|
|
|
public function offsetSet($offset, $value) |
2487
|
|
|
{ |
2488
|
|
|
if ($offset === null) { |
2489
|
|
|
$this->push($value); |
2490
|
|
|
} else { |
2491
|
|
|
throw new OutOfBoundsException(); |
2492
|
|
|
} |
2493
|
|
|
} |
2494
|
|
|
|
2495
|
|
|
/** |
2496
|
|
|
* @inheritdoc |
2497
|
|
|
* |
2498
|
|
|
* @throws Error |
2499
|
|
|
*/ |
2500
|
|
|
public function offsetGet($offset) |
2501
|
|
|
{ |
2502
|
|
|
throw new Error(); |
2503
|
|
|
} |
2504
|
|
|
|
2505
|
|
|
/** |
2506
|
|
|
* @inheritdoc |
2507
|
|
|
* |
2508
|
|
|
* @throws Error |
2509
|
|
|
*/ |
2510
|
|
|
public function offsetUnset($offset) |
2511
|
|
|
{ |
2512
|
|
|
throw new Error(); |
2513
|
|
|
} |
2514
|
|
|
|
2515
|
|
|
/** |
2516
|
|
|
* @inheritdoc |
2517
|
|
|
* |
2518
|
|
|
* @throws Error |
2519
|
|
|
*/ |
2520
|
|
|
public function offsetExists($offset) |
2521
|
|
|
{ |
2522
|
|
|
throw new Error(); |
2523
|
|
|
} |
2524
|
|
|
} |
2525
|
|
|
|
2526
|
|
|
/** |
2527
|
|
|
* A Vector is a sequence of values in a contiguous buffer that grows and |
2528
|
|
|
* shrinks automatically. It’s the most efficient sequential structure because |
2529
|
|
|
* a value’s index is a direct mapping to its index in the buffer, and the |
2530
|
|
|
* growth factor isn't bound to a specific multiple or exponent. |
2531
|
|
|
* |
2532
|
|
|
* @package Ds |
2533
|
|
|
*/ |
2534
|
|
View Code Duplication |
final class Vector implements IteratorAggregate, ArrayAccess, Sequence |
|
|
|
|
2535
|
|
|
{ |
2536
|
|
|
const MIN_CAPACITY = 8; |
2537
|
|
|
|
2538
|
|
|
// BEGIN GenericCollection Trait |
2539
|
|
|
/** |
2540
|
|
|
* Returns whether the collection is empty. |
2541
|
|
|
* |
2542
|
|
|
* This should be equivalent to a count of zero, but is not required. |
2543
|
|
|
* Implementations should define what empty means in their own context. |
2544
|
|
|
* |
2545
|
|
|
* @return bool whether the collection is empty. |
2546
|
|
|
*/ |
2547
|
|
|
public function isEmpty(): bool |
2548
|
|
|
{ |
2549
|
|
|
} |
2550
|
|
|
|
2551
|
|
|
/** |
2552
|
|
|
* Returns a representation that can be natively converted to JSON, which is |
2553
|
|
|
* called when invoking json_encode. |
2554
|
|
|
* |
2555
|
|
|
* @return mixed the data to be JSON encoded. |
2556
|
|
|
* |
2557
|
|
|
* @see JsonSerializable |
2558
|
|
|
*/ |
2559
|
|
|
public function jsonSerialize() |
2560
|
|
|
{ |
2561
|
|
|
} |
2562
|
|
|
|
2563
|
|
|
/** |
2564
|
|
|
* Creates a shallow copy of the collection. |
2565
|
|
|
* |
2566
|
|
|
* @return Collection a shallow copy of the collection. |
2567
|
|
|
*/ |
2568
|
|
|
public function copy(): Collection |
2569
|
|
|
{ |
2570
|
|
|
} |
2571
|
|
|
|
2572
|
|
|
/** |
2573
|
|
|
* Invoked when calling var_dump. |
2574
|
|
|
* |
2575
|
|
|
* @return array |
2576
|
|
|
*/ |
2577
|
|
|
public function __debugInfo(): array |
2578
|
|
|
{ |
2579
|
|
|
} |
2580
|
|
|
|
2581
|
|
|
/** |
2582
|
|
|
* Returns a string representation of the collection, which is invoked when |
2583
|
|
|
* the collection is converted to a string. |
2584
|
|
|
* |
2585
|
|
|
* @return string |
2586
|
|
|
*/ |
2587
|
|
|
public function __toString(): string |
2588
|
|
|
{ |
2589
|
|
|
} |
2590
|
|
|
// END GenericCollection Trait |
2591
|
|
|
|
2592
|
|
|
// BEGIN GenericSequence Trait |
2593
|
|
|
/** |
2594
|
|
|
* @inheritDoc |
2595
|
|
|
*/ |
2596
|
|
|
public function __construct($values = null) |
2597
|
|
|
{ |
2598
|
|
|
} |
2599
|
|
|
|
2600
|
|
|
/** |
2601
|
|
|
* Returns an array representation of the collection. |
2602
|
|
|
* |
2603
|
|
|
* The format of the returned array is implementation-dependent. Some |
2604
|
|
|
* implementations may throw an exception if an array representation |
2605
|
|
|
* could not be created (for example when object are used as keys). |
2606
|
|
|
* |
2607
|
|
|
* @return array |
2608
|
|
|
*/ |
2609
|
|
|
public function toArray(): array |
2610
|
|
|
{ |
2611
|
|
|
} |
2612
|
|
|
|
2613
|
|
|
/** |
2614
|
|
|
* @inheritdoc |
2615
|
|
|
*/ |
2616
|
|
|
public function apply(callable $callback) |
2617
|
|
|
{ |
2618
|
|
|
} |
2619
|
|
|
|
2620
|
|
|
/** |
2621
|
|
|
* @inheritdoc |
2622
|
|
|
*/ |
2623
|
|
|
public function merge($values): Sequence |
2624
|
|
|
{ |
2625
|
|
|
} |
2626
|
|
|
|
2627
|
|
|
/** |
2628
|
|
|
* @inheritdoc |
2629
|
|
|
*/ |
2630
|
|
|
public function count(): int |
2631
|
|
|
{ |
2632
|
|
|
} |
2633
|
|
|
|
2634
|
|
|
/** |
2635
|
|
|
* @inheritDoc |
2636
|
|
|
*/ |
2637
|
|
|
public function contains(...$values): bool |
2638
|
|
|
{ |
2639
|
|
|
} |
2640
|
|
|
|
2641
|
|
|
/** |
2642
|
|
|
* @inheritDoc |
2643
|
|
|
*/ |
2644
|
|
|
public function filter(callable $callback = null): Sequence |
2645
|
|
|
{ |
2646
|
|
|
} |
2647
|
|
|
|
2648
|
|
|
/** |
2649
|
|
|
* @inheritDoc |
2650
|
|
|
*/ |
2651
|
|
|
public function find($value) |
2652
|
|
|
{ |
2653
|
|
|
} |
2654
|
|
|
|
2655
|
|
|
/** |
2656
|
|
|
* @inheritDoc |
2657
|
|
|
*/ |
2658
|
|
|
public function first() |
2659
|
|
|
{ |
2660
|
|
|
} |
2661
|
|
|
|
2662
|
|
|
/** |
2663
|
|
|
* @inheritDoc |
2664
|
|
|
*/ |
2665
|
|
|
public function get(int $index) |
2666
|
|
|
{ |
2667
|
|
|
} |
2668
|
|
|
|
2669
|
|
|
/** |
2670
|
|
|
* @inheritDoc |
2671
|
|
|
*/ |
2672
|
|
|
public function insert(int $index, ...$values) |
2673
|
|
|
{ |
2674
|
|
|
} |
2675
|
|
|
|
2676
|
|
|
/** |
2677
|
|
|
* @inheritDoc |
2678
|
|
|
*/ |
2679
|
|
|
public function join(string $glue = null): string |
2680
|
|
|
{ |
2681
|
|
|
} |
2682
|
|
|
|
2683
|
|
|
/** |
2684
|
|
|
* @inheritDoc |
2685
|
|
|
*/ |
2686
|
|
|
public function last() |
2687
|
|
|
{ |
2688
|
|
|
} |
2689
|
|
|
|
2690
|
|
|
/** |
2691
|
|
|
* @inheritDoc |
2692
|
|
|
*/ |
2693
|
|
|
public function map(callable $callback): Sequence |
2694
|
|
|
{ |
2695
|
|
|
} |
2696
|
|
|
|
2697
|
|
|
/** |
2698
|
|
|
* @inheritDoc |
2699
|
|
|
*/ |
2700
|
|
|
public function pop() |
2701
|
|
|
{ |
2702
|
|
|
} |
2703
|
|
|
|
2704
|
|
|
/** |
2705
|
|
|
* @inheritDoc |
2706
|
|
|
*/ |
2707
|
|
|
public function push(...$values) |
2708
|
|
|
{ |
2709
|
|
|
} |
2710
|
|
|
|
2711
|
|
|
/** |
2712
|
|
|
* @inheritDoc |
2713
|
|
|
*/ |
2714
|
|
|
public function reduce(callable $callback, $initial = null) |
2715
|
|
|
{ |
2716
|
|
|
} |
2717
|
|
|
|
2718
|
|
|
/** |
2719
|
|
|
* @inheritDoc |
2720
|
|
|
*/ |
2721
|
|
|
public function remove(int $index) |
2722
|
|
|
{ |
2723
|
|
|
} |
2724
|
|
|
|
2725
|
|
|
/** |
2726
|
|
|
* @inheritDoc |
2727
|
|
|
*/ |
2728
|
|
|
public function reverse() |
2729
|
|
|
{ |
2730
|
|
|
} |
2731
|
|
|
|
2732
|
|
|
/** |
2733
|
|
|
* @inheritDoc |
2734
|
|
|
*/ |
2735
|
|
|
public function reversed(): Sequence |
2736
|
|
|
{ |
2737
|
|
|
} |
2738
|
|
|
|
2739
|
|
|
/** |
2740
|
|
|
* @inheritDoc |
2741
|
|
|
*/ |
2742
|
|
|
public function rotate(int $rotations) |
2743
|
|
|
{ |
2744
|
|
|
} |
2745
|
|
|
|
2746
|
|
|
/** |
2747
|
|
|
* @inheritDoc |
2748
|
|
|
*/ |
2749
|
|
|
public function set(int $index, $value) |
2750
|
|
|
{ |
2751
|
|
|
} |
2752
|
|
|
|
2753
|
|
|
/** |
2754
|
|
|
* @inheritDoc |
2755
|
|
|
*/ |
2756
|
|
|
public function shift() |
2757
|
|
|
{ |
2758
|
|
|
} |
2759
|
|
|
|
2760
|
|
|
/** |
2761
|
|
|
* @inheritDoc |
2762
|
|
|
*/ |
2763
|
|
|
public function slice(int $offset, int $length = null): Sequence |
2764
|
|
|
{ |
2765
|
|
|
} |
2766
|
|
|
|
2767
|
|
|
/** |
2768
|
|
|
* @inheritDoc |
2769
|
|
|
*/ |
2770
|
|
|
public function sort(callable $comparator = null) |
2771
|
|
|
{ |
2772
|
|
|
} |
2773
|
|
|
|
2774
|
|
|
/** |
2775
|
|
|
* @inheritDoc |
2776
|
|
|
*/ |
2777
|
|
|
public function sorted(callable $comparator = null): Sequence |
2778
|
|
|
{ |
2779
|
|
|
} |
2780
|
|
|
|
2781
|
|
|
/** |
2782
|
|
|
* @inheritDoc |
2783
|
|
|
*/ |
2784
|
|
|
public function sum() |
2785
|
|
|
{ |
2786
|
|
|
} |
2787
|
|
|
|
2788
|
|
|
/** |
2789
|
|
|
* @inheritDoc |
2790
|
|
|
*/ |
2791
|
|
|
public function unshift(...$values) |
2792
|
|
|
{ |
2793
|
|
|
} |
2794
|
|
|
|
2795
|
|
|
/** |
2796
|
|
|
* |
2797
|
|
|
*/ |
2798
|
|
|
public function getIterator() |
2799
|
|
|
{ |
2800
|
|
|
} |
2801
|
|
|
|
2802
|
|
|
/** |
2803
|
|
|
* @inheritdoc |
2804
|
|
|
*/ |
2805
|
|
|
public function clear() |
2806
|
|
|
{ |
2807
|
|
|
} |
2808
|
|
|
|
2809
|
|
|
/** |
2810
|
|
|
* @inheritdoc |
2811
|
|
|
*/ |
2812
|
|
|
public function offsetSet($offset, $value) |
2813
|
|
|
{ |
2814
|
|
|
} |
2815
|
|
|
|
2816
|
|
|
/** |
2817
|
|
|
* @inheritdoc |
2818
|
|
|
*/ |
2819
|
|
|
public function &offsetGet($offset) |
2820
|
|
|
{ |
2821
|
|
|
} |
2822
|
|
|
|
2823
|
|
|
/** |
2824
|
|
|
* @inheritdoc |
2825
|
|
|
*/ |
2826
|
|
|
public function offsetUnset($offset) |
2827
|
|
|
{ |
2828
|
|
|
} |
2829
|
|
|
|
2830
|
|
|
/** |
2831
|
|
|
* @inheritdoc |
2832
|
|
|
*/ |
2833
|
|
|
public function offsetExists($offset) |
2834
|
|
|
{ |
2835
|
|
|
} |
2836
|
|
|
// END GenericSequence Trait |
2837
|
|
|
|
2838
|
|
|
// BEGIN Capacity Trait |
2839
|
|
|
/** |
2840
|
|
|
* Returns the current capacity. |
2841
|
|
|
* |
2842
|
|
|
* @return int |
2843
|
|
|
*/ |
2844
|
|
|
public function capacity(): int |
2845
|
|
|
{ |
2846
|
|
|
} |
2847
|
|
|
|
2848
|
|
|
/** |
2849
|
|
|
* Ensures that enough memory is allocated for a specified capacity. This |
2850
|
|
|
* potentially reduces the number of reallocations as the size increases. |
2851
|
|
|
* |
2852
|
|
|
* @param int $capacity The number of values for which capacity should be |
2853
|
|
|
* allocated. Capacity will stay the same if this value |
2854
|
|
|
* is less than or equal to the current capacity. |
2855
|
|
|
*/ |
2856
|
|
|
public function allocate(int $capacity) |
2857
|
|
|
{ |
2858
|
|
|
} |
2859
|
|
|
|
2860
|
|
|
/** |
2861
|
|
|
* @return float the structures growth factor. |
2862
|
|
|
*/ |
2863
|
|
|
protected function getGrowthFactor(): float |
2864
|
|
|
{ |
2865
|
|
|
} |
2866
|
|
|
|
2867
|
|
|
/** |
2868
|
|
|
* @return float to multiply by when decreasing capacity. |
2869
|
|
|
*/ |
2870
|
|
|
protected function getDecayFactor(): float |
2871
|
|
|
{ |
2872
|
|
|
} |
2873
|
|
|
|
2874
|
|
|
/** |
2875
|
|
|
* @return float the ratio between size and capacity when capacity should be |
2876
|
|
|
* decreased. |
2877
|
|
|
*/ |
2878
|
|
|
protected function getTruncateThreshold(): float |
2879
|
|
|
{ |
2880
|
|
|
} |
2881
|
|
|
|
2882
|
|
|
/** |
2883
|
|
|
* Checks and adjusts capacity if required. |
2884
|
|
|
*/ |
2885
|
|
|
protected function checkCapacity() |
2886
|
|
|
{ |
2887
|
|
|
} |
2888
|
|
|
|
2889
|
|
|
/** |
2890
|
|
|
* Called when capacity should be increased to accommodate new values. |
2891
|
|
|
*/ |
2892
|
|
|
protected function increaseCapacity() |
2893
|
|
|
{ |
2894
|
|
|
} |
2895
|
|
|
|
2896
|
|
|
/** |
2897
|
|
|
* Called when capacity should be decrease if it drops below a threshold. |
2898
|
|
|
*/ |
2899
|
|
|
protected function decreaseCapacity() |
2900
|
|
|
{ |
2901
|
|
|
} |
2902
|
|
|
|
2903
|
|
|
/** |
2904
|
|
|
* @return bool whether capacity should be increased. |
2905
|
|
|
*/ |
2906
|
|
|
protected function shouldDecreaseCapacity(): bool |
2907
|
|
|
{ |
2908
|
|
|
} |
2909
|
|
|
|
2910
|
|
|
/** |
2911
|
|
|
* @return bool whether capacity should be increased. |
2912
|
|
|
*/ |
2913
|
|
|
protected function shouldIncreaseCapacity(): bool |
2914
|
|
|
{ |
2915
|
|
|
} |
2916
|
|
|
// END Capacity Trait |
2917
|
|
|
} |
2918
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.