3 * Copyright (c) 2014, Facebook, Inc.
6 * This source code is licensed under the MIT license found in the
7 * LICENSE file in the "hack" directory of this source tree.
12 * This file provides type information for some of HHVM's builtin classes.
14 * YOU SHOULD NEVER INCLUDE THIS FILE ANYWHERE!!!
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.
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`
27 * $v = Vector {1, 2, 3};
28 * $fv = $v->toImmVector();
31 * construct it with a `Traversable`:
34 * $a = array(1, 2, 3);
35 * $fv = new ImmVector($a);
38 * or use the literal syntax:
41 * $fv = ImmVector {1, 2, 3};
44 * @guide /hack/collections/introduction
45 * @guide /hack/collections/classes
48 final class ImmVector<+Tv> implements ConstVector<Tv> {
50 * Creates an `ImmVector` from the given `Traversable`, or an empty
51 * `ImmVector` if `null` is passed.
53 * @param $it - Any `Traversable` object from which to create the `ImmVector`
54 * (e.g., `array`). If `null`, then an empty `ImmVector` is
57 <<__Rx, __OnlyRxIfArgs>>
58 public function __construct(<<__MaybeMutable, __OnlyRxIfImpl(HH\Rx\Traversable::class)>> ?Traversable<Tv> $it);
61 * Checks if the current `ImmVector` is empty.
63 * @return - `true` if the current `ImmVector` is empty; `false` otherwise.
65 <<__Rx, __MaybeMutable>>
66 public function isEmpty(): bool;
69 * Returns the number of elements in the current `ImmVector`.
71 * @return - The number of elements in the current `ImmVector`.
73 <<__Rx, __MaybeMutable>>
74 public function count(): int;
77 * Returns the value at the specified key in the current `ImmVector`.
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.
82 * `$v = $vec->at($k)` is semantically equivalent to `$v = $vec[$k]`.
84 * @param $k - The key for which to retrieve the value.
86 * @return - The value at the specified key; or an exception if the key does
89 <<__Rx, __MaybeMutable>>
90 public function at(int $k): Tv;
93 * Returns the value at the specified key in the current `ImmVector`.
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()`.
98 * @param $k - The key for which to retrieve the value.
100 * @return - The value at the specified key; or `null` if the key does not
103 <<__Rx, __MaybeMutable>>
104 public function get(int $k): ?Tv;
107 * Determines if the specified key is in the current `ImmVector`.
109 * @return - `true` if the specified key is present in the current
110 * `ImmVector`; `false` otherwise.
112 * @guide /hack/generics/constraints
114 <<__Rx, __MaybeMutable>>
115 public function containsKey<Tu super int>(Tu $k): bool;
118 * Returns an `array` containing the values from the current `ImmVector`.
120 * This method is interchangeable with `toValuesArray()`.
122 * @return - An `array` containing the values from the current `ImmVector`.
124 <<__Rx, __MaybeMutable>>
125 /* HH_IGNORE_ERROR[2082] T30260145 */
126 public function toArray(): array<Tv>;
129 * Returns an `array` containing the values from the current `ImmVector`.
131 * This method is interchangeable with `toArray()`.
133 * @return - An `array` containing the values from the current `ImmVector`.
135 <<__Rx, __MaybeMutable>>
136 public function toValuesArray(): varray<Tv>;
139 * Returns an `array` whose values are the keys from the current `ImmVector`.
141 * @return - An `array` with the integer keys from the current `ImmVector`.
143 <<__Rx, __MaybeMutable>>
144 public function toKeysArray(): varray<Tv>;
147 * Returns an iterator that points to beginning of the current `ImmVector`.
149 * @return - A `KeyedIterator` that allows you to traverse the current
152 <<__Rx, __MutableReturn, __MaybeMutable>>
153 public function getIterator(): HH\Rx\KeyedIterator<int, Tv>;
156 * Returns the index of the first element that matches the search value.
158 * If no element matches the search value, this function returns -1.
160 * @param $search_value - The value that will be searched for in the current
163 * @return - The key (index) where that value is found; -1 if it is not found.
165 * @guide /hack/generics/constraints
167 <<__Rx, __MaybeMutable>>
168 public function linearSearch<Tu super Tv>(Tu $search_value): int;
171 * Creates an `ImmVector` from the given `Traversable`, or an empty
172 * `ImmVector` if `null` is passed.
174 * This is the static method version of the `ImmVector::__construct()`
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.
181 * @return - An `ImmVector` with the values from the `Traversable`; or an
182 * empty `ImmVector` if the `Traversable` is `null`.
184 <<__Rx, __OnlyRxIfArgs, __MaybeMutable>>
185 public static function fromItems(<<__MaybeMutable, __OnlyRxIfImpl(HH\Rx\Traversable::class)>> ?Traversable<Tv> $items): ImmVector<Tv>;
188 * Creates an `ImmVector` from the keys of the specified container.
190 * Every key in the provided `KeyedContainer` will appear sequentially in the
191 * returned `ImmVector`, with the next available integer key assigned to each.
193 * @param $container - The container with the keys used to create the
194 * current `ImmVector`.
196 * @return - An `ImmVector` built from the keys of the specified container.
198 <<__Rx, __MaybeMutable>>
199 public static function fromKeysOf<Tk>(
200 ?KeyedContainer<Tk, mixed> $container,
204 * Returns the `string` version of the current `ImmVector`, which is
207 * @return - The `string` `"ImmVector"`.
209 <<__Rx, __MaybeMutable>>
210 public function __toString(): string;
213 * Returns an `Iterable` view of the current `ImmVector`.
215 * The `Iterable` returned is one that produces the values from the current
218 * @return - The `Iterable` view of the current `ImmVector`.
220 <<__Rx, __MutableReturn, __MaybeMutable>>
221 public function items(): HH\Rx\Iterable<Tv>;
224 * Returns the current `ImmVector`.
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`.
230 * This method is interchangeable with `immutable()`.
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
238 * @return - The current `ImmVector`.
240 <<__Rx, __MaybeMutable>>
241 public function toImmVector(): ImmVector<Tv>;
244 * Returns a `Vector` containing the elemnts of the current `ImmVector`.
246 * The returned `Vector` will, of course, be mutable.
248 * @return - A `Vector` with the elements of the current `ImmVector`.
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
254 public function toVector(): Vector<Tv>;
257 * Returns an integer-keyed `Map` based on the elements of the current
260 * The keys are `0... count() - 1`.
262 * @return - An integer-keyed `Map` with the values of the current
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
269 public function toMap(): Map<int, Tv>;
272 * Returns an immutable integer-keyed Map (`ImmMap`) based on the elements of
273 * the current `ImmVector`.
275 * The keys are `0... count() - 1`.
277 * @return - An integer-keyed `ImmMap` with the values of the current
280 <<__Rx, __MaybeMutable>>
281 public function toImmMap(): ImmMap<int, Tv>;
284 * Returns a `Set` with the values of the current `ImmVector`.
286 * @return - A `Set` with the values of the current `ImmVector`.
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
292 public function toSet(): Set<Tv>;
295 * Returns an immutable Set (`ImmSet`) with the values of the current
298 * @return - An `ImmSet` with the current values of the current `ImmVector`.
300 <<__Rx, __MaybeMutable>>
301 public function toImmSet(): ImmSet<Tv>;
304 * Returns the current `ImmVector`.
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`.
310 * This method is interchangeable with `toImmVector()`.
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
318 * @return - The current `ImmVector`.
320 <<__Rx, __MaybeMutable>>
321 public function immutable(): ImmVector<Tv>;
324 * Returns a lazy, access-elements-only-when-needed view of the current
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()`.
331 * @return - An integer-keyed `KeyedIterable` representing the lazy view into
332 * the current `ImmVector`.
334 * @guide /hack/collections/examples
336 <<__Rx, __MutableReturn, __MaybeMutable>>
337 public function lazy(): HH\Rx\KeyedIterable<int, Tv>;
340 * Returns a new `ImmVector` containing the values of the current `ImmVector`;
341 * that is, a copy of the current `ImmVector`.
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
349 * @return - A new `ImmVector` containing the values of the current
352 <<__Rx, __MutableReturn, __MaybeMutable>>
353 public function values(): ImmVector<Tv>;
356 * Returns an `ImmVector` containing the keys, as values, of the current
359 * @return - An `ImmVector` containing, as values, the integer keys of the
360 * current `ImmVector`.
362 <<__Rx, __MutableReturn, __MaybeMutable>>
363 public function keys(): ImmVector<int>;
366 * Returns an `ImmVector` containing the results of applying an operation to
367 * each value in the current `ImmVector`.
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`.
373 * @param $callback - The callback containing the operation to apply to the
374 * current `ImmVector`'s values.
376 * @return - An `ImmVector` containing the results of applying a user-specified
377 * operation to each value of the current `ImmVector` in turn.
379 * @guide /hack/collections/examples
381 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
382 public function map<Tu>(<<__OnlyRxIfRxFunc>>(function(Tv): Tu) $callback): ImmVector<Tu>;
385 * Returns an `ImmVector` containing the results of applying an operation to
386 * each key/value pair in the current `ImmVector`.
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
393 * @param $callback - The callback containing the operation to apply to the
394 * current `ImmVector`'s key/value pairs.
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.
400 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
401 public function mapWithKey<Tu>(<<__OnlyRxIfRxFunc>>(function(int, Tv): Tu) $callback):
405 * Returns a `ImmVector` containing the values of the current `ImmVector` that
406 * meet a supplied condition.
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
412 * @param $callback - The callback containing the condition to apply to the
413 * current `ImmVector` values.
415 * @return - An `ImmVector` containing the values after a user-specified
416 * condition is applied.
418 * @guide /hack/collections/examples
420 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
421 public function filter(<<__OnlyRxIfRxFunc>>(function(Tv): bool) $callback): ImmVector<Tv>;
424 * Returns an `ImmVector` containing the values of the current `ImmVector`
425 * that meet a supplied condition applied to its keys and values.
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`.
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
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.
442 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
443 public function filterWithKey(<<__OnlyRxIfRxFunc>>(function(int, Tv): bool) $callback):
447 * Returns an `ImmVector` where each element is a `Pair` that combines the
448 * element of the current `ImmVector` and the provided `Traversable`.
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.
455 * @param $traversable - The `Traversable` to use to combine with the
456 * elements of the current `ImmVector`.
458 * @return - An `ImmVector` that combines the values of the current
459 * `ImmVector` with the provided `Traversable`.
461 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
462 public function zip<Tu>(<<__MaybeMutable, __OnlyRxIfImpl(HH\Rx\Traversable::class)>> Traversable<Tu> $traversable):
463 ImmVector<Pair<Tv, Tu>>;
466 * Returns an `ImmVector` containing the first `$n` values of the current
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`.
474 * `$n` is 1-based. So the first element is 1, the second 2, etc.
476 * @param $n - The last element that will be included in the returned
479 * @return - An `ImmVector` that is a subset of the current `ImmVector` up to
482 <<__Rx, __MutableReturn, __MaybeMutable>>
483 public function take(int $n): ImmVector<Tv>;
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`.
491 * The returned `ImmVector` will always be a subset (but not necessarily a
492 * proper subset) of the current `ImmVector`.
494 * @param $fn - The callback that is used to determine the stopping condition.
496 * @return - An `ImmVector` that is a subset of the current `ImmVector` up
497 * until when the callback returns `false`.
499 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
500 public function takeWhile(<<__OnlyRxIfRxFunc>>(function(Tv): bool) $fn): ImmVector<Tv>;
503 * Returns an `ImmVector` containing the values after the `$n`-th element of
504 * the current `ImmVector`.
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`.
512 * `$n` is 1-based. So the first element is 1, the second 2, etc.
514 * @param $n - The last element to be skipped; the `$n+1` element will be the
515 * first one in the returned `ImmVector`.
517 * @return - An `ImmVector` that is a subset of the current `ImmVector`
518 * containing values after the specified `$n`-th element.
520 <<__Rx, __MutableReturn, __MaybeMutable>>
521 public function skip(int $n): ImmVector<Tv>;
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
530 * The returned `ImmVector` will always be a subset (but not necessarily a
531 * proper subset) of the current `ImmVector`.
533 * @param $fn - The callback used to determine the starting element for the
534 * returned `ImmVector`.
536 * @return - An `ImmVector` that is a subset of the current `ImmVector`
537 * starting with the value for which the callback first returns
540 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
541 public function skipWhile(<<__OnlyRxIfRxFunc>>(function(Tv): bool) $fn): ImmVector<Tv>;
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
548 * `$start` is 0-based. `$len` is 1-based. So `slice(0, 2)` would return the
549 * elements at key 0 and 1.
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`.
558 * If either `$start` or `$len` is negative, an exception is thrown.
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`.
564 * @return - An `ImmVector` that is a subset of the current `ImmVector`
565 * starting at `$start` up to but not including the element
568 <<__Rx, __MutableReturn, __MaybeMutable>>
569 public function slice(int $start, int $len): ImmVector<Tv>;
572 * Returns an `ImmVector` that is the concatenation of the values of the
573 * current `ImmVector` and the values of the provided `Traversable`.
575 * The returned `ImmVector` is created from the values of the current
576 * `ImmVector`, followed by the values of the provided `Traversable`.
578 * The returned `ImmVector` is a new object; the current `ImmVector` is
581 * @param $traversable - The `Traversable` to concatenate to the current
584 * @return - A new `ImmVector` containing the values from `$traversable`
585 * concatenated to the values from the current `ImmVector`.
587 * @guide /hack/generics/constraints
589 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
590 public function concat<Tu super Tv>(
591 <<__MaybeMutable, __OnlyRxIfImpl(HH\Rx\Traversable::class)>> Traversable<Tu> $traversable
595 * Returns the first value in the current `ImmVector`.
597 * @return - The first value in the current `ImmVector`, or `null` if the
598 * current `ImmVector` is empty.
600 <<__Rx, __MaybeMutable>>
601 public function firstValue(): ?Tv;
604 * Returns the first key in the current `ImmVector`.
606 * @return - The first key (an integer) in the current `ImmVector`, or `null`
607 * if the current `ImmVector` is empty.
609 <<__Rx, __MaybeMutable>>
610 public function firstKey(): ?int;
613 * Returns the last value in the current `ImmVector`.
615 * @return - The last value in the current `ImmVector`, or `null` if the
616 * current `ImmVector` is empty.
618 <<__Rx, __MaybeMutable>>
619 public function lastValue(): ?Tv;
622 * Returns the last key in the current `ImmVector`.
624 * @return - The last key (an integer) in the current `ImmVector`, or `null`
625 * if the current `ImmVector` is empty.
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>;