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