Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@0
|
3 * Zend Framework (http://framework.zend.com/)
|
Chris@0
|
4 *
|
Chris@0
|
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
|
Chris@0
|
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
Chris@0
|
7 * @license http://framework.zend.com/license/new-bsd New BSD License
|
Chris@0
|
8 */
|
Chris@0
|
9
|
Chris@0
|
10 namespace Zend\Stdlib;
|
Chris@0
|
11
|
Chris@0
|
12 use Iterator;
|
Chris@0
|
13 use Countable;
|
Chris@0
|
14 use Serializable;
|
Chris@0
|
15 use SplPriorityQueue as PhpSplPriorityQueue;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * This is an efficient implementation of an integer priority queue in PHP
|
Chris@0
|
19 *
|
Chris@0
|
20 * This class acts like a queue with insert() and extract(), removing the
|
Chris@0
|
21 * elements from the queue and it also acts like an Iterator without removing
|
Chris@0
|
22 * the elements. This behaviour can be used in mixed scenarios with high
|
Chris@0
|
23 * performance boost.
|
Chris@0
|
24 */
|
Chris@0
|
25 class FastPriorityQueue implements Iterator, Countable, Serializable
|
Chris@0
|
26 {
|
Chris@0
|
27 const EXTR_DATA = PhpSplPriorityQueue::EXTR_DATA;
|
Chris@0
|
28 const EXTR_PRIORITY = PhpSplPriorityQueue::EXTR_PRIORITY;
|
Chris@0
|
29 const EXTR_BOTH = PhpSplPriorityQueue::EXTR_BOTH;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * @var integer
|
Chris@0
|
33 */
|
Chris@0
|
34 protected $extractFlag = self::EXTR_DATA;
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * Elements of the queue, divided by priorities
|
Chris@0
|
38 *
|
Chris@0
|
39 * @var array
|
Chris@0
|
40 */
|
Chris@0
|
41 protected $values = [];
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * Array of priorities
|
Chris@0
|
45 *
|
Chris@0
|
46 * @var array
|
Chris@0
|
47 */
|
Chris@0
|
48 protected $priorities = [];
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * Array of priorities used for the iteration
|
Chris@0
|
52 *
|
Chris@0
|
53 * @var array
|
Chris@0
|
54 */
|
Chris@0
|
55 protected $subPriorities = [];
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * Max priority
|
Chris@0
|
59 *
|
Chris@0
|
60 * @var integer
|
Chris@0
|
61 */
|
Chris@0
|
62 protected $maxPriority = 0;
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * Total number of elements in the queue
|
Chris@0
|
66 *
|
Chris@0
|
67 * @var integer
|
Chris@0
|
68 */
|
Chris@0
|
69 protected $count = 0;
|
Chris@0
|
70
|
Chris@0
|
71 /**
|
Chris@0
|
72 * Index of the current element in the queue
|
Chris@0
|
73 *
|
Chris@0
|
74 * @var integer
|
Chris@0
|
75 */
|
Chris@0
|
76 protected $index = 0;
|
Chris@0
|
77
|
Chris@0
|
78 /**
|
Chris@0
|
79 * Sub index of the current element in the same priority level
|
Chris@0
|
80 *
|
Chris@0
|
81 * @var integer
|
Chris@0
|
82 */
|
Chris@0
|
83 protected $subIndex = 0;
|
Chris@0
|
84
|
Chris@0
|
85 /**
|
Chris@0
|
86 * Insert an element in the queue with a specified priority
|
Chris@0
|
87 *
|
Chris@0
|
88 * @param mixed $value
|
Chris@0
|
89 * @param integer $priority a positive integer
|
Chris@0
|
90 */
|
Chris@0
|
91 public function insert($value, $priority)
|
Chris@0
|
92 {
|
Chris@0
|
93 if (! is_int($priority)) {
|
Chris@0
|
94 throw new Exception\InvalidArgumentException('The priority must be an integer');
|
Chris@0
|
95 }
|
Chris@0
|
96 $this->values[$priority][] = $value;
|
Chris@0
|
97 if (! isset($this->priorities[$priority])) {
|
Chris@0
|
98 $this->priorities[$priority] = $priority;
|
Chris@0
|
99 $this->maxPriority = max($priority, $this->maxPriority);
|
Chris@0
|
100 }
|
Chris@0
|
101 ++$this->count;
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 /**
|
Chris@0
|
105 * Extract an element in the queue according to the priority and the
|
Chris@0
|
106 * order of insertion
|
Chris@0
|
107 *
|
Chris@0
|
108 * @return mixed
|
Chris@0
|
109 */
|
Chris@0
|
110 public function extract()
|
Chris@0
|
111 {
|
Chris@0
|
112 if (! $this->valid()) {
|
Chris@0
|
113 return false;
|
Chris@0
|
114 }
|
Chris@0
|
115 $value = $this->current();
|
Chris@0
|
116 $this->nextAndRemove();
|
Chris@0
|
117 return $value;
|
Chris@0
|
118 }
|
Chris@0
|
119
|
Chris@0
|
120 /**
|
Chris@0
|
121 * Remove an item from the queue
|
Chris@0
|
122 *
|
Chris@0
|
123 * This is different than {@link extract()}; its purpose is to dequeue an
|
Chris@0
|
124 * item.
|
Chris@0
|
125 *
|
Chris@0
|
126 * Note: this removes the first item matching the provided item found. If
|
Chris@0
|
127 * the same item has been added multiple times, it will not remove other
|
Chris@0
|
128 * instances.
|
Chris@0
|
129 *
|
Chris@0
|
130 * @param mixed $datum
|
Chris@0
|
131 * @return bool False if the item was not found, true otherwise.
|
Chris@0
|
132 */
|
Chris@0
|
133 public function remove($datum)
|
Chris@0
|
134 {
|
Chris@0
|
135 $this->rewind();
|
Chris@0
|
136 while ($this->valid()) {
|
Chris@0
|
137 if (current($this->values[$this->maxPriority]) === $datum) {
|
Chris@0
|
138 $index = key($this->values[$this->maxPriority]);
|
Chris@0
|
139 unset($this->values[$this->maxPriority][$index]);
|
Chris@0
|
140 --$this->count;
|
Chris@0
|
141 return true;
|
Chris@0
|
142 }
|
Chris@0
|
143 $this->next();
|
Chris@0
|
144 }
|
Chris@0
|
145 return false;
|
Chris@0
|
146 }
|
Chris@0
|
147
|
Chris@0
|
148 /**
|
Chris@0
|
149 * Get the total number of elements in the queue
|
Chris@0
|
150 *
|
Chris@0
|
151 * @return integer
|
Chris@0
|
152 */
|
Chris@0
|
153 public function count()
|
Chris@0
|
154 {
|
Chris@0
|
155 return $this->count;
|
Chris@0
|
156 }
|
Chris@0
|
157
|
Chris@0
|
158 /**
|
Chris@0
|
159 * Get the current element in the queue
|
Chris@0
|
160 *
|
Chris@0
|
161 * @return mixed
|
Chris@0
|
162 */
|
Chris@0
|
163 public function current()
|
Chris@0
|
164 {
|
Chris@0
|
165 switch ($this->extractFlag) {
|
Chris@0
|
166 case self::EXTR_DATA:
|
Chris@0
|
167 return current($this->values[$this->maxPriority]);
|
Chris@0
|
168 case self::EXTR_PRIORITY:
|
Chris@0
|
169 return $this->maxPriority;
|
Chris@0
|
170 case self::EXTR_BOTH:
|
Chris@0
|
171 return [
|
Chris@0
|
172 'data' => current($this->values[$this->maxPriority]),
|
Chris@0
|
173 'priority' => $this->maxPriority
|
Chris@0
|
174 ];
|
Chris@0
|
175 }
|
Chris@0
|
176 }
|
Chris@0
|
177
|
Chris@0
|
178 /**
|
Chris@0
|
179 * Get the index of the current element in the queue
|
Chris@0
|
180 *
|
Chris@0
|
181 * @return integer
|
Chris@0
|
182 */
|
Chris@0
|
183 public function key()
|
Chris@0
|
184 {
|
Chris@0
|
185 return $this->index;
|
Chris@0
|
186 }
|
Chris@0
|
187
|
Chris@0
|
188 /**
|
Chris@0
|
189 * Set the iterator pointer to the next element in the queue
|
Chris@0
|
190 * removing the previous element
|
Chris@0
|
191 */
|
Chris@0
|
192 protected function nextAndRemove()
|
Chris@0
|
193 {
|
Chris@0
|
194 if (false === next($this->values[$this->maxPriority])) {
|
Chris@0
|
195 unset($this->priorities[$this->maxPriority]);
|
Chris@0
|
196 unset($this->values[$this->maxPriority]);
|
Chris@0
|
197 $this->maxPriority = empty($this->priorities) ? 0 : max($this->priorities);
|
Chris@0
|
198 $this->subIndex = -1;
|
Chris@0
|
199 }
|
Chris@0
|
200 ++$this->index;
|
Chris@0
|
201 ++$this->subIndex;
|
Chris@0
|
202 --$this->count;
|
Chris@0
|
203 }
|
Chris@0
|
204
|
Chris@0
|
205 /**
|
Chris@0
|
206 * Set the iterator pointer to the next element in the queue
|
Chris@0
|
207 * without removing the previous element
|
Chris@0
|
208 */
|
Chris@0
|
209 public function next()
|
Chris@0
|
210 {
|
Chris@0
|
211 if (false === next($this->values[$this->maxPriority])) {
|
Chris@0
|
212 unset($this->subPriorities[$this->maxPriority]);
|
Chris@0
|
213 reset($this->values[$this->maxPriority]);
|
Chris@0
|
214 $this->maxPriority = empty($this->subPriorities) ? 0 : max($this->subPriorities);
|
Chris@0
|
215 $this->subIndex = -1;
|
Chris@0
|
216 }
|
Chris@0
|
217 ++$this->index;
|
Chris@0
|
218 ++$this->subIndex;
|
Chris@0
|
219 }
|
Chris@0
|
220
|
Chris@0
|
221 /**
|
Chris@0
|
222 * Check if the current iterator is valid
|
Chris@0
|
223 *
|
Chris@0
|
224 * @return boolean
|
Chris@0
|
225 */
|
Chris@0
|
226 public function valid()
|
Chris@0
|
227 {
|
Chris@0
|
228 return isset($this->values[$this->maxPriority]);
|
Chris@0
|
229 }
|
Chris@0
|
230
|
Chris@0
|
231 /**
|
Chris@0
|
232 * Rewind the current iterator
|
Chris@0
|
233 */
|
Chris@0
|
234 public function rewind()
|
Chris@0
|
235 {
|
Chris@0
|
236 $this->subPriorities = $this->priorities;
|
Chris@0
|
237 $this->maxPriority = empty($this->priorities) ? 0 : max($this->priorities);
|
Chris@0
|
238 $this->index = 0;
|
Chris@0
|
239 $this->subIndex = 0;
|
Chris@0
|
240 }
|
Chris@0
|
241
|
Chris@0
|
242 /**
|
Chris@0
|
243 * Serialize to an array
|
Chris@0
|
244 *
|
Chris@0
|
245 * Array will be priority => data pairs
|
Chris@0
|
246 *
|
Chris@0
|
247 * @return array
|
Chris@0
|
248 */
|
Chris@0
|
249 public function toArray()
|
Chris@0
|
250 {
|
Chris@0
|
251 $array = [];
|
Chris@0
|
252 foreach (clone $this as $item) {
|
Chris@0
|
253 $array[] = $item;
|
Chris@0
|
254 }
|
Chris@0
|
255 return $array;
|
Chris@0
|
256 }
|
Chris@0
|
257
|
Chris@0
|
258 /**
|
Chris@0
|
259 * Serialize
|
Chris@0
|
260 *
|
Chris@0
|
261 * @return string
|
Chris@0
|
262 */
|
Chris@0
|
263 public function serialize()
|
Chris@0
|
264 {
|
Chris@0
|
265 $clone = clone $this;
|
Chris@0
|
266 $clone->setExtractFlags(self::EXTR_BOTH);
|
Chris@0
|
267
|
Chris@0
|
268 $data = [];
|
Chris@0
|
269 foreach ($clone as $item) {
|
Chris@0
|
270 $data[] = $item;
|
Chris@0
|
271 }
|
Chris@0
|
272
|
Chris@0
|
273 return serialize($data);
|
Chris@0
|
274 }
|
Chris@0
|
275
|
Chris@0
|
276 /**
|
Chris@0
|
277 * Deserialize
|
Chris@0
|
278 *
|
Chris@0
|
279 * @param string $data
|
Chris@0
|
280 * @return void
|
Chris@0
|
281 */
|
Chris@0
|
282 public function unserialize($data)
|
Chris@0
|
283 {
|
Chris@0
|
284 foreach (unserialize($data) as $item) {
|
Chris@0
|
285 $this->insert($item['data'], $item['priority']);
|
Chris@0
|
286 }
|
Chris@0
|
287 }
|
Chris@0
|
288
|
Chris@0
|
289 /**
|
Chris@0
|
290 * Set the extract flag
|
Chris@0
|
291 *
|
Chris@0
|
292 * @param integer $flag
|
Chris@0
|
293 */
|
Chris@0
|
294 public function setExtractFlags($flag)
|
Chris@0
|
295 {
|
Chris@0
|
296 switch ($flag) {
|
Chris@0
|
297 case self::EXTR_DATA:
|
Chris@0
|
298 case self::EXTR_PRIORITY:
|
Chris@0
|
299 case self::EXTR_BOTH:
|
Chris@0
|
300 $this->extractFlag = $flag;
|
Chris@0
|
301 break;
|
Chris@0
|
302 default:
|
Chris@0
|
303 throw new Exception\InvalidArgumentException("The extract flag specified is not valid");
|
Chris@0
|
304 }
|
Chris@0
|
305 }
|
Chris@0
|
306
|
Chris@0
|
307 /**
|
Chris@0
|
308 * Check if the queue is empty
|
Chris@0
|
309 *
|
Chris@0
|
310 * @return boolean
|
Chris@0
|
311 */
|
Chris@0
|
312 public function isEmpty()
|
Chris@0
|
313 {
|
Chris@0
|
314 return empty($this->values);
|
Chris@0
|
315 }
|
Chris@0
|
316
|
Chris@0
|
317 /**
|
Chris@0
|
318 * Does the queue contain the given datum?
|
Chris@0
|
319 *
|
Chris@0
|
320 * @param mixed $datum
|
Chris@0
|
321 * @return bool
|
Chris@0
|
322 */
|
Chris@0
|
323 public function contains($datum)
|
Chris@0
|
324 {
|
Chris@0
|
325 foreach ($this->values as $values) {
|
Chris@0
|
326 if (in_array($datum, $values)) {
|
Chris@0
|
327 return true;
|
Chris@0
|
328 }
|
Chris@0
|
329 }
|
Chris@0
|
330 return false;
|
Chris@0
|
331 }
|
Chris@0
|
332
|
Chris@0
|
333 /**
|
Chris@0
|
334 * Does the queue have an item with the given priority?
|
Chris@0
|
335 *
|
Chris@0
|
336 * @param int $priority
|
Chris@0
|
337 * @return bool
|
Chris@0
|
338 */
|
Chris@0
|
339 public function hasPriority($priority)
|
Chris@0
|
340 {
|
Chris@0
|
341 return isset($this->values[$priority]);
|
Chris@0
|
342 }
|
Chris@0
|
343 }
|