Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 173 lines (160 sloc) 4.099 kb
55bdd95 removed carriage returns from files.
qiang.xue authored
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/
4be0af6 changed copyright year.
qiang.xue authored
7 * @copyright Copyright &copy; 2008-2011 Yii Software LLC
55bdd95 removed carriage returns from files.
qiang.xue authored
8 * @license http://www.yiiframework.com/license/
9 */
10
11 /**
12 * CQueue implements a queue.
13 *
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.
19 *
20 * Items in the queue may be traversed using foreach as follows,
21 * <pre>
22 * foreach($queue as $item) ...
23 * </pre>
24 *
5a90468 More accessors docs, a script to re-generate docs automatically
alexander.makarow authored
25 * @property Iterator $iterator An iterator for traversing the items in the queue.
26 * @property integer $count The number of items in the queue.
27 *
55bdd95 removed carriage returns from files.
qiang.xue authored
28 * @author Qiang Xue <qiang.xue@gmail.com>
29 * @version $Id$
30 * @package system.collections
31 * @since 1.0
32 */
33 class CQueue extends CComponent implements IteratorAggregate,Countable
34 {
35 /**
36 * internal data storage
37 * @var array
38 */
39 private $_d=array();
40 /**
41 * number of items
42 * @var integer
43 */
44 private $_c=0;
45
46 /**
47 * Constructor.
48 * Initializes the queue with an array or an iterable object.
72b9745 Updated PHPDoc @param parameter names
mdomba authored
49 * @param array $data the intial data. Default is null, meaning no initialization.
55bdd95 removed carriage returns from files.
qiang.xue authored
50 * @throws CException If data is not null and neither an array nor an iterator.
51 */
52 public function __construct($data=null)
53 {
54 if($data!==null)
55 $this->copyFrom($data);
56 }
57
58 /**
59 * @return array the list of items in queue
60 */
61 public function toArray()
62 {
63 return $this->_d;
64 }
65
66 /**
67 * Copies iterable data into the queue.
68 * Note, existing data in the list will be cleared first.
72b9745 Updated PHPDoc @param parameter names
mdomba authored
69 * @param mixed $data the data to be copied from, must be an array or object implementing Traversable
55bdd95 removed carriage returns from files.
qiang.xue authored
70 * @throws CException If data is neither an array nor a Traversable.
71 */
72 public function copyFrom($data)
73 {
74 if(is_array($data) || ($data instanceof Traversable))
75 {
76 $this->clear();
77 foreach($data as $item)
78 {
79 $this->_d[]=$item;
80 ++$this->_c;
81 }
82 }
83 else if($data!==null)
cf9b8de Changed Yii::t() and added a category parameter as its first parameter
qiang.xue authored
84 throw new CException(Yii::t('yii','Queue data must be an array or an object implementing Traversable.'));
55bdd95 removed carriage returns from files.
qiang.xue authored
85 }
86
87 /**
88 * Removes all items in the queue.
89 */
90 public function clear()
91 {
92 $this->_c=0;
93 $this->_d=array();
94 }
95
96 /**
72b9745 Updated PHPDoc @param parameter names
mdomba authored
97 * @param mixed $item the item
55bdd95 removed carriage returns from files.
qiang.xue authored
98 * @return boolean whether the queue contains the item
99 */
100 public function contains($item)
101 {
102 return array_search($item,$this->_d,true)!==false;
103 }
104
105 /**
106 * Returns the item at the top of the queue.
107 * @return mixed item at the top of the queue
108 * @throws CException if the queue is empty
109 */
110 public function peek()
111 {
112 if($this->_c===0)
cf9b8de Changed Yii::t() and added a category parameter as its first parameter
qiang.xue authored
113 throw new CException(Yii::t('yii','The queue is empty.'));
55bdd95 removed carriage returns from files.
qiang.xue authored
114 else
7b10ed7 merge from 1.0
qiang.xue authored
115 return $this->_d[0];
55bdd95 removed carriage returns from files.
qiang.xue authored
116 }
117
118 /**
119 * Removes and returns the object at the beginning of the queue.
120 * @return mixed the item at the beginning of the queue
121 * @throws CException if the queue is empty
122 */
123 public function dequeue()
124 {
125 if($this->_c===0)
cf9b8de Changed Yii::t() and added a category parameter as its first parameter
qiang.xue authored
126 throw new CException(Yii::t('yii','The queue is empty.'));
55bdd95 removed carriage returns from files.
qiang.xue authored
127 else
128 {
129 --$this->_c;
130 return array_shift($this->_d);
131 }
132 }
133
134 /**
135 * Adds an object to the end of the queue.
72b9745 Updated PHPDoc @param parameter names
mdomba authored
136 * @param mixed $item the item to be appended into the queue
55bdd95 removed carriage returns from files.
qiang.xue authored
137 */
138 public function enqueue($item)
139 {
140 ++$this->_c;
363f7b5 Alexander Makarov replaced array_push with faster alternative
samdark authored
141 $this->_d[]=$item;
55bdd95 removed carriage returns from files.
qiang.xue authored
142 }
143
144 /**
145 * Returns an iterator for traversing the items in the queue.
146 * This method is required by the interface IteratorAggregate.
147 * @return Iterator an iterator for traversing the items in the queue.
148 */
149 public function getIterator()
150 {
151 return new CQueueIterator($this->_d);
152 }
153
154 /**
15e94ba Added documentations that goes to the method summary table...
mdomba authored
155 * Returns the number of items in the queue.
55bdd95 removed carriage returns from files.
qiang.xue authored
156 * @return integer the number of items in the queue
157 */
158 public function getCount()
159 {
160 return $this->_c;
161 }
162
163 /**
164 * Returns the number of items in the queue.
165 * This method is required by Countable interface.
166 * @return integer number of items in the queue.
167 */
168 public function count()
169 {
170 return $this->getCount();
171 }
172 }
Something went wrong with that request. Please try again.