Contents

Ds\Map Function

Basic Ds\Map Function

allocate()
Description: Allocates enough memory for the required capacity.
Example:

				
					$set = new \Ds\Set();
$set->add(1, 2, 3);
print_r($set);
// Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 3)

				
			

apply()
Description: Applies a specific operation to all of the elements present in the map.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
$map->apply(function($key, $value) {
    return strtoupper($value);
});
// Output: Map with values ["ONE", "TWO"]

				
			

capacity()
Description: Returns the current capacity of the set.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
echo $map->capacity();
// Output: Capacity of the map (e.g., 2)

				
			

clear()
Description: Removes all values from the set.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
$map->clear();
// Output: The map is now empty.

				
			

__construct()
Description:
Creates a new instance of a Map.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
// Output: A map with key-value pairs [1 => "One", 2 => "Two"]

				
			

copy()
Description: Gets a shallow copy of the specified Map instance.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
$copy = $map->copy();
// Output: A shallow copy of the map [1 => "One", 2 => "Two"]

				
			

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:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
echo $map->count();
// Output: 2

				
			

diff()
Description: Creates a new set with elements in the current set but not in the given set.
Example:

				
					$map1 = new \Ds\Map([1 => "One", 2 => "Two"]);
$map2 = new \Ds\Map([2 => "Two", 3 => "Three"]);
$diffMap = $map1->diff($map2);
// Output: Map with [1 => "One"]

				
			

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: Gets the first key-value pair from the Map instance.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
$first = $map->first();
// Output: Pair with [1 => "One"]

				
			

get()
Description: Returns the value of the given key.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
echo $map->get(1);
// Output: "One"

				
			

hasKey()
Description: Checks whether a given Key is present in the Map object.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
echo $map->hasKey(1);
// Output: 1 (true)

				
			

intersect()
Description: Creates a new set with elements common to both sets.
Example:

				
					$map1 = new \Ds\Map([1 => "One", 2 => "Two"]);
$map2 = new \Ds\Map([2 => "Two", 3 => "Three"]);
$intersection = $map1->intersect($map2);
// Output: Map with [2 => "Two"]

				
			

isEmpty()
Description: Checks whether a given Map is empty or not.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
echo $map->isEmpty();
// Output:  (false)

				
			

keys()
Description: Returns the set of keys of the current Map instance.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
$keys = $map->keys();
// Output: [1, 2]

				
			

ksort()
Description: Sorts the map elements in-place by key.
Example:

				
					$map = new \Ds\Map([2 => "Two", 1 => "One"]);
$map->ksort();
// Output: Map with [1 => "One", 2 => "Two"]

				
			

ksorted()
Description: Returns a copy which is sorted by key.
Example:

				
					$map = new \Ds\Map([2 => "Two", 1 => "One"]);
$sortedMap = $map->ksorted();
// Output: Map with [1 => "One", 2 => "Two"]

				
			

last()
Description: Finds and gets the last key-value pair from a Map object.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
$last = $map->last();
// Output: Pair with [2 => "Two"]

				
			

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)

				
			

map()
Description: Applies a callback function to a Map object.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
$newMap = $map->map(function($key, $value) {
    return strtoupper($value);
});
// Output: Map with [1 => "ONE", 2 => "TWO"]

				
			

merge()
Description: Returns the result of adding all given associations.
Example:

				
					$map1 = new \Ds\Map([1 => "One"]);
$map2 = new \Ds\Map([2 => "Two"]);
$mergedMap = $map1->merge($map2);
// Output: Map with [1 => "One", 2 => "Two"]

				
			

slice()
Description: Returns a subset of the set based on the specified range.
Example:

				
					$result = gmp_nextprime("100");
// Output: (next prime is 101)

				
			

pairs()
Description: Gets all of the pairs from the specified Map instance.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
$pairs = $map->pairs();
// Output: Pair objects [1 => "One", 2 => "Two"]

				
			

put()
Description: Associates a key with a value.
Example:

				
					$set = new \Ds\Set([3, 1, 2]);
$result = $set->sorted();
print_r($result);
// Output: Ds\Set Object ([0] => 1, [1] => 2, [2] => 3)

				
			

putAll()
Description: Associates all key-value pairs of a traversable object or array.
Example:

				
					$map = new \Ds\Map([1 => "One"]);
$map->putAll([2 => "Two", 3 => "Three"]);
// Output: Map with [1 => "One", 2 => "Two", 3 => "Three"]

				
			

reduce()
Description: Reduces the map to a single value by applying operations using the callback function.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
$result = $map->reduce(function($carry, $item) {
    return $carry . $item;
}, "");
// Output: "OneTwo"

				
			

