Contents
Ds\Set Functions
Basic Ds\Set Functions
add()
Description: Adds one or more values to the set.
Example:
$set = new \Ds\Set();
$set->add(1, 2, 3);
print_r($set);
// Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 3)
allocate()
Description: Allocates memory for a given capacity.
Example:
$set = new \Ds\Set();
$set->allocate(10);
// Output: (allocates memory for 10 elements)
capacity()
Description: Returns the current capacity of the set.
Example:
$set = new \Ds\Set([1, 2, 3]);
echo $set->capacity();
// Output: (capacity is 3)
clear()
Description: Removes all values from the set.
Example:
$set = new \Ds\Set([1, 2, 3]);
$set->clear();
print_r($set);
// Output: Ds\Set Object ()
__construct()
Description: Creates a new instance of a set.
Example:
$set = new \Ds\Set([1, 2, 3]);
print_r($set);
// Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 3)
contains()
Description: Checks if the given value exists in the set.
Example:
$set = new \Ds\Set([1, 2, 3]);
var_dump($set->contains(2));
// Output: (bool true)
copy()
Description: Returns a shallow copy of the set.
Example:
$result = gmp_div_qr("10", "3");
// Output: ([quotient => 3, remainder => 1])
count()
Description: Counts the number of values in the set.
Example:
$set = new \Ds\Set([1, 2, 3]);
echo $set->count();
// Output: (3)
diff()
Description: Creates a new set with elements in the current set but not in the given set.
Example:
$set1 = new \Ds\Set([1, 2, 3]);
$set2 = new \Ds\Set([2, 3, 4]);
$result = $set1->diff($set2);
print_r($result);
// Output: Ds\Set Object ([0] => 1)
filter()
Description: Creates a new set with values that pass a callback function.
Example:
$set = new \Ds\Set([1, 2, 3, 4]);
$result = $set->filter(fn($value) => $value % 2 === 0);
print_r($result);
// Output: Ds\Set Object ([0] => 2, [1] => 4)
first()
Description: Returns the first value in the set.
Example:
$set = new \Ds\Set([1, 2, 3]);
echo $set->first();
// Output: (1)
get()
Description: Retrieves a value by its index.
Example:
$set = new \Ds\Set([1, 2, 3]);
echo $set->get(1);
// Output: (2)
gmp_gcdext()
Description: Calculates the GCD and multipliers for the equation.
Example:
$result = gmp_gcdext("48", "18");
// Output: ([gcd => 6, s => -1, t => 3])
intersect()
Description: Creates a new set with elements common to both sets.
Example:
$set1 = new \Ds\Set([1, 2, 3]);
$set2 = new \Ds\Set([2, 3, 4]);
$result = $set1->intersect($set2);
print_r($result);
// Output: Ds\Set Object ([0] => 2, [1] => 3)
isEmpty()
Description: Checks whether the set is empty.
Example:
$set = new \Ds\Set();
var_dump($set->isEmpty());
// Output: (bool true)
join()
Description: Joins all elements of the set into a string.
Example:
$set = new \Ds\Set(["a", "b", "c"]);
echo $set->join(", ");
// Output: (a, b, c)
last()
Description: Returns the last value in the set.
Example:
$set = new \Ds\Set([1, 2, 3]);
echo $set->last();
// Output: (3)
merge()
Description: Merges another set or array into the current set.
Example:
$set = new \Ds\Set([1, 2]);
$result = $set->merge([3, 4]);
print_r($result);
// Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 3, [3] => 4)
reduce()
Description: Reduces the set to a single value using a callback.
Example:
$set = new \Ds\Set([1, 2, 3]);
$result = $set->reduce(fn($carry, $value) => $carry + $value, 0);
echo $result;
// Output: (6)
remove()
Description: Removes specific values from the set.
Example:
$set = new \Ds\Set([1, 2, 3, 4]);
$set->remove(2, 4);
print_r($set);
// Output: Ds\Set Object ([0] => 1, [1] => 3)
reverse()
Description: Reverses the order of elements in the set.
Example:
$set = new \Ds\Set([1, 2, 3]);
$set->reverse();
print_r($set);
// Output: Ds\Set Object ([0] => 3, [1] => 2, [2] => 1)
reversed()
Description: Creates a new set with values in reverse order.
Example:
$set = new \Ds\Set([1, 2, 3]);
$result = $set->reversed();
print_r($result);
// Output: Ds\Set Object ([0] => 3, [1] => 2, [2] => 1)
slice()
Description: Returns a subset of the set based on the specified range.
Example:
$result = gmp_nextprime("100");
// Output: (next prime is 101)
sort()
Description: Sorts the elements of the set in-place.
Example:
$set = new \Ds\Set([3, 1, 2]);
$set->sort();
print_r($set);
// Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 3)
sorted()
Description: Returns a new set with elements sorted in ascending order.
Example:
$set = new \Ds\Set([3, 1, 2]);
$result = $set->sorted();
print_r($result);
// Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 3)
sum()
Description: Returns the sum of all elements in the set.
Example:
$set = new \Ds\Set([1, 2, 3]);
echo $set->sum();
// Output: (6)
toArray()
Description: Converts the set into an array.
Example:
$set = new \Ds\Set([1, 2, 3]);
print_r($set->toArray());
// Output: Array ([0] => 1, [1] => 2, [2] => 3)
union()
Description: Creates a new set that contains the union of two sets.
Example:
$set1 = new \Ds\Set([1, 2, 3]);
$set2 = new \Ds\Set([3, 4, 5]);
$result = $set1->union($set2);
print_r($result);
// Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 3, [3] => 4, [4] => 5)
xor()
Description: Creates a new set containing values that exist in either set but not both.
Example:
$set1 = new \Ds\Set([1, 2, 3]);
$set2 = new \Ds\Set([3, 4, 5]);
$result = $set1->xor($set2);
print_r($result);
// Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 4, [3] => 5)
add()
Description: Adds values to the set.
Example:
$set = new \Ds\Set([1, 2]);
$set->add(3);
print_r($set);
// Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 3)
allocate()
Description: Allocates memory for the required capacity of the set.
Example:
$set = new \Ds\Set([1, 2, 3]);
$set->allocate(10);
echo "Capacity: " . $set->capacity();
// Output: Capacity: 10
capacity()
Description: Returns the capacity of the set.
Example:
$set = new \Ds\Set([1, 2, 3]);
echo $set->capacity();
// Output: 8 (The default capacity may vary based on implementation)
clear()
Description: Removes all values from the set.
Example:
$set = new \Ds\Set([1, 2, 3]);
$set->clear();
print_r($set);
// Output: Ds\Set Object () (empty set)
__construct()
Description: Creates a new instance of a set.
Example:
$set = new \Ds\Set([1, 2, 3]);
print_r($set);
// Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 3)
contains()
Description: Checks if a given value exists in the set.
Example:
$set = new \Ds\Set([1, 2, 3]);
echo $set->contains(2);
// Output: 1 (true)
copy()
Description: Returns a copy of the set.
Example:
$set = new \Ds\Set([1, 2, 3]);
$copy = $set->copy();
print_r($copy);
// Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 3)
count()
Description: Counts the number of values present in the set.
Example:
$set = new \Ds\Set([1, 2, 3]);
echo $set->count();
// Output: 3
diff()
Description: Creates a set containing the elements of the first set that are not present in the second set.
Example:
$set1 = new \Ds\Set([1, 2, 3]);
$set2 = new \Ds\Set([3, 4, 5]);
$result = $set1->diff($set2);
print_r($result);
// Output: Ds\Set Object ([0] => 1, [1] => 2)
filter()
Description: Creates a new set using a filter function.
Example:
$set = new \Ds\Set([1, 2, 3, 4]);
$result = $set->filter(function($value) {
return $value > 2;
});
print_r($result);
// Output: Ds\Set Object ([0] => 3, [1] => 4)
first()
Description: Returns the first element of the set.
Example:
$set = new \Ds\Set([1, 2, 3]);
echo $set->first();
// Output: 1
get()
Description: Gets a value from the set instance.
Example:
$set = new \Ds\Set([1, 2, 3]);
echo $set->get(1);
// Output: 2 (Gets the value at index 1)
intersect()
Description: Creates a new set that contains the intersection of two sets.
Example:
$set1 = new \Ds\Set([1, 2, 3]);
$set2 = new \Ds\Set([2, 3, 4]);
$result = $set1->intersect($set2);
print_r($result);
// Output: Ds\Set Object ([0] => 2, [1] => 3)
isEmpty()
Description: Checks whether the set is empty or not.
Example:
$set = new \Ds\Set([1, 2, 3]);
echo $set->isEmpty() ? 'Yes' : 'No';
// Output: No
join()
Description: Joins all values in the set as a string.
$set = new \Ds\Set([1, 2, 3]);
echo $set->join(', ');
// Output: 1, 2, 3