1 | <?php |
||
26 | trait PriorityQueueTrait |
||
27 | { |
||
28 | /** |
||
29 | * inner data storage |
||
30 | * |
||
31 | * @var array |
||
32 | * @access protected |
||
33 | */ |
||
34 | protected $queue = []; |
||
35 | |||
36 | /** |
||
37 | * marker for sorted queue |
||
38 | * |
||
39 | * @var bool |
||
40 | * @access protected |
||
41 | */ |
||
42 | protected $sorted = false; |
||
43 | |||
44 | /** |
||
45 | * priority counter, descreasing |
||
46 | * |
||
47 | * @var int |
||
48 | * @access protected |
||
49 | */ |
||
50 | protected $counter = 10000000; |
||
51 | |||
52 | /** |
||
53 | * {@inheritDoc} |
||
54 | */ |
||
55 | public function insert($data, /*# int */ $priority = 0) |
||
74 | |||
75 | /** |
||
76 | * {@inheritDoc} |
||
77 | */ |
||
78 | public function remove($data) |
||
88 | |||
89 | /** |
||
90 | * {@inheritdic} |
||
91 | */ |
||
92 | public function flush() |
||
97 | |||
98 | /** |
||
99 | * {@inheritDoc} |
||
100 | */ |
||
101 | public function combine( |
||
114 | |||
115 | /** |
||
116 | * {@inheritDoc} |
||
117 | */ |
||
118 | public function count() |
||
122 | |||
123 | /** |
||
124 | * {@inheritDoc} |
||
125 | */ |
||
126 | public function getIterator() |
||
134 | |||
135 | /** |
||
136 | * Make sure priority in the range of -100 - +100 |
||
137 | * |
||
138 | * @param int $priority |
||
139 | * @return int |
||
140 | * @access protected |
||
141 | */ |
||
142 | protected function fixPriority(/*# int */ $priority)/*# : int */ |
||
146 | |||
147 | /** |
||
148 | * Generate one int base on the priority |
||
149 | * |
||
150 | * @param int $priority |
||
151 | * @return int |
||
152 | * @access protected |
||
153 | */ |
||
154 | protected function generateKey(/*# int */ $priority)/*# : int */ |
||
158 | |||
159 | /** |
||
160 | * Sort the queue from higher to lower int $key |
||
161 | * |
||
162 | * @return $this |
||
163 | * @access protected |
||
164 | */ |
||
165 | protected function sortQueue() |
||
173 | } |
||
174 | |||
175 |