| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Array::Heap::PriorityQueue::String; |
|
2
|
1
|
|
|
1
|
|
15890
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
27
|
|
|
3
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
27
|
|
|
4
|
1
|
|
|
1
|
|
3
|
use vars qw( $VERSION ); |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
67
|
|
|
5
|
|
|
|
|
|
|
$VERSION = '1.10'; |
|
6
|
1
|
|
|
1
|
|
498
|
use Array::Heap qw( make_heap_lex pop_heap_lex push_heap_lex ); |
|
|
1
|
|
|
|
|
506
|
|
|
|
1
|
|
|
|
|
444
|
|
|
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. |
|
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
|
|
|
|
|
|
|
sub new { |
|
42
|
1
|
|
|
1
|
1
|
18
|
my ($class) = @_; |
|
43
|
1
|
|
|
|
|
5
|
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
|
|
|
|
|
|
|
sub add { |
|
54
|
1
|
|
|
1
|
1
|
4
|
my ($self, $item, $weight) = @_; |
|
55
|
1
|
50
|
|
|
|
5
|
$weight = "$item" unless defined $weight; |
|
56
|
1
|
|
|
|
|
8
|
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
|
|
|
|
|
|
|
sub peek { |
|
67
|
1
|
|
|
1
|
1
|
3
|
my ($self) = @_; |
|
68
|
1
|
50
|
|
|
|
8
|
my $node = $self->[0] or return; |
|
69
|
1
|
|
|
|
|
6
|
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
|
|
|
|
|
|
|
sub get { |
|
82
|
1
|
|
|
1
|
1
|
4
|
my ($self) = @_; |
|
83
|
1
|
50
|
|
|
|
16
|
my $node = pop_heap_lex @$self or return; |
|
84
|
1
|
|
|
|
|
6
|
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
|
|
|
|
|
|
|
sub min_weight { |
|
94
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
|
95
|
0
|
0
|
|
|
|
0
|
my $node = $self->[0] or return; |
|
96
|
0
|
|
|
|
|
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
|
|
|
|
|
|
|
sub size { |
|
106
|
1
|
|
|
1
|
1
|
7
|
my ($self) = @_; |
|
107
|
1
|
|
|
|
|
7
|
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
|
|
|
|
|
|
|
sub items { |
|
117
|
1
|
|
|
1
|
1
|
3
|
my ($self) = @_; |
|
118
|
1
|
|
|
|
|
3
|
return map { $_->[1] } @$self; |
|
|
3
|
|
|
|
|
16
|
|
|
119
|
|
|
|
|
|
|
} |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=item $pq->sorted_items() |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Returns all items in the heap, in weight order. |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=cut |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub sorted_items { |
|
128
|
1
|
|
|
1
|
1
|
3
|
my ($self) = @_; |
|
129
|
1
|
|
|
|
|
4
|
return map { $_->[1] } sort { $a->[0] cmp $b->[0] } @$self; |
|
|
3
|
|
|
|
|
8
|
|
|
|
3
|
|
|
|
|
6
|
|
|
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
|
|
|
|
|
|
|
sub add_unordered { |
|
142
|
3
|
|
|
3
|
1
|
974
|
my ($self, $item, $weight) = @_; |
|
143
|
3
|
50
|
|
|
|
9
|
$weight = "$item" unless defined $weight; |
|
144
|
3
|
|
|
|
|
52
|
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
|
|
|
|
|
|
|
sub restore_order { |
|
155
|
1
|
|
|
1
|
1
|
5
|
my ($self) = @_; |
|
156
|
1
|
|
|
|
|
9
|
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. |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
L |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 AUTHOR |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Bob Mathews |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head1 REPOSITORY |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
L |
|
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 # end String.pm |