To have a good idea what you can do with SplHeap, I created a little example script that will show the rankings of Belgian soccer teams in the Jupiler League.
<?php
/**
* A class that extends SplHeap for showing rankings in the Belgian
* soccer tournament JupilerLeague
*/
class JupilerLeague extends SplHeap
{
/**
* We modify the abstract method compare so we can sort our
* rankings using the values of a given array
*/
public function compare($array1, $array2)
{
$values1 = array_values($array1);
$values2 = array_values($array2);
if ($values1[0] === $values2[0]) return 0;
return $values1[0] < $values2[0] ? -1 : 1;
}
}
// Let's populate our heap here (data of 2009)
$heap = new JupilerLeague();
$heap->insert(array ('AA Gent' => 15));
$heap->insert(array ('Anderlecht' => 20));
$heap->insert(array ('Cercle Brugge' => 11));
$heap->insert(array ('Charleroi' => 12));
$heap->insert(array ('Club Brugge' => 21));
$heap->insert(array ('G. Beerschot' => 15));
$heap->insert(array ('Kortrijk' => 10));
$heap->insert(array ('KV Mechelen' => 18));
$heap->insert(array ('Lokeren' => 10));
$heap->insert(array ('Moeskroen' => 7));
$heap->insert(array ('Racing Genk' => 11));
$heap->insert(array ('Roeselare' => 6));
$heap->insert(array ('Standard' => 20));
$heap->insert(array ('STVV' => 17));
$heap->insert(array ('Westerlo' => 10));
$heap->insert(array ('Zulte Waregem' => 15));
// For displaying the ranking we move up to the first node
$heap->top();
// Then we iterate through each node for displaying the result
while ($heap->valid()) {
list ($team, $score) = each ($heap->current());
echo $team . ': ' . $score . PHP_EOL;
$heap->next();
}
?>
This results in the following output:
Club Brugge: 21
Anderlecht: 20
Standard: 20
KV Mechelen: 18
STVV: 17
Zulte Waregem: 15
AA Gent: 15
G. Beerschot: 15
Charleroi: 12
Racing Genk: 11
Cercle Brugge: 11
Kortrijk: 10
Lokeren: 10
Westerlo: 10
Moeskroen: 7
Roeselare: 6
Hope this example paved the way for more complex implementations of SplHeap.
SplHeap クラス
導入
SplHeap クラスは、ヒープの主要な機能を提供します。
クラス概要
目次
- SplHeap::compare — 要素を比較し、ヒープ内の適切な位置に置く
- SplHeap::__construct — 新しい空のヒープを作成する
- SplHeap::count — ヒープ内の要素数を数える
- SplHeap::current — イテレータが指す現在のノードを返す
- SplHeap::extract — ヒープの先頭からノードを取り出す
- SplHeap::insert — ヒープに要素を挿入する
- SplHeap::isEmpty — ヒープが空かどうかを調べる
- SplHeap::key — 現在のノードのインデックスを返す
- SplHeap::next — 次のノードに移動する
- SplHeap::recoverFromCorruption — 破壊されたヒープを復旧し、それ以降の操作をできるようにする
- SplHeap::rewind — イテレータを先頭に巻き戻す (何もしない)
- SplHeap::top — ヒープの先頭のノードを取り出す
- SplHeap::valid — ヒープにまだノードがあるかどうかを調べる
SplHeap
Michelangelo van Dam
07-Oct-2009 06:44
07-Oct-2009 06:44
