CKTIS / PHP
SplHeap
Rozšiřte abstraktní haldu a určete prioritu pomocí compare.
Verze: PHP 8.4; repository runtime, production runtime not attested
Syntaxe
class ByLength extends SplHeap { protected function compare(mixed $a, mixed $b): int { return strlen($a) <=> strlen($b); } }Rozšiřte abstraktní haldu a určete prioritu pomocí compare. Kladný výsledek upřednostní první argument. top čte nejvyšší prvek, extract jej odebere. Při shodě není bez dalšího kritéria pořadí zaručeno.
class ByLength extends SplHeap { protected function compare(mixed $a,mixed $b): int{return strlen($a)<=>strlen($b);} }
$heap=new ByLength(); foreach(["aa","xxxx","b"] as $s){$heap->insert($s);}
echo json_encode([$heap->top(),$heap->count(),$heap->extract(),$heap->top()]);
["xxxx",3,"xxxx","aa"]
Příklady
<?php
class ByLength extends SplHeap { protected function compare(mixed $a,mixed $b): int{return strlen($a)<=>strlen($b);} }
$heap=new ByLength(); foreach(["aa","xxxx","b"] as $s){$heap->insert($s);}
echo json_encode([$heap->top(),$heap->count(),$heap->extract(),$heap->top()]);
Výsledek
["xxxx",3,"xxxx","aa"]