3 * TStack, TStackIterator classes
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link http://www.pradosoft.com/
7 * @copyright Copyright © 2005-2014 PradoSoft
8 * @license http://www.pradosoft.com/license/
9 * @package System.Collections
15 * TStack implements a stack.
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.
23 * Items in the stack may be traversed using foreach as follows,
25 * foreach($stack as $item) ...
28 * @author Qiang Xue <qiang.xue@gmail.com>
29 * @package System.Collections
32 class TStack extends TComponent implements IteratorAggregate,Countable
35 * internal data storage
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.
51 public function __construct($data=null)
54 $this->copyFrom($data);
58 * @return array the list of items in stack
60 public function toArray()
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.
71 public function copyFrom($data)
73 if(is_array($data) || ($data instanceof Traversable))
76 foreach($data as $item)
83 throw new TInvalidDataTypeException('stack_data_not_iterable');
87 * Removes all items in the stack.
89 public function clear()
96 * @param mixed the item
97 * @return boolean whether the stack contains the item
99 public function contains($item)
101 return array_search($item,$this->_d,true)!==false;
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
110 public function peek()
113 throw new TInvalidOperationException('stack_empty');
115 return $this->_d[$this->_c-1];
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
123 public function pop()
126 throw new TInvalidOperationException('stack_empty');
130 return array_pop($this->_d);
135 * Pushes an item into the stack.
136 * @param mixed the item to be pushed into the stack
138 public function push($item)
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.
149 public function getIterator()
151 return new ArrayIterator( $this->_d );
155 * @return integer the number of items in the stack
157 public function getCount()
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.
167 public function count()
169 return $this->getCount();
174 * TStackIterator class
176 * TStackIterator implements Iterator interface.
178 * TStackIterator is used by TStack. It allows TStack to return a new iterator
179 * for traversing the items in the list.
181 * @deprecated Issue 264 : ArrayIterator should be used instead
182 * @author Qiang Xue <qiang.xue@gmail.com>
183 * @package System.Collections
186 class TStackIterator implements Iterator
189 * @var array the data to be iterated through
193 * @var integer index of the current item
197 * @var integer count of the data items
203 * @param array the data to be iterated through
205 public function __construct(&$data)
209 $this->_c=count($this->_d);
213 * Rewinds internal array pointer.
214 * This method is required by the interface Iterator.
216 public function rewind()
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
226 public function key()
232 * Returns the current array item.
233 * This method is required by the interface Iterator.
234 * @return mixed the current array item
236 public function current()
238 return $this->_d[$this->_i];
242 * Moves the internal pointer to the next array item.
243 * This method is required by the interface Iterator.
245 public function next()
251 * Returns whether there is an item at current position.
252 * This method is required by the interface Iterator.
255 public function valid()
257 return $this->_i<$this->_c;