List::GroupingPriorityQueue - priority queue with grouping
use List::GroupingPriorityQueue qw(grpriq_add grpriq_min_values); my $queue = []; grpriq_add($queue, 2, 'cat'); grpriq_add($queue, 4, 'dog'); grpriq_add($queue, 2, 'mlatu'); grpriq_min_values($queue); # ['cat', 'mlatu'] # fast iteration ($queue must not be modified mid-loop) for my $entry (@{$queue}) { my ($payload_r, $priority) = @{$entry}; ... } # OO my $pq = List::GroupingPriorityQueue->new; $pq->insert(2, 'cat'); $pq->insert(4, 'dog'); $pq->insert(2, 'mlatu'); $pq->pop; # ['cat', 'mlatu'] # slow iteration (but allows new items to be added) while (my $payload_r = $pq->pop) { ... } # or instead via $pq->each( sub { my ($payload_r, $priority) = @_; ... } );
This priority queue implementation provides grouping of elements with the same priority. With a traditional priority queue multiple "pop" calls would need to be made until the priority value changes; worse, some implementations do not return the priority value. That information would need to be encoded into and decoded from the payload under such implementations. This module instead considers payloads with the same priority as belonging to the same set and returns them together as an array reference. The priority information is available to the caller, if need be.
The following functions are available for export. An array reference qref is operated on. Modification of the qref in the calling code may break this module in unexpected ways.
Adds the given payload(s) to the queue qref. There is no return value.
Note that the order of arguments differs from other priority queue modules; the difference allows multiple payload elements to be added with a single call.
The priority should probably be an integer; floating point values are more likely to run into compiler or platform wonkiness should the queue be saved to disk and reloaded elsewhere.
Pulls the lowest priority [[payload,...],priority] array reference from qref, if any.
[[payload,...],priority]
Use this if you need the priority value for some calculation.
Pulls the lowest priority [payload,...] array reference from qref, if any. Equivalent to pop in the OO interface.
[payload,...]
Pulls the highest priority [[payload,...],priority] array reference from qref, if any.
Pulls the highest priority [payload,...] array reference from qref, if any.
A simple OO interface is also provided. This hides the qref but adds overhead.
Iterator that drains the queue by minimum value. The callback code reference is passed the payload array reference and priority value for each element in the queue.
Constructor.
Adds the payload(s) to the priority queue.
Returns the lowest priority [[payload],priority] array reference.
[[payload],priority]
Returns the payload (or payloads) with the lowest priority as an array reference. Alias: pop.
Returns the payload (or payloads) with the highest priority as an array reference.
Alias for min_values. Makes the interface similar to that of List::PriorityQueue.
<https://github.com/thrig/List-GroupingPriorityQueue>
List::PriorityQueue - what I used in Game::PlatformsOfPeril, and what this module is based on. Other priority queue modules on CPAN have different features that may suit particular needs better, see e.g. Array::Queue::Priority, Hash::PriorityQueue, and Queue::Priority, among others.
Music::RhythmSet makes use of this module to record changes in beat patterns across multiple musical voices.
Copyright 2021 Jeremy Mates
This program is distributed under the (Revised) BSD License: https://opensource.org/licenses/BSD-3-Clause
To install List::GroupingPriorityQueue, copy and paste the appropriate command in to your terminal.
cpanm
cpanm List::GroupingPriorityQueue
CPAN shell
perl -MCPAN -e shell install List::GroupingPriorityQueue
For more information on module installation, please visit the detailed CPAN module installation guide.