Core\Heap::isEmpty
$heap->isEmpty(): boolReturns true when a heap has no values, and false when it has at least one.
peek and pop throw a RuntimeError on an empty heap, so isEmpty is the check to make before you call them. A common pattern is a loop that runs while the heap is not empty and takes one value out in each round. A new heap is empty, and a heap becomes empty again after its last value is taken out.
Return value
Returns bool. true for an empty heap, false otherwise.
Examples
A new heap is empty
<?nvs// Shows how `isEmpty` changes when a heap gets its first value.
var $heap = new Core\Heap<int>();
// A new heap has no values. This prints "empty".echo $heap->isEmpty() ? 'empty' : 'not empty', "\n";
$heap->push(42);
// Now the heap has one value. This prints "not empty".echo $heap->isEmpty() ? 'empty' : 'not empty', "\n";emptynot emptyEmpty again after the last pop
<?nvs// Shows that a heap is empty again after its last value is taken out.
var $heap = new Core\Heap<string>();$heap->push('b');$heap->push('a');
$heap->pop();// One value is left. This prints "not empty".echo $heap->isEmpty() ? 'empty' : 'not empty', "\n";
$heap->pop();// No values are left. This prints "empty".echo $heap->isEmpty() ? 'empty' : 'not empty', "\n";not emptyemptyA worker with nothing to do
<?nvs// Shows a worker that checks for work before it takes any. `pop` is called only when there is a value.
class Worker{ public static function runNext(Core\Heap<string> $tasks): void { if ($tasks->isEmpty()) { echo "Nothing to do.\n"; return; } echo 'Doing: ', $tasks->pop(), "\n"; }}
var $tasks = new Core\Heap<string>();$tasks->push('resize the upload');
// The first call finds a task. The second call finds none and prints "Nothing to do.".Worker::runNext($tasks);Worker::runNext($tasks);Doing: resize the uploadNothing to do.