CQueue.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * This file contains classes implementing the queue feature.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CQueue implements a queue.
  12. *
  13. * The typical queue operations are implemented, which include
  14. * {@link enqueue()}, {@link dequeue()} and {@link peek()}. In addition,
  15. * {@link contains()} can be used to check if an item is contained
  16. * in the queue. To obtain the number of the items in the queue,
  17. * check the {@link getCount Count} property.
  18. *
  19. * Items in the queue may be traversed using foreach as follows,
  20. * <pre>
  21. * foreach($queue as $item) ...
  22. * </pre>
  23. *
  24. * @property Iterator $iterator An iterator for traversing the items in the queue.
  25. * @property integer $count The number of items in the queue.
  26. *
  27. * @author Qiang Xue <qiang.xue@gmail.com>
  28. * @package system.collections
  29. * @since 1.0
  30. */
  31. class CQueue extends CComponent implements IteratorAggregate,Countable
  32. {
  33. /**
  34. * internal data storage
  35. * @var array
  36. */
  37. private $_d=array();
  38. /**
  39. * number of items
  40. * @var integer
  41. */
  42. private $_c=0;
  43. /**
  44. * Constructor.
  45. * Initializes the queue with an array or an iterable object.
  46. * @param array $data the initial data. Default is null, meaning no initialization.
  47. * @throws CException If data is not null and neither an array nor an iterator.
  48. */
  49. public function __construct($data=null)
  50. {
  51. if($data!==null)
  52. $this->copyFrom($data);
  53. }
  54. /**
  55. * @return array the list of items in queue
  56. */
  57. public function toArray()
  58. {
  59. return $this->_d;
  60. }
  61. /**
  62. * Copies iterable data into the queue.
  63. * Note, existing data in the list will be cleared first.
  64. * @param mixed $data the data to be copied from, must be an array or object implementing Traversable
  65. * @throws CException If data is neither an array nor a Traversable.
  66. */
  67. public function copyFrom($data)
  68. {
  69. if(is_array($data) || ($data instanceof Traversable))
  70. {
  71. $this->clear();
  72. foreach($data as $item)
  73. {
  74. $this->_d[]=$item;
  75. ++$this->_c;
  76. }
  77. }
  78. elseif($data!==null)
  79. throw new CException(Yii::t('yii','Queue data must be an array or an object implementing Traversable.'));
  80. }
  81. /**
  82. * Removes all items in the queue.
  83. */
  84. public function clear()
  85. {
  86. $this->_c=0;
  87. $this->_d=array();
  88. }
  89. /**
  90. * @param mixed $item the item
  91. * @return boolean whether the queue contains the item
  92. */
  93. public function contains($item)
  94. {
  95. return array_search($item,$this->_d,true)!==false;
  96. }
  97. /**
  98. * Returns the item at the top of the queue.
  99. * @return mixed item at the top of the queue
  100. * @throws CException if the queue is empty
  101. */
  102. public function peek()
  103. {
  104. if($this->_c===0)
  105. throw new CException(Yii::t('yii','The queue is empty.'));
  106. else
  107. return $this->_d[0];
  108. }
  109. /**
  110. * Removes and returns the object at the beginning of the queue.
  111. * @return mixed the item at the beginning of the queue
  112. * @throws CException if the queue is empty
  113. */
  114. public function dequeue()
  115. {
  116. if($this->_c===0)
  117. throw new CException(Yii::t('yii','The queue is empty.'));
  118. else
  119. {
  120. --$this->_c;
  121. return array_shift($this->_d);
  122. }
  123. }
  124. /**
  125. * Adds an object to the end of the queue.
  126. * @param mixed $item the item to be appended into the queue
  127. */
  128. public function enqueue($item)
  129. {
  130. ++$this->_c;
  131. $this->_d[]=$item;
  132. }
  133. /**
  134. * Returns an iterator for traversing the items in the queue.
  135. * This method is required by the interface IteratorAggregate.
  136. * @return Iterator an iterator for traversing the items in the queue.
  137. */
  138. public function getIterator()
  139. {
  140. return new CQueueIterator($this->_d);
  141. }
  142. /**
  143. * Returns the number of items in the queue.
  144. * @return integer the number of items in the queue
  145. */
  146. public function getCount()
  147. {
  148. return $this->_c;
  149. }
  150. /**
  151. * Returns the number of items in the queue.
  152. * This method is required by Countable interface.
  153. * @return integer number of items in the queue.
  154. */
  155. public function count()
  156. {
  157. return $this->getCount();
  158. }
  159. }