note 92093 deleted from class.splheap by danbrown

From: Date: Wed, 08 Jul 2009 14:54:47 +0000
Subject: note 92093 deleted from class.splheap by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-158062@lists.php.net to get a copy of this message
Note Submitter: Anonymous ---- <?php class CallbackComparatorHeap extends SplHeap { protected $callback; public function __construct($callback) { if (!is_callable($callback)) { throw new InvalidArgumentException(); } $this->callback = $callback; } public function compare($mixed1, $mixed2) { return call_user_func($this->callback, $mixed1, $mixed2); } } // max heap. // or do b - a for a min heap $heap = new CallbackComparatorHeap(function($a, $b) { return $a['foo'] - $b['foo']; }); $heap->insert(array('foo' => 9)); $heap->insert(array('foo' => 11)); $heap->insert(array('foo' => 2)); foreach ($heap as $node) { echo $node['foo']; } ?>

« previous php.notes (#158062) next »