Bob Mathews
and 1 contributors


Array::Heap::PriorityQueue::String - String-weighted priority queue


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


This module implements a priority queue, which is a data structure that can efficiently locate the item with the lowest weight at any time.

Weights are strings, which are sorted in lexicographic order.

This module is a wrapper around the *_heap_lex methods provided by Array::Heap.



Create a new, empty priority queue.

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

Add an item to the priority queue with the given weight. Weights are compared as strings (lexicographically), and default to item.


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


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.


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


Returns the number of items in the priority queue.


Returns all items in the heap, in an arbitrary order.


Returns all items in the heap, in weight order.

$pq->add_unordered($item, $weight)

Add an item to the priority queue or change its weight, without updating the heap structure. If you are adding a bunch of items at once, it may be more efficient to use add_unordered, then call $pq->restore_order() once you are done. Weight defaults to item.


Restore the heap structure after calling add_unordered. You need to do this before calling any of the ordered methods (add, peek, or get).


Strings are sorted in alphabetical order. If you want reverse order, use Array::Heap::PriorityQueue::Compare.




Bob Mathews <>



This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the LICENSE file included with this module.