MaybeMutable the collection builtins
[hiphop-php.git] / hphp / hack / hhi / collections / ImmVector.hhi
blob045f201284a90c794166a2f78345439eb7562564
1 <?hh // decl
2 /**
3  * Copyright (c) 2014, Facebook, Inc.
4  * All rights reserved.
5  *
6  * This source code is licensed under the MIT license found in the
7  * LICENSE file in the "hack" directory of this source tree.
8  *
9  */
11 /**
12  * This file provides type information for some of HHVM's builtin classes.
13  *
14  * YOU SHOULD NEVER INCLUDE THIS FILE ANYWHERE!!!
15  */
17 /**
18  * `ImmVector` is an immutable `Vector`. HHVM provides a native implementation
19  * for this class. The PHP class definition below is not actually used at run
20  * time; it is simply provided for the typechecker and for developer reference.
21  *
22  * An `ImmVector` cannot be mutated. No elements can be added to it or removed
23  * from it, nor can elements be overwritten using assignment (i.e. `$c[$k] = $v`
24  * is not allowed).
25  *
26  * ```
27  * $v = Vector {1, 2, 3};
28  * $fv = $v->toImmVector();
29  * ```
30  *
31  * construct it with a `Traversable`:
32  *
33  * ```
34  * $a = array(1, 2, 3);
35  * $fv = new ImmVector($a);
36  * ```
37  *
38  * or use the literal syntax:
39  *
40  * ```
41  * $fv = ImmVector {1, 2, 3};
42  * ```
43  *
44  * @guide /hack/collections/introduction
45  * @guide /hack/collections/classes
46  */
48 final class ImmVector<+Tv> implements ConstVector<Tv> {
49   /**
50    * Creates an `ImmVector` from the given `Traversable`, or an empty
51    * `ImmVector` if `null` is passed.
52    *
53    * @param $it - Any `Traversable` object from which to create the `ImmVector`
54    *              (e.g., `array`). If `null`, then an empty `ImmVector` is
55    *              created.
56    */
57   <<__Rx, __OnlyRxIfArgs>>
58   public function __construct(<<__MaybeMutable, __OnlyRxIfImpl(HH\Rx\Traversable::class)>> ?Traversable<Tv> $it);
60   /**
61    * Checks if the current `ImmVector` is empty.
62    *
63    * @return - `true` if the current `ImmVector` is empty; `false` otherwise.
64    */
65   <<__Rx, __MaybeMutable>>
66   public function isEmpty(): bool;
68   /**
69    * Returns the number of elements in the current `ImmVector`.
70    *
71    * @return - The number of elements in the current `ImmVector`.
72    */
73   <<__Rx, __MaybeMutable>>
74   public function count(): int;
76   /**
77    * Returns the value at the specified key in the current `ImmVector`.
78    *
79    * If the key is not present, an exception is thrown. If you don't want an
80    * exception to be thrown, use `get()` instead.
81    *
82    * `$v = $vec->at($k)` is semantically equivalent to `$v = $vec[$k]`.
83    *
84    * @param $k - The key for which to retrieve the value.
85    *
86    * @return - The value at the specified key; or an exception if the key does
87    *           not exist.
88    */
89   <<__Rx, __MaybeMutable>>
90   public function at(int $k): Tv;
92   /**
93    * Returns the value at the specified key in the current `ImmVector`.
94    *
95    * If the key is not present, `null` is returned. If you would rather have an
96    * exception thrown when a key is not present, then use `at()`.
97    *
98    * @param $k - The key for which to retrieve the value.
99    *
100    * @return - The value at the specified key; or `null` if the key does not
101    *           exist.
102    */
103   <<__Rx, __MaybeMutable>>
104   public function get(int $k): ?Tv;
106   /**
107    * Determines if the specified key is in the current `ImmVector`.
108    *
109    * @return - `true` if the specified key is present in the current
110    *           `ImmVector`; `false` otherwise.
111    *
112    * @guide /hack/generics/constraints
113    */
114   <<__Rx, __MaybeMutable>>
115   public function containsKey<Tu super int>(Tu $k): bool;
117   /**
118    * Returns an `array` containing the values from the current `ImmVector`.
119    *
120    * This method is interchangeable with `toValuesArray()`.
121    *
122    * @return - An `array` containing the values from the current `ImmVector`.
123    */
124   <<__Rx, __MaybeMutable>>
125   /* HH_IGNORE_ERROR[2082] T30260145 */
126   public function toArray(): array<Tv>;
128   /**
129    * Returns an `array` containing the values from the current `ImmVector`.
130    *
131    * This method is interchangeable with `toArray()`.
132    *
133    * @return - An `array` containing the values from the current `ImmVector`.
134    */
135   <<__Rx, __MaybeMutable>>
136   public function toValuesArray(): varray<Tv>;
138   /**
139    * Returns an `array` whose values are the keys from the current `ImmVector`.
140    *
141    * @return - An `array` with the integer keys from the current `ImmVector`.
142    */
143   <<__Rx, __MaybeMutable>>
144   public function toKeysArray(): varray<Tv>;
146   /**
147    * Returns an iterator that points to beginning of the current `ImmVector`.
148    *
149    * @return - A `KeyedIterator` that allows you to traverse the current
150    *           `ImmVector`.
151    */
152   <<__Rx, __MutableReturn, __MaybeMutable>>
153   public function getIterator(): HH\Rx\KeyedIterator<int, Tv>;
155   /**
156    * Returns the index of the first element that matches the search value.
157    *
158    * If no element matches the search value, this function returns -1.
159    *
160    * @param $search_value - The value that will be searched for in the current
161    *                        `ImmVector`.
162    *
163    * @return - The key (index) where that value is found; -1 if it is not found.
164    *
165    * @guide /hack/generics/constraints
166    */
167   <<__Rx, __MaybeMutable>>
168   public function linearSearch<Tu super Tv>(Tu $search_value): int;
170   /**
171    * Creates an `ImmVector` from the given `Traversable`, or an empty
172    * `ImmVector` if `null` is passed.
173    *
174    * This is the static method version of the `ImmVector::__construct()`
175    * constructor.
176    *
177    * @param $items - Any `Traversable` object from which to create an
178    *                 `ImmVector` (e.g., `array`). If `null`, then an empty
179    *                 `ImmVector` is created.
180    *
181    * @return - An `ImmVector` with the values from the `Traversable`; or an
182    *           empty `ImmVector` if the `Traversable` is `null`.
183    */
184   <<__Rx, __OnlyRxIfArgs, __MaybeMutable>>
185   public static function fromItems(<<__MaybeMutable, __OnlyRxIfImpl(HH\Rx\Traversable::class)>> ?Traversable<Tv> $items): ImmVector<Tv>;
187   /**
188    * Creates an `ImmVector` from the keys of the specified container.
189    *
190    * Every key in the provided `KeyedContainer` will appear sequentially in the
191    * returned `ImmVector`, with the next available integer key assigned to each.
192    *
193    * @param $container - The container with the keys used to create the
194    *                     current `ImmVector`.
195    *
196    * @return - An `ImmVector` built from the keys of the specified container.
197    */
198   <<__Rx, __MaybeMutable>>
199   public static function fromKeysOf<Tk>(
200     ?KeyedContainer<Tk, mixed> $container,
201   ): ImmVector<Tk>;
203   /**
204    * Returns the `string` version of the current `ImmVector`, which is
205    * `"ImmVector"`.
206    *
207    * @return - The `string` `"ImmVector"`.
208    */
209   <<__Rx, __MaybeMutable>>
210   public function __toString(): string;
212   /**
213    * Returns an `Iterable` view of the current `ImmVector`.
214    *
215    * The `Iterable` returned is one that produces the values from the current
216    * `ImmVector`.
217    *
218    * @return - The `Iterable` view of the current `ImmVector`.
219    */
220   <<__Rx, __MutableReturn, __MaybeMutable>>
221   public function items(): HH\Rx\Iterable<Tv>;
223   /**
224    * Returns the current `ImmVector`.
225    *
226    * Unlike `Vector`'s `toVector()` method, this does not actually return a copy
227    * of the current `ImmVector`. Since `ImmVector`s are immutable, there is no
228    * reason to pay the cost of creating a copy of the current `ImmVector`.
229    *
230    * This method is interchangeable with `immutable()`.
231    *
232    * This method is NOT interchangeable with `values()`. `values()` returns a
233    * new `ImmVector` that is a copy of the current `ImmVector`, and thus incurs
234    * both the cost of copying the current `ImmVector`, and the memory space
235    * consumed by the new `ImmVector`.  This may be significant, for large
236    * `ImmVector`s.
237    *
238    * @return - The current `ImmVector`.
239    */
240   <<__Rx, __MaybeMutable>>
241   public function toImmVector(): ImmVector<Tv>;
243  /**
244    * Returns a `Vector` containing the elemnts of the current `ImmVector`.
245    *
246    * The returned `Vector` will, of course, be mutable.
247    *
248    * @return - A `Vector` with the elements of the current `ImmVector`.
249    */
250   <<__Rx, __MutableReturn, __MaybeMutable>>
251   /* HH_FIXME[4120]: While this violates our variance annotations, we are
252    * returning a copy of the underlying collection, so it is actually safe
253    * See #6853603. */
254   public function toVector(): Vector<Tv>;
256   /**
257    * Returns an integer-keyed `Map` based on the elements of the current
258    * `ImmVector`.
259    *
260    * The keys are `0... count() - 1`.
261    *
262    * @return - An integer-keyed `Map` with the values of the current
263    *           `ImmVector`.
264    */
265   <<__Rx, __MutableReturn, __MaybeMutable>>
266   /* HH_FIXME[4120]: While this violates our variance annotations, we are
267    * returning a copy of the underlying collection, so it is actually safe
268    * See #6853603. */
269   public function toMap(): Map<int, Tv>;
271   /**
272    * Returns an immutable integer-keyed Map (`ImmMap`) based on the elements of
273    * the current `ImmVector`.
274    *
275    * The keys are `0... count() - 1`.
276    *
277    * @return - An integer-keyed `ImmMap` with the values of the current
278    *           `ImmVector`.
279    */
280   <<__Rx, __MaybeMutable>>
281   public function toImmMap(): ImmMap<int, Tv>;
283   /**
284    * Returns a `Set` with the values of the current `ImmVector`.
285    *
286    * @return - A `Set` with the values of the current `ImmVector`.
287    */
288   <<__Rx, __MutableReturn, __MaybeMutable>>
289   /* HH_FIXME[4120]: While this violates our variance annotations, we are
290    * returning a copy of the underlying collection, so it is actually safe
291    * See #6853603. */
292   public function toSet(): Set<Tv>;
294   /**
295    * Returns an immutable Set (`ImmSet`) with the values of the current
296    * `ImmVector`.
297    *
298    * @return - An `ImmSet` with the current values of the current `ImmVector`.
299    */
300   <<__Rx, __MaybeMutable>>
301   public function toImmSet(): ImmSet<Tv>;
303   /**
304    * Returns the current `ImmVector`.
305    *
306    * Unlike `Vector`'s `toVector()` method, this does not actually return a copy
307    * of the current `ImmVector`. Since `ImmVector`s are immutable, there is no
308    * reason to pay the cost of creating a copy of the current `ImmVector`.
309    *
310    * This method is interchangeable with `toImmVector()`.
311    *
312    * This method is NOT interchangeable with `values()`. `values()` returns a
313    * new `ImmVector` that is a copy of the current `ImmVector`, and thus incurs
314    * both the cost of copying the current `ImmVector`, and the memory space
315    * consumed by the new `ImmVector`.  This may be significant, for large
316    * `ImmVector`s.
317    *
318    * @return - The current `ImmVector`.
319    */
320   <<__Rx, __MaybeMutable>>
321   public function immutable(): ImmVector<Tv>;
323   /**
324    * Returns a lazy, access-elements-only-when-needed view of the current
325    * `ImmVector`.
326    *
327    * Normally, memory is allocated for all of the elements of an `ImmVector`.
328    * With a lazy view, memory is allocated for an element only when needed or
329    * used in a calculation like in `map()` or `filter()`.
330    *
331    * @return - An integer-keyed `KeyedIterable` representing the lazy view into
332    *           the current `ImmVector`.
333    *
334    * @guide /hack/collections/examples
335    */
336   <<__Rx, __MutableReturn, __MaybeMutable>>
337   public function lazy(): HH\Rx\KeyedIterable<int, Tv>;
339   /**
340    * Returns a new `ImmVector` containing the values of the current `ImmVector`;
341    * that is, a copy of the current `ImmVector`.
342    *
343    * This method is NOT interchangeable with `toImmVector()` and `immutable()`.
344    * `toImmVector()` and `immutable()` return the current `ImmVector`, and do
345    * not incur the cost of copying the current `ImmVector`, or the memory space
346    * consumed by the new `ImmVector`.  This may be significant, for large
347    * `ImmVector`s.
348    *
349    * @return - A new `ImmVector` containing the values of the current
350    *           `ImmVector`.
351    */
352   <<__Rx, __MutableReturn, __MaybeMutable>>
353   public function values(): ImmVector<Tv>;
355   /**
356    * Returns an `ImmVector` containing the keys, as values, of the current
357    * `ImmVector`.
358    *
359    * @return - An `ImmVector` containing, as values, the integer keys of the
360    *           current `ImmVector`.
361    */
362   <<__Rx, __MutableReturn, __MaybeMutable>>
363   public function keys(): ImmVector<int>;
365   /**
366    * Returns an `ImmVector` containing the results of applying an operation to
367    * each value in the current `ImmVector`.
368    *
369    * `map()`'s result contains a value for every value in the current
370    * `ImmVector`; unlike `filter()`, where only values that meet a certain
371    * criterion are included in the resulting `ImmVector`.
372    *
373    * @param $callback - The callback containing the operation to apply to the
374    *                    current `ImmVector`'s values.
375    *
376    * @return - An `ImmVector` containing the results of applying a user-specified
377    *           operation to each value of the current `ImmVector` in turn.
378    *
379    * @guide /hack/collections/examples
380    */
381   <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
382   public function map<Tu>(<<__OnlyRxIfRxFunc>>(function(Tv): Tu) $callback): ImmVector<Tu>;
384   /**
385    * Returns an `ImmVector` containing the results of applying an operation to
386    * each key/value pair in the current `ImmVector`.
387    *
388    * `mapWithKey()`'s result contains a value for every key/value pair in the
389    * current `ImmVector`; unlike `filterWithKey()`, where only values whose
390    * key/value pairs meet a certain criterion are included in the resulting
391    * `ImmVector`.
392    *
393    * @param $callback - The callback containing the operation to apply to the
394    *                    current `ImmVector`'s key/value pairs.
395    *
396    * @return - An `ImmVector` containing the results of applying a
397    *           user-specified operation to each key/value pair of the current
398    *           `ImmVector` in turn.
399    */
400   <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
401   public function mapWithKey<Tu>(<<__OnlyRxIfRxFunc>>(function(int, Tv): Tu) $callback):
402     ImmVector<Tu>;
404   /**
405    * Returns a `ImmVector` containing the values of the current `ImmVector` that
406    * meet a supplied condition.
407    *
408    * `filter()`'s result contains only values that meet the provided criterion;
409    * unlike `map()`, where a value is included for each value in the original
410    * `ImmVector`.
411    *
412    * @param $callback - The callback containing the condition to apply to the
413    *                    current `ImmVector` values.
414    *
415    * @return - An `ImmVector` containing the values after a user-specified
416    *           condition is applied.
417    *
418    * @guide /hack/collections/examples
419    */
420   <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
421   public function filter(<<__OnlyRxIfRxFunc>>(function(Tv): bool) $callback): ImmVector<Tv>;
423   /**
424    * Returns an `ImmVector` containing the values of the current `ImmVector`
425    * that meet a supplied condition applied to its keys and values.
426    *
427    * `filterWithKey()`'s result contains only values whose key/value pairs
428    * satisfy the provided criterion; unlike `mapWithKey()`, which contains
429    * results derived from every key/value pair in the original `ImmVector`.
430    *
431    * @param $callback - The callback containing the condition to apply to the
432    *                    `ImmVector`'s key/value pairs. For each key/value pair,
433    *                    the key is passed as the first parameter to the
434    *                    callback, and the value is passed as the second
435    *                    parameter.
436    *
437    * @return - An `ImmVector` containing the values of the current `ImmVector`
438    *           for which a user-specified test condition returns true when
439    *           applied to the corresponding key/value pairs.
440    *
441    */
442   <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
443   public function filterWithKey(<<__OnlyRxIfRxFunc>>(function(int, Tv): bool) $callback):
444     ImmVector<Tv>;
446   /**
447    * Returns an `ImmVector` where each element is a `Pair` that combines the
448    * element of the current `ImmVector` and the provided `Traversable`.
449    *
450    * If the number of elements of the current `ImmVector` are not equal to the
451    * number of elements in the `Traversable`, then only the combined elements
452    * up to and including the final element of the one with the least number of
453    * elements is included.
454    *
455    * @param $traversable - The `Traversable` to use to combine with the
456    *                       elements of the current `ImmVector`.
457    *
458    * @return - An `ImmVector` that combines the values of the current
459    *           `ImmVector` with the provided `Traversable`.
460    */
461   <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
462   public function zip<Tu>(<<__MaybeMutable, __OnlyRxIfImpl(HH\Rx\Traversable::class)>> Traversable<Tu> $traversable):
463     ImmVector<Pair<Tv, Tu>>;
465   /**
466    * Returns an `ImmVector` containing the first `$n` values of the current
467    * `ImmVector`.
468    *
469    * The returned `ImmVector` will always be a subset (but not necessarily a
470    * proper subset) of the current `ImmVector`. If `$n` is greater than the
471    * length of the current `ImmVector`, the returned `ImmVector` will contain
472    * all elements of the current `ImmVector`.
473    *
474    * `$n` is 1-based. So the first element is 1, the second 2, etc.
475    *
476    * @param $n - The last element that will be included in the returned
477    *             `ImmVector`.
478    *
479    * @return - An `ImmVector` that is a subset of the current `ImmVector` up to
480    *           `$n` elements.
481    */
482   <<__Rx, __MutableReturn, __MaybeMutable>>
483   public function take(int $n): ImmVector<Tv>;
485   /**
486    * Returns an `ImmVector` containing the values of the current `ImmVector` up
487    * to but not including the first value that produces `false` when passed to
488    * the specified callback. That is, takes the continuous prefix of values in
489    * the current `ImmVector` for which the specified callback returns `true`.
490    *
491    * The returned `ImmVector` will always be a subset (but not necessarily a
492    * proper subset) of the current `ImmVector`.
493    *
494    * @param $fn - The callback that is used to determine the stopping condition.
495    *
496    * @return - An `ImmVector` that is a subset of the current `ImmVector` up
497    *           until when the callback returns `false`.
498    */
499   <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
500   public function takeWhile(<<__OnlyRxIfRxFunc>>(function(Tv): bool) $fn): ImmVector<Tv>;
502   /**
503    * Returns an `ImmVector` containing the values after the `$n`-th element of
504    * the current `ImmVector`.
505    *
506    * The returned `ImmVector` will always be a subset (but not necessarily a
507    * proper subset) of the current `ImmVector`. If `$n` is greater than or equal
508    * to the length of the current `ImmVector`, the returned `ImmVector` will
509    * contain no elements. If `$n` is negative, the returned `ImmVector` will
510    * contain all elements of the current `ImmVector`.
511    *
512    * `$n` is 1-based. So the first element is 1, the second 2, etc.
513    *
514    * @param $n - The last element to be skipped; the `$n+1` element will be the
515    *             first one in the returned `ImmVector`.
516    *
517    * @return - An `ImmVector` that is a subset of the current `ImmVector`
518    *           containing values after the specified `$n`-th element.
519    */
520   <<__Rx, __MutableReturn, __MaybeMutable>>
521   public function skip(int $n): ImmVector<Tv>;
523   /**
524    * Returns an `ImmVector` containing the values of the current `ImmVector`
525    * starting after and including the first value that produces `false` when
526    * passed to the specified callback. That is, skips the continuous prefix of
527    * values in the current `ImmVector` for which the specified callback returns
528    * `true`.
529    *
530    * The returned `ImmVector` will always be a subset (but not necessarily a
531    * proper subset) of the current `ImmVector`.
532    *
533    * @param $fn - The callback used to determine the starting element for the
534    *              returned `ImmVector`.
535    *
536    * @return - An `ImmVector` that is a subset of the current `ImmVector`
537    *           starting with the value for which the callback first returns
538    *           `false`.
539    */
540   <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
541   public function skipWhile(<<__OnlyRxIfRxFunc>>(function(Tv): bool) $fn): ImmVector<Tv>;
543   /**
544    * Returns a subset of the current `ImmVector` starting from a given key up
545    * to, but not including, the element at the provided length from the
546    * starting key.
547    *
548    * `$start` is 0-based. `$len` is 1-based. So `slice(0, 2)` would return the
549    * elements at key 0 and 1.
550    *
551    * The returned `ImmVector` will always be a subset (but not necessarily a
552    * proper subset) of the current `ImmVector`. If `$start` is greater than or
553    * equal to the length of the current `Vector`, the returned `Vector` will
554    * contain no elements.  If `$start` + `$len` is greater than or equal to the
555    * length of the current `Vector`, the returned `Vector` will contain the
556    * elements from `$start` to the end of the current `Vector`.
557    *
558    * If either `$start` or `$len` is negative, an exception is thrown.
559    *
560    * @param $start - The starting key of the current `ImmVector` at which to
561    *                 begin the returned `ImmVector`.
562    * @param $len - The length of the returned `ImmVector`.
563    *
564    * @return - An `ImmVector` that is a subset of the current `ImmVector`
565    *           starting at `$start` up to but not including the element
566    *           `$start + $len`.
567    */
568   <<__Rx, __MutableReturn, __MaybeMutable>>
569   public function slice(int $start, int $len): ImmVector<Tv>;
571   /**
572    * Returns an `ImmVector` that is the concatenation of the values of the
573    * current `ImmVector` and the values of the provided `Traversable`.
574    *
575    * The returned `ImmVector` is created from the values of the current
576    * `ImmVector`, followed by the values of the provided `Traversable`.
577    *
578    * The returned `ImmVector` is a new object; the current `ImmVector` is
579    * unchanged.
580    *
581    * @param $traversable - The `Traversable` to concatenate to the current
582    *                       `ImmVector`.
583    *
584    * @return - A new `ImmVector` containing the values from `$traversable`
585    *           concatenated to the values from the current `ImmVector`.
586    *
587    * @guide /hack/generics/constraints
588    */
589   <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
590   public function concat<Tu super Tv>(
591     <<__MaybeMutable, __OnlyRxIfImpl(HH\Rx\Traversable::class)>> Traversable<Tu> $traversable
592   ): ImmVector<Tv>;
594   /**
595    * Returns the first value in the current `ImmVector`.
596    *
597    * @return - The first value in the current `ImmVector`, or `null` if the
598    *           current `ImmVector` is empty.
599    */
600   <<__Rx, __MaybeMutable>>
601   public function firstValue(): ?Tv;
603   /**
604    * Returns the first key in the current `ImmVector`.
605    *
606    * @return - The first key (an integer) in the current `ImmVector`, or `null`
607    *           if the current `ImmVector` is empty.
608    */
609   <<__Rx, __MaybeMutable>>
610   public function firstKey(): ?int;
612   /**
613    * Returns the last value in the current `ImmVector`.
614    *
615    * @return - The last value in the current `ImmVector`, or `null` if the
616    *           current `ImmVector` is empty.
617    */
618   <<__Rx, __MaybeMutable>>
619   public function lastValue(): ?Tv;
621   /**
622    * Returns the last key in the current `ImmVector`.
623    *
624    * @return - The last key (an integer) in the current `ImmVector`, or `null`
625    *           if the current `ImmVector` is empty.
626    */
627   <<__Rx, __MaybeMutable>>
628   public function lastKey(): ?int;
630   <<__Rx, __MaybeMutable>> /* HH_FIXME[0002] */
631   public function toVArray(): varray<Tv>;
632   <<__Rx, __MaybeMutable>> /* HH_FIXME[0001] */
633   public function toDArray(): darray<int, Tv>;