| 1 | package Array::Heap::PriorityQueue::Numeric; | ||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use vars qw( $VERSION ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | $VERSION = '1.10'; | |||||||||
| 6 | 1 | ▶ | use Array::Heap qw( make_heap pop_heap push_heap ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | =head1 NAME | ||||||||||
| 9 | |||||||||||
| 10 | Array::Heap::PriorityQueue::Numeric - Numeric priority queue | ||||||||||
| 11 | |||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||
| 13 | |||||||||||
| 14 | use Array::Heap::PriorityQueue::Numeric; | ||||||||||
| 15 | my $pq = Array::Heap::PriorityQueue::Numeric->new(); | ||||||||||
| 16 | $pq->add('fish', 42); | ||||||||||
| 17 | $pq->add('banana', 27); | ||||||||||
| 18 | print $pq->get(), "\n"; # banana | ||||||||||
| 19 | print $pq->peek(), "\n"; # fish | ||||||||||
| 20 | |||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||
| 22 | |||||||||||
| 23 | This module implements a priority queue, which is a data structure that can | ||||||||||
| 24 | efficiently locate the item with the lowest weight at any time. This is useful | ||||||||||
| 25 | for writing cost-minimizing and shortest-path algorithms. | ||||||||||
| 26 | |||||||||||
| 27 | Weights are numeric values. | ||||||||||
| 28 | |||||||||||
| 29 | This module is a wrapper for the *_heap methods provided by L<Array::Heap>. | ||||||||||
| 30 | |||||||||||
| 31 | =head1 FUNCTIONS | ||||||||||
| 32 | |||||||||||
| 33 | =over 4 | ||||||||||
| 34 | |||||||||||
| 35 | =item Array::Heap::PriorityQueue::Numeric->new() | ||||||||||
| 36 | |||||||||||
| 37 | Create a new, empty priority queue. | ||||||||||
| 38 | |||||||||||
| 39 | =cut | ||||||||||
| 40 | |||||||||||
| 41 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 42 | 1 | my ($class) = @_; | |||||||||
| 43 | 1 | return bless [ ] => $class; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | =item $pq->add($item, $weight) | ||||||||||
| 47 | |||||||||||
| 48 | Add an item to the priority queue with the given weight. Weight must be | ||||||||||
| 49 | numeric, and defaults to item. | ||||||||||
| 50 | |||||||||||
| 51 | =cut | ||||||||||
| 52 | |||||||||||
| 53 | 1 | ▶ | sub add { | ||||||||
|
Subroutine
add: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 54 | 1 | my ($self, $item, $weight) = @_; | |||||||||
| 55 | 1 | ▶ | $weight = $item + 0 unless defined $weight; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 1 | push_heap @$self, [ $weight, $item ]; | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | =item $pq->peek() | ||||||||||
| 60 | |||||||||||
| 61 | Return the first (numerically lowest weight) item from the queue. | ||||||||||
| 62 | Does not modify the queue. Returns undef if the queue is empty. | ||||||||||
| 63 | |||||||||||
| 64 | =cut | ||||||||||
| 65 | |||||||||||
| 66 | 1 | ▶ | sub peek { | ||||||||
|
Subroutine
peek: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 67 | 1 | my ($self) = @_; | |||||||||
| 68 | 1 | ▶ | my $node = $self->[0] or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 1 | return $node->[1]; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | =item $pq->get() | ||||||||||
| 73 | |||||||||||
| 74 | Removes the first item from the priority queue and returns it. | ||||||||||
| 75 | Returns undef if the queue is empty. If two items in the queue | ||||||||||
| 76 | have equal weight, this module makes no guarantee as to which | ||||||||||
| 77 | one will be returned first. | ||||||||||
| 78 | |||||||||||
| 79 | =cut | ||||||||||
| 80 | |||||||||||
| 81 | 1 | ▶ | sub get { | ||||||||
|
Subroutine
get: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 82 | 1 | my ($self) = @_; | |||||||||
| 83 | 1 | ▶ | my $node = pop_heap @$self or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 1 | return $node->[1]; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | =item $pq->min_weight() | ||||||||||
| 88 | |||||||||||
| 89 | Returns the weight of the lowest item in the queue, or undef if empty. | ||||||||||
| 90 | |||||||||||
| 91 | =cut | ||||||||||
| 92 | |||||||||||
| 93 | 0 | ▶ | sub min_weight { | ||||||||
|
Subroutine
min_weight: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 94 | 0 | my ($self) = @_; | |||||||||
| 95 | 0 | ▶ | my $node = $self->[0] or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 0 | return $node->[0]; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | =item $pq->size() | ||||||||||
| 100 | |||||||||||
| 101 | Returns the number of items in the priority queue. | ||||||||||
| 102 | |||||||||||
| 103 | =cut | ||||||||||
| 104 | |||||||||||
| 105 | 1 | ▶ | sub size { | ||||||||
|
Subroutine
size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 106 | 1 | my ($self) = @_; | |||||||||
| 107 | 1 | return scalar @$self; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | =item $pq->items() | ||||||||||
| 111 | |||||||||||
| 112 | Returns all items in the heap, in an arbitrary order. | ||||||||||
| 113 | |||||||||||
| 114 | =cut | ||||||||||
| 115 | |||||||||||
| 116 | 1 | ▶ | sub items { | ||||||||
|
Subroutine
items: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 117 | 1 | my ($self) = @_; | |||||||||
| 118 | 1 | return map { $_->[1] } @$self; | |||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | =item $pq->sorted_items() | ||||||||||
| 122 | |||||||||||
| 123 | Returns all items in the heap, in weight order. | ||||||||||
| 124 | |||||||||||
| 125 | =cut | ||||||||||
| 126 | |||||||||||
| 127 | 1 | ▶ | sub sorted_items { | ||||||||
|
Subroutine
sorted_items: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 128 | 1 | my ($self) = @_; | |||||||||
| 129 | 1 | return map { $_->[1] } sort { $a->[0] <=> $b->[0] } @$self; | |||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | =item $pq->add_unordered($item, $weight) | ||||||||||
| 133 | |||||||||||
| 134 | Add an item to the priority queue or change its weight, without updating | ||||||||||
| 135 | the heap structure. If you are adding a bunch of items at once, it may be | ||||||||||
| 136 | more efficient to use add_unordered, then call $pq->restore_order() once | ||||||||||
| 137 | you are done. Weight defaults to item. | ||||||||||
| 138 | |||||||||||
| 139 | =cut | ||||||||||
| 140 | |||||||||||
| 141 | 3 | ▶ | sub add_unordered { | ||||||||
|
Subroutine
add_unordered: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 142 | 3 | my ($self, $item, $weight) = @_; | |||||||||
| 143 | 3 | ▶ | $weight = $item + 0 unless defined $weight; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | 3 | push @$self, [ $weight, $item ]; | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | =item $pq->restore_order() | ||||||||||
| 148 | |||||||||||
| 149 | Restore the heap structure after calling add_unordered. You need to do this | ||||||||||
| 150 | before calling any of the ordered methods (add, peek, or get). | ||||||||||
| 151 | |||||||||||
| 152 | =cut | ||||||||||
| 153 | |||||||||||
| 154 | 1 | ▶ | sub restore_order { | ||||||||
|
Subroutine
restore_order: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 155 | 1 | my ($self) = @_; | |||||||||
| 156 | 1 | make_heap @$self; | |||||||||
| 157 | } | ||||||||||
| 158 | |||||||||||
| 159 | =back | ||||||||||
| 160 | |||||||||||
| 161 | =head1 LIMITATIONS | ||||||||||
| 162 | |||||||||||
| 163 | Weights are sorted in increasing order only. If you want it the other way, | ||||||||||
| 164 | use the negative of the weights you have. | ||||||||||
| 165 | |||||||||||
| 166 | =head1 SEE ALSO | ||||||||||
| 167 | |||||||||||
| 168 | L<Array::Heap::ModifiablePriorityQueue> | ||||||||||
| 169 | |||||||||||
| 170 | =head1 AUTHOR | ||||||||||
| 171 | |||||||||||
| 172 | Bob Mathews <bobmathews@alumni.calpoly.edu> | ||||||||||
| 173 | |||||||||||
| 174 | =head1 REPOSITORY | ||||||||||
| 175 | |||||||||||
| 176 | L<https://github.com/bobmath/ModifiablePriorityQueue> | ||||||||||
| 177 | |||||||||||
| 178 | =head1 COPYRIGHT | ||||||||||
| 179 | |||||||||||
| 180 | This program is free software; you can redistribute | ||||||||||
| 181 | it and/or modify it under the same terms as Perl itself. | ||||||||||
| 182 | |||||||||||
| 183 | The full text of the license can be found in the | ||||||||||
| 184 | LICENSE file included with this module. | ||||||||||
| 185 | |||||||||||
| 186 | =cut | ||||||||||
| 187 | |||||||||||
| 188 | 1 | 1 # end Numeric.pm | |||||||||