Contents

Ds\Vector Functions

Ds\Vector Functions

allocate()
Description: Provides the custom size of the vector to allocate space.
Example:

				
					$vector = new Vector();
$vector->allocate(10);
echo $vector->capacity();
// Output: 10

				
			

apply()
Description: Updates all values in the array by applying the callback function to each value of the vector.
Example:

				
					$vector = new Vector([1, 2, 3]);
$vector->apply(fn($value) => $value * 2);
print_r($vector->toArray());
// Output: [2, 4, 6]

				
			

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

				
					$vector = new Vector();
echo $vector->capacity();
// Output: 0

				
			

clear()
Description: Clears the vector elements by removing all the elements.
Example:

				
					$vector = new Vector([1, 2, 3]);
$vector->clear();
echo $vector->isEmpty();
// Output: 1 (true)

				
			

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

				
					$vector = new Vector([1, 2, 3]);
print_r($vector->toArray());
// Output: [1, 2, 3]

				
			

contains()
Description: Checks whether the vector contains the given value.
Example:

				
					$vector = new Vector([1, 2, 3]);
echo $vector->contains(2);
// Output: 1 (true)

				
			

copy()
Description: Returns a shallow copy of the set.
Example:

				
					$result = gmp_div_qr("10", "3");
// Output: ([quotient => 3, remainder => 1])

				
			

copy()
Description: Creates a copy of the given vector.
Example:

				
					$vector = new Vector([1, 2, 3]);
$copy = $vector->copy();
print_r($copy->toArray());
// Output: [1, 2, 3]

				
			

count()
Description: Counts the number of elements in the vector.
Example:

				
					$vector = new Vector([1, 2, 3]);
echo $vector->count();
// Output: 3

				
			

filter()
Description: Filters out elements that satisfy the condition defined in the callback function.
Example:

				
					$vector = new Vector([1, 2, 3, 4]);
$result = $vector->filter(fn($value) => $value > 2);
print_r($result->toArray());
// Output: [3, 4]

				
			

find()
Description: Finds the index of the element in the vector.
Example:

				
					$vector = new Vector([1, 2, 3]);
echo $vector->find(2);
// Output: 1

				
			

first()
Description: Returns the first element in the vector.
Example:

				
					$vector = new Vector([1, 2, 3]);
echo $vector->first();
// Output: 1

				
			

get()
Description: Returns the element at the given index.
Example:

				
					$vector = new Vector([1, 2, 3]);
echo $vector->get(1);
// Output: 2

				
			

insert()
Description: Inserts the element into the vector at the given index.
Example:

				
					$vector = new Vector([1, 3]);
$vector->insert(1, 2);
print_r($vector->toArray());
// Output: [1, 2, 3]

				
			

isEmpty()
Description: Checks whether the vector is empty.
Example:

				
					$vector = new Vector();
echo $vector->isEmpty();
// Output: 1 (true)

				
			

join()
Description: Joins elements of the vector into a string using the specified separator.
Example:

				
					$vector = new Vector(["a", "b", "c"]);
echo $vector->join(",");
// Output: a,b,c

				
			

jsonSerialize()
Description: Returns elements that can be converted to JSON.
Example:

				
					$vector = new Vector(["a", "b", "c"]);
echo json_encode($vector);
// Output: ["a","b","c"]

				
			

last()
Description: Returns the last element of the vector.
Example:

				
					$vector = new Vector([1, 2, 3]);
echo $vector->last();
// Output: 3

				
			

map()
Description: Returns the result of a callback after applying it to each value in the vector.
Example:

				
					$vector = new Vector([1, 2, 3]);
$result = $vector->map(fn($value) => $value * 2);
print_r($result->toArray());
// Output: [2, 4, 6]

				
			

merge()
Description: Merges all the elements into the vector.
Example:

				
					$vector = new Vector([1, 2]);
$vector->merge([3, 4]);
print_r($vector->toArray());
// Output: [1, 2, 3, 4]

				
			

pop()
Description: Removes the last element of the vector and returns it.
Example:

				
					$vector = new Vector([1, 2, 3]);
echo $vector->pop();
// Output: 3

				
			

push()
Description: Adds elements to the end of the vector.
Example:

				
					$vector = new Vector([1, 2]);
$vector->push(3);
print_r($vector->toArray());
// Output: [1, 2, 3]

				
			

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

				
					$vector = new Vector([1, 2, 3]);
$result = $vector->reduce(fn($carry, $value) => $carry + $value, 0);
echo $result;
// Output: 6

				
			

remove()
Description: Removes and returns a value by index.
Example:

				
					$vector = new Vector([1, 2, 3]);
echo $vector->remove(1);
// Output: 2

				
			

reverse()
Description: Reverses the vector elements in place.
Example:

				
					$vector = new Vector([1, 2, 3]);
$vector->reverse();
print_r($vector->toArray());
// Output: [3, 2, 1]

				
			

rotate()
Description: Rotates the array elements by a given number of rotations. Rotations happen in place.
Example:

				
					$vector = new Vector([1, 2, 3, 4]);
$vector->rotate(2);
print_r($vector->toArray());
// Output: [3, 4, 1, 2]

				
			

set()
Description: Sets the value in the vector at the given index.
Example:

				
					$vector = new Vector([1, 2, 3]);
$vector->set(1, 5);
print_r($vector->toArray());
// Output: [1, 5, 3]

				
			

shift()
Description: Removes the first element from the vector and returns it.
Example:

				
					$vector = new Vector([1, 2, 3]);
echo $vector->shift();
// Output: 1

				
			

slice()
Description: Returns the sub-vector of the given vector.
Example:

				
					$vector = new Vector([1, 2, 3, 4]);
$subVector = $vector->slice(1, 3);
print_r($subVector->toArray());
// Output: [2, 3]

				
			

sort()
Description: Sorts the elements of the vector in place.
Example:

				
					$vector = new Vector([3, 1, 2]);
$vector->sort();
print_r($vector->toArray());
// Output: [1, 2, 3]