A set is a collection of unique values. The Ds\Set in PHP works like Ds\Map
and uses a hash table. In Ds\Set, the values act as keys, and the mapped
values are not used.
Requirements
To use Ds\Set, you need:
- PHP 7 or later
- The compatibility polyfill
Installation
The easiest way to install Ds\Set is by using the PECL extension. Run the
following command:
pecl install ds
Syntax
The syntax for using functions in Ds\Set is:
public Ds\Set::functionName()
Example
Here is an example of using the
Ds\Set::count() function:
<?php // Declare a new Set $set = new \Ds\Set([10, 15, 21]); // Display the Set elements var_dump($set); // Display the count of elements in the Set echo "Count is: "; print_r($set->count()); ?>
Output
The output of the above code will be:
object(Ds\Set)#1 (3) {
[0]=>
int(10)
[1]=>
int(15)
[2]=>
int(21)
}
Count is: 3
This simple guide should help you understand and use Ds\Set in your PHP
projects.
Complete list of data structure DS\Set:
| Ds\Set Functions | Description |
|---|---|
| add(value) | Add a new value to the set. The value must be unique, and attempting to add duplicate values will have no effect. |
| allocate(capacity) | Allocate enough memory for the set to hold a specific number of elements (`capacity`). This pre-allocates space to potentially avoid performance overhead during insertions. |
| capacity() | Return the current maximum number of elements the set can hold without needing reallocation. |
| clear() | Remove all elements from the set. |
| __construct([iterable]) | **Optional:** Create a new set instance. You can optionally provide an iterable object (like an array) to populate the set initially. |
| contains(value) | Check whether the set contains a specific value. |
| copy() | Create a new set that is a shallow copy of the original set. Changes to the original set will not be reflected in the copy, and vice versa. |
| count() | Return the number of elements currently in the set. |
| diff(otherSet) | Create a new set containing only the elements that are present in the first set but not in the `otherSet`. |
| filter(callback) | Create a new set containing only the elements that pass a test defined by the callback function. |
| first() | Return the first element (in insertion order) from the set. If the set is empty, it may return an error or null value. |
| get(value) | **Not applicable:** Sets don't use keys for retrieval. Use `contains` to check if a value exists. |
| intersect(otherSet) | Create a new set containing only the elements that are present in both the current set and the `otherSet`. |
| isEmpty() | Check whether the set is empty (contains no elements). |
| join(separator) | Join all values in the set into a string, separated by the provided delimiter (separator). |
| last() | Return the last element added to the set (if the insertion order is maintained). If the set is empty, it may return an error or null value. |
| merge(iterable) | Add all the elements from the provided iterable (like an array) to the set. Duplicate values will be ignored. |
| reduce(callback, initial_value) | Reduce the set to a single value by applying a callback function to each element sequentially. The callback function takes the accumulated result and the current element as arguments and returns a new accumulated value. |
| remove(value) | Remove a specific value from the set. If the value is not found, no error is raised. |
| reverse() | **Not applicable:** Sets don't have a defined order. Use `toArray` and reverse the resulting array if needed. |
| reversed() | **Not applicable:** Similar to `reverse`, creating a copy with a reversed order isn't efficient for sets. |
| slice(offset, length) | **Not applicable:** Slicing a set doesn't make sense as sets don't maintain a specific order. |
| sort([comparison_function]) | Sort the elements in-place by value. You can optionally provide a comparison function to define the sorting order. Sorting a set might not be very efficient due to the underlying hashing mechanism. |
| sorted([comparison_function]) | Create a new set containing a copy of the elements sorted by value. You can optionally provide a comparison function to define the sorting order. Sorting a set might not be very efficient due to the underlying hashing mechanism. |
| sum() | **Not applicable:** Sets typically don't contain numerical values that can be summed. If you need to perform mathematical operations, consider converting the set to a more suitable data structure like an array. |
| toArray() | Convert the set to a regular PHP array. The order of elements in the resulting array may not reflect the insertion order in the set. |
| union(otherSet) | Create a new set containing all the elements that are present in either the current set or the `otherSet`, or both. |
| xor(otherSet) | Create a new set containing the elements that are present in exactly one of the current set and the `otherSet`. Elements that are in both sets will be excluded. |
