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 * `Set` is an ordered set-style collection. HHVM provides a native
19 * 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 * Like all objects in PHP, `Set`s have reference-like semantics. When a caller
24 * passes a `Set` to a callee, the callee can modify the `Set` and the caller
25 * will see the changes. `Set`s do not have "copy-on-write" semantics.
27 * `Set`s preserve insertion order of the elements. When iterating over a
28 * `Set`, the elements appear in the order they were inserted. Also, `Set`s do
29 * not automagically convert integer-like strings (ex. "123") into integers.
31 * `Set`s only support `int` values and `string` values. If a value of a
32 * different type is used, an exception will be thrown.
34 * In general, Sets do not support `$c[$k]` style syntax. Adding an element
35 * using `$c[] = ..` syntax is supported.
37 * `Set` do not support iteration while elements are being added or removed.
38 * When an element is added or removed, all iterators that point to the `Set`
39 * shall be considered invalid.
41 * `Set`s do not support taking elements by reference. If binding assignment
42 * (`=&`) is used when adding a new element to a `Set` (ex. `$c[] =& ...`), or
43 * if a `Set` is used with `foreach` by reference, an exception will be thrown.
45 * @guide /hack/collections/introduction
46 * @guide /hack/collections/classes
49 final class Set<Tv> implements MutableSet<Tv> {
51 * Creates a `Set` from the given `Traversable`, or an empty `Set` if `null`
54 * @param $it - any `Traversable` object from which to create the `Set`
55 * (e.g., `array`). If `null`, then an empty `Set` is created.
57 <<__Rx, __OnlyRxIfArgs>>
58 public function __construct(<<__MaybeMutable, __OnlyRxIfImpl(HH\Rx\Traversable::class)>> ?Traversable<Tv> $it);
61 * Returns an `array` containing the values from the current `Set`.
63 * For the returned `array`, each key is the same as its associated value.
65 * @return - an `array` containing the values from the current `Set`, where
66 * each key of the `array` are the same as each value.
68 <<__Rx, __MaybeMutable>>
69 /* HH_IGNORE_ERROR[2082] T30260145 */
70 public function toArray(): array<Tv, Tv>;
73 * Returns an `array` containing the values from the current `Set`.
75 * `Set`s don't have keys. So this method just returns the values.
77 * This method is interchangeable with `toValuesArray()`.
79 * @return - an integer-indexed `array` containing the values from the
82 <<__Rx, __MaybeMutable>>
83 public function toKeysArray(): varray<Tv>;
86 * Returns an `array` containing the values from the current `Set`.
88 * This method is interchangeable with `toKeysArray()`.
90 * @return - an integer-indexed `array` containing the values from the
93 <<__Rx, __MaybeMutable>>
94 public function toValuesArray(): varray<Tv>;
97 * Returns a `Vector` of the current `Set` values.
99 * @return - a `Vector` (integer-indexed) that contains the values of the
102 <<__Rx, __MutableReturn, __MaybeMutable>>
103 public function toVector(): Vector<Tv>;
106 * Returns an immutable vector (`ImmVector`) with the values of the current
109 * @return - an `ImmVector` (integer-indexed) with the values of the current
112 <<__Rx, __MaybeMutable>>
113 public function toImmVector(): ImmVector<Tv>;
116 * Returns a `Map` based on the values of the current `Set`.
118 * Each key of the `Map` will be the same as its value.
120 * @return - a `Map` that that contains the values of the current `Set`, with
121 * each key of the `Map` being the same as its value.
123 <<__Rx, __MutableReturn, __MaybeMutable>>
124 public function toMap(): Map<arraykey, Tv>;
127 * Returns an immutable map (`ImmMap`) based on the values of the current
130 * Each key of the `Map` will be the same as its value.
132 * @return - an `ImmMap` that that contains the values of the current `Set`,
133 * with each key of the Map being the same as its value.
135 <<__Rx, __MaybeMutable>>
136 public function toImmMap(): ImmMap<arraykey, Tv>;
139 * Returns a deep copy of the current `Set`.
141 * @return - a `Set` that is a deep copy of the current `Set`.
143 <<__Rx, __MutableReturn, __MaybeMutable>>
144 public function toSet(): Set<Tv>;
147 * Returns an immutable (`ImmSet`), deep copy of the current `Set`.
149 * This method is interchangeable with `immutable()`.
151 * @return - an `ImmSet` that is a deep copy of the current `Set`.
153 <<__Rx, __MaybeMutable>>
154 public function toImmSet(): ImmSet<Tv>;
157 * Returns an immutable (`ImmSet`), deep copy of the current `Set`.
159 * This method is interchangeable with `toImmSet()`.
161 * @return - an `ImmSet` that is a deep copy of the current `Set`.
163 <<__Rx, __MaybeMutable>>
164 public function immutable(): ImmSet<Tv>;
167 * Returns a lazy, access elements only when needed view of the current
170 * Normally, memory is allocated for all of the elements of the `Set`. With
171 * a lazy view, memory is allocated for an element only when needed or used
172 * in a calculation like in `map()` or `filter()`.
174 * @return - an `KeyedIterable` representing the lazy view into the current
175 * `Set`, where the keys are the same as the values.
177 * @guide /hack/collections/examples
179 <<__Rx, __MutableReturn, __MaybeMutable>>
180 public function lazy(): HH\Rx\KeyedIterable<arraykey, Tv>;
183 * Returns a `Vector` containing the values of the current `Set`.
185 * This method is interchangeable with `toVector()` and `keys()`.
187 * @return - a `Vector` (integer-indexed) containing the values of the
190 <<__Rx, __MutableReturn, __MaybeMutable>>
191 public function values(): Vector<Tv>;
194 * Returns a `Vector` containing the values of the current `Set`.
196 * `Set`s don't have keys, so this will return the values.
198 * This method is interchangeable with `toVector()` and `values()`.
200 * @return - a `Vector` (integer-indexed) containing the values of the
203 <<__Rx, __MutableReturn, __MaybeMutable>>
204 public function keys(): Vector<arraykey>;
207 * Returns a `Set` containing the values after an operation has been applied
208 * to each value in the current `Set`.
210 * Every value in the current `Set` is affected by a call to `map()`, unlike
211 * `filter()` where only values that meet a certain criteria are affected.
213 * @param $callback - The callback containing the operation to apply to the
214 * current `Set` values.
216 * @return - a `Set` containing the values after a user-specified operation
219 * @guide /hack/collections/examples
221 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
222 public function map<Tu>(<<__OnlyRxIfRxFunc>>(function(Tv): Tu) $callback): Set<Tu>;
225 * Returns a `Set` containing the values after an operation has been applied
226 * to each "key" and value in the current `Set`.
228 * Since `Set`s don't have keys, the callback uses the values as the keys
231 * Every value in the current `Set` is affected by a call to `mapWithKey()`,
232 * unlike `filterWithKey()` where only values that meet a certain criteria are
235 * @param $callback - The callback containing the operation to apply to the
236 * current `Set` keys and values.
238 * @return - a `Set` containing the values after a user-specified operation
239 * on the current `Set`'s values is applied.
241 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
242 public function mapWithKey<Tu>(<<__OnlyRxIfRxFunc>>(function(arraykey, Tv): Tu) $callback): Set<Tu>;
245 * Returns a `Set` containing the values of the current `Set` that meet
246 * a supplied condition applied to each value.
248 * Only values that meet a certain criteria are affected by a call to
249 * `filter()`, while all values are affected by a call to `map()`.
251 * @param $callback - The callback containing the condition to apply to the
252 * current `Set` values.
254 * @return - a `Set` containing the values after a user-specified condition
257 * @guide /hack/collections/examples
259 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
260 public function filter(<<__OnlyRxIfRxFunc>>(function(Tv): bool) $callback): Set<Tv>;
263 * Returns a `Set` containing the values of the current `Set` that meet
264 * a supplied condition applied to its "keys" and values.
266 * Since `Set`s don't have keys, the callback uses the values as the keys
269 * Only values that meet a certain criteria are affected by a call to
270 * `filterWithKey()`, while all values are affected by a call to
273 * @param $callback - The callback containing the condition to apply to the
274 * current `Set` keys and values.
276 * @return - a `Set` containing the values after a user-specified condition
277 * is applied to the values of the current `Set`.
280 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
281 public function filterWithKey(<<__OnlyRxIfRxFunc>>(function(arraykey, Tv): bool) $callback): Set<Tv>;
284 * Alters the current `Set` so that it only contains the values that meet a
285 * supplied condition on each value.
287 * This method is like `filter()`, but mutates the current `Set` too in
288 * addition to returning the current `Set`.
290 * Future changes made to the current `Set` ARE reflected in the returned
291 * `Set`, and vice-versa.
293 * @param $callback - The callback containing the condition to apply to the
294 * current `Set` values.
296 * @return - Returns itself.
298 <<__Rx, __Mutable, __OnlyRxIfArgs, __ReturnsVoidToRx>>
299 public function retain(<<__OnlyRxIfRxFunc>>(function(Tv): bool) $callback): Set<Tv>;
302 * Alters the current `Set` so that it only contains the values that meet a
303 * supplied condition on its "keys" and values.
305 * `Set`s don't have keys, so the `Set` values are used as the key in the
308 * This method is like `filterWithKey()`, but mutates the current `Set` too
309 * in addition to returning the current `Set`.
311 * Future changes made to the current `Set` ARE reflected in the returned
312 * `Set`, and vice-versa.
314 * @param $callback - The callback containing the condition to apply to the
315 * current `Set` values.
317 * @return - Returns itself.
319 <<__Rx, __Mutable, __OnlyRxIfArgs, __ReturnsVoidToRx>>
320 public function retainWithKey(<<__OnlyRxIfRxFunc>>(function(arraykey, Tv): bool) $callback): Set<Tv>;
323 * Throws an exception unless the current `Set` or the `Traversable` is
326 * Since `Set`s only support integers or strings as values, we cannot have
327 * a `Pair` as a `Set` value. So in order to avoid an
328 * `InvalidArgumentException`, either the current `Set` or the `Traversable`
329 * must be empty so that we actually return an empty `Set`.
331 * @param $traversable - The `Traversable` to use to combine with the
332 * elements of the current `Set`.
334 * @return - The `Set` that combines the values of the current `Set` with
335 * the provided `Traversable`; one of these must be empty or an
336 * exception is thrown.
338 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
339 public function zip<Tu>(<<__MaybeMutable, __OnlyRxIfImpl(HH\Rx\Traversable::class)>> Traversable<Tu> $traversable): Set<Pair<Tv, Tu>>;
342 * Returns a `Set` containing the first `n` values of the current `Set`.
344 * The returned `Set` will always be a proper subset of the current `Set`.
346 * `n` is 1-based. So the first element is 1, the second 2, etc.
348 * @param $n - The last element that will be included in the `Set`.
350 * @return - A `Set` that is a proper subset of the current `Set` up to `n`
353 <<__Rx, __MutableReturn, __MaybeMutable>>
354 public function take(int $n): Set<Tv>;
357 * Returns a `Set` containing the values of the current `Set` up to but not
358 * including the first value that produces `false` when passed to the
359 * specified callback.
361 * The returned `Set` will always be a proper subset of the current `Set`.
363 * @param $fn - The callback that is used to determine the stopping condition.
365 * @return - A `Set` that is a proper subset of the current `Set` up until
366 * the callback returns `false`.
368 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
369 public function takeWhile(<<__OnlyRxIfRxFunc>>(function(Tv): bool) $fn): Set<Tv>;
372 * Returns a `Set` containing the values after the `n`-th element of the
375 * The returned `Set` will always be a proper subset of the current `Set`.
377 * `n` is 1-based. So the first element is 1, the second 2, etc.
379 * @param $n - The last element to be skipped; the `$n+1` element will be
380 * the first one in the returned `Set`.
382 * @return - A `Set` that is a proper subset of the current `Set` containing
383 * values after the specified `n`-th element.
385 <<__Rx, __MutableReturn, __MaybeMutable>>
386 public function skip(int $n): Set<Tv>;
389 * Returns a `Set` containing the values of the current `Set` starting after
390 * and including the first value that produces `true` when passed to the
391 * specified callback.
393 * The returned `Set` will always be a proper subset of the current `Set`.
395 * @param $fn - The callback used to determine the starting element for the
398 * @return - A `Set` that is a proper subset of the current `Set` starting
399 * after the callback returns `true`.
401 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
402 public function skipWhile(<<__OnlyRxIfRxFunc>>(function(Tv): bool) $fn): Set<Tv>;
405 * Returns a subset of the current `Set` starting from a given key up to, but
406 * not including, the element at the provided length from the starting key.
408 * `$start` is 0-based. `$len` is 1-based. So `slice(0, 2)` would return the
409 * elements at key 0 and 1.
411 * The returned `Set` will always be a proper subset of the current `Set`.
413 * @param $start - The starting value in the current `Set` for the returned
415 * @param $len - The length of the returned `Set`.
417 * @return - A `Set` that is a proper subset of the current `Set` starting at
418 * `$start` up to but not including the element `$start + $len`.
420 <<__Rx, __MutableReturn, __MaybeMutable>>
421 public function slice(int $start, int $len): Set<Tv>;
424 * Returns a `Vector` that is the concatenation of the values of the current
425 * `Set` and the values of the provided `Traversable`.
427 * The values of the provided `Traversable` is concatenated to the end of the
428 * current `Set` to produce the returned `Vector`.
430 * @param $traversable - The `Traversable` to concatenate to the current
433 * @return - The concatenated `Vector`.
435 * @guide /hack/generics/constraints
437 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
438 public function concat<Tu super Tv>(<<__MaybeMutable, __OnlyRxIfImpl(HH\Rx\Traversable::class)>> Traversable<Tu> $traversable): Vector<Tu>;
441 * Returns the first value in the current `Set`.
443 * @return - The first value in the current `Set`, or `null` if the `Set` is
446 <<__Rx, __MaybeMutable>>
447 public function firstValue(): ?Tv;
450 * Returns the first "key" in the current `Set`.
452 * Since `Set`s do not have keys, it returns the first value.
454 * This method is interchangeable with `firstValue()`.
456 * @return - The first value in the current `Set`, or `null` if the `Set` is
459 <<__Rx, __MaybeMutable>>
460 public function firstKey(): ?arraykey;
463 * Returns the last value in the current `Set`.
465 * @return - The last value in the current `Set`, or `null` if the current
468 <<__Rx, __MaybeMutable>>
469 public function lastValue(): ?Tv;
472 * Returns the last "key" in the current `Set`.
474 * Since `Set`s do not have keys, it returns the last value.
476 * This method is interchangeable with `lastValue()`.
478 * @return - The last value in the current `Set`, or `null` if the current
481 <<__Rx, __MaybeMutable>>
482 public function lastKey(): ?arraykey;
485 * Checks if the current `Set` is empty.
487 * @return - `true` if the current `Set` is empty; `false` otherwise.
489 <<__Rx, __MaybeMutable>>
490 public function isEmpty(): bool;
493 * Provides the number of elements in the current `Set`.
495 * @return - The number of elements in the current `Set`.
497 <<__Rx, __MaybeMutable>>
498 public function count(): int;
501 * Remove all the elements from the current `Set`.
503 * Future changes made to the current `Set` ARE reflected in the returned
504 * `Set`, and vice-versa.
506 * @return - Returns itself.
508 <<__Rx, __Mutable, __ReturnsVoidToRx>>
509 public function clear(): Set<Tv>;
512 * Determines if the specified value is in the current `Set`.
514 * @param $v - The value to check.
515 * @return - `true` if the specified value is present in the current `Set`;
518 <<__Rx, __MaybeMutable>>
519 public function contains<Tu super Tv>(Tu $v): bool;
522 * Add the value to the current `Set`.
524 * `$set->add($v)` is semantically equivalent to `$set[] = $v` (except that
525 * `add()` returns the `Set`).
527 * Future changes made to the current `Set` ARE reflected in the returned
528 * `Set`, and vice-versa.
530 * @param $v - The value to add to the current `Set`
532 * @return - Returns itself.
534 <<__Rx, __Mutable, __ReturnsVoidToRx>>
535 public function add(Tv $v): Set<Tv>;
538 * For every element in the provided `Traversable`, add the value into the
541 * Future changes made to the original `Set` ARE reflected in the returned
542 * `Set`, and vice-versa.
544 * @param $k - The `Traversable` with the new values to add. If `null` is
545 * provided, no changes are made.
547 * @return - Returns itself.
549 <<__Rx, __Mutable, __OnlyRxIfArgs, __ReturnsVoidToRx>>
550 public function addAll(<<__MaybeMutable, __OnlyRxIfImpl(HH\Rx\Traversable::class)>> ?Traversable<Tv> $it): Set<Tv>;
553 * Adds the keys of the specified container to the current `Set` as new
556 * Future changes made to the current `Set` ARE reflected in the returned
557 * `Set`, and vice-versa.
559 * @param $container - The container with the new keys to add.
561 * @return - Returns itself.
563 <<__Rx, __Mutable, __ReturnsVoidToRx>>
564 public function addAllKeysOf<Tv2>(
565 ?KeyedContainer<Tv,Tv2> $container,
569 * Reserves enough memory to accommodate a given number of elements.
571 * Reserves enough memory for `sz` elements. If `sz` is less than or equal
572 * to the current capacity of this `Set`, this method does nothing.
574 * @param $sz - The pre-determined size you want for the current `Set`.
577 public function reserve(int $sz): void;
580 * Removes the specified value from the current `Set`.
582 * Future changes made to the current `Set` ARE reflected in the returned
583 * `Set`, and vice-versa.
585 * @param $v - The value to remove.
587 * @return - Returns itself.
589 <<__Rx, __Mutable, __ReturnsVoidToRx>>
590 public function remove(Tv $v): Set<Tv>;
593 * Removes the values in the current `Set` that are also in the `Traversable`.
595 * If a value in the `Traversable` doesn't exist in the current `Set`, that
596 * value in the `Traversable` is ignored.
598 * Future changes made to the current `Set` ARE reflected in the returned
599 * `Set`, and vice-versa.
601 * @param $other - The `Traversable` containing values that will be removed
604 * @return - Returns itself.
606 <<__Rx, __Mutable, __OnlyRxIfArgs, __ReturnsVoidToRx>>
607 public function removeAll(<<__MaybeMutable, __OnlyRxIfImpl(HH\Rx\Traversable::class)>> Traversable<Tv> $other): Set<Tv>;
610 * Returns an iterator that points to beginning of the current `Set`.
612 * @return - A `KeyedIterator` that allows you to traverse the current `Set`.
614 <<__Rx, __MutableReturn, __MaybeMutable>>
615 public function getIterator(): HH\Rx\KeyedIterator<arraykey, Tv>;
618 * Returns a `Set` containing the values from the specified `array`.
620 * This function is deprecated. Use `new Set ($arr)` instead.
622 * @param $arr - The `array` to convert to a `Set`.
624 * @return - A `Set` with the values from the provided `array`.
626 <<__Deprecated('Use `new Set($arr)` instead.')>>
627 public static function fromArray<T>(darray<T, Tv> $arr): Set<Tv>;
630 * Returns a `Set` containing all the values from the specified `array`(s).
632 * @param ... - The `array`s to convert to a `Set`.
634 * @return - A `Set` with the values from the passed `array`(s).
636 <<__Rx, __MutableReturn, __MaybeMutable>>
637 public static function fromArrays(...): Set<Tv>;
640 * Creates a `Set` from the given `Traversable`, or an empty `Set` if `null`
643 * This is the static method version of the `Set::__construct()` constructor.
645 * @param $items - any `Traversable` object from which to create a `Set`
646 * (e.g., `array`). If `null`, then an empty `Set` is created.
648 * @return - A `Set` with the values from the `Traversable`; or an empty `Set`
649 * if the `Traversable` is `null`.
651 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
652 public static function fromItems<Tv2>(<<__MaybeMutable, __OnlyRxIfImpl(HH\Rx\Traversable::class)>> ?Traversable<Tv2> $items): Set<Tv2>;
655 * Creates a `Set` from the keys of the specified container.
657 * The keys of the container will be the values of the `Set`.
659 * @param $container - The container with the keys used to create the `Set`.
661 * @return - A `Set` built from the keys of the specified container.
663 <<__Rx, __MutableReturn, __MaybeMutable>>
664 public static function fromKeysOf<Tk, Tv2>(
665 ?KeyedContainer<Tk,Tv2> $container,
669 * Returns the `string` version of the current `Set`, which is `"Set"`.
671 * @return - The `string` `"Set"`.
673 <<__Rx, __MaybeMutable>>
674 public function __toString(): string;
677 * Returns an `Iterable` view of the current `Set`.
679 * The `Iterable` returned is one that produces the values from the current
682 * @return - The `Iterable` view of the current `Set`.
684 <<__Rx, __MutableReturn, __MaybeMutable>>
685 public function items(): HH\Rx\Iterable<Tv>;
687 <<__Rx, __MaybeMutable>> /* HH_FIXME[0002] */
688 public function toVArray(): varray<Tv>;
689 <<__Rx, __MaybeMutable>> /* HH_FIXME[0001] */
690 public function toDArray(): darray<Tv, Tv>;
696 * Methods and functions should take and return the KeyedIterator interface.
698 class SetIterator<+Tv> implements HH\Rx\KeyedIterator<arraykey, Tv> {
700 public function __construct();
701 <<__Rx, __MaybeMutable>>
702 public function current(): Tv;
703 <<__Rx, __MaybeMutable>>
704 public function key(): arraykey;
705 <<__Rx, __MaybeMutable>>
706 public function valid(): bool;
708 public function next(): void;
710 public function rewind(): void;