Set
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
Creates a new instance, using either a traversable object or an array for the initial values.
Adds all given values to the set that haven't already been added.
Allocates enough memory for a required capacity.
Determines if the set contains all values.
Returns the current capacity.
Removes all values from the set.
Count elements of an object
Returns a shallow copy of the set.
Creates a new set using a callable to determine which values to include
Returns the first value in the set.
Returns the value at a given index.
No description
Returns whether the set is empty.
Joins all values together as a string using an optional separator between each value.
Returns the result of applying a callback function to each value in the set.
Reduces the set to a single value using a callback function.
Removes all given values from the set, ignoring any that are not in the set.
Reverses the set in-place.
Returns the last value in the set.
Sorts the set in-place, using an optional comparator function.
Returns a sorted copy, using an optional comparator function.
Returns the sum of all values in the set.
Converts the set to an array.
Specify data which should be serialized to JSON
No description
No description
No description
No description
Details
__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.
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).
allocate(int $capacity)
Allocates enough memory for a required capacity.
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).
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.
A \ B = {x ∈ A | x ∉ B}
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.
Traversable
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.
A ∩ B = {x : x ∈ A ∧ x ∈ B}
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.
Note: The current instance won't be affected.
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.
Note: Arrays and objects are considered equal to zero when calculating the sum.
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}
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)}
array
toArray()
Converts the set to an array.
Note: Casting to an array is not supported yet.
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