line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
1
|
|
|
1
|
|
1583
|
use 5.008; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
44
|
|
2
|
1
|
|
|
1
|
|
146
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
3
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
57
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Number::Rangify; |
6
|
|
|
|
|
|
|
our $VERSION = '1.100860'; |
7
|
|
|
|
|
|
|
# ABSTRACT: Optimize a list of values into ranges |
8
|
1
|
|
|
1
|
|
1918
|
use Set::IntRange; |
|
1
|
|
|
|
|
13738
|
|
|
1
|
|
|
|
|
83
|
|
9
|
1
|
|
|
1
|
|
11
|
use Exporter qw(import); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
312
|
|
10
|
|
|
|
|
|
|
our %EXPORT_TAGS = (util => [qw(rangify)],); |
11
|
|
|
|
|
|
|
our @EXPORT_OK = @{ $EXPORT_TAGS{all} = [ map { @$_ } values %EXPORT_TAGS ] }; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub rangify { |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# each @range element is a [ $lower, $upper ] array ref. |
16
|
2
|
|
|
2
|
1
|
8330
|
my @ranges; |
17
|
2
|
|
|
|
|
15
|
VAL: for my $val (sort { $a <=> $b } @_) { |
|
58
|
|
|
|
|
67
|
|
18
|
26
|
|
|
|
|
39
|
for my $range (@ranges) { |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# is the value already in the range? |
21
|
66
|
100
|
66
|
|
|
246
|
next VAL if $val >= $range->[0] && $val <= $range->[1]; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# extend the range downwards or upwards? |
24
|
58
|
50
|
|
|
|
173
|
if ($val == $range->[0] - 1) { |
|
|
100
|
|
|
|
|
|
25
|
0
|
|
|
|
|
0
|
$range->[0]--; |
26
|
0
|
|
|
|
|
0
|
next VAL; |
27
|
|
|
|
|
|
|
} elsif ($val == $range->[1] + 1) { |
28
|
8
|
|
|
|
|
11
|
$range->[1]++; |
29
|
8
|
|
|
|
|
15
|
next VAL; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# still here? make a new range |
34
|
10
|
|
|
|
|
32
|
push @ranges, [ $val, $val ]; |
35
|
|
|
|
|
|
|
} |
36
|
2
|
|
|
|
|
7
|
my @range_obj = map { Set::IntRange->new(@$_) } @ranges; |
|
10
|
|
|
|
|
203
|
|
37
|
2
|
100
|
|
|
|
45
|
wantarray ? @range_obj : \@range_obj; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
1; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
__END__ |