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; |
||
65 | |||
66 | /** |
||
67 | * Initializes a new priority queue. |
||
68 | */ |
||
69 | public function __construct() |
||
73 | |||
74 | /** |
||
75 | * @inheritDoc |
||
76 | */ |
||
77 | public function clear() |
||
81 | |||
82 | /** |
||
83 | * Reset the PriorityQueue to an initialised state. |
||
84 | */ |
||
85 | private function reset() |
||
91 | |||
92 | /** |
||
93 | * @inheritDoc |
||
94 | */ |
||
95 | public function copy() |
||
104 | |||
105 | /** |
||
106 | * @inheritDoc |
||
107 | */ |
||
108 | public function count(): int |
||
112 | |||
113 | /** |
||
114 | * Returns the value with the highest priority in the priority queue. |
||
115 | * |
||
116 | * @return mixed |
||
117 | * |
||
118 | * @throw UnderflowException |
||
119 | */ |
||
120 | public function peek() |
||
128 | |||
129 | /** |
||
130 | * Left |
||
131 | * |
||
132 | * @param int $index |
||
133 | * |
||
134 | * @return int |
||
135 | */ |
||
136 | private function left(int $index): int |
||
140 | |||
141 | /** |
||
142 | * Right |
||
143 | * |
||
144 | * @param int $index |
||
145 | * |
||
146 | * @return int |
||
147 | */ |
||
148 | private function right(int $index): int |
||
152 | |||
153 | /** |
||
154 | * Parent |
||
155 | * |
||
156 | * @param int $index |
||
157 | * |
||
158 | * @return int |
||
159 | */ |
||
160 | private function parent(int $index): int |
||
164 | |||
165 | /** |
||
166 | * Compare |
||
167 | * |
||
168 | * @param int $a |
||
169 | * @param int $b |
||
170 | * |
||
171 | * @return integer |
||
172 | */ |
||
173 | private function compare(int $a, int $b) |
||
181 | |||
182 | /** |
||
183 | * Swap |
||
184 | * |
||
185 | * @param int $a |
||
186 | * @param int $b |
||
187 | */ |
||
188 | private function swap(int $a, int $b) |
||
194 | |||
195 | /** |
||
196 | * Get Largest Leaf |
||
197 | * |
||
198 | * @param int $parent |
||
199 | * |
||
200 | * @return int |
||
201 | */ |
||
202 | private function getLargestLeaf(int $parent) |
||
213 | |||
214 | /** |
||
215 | * Sift Down |
||
216 | * |
||
217 | * @param int $node |
||
218 | */ |
||
219 | private function siftDown(int $node) |
||
236 | |||
237 | /** |
||
238 | * Set Root |
||
239 | * |
||
240 | * @param PriorityNode $node |
||
241 | */ |
||
242 | private function setRoot(PriorityNode $node) |
||
246 | |||
247 | /** |
||
248 | * Get Root |
||
249 | * |
||
250 | * @return PriorityNode |
||
251 | */ |
||
252 | private function getRoot(): PriorityNode |
||
256 | |||
257 | /** |
||
258 | * Returns and removes the value with the highest priority in the queue. |
||
259 | * |
||
260 | * @return mixed |
||
261 | */ |
||
262 | public function pop() |
||
285 | |||
286 | /** |
||
287 | * Sift Up |
||
288 | * |
||
289 | * @param int $leaf |
||
290 | */ |
||
291 | private function siftUp(int $leaf) |
||
304 | |||
305 | /** |
||
306 | * Pushes a value into the queue, with a specified priority. |
||
307 | * |
||
308 | * @param mixed $value |
||
309 | * @param int $priority |
||
310 | */ |
||
311 | public function push($value, int $priority) |
||
319 | |||
320 | /** |
||
321 | * @inheritDoc |
||
322 | */ |
||
323 | public function toArray(): array |
||
335 | |||
336 | /** |
||
337 | * Get iterator |
||
338 | */ |
||
339 | public function getIterator() |
||
345 | } |
||
346 |
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.