package include::pqueue; use strict; use warnings; use include; # Returns a new priority queue object. sub queueify { return bless [], shift; } # Takes a hashref with a priority value ("P") and whatever other data you want # to store. Adds it to the right place in the queue. sub add { my ($self, $node) = @_; push @$self, $node; my $i = $#{ $self }; # Sort by P, putting the lowest values at the beginning of the queue. # TODO: make me efficent! while ($i != 0) { # Can we swap node number $i with the node before it? # TODO: the 'bubble' option? <=? newest nodes first? if ($self->[$i]{P} < $self->[$i - 1]{P}) { ($self->[$i]{P}, $self->[$i - 1]{P}) = ($self->[$i - 1]{P}, $self->[$i]{P}); $i--; } else { last; } } return 1; } # Nukes and returns the node with the lowest P value. sub lowest { my $self = shift; return undef unless @$self; return shift @$self; } # Nukes and returns the node with the highest P value. sub highest { my $self = shift; return undef unless @$self; return pop @$self; } 42;