96 implements \Iterator, \Countable {
103 private $elements = array();
112 foreach ($elements as
$priority => $element) {
141 $this->sorted =
false;
155 public function remove($element, $strict =
false) {
156 $index = array_search($element, $this->elements, $strict);
157 if ($index !==
false) {
158 unset($this->elements[$index]);
173 public function move($element, $new_priority, $strict =
false) {
174 $new_priority = (int) $new_priority;
176 $current_priority = $this->
getPriority($element, $strict);
177 if ($current_priority ===
false) {
181 if ($current_priority == $new_priority) {
186 $element = $this->
getElement($current_priority);
187 unset($this->elements[$current_priority]);
188 return $this->
add($element, $new_priority);
197 $this->sortIfUnsorted();
198 return $this->elements;
214 public function sort($callback = null) {
216 ksort($this->elements, SORT_NUMERIC);
218 $sorted = call_user_func($callback, $this->elements);
224 $this->elements = $sorted;
227 $this->sorted =
true;
236 private function sortIfUnsorted() {
237 if (!$this->sorted) {
238 return $this->
sort();
251 while (array_key_exists($near, $this->elements)) {
268 return array_search($element, $this->elements, $strict);
288 public function contains($element, $strict =
false) {
289 return $this->
getPriority($element, $strict) !==
false;
308 $this->sortIfUnsorted();
309 return reset($this->elements);
319 $this->sortIfUnsorted();
320 return current($this->elements);
330 $this->sortIfUnsorted();
331 return key($this->elements);
341 $this->sortIfUnsorted();
342 return next($this->elements);
352 $this->sortIfUnsorted();
354 return (
$key !== null &&
$key !==
false);
364 return count($this->elements);
getElement($priority)
Returns the element at $priority.
add($element, $priority=null, $exact=false)
Adds an element to the list.
contains($element, $strict=false)
Returns if the list contains $element.
getNextPriority($near=0)
Returns the next priority available.
getElements()
Returns the elements.
key()
PHP Iterator Interface.
count()
Countable interface.
move($element, $new_priority, $strict=false)
Move an existing element to a new priority.
current()
PHP Iterator Interface.
next()
PHP Iterator Interface.
sort($callback=null)
Sort the elements optionally by a callback function.
__construct(array $elements=array())
Create a new priority list.
getPriority($element, $strict=false)
Returns the priority of an element if it exists in the list.
valid()
PHP Iterator Interface.