class Set implements Collection, ArrayAccess (View source)

A Set is a sequence of unique values. This implementation uses the same hash table as Ds\Map, where values are used as keys and the mapped value is ignored.

Methods

__construct(iterable $values = [])

Creates a new instance, using either a traversable object or an array for the initial values.

add(TValue ...$values)

Adds all given values to the set that haven't already been added.

allocate(int $capacity)

Allocates enough memory for a required capacity.

bool
contains(TValue ...$values)

Determines if the set contains all values.

int
capacity()

Returns the current capacity.

void
clear()

Removes all values from the set.

int
count()

Count elements of an object

TValue>
copy()

Returns a shallow copy of the set.

Set
diff(Set $set)

Creates a new set using values that aren't in another set.

Set
filter(callable|null $callback = null)

Creates a new set using a callable to determine which values to include

TValue
first()

Returns the first value in the set.

TValue
get(int $index)

Returns the value at a given index.

getIterator()

No description

Set
intersect(Set $set)

Creates a new set using values common to both the current instance and another set. In other words, returns a copy of the current instance with all values removed that are not in the other set.

bool
isEmpty()

Returns whether the set is empty.

string
join(string|null $glue = null)

Joins all values together as a string using an optional separator between each value.

Set
map(callable $callback)

Returns the result of applying a callback function to each value in the set.

Set
merge(TValue2> $values)

Returns the result of adding all given values to the set.

TCarry
reduce(callable $callback, TCarry $initial = null)

Reduces the set to a single value using a callback function.

remove(TValue ...$values)

Removes all given values from the set, ignoring any that are not in the set.

reverse()

Reverses the set in-place.

Set
reversed()

Returns a reversed copy of the set.

Set
slice(int $index, int|null $length = null)

Returns a sub-set of a given range

TValue
last()

Returns the last value in the set.

sort(callable|null $comparator = null)

Sorts the set in-place, using an optional comparator function.

Set
sorted(callable|null $comparator = null)

Returns a sorted copy, using an optional comparator function.

float|int
sum()

Returns the sum of all values in the set.

Set
union(Set $set)

Creates a new set that contains the values of the current instance as well as the values of another set.

Set
xor(Set $set)

Creates a new set using values in either the current instance or in another set, but not in both.

array
toArray()

Converts the set to an array.

mixed
jsonSerialize()

Specify data which should be serialized to JSON

bool
offsetExists(TKey $offset)

No description

mixed
offsetGet(TKey $offset)

No description

void
offsetSet(TKey $offset, TValue $value)

No description

void
offsetUnset(TKey $offset)

No description

Details

__construct(iterable $values = [])

Creates a new instance, using either a traversable object or an array for the initial values.

Parameters

iterable $values

A traversable object of an array to use the initial values.

add(TValue ...$values)

Adds all given values to the set that haven't already been added.

Note: Values of type object are supported. If an object implements Ds\Hashable, equality will be determined by the object's equals function. If an object does not implement Ds\Hashable, objects must be references to the same instance to be considered equal.

Caution: All comparisons are strict (type and value).

Parameters

TValue ...$values

Values to add to the set.

allocate(int $capacity)

Allocates enough memory for a required capacity.

Parameters

int $capacity

The number of values for which capacity should be allocated.

Note: Capacity will stay the same if this value is less than or equal to the current capacity.

Capacity will always be rounded up to the nearest power of 2.

bool contains(TValue ...$values)

Determines if the set contains all values.

Values of type object are supported. If an object implements Ds\Hashable, equality will be determined by the object's equals function. If an object does not implement Ds\Hashable, objects must be references to the same instance to be considered equal.

Caution: All comparisons are strict (type and value).

Parameters

TValue ...$values

Values to check.

Return Value

bool

int capacity()

Returns the current capacity.

Return Value

int

void clear()

Removes all values from the set.

Return Value

void

int count()

Since: 5.1

Count elements of an object

Return Value

int

The custom count as an integer.

The return value is cast to an integer.

TValue> copy()

Returns a shallow copy of the set.

Return Value

TValue>

Set diff(Set $set)

Creates a new set using values that aren't in another set.

A \ B = {x ∈ A | x ∉ B}

Parameters

Set $set

Set containing the values to exclude.

Return Value

Set

A new set containing all values that were not in the other set.

Set filter(callable|null $callback = null)

Creates a new set using a callable to determine which values to include

Parameters

callable|null $callback

Return Value

Set

A new set containing all the values for which either the callback returned TRUE, or all values that convert to TRUE if a callback was not provided.

TValue first()

