CStack.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * This file contains classes implementing the stack feature.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CStack implements a stack.
  12. *
  13. * The typical stack operations are implemented, which include
  14. * {@link push()}, {@link pop()} and {@link peek()}. In addition,
  15. * {@link contains()} can be used to check if an item is contained
  16. * in the stack. To obtain the number of the items in the stack,
  17. * check the {@link getCount Count} property.
  18. *
  19. * Items in the stack may be traversed using foreach as follows,
  20. * <pre>
  21. * foreach($stack as $item) ...
  22. * </pre>
  23. *
  24. * @property Iterator $iterator An iterator for traversing the items in the stack.
  25. * @property integer $count The number of items in the stack.
  26. *
  27. * @author Qiang Xue <qiang.xue@gmail.com>
  28. * @package system.collections
  29. * @since 1.0
  30. */
  31. class CStack extends CComponent implements IteratorAggregate,Countable
  32. {
  33. /**
  34. * internal data storage
  35. * @var array
  36. */
  37. private $_d=array();
  38. /**
  39. * number of items
  40. * @var integer
  41. */
  42. private $_c=0;
  43. /**
  44. * Constructor.
  45. * Initializes the stack with an array or an iterable object.
  46. * @param array $data the initial 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. * @return array the list of items in stack
  56. */
  57. public function toArray()
  58. {
  59. return $this->_d;
  60. }
  61. /**
  62. * Copies iterable data into the stack.
  63. * Note, existing data in the list will be cleared first.
  64. * @param mixed $data the data to be copied from, must be an array or object implementing Traversable
  65. * @throws CException If data is neither an array nor a Traversable.
  66. */
  67. public function copyFrom($data)
  68. {
  69. if(is_array($data) || ($data instanceof Traversable))
  70. {
  71. $this->clear();
  72. foreach($data as $item)
  73. {
  74. $this->_d[]=$item;
  75. ++$this->_c;
  76. }
  77. }
  78. elseif($data!==null)
  79. throw new CException(Yii::t('yii','Stack data must be an array or an object implementing Traversable.'));
  80. }
  81. /**
  82. * Removes all items in the stack.
  83. */
  84. public function clear()
  85. {
  86. $this->_c=0;
  87. $this->_d=array();
  88. }
  89. /**
  90. * @param mixed $item the item
  91. * @return boolean whether the stack contains the item
  92. */
  93. public function contains($item)
  94. {
  95. return array_search($item,$this->_d,true)!==false;
  96. }
  97. /**
  98. * Returns the item at the top of the stack.
  99. * Unlike {@link pop()}, this method does not remove the item from the stack.
  100. * @return mixed item at the top of the stack
  101. * @throws CException if the stack is empty
  102. */
  103. public function peek()
  104. {
  105. if($this->_c)
  106. return $this->_d[$this->_c-1];
  107. else
  108. throw new CException(Yii::t('yii','The stack is empty.'));
  109. }
  110. /**
  111. * Pops up the item at the top of the stack.
  112. * @return mixed the item at the top of the stack
  113. * @throws CException if the stack is empty
  114. */
  115. public function pop()
  116. {
  117. if($this->_c)
  118. {
  119. --$this->_c;
  120. return array_pop($this->_d);
  121. }
  122. else
  123. throw new CException(Yii::t('yii','The stack is empty.'));
  124. }
  125. /**
  126. * Pushes an item into the stack.
  127. * @param mixed $item the item to be pushed into the stack
  128. */
  129. public function push($item)
  130. {
  131. ++$this->_c;
  132. $this->_d[]=$item;
  133. }
  134. /**
  135. * Returns an iterator for traversing the items in the stack.
  136. * This method is required by the interface IteratorAggregate.
  137. * @return Iterator an iterator for traversing the items in the stack.
  138. */
  139. public function getIterator()
  140. {
  141. return new CStackIterator($this->_d);
  142. }
  143. /**
  144. * Returns the number of items in the stack.
  145. * @return integer the number of items in the stack
  146. */
  147. public function getCount()
  148. {
  149. return $this->_c;
  150. }
  151. /**
  152. * Returns the number of items in the stack.
  153. * This method is required by Countable interface.
  154. * @return integer number of items in the stack.
  155. */
  156. public function count()
  157. {
  158. return $this->getCount();
  159. }
  160. }