1 | <?php |
||
19 | class PriorityQueue implements \IteratorAggregate, \Countable |
||
20 | { |
||
21 | /** |
||
22 | * data storage |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $queue = []; |
||
27 | |||
28 | /** |
||
29 | * marker for sorted or not |
||
30 | * |
||
31 | * @var bool |
||
32 | */ |
||
33 | protected $sorted = TRUE; |
||
34 | |||
35 | /** |
||
36 | * counter for priority |
||
37 | * |
||
38 | * @var int |
||
39 | */ |
||
40 | protected $counter = 20000000; |
||
41 | |||
42 | /** |
||
43 | * Insert data into the queue with priority |
||
44 | * |
||
45 | * @param mixed $data |
||
46 | * @param int $priority priority, higher number retrieved first(-1000 - 1000) |
||
47 | * @return void |
||
48 | * @throws \RuntimeException if priority out of range |
||
49 | */ |
||
50 | public function insert($data, int $priority = 0): void |
||
56 | |||
57 | /** |
||
58 | * Combine with queue and return a combined new queue |
||
59 | * |
||
60 | * @param PriorityQueueInterface $queue |
||
61 | * @return PriorityQueueInterface |
||
62 | */ |
||
63 | public function combine(PriorityQueue $queue): PriorityQueue |
||
71 | |||
72 | /** |
||
73 | * {@inheritDoc} |
||
74 | */ |
||
75 | public function count() |
||
79 | |||
80 | /** |
||
81 | * {@inheritDoc} |
||
82 | */ |
||
83 | public function getIterator() |
||
88 | |||
89 | /** |
||
90 | * Generate an integer key |
||
91 | * |
||
92 | * @param int $priority |
||
93 | * @return int |
||
94 | * @throws \RuntimeException priority out of range |
||
95 | */ |
||
96 | protected function getIndex(int $priority): int |
||
103 | |||
104 | /** |
||
105 | * Sort the queue from higher to lower int $key |
||
106 | * |
||
107 | * @return $this |
||
108 | */ |
||
109 | protected function sortQueue() |
||
117 | } |