Returns the first value in the set.

Return Value

TValue

The first value in the set.

TValue get(int $index)

Returns the value at a given index.

Parameters

int $index

The index to access, starting at 0.

Return Value

TValue

The value at the requested index.

Traversable getIterator()

No description

Return Value

Traversable

An instance of an object implementing Iterator or Traversable

Set intersect(Set $set)

Creates a new set using values common to both the current instance and another set. In other words, returns a copy of the current instance with all values removed that are not in the other set.

A ∩ B = {x : x ∈ A ∧ x ∈ B}

Parameters

Set $set

The other set.

Return Value

Set

The intersection of the current instance and another set.

bool isEmpty()

Returns whether the set is empty.

Return Value

bool

string join(string|null $glue = null)

Joins all values together as a string using an optional separator between each value.

Parameters

string|null $glue

An optional string to separate each value.

Return Value

string

Set map(callable $callback)

Returns the result of applying a callback function to each value in the set.

Parameters

callable $callback

Return Value

Set

The result of applying a callback to each value in the set.

Note: The values of the current instance won't be affected.

Set merge(TValue2> $values)

Returns the result of adding all given values to the set.

Note: The current instance won't be affected.

Parameters

TValue2> $values

A traversable object or an array.

Return Value

Set

The result of adding all given values to the set, effectively the same as adding the values to a copy, then returning that copy.

TCarry reduce(callable $callback, TCarry $initial = null)

Reduces the set to a single value using a callback function.

Parameters

callable $callback
TCarry $initial

The initial value of the carry value. Can be NULL.

Return Value

TCarry

The return value of the final callback.

remove(TValue ...$values)

Removes all given values from the set, ignoring any that are not in the set.

Parameters

TValue ...$values

The values to remove.

reverse()

Reverses the set in-place.

Set reversed()

Returns a reversed copy of the set.

Return Value

Set

A reversed copy of the set.

Set slice(int $index, int|null $length = null)

Returns a sub-set of a given range

Parameters

int $index

The index at which the sub-set starts. If positive, the set will start at that index in the set. If negative, the set will start that far from the end.

int|null $length

If a length is given and is positive, the resulting set will have up to that many values in it. If the length results in an overflow, only values up to the end of the set will be included. If a length is given and is negative, the set will stop that many values from the end. If a length is not provided, the resulting set will contain all values between the index and the end of the set.

Return Value

Set

A sub-set of the given range.

TValue last()

Returns the last value in the set.

Return Value

TValue

The last value in the set.

Exceptions

UnderflowException

sort(callable|null $comparator = null)

Sorts the set in-place, using an optional comparator function.

Parameters

callable|null $comparator

Set sorted(callable|null $comparator = null)

Returns a sorted copy, using an optional comparator function.

Parameters

callable|null $comparator

Return Value

Set

Returns a sorted copy of the set.

float|int sum()

Returns the sum of all values in the set.

Note: Arrays and objects are considered equal to zero when calculating the sum.

Return Value

float|int

The sum of all the values in the set as either a float or int depending on the values in the set.

Set union(Set $set)

Creates a new set that contains the values of the current instance as well as the values of another set.

A ∪ B = {x: x ∈ A ∨ x ∈ B}

Parameters

Set $set

The other set, to combine with the current instance.

Return Value

Set

A new set containing all the values of the current instance as well as another set.

Set xor(Set $set)

Creates a new set using values in either the current instance or in another set, but not in both.

A ⊖ B = {x : x ∈ (A \ B) ∪ (B \ A)}

Parameters

Set $set

The other set.

Return Value

Set

A new set containing values in the current instance as well as another set, but not in both.

array toArray()

Converts the set to an array.

Note: Casting to an array is not supported yet.

Return Value

array

An array containing all the values in the same order as the collection.

mixed jsonSerialize()

Since: 5.4

Specify data which should be serialized to JSON

Return Value

mixed

data which can be serialized by json_encode, which is a value of any type other than a resource.

bool offsetExists(TKey $offset)

No description

Parameters

TKey $offset

An offset to check for.

Return Value

bool

true on success or false on failure.

The return value will be casted to boolean if non-boolean was returned.

mixed offsetGet(TKey $offset)

No description

Parameters

TKey $offset

The offset to retrieve.

Return Value

mixed

Can return all value types.

void offsetSet(TKey $offset, TValue $value)

No description

Parameters

TKey $offset

The offset to assign the value to.

TValue $value

The value to set.

Return Value

void

void offsetUnset(TKey $offset)

No description

Parameters

TKey $offset

The offset to unset.

Return Value

void