1 | <?php |
||
13 | final class PriorityQueue implements \IteratorAggregate, Collection |
||
14 | { |
||
15 | use Traits\GenericCollection; |
||
16 | use Traits\SquaredCapacity; |
||
17 | |||
18 | /** |
||
19 | * @var int |
||
20 | */ |
||
21 | const MIN_CAPACITY = 8; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | private $heap = []; |
||
27 | |||
28 | /** |
||
29 | * @var int |
||
30 | */ |
||
31 | private $stamp = 0; |
||
32 | |||
33 | /** |
||
34 | * Creates a new instance. |
||
35 | */ |
||
36 | public function __construct() |
||
39 | |||
40 | /** |
||
41 | * @inheritDoc |
||
42 | */ |
||
43 | public function clear() |
||
49 | |||
50 | /** |
||
51 | * @inheritDoc |
||
52 | */ |
||
53 | public function copy(): \Ds\Collection |
||
62 | |||
63 | /** |
||
64 | * @inheritDoc |
||
65 | */ |
||
66 | public function count(): int |
||
70 | |||
71 | /** |
||
72 | * Returns the value with the highest priority in the priority queue. |
||
73 | * |
||
74 | * @return mixed |
||
75 | * |
||
76 | * @throw UnderflowException |
||
77 | */ |
||
78 | public function peek() |
||
86 | |||
87 | /** |
||
88 | * Returns the index of a node's left leaf. |
||
89 | * |
||
90 | * @param int $index The index of the node. |
||
91 | * |
||
92 | * @return int The index of the left leaf. |
||
93 | */ |
||
94 | private function left(int $index): int |
||
98 | |||
99 | /** |
||
100 | * Returns the index of a node's right leaf. |
||
101 | * |
||
102 | * @param int $index The index of the node. |
||
103 | * |
||
104 | * @return int The index of the right leaf. |
||
105 | */ |
||
106 | private function right(int $index): int |
||
110 | |||
111 | /** |
||
112 | * Returns the index of a node's parent node. |
||
113 | * |
||
114 | * @param int $index The index of the node. |
||
115 | * |
||
116 | * @return int The index of the parent. |
||
117 | */ |
||
118 | private function parent(int $index): int |
||
122 | |||
123 | /** |
||
124 | * Compares two indices of the heap. |
||
125 | * |
||
126 | * @param int $a |
||
127 | * @param int $b |
||
128 | * |
||
129 | * @return int |
||
130 | */ |
||
131 | private function compare(int $a, int $b) |
||
139 | |||
140 | /** |
||
141 | * Swaps the nodes at two indices of the heap. |
||
142 | * |
||
143 | * @param int $a |
||
144 | * @param int $b |
||
145 | */ |
||
146 | private function swap(int $a, int $b) |
||
152 | |||
153 | /** |
||
154 | * Returns the index of a node's largest leaf node. |
||
155 | * |
||
156 | * @param int $parent the parent node. |
||
157 | * |
||
158 | * @return int the index of the node's largest leaf node. |
||
159 | */ |
||
160 | private function getLargestLeaf(int $parent) |
||
171 | |||
172 | /** |
||
173 | * Starts the process of sifting down a given node index to ensure that |
||
174 | * the heap's properties are preserved. |
||
175 | * |
||
176 | * @param int $node |
||
177 | */ |
||
178 | private function siftDown(int $node) |
||
195 | |||
196 | /** |
||
197 | * Sets the root node and sifts it down the heap. |
||
198 | * |
||
199 | * @param PriorityNode $node |
||
200 | */ |
||
201 | private function setRoot(PriorityNode $node) |
||
206 | |||
207 | /** |
||
208 | * Returns the root node of the heap. |
||
209 | * |
||
210 | * @return PriorityNode |
||
211 | */ |
||
212 | private function getRoot(): PriorityNode |
||
216 | |||
217 | /** |
||
218 | * Returns and removes the value with the highest priority in the queue. |
||
219 | * |
||
220 | * @return mixed |
||
221 | */ |
||
222 | public function pop() |
||
244 | |||
245 | /** |
||
246 | * Sifts a node up the heap until it's in the right position. |
||
247 | * |
||
248 | * @param int $leaf |
||
249 | */ |
||
250 | private function siftUp(int $leaf) |
||
263 | |||
264 | /** |
||
265 | * Pushes a value into the queue, with a specified priority. |
||
266 | * |
||
267 | * @param mixed $value |
||
268 | * @param int $priority |
||
269 | */ |
||
270 | public function push($value, int $priority) |
||
278 | |||
279 | /** |
||
280 | * @inheritDoc |
||
281 | */ |
||
282 | public function toArray(): array |
||
294 | |||
295 | /** |
||
296 | * @inheritDoc |
||
297 | */ |
||
298 | public function getIterator() |
||
304 | } |
||
305 | |||
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.