3 * Copyright (c) 2014, Facebook, Inc.
6 * This source code is licensed under the BSD-style license found in the
7 * LICENSE file in the "hack" directory of this source tree. An additional grant
8 * of patent rights can be found in the PATENTS file in the same directory.
13 * This file provides type information for some of HHVM's builtin classes.
15 * YOU SHOULD NEVER INCLUDE THIS FILE ANYWHERE!!!
19 * `Set` is an ordered set-style collection. HHVM provides a native
20 * implementation for this class. The PHP class definition below is not
21 * actually used at run time; it is simply provided for the typechecker and
22 * for developer reference.
24 * Like all objects in PHP, `Set`s have reference-like semantics. When a caller
25 * passes a `Set` to a callee, the callee can modify the `Set` and the caller
26 * will see the changes. `Set`s do not have "copy-on-write" semantics.
28 * `Set`s preserve insertion order of the elements. When iterating over a
29 * `Set`, the elements appear in the order they were inserted. Also, `Set`s do
30 * not automagically convert integer-like strings (ex. "123") into integers.
32 * `Set`s only support `int` values and `string` values. If a value of a
33 * different type is used, an exception will be thrown.
35 * In general, Sets do not support `$c[$k]` style syntax. Adding an element
36 * using `$c[] = ..` syntax is supported.
38 * `Set` do not support iteration while elements are being added or removed.
39 * When an element is added or removed, all iterators that point to the `Set`
40 * shall be considered invalid.
42 * `Set`s do not support taking elements by reference. If binding assignment
43 * (`=&`) is used when adding a new element to a `Set` (ex. `$c[] =& ...`), or
44 * if a `Set` is used with `foreach` by reference, an exception will be thrown.
46 * @guide /hack/collections/introduction
47 * @guide /hack/collections/classes
50 final class Set<Tv> implements MutableSet<Tv> {
52 * Creates a `Set` from the given `Traversable`, or an empty `Set` if `null`
55 * @param $it - any `Traversable` object from which to create the `Set`
56 * (e.g., `array`). If `null`, then an empty `Set` is created.
58 public function __construct(?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 public function toArray(): array<Tv, Tv>;
71 * Returns an `array` containing the values from the current `Set`.
73 * `Set`s don't have keys. So this method just returns the values.
75 * This method is interchangeable with `toValuesArray()`.
77 * @return - an integer-indexed `array` containing the values from the
80 public function toKeysArray(): array<Tv>;
83 * Returns an `array` containing the values from the current `Set`.
85 * This method is interchangeable with `toKeysArray()`.
87 * @return - an integer-indexed `array` containing the values from the
90 public function toValuesArray(): array<Tv>;
93 * Returns a `Vector` of the current `Set` values.
95 * @return - a `Vector` (integer-indexed) that contains the values of the
98 public function toVector(): Vector<Tv>;
101 * Returns an immutable vector (`ImmVector`) with the values of the current
104 * @return - an `ImmVector` (integer-indexed) with the values of the current
107 public function toImmVector(): ImmVector<Tv>;
110 * Returns a `Map` based on the values of the current `Set`.
112 * Each key of the `Map` will be the same as its value.
114 * @return - a `Map` that that contains the values of the current `Set`, with
115 * each key of the `Map` being the same as its value.
117 public function toMap(): Map<mixed, Tv>;
120 * Returns an immutable map (`ImmMap`) based on the values of the current
123 * Each key of the `Map` will be the same as its value.
125 * @return - an `ImmMap` that that contains the values of the current `Set`,
126 * with each key of the Map being the same as its value.
128 public function toImmMap(): ImmMap<mixed, Tv>;
131 * Returns a deep copy of the current `Set`.
133 * @return - a `Set` that is a deep copy of the current `Set`.
135 public function toSet(): Set<Tv>;
138 * Returns an immutable (`ImmSet`), deep copy of the current `Set`.
140 * This method is interchangeable with `immutable()`.
142 * @return - an `ImmSet` that is a deep copy of the current `Set`.
144 public function toImmSet(): ImmSet<Tv>;
147 * Returns an immutable (`ImmSet`), deep copy of the current `Set`.
149 * This method is interchangeable with `toImmSet()`.
151 * @return - an `ImmSet` that is a deep copy of the current `Set`.
153 public function immutable(): ImmSet<Tv>;
156 * Returns a lazy, access elements only when needed view of the current
159 * Normally, memory is allocated for all of the elements of the `Set`. With
160 * a lazy view, memory is allocated for an element only when needed or used
161 * in a calculation like in `map()` or `filter()`.
163 * @return - an `KeyedIterable` representing the lazy view into the current
164 * `Set`, where the keys are the same as the values.
166 * @guide /hack/collections/examples
168 public function lazy(): KeyedIterable<mixed, Tv>;
171 * Returns a `Vector` containing the values of the current `Set`.
173 * This method is interchangeable with `toVector()` and `keys()`.
175 * @return - a `Vector` (integer-indexed) containing the values of the
178 public function values(): Vector<Tv>;
181 * Returns a `Vector` containing the values of the current `Set`.
183 * `Set`s don't have keys, so this will return the values.
185 * This method is interchangeable with `toVector()` and `values()`.
187 * @return - a `Vector` (integer-indexed) containing the values of the
190 public function keys(): Vector<mixed>;
193 * Returns a `Set` containing the values after an operation has been applied
194 * to each value in the current `Set`.
196 * Every value in the current `Set` is affected by a call to `map()`, unlike
197 * `filter()` where only values that meet a certain criteria are affected.
199 * @param $callback - The callback containing the operation to apply to the
200 * current `Set` values.
202 * @return - a `Set` containing the values after a user-specified operation
205 * @guide /hack/collections/examples
207 public function map<Tu>((function(Tv): Tu) $callback): Set<Tu>;
210 * Returns a `Set` containing the values after an operation has been applied
211 * to each "key" and value in the current `Set`.
213 * Since `Set`s don't have keys, the callback uses the values as the keys
216 * Every value in the current `Set` is affected by a call to `mapWithKey()`,
217 * unlike `filterWithKey()` where only values that meet a certain criteria are
220 * @param $callback - The callback containing the operation to apply to the
221 * current `Set` keys and values.
223 * @return - a `Set` containing the values after a user-specified operation
224 * on the current `Set`'s values is applied.
226 public function mapWithKey<Tu>((function(mixed, Tv): Tu) $callback): Set<Tu>;
229 * Returns a `Set` containing the values of the current `Set` that meet
230 * a supplied condition applied to each value.
232 * Only values that meet a certain criteria are affected by a call to
233 * `filter()`, while all values are affected by a call to `map()`.
235 * @param $callback - The callback containing the condition to apply to the
236 * current `Set` values.
238 * @return - a `Set` containing the values after a user-specified condition
241 * @guide /hack/collections/examples
243 public function filter((function(Tv): bool) $callback): Set<Tv>;
246 * Returns a `Set` containing the values of the current `Set` that meet
247 * a supplied condition applied to its "keys" and values.
249 * Since `Set`s don't have keys, the callback uses the values as the keys
252 * Only values that meet a certain criteria are affected by a call to
253 * `filterWithKey()`, while all values are affected by a call to
256 * @param $callback - The callback containing the condition to apply to the
257 * current `Set` keys and values.
259 * @return - a `Set` containing the values after a user-specified condition
260 * is applied to the values of the current `Set`.
263 public function filterWithKey((function(mixed, Tv): bool) $callback): Set<Tv>;
266 * Alters the current `Set` so that it only contains the values that meet a
267 * supplied condition on each value.
269 * This method is like `filter()`, but mutates the current `Set` too in
270 * addition to returning a shallow copy of the current `Set`.
272 * Future changes made to the current `Set` ARE reflected in the returned
273 * `Set`, and vice-versa.
275 * @param $callback - The callback containing the condition to apply to the
276 * current `Set` values.
278 * @return - a shallow copy of the current `Set` containing the values after
279 * a user-specified condition is applied.
281 public function retain((function(Tv): bool) $callback): Set<Tv>;
284 * Alters the current `Set` so that it only contains the values that meet a
285 * supplied condition on its "keys" and values.
287 * `Set`s don't have keys, so the `Set` values are used as the key in the
290 * This method is like `filterWithKey()`, but mutates the current `Set` too
291 * in addition to returning a shallow copy of the current `Set`.
293 * Future changes made to the current `Set` ARE reflected in the returned
294 * `Set`, and vice-versa.
296 * @param $callback - The callback containing the condition to apply to the
297 * current `Set` values.
299 * @return - a shallow copy of the current `Set` containing the values after
300 * a user-specified condition is applied.
302 public function retainWithKey((function(mixed, Tv): bool) $callback): Set<Tv>;
305 * Throws an exception unless the current `Set` or the `Traversable` is
308 * Since `Set`s only support integers or strings as values, we cannot have
309 * a `Pair` as a `Set` value. So in order to avoid an
310 * `InvalidArgumentException`, either the current `Set` or the `Traversable`
311 * must be empty so that we actually return an empty `Set`.
313 * @param $traversable - The `Traversable` to use to combine with the
314 * elements of the current `Set`.
316 * @return - The `Set` that combines the values of the current `Set` with
317 * the provided `Traversable`; one of these must be empty or an
318 * exception is thrown.
320 public function zip<Tu>(Traversable<Tu> $traversable): Set<Pair<Tv, Tu>>;
323 * Returns a `Set` containing the first `n` values of the current `Set`.
325 * The returned `Set` will always be a proper subset of the current `Set`.
327 * `n` is 1-based. So the first element is 1, the second 2, etc.
329 * @param $n - The last element that will be included in the `Set`.
331 * @return - A `Set` that is a proper subset of the current `Set` up to `n`
334 public function take(int $n): Set<Tv>;
337 * Returns a `Set` containing the values of the current `Set` up to but not
338 * including the first value that produces `false` when passed to the
339 * specified callback.
341 * The returned `Set` will always be a proper subset of the current `Set`.
343 * @param $fn - The callback that is used to determine the stopping condition.
345 * @return - A `Set` that is a proper subset of the current `Set` up until
346 * the callback returns `false`.
348 public function takeWhile((function(Tv): bool) $fn): Set<Tv>;
351 * Returns a `Set` containing the values after the `n`-th element of the
354 * The returned `Set` will always be a proper subset of the current `Set`.
356 * `n` is 1-based. So the first element is 1, the second 2, etc.
358 * @param $n - The last element to be skipped; the `$n+1` element will be
359 * the first one in the returned `Set`.
361 * @return - A `Set` that is a proper subset of the current `Set` containing
362 * values after the specified `n`-th element.
364 public function skip(int $n): Set<Tv>;
367 * Returns a `Set` containing the values of the current `Set` starting after
368 * and including the first value that produces `true` when passed to the
369 * specified callback.
371 * The returned `Set` will always be a proper subset of the current `Set`.
373 * @param $fn - The callback used to determine the starting element for the
376 * @return - A `Set` that is a proper subset of the current `Set` starting
377 * after the callback returns `true`.
379 public function skipWhile((function(Tv): bool) $fn): Set<Tv>;
382 * Returns a subset of the current `Set` starting from a given key up to, but
383 * not including, the element at the provided length from the starting key.
385 * `$start` is 0-based. `$len` is 1-based. So `slice(0, 2)` would return the
386 * elements at key 0 and 1.
388 * The returned `Set` will always be a proper subset of the current `Set`.
390 * @param $start - The starting value in the current `Set` for the returned
392 * @param $len - The length of the returned `Set`.
394 * @return - A `Set` that is a proper subset of the current `Set` starting at
395 * `$start` up to but not including the element `$start + $len`.
397 public function slice(int $start, int $len): Set<Tv>;
400 * Returns a `Vector` that is the concatenation of the values of the current
401 * `Set` and the values of the provided `Traversable`.
403 * The values of the provided `Traversable` is concatenated to the end of the
404 * current `Set` to produce the returned `Vector`.
406 * @param $traversable - The `Traversable` to concatenate to the current
409 * @return - The concatenated `Vector`.
411 * @guide /hack/generics/constraints
413 public function concat<Tu super Tv>(Traversable<Tu> $traversable): Vector<Tu>;
416 * Returns the first value in the current `Set`.
418 * @return - The first value in the current `Set`, or `null` if the `Set` is
421 public function firstValue(): ?Tv;
424 * Returns the first "key" in the current `Set`.
426 * Since `Set`s do not have keys, it returns the first value.
428 * This method is interchangeable with `firstValue()`.
430 * @return - The first value in the current `Set`, or `null` if the `Set` is
433 public function firstKey(): mixed;
436 * Returns the last value in the current `Set`.
438 * @return - The last value in the current `Set`, or `null` if the current
441 public function lastValue(): ?Tv;
444 * Returns the last "key" in the current `Set`.
446 * Since `Set`s do not have keys, it returns the last value.
448 * This method is interchangeable with `lastValue()`.
450 * @return - The last value in the current `Set`, or `null` if the current
453 public function lastKey(): mixed;
456 * Checks if the current `Set` is empty.
458 * @return - `true` if the current `Set` is empty; `false` otherwise.
460 public function isEmpty(): bool;
463 * Provides the number of elements in the current `Set`.
465 * @return - The number of elements in the current `Set`.
467 public function count(): int;
470 * Remove all the elements from the current `Set`.
472 * Future changes made to the current `Set` ARE reflected in the returned
473 * `Set`, and vice-versa.
475 * @return - A shallow, empty copy of the current `Set`. The current `Set` is
478 public function clear(): Set<Tv>;
481 * Determines if the specified value is in the current `Set`.
483 * @param $v - The value to check.
484 * @return - `true` if the specified value is present in the current `Set`;
487 public function contains<Tu super Tv>(Tu $v): bool;
490 * Add the value to the current `Set`.
492 * `$set->add($v)` is semantically equivalent to `$set[] = $v` (except that
493 * `add()` returns the `Set`).
495 * Future changes made to the current `Set` ARE reflected in the returned
496 * `Set`, and vice-versa.
498 * @param $v - The value to add to the current `Set`
500 * @return - A shallow copy of the current `Set` with the added the value
501 * set. The current `Set` is also updated.
503 public function add(Tv $v): Set<Tv>;
506 * For every element in the provided `Traversable`, add the value into the
509 * Future changes made to the original `Set` ARE reflected in the returned
510 * `Set`, and vice-versa.
512 * @param $k - The `Traversable` with the new values to add. If `null` is
513 * provided, no changes are made.
515 * @return - A shallow copy of the current `Set` with the added the values
516 * set. The current `Set` is also updated.
518 public function addAll(?Traversable<Tv> $it): Set<Tv>;
521 * Adds the keys of the specified container to the current `Set` as new
524 * Future changes made to the current `Set` ARE reflected in the returned
525 * `Set`, and vice-versa.
527 * @param $container - The container with the new keys to add.
529 * @return - A shallow copy of the current `Set` with the new keys added; the
530 * current `Set` is also updated.
532 public function addAllKeysOf<Tv2>(
533 ?KeyedContainer<Tv,Tv2> $container,
537 * Reserves enough memory to accommodate a given number of elements.
539 * Reserves enough memory for `sz` elements. If `sz` is less than or equal
540 * to the current capacity of this `Set`, this method does nothing.
542 * @param $sz - The pre-determined size you want for the current `Set`.
544 public function reserve(int $sz): void;
547 * Removes the specified value from the current `Set`.
549 * Future changes made to the current `Set` ARE reflected in the returned
550 * `Set`, and vice-versa.
552 * @param $v - The value to remove.
554 * @return - A shallow copy of the current `Set` with the value removed; the
555 * current `Set` is also updated.
557 public function remove(Tv $v): Set<Tv>;
560 * Removes the values in the current `Set` that are also in the `Traversable`.
562 * If a value in the `Traversable` doesn't exist in the current `Set`, that
563 * value in the `Traversable` is ignored.
565 * Future changes made to the current `Set` ARE reflected in the returned
566 * `Set`, and vice-versa.
568 * @param $other - The `Traversable` containing values that will be removed
571 * @return - A shallow copy of the current `Set` with the values removed; the
572 * current `Set` is also updated.
574 public function removeAll(Traversable<Tv> $other): Set<Tv>;
577 * Returns an iterator that points to beginning of the current `Set`.
579 * @return - A `KeyedIterator` that allows you to traverse the current `Set`.
581 public function getIterator(): KeyedIterator<mixed, Tv>;
584 * Returns a `Set` containing the values from the specified `array`.
586 * This function is deprecated. Use `new Set ($arr)` instead.
588 * @param $arr - The `array` to convert to a `Set`.
590 * @return - A `Set` with the values from the provided `array`.
592 <<__Deprecated('Use `new Set($arr)` instead.')>>
593 public static function fromArray<T>(array<T, Tv> $arr): Set<Tv>;
596 * Returns a `Set` containing all the values from the specified `array`(s).
598 * @param ... - The `array`s to convert to a `Set`.
600 * @return - A `Set` with the values from the passed `array`(s).
602 public static function fromArrays(...): Set<Tv>;
605 * Creates a `Set` from the given `Traversable`, or an empty `Set` if `null`
608 * This is the static method version of the `Set::__construct()` constructor.
610 * @param $items - any `Traversable` object from which to create a `Set`
611 * (e.g., `array`). If `null`, then an empty `Set` is created.
613 * @return - A `Set` with the values from the `Traversable`; or an empty `Set`
614 * if the `Traversable` is `null`.
616 public static function fromItems<Tv2>(?Traversable<Tv2> $items): Set<Tv2>;
619 * Creates a `Set` from the keys of the specified container.
621 * The keys of the container will be the values of the `Set`.
623 * @param $container - The container with the keys used to create the `Set`.
625 * @return - A `Set` built from the keys of the specified container.
627 public static function fromKeysOf<Tk, Tv2>(
628 ?KeyedContainer<Tk,Tv2> $container,
632 * Returns the `string` version of the current `Set`, which is `"Set"`.
634 * @return - The `string` `"Set"`.
636 public function __toString(): string;
639 * Returns an `Iterable` view of the current `Set`.
641 * The `Iterable` returned is one that produces the values from the current
644 * @return - The `Iterable` view of the current `Set`.
646 public function items(): Iterable<Tv>;
652 * Methods and functions should take and return the KeyedIterator interface.
654 class SetIterator<+Tv> implements KeyedIterator<mixed, Tv> {
655 public function __construct();
656 public function current(): Tv;
657 public function key(): mixed;
658 public function valid(): bool;
659 public function next(): void;
660 public function rewind(): void;