3
* This file contains classes implementing the queue feature.
5
* @author Qiang Xue <qiang.xue@gmail.com>
6
* @link http://www.yiiframework.com/
7
* @copyright Copyright © 2008-2011 Yii Software LLC
8
* @license http://www.yiiframework.com/license/
12
* CQueue implements a queue.
14
* The typical queue operations are implemented, which include
15
* {@link enqueue()}, {@link dequeue()} and {@link peek()}. In addition,
16
* {@link contains()} can be used to check if an item is contained
17
* in the queue. To obtain the number of the items in the queue,
18
* check the {@link getCount Count} property.
20
* Items in the queue may be traversed using foreach as follows,
22
* foreach($queue as $item) ...
25
* @author Qiang Xue <qiang.xue@gmail.com>
26
* @version $Id: CQueue.php 3001 2011-02-24 16:42:44Z alexander.makarow $
27
* @package system.collections
30
class CQueue extends CComponent implements IteratorAggregate,Countable
33
* internal data storage
45
* Initializes the queue with an array or an iterable object.
46
* @param array $data the intial data. Default is null, meaning no initialization.
47
* @throws CException If data is not null and neither an array nor an iterator.
49
public function __construct($data=null)
52
$this->copyFrom($data);
56
* @return array the list of items in queue
58
public function toArray()
64
* Copies iterable data into the queue.
65
* Note, existing data in the list will be cleared first.
66
* @param mixed $data the data to be copied from, must be an array or object implementing Traversable
67
* @throws CException If data is neither an array nor a Traversable.
69
public function copyFrom($data)
71
if(is_array($data) || ($data instanceof Traversable))
74
foreach($data as $item)
81
throw new CException(Yii::t('yii','Queue data must be an array or an object implementing Traversable.'));
85
* Removes all items in the queue.
87
public function clear()
94
* @param mixed $item the item
95
* @return boolean whether the queue contains the item
97
public function contains($item)
99
return array_search($item,$this->_d,true)!==false;
103
* Returns the item at the top of the queue.
104
* @return mixed item at the top of the queue
105
* @throws CException if the queue is empty
107
public function peek()
110
throw new CException(Yii::t('yii','The queue is empty.'));
116
* Removes and returns the object at the beginning of the queue.
117
* @return mixed the item at the beginning of the queue
118
* @throws CException if the queue is empty
120
public function dequeue()
123
throw new CException(Yii::t('yii','The queue is empty.'));
127
return array_shift($this->_d);
132
* Adds an object to the end of the queue.
133
* @param mixed $item the item to be appended into the queue
135
public function enqueue($item)
138
array_push($this->_d,$item);
142
* Returns an iterator for traversing the items in the queue.
143
* This method is required by the interface IteratorAggregate.
144
* @return Iterator an iterator for traversing the items in the queue.
146
public function getIterator()
148
return new CQueueIterator($this->_d);
152
* Returns the number of items in the queue.
153
* @return integer the number of items in the queue
155
public function getCount()
161
* Returns the number of items in the queue.
162
* This method is required by Countable interface.
163
* @return integer number of items in the queue.
165
public function count()
167
return $this->getCount();