HH\Set
Set
is an ordered set-style collection
HHVM provides a native implementation for this class. The PHP class definition below is not actually used at run time; it is simply provided for the typechecker and for developer reference.
Like all objects in PHP, Set
s have reference-like semantics. When a caller
passes a Set
to a callee, the callee can modify the Set
and the caller
will see the changes. Set
s do not have "copy-on-write" semantics.
Set
s preserve insertion order of the elements. When iterating over a
Set
, the elements appear in the order they were inserted. Also, Set
s do
not automagically convert integer-like strings (ex. "123") into integers.
Set
s only support int
values and string
values. If a value of a
different type is used, an exception will be thrown.
In general, Sets do not support $c[$k]
style syntax. Adding an element
using $c[] = ..
syntax is supported.
Set
do not support iteration while elements are being added or removed.
When an element is added or removed, all iterators that point to the Set
shall be considered invalid.
Guides
Examples
More documentation and examples are in the
[Set
and ImmSet
](</hack/arrays-and-collections/collections#set-and-immset>)
guide.
Interface Synopsis
namespace HH;
final class Set implements \MutableSet<Tv> {...}
Public Methods
::fromArray(darray<arraykey, Tv> $arr): Set<Tv>
Returns aSet
containing the values from the specifiedarray
::fromArrays(...$argv): Set<Tv>
Returns aSet
containing all the values from the specifiedarray
(s)::fromItems(?Traversable<Tv> $iterable): Set<Tv>
Creates aSet
from the givenTraversable
, or an emptySet
ifnull
is passed::fromKeysOf<Tk as arraykey>(?KeyedContainer<Tk, mixed> $container): Set<Tk>
Creates aSet
from the keys of the specified container->__construct(?Traversable<Tv> $iterable = NULL): void
Creates aSet
from the givenTraversable
, or an emptySet
ifnull
is passed->__toString(): string
Returns thestring
version of the currentSet
, which is"Set"
->add(Tv $val): Set<Tv>
Add the value to the currentSet
->addAll(?Traversable<Tv> $iterable): Set<Tv>
For every element in the providedTraversable
, add the value into the currentSet
->addAllKeysOf(?KeyedContainer<Tv, mixed> $container): Set<Tv>
Adds the keys of the specified container to the currentSet
as new values->clear(): Set<Tv>
Remove all the elements from the currentSet
->concat<Tu super Tv>(Traversable<Tu> $traversable): Vector<Tu>
Returns aVector
that is the concatenation of the values of the currentSet
and the values of the providedTraversable
->contains(arraykey $val): bool
Determines if the specified value is in the currentSet
->count(): int
Provides the number of elements in the currentSet
->difference(mixed $iterable)
->filter((function(Tv): bool) $callback): Set<Tv>
Returns aSet
containing the values of the currentSet
that meet a supplied condition applied to each value->filterWithKey((function(arraykey, Tv): bool) $callback): Set<Tv>
Returns aSet
containing the values of the currentSet
that meet a supplied condition applied to its "keys" and values->firstKey(): ?arraykey
Returns the first "key" in the currentSet
->firstValue(): ?Tv
Returns the first value in the currentSet
->getIterator(): KeyedIterator<arraykey, Tv>
Returns an iterator that points to beginning of the currentSet
->immutable(): ImmSet<Tv>
Returns an immutable (ImmSet
), deep copy of the currentSet
->isEmpty(): bool
Checks if the currentSet
is empty->items(): Iterable<Tv>
Returns anIterable
view of the currentSet
->keys(): Vector<arraykey>
Returns aVector
containing the values of the currentSet
->lastKey(): ?arraykey
Returns the last "key" in the currentSet
->lastValue(): ?Tv
Returns the last value in the currentSet
->lazy(): KeyedIterable<arraykey, Tv>
Returns a lazy, access elements only when needed view of the currentSet
->map<Tu as arraykey>((function(Tv): Tu) $callback): Set<Tu>
Returns aSet
containing the values after an operation has been applied to each value in the currentSet
->mapWithKey<Tu as arraykey>((function(arraykey, Tv): Tu) $callback): Set<Tu>
Returns aSet
containing the values after an operation has been applied to each "key" and value in the currentSet
->remove(Tv $val): Set<Tv>
Removes the specified value from the currentSet
->removeAll(Traversable<Tv> $iterable): Set<Tv>
Removes the values in the currentSet
that are also in theTraversable
->reserve(int $sz): void
Reserves enough memory to accommodate a given number of elements->retain((function(Tv): bool) $callback): Set<Tv>
Alters the currentSet
so that it only contains the values that meet a supplied condition on each value->retainWithKey((function(arraykey, Tv): bool) $callback): Set<Tv>
Alters the currentSet
so that it only contains the values that meet a supplied condition on its "keys" and values->skip(int $n): Set<Tv>
Returns aSet
containing the values after then
-th element of the currentSet
->skipWhile((function(Tv): bool) $fn): Set<Tv>
Returns aSet
containing the values of the currentSet
starting after and including the first value that producestrue
when passed to the specified callback->slice(int $start, int $len): Set<Tv>
Returns a subset of the currentSet
starting from a given key up to, but not including, the element at the provided length from the starting key->take(int $n): Set<Tv>
Returns aSet
containing the firstn
values of the currentSet
->takeWhile((function(Tv): bool) $callback): Set<Tv>
Returns aSet
containing the values of the currentSet
up to but not including the first value that producesfalse
when passed to the specified callback->toDArray(): darray<Tv, Tv>
Returns a darray built from the values from this Set, darray[val1 => val1, val2 => val2, ...]->toImmMap(): ImmMap<arraykey, Tv>
Returns an immutable map (ImmMap
) based on the values of the currentSet
->toImmSet(): ImmSet<Tv>
Returns an immutable (ImmSet
), deep copy of the currentSet
->toImmVector(): ImmVector<Tv>
Returns an immutable vector (ImmVector
) with the values of the currentSet
->toKeysArray(): varray<Tv>
Returns anarray
containing the values from the currentSet
->toMap(): Map<arraykey, Tv>
Returns aMap
based on the values of the currentSet
->toSet(): Set<Tv>
Returns a deep copy of the currentSet
->toVArray(): varray<Tv>
->toValuesArray(): varray<Tv>
Returns anarray
containing the values from the currentSet
->toVector(): Vector<Tv>
Returns aVector
of the currentSet
values->values(): Vector<Tv>
Returns aVector
containing the values of the currentSet
->zip<Tu>(Traversable<Tu> $traversable): Set<nothing>
Throws an exception unless the currentSet
or theTraversable
is empty