Contents

Ds\Sequence Functions

Ds\Deque Functions

allocate()
Description: This is used to allocate memory as specified in the argument.
Example:

				
					$sequence = new \Ds\Vector([1, 2, 3]);
$sequence->allocate(10);

echo $sequence->capacity();
// Output: 10

				
			

apply()
Description: Update the values of the Deque by performing operations as defined by the callback function.
Example:

				
					$sequence = new \Ds\Vector([1, 2, 3]);
$sequence->apply(fn($value) => $value * 2);

print_r($sequence);
// Output: Ds\Vector Object ([0] => 2 [1] => 4 [2] => 6)

				
			

capacity()
Description: Return the current capacity of the sequence.
Example:

				
					$sequence = new \Ds\Vector([1, 2]);
echo $sequence->capacity();
// Output: 2

				
			

contains()
Description: Check whether the given value exists in the sequence.
Example:

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

				
			

filter()
Description: Create a new sequence using a filter function.
Example:

				
					$sequence = new \Ds\Vector([1, 2, 3, 4]);
$filtered = $sequence->filter(fn($value) => $value % 2 == 0);

print_r($filtered);
// Output: Ds\Vector Object ([0] => 2 [1] => 4)

				
			

find()
Description: Find the index of a value in the sequence. Returns false if not found.
Example:

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

				
			

first()
Description: Return the first element in the sequence.
Example:

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

				
			

get()
Description: Return the value at the given index.
Example:

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

				
			

insert()
Description: Insert a value at the given index in the sequence.
Example:

				
					$sequence = new \Ds\Vector([1, 2, 4]);
$sequence->insert(2, 3);

print_r($sequence);
// Output: Ds\Vector Object ([0] => 1 [1] => 2 [2] => 3 [3] => 4)

				
			

join()
Description: Join all values in the sequence into a string.
Example:

				
					$sequence = new \Ds\Vector([1, 2, 3]);
echo $sequence->join(", ");
// Output: 1, 2, 3

				
			

last()
Description: Return the last element in the sequence.
Example:

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

				
			

map()
Description: Return the result of applying a callback function to each value in the sequence.
Example:

				
					$sequence = new \Ds\Vector([1, 2, 3]);
$mapped = $sequence->map(fn($value) => $value * 2);

print_r($mapped);
// Output: Ds\Vector Object ([0] => 2 [1] => 4 [2] => 6)

				
			

merge()
Description: Returns a sequence after adding all given values to the sequence.
Example:

				
					$sequence = new \Ds\Vector([1, 2]);
$merged = $sequence->merge([3, 4, 5]);

print_r($merged);
// Output: Ds\Vector Object ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5)

				
			

pop()
Description: Removes and returns the last value from the sequence.
Example:

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

print_r($sequence);
// Output: Ds\Vector Object ([0] => 1 [1] => 2)

				
			

push()
Description: Adds values to the end of the sequence.
Example:

				
					$sequence = new \Ds\Vector([1, 2]);
$sequence->push(3, 4);

print_r($sequence);
// Output: Ds\Vector Object ([0] => 1 [1] => 2 [2] => 3 [3] => 4)

				
			

reduce()
Description: Reduce the sequence to a single value using a callback function.
Example:

				
					$sequence = new \Ds\Vector([1, 2, 3, 4]);
$result = $sequence->reduce(fn($carry, $value) => $carry + $value, 0);

echo $result;
// Output: 10

				
			

remove()
Description: Remove and return the value at a given index.
Example:

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

print_r($sequence);
// Output: Ds\Vector Object ([0] => 1 [1] => 3)

				
			

reverse()
Description: Reverse the sequence in place.
Example:

				
					$sequence = new \Ds\Vector([1, 2, 3]);
$sequence->reverse();

print_r($sequence);
// Output: Ds\Vector Object ([0] => 3 [1] => 2 [2] => 1)

				
			

reversed()
Description: Return a reversed copy of the sequence.
Example:

				
					$sequence = new \Ds\Vector([1, 2, 3]);
$reversed = $sequence->reversed();

print_r($reversed);
// Output: Ds\Vector Object ([0] => 3 [1] => 2 [2] => 1)

				
			

