The SplPriorityQueue class

(PHP 5 >= 5.3.0)

Introduzione

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

Sommario dellla classe

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

Indice dei contenuti

add a note add a note

User Contributed Notes 2 notes

up
5
rajatn at rediff dot co dot in
3 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
1
lsroudi at gmail dot com
2 months ago
<?php

/**
 * Description of PriorityQueue
 *
 * (c) lsroudi http://lsroudi.com/ <lsroudi@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
interface PriorityLoggerInterface {

    public function
insert($value, $priority);
}

class
PriorityLogger extends SplPriorityQueue implements PriorityLoggerInterface {
   
}

class
Logger {

    const
ERROR = 3;
    const
NOTICE = 1;
    const
WARNING = 2;

    private
$priorityLogger;

    public function
__construct(PriorityLoggerInterface $priorityLogger)
    {
       
$this->priorityLogger = $priorityLogger;
    }

    public function
addMessage($value, $priority)
    {
       
$this->priorityLogger->insert($value, $priority);
    }

    public function
getPriorityLogger()
    {
        return
$this->priorityLogger;
    }

}

$priorityLogger = new PriorityLogger();

$logger = new Logger($priorityLogger);
$logger->addMessage('Message with notice type', Logger::NOTICE);
$logger->addMessage('Message with warning type', Logger::WARNING);
$logger->addMessage('Message with error type', Logger::ERROR);

$priorityLoggerQueue = $logger->getPriorityLogger();

foreach (
$priorityLoggerQueue as $queue){
    print
$queue . PHP_EOL;
}

//Résultat
//Message with error type
//Message with warning type
//Message with notice type
?>
To Top