CStack
CStack implements a stack.
The typical stack operations are implemented, which include
push(),
pop() and
peek(). In addition,
contains() can be used to check if an item is contained in the stack. To obtain the number of the items in the stack, check the
Count property.
Items in the stack may be traversed using foreach as follows,
foreach($stack as $item) ...
Public Properties
| Property |
Type |
Description |
Defined By |
| count | integer | Returns the number of items in the stack. | CStack |
| iterator | Iterator | Returns an iterator for traversing the items in the stack. | CStack |
Property Details
public integer getCount()
Returns the number of items in the stack.
public Iterator getIterator()
Returns an iterator for traversing the items in the stack. This method is required by the interface IteratorAggregate.
Method Details
public void __construct(array $data=NULL) |
| $data | array | the initial data. Default is null, meaning no initialization. |
Constructor. Initializes the stack with an array or an iterable object.
Removes all items in the stack.
public boolean contains(mixed $item) |
| $item | mixed | the item |
| {return} | boolean | whether the stack contains the item |
public void copyFrom(mixed $data) |
| $data | mixed | the data to be copied from, must be an array or object implementing Traversable |
Source Code: framework/collections/CStack.php#71 (
show)
public function copyFrom($data)
{
if(is_array($data) || ($data instanceof Traversable))
{
$this->clear();
foreach($data as $item)
{
$this->_d[]=$item;
++$this->_c;
}
}
elseif($data!==null)
throw new CException(Yii::t('yii','Stack data must be an array or an object implementing Traversable.'));
}
Copies iterable data into the stack. Note, existing data in the list will be cleared first.
public integer count() |
| {return} | integer | number of items in the stack. |
Returns the number of items in the stack. This method is required by Countable interface.
public integer getCount() |
| {return} | integer | the number of items in the stack |
Returns the number of items in the stack.
public Iterator getIterator() |
| {return} | Iterator | an iterator for traversing the items in the stack. |
Returns an iterator for traversing the items in the stack. This method is required by the interface IteratorAggregate.
public mixed peek() |
| {return} | mixed | item at the top of the stack |
Returns the item at the top of the stack. Unlike pop(), this method does not remove the item from the stack.
public mixed pop() |
| {return} | mixed | the item at the top of the stack |
Source Code: framework/collections/CStack.php#123 (
show)
public function pop()
{
if($this->_c)
{
--$this->_c;
return array_pop($this->_d);
}
else
throw new CException(Yii::t('yii','The stack is empty.'));
}
Pops up the item at the top of the stack.
public void push(mixed $item) |
| $item | mixed | the item to be pushed into the stack |
Pushes an item into the stack.
public array toArray() |
| {return} | array | the list of items in stack |