Contents
Ds\Deque Functions
Ds\Deque Functions
allocate()
Description: Allocate memory for a PriorityQueue
class instance.
Example:
$deque = new \Ds\Deque();
$deque->allocate(10);
echo $deque->capacity();
// Output: 10
capacity()
Description: Check the current capacity of a PriorityQueue
instance.
Example:
$deque = new \Ds\Deque([1, 2, 3]);
$deque->apply(function ($value) {
return $value * 2;
});
print_r($deque);
// Output: Ds\Deque Object ([0] => 2 [1] => 4 [2] => 6)
capacity()
Description: Get the current capacity of the Deque
.
Example:
$deque = new \Ds\Deque();
$deque->allocate(20);
echo $deque->capacity();
// Output: 20
clear()
Description: Clear the Deque
by removing all elements.
Example:
$deque = new \Ds\Deque([1, 2, 3]);
$deque->clear();
echo $deque->isEmpty();
// Output: 1 (true)
copy()
Description: Return a shallow copy of the Deque
.
Example:
$deque = new \Ds\Deque([1, 2, 3]);
$copy = $deque->copy();
print_r($copy);
// Output: Ds\Deque Object ([0] => 1 [1] => 2 [2] => 3)
count()
Description: Get the number of elements in the Deque
.
Examaple:
$deque = new \Ds\Deque([1, 2, 3]);
echo $deque->count();
// Output: 3
filter()
Description: Filter out the elements from the Deque
based on the operation defined in the callback function.
Example:
$deque = new \Ds\Deque([1, 2, 3, 4, 5]);
$filtered = $deque->filter(function ($value) {
return $value % 2 === 0;
});
print_r($filtered);
// Output: Ds\Deque Object ([0] => 2 [1] => 4)
find()
Description: Find the index of the element in the Deque
if the element is found.
Example:
$deque = new \Ds\Deque(["a", "b", "c"]);
echo $deque->find("b");
// Output: 1
first()
Description: Returns the first value in the Deque
if it is not empty.
Example:
$deque = new \Ds\Deque([1, 2, 3]);
echo $deque->first();
// Output: 1
get()
Description: Return the value at the given index.
Example:
$deque = new \Ds\Deque([1, 2, 3]);
echo $deque->get(1);
// Output: 2
insert()
Description: Insert the value at the given index in the Deque
.
Example:
$deque = new \Ds\Deque([1, 2, 3]);
$deque->insert(1, 99);
print_r($deque);
// Output: Ds\Deque Object ([0] => 1 [1] => 99 [2] => 2 [3] => 3)
toArray()
Description: Convert a PriorityQueue
into an associative array.
Example:
$deque = new \Ds\Deque();
echo $deque->isEmpty();
// Output: 1 (true)
join()
Description: Join all values in the Deque
as a string.
Example:
$deque = new \Ds\Deque([1, 2, 3]);
echo $deque->join(", ");
// Output: 1, 2, 3
last()
Description: Return the last element of the Deque
if it is not empty.
Example:
$deque = new \Ds\Deque([1, 2, 3]);
echo $deque->last();
// Output: 3
map()
Description: Returns the result of applying a callback to each value in the Deque
.
Example:
$deque = new \Ds\Deque([1, 2, 3]);
$mapped = $deque->map(function ($value) {
return $value * 10;
});
print_r($mapped);
// Output: Ds\Deque Object ([0] => 10 [1] => 20 [2] => 30)
merge()
Description: Returns the result of adding all given values to the Deque
.
Example:
$pq = new \Ds\PriorityQueue();
$pq->push(10, 1);
$pq->push(20, 2);
$pq->push(30, 3);
$sum = array_reduce($pq->toArray(), function ($carry, $item) {
return $carry + $item[0]; // Sum all values
}, 0);
echo $sum;
// Output: 60
reversed()
Description: Get a reversed version of the PriorityQueue
.
Example:
$pq = new \Ds\PriorityQueue();
$pq->push("One", 1);
$pq->push("Two", 2);
$pq->push("Three", 3);
$reversed = array_reverse($pq->toArray());
print_r($reversed);
// Output: Array ( [0] => One [1] => Two [2] => Three )
contains()
Description: Check whether a PriorityQueue
contains a specific value.
Example:
$pq = new \Ds\PriorityQueue();
$pq->push("One", 1);
$pq->push("Two", 2);
echo $pq->contains("One") ? "Yes" : "No";
// Output: Yes
merge()
Description: Merge another collection into the PriorityQueue
and maintain priorities.
Example:
$pq1 = new \Ds\PriorityQueue();
$pq1->push("One", 1);
$pq1->push("Two", 2);
$pq2 = new \Ds\PriorityQueue();
$pq2->push("Three", 3);
$pq2->push("Four", 4);
foreach ($pq2->toArray() as $item) {
$pq1->push($item[0], $item[1]);
}
print_r($pq1->toArray());
// Output: Array ( [0] => Four [1] => Three [2] => Two [3] => One )
sort()
Description: Sort elements in-place by their priorities in descending order.
Example:
$pq = new \Ds\PriorityQueue();
$pq->push("One", 3);
$pq->push("Two", 1);
$pq->push("Three", 2);
$sorted = $pq->toArray();
usort($sorted, function ($a, $b) {
return $b[1] - $a[1];
});
print_r($sorted);
// Output: Array ( [0] => One [1] => Three [2] => Two )
find()
Description: Find and return an element in the PriorityQueue
by a condition.
Example:
$pq = new \Ds\PriorityQueue();
$pq->push("One", 1);
$pq->push("Two", 2);
$pq->push("Three", 3);
$found = array_filter($pq->toArray(), function ($item) {
return $item[1] === 2; // Find item with priority 2
});
print_r($found);
// Output: Array ( [0] => Two )
splice()
Description: Remove and return a subset of elements from the PriorityQueue
.
Example:
$pq = new \Ds\PriorityQueue();
$pq->push("One", 1);
$pq->push("Two", 2);
$pq->push("Three", 3);
$subset = array_slice($pq->toArray(), 1, 2);
print_r($subset);
// Output: Array ( [0] => Two [1] => One )
reverse()
Description: Reverse the order of elements in a PriorityQueue
.
Example:
$pq = new \Ds\PriorityQueue();
$pq->push("One", 1);
$pq->push("Two", 2);
$pq->push("Three", 3);
$reversed = array_reverse($pq->toArray());
print_r($reversed);
// Output: Array ( [0] => One [1] => Two [2] => Three )
maxPriority()
Description: Find the maximum priority present in the PriorityQueue
.
Example:
$deque = new \Ds\Deque([1, 2, 3]);
echo $deque->shift();
// Output: 1
print_r($deque);
// Output: Ds\Deque Object ([0] => 2 [1] => 3)
slice()
Description: Return a sub-Deque
containing elements within the specified index range.
Example:
$deque = new \Ds\Deque([1, 2, 3, 4, 5]);
$sliced = $deque->slice(1, 3);
print_r($sliced);
// Output: Ds\Deque Object ([0] => 2 [1] => 3 [2] => 4)
sort()
Description: Sort the Deque
in place by arranging elements in increasing order.
Example:
$deque = new \Ds\Deque([3, 1, 2]);
$deque->sort();
print_r($deque);
// Output: Ds\Deque Object ([0] => 1 [1] => 2 [2] => 3)
sorted()
Description: Return a copy of the Deque
with elements sorted in ascending order.
Example:
$deque = new \Ds\Deque([3, 1, 2]);
$sorted = $deque->sorted();
print_r($sorted);
// Output: Ds\Deque Object ([0] => 1 [1] => 2 [2] => 3)
sum()
Description: Return the sum of all elements in the Deque
.
Example:
$deque = new \Ds\Deque([1, 2, 3]);
echo $deque->sum();
// Output: 6
toArray()
Description: Convert the Deque
to an array.
Example:
$deque = new \Ds\Deque([1, 2, 3]);
$array = $deque->toArray();
print_r($array);
// Output: Array ([0] => 1 [1] => 2 [2] => 3)
unshift()
Description: Add a value to the front of the Deque
.
Example:
$deque = new \Ds\Deque([2, 3]);
$deque->unshift(1);
print_r($deque);
// Output: Ds\Deque Object ([0] => 1 [1] => 2 [2] => 3)
filter()
Description: Filter out the elements from the Deque
based on the condition defined in the callback function.
Example:
find()
Description: Find the index of the specified element in the Deque
. Returns false
if not found.
Example:
$deque = new \Ds\Deque([1, 2, 3, 4, 5]);
$filtered = $deque->filter(fn($value) => $value % 2 == 0);
print_r($filtered);
// Output: Ds\Deque Object ([0] => 2 [1] => 4)
first()
Description: Return the first element in the Deque
.
Example:
$deque = new \Ds\Deque([1, 2, 3, 4]);
echo $deque->find(3);
// Output: 2
first()
Description: Return the first element in the Deque
.
Example:
$deque = new \Ds\Deque([1, 2, 3]);
echo $deque->first();
// Output: 1
$deque = new \Ds\Deque([1, 2, 3]);
echo $deque->first();
// Output: 1