Source for file Queue.class.php

Documentation is available at Queue.class.php

  1. <?php
  2.  
  3. //require_once(HARMONI."utilities/Queue.interface.php");
  4. /**
  5. * A generic queue of objects. It provides iterator functions next() and hasNext().
  6. *
  7. * @package harmoni.utilities
  8. *
  9. * @copyright Copyright &copy; 2005, Middlebury College
  10. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  11. *
  12. * @version $Id: Queue.class.php,v 1.5 2007/09/04 20:25:54 adamfranco Exp $
  13. */
  14. class Queue {
  15.  
  16. /**
  17. * An array used to store the elements of the queue.
  18. * @var array $_queue An array used to store the elements of the queue.
  19. */
  20. var $_queue;
  21.  
  22. /**
  23. * The position in the array of the next element of the queue that will be returned when next() is called.
  24. * @var integer $_nextPosition The position in the array of the next element of the queue that will be returned when next() is called.
  25. */
  26. var $_nextPosition;
  27.  
  28. /**
  29. * The start position of the queue in the array. In a reversed queue, this
  30. * would be the last element of the array. In a normal queue, this would equal
  31. * zero.
  32. * @var integer $_startPosition The start position of the queue in the array.
  33. */
  34. var $_startPosition;
  35.  
  36. /**
  37. * The order of extraction from the queue.
  38. * Indicates whether the order in which objects are extracted from the queue
  39. * is FIFO ($_reversed = false) or FILO ($_reversed = true).
  40. *
  41. * @var boolean $_reversed TRUE, if the queue is reversed.
  42. */
  43. var $_reversed;
  44.  
  45. /**
  46. * Constructor. Create a new Queue.
  47. *
  48. * @param boolean $reversed The order of extraction from the queue.
  49. * @access public
  50. */
  51. function Queue($reversed = false){
  52. $this->_reversed = $reversed;
  53. $this->clear();
  54. }
  55.  
  56. /**
  57. * Clear the queue.
  58. *
  59. * @access public
  60. */
  61. function clear() {
  62. $this->_queue = array();
  63. // these will be set correctly when the first element gets added
  64. $this->_startPosition = null;
  65. $this->_nextPosition = null;
  66. }
  67.  
  68. /**
  69. * Add an object to the queue. The queue is automatically rewound at the end.
  70. * @param object $object The object to add to the queue.
  71. * @access public
  72. */
  73. function add($object) {
  74. $this->_queue[] =$object;
  75.  
  76. // if we just inserted the first element in the queue, then reset the position indices
  77. if ($this->getSize() == 1) {
  78. $this->_startPosition = 0;
  79. $this->_nextPosition = 0;
  80. }
  81. // if the inserted element is not the first, then just adjust _startPosition if
  82. // the queue is reversed
  83. elseif ($this->_reversed)
  84. $this->_startPosition++;
  85.  
  86. // when adding a new element, the queue is automatically rewound
  87. $this->rewind();
  88. }
  89.  
  90. /**
  91. * Get the current element and increase the position by one.
  92. * @return object Object at the current position in the queue and increase the position by one.
  93. * @access public
  94. */
  95. function next() {
  96. // get next element
  97. $object =$this->_queue[$this->_nextPosition];
  98.  
  99. // adjust _nextPosition depending on the queue type
  100. if($this->_reversed)
  101. $this->_nextPosition--;
  102. else
  103. $this->_nextPosition++;
  104. return $object;
  105. }
  106.  
  107. /**
  108. * @return boolean Whether there exists an object in the queue at the current position.
  109. * @access public
  110. */
  111. function hasNext() {
  112. return (isset($this->_queue[$this->_nextPosition])) ? true : false;
  113. }
  114. /**
  115. * Get the size of the queue.
  116. * @return integer The size of the queue
  117. * @access public
  118. */
  119. function getSize() {
  120. return count($this->_queue);
  121. }
  122.  
  123. /**
  124. * Rewind the queue if it is not reversed
  125. * @return boolean True on sucess, false on failure (queue is reversed).
  126. * @access public
  127. */
  128. function rewind() {
  129. $this->_nextPosition = $this->_startPosition;
  130. }
  131. }
  132.  
  133.  
  134. ?>

Documentation generated on Wed, 19 Sep 2007 10:25:45 -0400 by phpDocumentor 1.3.0RC3