Repository navigation
Expand file tree
/
Copy pathSequence.php
More file actions
277 lines (250 loc) · 8.34 KB
/
Copy pathSequence.php
File metadata and controls
277 lines (250 loc) · 8.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
namespace Ds;
/**
* Describes the behaviour of values arranged in a single, linear dimension.
* Some languages refer to this as a "List". It’s similar to an array that uses
* incremental integer keys, with the exception of a few characteristics:
*
* - Values will always be indexed as [0, 1, 2, …, size - 1].
* - Only allowed to access values by index in the range [0, size - 1].
*
* @package Ds
*/
interface Sequence extends Collection
{
/**
* Ensures that enough memory is allocated for a required capacity.
*
* @param int $capacity The number of values for which capacity should be
* allocated. Capacity will stay the same if this value
* is less than or equal to the current capacity.
*/
public function allocate(int $capacity);
/**
* Updates every value in the sequence by applying a callback, using the
* return value as the new value.
*
* @param callable $callback Accepts the value, returns the new value.
*/
public function apply(callable $callback);
/**
* Returns the current capacity of the sequence.
*
* @return int
*/
public function capacity(): int;
/**
* Determines whether the sequence contains all of zero or more values.
*
* @param mixed ...$values
*
* @return bool true if at least one value was provided and the sequence
* contains all given values, false otherwise.
*/
public function contains(...$values): bool;
/**
* Returns a new sequence containing only the values for which a callback
* returns true. A boolean test will be used if a callback is not provided.
*
* @param callable|null $callback Accepts a value, returns a boolean result:
* true : include the value,
* false: skip the value.
*
* @return Sequence
*/
public function filter(callable $callback = null): Sequence;
/**
* Returns the index of a given value, or false if it could not be found.
*
* @param mixed $value
*
* @return int|bool
*/
public function find($value);
/**
* Returns the first value in the sequence.
*
* @return mixed
*
* @throws \UnderflowException if the sequence is empty.
*/
public function first();
/**
* Returns the value at a given index (position) in the sequence.
*
* @param int $index
*
* @return mixed
*
* @throws \OutOfRangeException if the index is not in the range [0, size-1]
*/
public function get(int $index);
/**
* Inserts zero or more values at a given index.
*
* Each value after the index will be moved one position to the right.
* Values may be inserted at an index equal to the size of the sequence.
*
* @param int $index
* @param mixed ...$values
*
* @throws \OutOfRangeException if the index is not in the range [0, n]
*/
public function insert(int $index, ...$values);
/**
* Joins all values of the sequence into a string, adding an optional 'glue'
* between them. Returns an empty string if the sequence is empty.
*
* @param string $glue
*
* @return string
*/
public function join(string $glue = null): string;
/**
* Returns the last value in the sequence.
*
* @return mixed
*
* @throws \UnderflowException if the sequence is empty.
*/
public function last();
/**
* Returns a new sequence using the results of applying a callback to each
* value.
*
* @param callable $callback
*
* @return Sequence
*/
public function map(callable $callback): Sequence;
/**
* Returns the result of adding all given values to the sequence.
*
* @param array|\Traversable $values
*
* @return Sequence
*/
public function merge($values): Sequence;
/**
* Removes the last value in the sequence, and returns it.
*
* @return mixed what was the last value in the sequence.
*
* @throws \UnderflowException if the sequence is empty.
*/
public function pop();
/**
* Adds zero or more values to the end of the sequence.
*
* @param mixed ...$values
*/
public function push(...$values);
/**
* Iteratively reduces the sequence to a single value using a callback.
*
* @param callable $callback Accepts the carry and current value, and
* returns an updated carry value.
*
* @param mixed|null $initial Optional initial carry value.
*
* @return mixed The carry value of the final iteration, or the initial
* value if the sequence was empty.
*/
public function reduce(callable $callback, $initial = null);
/**
* Removes and returns the value at a given index in the sequence.
*
* @param int $index this index to remove.
*
* @return mixed the removed value.
*
* @throws \OutOfRangeException if the index is not in the range [0, size-1]
*/
public function remove(int $index);
/**
* Reverses the sequence in-place.
*/
public function reverse();
/**
* Returns a reversed copy of the sequence.
*
* @return Sequence
*/
public function reversed();
/**
* Rotates the sequence by a given number of rotations, which is equivalent
* to successive calls to 'shift' and 'push' if the number of rotations is
* positive, or 'pop' and 'unshift' if negative.
*
* @param int $rotations The number of rotations (can be negative).
*/
public function rotate(int $rotations);
/**
* Replaces the value at a given index in the sequence with a new value.
*
* @param int $index
* @param mixed $value
*
* @throws \OutOfRangeException if the index is not in the range [0, size-1]
*/
public function set(int $index, $value);
/**
* Removes and returns the first value in the sequence.
*
* @return mixed what was the first value in the sequence.
*
* @throws \UnderflowException if the sequence was empty.
*/
public function shift();
/**
* Returns a sub-sequence of a given length starting at a specified index.
*
* @param int $index If the index is positive, the sequence will start
* at that index in the sequence. If index is negative,
* the sequence will start that far from the end.
*
* @param int $length If a length is given and is positive, the resulting
* sequence will have up to that many values in it.
* If the length results in an overflow, only values
* up to the end of the sequence will be included.
*
* If a length is given and is negative, the sequence
* will stop that many values from the end.
*
* If a length is not provided, the resulting sequence
* will contain all values between the index and the
* end of the sequence.
*
* @return Sequence
*/
public function slice(int $index, int $length = null): Sequence;
/**
* Sorts the sequence in-place, based on an optional callable comparator.
*
* @param callable|null $comparator Accepts two values to be compared.
* Should return the result of a <=> b.
*/
public function sort(callable $comparator = null);
/**
* Returns a sorted copy of the sequence, based on an optional callable
* comparator. Natural ordering will be used if a comparator is not given.
*
* @param callable|null $comparator Accepts two values to be compared.
* Should return the result of a <=> b.
*
* @return Sequence
*/
public function sorted(callable $comparator = null): Sequence;
/**
* Returns the sum of all values in the sequence.
*
* @return int|float The sum of all the values in the sequence.
*/
public function sum();
/**
* Adds zero or more values to the front of the sequence.
*
* @param mixed ...$values
*/
public function unshift(...$values);
}