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 * Vector is a stack-like collection. HHVM provides a native implementation
20 * for this class. The PHP class definition below is not actually used at run
21 * time; it is simply provided for the typechecker and for developer reference.
23 * Like all objects in PHP, Vectors have reference-like semantics. When a
24 * caller passes a Vector to a callee, the callee can modify the Vector and the
25 * caller will see the changes. Vectors do not have "copy-on-write" semantics.
27 * Vectors only support integer keys. If a non-integer key is used, an
28 * exception will be thrown.
30 * Vectors suoport "$m[$k]" style syntax for getting and setting values by
31 * key. Vectors also support "isset($m[$k])" and "empty($m[$k])" syntax, and
32 * they provide similar semantics as arrays. Elements can be added to a Vector
33 * using "$m[] = .." syntax.
35 * Vectors do not support iterating while new elements are being added or
36 * elements are being removed. When a new element is added or removed, all
37 * iterators that point to the Vector shall be considered invalid.
39 * Vectors do not support taking elements by reference. If binding assignment
40 * (=&) is used with an element of a Vector, or if an element of a Vector is
41 * passed by reference, of if a Vector is used with foreach by reference, an
42 * exception will be thrown.
45 final class Vector<Tv> implements MutableVector<Tv> {
47 * Creates a Vector from the given Traversable, or an empty Vector
48 * if "null" is passed.
50 public function __construct(?Traversable<Tv> $it);
53 * Returns an array containing the values from this Vector.
55 public function toArray(): array<Tv>;
58 * Returns an array containing the values from this Vector.
60 public function toValuesArray(): array;
63 * Returns an array whose values are the keys from this Vector.
65 public function toKeysArray(): array;
67 public function toVector(): Vector<Tv>;
68 public function toImmVector(): ImmVector<Tv>;
69 public function toMap(): Map<int, Tv>;
70 public function toImmMap(): ImmMap<int, Tv>;
71 public function toSet(): Set<Tv>;
72 public function toImmSet(): ImmSet<Tv>;
73 public function immutable(): ImmVector<Tv>;
74 public function lazy(): KeyedIterable<int, Tv>;
75 public function values(): Vector<Tv>;
76 public function keys(): Vector<int>;
77 public function map<Tu>((function(Tv): Tu) $callback): Vector<Tu>;
78 public function mapWithKey<Tu>((function(int, Tv): Tu) $callback): Vector<Tu>;
79 public function filter((function(Tv): bool) $callback): Vector<Tv>;
80 public function filterWithKey((function(int, Tv): bool) $callback):
82 public function zip<Tu>(Traversable<Tu> $traversable): Vector<Pair<Tv, Tu>>;
85 * Returns a Vector containing the first n values of this Vector.
87 public function take(int $n): Vector<Tv>;
90 * Returns a Vector containing the values of this Vector up to but not
91 * including the first value that produces false when passed to the specified
94 public function takeWhile((function(Tv): bool) $fn): Vector<Tv>;
96 public function skip(int $n): Vector<Tv>;
97 public function skipWhile((function(Tv): bool) $fn): Vector<Tv>;
98 public function slice(int $start, int $len): Vector<Tv>;
99 public function concat<Tu super Tv>(Traversable<Tu> $traversable): Vector<Tu>;
100 public function firstValue(): ?Tv;
101 public function firstKey(): ?int;
102 public function lastValue(): ?Tv;
103 public function lastKey(): ?int;
106 * Returns true if the Vector is empty, false otherwise.
108 public function isEmpty(): bool;
111 * Returns the number of elements in this Vector.
113 public function count(): int;
116 * Returns the value at the specified key. If the key is not present,
117 * an exception is thrown. "$v = $vec->at($k)" is semantically equivalent
118 * to "$v = $vec[$k]".
120 public function at(int $k): Tv;
123 * Returns the value at the specified key. If the key is not present,
126 public function get(int $k): ?Tv;
129 * Stores a value into the Vector with the specified key, overwriting the
130 * previous value associated with the key. If the key is not present,
131 * an exception is thrown. "$vec->set($k,$v)" is semantically equivalent
132 * to "$vec[$k] = $v" (except that set() returns the Vector).
134 public function set(int $k, Tv $v): Vector<Tv>;
136 public function setAll(?KeyedTraversable<int, Tv> $it): Vector<Tv>;
139 * Remove all the elements from this Vector.
141 public function clear(): Vector<Tv>;
144 * Returns true if the specified key is present in the Vector, returns
147 public function containsKey<Tu super int>(Tu $k): bool;
150 * Append a copy of a value to the end of the Vector, assigning the next
151 * available integer key. "$vec->add($v)" is semantically equivalent to
152 * "$vec[] = $v" (except that add() returns the Vector).
154 public function add(Tv $value): Vector<Tv>;
156 public function addAll(?Traversable<Tv> $it): Vector<Tv>;
159 * Adds the keys of the specified container to this Vector and returns itself.
161 public function addAllKeysOf<Tv2>(
162 ?KeyedContainer<Tv,Tv2> $container,
166 * Removes the specified key from this Vector. This will cause elements
167 * with higher keys to be renumbered.
169 public function removeKey(int $k): Vector<Tv>;
172 * Remove the last element of this Vector and return it. This function
173 * throws an exception if this Vector is empty.
175 public function pop(): Tv;
178 * Resize this Vector to contain 'sz' elements. If 'sz' is smaller than
179 * the current size of this Vector, elements are removed from the end of
180 * this Vector. If 'sz' is greater than the current size of this Vector,
181 * this Vector is extended by appending as many copies of 'value' as
182 * needed to reach a size of 'sz' elements.
184 public function resize(int $sz, Tv $value): void;
187 * Reserves enough memory to accommodate 'sz' elements. If 'sz' is less than or
188 * equal to the current capacity of this Vector, does nothing.
190 public function reserve(int $sz): void;
193 * Returns an iterator that points to beginning of this Vector.
195 public function getIterator(): KeyedIterator<int, Tv>;
198 * Reverse the elements of this Vector in place.
200 public function reverse(): void;
203 * Splice this Vector in place. This function provides the functionality
204 * of array_splice() for Vectors. Note that this function modifies this
207 public function splice(int $offset, ?int $len = null): void;
210 * Returns the index of the first element that matches the search value.
211 * If no element matches the search value, this function returns -1.
213 public function linearSearch<Tu super Tv>(Tu $search_value): int;
216 * Shuffles the values of the Vector randomly in place.
218 public function shuffle(): void;
221 * Returns a Vector containing the values from the specified array.
223 <<__Deprecated('Use `new Vector()` instead.')>>
224 public static function fromArray<T>(array<T, Tv> $arr): Vector<Tv>;
226 public static function fromItems(?Traversable<Tv> $items): Vector<Tv>;
229 * Returns a Vector built from the keys of the specified container.
231 public static function fromKeysOf<Tk,Tv2>(
232 ?KeyedContainer<Tk,Tv2> $container
235 public function __toString(): string;
237 public function items(): Iterable<Tv>;
240 class VectorIterator<+Tv> implements KeyedIterator<int, Tv> {
241 public function __construct();
242 public function rewind(): void;
243 public function current(): Tv;
244 public function key(): int;
245 public function next(): void;
246 public function valid(): bool;