>[danger] 注意,因为是`堆`实现,所以`rewind`方法是一个`no-op`没有什作用的操作,因为`头指针`始终指向`堆顶`,即`current`始终等于`top`,不像`List`只是游走指针,出队是会删除堆元素的,`extract`\=`current + next`(current出队,从堆中删除)
## **方法:**
```
abstract SplHeap implements Iterator , Countable {
/* 方法 */
public __construct ( void )
abstract protected compare ( mixed $value1 , mixed $value2 ) : int //比较元素,以便在筛选时将它们正确地放在堆中,如果value1大于value2则为正整数,如果相等则为0,否则为负整数
public count ( void ) : int //返回元素的个数 Countable
public current ( void ) : mixed //返回当前记录节点索引的值 超出范围返回空 Iterator
public extract ( void ) : mixed //从堆顶部提取节点并进行筛选
public insert ( mixed $value ) : void //通过筛选将一个元素插入堆中
public isCorrupted ( void ) : bool //判断堆是否处于损坏状态
public isEmpty ( void ) : bool //检查堆是否为空
public key ( void ) : mixed //返回当前节点索引 Iterator
public next ( void ) : void //移动到下一个节点 Iterator
public recoverFromCorruption ( void ) : void //从损坏的状态恢复并允许堆上的进一步操作
public rewind ( void ) : void //将指针指向迭代开始处 Iterator
public top ( void ) : mixed //返回堆顶部的节点
public valid ( void ) : bool //检查堆是否还有节点 Iterator
}
```
## **例子:**
```
$obj=new SplMaxHeap();
$obj->insert( 4 );
$obj->insert( 8 );
$obj->insert( 1 );
$obj->insert( 0 );
foreach( $obj as $number ) {
echo $number."\n";
}
结果:8 4 1 0
```
等同于:
```
class MySimpleHeap extends SplHeap
{
public function compare( $value1, $value2 ) {
return ( $value1 - $value2 );
}
}
$obj = new MySimpleHeap();
$obj->insert( 4 );
$obj->insert( 8 );
$obj->insert( 1 );
$obj->insert( 0 );
foreach( $obj as $number ) {
echo $number."\n";
}
结果:8 4 1 0
```