~joebordes/chive/chive_lib_upgrade

« back to all changes in this revision

Viewing changes to yii/collections/CQueue.php

  • Committer: Joe Bordes
  • Date: 2011-08-22 18:24:26 UTC
  • Revision ID: joe@tsolucio.com-20110822182426-zrvpiuyvm20ybjki
Update yii framework 1.1.8 and associated libraries.
Update About page to reflect changes.
Basic testing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 Copyright &copy; 2008-2011 Yii Software LLC
 
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
 *
 
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
 
28
 * @since 1.0
 
29
 */
 
30
class CQueue extends CComponent implements IteratorAggregate,Countable
 
31
{
 
32
        /**
 
33
         * internal data storage
 
34
         * @var array
 
35
         */
 
36
        private $_d=array();
 
37
        /**
 
38
         * number of items
 
39
         * @var integer
 
40
         */
 
41
        private $_c=0;
 
42
 
 
43
        /**
 
44
         * Constructor.
 
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.
 
48
         */
 
49
        public function __construct($data=null)
 
50
        {
 
51
                if($data!==null)
 
52
                        $this->copyFrom($data);
 
53
        }
 
54
 
 
55
        /**
 
56
         * @return array the list of items in queue
 
57
         */
 
58
        public function toArray()
 
59
        {
 
60
                return $this->_d;
 
61
        }
 
62
 
 
63
        /**
 
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.
 
68
         */
 
69
        public function copyFrom($data)
 
70
        {
 
71
                if(is_array($data) || ($data instanceof Traversable))
 
72
                {
 
73
                        $this->clear();
 
74
                        foreach($data as $item)
 
75
                        {
 
76
                                $this->_d[]=$item;
 
77
                                ++$this->_c;
 
78
                        }
 
79
                }
 
80
                else if($data!==null)
 
81
                        throw new CException(Yii::t('yii','Queue data must be an array or an object implementing Traversable.'));
 
82
        }
 
83
 
 
84
        /**
 
85
         * Removes all items in the queue.
 
86
         */
 
87
        public function clear()
 
88
        {
 
89
                $this->_c=0;
 
90
                $this->_d=array();
 
91
        }
 
92
 
 
93
        /**
 
94
         * @param mixed $item the item
 
95
         * @return boolean whether the queue contains the item
 
96
         */
 
97
        public function contains($item)
 
98
        {
 
99
                return array_search($item,$this->_d,true)!==false;
 
100
        }
 
101
 
 
102
        /**
 
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
 
106
         */
 
107
        public function peek()
 
108
        {
 
109
                if($this->_c===0)
 
110
                        throw new CException(Yii::t('yii','The queue is empty.'));
 
111
                else
 
112
                        return $this->_d[0];
 
113
        }
 
114
 
 
115
        /**
 
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
 
119
         */
 
120
        public function dequeue()
 
121
        {
 
122
                if($this->_c===0)
 
123
                        throw new CException(Yii::t('yii','The queue is empty.'));
 
124
                else
 
125
                {
 
126
                        --$this->_c;
 
127
                        return array_shift($this->_d);
 
128
                }
 
129
        }
 
130
 
 
131
        /**
 
132
         * Adds an object to the end of the queue.
 
133
         * @param mixed $item the item to be appended into the queue
 
134
         */
 
135
        public function enqueue($item)
 
136
        {
 
137
                ++$this->_c;
 
138
                array_push($this->_d,$item);
 
139
        }
 
140
 
 
141
        /**
 
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.
 
145
         */
 
146
        public function getIterator()
 
147
        {
 
148
                return new CQueueIterator($this->_d);
 
149
        }
 
150
 
 
151
        /**
 
152
         * Returns the number of items in the queue.
 
153
         * @return integer the number of items in the queue
 
154
         */
 
155
        public function getCount()
 
156
        {
 
157
                return $this->_c;
 
158
        }
 
159
 
 
160
        /**
 
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.
 
164
         */
 
165
        public function count()
 
166
        {
 
167
                return $this->getCount();
 
168
        }
 
169
}