1 | <?php |
||
46 | final class PriorityQueue implements \IteratorAggregate, Collection |
||
|
|||
47 | { |
||
48 | use Traits\Collection; |
||
49 | use Traits\SquaredCapacity; |
||
50 | |||
51 | /** |
||
52 | * @var int |
||
53 | */ |
||
54 | const MIN_CAPACITY = 8; |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | private $heap = []; |
||
60 | |||
61 | /** |
||
62 | * @var int |
||
63 | */ |
||
64 | private $stamp = 0; |
||
65 | |||
66 | /** |
||
67 | * Initializes a new priority queue. |
||
68 | */ |
||
69 | 63 | public function __construct() |
|
73 | |||
74 | /** |
||
75 | * @inheritDoc |
||
76 | */ |
||
77 | 2 | public function clear() |
|
83 | |||
84 | /** |
||
85 | * @inheritDoc |
||
86 | */ |
||
87 | 5 | public function copy(): \Ds\Collection |
|
96 | |||
97 | /** |
||
98 | * @inheritDoc |
||
99 | */ |
||
100 | 53 | public function count(): int |
|
104 | |||
105 | /** |
||
106 | * Returns the value with the highest priority in the priority queue. |
||
107 | * |
||
108 | * @return mixed |
||
109 | * |
||
110 | * @throw UnderflowException |
||
111 | */ |
||
112 | 4 | public function peek() |
|
113 | { |
||
114 | 4 | if ($this->isEmpty()) { |
|
115 | 1 | throw new UnderflowException(); |
|
116 | } |
||
117 | |||
118 | 3 | return $this->heap[0]->value; |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * Left |
||
123 | * |
||
124 | * @param int $index |
||
125 | * |
||
126 | * @return int |
||
127 | */ |
||
128 | 10 | private function left(int $index): int |
|
132 | |||
133 | /** |
||
134 | * Right |
||
135 | * |
||
136 | * @param int $index |
||
137 | * |
||
138 | * @return int |
||
139 | */ |
||
140 | 10 | private function right(int $index): int |
|
141 | { |
||
142 | 10 | return ($index * 2) + 2; |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * Parent |
||
147 | * |
||
148 | * @param int $index |
||
149 | * |
||
150 | * @return int |
||
151 | */ |
||
152 | 36 | private function parent(int $index): int |
|
156 | |||
157 | /** |
||
158 | * Compare |
||
159 | * |
||
160 | * @param int $a |
||
161 | * @param int $b |
||
162 | * |
||
163 | * @return integer |
||
164 | */ |
||
165 | 36 | private function compare(int $a, int $b) |
|
173 | |||
174 | /** |
||
175 | * Swap |
||
176 | * |
||
177 | * @param int $a |
||
178 | * @param int $b |
||
179 | */ |
||
180 | 23 | private function swap(int $a, int $b) |
|
186 | |||
187 | /** |
||
188 | * Get Largest Leaf |
||
189 | * |
||
190 | * @param int $parent |
||
191 | * |
||
192 | * @return int |
||
193 | */ |
||
194 | 10 | private function getLargestLeaf(int $parent) |
|
205 | |||
206 | /** |
||
207 | * Sift Down |
||
208 | * |
||
209 | * @param int $node |
||
210 | */ |
||
211 | 29 | private function siftDown(int $node) |
|
228 | |||
229 | /** |
||
230 | * Set Root |
||
231 | * |
||
232 | * @param PriorityNode $node |
||
233 | */ |
||
234 | 29 | private function setRoot(PriorityNode $node) |
|
238 | |||
239 | /** |
||
240 | * Get Root |
||
241 | * |
||
242 | * @return PriorityNode |
||
243 | */ |
||
244 | 29 | private function getRoot(): PriorityNode |
|
248 | |||
249 | /** |
||
250 | * Returns and removes the value with the highest priority in the queue. |
||
251 | * |
||
252 | * @return mixed |
||
253 | */ |
||
254 | 38 | public function pop() |
|
277 | |||
278 | /** |
||
279 | * Sift Up |
||
280 | * |
||
281 | * @param int $leaf |
||
282 | */ |
||
283 | 44 | private function siftUp(int $leaf) |
|
296 | |||
297 | /** |
||
298 | * Pushes a value into the queue, with a specified priority. |
||
299 | * |
||
300 | * @param mixed $value |
||
301 | * @param int $priority |
||
302 | */ |
||
303 | 44 | public function push($value, int $priority) |
|
311 | |||
312 | /** |
||
313 | * @inheritDoc |
||
314 | */ |
||
315 | 34 | public function toArray(): array |
|
327 | |||
328 | /** |
||
329 | * Get iterator |
||
330 | */ |
||
331 | 7 | public function getIterator() |
|
337 | } |
||
338 |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.