7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
16 * @package Zend_Search_Lucene
17 * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
23 * Abstract Priority Queue
25 * It implements a priority queue.
26 * Please go to "Data Structures and Algorithms",
27 * Aho, Hopcroft, and Ullman, Addison-Wesley, 1983 (corrected 1987 edition),
28 * for implementation details.
30 * It provides O(log(N)) time of put/pop operations, where N is a size of queue
33 * @package Zend_Search_Lucene
34 * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
35 * @license http://framework.zend.com/license/new-bsd New BSD License
37 abstract class Zend_Search_Lucene_PriorityQueue
42 * Heap contains balanced partial ordered binary tree represented in array
43 * [0] - top of the tree
44 * [1] - first child of [0]
45 * [2] - second child of [0]
47 * [2*n + 1] - first child of [n]
48 * [2*n + 2] - second child of [n]
52 private $_heap = array();
56 * Add element to the queue
60 * @param mixed $element
62 public function put($element)
64 $nodeId = count($this->_heap
);
65 $parentId = ($nodeId-1) >> 1; // floor( ($nodeId-1)/2 )
67 while ($nodeId != 0 && $this->_less($element, $this->_heap
[$parentId])) {
68 // Move parent node down
69 $this->_heap
[$nodeId] = $this->_heap
[$parentId];
71 // Move pointer to the next level of tree
73 $parentId = ($nodeId-1) >> 1; // floor( ($nodeId-1)/2 )
76 // Put new node into the tree
77 $this->_heap
[$nodeId] = $element;
82 * Return least element of the queue
90 if (count($this->_heap
) == 0) {
94 return $this->_heap
[0];
99 * Removes and return least element of the queue
105 public function pop()
107 if (count($this->_heap
) == 0) {
111 $top = $this->_heap
[0];
112 $lastId = count($this->_heap
) - 1;
115 * Find appropriate position for last node
117 $nodeId = 0; // Start from a top
118 $childId = 1; // First child
120 // Choose smaller child
121 if ($lastId > 2 && $this->_less($this->_heap
[2], $this->_heap
[1])) {
125 while ($childId < $lastId &&
126 $this->_less($this->_heap
[$childId], $this->_heap
[$lastId])
128 // Move child node up
129 $this->_heap
[$nodeId] = $this->_heap
[$childId];
131 $nodeId = $childId; // Go down
132 $childId = ($nodeId << 1) +
1; // First child
134 // Choose smaller child
135 if (($childId+
1) < $lastId &&
136 $this->_less($this->_heap
[$childId+
1], $this->_heap
[$childId])
142 // Move last element to the new position
143 $this->_heap
[$nodeId] = $this->_heap
[$lastId];
144 unset($this->_heap
[$lastId]);
153 public function clear()
155 $this->_heap
= array();
162 * Returns true, if $el1 is less than $el2; else otherwise
168 abstract protected function _less($el1, $el2);