downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

SplPriorityQueue::compare> <SplMinHeap::compare
[edit] Last updated: Fri, 26 Apr 2013

view this page in

The SplPriorityQueue class

(PHP 5 >= 5.3.0)

Introduction

The SplPriorityQueue class provides the main functionalities of an prioritized queue, implemented using a max heap.

Class synopsis

SplPriorityQueue implements Iterator , Countable {
/* Methods */
__construct ( void )
int compare ( mixed $priority1 , mixed $priority2 )
int count ( void )
mixed current ( void )
mixed extract ( void )
void insert ( mixed $value , mixed $priority )
bool isEmpty ( void )
mixed key ( void )
void next ( void )
void recoverFromCorruption ( void )
void rewind ( void )
void setExtractFlags ( int $flags )
mixed top ( void )
bool valid ( void )
}

Table of Contents



add a note add a note User Contributed Notes SplPriorityQueue - [2 notes]
up
1
rajatn at rediff dot co dot in
2 years ago
quick implementation of SPL Priority Queue:

<?php

class PQtest extends SplPriorityQueue
{
    public function
compare($priority1, $priority2)
    {
        if (
$priority1 === $priority2) return 0;
        return
$priority1 < $priority2 ? -1 : 1;
    }
}

$objPQ = new PQtest();

$objPQ->insert('A',3);
$objPQ->insert('B',6);
$objPQ->insert('C',1);
$objPQ->insert('D',2);

echo
"COUNT->".$objPQ->count()."<BR>";

//mode of extraction
$objPQ->setExtractFlags(PQtest::EXTR_BOTH);

//Go to TOP
$objPQ->top();

while(
$objPQ->valid()){
   
print_r($objPQ->current());
    echo
"<BR>";
   
$objPQ->next();
}

?>

output:

COUNT->4
Array ( [data] => B [priority] => 6 )
Array ( [data] => A [priority] => 3 )
Array ( [data] => D [priority] => 2 )
Array ( [data] => C [priority] => 1 )
up
0
Anonymous
2 years ago
This documentation does not mention whether this is a min-heap or a max-heap.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites