Contents
Ds\PriorityQueue Functions
Basic Ds\PriorityQueue Functions
allocate()
Description: Allocate memory for a PriorityQueue
class instance.
Example:
$pq = new \Ds\PriorityQueue();
$pq->allocate(10);
echo $pq->capacity();
// Output: 10
capacity()
Description: Check the current capacity of a PriorityQueue
instance.
Example:
$pq = new \Ds\PriorityQueue();
echo $pq->capacity();
// Output: 10 (default or allocated capacity)
clear()
Description: Clear all of the elements from a PriorityQueue
instance.
Example:
$pq = new \Ds\PriorityQueue();
$pq->push("One", 1);
$pq->push("Two", 2);
$pq->clear();
echo $pq->count();
// Output: 0
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 ()
copy()
Description: Create a shallow copy of a particular PriorityQueue
instance.
Example:
$pq = new \Ds\PriorityQueue();
$pq->push("One", 1);
$pq->push("Two", 2);
$copy = $pq->copy();
print_r($copy->toArray());
// Output: Array ( [0] => Two [1] => One ) (based on priority)
count()
Description: Get the count of elements present in a PriorityQueue
instance.
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:
$pq = new \Ds\PriorityQueue();
$pq->push("One", 1);
$pq->push("Two", 2);
echo $pq->count();
// Output: 2
peek()
Description: Get the value present at the front of a PriorityQueue
.
Example:
$pq = new \Ds\PriorityQueue();
$pq->push("One", 1);
$pq->push("Two", 2);
echo $pq->peek();
// Output: Two (highest priority)
pop()
Description: Remove and return the value present at the top of the PriorityQueue
.
Example:
$pq = new \Ds\PriorityQueue();
$pq->push("One", 1);
$pq->push("Two", 2);
echo $pq->pop();
// Output: Two
push()
Description: Push or insert values in a PriorityQueue
instance with a given priority.
Example:
$pq = new \Ds\PriorityQueue();
$pq->push("One", 1);
$pq->push("Two", 2);
print_r($pq->toArray());
// Output: Array ( [0] => Two [1] => One ) (based on priority)
toArray()
Description: Convert a PriorityQueue
into an associative array.
Example:
$pq = new \Ds\PriorityQueue();
$pq->push("One", 1);
$pq->push("Two", 2);
print_r($pq->toArray());
// Output: Array ( [0] => Two [1] => One )
union()
Description: Create a new PriorityQueue
that combines elements from two PriorityQueue
instances.
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);
$union = new \Ds\PriorityQueue();
foreach ($pq1 as $item) {
$union->push($item[0], $item[1]);
}
foreach ($pq2 as $item) {
$union->push($item[0], $item[1]);
}
print_r($union->toArray());
// Output: Array ( [0] => Four [1] => Three [2] => Two [3] => One )
intersect()
Description: Create a new PriorityQueue
containing only the common elements of two PriorityQueue
instances.
Example:
$pq1 = new \Ds\PriorityQueue();
$pq1->push("One", 1);
$pq1->push("Two", 2);
$pq2 = new \Ds\PriorityQueue();
$pq2->push("Two", 2);
$pq2->push("Three", 3);
$intersection = [];
foreach ($pq1 as $item1) {
foreach ($pq2 as $item2) {
if ($item1[0] === $item2[0] && $item1[1] === $item2[1]) {
$intersection[] = $item1;
}
}
}
print_r($intersection);
// Output: Array ( [0] => Array ( [0] => Two [1] => 2 ) )
filter()
Description: Filter elements of a PriorityQueue
based on a callback function.
Example:
$pq = new \Ds\PriorityQueue();
$pq->push("One", 1);
$pq->push("Two", 2);
$pq->push("Three", 3);
$filtered = array_filter($pq->toArray(), function ($item) {
return $item[1] > 1; // Keep elements with priority > 1
});
print_r($filtered);
// Output: Array ( [0] => Two [1] => Three )
reduce()
Description: Reduce the elements of the PriorityQueue
to a single value using a callback function.
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:
$pq = new \Ds\PriorityQueue();
$pq->push("One", 1);
$pq->push("Two", 2);
$pq->push("Three", 3);
$maxPriority = max(array_column($pq->toArray(), 1));
echo $maxPriority;
// Output: 3
minPriority()
Description: Find the minimum priority present in the PriorityQueue
.
Example:
$pq = new \Ds\PriorityQueue();
$pq->push("One", 1);
$pq->push("Two", 2);
$pq->push("Three", 3);
$minPriority = min(array_column($pq->toArray(), 1));
echo $minPriority;
// Output: 1
extractWithPriority()
Description: Extract all elements with a specific priority.
Example:
$pq = new \Ds\PriorityQueue();
$pq->push("One", 1);
$pq->push("Two", 2);
$pq->push("Another One", 1);
$priorityToExtract = 1;
$extracted = array_filter($pq->toArray(), function ($item) use ($priorityToExtract) {
return $item[1] === $priorityToExtract;
});
print_r($extracted);
// Output: Array ( [0] => One [1] => Another One )
priorities()
Description: Get a list of all unique priorities in the PriorityQueue
.
Example:
$pq = new \Ds\PriorityQueue();
$pq->push("One", 1);
$pq->push("Two", 2);
$pq->push("Three", 3);
$pq->push("Another One", 1);
$uniquePriorities = array_unique(array_column($pq->toArray(), 1));
print_r($uniquePriorities);
// Output: Array ( [0] => 1 [1] => 2 [2] => 3 )
mergeWithPriorityOverride()
Description: Merge two PriorityQueue
instances, overriding the priority of duplicate elements.
Example:
$pq1 = new \Ds\PriorityQueue();
$pq1->push("One", 1);
$pq1->push("Two", 2);
$pq2 = new \Ds\PriorityQueue();
$pq2->push("Two", 5);
$pq2->push("Three", 3);
$merged = [];
foreach ($pq1->toArray() as $item) {
$merged[$item[0]] = $item[1];
}
foreach ($pq2->toArray() as $item) {
$merged[$item[0]] = $item[1]; // Override priority if the key exists
}
$result = new \Ds\PriorityQueue();
foreach ($merged as $value => $priority) {
$result->push($value, $priority);
}
print_r($result->toArray());
// Output: Array ( [0] => Two [1] => Three [2] => One )
filterByPriorityRange()
Description: Filter elements with priorities within a given range.
Example:
$pq = new \Ds\PriorityQueue();
$pq->push("One", 1);
$pq->push("Two", 2);
$pq->push("Three", 3);
$rangeStart = 1;
$rangeEnd = 2;
$filtered = array_filter($pq->toArray(), function ($item) use ($rangeStart, $rangeEnd) {
return $item[1] >= $rangeStart && $item[1] <= $rangeEnd;
});
print_r($filtered);
// Output: Array ( [0] => One [1] => Two )
topN()
Description: Retrieve the top N elements based on priority.
Example:
$pq = new \Ds\PriorityQueue();
$pq->push("One", 1);
$pq->push("Two", 2);
$pq->push("Three", 3);
$topN = 2;
$sorted = $pq->toArray();
usort($sorted, function ($a, $b) {
return $b[1] - $a[1];
});
print_r(array_slice($sorted, 0, $topN));
// Output: Array ( [0] => Three [1] => Two )