PriorityQueue/String.pm

filter: Statementstmt 92.8% 39 / 42 Branchbran 40.0% 4 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.8% 13 / 14 Podpod 100% 10 / 10 totaltotal 86.8% 66 / 76 CC 6 SCAR 6.7
CC
6
Cov
83%
min_weight
6.0
add
2.0
peek
2.0
CRAP 2.0
« PriorityQueue/Numeric.pm ↑ Summary ModifiablePriorityQueue.pm »
1 package Array::Heap::PriorityQueue::String;
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_lex pop_heap_lex push_heap_lex );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Array::Heap::PriorityQueue::String - String-weighted priority queue
11  
12 =head1 SYNOPSIS
13  
14 use Array::Heap::PriorityQueue::String;
15 my $pq = Array::Heap::PriorityQueue::String->new();
16 $pq->add('fish', 'b');
17 $pq->add('banana', 'a');
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.
25  
26 Weights are strings, which are sorted in lexicographic order.
27  
28 This module is a wrapper around the *_heap_lex methods provided by
29 L<Array::Heap>.
30  
31 =head1 FUNCTIONS
32  
33 =over 4
34  
35 =item Array::Heap::PriorityQueue::String->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. Weights are
49 compared as strings (lexicographically), and default 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" unless defined $weight;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $weight 0 1 1
56 1 push_heap_lex @$self, [ $weight, $item ];
57 }
58  
59 =item $pq->peek()
60  
61 Return the first (lexicographically 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
BranchTrueFalseTotal
my $node = $self->[0] or return 1 0 1
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_lex @$self or return;
Branch: 1 branch
BranchTrueFalseTotal
my $node = &pop_heap_lex(\@$self) or return 1 0 1
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
BranchTrueFalseTotal
my $node = $self->[0] or return 0 0 0
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] cmp $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" unless defined $weight;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $weight 0 3 3
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_lex @$self;
157 }
158  
159 =back
160  
161 =head1 LIMITATIONS
162  
163 Strings are sorted in alphabetical order. If you want reverse order, use
164 L<Array::Heap::PriorityQueue::Compare>.
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 String.pm
« PriorityQueue/Numeric.pm ↑ Summary ModifiablePriorityQueue.pm »