Array-Heap-ModifiablePriorityQueue

 view release on metacpan or  search on metacpan

lib/Array/Heap/PriorityQueue/Numeric.pm  view on Meta::CPAN

package Array::Heap::PriorityQueue::Numeric;
use strict;
use warnings;
use vars qw( $VERSION );
$VERSION = '1.10';
use Array::Heap qw( make_heap pop_heap push_heap );

=head1 NAME

Array::Heap::PriorityQueue::Numeric - Numeric priority queue

=head1 SYNOPSIS

   use Array::Heap::PriorityQueue::Numeric;
   my $pq = Array::Heap::PriorityQueue::Numeric->new();
   $pq->add('fish', 42);
   $pq->add('banana', 27);
   print $pq->get(), "\n"; # banana
   print $pq->peek(), "\n"; # fish

=head1 DESCRIPTION

This module implements a priority queue, which is a data structure that can
efficiently locate the item with the lowest weight at any time. This is useful
for writing cost-minimizing and shortest-path algorithms.

Weights are numeric values.

This module is a wrapper for the *_heap methods provided by L<Array::Heap>.

=head1 FUNCTIONS

=over 4

=item Array::Heap::PriorityQueue::Numeric->new()

Create a new, empty priority queue.

=cut

sub new {
   my ($class) = @_;
   return bless [ ] => $class;
}

=item $pq->add($item, $weight)

Add an item to the priority queue with the given weight. Weight must be
numeric, and defaults to item.

=cut

sub add {
   my ($self, $item, $weight) = @_;
   $weight = $item + 0 unless defined $weight;
   push_heap @$self, [ $weight, $item ];
}

=item $pq->peek()

Return the first (numerically lowest weight) item from the queue.
Does not modify the queue. Returns undef if the queue is empty.

=cut

sub peek {
   my ($self) = @_;
   my $node = $self->[0] or return;
   return $node->[1];
}

=item $pq->get()

Removes the first item from the priority queue and returns it.
Returns undef if the queue is empty. If two items in the queue
have equal weight, this module makes no guarantee as to which
one will be returned first.

=cut

sub get {
   my ($self) = @_;
   my $node = pop_heap @$self or return;
   return $node->[1];
}

=item $pq->min_weight()

Returns the weight of the lowest item in the queue, or undef if empty.

=cut

sub min_weight {
   my ($self) = @_;
   my $node = $self->[0] or return;
   return $node->[0];
}

=item $pq->size()

Returns the number of items in the priority queue.

=cut

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.611 second using v1.00-cache-2.02-grep-82fe00e-cpan-1925d2aa809 )