reverse()
Description: In-place reverse the elements of a specified Map instance.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
$map->reverse();
// Output: Map with [2 => "Two", 1 => "One"]

				
			

reversed()
Description: Gets a copy of the reverse of elements of a specified Map instance.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
$reversedMap = $map->reversed();
// Output: Map with [2 => "Two", 1 => "One"]

				
			

skip()
Description: Returns the pair at a given positional index.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
$pair = $map->skip(1);
// Output: Pair with [2 => "Two"]

				
			

slice()
Description: Gets a subset of the specified Map instance.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two", 3 => "Three"]);
$subset = $map->slice(0, 2);
// Output: Map with [1 => "One", 2 => "Two"]

				
			

sort()
Description: In-place sort the elements of a specified Map instance according to the values.
Example:

				
					$map = new \Ds\Map([2 => "Two", 1 => "One"]);
$map->sort();
// Output: Map with [1 => "One", 2 => "Two"]

				
			

sorted()
Description: Gets a copy of the specified Map instance sorted according to the values.
Example:

				
					$map = new \Ds\Map([2 => "Two", 1 => "One"]);
$sortedMap = $map->sorted();
// Output: Map with [1 => "One", 2 => "Two"]

				
			

sum()
Description: Gets the sum of all of the values present in the Map instance.
Example:

				
					$map = new \Ds\Map([1 => 1, 2 => 2, 3 => 3]);
$sum = $map->sum();
// Output: 6

				
			

toArray()
Description: Gets an array generated by converting the Map instance into an Array.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
$array = $map->toArray();
// Output: [1 => "One", 2 => "Two"]

				
			

union()
Description: Creates a new map that contains the union of two maps.
Example:

				
					$map1 = new \Ds\Map([1 => "One", 2 => "Two"]);
$map2 = new \Ds\Map([3 => "Three", 4 => "Four"]);
$unionMap = $map1->union($map2);
// Output: Map with [1 => "One", 2 => "Two", 3 => "Three", 4 => "Four"]

				
			

values()
Description: Returns a sequence of the map’s values.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
$values = $map->values();
// Output: ["One", "Two"]

				
			

xor()
Description: Creates a new map that contains the value either in the first map or the second map, but not both.
Example:

				
					$map1 = new \Ds\Map([1 => "One", 2 => "Two"]);
$map2 = new \Ds\Map([2 => "Two", 3 => "Three"]);
$xorMap = $map1->xor($map2);
// Output: Map with [1 => "One", 3 => "Three"]

				
			

mergeRecursive()
Description: Recursively merges all key-value pairs from the provided map into the current map.
Example:

				
					$map1 = new \Ds\Map([1 => "One", 2 => "Two"]);
$map2 = new \Ds\Map([2 => "Second", 3 => "Three"]);
$mergedMap = $map1->mergeRecursive($map2);
// Output: Map with [1 => "One", 2 => "Second", 3 => "Three"]

				
			

pop()
Description: Removes and returns the last key-value pair from the map.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
$last = $map->pop();
// Output: Pair with [2 => "Two"], and the map becomes [1 => "One"]

				
			

push()
Description: Adds a key-value pair to the end of the map.
Example:

				
					$map = new \Ds\Map([1 => "One"]);
$map->push(2, "Two");
// Output: Map with [1 => "One", 2 => "Two"]

				
			

shift()
Description: Removes and returns the first key-value pair from the map.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
$first = $map->shift();
// Output: Pair with [1 => "One"], and the map becomes [2 => "Two"]

				
			

sliceKeys()
Description: Returns a subset of the map containing only the specified keys.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two", 3 => "Three"]);
$sliced = $map->sliceKeys([1, 3]);
// Output: Map with [1 => "One", 3 => "Three"]

				
			

mapKeys()
Description: Transforms the keys of the map using a given callback function.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
$newMap = $map->mapKeys(function($key) {
    return $key * 2;
});
// Output: Map with [2 => "One", 4 => "Two"]

				
			

swap()
Description: Swaps the positions of two elements in the map using their keys.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two", 3 => "Three"]);
$map->swap(1, 3);
// Output: Map with [3 => "One", 2 => "Two", 1 => "Three"]

				
			

mapKeys()
Description: Transforms the keys of the map using a given callback function.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
$newMap = $map->mapKeys(function($key) {
    return $key * 2;
});
// Output: Map with [2 => "One", 4 => "Two"]

				
			

unique()
Description: Removes duplicate values from the map.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "One", 3 => "Two"]);
$uniqueMap = $map->unique();
// Output: Map with [1 => "One", 3 => "Two"]

				
			

find()
Description: Finds and returns the key of a given value.
Example:

				
					$map = new \Ds\Map([1 => "One", 2 => "Two"]);
$key = $map->find("Two");
// Output: 2