]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Collections/TStack.php
91996aaaa3036f408c5208e5678d59379a0f1d9a
[bacula/bacula] / gui / baculum / framework / Collections / TStack.php
1 <?php
2 /**
3  * TStack, TStackIterator classes
4  *
5  * @author Qiang Xue <qiang.xue@gmail.com>
6  * @link http://www.pradosoft.com/
7  * @copyright Copyright &copy; 2005-2014 PradoSoft
8  * @license http://www.pradosoft.com/license/
9  * @package System.Collections
10  */
11
12 /**
13  * TStack class
14  *
15  * TStack implements a stack.
16  *
17  * The typical stack operations are implemented, which include
18  * {@link push()}, {@link pop()} and {@link peek()}. In addition,
19  * {@link contains()} can be used to check if an item is contained
20  * in the stack. To obtain the number of the items in the stack,
21  * check the {@link getCount Count} property.
22  *
23  * Items in the stack may be traversed using foreach as follows,
24  * <code>
25  * foreach($stack as $item) ...
26  * </code>
27  *
28  * @author Qiang Xue <qiang.xue@gmail.com>
29  * @package System.Collections
30  * @since 3.0
31  */
32 class TStack extends TComponent implements IteratorAggregate,Countable
33 {
34         /**
35          * internal data storage
36          * @var array
37          */
38         private $_d=array();
39         /**
40          * number of items
41          * @var integer
42          */
43         private $_c=0;
44
45         /**
46          * Constructor.
47          * Initializes the stack with an array or an iterable object.
48          * @param array|Iterator the initial data. Default is null, meaning no initialization.
49          * @throws TInvalidDataTypeException If data is not null and neither an array nor an iterator.
50          */
51         public function __construct($data=null)
52         {
53                 if($data!==null)
54                         $this->copyFrom($data);
55         }
56
57         /**
58          * @return array the list of items in stack
59          */
60         public function toArray()
61         {
62                 return $this->_d;
63         }
64
65         /**
66          * Copies iterable data into the stack.
67          * Note, existing data in the list will be cleared first.
68          * @param mixed the data to be copied from, must be an array or object implementing Traversable
69          * @throws TInvalidDataTypeException If data is neither an array nor a Traversable.
70          */
71         public function copyFrom($data)
72         {
73                 if(is_array($data) || ($data instanceof Traversable))
74                 {
75                         $this->clear();
76                         foreach($data as $item)
77                         {
78                                 $this->_d[]=$item;
79                                 ++$this->_c;
80                         }
81                 }
82                 else if($data!==null)
83                         throw new TInvalidDataTypeException('stack_data_not_iterable');
84         }
85
86         /**
87          * Removes all items in the stack.
88          */
89         public function clear()
90         {
91                 $this->_c=0;
92                 $this->_d=array();
93         }
94
95         /**
96          * @param mixed the item
97          * @return boolean whether the stack contains the item
98          */
99         public function contains($item)
100         {
101                 return array_search($item,$this->_d,true)!==false;
102         }
103
104         /**
105          * Returns the item at the top of the stack.
106          * Unlike {@link pop()}, this method does not remove the item from the stack.
107          * @return mixed item at the top of the stack
108          * @throws TInvalidOperationException if the stack is empty
109          */
110         public function peek()
111         {
112                 if($this->_c===0)
113                         throw new TInvalidOperationException('stack_empty');
114                 else
115                         return $this->_d[$this->_c-1];
116         }
117
118         /**
119          * Pops up the item at the top of the stack.
120          * @return mixed the item at the top of the stack
121          * @throws TInvalidOperationException if the stack is empty
122          */
123         public function pop()
124         {
125                 if($this->_c===0)
126                         throw new TInvalidOperationException('stack_empty');
127                 else
128                 {
129                         --$this->_c;
130                         return array_pop($this->_d);
131                 }
132         }
133
134         /**
135          * Pushes an item into the stack.
136          * @param mixed the item to be pushed into the stack
137          */
138         public function push($item)
139         {
140                 ++$this->_c;
141                 $this->_d[] = $item;
142         }
143
144         /**
145          * Returns an iterator for traversing the items in the stack.
146          * This method is required by the interface IteratorAggregate.
147          * @return Iterator an iterator for traversing the items in the stack.
148          */
149         public function getIterator()
150         {
151                 return new ArrayIterator( $this->_d );
152         }
153
154         /**
155          * @return integer the number of items in the stack
156          */
157         public function getCount()
158         {
159                 return $this->_c;
160         }
161
162         /**
163          * Returns the number of items in the stack.
164          * This method is required by Countable interface.
165          * @return integer number of items in the stack.
166          */
167         public function count()
168         {
169                 return $this->getCount();
170         }
171 }
172
173 /**
174  * TStackIterator class
175  *
176  * TStackIterator implements Iterator interface.
177  *
178  * TStackIterator is used by TStack. It allows TStack to return a new iterator
179  * for traversing the items in the list.
180  *
181  * @deprecated Issue 264 : ArrayIterator should be used instead
182  * @author Qiang Xue <qiang.xue@gmail.com>
183  * @package System.Collections
184  * @since 3.0
185  */
186 class TStackIterator implements Iterator
187 {
188         /**
189          * @var array the data to be iterated through
190          */
191         private $_d;
192         /**
193          * @var integer index of the current item
194          */
195         private $_i;
196         /**
197          * @var integer count of the data items
198          */
199         private $_c;
200
201         /**
202          * Constructor.
203          * @param array the data to be iterated through
204          */
205         public function __construct(&$data)
206         {
207                 $this->_d=&$data;
208                 $this->_i=0;
209                 $this->_c=count($this->_d);
210         }
211
212         /**
213          * Rewinds internal array pointer.
214          * This method is required by the interface Iterator.
215          */
216         public function rewind()
217         {
218                 $this->_i=0;
219         }
220
221         /**
222          * Returns the key of the current array item.
223          * This method is required by the interface Iterator.
224          * @return integer the key of the current array item
225          */
226         public function key()
227         {
228                 return $this->_i;
229         }
230
231         /**
232          * Returns the current array item.
233          * This method is required by the interface Iterator.
234          * @return mixed the current array item
235          */
236         public function current()
237         {
238                 return $this->_d[$this->_i];
239         }
240
241         /**
242          * Moves the internal pointer to the next array item.
243          * This method is required by the interface Iterator.
244          */
245         public function next()
246         {
247                 $this->_i++;
248         }
249
250         /**
251          * Returns whether there is an item at current position.
252          * This method is required by the interface Iterator.
253          * @return boolean
254          */
255         public function valid()
256         {
257                 return $this->_i<$this->_c;
258         }
259 }
260