| 包 | system.collections |
|---|---|
| 继承 | class CStack » CComponent |
| 实现 | IteratorAggregate, Traversable, Countable |
| 源自 | 1.0 |
| 版本 | $Id: CStack.PHP 3427 2011-10-25 00:03:52Z alexander.makarow $ |
| 源码 |
CStack实现一个栈。
这个类已经实现了一些典型的栈的操作,包括 push(),pop()和peek()。另外, contains()可以用来检查项目是否存在 于栈中。通过Count属性,可以取得 列表中的项目数。
栈中的项目可以像下面那样使用foreach遍历,
这个类已经实现了一些典型的栈的操作,包括 push(),pop()和peek()。另外, contains()可以用来检查项目是否存在 于栈中。通过Count属性,可以取得 列表中的项目数。
栈中的项目可以像下面那样使用foreach遍历,
foreach($stack as $item) ...
公共属性
| 属性 | 类型 | 描述 | 定义在 |
|---|---|---|---|
| count | integer | 返回栈中的项目数。 | CStack |
| iterator | Iterator | 返回遍历这个栈的项目的迭代器。 | CStack |
公共方法
| 方法 | 描述 | 定义在 |
|---|---|---|
| __call() | 如果类中没有调的方法名,则调用这个方法。 | CComponent |
| __construct() | 构造方法。 | CStack |
| __get() | 返回一个属性值、一个事件处理程序列表或一个行为名称。 | CComponent |
| __isset() | 检查一个属性是否为null。 | CComponent |
| __set() | 设置一个组件的属性值。 | CComponent |
| __unset() | 设置一个组件的属性为null。 | CComponent |
| asa() | 返回这个名字的行为对象。 | CComponent |
| attachBehavior() | 附加一个行为到组件。 | CComponent |
| attachBehaviors() | 附加一个行为列表到组件。 | CComponent |
| attachEventHandler() | 为事件附加一个事件处理程序。 | CComponent |
| canGetProperty() | 确定属性是否可读。 | CComponent |
| canSetProperty() | 确定属性是否可写。 | CComponent |
| clear() | 删除栈中所有项目。 | CStack |
| contains() | CStack | |
| copyFrom() | 将迭代器中的数据复制到栈。 | CStack |
| count() | 返回栈中的项目数。 | CStack |
| detachBehavior() | 从组件中分离一个行为。 | CComponent |
| detachBehaviors() | 从组件中分离所有行为。 | CComponent |
| detachEventHandler() | 分离一个存在的事件处理程序。 | CComponent |
| disableBehavior() | 禁用一个附加行为。 | CComponent |
| disableBehaviors() | 禁用组件附加的所有行为。 | CComponent |
| enableBehavior() | 启用一个附加行为。 | CComponent |
| enableBehaviors() | 启用组件附加的所有行为。 | CComponent |
| evaLuateExpression() | 计算一个PHP表达式,或根据组件上下文执行回调。 | CComponent |
| getCount() | 返回栈中的项目数。 | CStack |
| getEventHandlers() | 返回一个事件的附加处理程序列表。 | CComponent |
| getIterator() | 返回遍历这个栈的项目的迭代器。 | CStack |
| hasEvent() | 确定一个事件是否定义。 | CComponent |
| hasEventHandler() | 检查事件是否有附加的处理程序。 | CComponent |
| hasProperty() | 确定属性是否被定义。 | CComponent |
| peek() | 返回处于栈头部的项目。 | CStack |
| pop() | 取出处于栈头部的项目。 | CStack |
| push() | 添加一个对象到栈里面。 | CStack |
| raiseEvent() | 发起一个事件。 | CComponent |
| toArray() | CStack |
属性详细
count
属性
只读
public integer getCount()
返回栈中的项目数。
iterator
属性
只读
public Iterator getIterator()
返回遍历这个栈的项目的迭代器。 此方法为接口IteratorAggregate强制要求实现。
方法详细
__construct()
方法
|
public void __construct(array $data=NULL)
| ||
| $data | array | 初始化的数据。默认为null,意味着不会初始化。 |
public function __construct($data=null)
{
if($data!==null)
$this->copyFrom($data);
}
构造方法。 根据数组或者迭代对象初始化这个栈。
clear()
方法
|
public void clear()
|
public function clear()
{
$this->_c=0;
$this->_d=array();
}
删除栈中所有项目。
contains()
方法
|
public boolean contains(mixed $item)
| ||
| $item | mixed | 项目对象 |
| {return} | boolean | 返回值说明栈中是否包含该项目 |
public function contains($item)
{
return array_search($item,$this->_d,true)!==false;
}
copyFrom()
方法
|
public void copyFrom(mixed $data)
| ||
| $data | mixed | 要复制的数据, 只能是数组或者继承于Traversable的对象。 |
public function copyFrom($data)
{
if(is_array($data) || ($data instanceof Traversable))
{
$this->clear();
foreach($data as $item)
{
$this->_d[]=$item;
++$this->_c;
}
}
else if($data!==null)
throw new CException(Yii::t('yii','Stack data must be an array or an object implementing Traversable.'));
}
将迭代器中的数据复制到栈。 注意,栈中已经存在的数据会被首先删除。
count()
方法
|
public integer count()
| ||
| {return} | integer | 返回栈中的项目数。 |
public function count()
{
return $this->getCount();
}
返回栈中的项目数。 此方法为接口Countable强制要求实现。
getCount()
方法
|
public integer getCount()
| ||
| {return} | integer | 返回栈中的项目数。 |
public function getCount()
{
return $this->_c;
}
返回栈中的项目数。
getIterator()
方法
|
public Iterator getIterator()
| ||
| {return} | Iterator | 返回遍历这个栈的项目的迭代器。 |
public function getIterator()
{
return new CStackIterator($this->_d);
}
返回遍历这个栈的项目的迭代器。 此方法为接口IteratorAggregate强制要求实现。
peek()
方法
|
public mixed peek()
| ||
| {return} | mixed | 返回处于栈头部的项目。 |
public function peek()
{
if($this->_c)
return $this->_d[$this->_c-1];
else
throw new CException(Yii::t('yii','The stack is empty.'));
}
返回处于栈头部的项目。 跟pop()不同,这个方法不会删除栈的项目。
pop()
方法
|
public mixed pop()
| ||
| {return} | mixed | 返回处于栈头部的项目。 |
public function pop()
{
if($this->_c)
{
--$this->_c;
return array_pop($this->_d);
}
else
throw new CException(Yii::t('yii','The stack is empty.'));
}
取出处于栈头部的项目。
push()
方法
|
public void push(mixed $item)
| ||
| $item | mixed | 要添加到栈里面的对象。 |
public function push($item)
{
++$this->_c;
array_push($this->_d,$item);
}
添加一个对象到栈里面。
toArray()
方法
|
public 栈中的项目列表。 toArray()
| ||
| {return} | 栈中的项目列表。 | |
public function toArray()
{
return $this->_d;
}