line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# List::Util::PP.pm |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Copyright (c) 1997-2009 Graham Barr . All rights reserved. |
4
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or |
5
|
|
|
|
|
|
|
# modify it under the same terms as Perl itself. |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
package List::Util::PP; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use strict; |
10
|
|
|
|
|
|
|
use warnings; |
11
|
|
|
|
|
|
|
require Exporter; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
14
|
|
|
|
|
|
|
first min max minstr maxstr reduce sum shuffle |
15
|
|
|
|
|
|
|
all any none notall product sum0 uniq uniqnum uniqstr |
16
|
|
|
|
|
|
|
pairs unpairs pairkeys pairvalues pairmap pairgrep pairfirst |
17
|
|
|
|
|
|
|
); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our $VERSION = "1.46_01"; |
20
|
|
|
|
|
|
|
$VERSION = eval $VERSION; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub import { |
23
|
0
|
|
|
0
|
|
|
my $pkg = caller; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# (RT88848) Touch the caller's $a and $b, to avoid the warning of |
26
|
|
|
|
|
|
|
# Name "main::a" used only once: possible typo" warning |
27
|
|
|
|
|
|
|
no strict 'refs'; |
28
|
0
|
|
|
|
|
|
${"${pkg}::a"} = ${"${pkg}::a"}; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
29
|
0
|
|
|
|
|
|
${"${pkg}::b"} = ${"${pkg}::b"}; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
0
|
|
|
|
|
|
goto &Exporter::import; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub reduce (&@) { |
35
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
36
|
0
|
0
|
0
|
|
|
|
unless ( ref $f && eval { \&$f } ) { |
|
0
|
|
|
|
|
|
|
37
|
0
|
|
|
|
|
|
require Carp; |
38
|
0
|
|
|
|
|
|
Carp::croak("Not a subroutine reference"); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
0
|
0
|
|
|
|
|
return shift unless @_ > 1; |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
my $pkg = caller; |
44
|
0
|
|
|
|
|
|
my $a = shift; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
no strict 'refs'; |
47
|
0
|
|
|
|
|
|
local *{"${pkg}::a"} = \$a; |
|
0
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
my $glob_b = \*{"${pkg}::b"}; |
|
0
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
foreach my $b (@_) { |
51
|
0
|
|
|
|
|
|
local *$glob_b = \$b; |
52
|
0
|
|
|
|
|
|
$a = $f->(); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
$a; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub first (&@) { |
59
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
60
|
0
|
0
|
0
|
|
|
|
unless ( ref $f && eval { \&$f } ) { |
|
0
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
require Carp; |
62
|
0
|
|
|
|
|
|
Carp::croak("Not a subroutine reference"); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
$f->() and return $_ |
66
|
0
|
|
0
|
|
|
|
foreach @_; |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
undef; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub sum (@) { |
72
|
0
|
0
|
|
0
|
0
|
|
return undef unless @_; |
73
|
0
|
|
|
|
|
|
my $s = 0; |
74
|
0
|
|
|
|
|
|
$s += $_ foreach @_; |
75
|
0
|
|
|
|
|
|
return $s; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub min (@) { |
79
|
0
|
0
|
|
0
|
0
|
|
return undef unless @_; |
80
|
0
|
|
|
|
|
|
my $min = shift; |
81
|
|
|
|
|
|
|
$_ < $min and $min = $_ |
82
|
0
|
|
0
|
|
|
|
foreach @_; |
83
|
0
|
|
|
|
|
|
return $min; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub max (@) { |
87
|
0
|
0
|
|
0
|
0
|
|
return undef unless @_; |
88
|
0
|
|
|
|
|
|
my $max = shift; |
89
|
|
|
|
|
|
|
$_ > $max and $max = $_ |
90
|
0
|
|
0
|
|
|
|
foreach @_; |
91
|
0
|
|
|
|
|
|
return $max; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub minstr (@) { |
95
|
0
|
0
|
|
0
|
0
|
|
return undef unless @_; |
96
|
0
|
|
|
|
|
|
my $min = shift; |
97
|
|
|
|
|
|
|
$_ lt $min and $min = $_ |
98
|
0
|
|
0
|
|
|
|
foreach @_; |
99
|
0
|
|
|
|
|
|
return $min; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub maxstr (@) { |
103
|
0
|
0
|
|
0
|
0
|
|
return undef unless @_; |
104
|
0
|
|
|
|
|
|
my $max = shift; |
105
|
|
|
|
|
|
|
$_ gt $max and $max = $_ |
106
|
0
|
|
0
|
|
|
|
foreach @_; |
107
|
0
|
|
|
|
|
|
return $max; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub shuffle (@) { |
111
|
0
|
|
|
0
|
0
|
|
my @a=\(@_); |
112
|
0
|
|
|
|
|
|
my $n; |
113
|
0
|
|
|
|
|
|
my $i=@_; |
114
|
|
|
|
|
|
|
map { |
115
|
0
|
|
|
|
|
|
$n = rand($i--); |
|
0
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
(${$a[$n]}, $a[$n] = $a[$i])[0]; |
|
0
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
} @_; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub all (&@) { |
121
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
122
|
|
|
|
|
|
|
$f->() or return 0 |
123
|
0
|
|
0
|
|
|
|
foreach @_; |
124
|
0
|
|
|
|
|
|
return 1; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub any (&@) { |
128
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
129
|
|
|
|
|
|
|
$f->() and return 1 |
130
|
0
|
|
0
|
|
|
|
foreach @_; |
131
|
0
|
|
|
|
|
|
return 0; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub none (&@) { |
135
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
136
|
|
|
|
|
|
|
$f->() and return 0 |
137
|
0
|
|
0
|
|
|
|
foreach @_; |
138
|
0
|
|
|
|
|
|
return 1; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub notall (&@) { |
142
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
143
|
|
|
|
|
|
|
$f->() or return 1 |
144
|
0
|
|
0
|
|
|
|
foreach @_; |
145
|
0
|
|
|
|
|
|
return 0; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub product (@) { |
149
|
0
|
|
|
0
|
0
|
|
my $p = 1; |
150
|
0
|
|
|
|
|
|
$p *= $_ foreach @_; |
151
|
0
|
|
|
|
|
|
return $p; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub sum0 (@) { |
155
|
0
|
|
|
0
|
0
|
|
my $s = 0; |
156
|
0
|
|
|
|
|
|
$s += $_ foreach @_; |
157
|
0
|
|
|
|
|
|
return $s; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub pairs (@) { |
161
|
0
|
0
|
|
0
|
0
|
|
if (@_ % 2) { |
162
|
0
|
|
|
|
|
|
warnings::warnif('misc', 'Odd number of elements in pairs'); |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
return |
166
|
0
|
|
|
|
|
|
map { bless [ @_[$_, $_ + 1] ], 'List::Util::PP::_Pair' } |
|
0
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
map $_*2, |
168
|
|
|
|
|
|
|
0 .. int($#_/2); |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub unpairs (@) { |
172
|
0
|
|
|
0
|
0
|
|
map @{$_}[0,1], @_; |
|
0
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
sub pairkeys (@) { |
176
|
0
|
0
|
|
0
|
0
|
|
if (@_ % 2) { |
177
|
0
|
|
|
|
|
|
warnings::warnif('misc', 'Odd number of elements in pairkeys'); |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
return |
181
|
0
|
|
|
|
|
|
map $_[$_*2], |
182
|
|
|
|
|
|
|
0 .. int($#_/2); |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
sub pairvalues (@) { |
186
|
0
|
0
|
|
0
|
0
|
|
if (@_ % 2) { |
187
|
0
|
|
|
|
|
|
require Carp; |
188
|
0
|
|
|
|
|
|
warnings::warnif('misc', 'Odd number of elements in pairvalues'); |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
return |
192
|
0
|
|
|
|
|
|
map $_[$_*2 + 1], |
193
|
|
|
|
|
|
|
0 .. int($#_/2); |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
sub pairmap (&@) { |
197
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
198
|
0
|
0
|
|
|
|
|
if (@_ % 2) { |
199
|
0
|
|
|
|
|
|
warnings::warnif('misc', 'Odd number of elements in pairmap'); |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
0
|
|
|
|
|
|
my $pkg = caller; |
203
|
|
|
|
|
|
|
no strict 'refs'; |
204
|
0
|
|
|
|
|
|
my $glob_a = \*{"${pkg}::a"}; |
|
0
|
|
|
|
|
|
|
205
|
0
|
|
|
|
|
|
my $glob_b = \*{"${pkg}::b"}; |
|
0
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
return |
208
|
|
|
|
|
|
|
map { |
209
|
0
|
|
|
|
|
|
local (*$glob_a, *$glob_b) = \( @_[$_,$_+1] ); |
|
0
|
|
|
|
|
|
|
210
|
0
|
|
|
|
|
|
$f->(); |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
map $_*2, |
213
|
|
|
|
|
|
|
0 .. int($#_/2); |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
sub pairgrep (&@) { |
217
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
218
|
0
|
0
|
|
|
|
|
if (@_ % 2) { |
219
|
0
|
|
|
|
|
|
warnings::warnif('misc', 'Odd number of elements in pairgrep'); |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
|
222
|
0
|
|
|
|
|
|
my $pkg = caller; |
223
|
|
|
|
|
|
|
no strict 'refs'; |
224
|
0
|
|
|
|
|
|
my $glob_a = \*{"${pkg}::a"}; |
|
0
|
|
|
|
|
|
|
225
|
0
|
|
|
|
|
|
my $glob_b = \*{"${pkg}::b"}; |
|
0
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
return |
228
|
|
|
|
|
|
|
map { |
229
|
0
|
|
|
|
|
|
local (*$glob_a, *$glob_b) = \( @_[$_,$_+1] ); |
|
0
|
|
|
|
|
|
|
230
|
0
|
0
|
|
|
|
|
$f->() ? (wantarray ? @_[$_,$_+1] : 1) : (); |
|
|
0
|
|
|
|
|
|
231
|
|
|
|
|
|
|
} |
232
|
|
|
|
|
|
|
map $_*2, |
233
|
|
|
|
|
|
|
0 .. int ($#_/2); |
234
|
|
|
|
|
|
|
} |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
sub pairfirst (&@) { |
237
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
238
|
0
|
0
|
|
|
|
|
if (@_ % 2) { |
239
|
0
|
|
|
|
|
|
warnings::warnif('misc', 'Odd number of elements in pairfirst'); |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
0
|
|
|
|
|
|
my $pkg = caller; |
243
|
|
|
|
|
|
|
no strict 'refs'; |
244
|
0
|
|
|
|
|
|
my $glob_a = \*{"${pkg}::a"}; |
|
0
|
|
|
|
|
|
|
245
|
0
|
|
|
|
|
|
my $glob_b = \*{"${pkg}::b"}; |
|
0
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
|
247
|
0
|
|
|
|
|
|
foreach my $i (map $_*2, 0 .. int($#_/2)) { |
248
|
0
|
|
|
|
|
|
local (*$glob_a, *$glob_b) = \( @_[$i,$i+1] ); |
249
|
0
|
0
|
|
|
|
|
return wantarray ? @_[$i,$i+1] : 1 |
|
|
0
|
|
|
|
|
|
250
|
|
|
|
|
|
|
if $f->(); |
251
|
|
|
|
|
|
|
} |
252
|
0
|
|
|
|
|
|
return (); |
253
|
|
|
|
|
|
|
} |
254
|
|
|
|
|
|
|
|
255
|
0
|
|
|
0
|
|
|
sub List::Util::PP::_Pair::key { $_[0][0] } |
256
|
0
|
|
|
0
|
|
|
sub List::Util::PP::_Pair::value { $_[0][1] } |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
sub uniq { |
259
|
0
|
|
|
0
|
0
|
|
my %seen; |
260
|
|
|
|
|
|
|
my $undef; |
261
|
0
|
0
|
|
|
|
|
my @uniq = grep defined($_) ? !$seen{$_}++ : !$undef++, @_; |
262
|
0
|
|
|
|
|
|
@uniq; |
263
|
|
|
|
|
|
|
} |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
sub uniqnum { |
266
|
0
|
|
|
0
|
0
|
|
my %seen; |
267
|
|
|
|
|
|
|
my @uniq = |
268
|
|
|
|
|
|
|
grep !$seen{(eval { pack "J", $_ }||'') . pack "F", $_}++, |
269
|
|
|
|
|
|
|
map +(defined($_) ? $_ |
270
|
0
|
0
|
0
|
|
|
|
: do { warnings::warnif('uninitialized', 'Use of uninitialized value in subroutine entry'); 0 }), |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
@_; |
272
|
0
|
|
|
|
|
|
@uniq; |
273
|
|
|
|
|
|
|
} |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
sub uniqstr { |
276
|
0
|
|
|
0
|
0
|
|
my %seen; |
277
|
|
|
|
|
|
|
my @uniq = |
278
|
|
|
|
|
|
|
grep !$seen{$_}++, |
279
|
|
|
|
|
|
|
map +(defined($_) ? $_ |
280
|
0
|
0
|
|
|
|
|
: do { warnings::warnif('uninitialized', 'Use of uninitialized value in subroutine entry'); '' }), |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
@_; |
282
|
0
|
|
|
|
|
|
@uniq; |
283
|
|
|
|
|
|
|
} |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
1; |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
__END__ |