blib/lib/Set/IntSpan/Partition.pm

filter: Statementstmt 100% 98 / 98 Branchbran 91.6% 11 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod 100% 2 / 2 totaltotal 99.2% 126 / 127 CC 9 SCAR 9.4
CC
9
Cov
99%
intspan_partition_map
6.0
_uniq
2.0
_add
2.0
CRAP 2.6
↑ Summary
1 package Set::IntSpan::Partition;
2 4 ▶use 5.008000;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use List::Util qw/min max/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use List::MoreUtils qw/uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use List::UtilsBy qw/partition_by nsort_by/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use List::StackBy;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Set::IntSpan;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 our $VERSION = '0.06';
13  
14 4 our %EXPORT_TAGS = ( 'all' => [ qw(
15  
16 ) ] );
17  
18 4 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
19  
20 4 our @EXPORT = qw(
21 intspan_partition
22 intspan_partition_map
23 );
24  
25 1001 ▶sub _uniq (@) {
Subroutine _uniq: called CC=2 SCAR=7.1
26 1001 my %h;
27 1001 ▶ return map { $h{$_}++ == 0 ? $_ : () } @_;
Branch: 1 branch
BranchTrueFalseTotal
$h{$_}++ == 0 ? : 24879 0 24879
28 }
29  
30 4491 ▶sub _add {
Subroutine _add: called CC=2 SCAR=6.9
31 4491 my $rest = shift;
32  
33 my @parts = map {
34 4491 my $old = $_;
35  
36 46406 my $right = $rest->diff($old);
37 46406 my $left = $old->diff($rest);
38 46406 my $both = $old->intersect($rest);
39  
40 46406 $rest = $right;
41  
42 46406 grep { !$_->empty } $left, $both
43  
44 } @_;
45  
46 4491 ▶ push @parts, $rest unless $rest->empty;
Branch: 1 branch
BranchTrueFalseTotal
unless $rest->empty 4117 374 4491
47 4491 return @parts;
48 }
49  
50 1001 ▶sub intspan_partition {
Subroutine intspan_partition: called CC=2 SCAR=6.9
Pod documented
51 1001 my @parts = ();
52  
53 1001 @parts = _add($_, @parts) for @_;
54  
55 # TODO: It's not really possible to get non-unique
56 # items into the list? But play it safe for now.
57 1001 return _uniq @parts;
58 }
59  
60 1000 ▶sub intspan_partition_map {
Subroutine intspan_partition_map: called CC=6 SCAR=17.9
Pod documented
61  
62 1000 my @intspans = @_;
63  
64 my @stacks =
65 90225 ▶ stack_by { $_->[0] }
Subroutine __ANON__: called CC=1 SCAR=0.0
66 356053 sort { $a->[0] <=> $b->[0] }
67 map {
68 1000 my $ix = $_;
69 4485 map { [ @$_, $ix ] } $intspans[$_]->spans
70 } 0 .. $#intspans;
71  
72 1000 ▶ return unless @stacks;
Branch: 1 branch
BranchTrueFalseTotal
unless @stacks 92 908 1000
73  
74 908 my $min_overall = min(map { $_->[0] } map { @$_ } @stacks);
75 908 my $max_overall = max(map { $_->[1] } map { @$_ } @stacks);
76  
77 908 push @{ $stacks[0] },
78 [ $min_overall,
79 $max_overall + 1, '' ];
80  
81 908 push @stacks,[
82 [ $max_overall + 1,
83 $max_overall + 1, '' ] ];
84  
85 908 ▶ for (my $ix = 0; $ix < @stacks - 1; ++$ix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ix < @stacks - 1) 74815 908 75723
86  
87 my $max = min(
88 $stacks[$ix+1][0][0] - 1,
89 74815 map { $_->[1] } @{ $stacks[$ix] },
90 );
91  
92 my @current_stack =
93 227635 map { [ $_->[0], min($_->[1], $max), $_->[2] ] }
94 74815 @{ $stacks[$ix] };
95  
96 my @new_stack =
97 227635 grep { $_->[0] <= $_->[1] }
98 227635 map { [ $max + 1, $_->[1], $_->[2] ] }
99 74815 @{ $stacks[$ix] };
100  
101 74815 $stacks[$ix] = \@current_stack;
102  
103 74815 ▶ if ($max + 1 == $stacks[$ix+1][0][0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max + 1 == $stacks[$ix + 1][0][0]) { } 55317 19498 74815
104 55317 push @{ $stacks[$ix+1] }, @new_stack;
105 } else {
106 19498 splice @stacks, $ix+1, 0, \@new_stack;
107 }
108  
109 }
110  
111 75723 ▶ my %h = partition_by {
Subroutine __ANON__: called CC=1 SCAR=0.0
112 75723 join ',', sort { $a cmp $b } uniq map { $_->[2] } @$_
113 } grep {
114 908 scalar @$_
115 } @stacks;
116  
117 # TODO(bh): this could be nicer:
118  
119 908 my %map;
120 908 ▶ while (my ($k, $v) = each %h) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %h) 25781 908 26689
121 25781 for my $in (split/,/, $k) {
122 my $class = Set::IntSpan->new([map {
123 218254 [ $_->[0], $_->[1] ]
124 99452 } grep { $_->[2] eq $in } map { @$_ } @$v]);
125 99452 push @{ $map{$in} }, $class;
126 }
127 }
128  
129 908 delete $map{''};
130  
131 908 return %map;
132 }
133  
134 4 1;
135  
136 __END__
↑ Summary