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 * `ImmSet` is an immutable, ordered set-style collection. HHVM provides a
19 * native implementation for this class. The PHP class definition below is not
20 * actually used at run time; it is simply provided for the typechecker and
21 * for developer reference.
23 * An `ImmSet` cannot be mutated. No elements can be added or removed from it,
24 * nor can elements be overwritten using assignment (i.e. `$s[$k] = $v` is
27 * Construct it with a `Traversable`:
31 * $s = new ImmSet($a);
34 * or use the literal syntax:
40 * @guide /hack/collections/introduction
41 * @guide /hack/collections/classes
44 final class ImmSet<+Tv> implements ConstSet<Tv> {
46 * Creates an `ImmSet` from the given `Traversable`, or an empty `ImmSet` if
49 * @param $it - any `Traversable` object from which to create the `ImmSet`
50 * (e.g., `array`). If `null`, then an empty `ImmSet` is created.
52 <<__Rx, __OnlyRxIfArgs>>
53 public function __construct(<<__MaybeMutable, __OnlyRxIfImpl(HH\Rx\Traversable::class)>> ?Traversable<Tv> $it);
56 * Checks if the current `ImmSet` is empty.
58 * @return - `true` if the current `ImmSet` is empty; `false` otherwise.
60 <<__Rx, __MaybeMutable>>
61 public function isEmpty(): bool;
64 * Provides the number of elements in the current `ImmSet`.
66 * @return - The number of elements in the current `ImmSet`.
68 <<__Rx, __MaybeMutable>>
69 public function count(): int;
72 * Determines if the specified value is in the current `ImmSet`.
74 * @param $v - The value to check.
75 * @return - `true` if the specified value is present in the current `ImmSet`;
78 <<__Rx, __MaybeMutable>>
79 public function contains<Tu super Tv>(Tu $k): bool;
82 * Returns an `array` containing the values from the current `ImmSet`.
84 * For the returned `array`, each key is the same as its associated value.
86 * @return - an `array` containing the values from the current `ImmSet`,
87 * where each key of the `array` are the same as each value.
89 <<__Rx, __MaybeMutable>>
90 /* HH_IGNORE_ERROR[2082] T30260145 */
91 public function toArray(): array<Tv, Tv>;
94 * Returns an `array` containing the values from the current `ImmSet`.
96 * `Set`s don't have keys. So this method just returns the values.
98 * This method is interchangeable with `toValuesArray()`.
100 * @return - an integer-indexed `array` containing the values from the
103 <<__Rx, __MaybeMutable>>
104 public function toKeysArray(): varray<Tv>;
107 * Returns an `array` containing the values from the current `ImmSet`.
109 * This method is interchangeable with `toKeysArray()`.
111 * @return - an integer-indexed `array` containing the values from the
114 <<__Rx, __MaybeMutable>>
115 public function toValuesArray(): varray<Tv>;
118 * Returns an iterator that points to beginning of the current `ImmSet`.
120 * The keys and values when iterating through the `KeyedIterator` will be
121 * identical since `ImmSet`s have no keys, the values are used as keys.
123 * @return - A `KeyedIterator` that allows you to traverse the current
126 <<__Rx, __MutableReturn, __MaybeMutable>>
127 public function getIterator(): HH\Rx\KeyedIterator<arraykey, Tv>;
130 * Creates an `ImmSet` from the given `Traversable`, or an empty `ImmSet` if
133 * This is the static method version of the `ImmSet::__construct()`
136 * @param $items - any `Traversable` object from which to create an `ImmSet`
137 * (e.g., `array`). If `null`, then an empty `ImmSet` is
140 * @return - An `ImmSet` with the values from the `Traversable`; or an empty
141 * `ImmSet` if the `Traversable` is `null`.
143 <<__Rx, __OnlyRxIfArgs, __MaybeMutable>>
144 public static function fromItems(<<__MaybeMutable, __OnlyRxIfImpl(HH\Rx\Traversable::class)>> ?Traversable<Tv> $items): ImmSet<Tv>;
147 * Returns an `ImmSet` containing all the values from the specified
150 * @param ... - The `array`(s) to convert to an `ImmSet`.
152 * @return - An `ImmSet` with the values from the passed `array`(s).
154 <<__Rx, __MaybeMutable>>
155 public static function fromArrays(...): ImmSet<Tv>;
158 * Creates an `ImmSet` from the keys of the specified container.
160 * The keys of the container will be the values of the `ImmSet`.
162 * @param $container - The container with the keys used to create the
165 * @return - An `ImmSet` built from the keys of the specified container.
167 <<__Rx, __MaybeMutable>>
168 public static function fromKeysOf<Tk, Tv2>(
169 ?KeyedContainer<Tk,Tv2> $container
173 * Returns the `string` version of this `ImmSet`, which is `"ImmSet"`.
175 * @return - The `string` `"ImmSet"`.
177 <<__Rx, __MaybeMutable>>
178 public function __toString(): string;
181 * Returns a `Vector` of the current `ImmSet` values.
183 * @return - a `Vector` (integer-indexed) that contains the values of the
186 <<__Rx, __MutableReturn, __MaybeMutable>>
187 /* HH_FIXME[4120]: While this violates our variance annotations, we are
188 * returning a copy of the underlying collection, so it is actually safe
190 public function toVector(): Vector<Tv>;
193 * Returns an immutable vector (`ImmVector`) with the values of the current
196 * @return - an `ImmVector` (integer-indexed) with the values of the current
199 <<__Rx, __MaybeMutable>>
200 public function toImmVector(): ImmVector<Tv>;
203 * Returns a `Map` based on the values of the current `ImmSet`.
205 * Each key of the `Map` will be the same as its value.
207 * @return - a `Map` that that contains the values of the current `ImmSet`,
208 * with each key of the `Map` being the same as its value.
210 <<__Rx, __MutableReturn, __MaybeMutable>> /* HH_FIXME[4120]: While this violates our variance annotations, we are
211 * returning a copy of the underlying collection, so it is actually safe
213 public function toMap(): Map<arraykey, Tv>;
216 * Returns an immutable map (`ImmMap`) based on the values of the current
219 * Each key of the `Map` will be the same as its value.
221 * @return - an `ImmMap` that that contains the values of the current
222 * `ImmSet`, with each key of the `ImmMap` being the same as its
225 <<__Rx, __MaybeMutable>>
226 public function toImmMap(): ImmMap<arraykey, Tv>;
229 * Returns a mutable copy (`Set`) of the current `ImmSet`.
231 * @return - a `Set` that is a copy of the current `ImmSet`.
233 <<__Rx, __MutableReturn, __MaybeMutable>>
234 /* HH_FIXME[4120]: While this violates our variance annotations, we are
235 * returning a copy of the underlying collection, so it is actually safe
237 public function toSet(): Set<Tv>;
240 * Returns an immutable copy (`ImmSet`) of the current `ImmSet`.
242 * This function is interchangeable with `immutable()`.
244 * @return - an `ImmSet` that is a copy of the current `ImmSet`.
246 <<__Rx, __MaybeMutable>>
247 public function toImmSet(): ImmSet<Tv>;
250 * Returns an immutable copy (`ImmSet`) of the current `ImmSet`.
252 * This method is interchangeable with `toImmSet()`.
254 * @return - an `ImmSet` that is a copy of the current `ImmSet`.
256 <<__Rx, __MaybeMutable>>
257 public function immutable(): ImmSet<Tv>;
260 * Returns an Iterable view of the current `ImmSet`.
262 * The `Iterable` returned is one that produces the values from the current
265 * @return - The `Iterable` view of the current `ImmSet`.
267 <<__Rx, __MutableReturn, __MaybeMutable>>
268 public function items(): HH\Rx\Iterable<Tv>;
271 * Returns an `ImmVector` containing the values of the current `ImmSet`.
273 * This method is interchangeable with `toImmVector()` and `keys()`.
275 * @return - an `ImmVector` containing the values of the current `ImmSet`.
277 <<__Rx, __MutableReturn, __MaybeMutable>>
278 public function values(): ImmVector<Tv>;
281 * Returns an `ImmVector` containing the values of this `ImmSet`.
283 * `ImmSet`s don't have keys, so this will return the values.
285 * This method is interchangeable with `toImmVector()` and `values()`.
287 * @return - an `ImmVector` containing the values of the current `ImmSet`.
289 <<__Rx, __MutableReturn, __MaybeMutable>>
290 public function keys(): ImmVector<arraykey>;
293 * Returns a lazy, access elements only when needed view of the current
296 * Normally, memory is allocated for all of the elements of an `ImmSet`. With
297 * a lazy view, memory is allocated for an element only when needed or used
298 * in a calculation like in `map()` or `filter()`.
300 * @return - an `KeyedIterable` representing the lazy view into the current
301 * `ImmSet`, where the keys are the same as the values.
303 * @guide /hack/collections/examples
305 <<__Rx, __MutableReturn, __MaybeMutable>>
306 public function lazy(): HH\Rx\KeyedIterable<arraykey, Tv>;
309 * Returns an `ImmSet` containing the values after an operation has been
310 * applied to each value in the current `ImmSet`.
312 * Every value in the current `ImmSet` is affected by a call to `map()`,
313 * unlike `filter()` where only values that meet a certain criteria are
316 * @param $callback - The callback containing the operation to apply to the
317 * current `ImmSet` values.
319 * @return - a `ImmSet` containing the values after a user-specified operation
322 * @guide /hack/collections/examples
324 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
325 public function map<Tu>(<<__OnlyRxIfRxFunc>>(function(Tv): Tu) $callback): ImmSet<Tu>;
328 * Returns an `ImmSet` containing the values after an operation has been
329 * applied to each "key" and value in the current `ImmSet`.
331 * Since `ImmSet`s don't have keys, the callback uses the values as the keys
334 * Every value in the current `ImmSet` is affected by a call to
335 * `mapWithKey()`, unlike `filterWithKey()` where only values that meet a
336 * certain criteria are affected.
338 * @param $callback - The callback containing the operation to apply to the
339 * current `ImmSet` keys and values.
341 * @return - an `ImmSet` containing the values after a user-specified
342 * operation on the current `ImmSet`'s values is applied.
344 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
345 public function mapWithKey<Tu>(<<__OnlyRxIfRxFunc>>(function(arraykey, Tv): Tu) $callback):
349 * Returns an `ImmSet` containing the values of the current `ImmSet` that
350 * meet a supplied condition applied to each value.
352 * Only values that meet a certain criteria are affected by a call to
353 * `filter()`, while all values are affected by a call to `map()`.
355 * @param $callback - The callback containing the condition to apply to the
356 * current `ImmSet` values.
358 * @return - an `ImmSet` containing the values after a user-specified
359 * condition is applied.
361 * @guide /hack/collections/examples
363 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
364 public function filter(<<__OnlyRxIfRxFunc>>(function(Tv): bool) $callback): ImmSet<Tv>;
367 * Returns an `ImmSet` containing the values of the current `ImmSet` that
368 * meet a supplied condition applied to its "keys" and values.
370 * Since `ImmSet`s don't have keys, the callback uses the values as the keys
373 * Only values that meet a certain criteria are affected by a call to
374 * `filterWithKey()`, while all values are affected by a call to
377 * @param $callback - The callback containing the condition to apply to the
378 * current `ImmSet` keys and values.
380 * @return - an `ImmSet` containing the values after a user-specified
381 * condition is applied to the values of the current `ImmSet`.
384 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
385 public function filterWithKey(<<__OnlyRxIfRxFunc>>(function(arraykey, Tv): bool) $callback):
389 * Throws an exception unless the current `ImmSet` or the `Traversable` is
392 * Since `ImmSet`s only support integers or strings as values, we cannot
393 * have a `Pair` as an `ImmSet` value. So in order to avoid an
394 * `InvalidArgumentException`, either the current `ImmSet` or the
395 * `Traversable` must be empty so that we actually return an empty `ImmSet`.
397 * @param $traversable - The `Traversable` to use to combine with the
398 * elements of the current `ImmSet`.
400 * @return - The `ImmSet` that combines the values of the current `ImmSet`
401 * with the provided `Traversable`; one of these must be empty or
402 * an exception is thrown.
404 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
405 public function zip<Tu>(<<__MaybeMutable, __OnlyRxIfImpl(HH\Rx\Traversable::class)>> Traversable<Tu> $traversable): ImmSet<Pair<Tv, Tu>>;
408 * Returns an `ImmSet` containing the first n values of the current `ImmSet`.
410 * The returned `ImmSet` will always be a proper subset of the current
413 * `n` is 1-based. So the first element is 1, the second 2, etc.
415 * @param $n - The last element that will be included in the returned
418 * @return - An `ImmSet` that is a proper subset of the current `ImmSet` up
421 <<__Rx, __MutableReturn, __MaybeMutable>>
422 public function take(int $n): ImmSet<Tv>;
425 * Returns an `ImmSet` containing the values of the current `ImmSet` up to
426 * but not including the first value that produces `false` when passed to the
427 * specified callback.
429 * The returned `ImmSet` will always be a proper subset of the current
432 * @param $fn - The callback that is used to determine the stopping condition.
434 * @return - An `ImmSet` that is a proper subset of the current `ImmSet` up
435 * until the callback returns `false`.
437 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
438 public function takeWhile(<<__OnlyRxIfRxFunc>>(function(Tv): bool) $fn): ImmSet<Tv>;
441 * Returns an `ImmSet` containing the values after the `n`-th element of the
444 * The returned `ImmSet` will always be a proper subset of the current
447 * `n` is 1-based. So the first element is 1, the second 2, etc.
449 * @param $n - The last element to be skipped; the `$n+1` element will be the
450 * first one in the returned `ImmSet`.
452 * @return - An `ImmSet` that is a proper subset of the current `ImmSet`
453 * containing values after the specified `n`-th element.
455 <<__Rx, __MutableReturn, __MaybeMutable>>
456 public function skip(int $n): ImmSet<Tv>;
459 * Returns an `ImmSet` containing the values of the current `ImmSet` starting
460 * after and including the first value that produces `true` when passed to
461 * the specified callback.
463 * The returned `ImmSet` will always be a proper subset of the current
466 * @param $fn - The callback used to determine the starting element for the
469 * @return - An `ImmSet` that is a proper subset of the current `ImmSet`
470 * starting after the callback returns `true`.
472 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
473 public function skipWhile(<<__OnlyRxIfRxFunc>>(function(Tv): bool) $fn): ImmSet<Tv>;
476 * Returns a subset of the current `ImmSet` starting from a given key up to,
477 * but not including, the element at the provided length from the starting
480 * `$start` is 0-based. `$len` is 1-based. So `slice(0, 2)` would return the
481 * elements at key 0 and 1.
483 * The returned `ImmSet` will always be a proper subset of the current
486 * @param $start - The starting value in the current `ImmSet` for the
488 * @param $len - The length of the returned `ImmSet`.
490 * @return - An `ImmSet` that is a proper subset of the current `ImmSet`
491 * starting at `$start` up to but not including the element
494 <<__Rx, __MutableReturn, __MaybeMutable>>
495 public function slice(int $start, int $len): ImmSet<Tv>;
498 * Returns an `ImmVector` that is the concatenation of the values of the
499 * current `ImmSet` and the values of the provided `Traversable`.
501 * The values of the provided `Traversable` is concatenated to the end of the
502 * current `ImmSet` to produce the returned `ImmVector`.
504 * @param $traversable - The `Traversable` to concatenate to the current
507 * @return - The concatenated `ImmVector`.
509 * @guide /hack/generics/constraints
511 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
512 public function concat<Tu super Tv>(
513 <<__MaybeMutable, __OnlyRxIfImpl(HH\Rx\Traversable::class)>> Traversable<Tu> $traversable
517 * Returns the first value in the current `ImmSet`.
519 * This method is interchangeable with `firstKey()`.
521 * @return - The first value in the current `ImmSet`, or `null` if the
522 * current `ImmSet` is empty.
524 <<__Rx, __MaybeMutable>>
525 public function firstValue(): ?Tv;
528 * Returns the first "key" in the current `ImmSet`.
530 * Since `ImmSet`s do not have keys, it returns the first value.
532 * This method is interchangeable with `firstValue()`.
534 * @return - The first value in the current `ImmSet`, or `null` if the
535 * current `ImmSet` is empty.
537 <<__Rx, __MaybeMutable>>
538 public function firstKey(): ?arraykey;
541 * Returns the last value in the current `ImmSet`.
543 * This method is interchangeable with `lastKey()`.
545 * @return - The last value in the current `ImmSet`, or `null` if the current
548 <<__Rx, __MaybeMutable>>
549 public function lastValue(): ?Tv;
552 * Returns the last "key" in the current `ImmSet`.
554 * Since `ImmSet`s do not have keys, it returns the last value.
556 * This method is interchangeable with `lastValue()`.
558 * @return - The last value in the current `ImmSet`, or `null` if the current
561 <<__Rx, __MaybeMutable>>
562 public function lastKey(): ?arraykey;
564 <<__Rx, __MaybeMutable>> /* HH_FIXME[0002] */
565 public function toVArray(): varray<Tv>;
566 <<__Rx, __MaybeMutable>> /* HH_FIXME[0001] */
567 public function toDArray(): darray<Tv, Tv>;