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 * `ImmMap` is an immutable `Map`. HHVM provides a native implementation for
19 * this class. The PHP class definition below is not actually used at run time;
20 * it is simply provided for the typechecker and for developer reference.
22 * A `ImmMap` cannot be mutated. No elements can be added or removed from it,
23 * nor can elements be overwritten using assignment (i.e. `$c[$k] = $v` is
26 * Construct it with a `Traversable`:
29 * $a = array('a' => 1, 'b' => 2);
30 * $fm = new ImmMap($a);
33 * or use the literal syntax
36 * $fm = ImmMap {'a' => 1, 'b' => 2};
39 * @guide /hack/collections/introduction
40 * @guide /hack/collections/classes
43 final class ImmMap<Tk, +Tv> implements ConstMap<Tk, Tv>, Indexish<Tk, Tv> {
45 * Creates an `ImmMap` from the given `KeyedTraversable`, or an empty
46 * `ImmMap` if `null` is passed.
48 * @param $it - any `Traversable` object from which to create an `ImmMap`
49 * (e.g., `array`). If `null`, then an empty `ImmMap` is created.
51 <<__Rx, __OnlyRxIfArgs>>
52 public function __construct(<<__MaybeMutable, __OnlyRxIfImpl(HH\Rx\KeyedTraversable::class)>> ?KeyedTraversable<Tk, Tv> $it);
55 * Returns an `array` containing the key/value pairs from the current
58 * @return - an `array` containing the key and value pairs from the current
61 <<__Rx, __MaybeMutable, __PHPStdLib>>
62 public function toArray(): array<Tk, Tv>;
65 * Returns an `array` containing the values from the current `ImmMap`.
67 * @return - an integer-indexed `array` containing the values from the
70 <<__Rx, __MaybeMutable>>
71 public function toValuesArray(): varray<Tv>;
74 * Returns an `array` whose values are the keys of the current `ImmMap`.
76 * @return - an integer-indexed `array` where the values are the keys from
77 * the current `ImmMap`.
79 <<__Rx, __MaybeMutable>>
80 public function toKeysArray(): varray<Tk>;
83 * Returns a `Vector` with the values of the current `ImmMap`.
85 * @return - a `Vector` that contains the values of the current `ImmMap`.
87 <<__Rx, __MutableReturn, __MaybeMutable>>
88 /* HH_FIXME[4120]: While this violates our variance annotations, we are
89 * returning a copy of the underlying collection, so it is actually safe
91 public function toVector(): Vector<Tv>;
94 * Returns an immutable vector (`ImmVector`) with the values of the current
97 * @return - an `ImmVector` that contains the values of the current `ImmMap`.
99 <<__Rx, __MaybeMutable>>
100 public function toImmVector(): ImmVector<Tv>;
103 * Returns a mutable copy (`Map`) of this `ImmMap`.
105 * @return - a mutable `Map` that is a copy of the current `ImmMap`.
107 <<__Rx, __MutableReturn, __MaybeMutable>>
108 /* HH_FIXME[4120]: While this violates our variance annotations, we are
109 * returning a copy of the underlying collection, so it is actually safe
111 public function toMap(): Map<Tk, Tv>;
114 * Returns an immutable copy (`ImmMap`) of the current `ImmMap`.
116 * @return - an `ImmMap` that is a copy of the current `ImmMap`.
118 <<__Rx, __MaybeMutable>>
119 public function toImmMap(): ImmMap<Tk, Tv>;
122 * Returns a `Set` based on the values of the current `ImmMap`.
124 * @return - a `Set` with the current values of the current `ImmMap`.
126 <<__Rx, __MutableReturn, __MaybeMutable>>
127 /* HH_FIXME[4120]: While this violates our variance annotations, we are
128 * returning a copy of the underlying collection, so it is actually safe
130 public function toSet(): Set<Tv>;
133 * Returns an immutable set (`ImmSet`) based on the values of the current
136 * @return - an `ImmSet` with the current values of the current `ImmMap`.
138 <<__Rx, __MaybeMutable>>
139 public function toImmSet(): ImmSet<Tv>;
142 * Returns an immutable copy (`ImmMap`) of the current `ImmMap`.
144 * This method is interchangeable with `toImmMap()`.
146 * @return - an `ImmMap` representing a copy of the current `ImmMap`.
148 <<__Rx, __MaybeMutable>>
149 public function immutable(): ImmMap<Tk, Tv>;
152 * Returns a lazy, access elements only when needed view of the current
155 * Normally, memory is allocated for all of the elements of an `ImmMap`. With
156 * a lazy view, memory is allocated for an element only when needed or used
157 * in a calculation like in `map()` or `filter()`.
159 * @return - a `KeyedIterable` representing the lazy view into the current
162 * @guide /hack/collections/examples
164 <<__Rx, __MutableReturn, __MaybeMutable>>
165 public function lazy(): HH\Rx\KeyedIterable<Tk, Tv>;
168 * Returns an ImmVector containing the values of the current `ImmMap`.
170 * This method is interchangeable with toImmVector().
172 * @return - an ImmVector containing the values of the current `ImmMap`.
174 <<__Rx, __MutableReturn, __MaybeMutable>>
175 public function values(): ImmVector<Tv>;
178 * Returns an ImmVector containing, as values, the keys of the current `ImmMap`.
180 * @return - an ImmVector containing, as values, the keys of the current
183 <<__Rx, __MutableReturn, __MaybeMutable>>
184 public function keys(): ImmVector<Tk>;
187 * Returns an `ImmMap` after an operation has been applied to each value in
188 * the current `ImmMap`.
190 * Every value in the current `ImmMap` is affected by a call to `map()`,
191 * unlike `filter()` where only values that meet a certain criteria are
194 * The keys will remain unchanged from this `ImmMap` to the returned `ImmMap`.
196 * @param $callback - The callback containing the operation to apply to the
197 * current `ImmMap` values.
199 * @return - an `ImmMap` containing key/value pairs after a user-specified
200 * operation is applied.
202 * @guide /hack/collections/examples
204 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
205 public function map<Tu>(<<__OnlyRxIfRxFunc>>(function(Tv): Tu) $callback): ImmMap<Tk, Tu>;
208 * Returns an `ImmMap` after an operation has been applied to each key and
209 * value in current `ImmMap`.
211 * Every key and value in the current `ImmMap` is affected by a call to
212 * `mapWithKey()`, unlike `filterWithKey()` where only values that meet a
213 * certain criteria are affected.
215 * The keys will remain unchanged from the current `ImmMap` to the returned
216 * `ImmMap`. The keys are only used to help in the operation.
218 * @param $callback - The callback containing the operation to apply to the
219 * current `ImmMap` keys and values.
221 * @return - an `ImmMap` containing the values after a user-specified
222 * operation on the current `ImmMap`'s keys and values is applied.
224 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
225 public function mapWithKey<Tu>(<<__OnlyRxIfRxFunc>>(function(Tk, Tv): Tu) $callback):
229 * Returns an `ImmMap` containing the values of the current `ImmMap` that
230 * meet a supplied condition.
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 * The keys associated with the current `ImmMap` remain unchanged in the
238 * @param $callback - The callback containing the condition to apply to the
239 * current `ImmMap` values.
241 * @return - an `ImmMap` containing the values after a user-specified
242 * condition is applied.
244 * @guide /hack/collections/examples
246 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
247 public function filter(<<__OnlyRxIfRxFunc>>(function(Tv): bool) $callback): ImmMap<Tk, Tv>;
250 * Returns an `ImmMap` containing the values of the current `ImmMap` that
251 * meet a supplied condition applied to its keys and values.
253 * Only keys and values that meet a certain criteria are affected by a call to
254 * `filterWithKey()`, while all values are affected by a call to
257 * The keys associated with the current `ImmMap` remain unchanged in the
258 * returned `ImmMap`; the keys will be used in the filtering process only.
260 * @param $callback - The callback containing the condition to apply to the
261 * current `ImmMap` keys and values.
263 * @return - an `ImmMap` containing the values after a user-specified
264 * condition is applied to the keys and values of the current
268 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
269 public function filterWithKey(<<__OnlyRxIfRxFunc>>(function(Tk, Tv): bool) $callback):
273 * Returns an `ImmMap` where each value is a `Pair` that combines the value
274 * of the current `ImmMap` and the provided `Traversable`.
276 * If the number of values of the current `ImmMap` are not equal to the
277 * number of elements in the `Traversable`, then only the combined elements
278 * up to and including the final element of the one with the least number of
279 * elements is included.
281 * The keys associated with the current `ImmMap` remain unchanged in the
284 * @param $traversable - The `Traversable` to use to combine with the
285 * elements of the current `ImmMap`.
287 * @return - The `ImmMap` that combines the values of the current `ImmMap`
288 * with the provided `Traversable`.
290 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
291 public function zip<Tu>(<<__MaybeMutable, __OnlyRxIfImpl(HH\Rx\Traversable::class)>> Traversable<Tu> $traversable):
292 ImmMap<Tk, Pair<Tv, Tu>>;
295 * Returns an `ImmMap` containing the first `n` key/values of the current
298 * The returned `ImmMap` will always be a proper subset of the current
301 * `n` is 1-based. So the first element is 1, the second 2, etc.
303 * @param $n - The last element that will be included in the returned
306 * @return - An `ImmMap` that is a proper subset of the current `ImmMap` up
309 <<__Rx, __MutableReturn, __MaybeMutable>>
310 public function take(int $n): ImmMap<Tk, Tv>;
313 * Returns an `ImmMap` containing the keys and values of the current `ImmMap`
314 * up to but not including the first value that produces `false` when passed
315 * to the specified callback.
317 * The returned `ImmMap` will always be a proper subset of the current
320 * @param $fn - The callback that is used to determine the stopping condition.
322 * @return - An `ImmMap` that is a proper subset of the current `ImmMap` up
323 * until when the callback returns `false`.
325 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
326 public function takeWhile(<<__OnlyRxIfRxFunc>>(function(Tv): bool) $fn): ImmMap<Tk, Tv>;
329 * Returns an `ImmMap` containing the values after the `n`-th element of the
332 * The returned `ImmMap` will always be a proper subset of the current
335 * `n` is 1-based. So the first element is 1, the second 2, etc.
337 * @param $n - The last element to be skipped; the `$n+1` element will be the
338 * first one in the returned `ImmMap`.
340 * @return - An `ImmMap` that is a proper subset of the current `ImmMap`
341 * containing values after the specified `n`-th element.
343 <<__Rx, __MutableReturn, __MaybeMutable>>
344 public function skip(int $n): ImmMap<Tk, Tv>;
347 * Returns an `ImmMap` containing the values of the current `ImmMap` starting
348 * after and including the first value that produces `true` when passed to
349 * the specified callback.
351 * The returned `ImmMap` will always be a proper subset of the current
354 * @param $fn - The callback used to determine the starting element for the
357 * @return - An `ImmMap` that is a proper subset of the current `ImmMap`
358 * starting after the callback returns `true`.
360 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
361 public function skipWhile(<<__OnlyRxIfRxFunc>>(function(Tv): bool) $fn): ImmMap<Tk, Tv>;
364 * Returns a subset of the current `ImmMap` starting from a given key
365 * location up to, but not including, the element at the provided length from
366 * the starting key location.
368 * `$start` is 0-based. `$len` is 1-based. So `slice(0, 2)` would return the
369 * keys and values at key location 0 and 1.
371 * The returned `ImmMap` will always be a proper subset of the current
374 * @param $start - The starting key location of the current `ImmMap` for the
376 * @param $len - The length of the returned `ImmMap`.
378 * @return - An `ImmMap` that is a proper subset of the current `ImmMap`
379 * starting at `$start` up to but not including the element
382 <<__Rx, __MutableReturn, __MaybeMutable>>
383 public function slice(int $start, int $len): ImmMap<Tk, Tv>;
386 * Returns an ImmVector that is the concatenation of the values of the
387 * current `ImmMap` and the values of the provided `Traversable`.
389 * The provided `Traversable` is concatenated to the end of the current
390 * `ImmMap` to produce the returned `ImmVector`.
392 * @param $traversable - The `Traversable` to concatenate to this `ImmMap`.
394 * @return - The integer-indexed concatenated `ImmVector`.
396 * @guide /hack/generics/constraints
398 <<__Rx, __OnlyRxIfArgs, __MutableReturn, __MaybeMutable>>
399 public function concat<Tu super Tv>(<<__MaybeMutable, __OnlyRxIfImpl(HH\Rx\Traversable::class)>> Traversable<Tu> $traversable):
403 * Returns the first value in the current `ImmMap`.
405 * @return - The first value in the current `ImmMap`, or `null` if the current
408 <<__Rx, __MaybeMutable>>
409 public function firstValue(): ?Tv;
412 * Returns the first key in the current `ImmMap`.
414 * @return - The first key in the current `ImmMap`, or `null` if the current
417 <<__Rx, __MaybeMutable>>
418 public function firstKey(): ?Tk;
421 * Returns the last value in the current `ImmMap`.
423 * @return - The last value in the current `ImmMap`, or `null` if the current
426 <<__Rx, __MaybeMutable>>
427 public function lastValue(): ?Tv;
430 * Returns the last key in the current `ImmMap`.
432 * @return - The last key in the current `ImmMap`, or `null` if the current
435 <<__Rx, __MaybeMutable>>
436 public function lastKey(): ?Tk;
439 * Checks if the current `ImmMap` is empty.
441 * @return - `true` if the current `ImmMap` is empty; `false` otherwise.
443 <<__Rx, __MaybeMutable>>
444 public function isEmpty(): bool;
447 * Provides the number of elements in the current `ImmMap`.
449 * @return - The number of elements in current `ImmMap`.
451 <<__Rx, __MaybeMutable>>
452 public function count(): int;
455 * Returns the value at the specified key in the current `ImmMap`.
457 * If the key is not present, an exception is thrown. If you don't want an
458 * exception to be thrown, use `get()` instead.
460 * `$v = $map->at($k)` is semantically equivalent to `$v = $map[$k]`.
462 * @param $k - the key from which to retrieve the value.
464 * @return - The value at the specified key; or an exception if the key does
467 <<__Rx, __MaybeMutable>>
468 public function at(Tk $k): Tv;
471 * Returns the value at the specified key in the current `ImmMap`.
473 * If the key is not present, null is returned. If you would rather have an
474 * exception thrown when a key is not present, then use `at()`.
476 * @param $k - the key from which to retrieve the value.
478 * @return - The value at the specified key; or `null` if the key does not
481 <<__Rx, __MaybeMutable>>
482 public function get(Tk $k): ?Tv;
485 * Determines if the specified key is in the current `ImmMap`.
487 * This function is interchangeable with `containsKey()`.
489 * @param $k - The key to check.
491 * @return - `true` if the specified key is present in the current `ImmMap`;
494 * @guide /hack/generics/constraints
496 <<__Rx, __MaybeMutable>>
497 public function contains<Tu super Tk>(Tu $k): bool;
500 * Determines if the specified key is in the current `ImmMap`.
502 * This function is interchangeable with `contains()`.
504 * @param $k - The key to check.
506 * @return - `true` if the specified key is present in the current `ImmMap`;
509 * @guide /hack/generics/constraints
511 <<__Rx, __MaybeMutable>>
512 public function containsKey<Tu super Tk>(Tu $k): bool;
515 * Returns a new `ImmMap` with the keys that are in the current `ImmMap`, but
516 * not in the provided `KeyedTraversable`.
518 * @param $traversable - The `KeyedTraversable` on which to compare the keys.
520 * @return - An `ImmMap` containing the keys (and associated values) of the
521 * current `ImmMap` that are not in the `KeyedTraversable`.
523 <<__Rx, __OnlyRxIfArgs, __MaybeMutable>>
524 public function differenceByKey<Tu super Tk, Tw>(
525 <<__MaybeMutable, __OnlyRxIfImpl(HH\Rx\KeyedTraversable::class)>> KeyedTraversable<Tu, Tw> $traversable
529 * Returns an iterator that points to beginning of the current `ImmMap`.
531 * @return - A `KeyedIterator` that allows you to traverse the current
534 <<__Rx, __MutableReturn, __MaybeMutable>>
535 public function getIterator(): HH\Rx\KeyedIterator<Tk, Tv>;
538 * Creates an `ImmMap` from the given `Traversable`, or an empty `ImmMap`
539 * if `null` is passed.
541 * This is the static method version of the `ImmMap::__construct()`
544 * @param $items - any Traversable object from which to create an `ImmMap`
545 * (e.g., `array`). If `null`, then an empty `ImmMap` is
548 * @return - An `ImmMap` with the key/value pairs from the `Traversable`; or
549 * an empty `ImmMap` if the `Traversable` is `null`.
551 <<__Rx, __OnlyRxIfArgs, __MaybeMutable>>
552 public static function fromItems(<<__MaybeMutable, __OnlyRxIfImpl(HH\Rx\Traversable::class)>> ?Traversable<Pair<Tk, Tv>> $items):
556 * Returns the `string` version of the current `ImmMap`, which is `"ImmMap"`.
558 * @return - The `string` `"ImmMap"`.
560 <<__Rx, __MaybeMutable>>
561 public function __toString(): string;
564 * Returns an `Iterable` view of the current `ImmMap`.
566 * The `Iterable` returned is one that produces the key/values from the
569 * @return - The `Iterable` view of the current `ImmMap`.
571 <<__Rx, __MutableReturn, __MaybeMutable>>
572 public function items(): HH\Rx\Iterable<Pair<Tk, Tv>>;
574 <<__Rx, __MaybeMutable>> /* HH_FIXME[0002] */
575 public function toVArray(): varray<Tv>;
576 <<__Rx, __MaybeMutable>> /* HH_FIXME[0001] */
577 public function toDArray(): darray<Tk, Tv>;