rotate()
Description: Rotate the sequence elements by a given number of rotations.
Example:

				
					$sequence = new \Ds\Vector([1, 2, 3, 4]);
$sequence->rotate(2);

print_r($sequence);
// Output: Ds\Vector Object ([0] => 3 [1] => 4 [2] => 1 [3] => 2)

				
			

set()
Description: Update the value at a given index in the sequence.
Example:

				
					$sequence = new \Ds\Vector([1, 2, 3]);
$sequence->set(1, 42);

print_r($sequence);
// Output: Ds\Vector Object ([0] => 1 [1] => 42 [2] => 3)

				
			

shift()
Description: Remove and return the first element of the sequence.
Example:

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

print_r($sequence);
// Output: Ds\Vector Object ([0] => 2 [1] => 3)

				
			

slice()
Description: Return a sub-sequence containing elements within the specified range.
Example:

				
					$sequence = new \Ds\Vector([1, 2, 3, 4, 5]);
$sliced = $sequence->slice(1, 3);

print_r($sliced);
// Output: Ds\Vector Object ([0] => 2 [1] => 3 [2] => 4)

				
			

sort()
Description: Sort the sequence elements in place.
Example:

				
					$sequence = new \Ds\Vector([3, 1, 2]);
$sequence->sort();

print_r($sequence);
// Output: Ds\Vector Object ([0] => 1 [1] => 2 [2] => 3)

				
			

sorted()
Description: Return a sorted copy of the sequence.
Example:

				
					$sequence = new \Ds\Vector([3, 1, 2]);
$sorted = $sequence->sorted();

print_r($sorted);
// Output: Ds\Vector Object ([0] => 1 [1] => 2 [2] => 3)

				
			

sum()
Description: Return the sum of all values in the sequence.
Example:

				
					$sequence = new \Ds\Vector([1, 2, 3, 4, 5]);
echo $sequence->sum();
// Output: 15

				
			

toArray()
Description: Convert the sequence into an array.
Example:

				
					$sequence = new \Ds\Vector([1, 2, 3]);
$array = $sequence->toArray();

print_r($array);
// Output: Array ( [0] => 1 [1] => 2 [2] => 3 )

				
			

unshift()
Description: Add values to the beginning of the sequence.
Example:

				
					$sequence = new \Ds\Vector([2, 3]);
$sequence->unshift(1);

print_r($sequence);
// Output: Ds\Vector Object ([0] => 1 [1] => 2 [2] => 3)

				
			

apply()
Description: Update all values in the sequence by applying a callback function to each value.
Example:

				
					$sequence = new \Ds\Vector([1, 2, 3]);
$sequence->apply(fn($value) => $value * 2);

print_r($sequence);
// Output: Ds\Vector Object ([0] => 2 [1] => 4 [2] => 6)

				
			

filter()
Description: Create a new sequence containing only the elements that match the callback function’s condition.
Example:

				
					$sequence = new \Ds\Vector([1, 2, 3, 4, 5]);
$filtered = $sequence->filter(fn($value) => $value % 2 === 0);

print_r($filtered);
// Output: Ds\Vector Object ([0] => 2 [1] => 4)

				
			

find()
Description: Find the index of the first occurrence of a value in the sequence.
Example:

				
					$sequence = new \Ds\Vector([10, 20, 30, 40]);
echo $sequence->find(30);
// Output: 2

				
			

first()
Description: Return the first value in the sequence.
Example:

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

				
			

last()
Description: Return the last value in the sequence.
Example:

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

				
			

contains()
Description: Check if a sequence contains a given value.
Example:

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

				
			

map()
Description: Return a new sequence by applying a callback function to each value in the sequence.
Example:

				
					$sequence = new \Ds\Vector([1, 2, 3]);
$mapped = $sequence->map(fn($value) => $value * 10);

print_r($mapped);
// Output: Ds\Vector Object ([0] => 10 [1] => 20 [2] => 30)

				
			

rotate()
Description: Rotate the sequence elements by a given number of rotations (positive or negative).
Example:

				
					$sequence = new \Ds\Vector([1, 2, 3, 4]);
$sequence->rotate(-2);

print_r($sequence);
// Output: Ds\Vector Object ([0] => 3 [1] => 4 [2] => 1 [3] => 2)