line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package List::MoreUtils::PP; |
2
|
|
|
|
|
|
|
|
3
|
118
|
|
|
118
|
|
2519
|
use 5.008_001; |
|
118
|
|
|
|
|
503
|
|
4
|
118
|
|
|
118
|
|
831
|
use strict; |
|
118
|
|
|
|
|
280
|
|
|
118
|
|
|
|
|
3523
|
|
5
|
118
|
|
|
118
|
|
865
|
use warnings; |
|
118
|
|
|
|
|
362
|
|
|
118
|
|
|
|
|
85125
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.427_001'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=pod |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
List::MoreUtils::PP - Provide List::MoreUtils pure Perl implementation |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
BEGIN { $ENV{LIST_MOREUTILS_PP} = 1; } |
18
|
|
|
|
|
|
|
use List::MoreUtils qw(:all); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=cut |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub any (&@) |
23
|
|
|
|
|
|
|
{ |
24
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
25
|
0
|
|
|
|
|
|
foreach (@_) |
26
|
|
|
|
|
|
|
{ |
27
|
0
|
0
|
|
|
|
|
return 1 if $f->(); |
28
|
|
|
|
|
|
|
} |
29
|
0
|
|
|
|
|
|
return 0; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub all (&@) |
33
|
|
|
|
|
|
|
{ |
34
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
35
|
0
|
|
|
|
|
|
foreach (@_) |
36
|
|
|
|
|
|
|
{ |
37
|
0
|
0
|
|
|
|
|
return 0 unless $f->(); |
38
|
|
|
|
|
|
|
} |
39
|
0
|
|
|
|
|
|
return 1; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub none (&@) |
43
|
|
|
|
|
|
|
{ |
44
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
45
|
0
|
|
|
|
|
|
foreach (@_) |
46
|
|
|
|
|
|
|
{ |
47
|
0
|
0
|
|
|
|
|
return 0 if $f->(); |
48
|
|
|
|
|
|
|
} |
49
|
0
|
|
|
|
|
|
return 1; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub notall (&@) |
53
|
|
|
|
|
|
|
{ |
54
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
55
|
0
|
|
|
|
|
|
foreach (@_) |
56
|
|
|
|
|
|
|
{ |
57
|
0
|
0
|
|
|
|
|
return 1 unless $f->(); |
58
|
|
|
|
|
|
|
} |
59
|
0
|
|
|
|
|
|
return 0; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub one (&@) |
63
|
|
|
|
|
|
|
{ |
64
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
65
|
0
|
|
|
|
|
|
my $found = 0; |
66
|
0
|
|
|
|
|
|
foreach (@_) |
67
|
|
|
|
|
|
|
{ |
68
|
0
|
0
|
0
|
|
|
|
$f->() and $found++ and return 0; |
69
|
|
|
|
|
|
|
} |
70
|
0
|
|
|
|
|
|
$found; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub any_u (&@) |
74
|
|
|
|
|
|
|
{ |
75
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
76
|
0
|
0
|
|
|
|
|
return if !@_; |
77
|
0
|
|
0
|
|
|
|
$f->() and return 1 foreach (@_); |
78
|
0
|
|
|
|
|
|
return 0; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub all_u (&@) |
82
|
|
|
|
|
|
|
{ |
83
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
84
|
0
|
0
|
|
|
|
|
return if !@_; |
85
|
0
|
|
0
|
|
|
|
$f->() or return 0 foreach (@_); |
86
|
0
|
|
|
|
|
|
return 1; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub none_u (&@) |
90
|
|
|
|
|
|
|
{ |
91
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
92
|
0
|
0
|
|
|
|
|
return if !@_; |
93
|
0
|
|
0
|
|
|
|
$f->() and return 0 foreach (@_); |
94
|
0
|
|
|
|
|
|
return 1; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub notall_u (&@) |
98
|
|
|
|
|
|
|
{ |
99
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
100
|
0
|
0
|
|
|
|
|
return if !@_; |
101
|
0
|
|
0
|
|
|
|
$f->() or return 1 foreach (@_); |
102
|
0
|
|
|
|
|
|
return 0; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub one_u (&@) |
106
|
|
|
|
|
|
|
{ |
107
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
108
|
0
|
0
|
|
|
|
|
return if !@_; |
109
|
0
|
|
|
|
|
|
my $found = 0; |
110
|
0
|
|
|
|
|
|
foreach (@_) |
111
|
|
|
|
|
|
|
{ |
112
|
0
|
0
|
0
|
|
|
|
$f->() and $found++ and return 0; |
113
|
|
|
|
|
|
|
} |
114
|
0
|
|
|
|
|
|
$found; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub reduce_u(&@) |
118
|
|
|
|
|
|
|
{ |
119
|
0
|
|
|
0
|
0
|
|
my $code = shift; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# Localise $a, $b |
122
|
|
|
|
|
|
|
my ($caller_a, $caller_b) = do |
123
|
0
|
|
|
|
|
|
{ |
124
|
0
|
|
|
|
|
|
my $pkg = caller(); |
125
|
118
|
|
|
118
|
|
1111
|
no strict 'refs'; |
|
118
|
|
|
|
|
318
|
|
|
118
|
|
|
|
|
48220
|
|
126
|
0
|
|
|
|
|
|
\*{$pkg . '::a'}, \*{$pkg . '::b'}; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
}; |
128
|
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
|
local (*$caller_a, *$caller_b); |
130
|
0
|
|
|
|
|
|
*$caller_a = \(); |
131
|
0
|
|
|
|
|
|
for (0 .. $#_) |
132
|
|
|
|
|
|
|
{ |
133
|
0
|
|
|
|
|
|
*$caller_b = \$_[$_]; |
134
|
0
|
|
|
|
|
|
*$caller_a = \($code->()); |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
0
|
|
|
|
|
|
${*$caller_a}; |
|
0
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub reduce_0(&@) |
141
|
|
|
|
|
|
|
{ |
142
|
0
|
|
|
0
|
0
|
|
my $code = shift; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
# Localise $a, $b |
145
|
|
|
|
|
|
|
my ($caller_a, $caller_b) = do |
146
|
0
|
|
|
|
|
|
{ |
147
|
0
|
|
|
|
|
|
my $pkg = caller(); |
148
|
118
|
|
|
118
|
|
3872
|
no strict 'refs'; |
|
118
|
|
|
|
|
386
|
|
|
118
|
|
|
|
|
22665
|
|
149
|
0
|
|
|
|
|
|
\*{$pkg . '::a'}, \*{$pkg . '::b'}; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
}; |
151
|
|
|
|
|
|
|
|
152
|
0
|
|
|
|
|
|
local (*$caller_a, *$caller_b); |
153
|
0
|
|
|
|
|
|
*$caller_a = \0; |
154
|
0
|
|
|
|
|
|
for (0 .. $#_) |
155
|
|
|
|
|
|
|
{ |
156
|
0
|
|
|
|
|
|
*$caller_b = \$_[$_]; |
157
|
0
|
|
|
|
|
|
*$caller_a = \($code->()); |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
0
|
|
|
|
|
|
${*$caller_a}; |
|
0
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub reduce_1(&@) |
164
|
|
|
|
|
|
|
{ |
165
|
0
|
|
|
0
|
0
|
|
my $code = shift; |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
# Localise $a, $b |
168
|
|
|
|
|
|
|
my ($caller_a, $caller_b) = do |
169
|
0
|
|
|
|
|
|
{ |
170
|
0
|
|
|
|
|
|
my $pkg = caller(); |
171
|
118
|
|
|
118
|
|
963
|
no strict 'refs'; |
|
118
|
|
|
|
|
363
|
|
|
118
|
|
|
|
|
200545
|
|
172
|
0
|
|
|
|
|
|
\*{$pkg . '::a'}, \*{$pkg . '::b'}; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
}; |
174
|
|
|
|
|
|
|
|
175
|
0
|
|
|
|
|
|
local (*$caller_a, *$caller_b); |
176
|
0
|
|
|
|
|
|
*$caller_a = \1; |
177
|
0
|
|
|
|
|
|
for (0 .. $#_) |
178
|
|
|
|
|
|
|
{ |
179
|
0
|
|
|
|
|
|
*$caller_b = \$_[$_]; |
180
|
0
|
|
|
|
|
|
*$caller_a = \($code->()); |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
0
|
|
|
|
|
|
${*$caller_a}; |
|
0
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
sub true (&@) |
187
|
|
|
|
|
|
|
{ |
188
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
189
|
0
|
|
|
|
|
|
my $count = 0; |
190
|
0
|
|
0
|
|
|
|
$f->() and ++$count foreach (@_); |
191
|
0
|
|
|
|
|
|
return $count; |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
sub false (&@) |
195
|
|
|
|
|
|
|
{ |
196
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
197
|
0
|
|
|
|
|
|
my $count = 0; |
198
|
0
|
|
0
|
|
|
|
$f->() or ++$count foreach (@_); |
199
|
0
|
|
|
|
|
|
return $count; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
sub firstidx (&@) |
203
|
|
|
|
|
|
|
{ |
204
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
205
|
0
|
|
|
|
|
|
foreach my $i (0 .. $#_) |
206
|
|
|
|
|
|
|
{ |
207
|
0
|
|
|
|
|
|
local *_ = \$_[$i]; |
208
|
0
|
0
|
|
|
|
|
return $i if $f->(); |
209
|
|
|
|
|
|
|
} |
210
|
0
|
|
|
|
|
|
return -1; |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
sub firstval (&@) |
214
|
|
|
|
|
|
|
{ |
215
|
0
|
|
|
0
|
0
|
|
my $test = shift; |
216
|
0
|
|
|
|
|
|
foreach (@_) |
217
|
|
|
|
|
|
|
{ |
218
|
0
|
0
|
|
|
|
|
return $_ if $test->(); |
219
|
|
|
|
|
|
|
} |
220
|
0
|
|
|
|
|
|
return undef; |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
sub firstres (&@) |
224
|
|
|
|
|
|
|
{ |
225
|
0
|
|
|
0
|
0
|
|
my $test = shift; |
226
|
0
|
|
|
|
|
|
foreach (@_) |
227
|
|
|
|
|
|
|
{ |
228
|
0
|
|
|
|
|
|
my $testval = $test->(); |
229
|
0
|
0
|
|
|
|
|
$testval and return $testval; |
230
|
|
|
|
|
|
|
} |
231
|
0
|
|
|
|
|
|
return undef; |
232
|
|
|
|
|
|
|
} |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
sub onlyidx (&@) |
235
|
|
|
|
|
|
|
{ |
236
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
237
|
0
|
|
|
|
|
|
my $found; |
238
|
0
|
|
|
|
|
|
foreach my $i (0 .. $#_) |
239
|
|
|
|
|
|
|
{ |
240
|
0
|
|
|
|
|
|
local *_ = \$_[$i]; |
241
|
0
|
0
|
|
|
|
|
$f->() or next; |
242
|
0
|
0
|
|
|
|
|
defined $found and return -1; |
243
|
0
|
|
|
|
|
|
$found = $i; |
244
|
|
|
|
|
|
|
} |
245
|
0
|
0
|
|
|
|
|
return defined $found ? $found : -1; |
246
|
|
|
|
|
|
|
} |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
sub onlyval (&@) |
249
|
|
|
|
|
|
|
{ |
250
|
0
|
|
|
0
|
0
|
|
my $test = shift; |
251
|
0
|
|
|
|
|
|
my $result = undef; |
252
|
0
|
|
|
|
|
|
my $found = 0; |
253
|
0
|
|
|
|
|
|
foreach (@_) |
254
|
|
|
|
|
|
|
{ |
255
|
0
|
0
|
|
|
|
|
$test->() or next; |
256
|
0
|
|
|
|
|
|
$result = $_; |
257
|
0
|
0
|
|
|
|
|
$found++ and return undef; |
258
|
|
|
|
|
|
|
} |
259
|
0
|
|
|
|
|
|
return $result; |
260
|
|
|
|
|
|
|
} |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
sub onlyres (&@) |
263
|
|
|
|
|
|
|
{ |
264
|
0
|
|
|
0
|
0
|
|
my $test = shift; |
265
|
0
|
|
|
|
|
|
my $result = undef; |
266
|
0
|
|
|
|
|
|
my $found = 0; |
267
|
0
|
|
|
|
|
|
foreach (@_) |
268
|
|
|
|
|
|
|
{ |
269
|
0
|
0
|
|
|
|
|
my $rv = $test->() or next; |
270
|
0
|
|
|
|
|
|
$result = $rv; |
271
|
0
|
0
|
|
|
|
|
$found++ and return undef; |
272
|
|
|
|
|
|
|
} |
273
|
0
|
0
|
|
|
|
|
return $found ? $result : undef; |
274
|
|
|
|
|
|
|
} |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
sub lastidx (&@) |
277
|
|
|
|
|
|
|
{ |
278
|
0
|
|
|
0
|
0
|
|
my $f = shift; |
279
|
0
|
|
|
|
|
|
foreach my $i (reverse 0 .. $#_) |
280
|
|
|
|
|
|
|
{ |
281
|
0
|
|
|
|
|
|
local *_ = \$_[$i]; |
282
|
0
|
0
|
|
|
|
|
return $i if $f->(); |
283
|
|
|
|
|
|
|
} |
284
|
0
|
|
|
|
|
|
return -1; |
285
|
|
|
|
|
|
|
} |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
sub lastval (&@) |
288
|
|
|
|
|
|
|
{ |
289
|
0
|
|
|
0
|
0
|
|
my $test = shift; |
290
|
0
|
|
|
|
|
|
my $ix; |
291
|
0
|
|
|
|
|
|
for ($ix = $#_; $ix >= 0; $ix--) |
292
|
|
|
|
|
|
|
{ |
293
|
0
|
|
|
|
|
|
local *_ = \$_[$ix]; |
294
|
0
|
|
|
|
|
|
my $testval = $test->(); |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
# Simulate $_ as alias |
297
|
0
|
|
|
|
|
|
$_[$ix] = $_; |
298
|
0
|
0
|
|
|
|
|
return $_ if $testval; |
299
|
|
|
|
|
|
|
} |
300
|
0
|
|
|
|
|
|
return undef; |
301
|
|
|
|
|
|
|
} |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
sub lastres (&@) |
304
|
|
|
|
|
|
|
{ |
305
|
0
|
|
|
0
|
0
|
|
my $test = shift; |
306
|
0
|
|
|
|
|
|
my $ix; |
307
|
0
|
|
|
|
|
|
for ($ix = $#_; $ix >= 0; $ix--) |
308
|
|
|
|
|
|
|
{ |
309
|
0
|
|
|
|
|
|
local *_ = \$_[$ix]; |
310
|
0
|
|
|
|
|
|
my $testval = $test->(); |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
# Simulate $_ as alias |
313
|
0
|
|
|
|
|
|
$_[$ix] = $_; |
314
|
0
|
0
|
|
|
|
|
return $testval if $testval; |
315
|
|
|
|
|
|
|
} |
316
|
0
|
|
|
|
|
|
return undef; |
317
|
|
|
|
|
|
|
} |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
sub insert_after (&$\@) |
320
|
|
|
|
|
|
|
{ |
321
|
0
|
|
|
0
|
0
|
|
my ($f, $val, $list) = @_; |
322
|
0
|
|
|
|
|
|
my $c = &firstidx($f, @$list); |
323
|
0
|
0
|
0
|
|
|
|
@$list = (@{$list}[0 .. $c], $val, @{$list}[$c + 1 .. $#$list],) and return 1 if $c != -1; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
324
|
0
|
|
|
|
|
|
return 0; |
325
|
|
|
|
|
|
|
} |
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
sub insert_after_string ($$\@) |
328
|
|
|
|
|
|
|
{ |
329
|
0
|
|
|
0
|
0
|
|
my ($string, $val, $list) = @_; |
330
|
0
|
0
|
|
0
|
|
|
my $c = firstidx { defined $_ and $string eq $_ } @$list; |
|
0
|
|
|
|
|
|
|
331
|
0
|
0
|
0
|
|
|
|
@$list = (@{$list}[0 .. $c], $val, @{$list}[$c + 1 .. $#$list],) and return 1 if $c != -1; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
332
|
0
|
|
|
|
|
|
return 0; |
333
|
|
|
|
|
|
|
} |
334
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
sub apply (&@) |
336
|
|
|
|
|
|
|
{ |
337
|
0
|
|
|
0
|
0
|
|
my $action = shift; |
338
|
0
|
|
|
|
|
|
&$action foreach my @values = @_; |
339
|
0
|
0
|
|
|
|
|
wantarray ? @values : $values[-1]; |
340
|
|
|
|
|
|
|
} |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
sub after (&@) |
343
|
|
|
|
|
|
|
{ |
344
|
0
|
|
|
0
|
0
|
|
my $test = shift; |
345
|
0
|
|
|
|
|
|
my $started; |
346
|
|
|
|
|
|
|
my $lag; |
347
|
|
|
|
|
|
|
grep $started ||= do |
348
|
0
|
|
0
|
|
|
|
{ |
349
|
0
|
|
|
|
|
|
my $x = $lag; |
350
|
0
|
|
|
|
|
|
$lag = $test->(); |
351
|
0
|
|
|
|
|
|
$x; |
352
|
|
|
|
|
|
|
}, @_; |
353
|
|
|
|
|
|
|
} |
354
|
|
|
|
|
|
|
|
355
|
|
|
|
|
|
|
sub after_incl (&@) |
356
|
|
|
|
|
|
|
{ |
357
|
0
|
|
|
0
|
0
|
|
my $test = shift; |
358
|
0
|
|
|
|
|
|
my $started; |
359
|
0
|
|
0
|
|
|
|
grep $started ||= $test->(), @_; |
360
|
|
|
|
|
|
|
} |
361
|
|
|
|
|
|
|
|
362
|
|
|
|
|
|
|
sub before (&@) |
363
|
|
|
|
|
|
|
{ |
364
|
0
|
|
|
0
|
0
|
|
my $test = shift; |
365
|
0
|
|
|
|
|
|
my $more = 1; |
366
|
0
|
|
0
|
|
|
|
grep $more &&= !$test->(), @_; |
367
|
|
|
|
|
|
|
} |
368
|
|
|
|
|
|
|
|
369
|
|
|
|
|
|
|
sub before_incl (&@) |
370
|
|
|
|
|
|
|
{ |
371
|
0
|
|
|
0
|
0
|
|
my $test = shift; |
372
|
0
|
|
|
|
|
|
my $more = 1; |
373
|
0
|
|
|
|
|
|
my $lag = 1; |
374
|
|
|
|
|
|
|
grep $more &&= do |
375
|
0
|
|
0
|
|
|
|
{ |
376
|
0
|
|
|
|
|
|
my $x = $lag; |
377
|
0
|
|
|
|
|
|
$lag = !$test->(); |
378
|
0
|
|
|
|
|
|
$x; |
379
|
|
|
|
|
|
|
}, @_; |
380
|
|
|
|
|
|
|
} |
381
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
sub indexes (&@) |
383
|
|
|
|
|
|
|
{ |
384
|
0
|
|
|
0
|
0
|
|
my $test = shift; |
385
|
|
|
|
|
|
|
grep { |
386
|
0
|
|
|
|
|
|
local *_ = \$_[$_]; |
|
0
|
|
|
|
|
|
|
387
|
0
|
|
|
|
|
|
$test->() |
388
|
|
|
|
|
|
|
} 0 .. $#_; |
389
|
|
|
|
|
|
|
} |
390
|
|
|
|
|
|
|
|
391
|
|
|
|
|
|
|
sub pairwise (&\@\@) |
392
|
|
|
|
|
|
|
{ |
393
|
0
|
|
|
0
|
0
|
|
my $op = shift; |
394
|
|
|
|
|
|
|
|
395
|
|
|
|
|
|
|
# Symbols for caller's input arrays |
396
|
118
|
|
|
118
|
|
1329
|
use vars qw{ @A @B }; |
|
118
|
|
|
|
|
389
|
|
|
118
|
|
|
|
|
13943
|
|
397
|
0
|
|
|
|
|
|
local (*A, *B) = @_; |
398
|
|
|
|
|
|
|
|
399
|
|
|
|
|
|
|
# Localise $a, $b |
400
|
|
|
|
|
|
|
my ($caller_a, $caller_b) = do |
401
|
0
|
|
|
|
|
|
{ |
402
|
0
|
|
|
|
|
|
my $pkg = caller(); |
403
|
118
|
|
|
118
|
|
981
|
no strict 'refs'; |
|
118
|
|
|
|
|
364
|
|
|
118
|
|
|
|
|
387270
|
|
404
|
0
|
|
|
|
|
|
\*{$pkg . '::a'}, \*{$pkg . '::b'}; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
405
|
|
|
|
|
|
|
}; |
406
|
|
|
|
|
|
|
|
407
|
|
|
|
|
|
|
# Loop iteration limit |
408
|
0
|
0
|
|
|
|
|
my $limit = $#A > $#B ? $#A : $#B; |
409
|
|
|
|
|
|
|
|
410
|
|
|
|
|
|
|
# This map expression is also the return value |
411
|
0
|
|
|
|
|
|
local (*$caller_a, *$caller_b); |
412
|
|
|
|
|
|
|
map { |
413
|
|
|
|
|
|
|
# Assign to $a, $b as refs to caller's array elements |
414
|
0
|
0
|
|
|
|
|
(*$caller_a, *$caller_b) = \($#A < $_ ? undef : $A[$_], $#B < $_ ? undef : $B[$_]); |
|
0
|
0
|
|
|
|
|
|
415
|
|
|
|
|
|
|
|
416
|
|
|
|
|
|
|
# Perform the transformation |
417
|
0
|
|
|
|
|
|
$op->(); |
418
|
|
|
|
|
|
|
} 0 .. $limit; |
419
|
|
|
|
|
|
|
} |
420
|
|
|
|
|
|
|
|
421
|
|
|
|
|
|
|
sub each_array (\@;\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@) |
422
|
|
|
|
|
|
|
{ |
423
|
0
|
|
|
0
|
0
|
|
return each_arrayref(@_); |
424
|
|
|
|
|
|
|
} |
425
|
|
|
|
|
|
|
|
426
|
|
|
|
|
|
|
sub each_arrayref |
427
|
|
|
|
|
|
|
{ |
428
|
0
|
|
|
0
|
0
|
|
my @list = @_; # The list of references to the arrays |
429
|
0
|
|
|
|
|
|
my $index = 0; # Which one the caller will get next |
430
|
0
|
|
|
|
|
|
my $max = 0; # Number of elements in longest array |
431
|
|
|
|
|
|
|
|
432
|
|
|
|
|
|
|
# Get the length of the longest input array |
433
|
0
|
|
|
|
|
|
foreach (@list) |
434
|
|
|
|
|
|
|
{ |
435
|
0
|
0
|
|
|
|
|
unless (ref $_ eq 'ARRAY') |
436
|
|
|
|
|
|
|
{ |
437
|
0
|
|
|
|
|
|
require Carp; |
438
|
0
|
|
|
|
|
|
Carp::croak("each_arrayref: argument is not an array reference\n"); |
439
|
|
|
|
|
|
|
} |
440
|
0
|
0
|
|
|
|
|
$max = @$_ if @$_ > $max; |
441
|
|
|
|
|
|
|
} |
442
|
|
|
|
|
|
|
|
443
|
|
|
|
|
|
|
# Return the iterator as a closure wrt the above variables. |
444
|
|
|
|
|
|
|
return sub { |
445
|
0
|
0
|
|
0
|
|
|
if (@_) |
446
|
|
|
|
|
|
|
{ |
447
|
0
|
|
|
|
|
|
my $method = shift; |
448
|
0
|
0
|
|
|
|
|
unless ($method eq 'index') |
449
|
|
|
|
|
|
|
{ |
450
|
0
|
|
|
|
|
|
require Carp; |
451
|
0
|
|
|
|
|
|
Carp::croak("each_array: unknown argument '$method' passed to iterator."); |
452
|
|
|
|
|
|
|
} |
453
|
|
|
|
|
|
|
|
454
|
|
|
|
|
|
|
# Return current (last fetched) index |
455
|
0
|
0
|
0
|
|
|
|
return undef if $index == 0 || $index > $max; |
456
|
0
|
|
|
|
|
|
return $index - 1; |
457
|
|
|
|
|
|
|
} |
458
|
|
|
|
|
|
|
|
459
|
|
|
|
|
|
|
# No more elements to return |
460
|
0
|
0
|
|
|
|
|
return if $index >= $max; |
461
|
0
|
|
|
|
|
|
my $i = $index++; |
462
|
|
|
|
|
|
|
|
463
|
|
|
|
|
|
|
# Return ith elements |
464
|
0
|
|
|
|
|
|
return map $_->[$i], @list; |
465
|
|
|
|
|
|
|
} |
466
|
0
|
|
|
|
|
|
} |
467
|
|
|
|
|
|
|
|
468
|
|
|
|
|
|
|
sub natatime ($@) |
469
|
|
|
|
|
|
|
{ |
470
|
0
|
|
|
0
|
0
|
|
my $n = shift; |
471
|
0
|
|
|
|
|
|
my @list = @_; |
472
|
|
|
|
|
|
|
return sub { |
473
|
0
|
|
|
0
|
|
|
return splice @list, 0, $n; |
474
|
|
|
|
|
|
|
} |
475
|
0
|
|
|
|
|
|
} |
476
|
|
|
|
|
|
|
|
477
|
|
|
|
|
|
|
# "leaks" when lexically hidden in arrayify |
478
|
|
|
|
|
|
|
my $flatten; |
479
|
|
|
|
|
|
|
$flatten = sub { |
480
|
|
|
|
|
|
|
map { (ref $_ and ("ARRAY" eq ref $_ or overload::Method($_, '@{}'))) ? ($flatten->(@{$_})) : ($_) } @_; |
481
|
|
|
|
|
|
|
}; |
482
|
|
|
|
|
|
|
|
483
|
|
|
|
|
|
|
sub arrayify |
484
|
|
|
|
|
|
|
{ |
485
|
0
|
|
|
0
|
0
|
|
map { $flatten->($_) } @_; |
|
0
|
|
|
|
|
|
|
486
|
|
|
|
|
|
|
} |
487
|
|
|
|
|
|
|
|
488
|
|
|
|
|
|
|
sub mesh (\@\@;\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@) |
489
|
|
|
|
|
|
|
{ |
490
|
0
|
|
|
0
|
0
|
|
my $max = -1; |
491
|
0
|
|
0
|
|
|
|
$max < $#$_ && ($max = $#$_) foreach @_; |
492
|
|
|
|
|
|
|
map { |
493
|
0
|
|
|
|
|
|
my $ix = $_; |
|
0
|
|
|
|
|
|
|
494
|
0
|
|
|
|
|
|
map $_->[$ix], @_; |
495
|
|
|
|
|
|
|
} 0 .. $max; |
496
|
|
|
|
|
|
|
} |
497
|
|
|
|
|
|
|
|
498
|
|
|
|
|
|
|
sub zip6 (\@\@;\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@) |
499
|
|
|
|
|
|
|
{ |
500
|
0
|
|
|
0
|
0
|
|
my $max = -1; |
501
|
0
|
|
0
|
|
|
|
$max < $#$_ && ($max = $#$_) foreach @_; |
502
|
|
|
|
|
|
|
map { |
503
|
0
|
|
|
|
|
|
my $ix = $_; |
|
0
|
|
|
|
|
|
|
504
|
0
|
|
|
|
|
|
[map $_->[$ix], @_]; |
505
|
|
|
|
|
|
|
} 0 .. $max; |
506
|
|
|
|
|
|
|
} |
507
|
|
|
|
|
|
|
|
508
|
|
|
|
|
|
|
sub listcmp (\@\@;\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@) |
509
|
|
|
|
|
|
|
{ |
510
|
0
|
|
|
0
|
0
|
|
my %ret; |
511
|
0
|
|
|
|
|
|
for (my $i = 0; $i < scalar @_; ++$i) |
512
|
|
|
|
|
|
|
{ |
513
|
0
|
|
|
|
|
|
my %seen; |
514
|
|
|
|
|
|
|
my $k; |
515
|
0
|
0
|
|
|
|
|
foreach my $w (grep { defined $_ and not $seen{$k = $_}++ } @{$_[$i]}) |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
516
|
|
|
|
|
|
|
{ |
517
|
0
|
|
0
|
|
|
|
$ret{$w} ||= []; |
518
|
0
|
|
|
|
|
|
push @{$ret{$w}}, $i; |
|
0
|
|
|
|
|
|
|
519
|
|
|
|
|
|
|
} |
520
|
|
|
|
|
|
|
} |
521
|
0
|
|
|
|
|
|
%ret; |
522
|
|
|
|
|
|
|
} |
523
|
|
|
|
|
|
|
|
524
|
|
|
|
|
|
|
sub uniq (@) |
525
|
|
|
|
|
|
|
{ |
526
|
0
|
|
|
0
|
0
|
|
my %seen = (); |
527
|
0
|
|
|
|
|
|
my $k; |
528
|
|
|
|
|
|
|
my $seen_undef; |
529
|
0
|
0
|
|
|
|
|
grep { defined $_ ? not $seen{$k = $_}++ : not $seen_undef++ } @_; |
|
0
|
|
|
|
|
|
|
530
|
|
|
|
|
|
|
} |
531
|
|
|
|
|
|
|
|
532
|
|
|
|
|
|
|
sub singleton (@) |
533
|
|
|
|
|
|
|
{ |
534
|
0
|
|
|
0
|
0
|
|
my %seen = (); |
535
|
0
|
|
|
|
|
|
my $k; |
536
|
|
|
|
|
|
|
my $seen_undef; |
537
|
0
|
0
|
|
|
|
|
grep { 1 == (defined $_ ? $seen{$k = $_} : $seen_undef) } |
538
|
0
|
0
|
|
|
|
|
grep { defined $_ ? not $seen{$k = $_}++ : not $seen_undef++ } @_; |
|
0
|
|
|
|
|
|
|
539
|
|
|
|
|
|
|
} |
540
|
|
|
|
|
|
|
|
541
|
|
|
|
|
|
|
sub duplicates (@) |
542
|
|
|
|
|
|
|
{ |
543
|
0
|
|
|
0
|
0
|
|
my %seen = (); |
544
|
0
|
|
|
|
|
|
my $k; |
545
|
|
|
|
|
|
|
my $seen_undef; |
546
|
0
|
0
|
|
|
|
|
grep { 1 < (defined $_ ? $seen{$k = $_} : $seen_undef) } |
547
|
0
|
0
|
|
|
|
|
grep { defined $_ ? not $seen{$k = $_}++ : not $seen_undef++ } @_; |
|
0
|
|
|
|
|
|
|
548
|
|
|
|
|
|
|
} |
549
|
|
|
|
|
|
|
|
550
|
|
|
|
|
|
|
sub frequency (@) |
551
|
|
|
|
|
|
|
{ |
552
|
0
|
|
|
0
|
0
|
|
my %seen = (); |
553
|
0
|
|
|
|
|
|
my $k; |
554
|
|
|
|
|
|
|
my $seen_undef; |
555
|
0
|
0
|
|
|
|
|
my %h = map { defined $_ ? ($_ => $seen{$k = $_}) : () } |
556
|
0
|
0
|
|
|
|
|
grep { defined $_ ? not $seen{$k = $_}++ : not $seen_undef++ } @_; |
|
0
|
|
|
|
|
|
|
557
|
0
|
0
|
|
|
|
|
wantarray or return (scalar keys %h) + ($seen_undef ? 1 : 0); |
|
|
0
|
|
|
|
|
|
558
|
0
|
|
|
|
|
|
undef $k; |
559
|
0
|
0
|
|
|
|
|
(%h, $seen_undef ? (\$k => $seen_undef) : ()); |
560
|
|
|
|
|
|
|
} |
561
|
|
|
|
|
|
|
|
562
|
|
|
|
|
|
|
sub occurrences (@) |
563
|
|
|
|
|
|
|
{ |
564
|
0
|
|
|
0
|
0
|
|
my %seen = (); |
565
|
0
|
|
|
|
|
|
my $k; |
566
|
|
|
|
|
|
|
my $seen_undef; |
567
|
0
|
|
|
|
|
|
my @ret; |
568
|
0
|
0
|
|
|
|
|
foreach my $l (map { $_ } grep { defined $_ ? not $seen{$k = $_}++ : not $seen_undef++ } @_) |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
569
|
|
|
|
|
|
|
{ |
570
|
0
|
0
|
|
|
|
|
my $n = defined $l ? $seen{$l} : $seen_undef; |
571
|
0
|
0
|
|
|
|
|
defined $ret[$n] or $ret[$n] = []; |
572
|
0
|
|
|
|
|
|
push @{$ret[$n]}, $l; |
|
0
|
|
|
|
|
|
|
573
|
|
|
|
|
|
|
} |
574
|
0
|
|
|
|
|
|
@ret; |
575
|
|
|
|
|
|
|
} |
576
|
|
|
|
|
|
|
|
577
|
|
|
|
|
|
|
sub mode (@) |
578
|
|
|
|
|
|
|
{ |
579
|
0
|
|
|
0
|
0
|
|
my %seen = (); |
580
|
0
|
|
|
|
|
|
my ($max, $k, $seen_undef) = (1); |
581
|
|
|
|
|
|
|
|
582
|
0
|
0
|
0
|
|
|
|
foreach (@_) { defined $_ ? ($max < ++$seen{$k = $_} and ++$max) : ($max < ++$seen_undef and ++$max) } |
|
0
|
|
0
|
|
|
|
|
583
|
0
|
0
|
|
|
|
|
wantarray or return $max; |
584
|
|
|
|
|
|
|
|
585
|
0
|
|
|
|
|
|
my @ret = ($max); |
586
|
0
|
|
|
|
|
|
foreach my $l (grep { $seen{$_} == $max } keys %seen) |
|
0
|
|
|
|
|
|
|
587
|
|
|
|
|
|
|
{ |
588
|
0
|
|
|
|
|
|
push @ret, $l; |
589
|
|
|
|
|
|
|
} |
590
|
0
|
0
|
0
|
|
|
|
$seen_undef and $seen_undef == $max and push @ret, undef; |
591
|
0
|
|
|
|
|
|
@ret; |
592
|
|
|
|
|
|
|
} |
593
|
|
|
|
|
|
|
|
594
|
|
|
|
|
|
|
sub samples ($@) |
595
|
|
|
|
|
|
|
{ |
596
|
0
|
|
|
0
|
0
|
|
my $n = shift; |
597
|
0
|
0
|
|
|
|
|
if ($n > @_) |
598
|
|
|
|
|
|
|
{ |
599
|
0
|
|
|
|
|
|
require Carp; |
600
|
0
|
|
|
|
|
|
Carp::croak(sprintf("Cannot get %d samples from %d elements", $n, scalar @_)); |
601
|
|
|
|
|
|
|
} |
602
|
|
|
|
|
|
|
|
603
|
0
|
|
|
|
|
|
for (my $i = @_; @_ - $i > $n;) |
604
|
|
|
|
|
|
|
{ |
605
|
0
|
|
|
|
|
|
my $idx = @_ - $i; |
606
|
0
|
|
|
|
|
|
my $swp = $idx + int(rand(--$i)); |
607
|
0
|
|
|
|
|
|
my $xchg = $_[$swp]; |
608
|
0
|
|
|
|
|
|
$_[$swp] = $_[$idx]; |
609
|
0
|
|
|
|
|
|
$_[$idx] = $xchg; |
610
|
|
|
|
|
|
|
} |
611
|
|
|
|
|
|
|
|
612
|
0
|
|
|
|
|
|
return splice @_, 0, $n; |
613
|
|
|
|
|
|
|
} |
614
|
|
|
|
|
|
|
|
615
|
|
|
|
|
|
|
sub minmax (@) |
616
|
|
|
|
|
|
|
{ |
617
|
0
|
0
|
|
0
|
0
|
|
return unless @_; |
618
|
0
|
|
|
|
|
|
my $min = my $max = $_[0]; |
619
|
|
|
|
|
|
|
|
620
|
0
|
|
|
|
|
|
for (my $i = 1; $i < @_; $i += 2) |
621
|
|
|
|
|
|
|
{ |
622
|
0
|
0
|
|
|
|
|
if ($_[$i - 1] <= $_[$i]) |
623
|
|
|
|
|
|
|
{ |
624
|
0
|
0
|
|
|
|
|
$min = $_[$i - 1] if $min > $_[$i - 1]; |
625
|
0
|
0
|
|
|
|
|
$max = $_[$i] if $max < $_[$i]; |
626
|
|
|
|
|
|
|
} |
627
|
|
|
|
|
|
|
else |
628
|
|
|
|
|
|
|
{ |
629
|
0
|
0
|
|
|
|
|
$min = $_[$i] if $min > $_[$i]; |
630
|
0
|
0
|
|
|
|
|
$max = $_[$i - 1] if $max < $_[$i - 1]; |
631
|
|
|
|
|
|
|
} |
632
|
|
|
|
|
|
|
} |
633
|
|
|
|
|
|
|
|
634
|
0
|
0
|
|
|
|
|
if (@_ & 1) |
635
|
|
|
|
|
|
|
{ |
636
|
0
|
|
|
|
|
|
my $i = $#_; |
637
|
0
|
0
|
|
|
|
|
if ($_[$i - 1] <= $_[$i]) |
638
|
|
|
|
|
|
|
{ |
639
|
0
|
0
|
|
|
|
|
$min = $_[$i - 1] if $min > $_[$i - 1]; |
640
|
0
|
0
|
|
|
|
|
$max = $_[$i] if $max < $_[$i]; |
641
|
|
|
|
|
|
|
} |
642
|
|
|
|
|
|
|
else |
643
|
|
|
|
|
|
|
{ |
644
|
0
|
0
|
|
|
|
|
$min = $_[$i] if $min > $_[$i]; |
645
|
0
|
0
|
|
|
|
|
$max = $_[$i - 1] if $max < $_[$i - 1]; |
646
|
|
|
|
|
|
|
} |
647
|
|
|
|
|
|
|
} |
648
|
|
|
|
|
|
|
|
649
|
0
|
|
|
|
|
|
return ($min, $max); |
650
|
|
|
|
|
|
|
} |
651
|
|
|
|
|
|
|
|
652
|
|
|
|
|
|
|
sub minmaxstr (@) |
653
|
|
|
|
|
|
|
{ |
654
|
0
|
0
|
|
0
|
0
|
|
return unless @_; |
655
|
0
|
|
|
|
|
|
my $min = my $max = $_[0]; |
656
|
|
|
|
|
|
|
|
657
|
0
|
|
|
|
|
|
for (my $i = 1; $i < @_; $i += 2) |
658
|
|
|
|
|
|
|
{ |
659
|
0
|
0
|
|
|
|
|
if ($_[$i - 1] le $_[$i]) |
660
|
|
|
|
|
|
|
{ |
661
|
0
|
0
|
|
|
|
|
$min = $_[$i - 1] if $min gt $_[$i - 1]; |
662
|
0
|
0
|
|
|
|
|
$max = $_[$i] if $max lt $_[$i]; |
663
|
|
|
|
|
|
|
} |
664
|
|
|
|
|
|
|
else |
665
|
|
|
|
|
|
|
{ |
666
|
0
|
0
|
|
|
|
|
$min = $_[$i] if $min gt $_[$i]; |
667
|
0
|
0
|
|
|
|
|
$max = $_[$i - 1] if $max lt $_[$i - 1]; |
668
|
|
|
|
|
|
|
} |
669
|
|
|
|
|
|
|
} |
670
|
|
|
|
|
|
|
|
671
|
0
|
0
|
|
|
|
|
if (@_ & 1) |
672
|
|
|
|
|
|
|
{ |
673
|
0
|
|
|
|
|
|
my $i = $#_; |
674
|
0
|
0
|
|
|
|
|
if ($_[$i - 1] le $_[$i]) |
675
|
|
|
|
|
|
|
{ |
676
|
0
|
0
|
|
|
|
|
$min = $_[$i - 1] if $min gt $_[$i - 1]; |
677
|
0
|
0
|
|
|
|
|
$max = $_[$i] if $max lt $_[$i]; |
678
|
|
|
|
|
|
|
} |
679
|
|
|
|
|
|
|
else |
680
|
|
|
|
|
|
|
{ |
681
|
0
|
0
|
|
|
|
|
$min = $_[$i] if $min gt $_[$i]; |
682
|
0
|
0
|
|
|
|
|
$max = $_[$i - 1] if $max lt $_[$i - 1]; |
683
|
|
|
|
|
|
|
} |
684
|
|
|
|
|
|
|
} |
685
|
|
|
|
|
|
|
|
686
|
0
|
|
|
|
|
|
return ($min, $max); |
687
|
|
|
|
|
|
|
} |
688
|
|
|
|
|
|
|
|
689
|
|
|
|
|
|
|
sub part (&@) |
690
|
|
|
|
|
|
|
{ |
691
|
0
|
|
|
0
|
0
|
|
my ($code, @list) = @_; |
692
|
0
|
|
|
|
|
|
my @parts; |
693
|
0
|
|
|
|
|
|
push @{$parts[$code->($_)]}, $_ foreach @list; |
|
0
|
|
|
|
|
|
|
694
|
0
|
|
|
|
|
|
return @parts; |
695
|
|
|
|
|
|
|
} |
696
|
|
|
|
|
|
|
|
697
|
|
|
|
|
|
|
sub bsearch(&@) |
698
|
|
|
|
|
|
|
{ |
699
|
0
|
|
|
0
|
0
|
|
my $code = shift; |
700
|
|
|
|
|
|
|
|
701
|
0
|
|
|
|
|
|
my $rc; |
702
|
0
|
|
|
|
|
|
my $i = 0; |
703
|
0
|
|
|
|
|
|
my $j = @_; |
704
|
|
|
|
|
|
|
do |
705
|
0
|
|
|
|
|
|
{ |
706
|
0
|
|
|
|
|
|
my $k = int(($i + $j) / 2); |
707
|
|
|
|
|
|
|
|
708
|
0
|
0
|
|
|
|
|
$k >= @_ and return; |
709
|
|
|
|
|
|
|
|
710
|
0
|
|
|
|
|
|
local *_ = \$_[$k]; |
711
|
0
|
|
|
|
|
|
$rc = $code->(); |
712
|
|
|
|
|
|
|
|
713
|
0
|
0
|
|
|
|
|
$rc == 0 |
|
|
0
|
|
|
|
|
|
714
|
|
|
|
|
|
|
and return wantarray ? $_ : 1; |
715
|
|
|
|
|
|
|
|
716
|
0
|
0
|
|
|
|
|
if ($rc < 0) |
717
|
|
|
|
|
|
|
{ |
718
|
0
|
|
|
|
|
|
$i = $k + 1; |
719
|
|
|
|
|
|
|
} |
720
|
|
|
|
|
|
|
else |
721
|
|
|
|
|
|
|
{ |
722
|
0
|
|
|
|
|
|
$j = $k - 1; |
723
|
|
|
|
|
|
|
} |
724
|
|
|
|
|
|
|
} until $i > $j; |
725
|
|
|
|
|
|
|
|
726
|
0
|
|
|
|
|
|
return; |
727
|
|
|
|
|
|
|
} |
728
|
|
|
|
|
|
|
|
729
|
|
|
|
|
|
|
sub bsearchidx(&@) |
730
|
|
|
|
|
|
|
{ |
731
|
0
|
|
|
0
|
0
|
|
my $code = shift; |
732
|
|
|
|
|
|
|
|
733
|
0
|
|
|
|
|
|
my $rc; |
734
|
0
|
|
|
|
|
|
my $i = 0; |
735
|
0
|
|
|
|
|
|
my $j = @_; |
736
|
|
|
|
|
|
|
do |
737
|
0
|
|
|
|
|
|
{ |
738
|
0
|
|
|
|
|
|
my $k = int(($i + $j) / 2); |
739
|
|
|
|
|
|
|
|
740
|
0
|
0
|
|
|
|
|
$k >= @_ and return -1; |
741
|
|
|
|
|
|
|
|
742
|
0
|
|
|
|
|
|
local *_ = \$_[$k]; |
743
|
0
|
|
|
|
|
|
$rc = $code->(); |
744
|
|
|
|
|
|
|
|
745
|
0
|
0
|
|
|
|
|
$rc == 0 and return $k; |
746
|
|
|
|
|
|
|
|
747
|
0
|
0
|
|
|
|
|
if ($rc < 0) |
748
|
|
|
|
|
|
|
{ |
749
|
0
|
|
|
|
|
|
$i = $k + 1; |
750
|
|
|
|
|
|
|
} |
751
|
|
|
|
|
|
|
else |
752
|
|
|
|
|
|
|
{ |
753
|
0
|
|
|
|
|
|
$j = $k - 1; |
754
|
|
|
|
|
|
|
} |
755
|
|
|
|
|
|
|
} until $i > $j; |
756
|
|
|
|
|
|
|
|
757
|
0
|
|
|
|
|
|
return -1; |
758
|
|
|
|
|
|
|
} |
759
|
|
|
|
|
|
|
|
760
|
|
|
|
|
|
|
sub lower_bound(&@) |
761
|
|
|
|
|
|
|
{ |
762
|
0
|
|
|
0
|
0
|
|
my $code = shift; |
763
|
0
|
|
|
|
|
|
my $count = @_; |
764
|
0
|
|
|
|
|
|
my $first = 0; |
765
|
0
|
|
|
|
|
|
while ($count > 0) |
766
|
|
|
|
|
|
|
{ |
767
|
0
|
|
|
|
|
|
my $step = $count >> 1; |
768
|
0
|
|
|
|
|
|
my $it = $first + $step; |
769
|
0
|
|
|
|
|
|
local *_ = \$_[$it]; |
770
|
0
|
0
|
|
|
|
|
if ($code->() < 0) |
771
|
|
|
|
|
|
|
{ |
772
|
0
|
|
|
|
|
|
$first = ++$it; |
773
|
0
|
|
|
|
|
|
$count -= $step + 1; |
774
|
|
|
|
|
|
|
} |
775
|
|
|
|
|
|
|
else |
776
|
|
|
|
|
|
|
{ |
777
|
0
|
|
|
|
|
|
$count = $step; |
778
|
|
|
|
|
|
|
} |
779
|
|
|
|
|
|
|
} |
780
|
|
|
|
|
|
|
|
781
|
0
|
|
|
|
|
|
$first; |
782
|
|
|
|
|
|
|
} |
783
|
|
|
|
|
|
|
|
784
|
|
|
|
|
|
|
sub upper_bound(&@) |
785
|
|
|
|
|
|
|
{ |
786
|
0
|
|
|
0
|
0
|
|
my $code = shift; |
787
|
0
|
|
|
|
|
|
my $count = @_; |
788
|
0
|
|
|
|
|
|
my $first = 0; |
789
|
0
|
|
|
|
|
|
while ($count > 0) |
790
|
|
|
|
|
|
|
{ |
791
|
0
|
|
|
|
|
|
my $step = $count >> 1; |
792
|
0
|
|
|
|
|
|
my $it = $first + $step; |
793
|
0
|
|
|
|
|
|
local *_ = \$_[$it]; |
794
|
0
|
0
|
|
|
|
|
if ($code->() <= 0) |
795
|
|
|
|
|
|
|
{ |
796
|
0
|
|
|
|
|
|
$first = ++$it; |
797
|
0
|
|
|
|
|
|
$count -= $step + 1; |
798
|
|
|
|
|
|
|
} |
799
|
|
|
|
|
|
|
else |
800
|
|
|
|
|
|
|
{ |
801
|
0
|
|
|
|
|
|
$count = $step; |
802
|
|
|
|
|
|
|
} |
803
|
|
|
|
|
|
|
} |
804
|
|
|
|
|
|
|
|
805
|
0
|
|
|
|
|
|
$first; |
806
|
|
|
|
|
|
|
} |
807
|
|
|
|
|
|
|
|
808
|
|
|
|
|
|
|
sub equal_range(&@) |
809
|
|
|
|
|
|
|
{ |
810
|
0
|
|
|
0
|
0
|
|
my $lb = &lower_bound(@_); |
811
|
0
|
|
|
|
|
|
my $ub = &upper_bound(@_); |
812
|
0
|
|
|
|
|
|
($lb, $ub); |
813
|
|
|
|
|
|
|
} |
814
|
|
|
|
|
|
|
|
815
|
|
|
|
|
|
|
sub binsert (&$\@) |
816
|
|
|
|
|
|
|
{ |
817
|
0
|
|
|
0
|
0
|
|
my $lb = &lower_bound($_[0], @{$_[2]}); |
|
0
|
|
|
|
|
|
|
818
|
0
|
|
|
|
|
|
splice @{$_[2]}, $lb, 0, $_[1]; |
|
0
|
|
|
|
|
|
|
819
|
0
|
|
|
|
|
|
$lb; |
820
|
|
|
|
|
|
|
} |
821
|
|
|
|
|
|
|
|
822
|
|
|
|
|
|
|
sub bremove (&\@) |
823
|
|
|
|
|
|
|
{ |
824
|
0
|
|
|
0
|
0
|
|
my $lb = &lower_bound($_[0], @{$_[1]}); |
|
0
|
|
|
|
|
|
|
825
|
0
|
|
|
|
|
|
splice @{$_[1]}, $lb, 1; |
|
0
|
|
|
|
|
|
|
826
|
|
|
|
|
|
|
} |
827
|
|
|
|
|
|
|
|
828
|
|
|
|
|
|
|
sub qsort(&\@) |
829
|
|
|
|
|
|
|
{ |
830
|
0
|
|
|
0
|
0
|
|
require Carp; |
831
|
0
|
|
|
|
|
|
Carp::croak("It's insane to use a pure-perl qsort"); |
832
|
|
|
|
|
|
|
} |
833
|
|
|
|
|
|
|
|
834
|
|
|
|
|
|
|
sub sort_by(&@) |
835
|
|
|
|
|
|
|
{ |
836
|
0
|
|
|
0
|
0
|
|
my ($code, @list) = @_; |
837
|
0
|
|
|
|
|
|
return map { $_->[0] } |
838
|
0
|
|
|
|
|
|
sort { $a->[1] cmp $b->[1] } |
839
|
0
|
|
|
|
|
|
map { [$_, scalar($code->())] } @list; |
|
0
|
|
|
|
|
|
|
840
|
|
|
|
|
|
|
} |
841
|
|
|
|
|
|
|
|
842
|
|
|
|
|
|
|
sub nsort_by(&@) |
843
|
|
|
|
|
|
|
{ |
844
|
0
|
|
|
0
|
0
|
|
my ($code, @list) = @_; |
845
|
0
|
|
|
|
|
|
return map { $_->[0] } |
846
|
0
|
|
|
|
|
|
sort { $a->[1] <=> $b->[1] } |
847
|
0
|
|
|
|
|
|
map { [$_, scalar($code->())] } @list; |
|
0
|
|
|
|
|
|
|
848
|
|
|
|
|
|
|
} |
849
|
|
|
|
|
|
|
|
850
|
0
|
|
|
0
|
|
|
sub _XScompiled { 0 } |
851
|
|
|
|
|
|
|
|
852
|
|
|
|
|
|
|
=head1 SEE ALSO |
853
|
|
|
|
|
|
|
|
854
|
|
|
|
|
|
|
L |
855
|
|
|
|
|
|
|
|
856
|
|
|
|
|
|
|
=head1 AUTHOR |
857
|
|
|
|
|
|
|
|
858
|
|
|
|
|
|
|
Jens Rehsack Erehsack AT cpan.orgE |
859
|
|
|
|
|
|
|
|
860
|
|
|
|
|
|
|
Adam Kennedy Eadamk@cpan.orgE |
861
|
|
|
|
|
|
|
|
862
|
|
|
|
|
|
|
Tassilo von Parseval Etassilo.von.parseval@rwth-aachen.deE |
863
|
|
|
|
|
|
|
|
864
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
865
|
|
|
|
|
|
|
|
866
|
|
|
|
|
|
|
Some parts copyright 2011 Aaron Crane. |
867
|
|
|
|
|
|
|
|
868
|
|
|
|
|
|
|
Copyright 2004 - 2010 by Tassilo von Parseval |
869
|
|
|
|
|
|
|
|
870
|
|
|
|
|
|
|
Copyright 2013 - 2017 by Jens Rehsack |
871
|
|
|
|
|
|
|
|
872
|
|
|
|
|
|
|
All code added with 0.417 or later is licensed under the Apache License, |
873
|
|
|
|
|
|
|
Version 2.0 (the "License"); you may not use this file except in compliance |
874
|
|
|
|
|
|
|
with the License. You may obtain a copy of the License at |
875
|
|
|
|
|
|
|
|
876
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0 |
877
|
|
|
|
|
|
|
|
878
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software |
879
|
|
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS, |
880
|
|
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
881
|
|
|
|
|
|
|
See the License for the specific language governing permissions and |
882
|
|
|
|
|
|
|
limitations under the License. |
883
|
|
|
|
|
|
|
|
884
|
|
|
|
|
|
|
All code until 0.416 is licensed under the same terms as Perl itself, |
885
|
|
|
|
|
|
|
either Perl version 5.8.4 or, at your option, any later version of |
886
|
|
|
|
|
|
|
Perl 5 you may have available. |
887
|
|
|
|
|
|
|
|
888
|
|
|
|
|
|
|
=cut |
889
|
|
|
|
|
|
|
|
890
|
|
|
|
|
|
|
1; |