Queue::Priority
use Queue::Priority; use List::Util qw( shuffle ); my $queue = Queue::Priority->new( 10 ); foreach my $i ( shuffle 1 .. 10 ) { $queue->insert( $i ); } while (1) { my $i = $queue->remove or last; printf "%d * 2 = %d\n", $i, $i * 2; }
Priority queues automatically order their contents according to the inserted item's priority. Calling code must ensure that their queue items are comparable via this strategy (e.g. by overloading the <=> operator). This module is implemented as an array heap.
Creates a new queue that can store $max items.
$max
Returns the number of items currently stored.
Returns true if the queue is empty.
Returns true if the queue is full.
Returns the first (highest priority) element in the queue without removing it from the queue.
Returns true if the queue has been shut down.
Shuts down the queue, after which no items may be inserted. Items already in the queue can be pulled normally until empty, after which further calls to remove will return undefined.
remove
Inserts an item into the queue. Dies if the queue is full, has been shut down, or if the only argument is undefined.
Removes and returns an item from the queue. If the queue is empty or shutdown, returns undefined immediately.
Prints an indented representation of the heap structure.
Jeff Ober <jeffober@gmail.com>
This software is copyright (c) 2015 by Jeff Ober.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Queue::Priority, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Queue::Priority
CPAN shell
perl -MCPAN -e shell install Queue::Priority
For more information on module installation, please visit the detailed CPAN module installation guide.