2
0

PriorityQueue.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Search_Lucene
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * Abstract Priority Queue
  22. *
  23. * It implements a priority queue.
  24. * Please go to "Data Structures and Algorithms",
  25. * Aho, Hopcroft, and Ullman, Addison-Wesley, 1983 (corrected 1987 edition),
  26. * for implementation details.
  27. *
  28. * It provides O(log(N)) time of put/pop operations, where N is a size of queue
  29. *
  30. * @category Zend
  31. * @package Zend_Search_Lucene
  32. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. abstract class Zend_Search_Lucene_PriorityQueue
  36. {
  37. /**
  38. * Queue heap
  39. *
  40. * Heap contains balanced partial ordered binary tree represented in array
  41. * [0] - top of the tree
  42. * [1] - first child of [0]
  43. * [2] - second child of [0]
  44. * ...
  45. * [2*n + 1] - first child of [n]
  46. * [2*n + 2] - second child of [n]
  47. *
  48. * @var array
  49. */
  50. private $_heap = array();
  51. /**
  52. * Add element to the queue
  53. *
  54. * O(log(N)) time
  55. *
  56. * @param mixed $element
  57. */
  58. public function put($element)
  59. {
  60. $nodeId = count($this->_heap);
  61. $parentId = ($nodeId-1) >> 1; // floor( ($nodeId-1)/2 )
  62. while ($nodeId != 0 && $this->_less($element, $this->_heap[$parentId])) {
  63. // Move parent node down
  64. $this->_heap[$nodeId] = $this->_heap[$parentId];
  65. // Move pointer to the next level of tree
  66. $nodeId = $parentId;
  67. $parentId = ($nodeId-1) >> 1; // floor( ($nodeId-1)/2 )
  68. }
  69. // Put new node into the tree
  70. $this->_heap[$nodeId] = $element;
  71. }
  72. /**
  73. * Return least element of the queue
  74. *
  75. * Constant time
  76. *
  77. * @return mixed
  78. */
  79. public function top()
  80. {
  81. if (count($this->_heap) == 0) {
  82. return null;
  83. }
  84. return $this->_heap[0];
  85. }
  86. /**
  87. * Removes and return least element of the queue
  88. *
  89. * O(log(N)) time
  90. *
  91. * @return mixed
  92. */
  93. public function pop()
  94. {
  95. if (count($this->_heap) == 0) {
  96. return null;
  97. }
  98. $top = $this->_heap[0];
  99. $lastId = count($this->_heap) - 1;
  100. /**
  101. * Find appropriate position for last node
  102. */
  103. $nodeId = 0; // Start from a top
  104. $childId = 1; // First child
  105. // Choose smaller child
  106. if ($lastId > 2 && $this->_less($this->_heap[2], $this->_heap[1])) {
  107. $childId = 2;
  108. }
  109. while ($childId < $lastId &&
  110. $this->_less($this->_heap[$childId], $this->_heap[$lastId])
  111. ) {
  112. // Move child node up
  113. $this->_heap[$nodeId] = $this->_heap[$childId];
  114. $nodeId = $childId; // Go down
  115. $childId = ($nodeId << 1) + 1; // First child
  116. // Choose smaller child
  117. if (($childId+1) < $lastId &&
  118. $this->_less($this->_heap[$childId+1], $this->_heap[$childId])
  119. ) {
  120. $childId++;
  121. }
  122. }
  123. // Move last element to the new position
  124. $this->_heap[$nodeId] = $this->_heap[$lastId];
  125. unset($this->_heap[$lastId]);
  126. return $top;
  127. }
  128. /**
  129. * Clear queue
  130. */
  131. public function clear()
  132. {
  133. $this->_heap = array();
  134. }
  135. /**
  136. * Compare elements
  137. *
  138. * Returns true, if $el1 is less than $el2; else otherwise
  139. *
  140. * @param mixed $el1
  141. * @param mixed $el2
  142. * @return boolean
  143. */
  144. abstract protected function _less($el1, $el2);
  145. }