blib/lib/Iterator/Array/Jagged.pm

filter: Statementstmt 100% 76 / 76 Branchbran 100% 16 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 100% 104 / 104 CC 12 SCAR 12.0
CC
12
Cov
100%
__ANON__
5.0
permute
4.0
_increment
3.0
CRAP 3.3
↑ Summary
1
2 package Iterator::Array::Jagged;
3
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 our $VERSION = '0.05';
7
8
9 #==============================================================================
10 sub new
11 1 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
12 1 my ($class, %args) = @_;
13
14 my $s = bless {
15 idx => [
16 4 map { 0 } 0...scalar(@{$args{data}}) - 1
17 ],
18 sizes => [
19 4 map { scalar(@$_) - 1 } @{$args{data}}
20 ],
21 data => $args{data},
22 1 _max => scalar(@{$args{data}}),
23 _is_finished => 0,
24 }, $class;
25
26 1 return $s;
27 }# end new()
28
29
30 #==============================================================================
31 sub _increment
32 340 ▶{
Subroutine _increment: called CC=3 SCAR=11.0
33 340 my ($s, $index) = @_;
34
35 340 ▶ if( $s->{idx}->[ $index ] < $s->{sizes}->[ $index ] )
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{'idx'}[$index] < $s->{'sizes'}[$index]) { } 255 85 340
36 {
37 255 $s->{idx}->[ $index ]++;
38 }
39 else
40 {
41 85 $s->{idx}->[ $index ] = 0;
42 85 ▶ if( $index + 1 < $s->{_max} )
Branch: 1 branch
BranchTrueFalseTotal
if ($index + 1 < $s->{'_max'}) { } 84 1 85
43 {
44 84 $s->_increment( $index + 1 );
45 }
46 else
47 {
48 1 $s->{_is_finished} = 1;
49 }# end if()
50 }# end if()
51 }# end _increment()
52
53
54 #==============================================================================
55 sub next
56 257 ▶{
Subroutine next: called CC=3 SCAR=11.0
Pod documented
57 257 my ($s) = @_;
58
59 257 ▶ return if $s->{_is_finished};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"_is_finished"} 1 256 257
60
61 # Calculate and return the current value:
62 256 my @parts = ();
63 256 for( 0...$s->{_max} - 1 )
64 {
65 1024 my $part_idx = $s->{idx}->[ $_ ];
66 1024 push @parts, $s->{data}->[ $_ ]->[ $part_idx ];
67 }# end for()
68
69 256 $s->_increment( 0 );
70
71 256 return @parts;
72 }# end next()
73
74
75 #==============================================================================
76 sub permute
77 1 ▶{
Subroutine permute: called CC=4 SCAR=13.9
Pod documented
78 1 my ($class, $func, @data) = @_;
79
80 1 my @idx = map { 0 } 0...scalar(@data) - 1;
81 1 my @sizes = map { scalar(@$_) - 1 } @data;
82 1 my $max = scalar(@data);
83 1 PERMUTATION: while( 1 )
84 {
85 # Prepare a 'set':
86 256 my @parts = ();
87 256 for my $num ( 0...$max - 1 )
88 {
89 1024 push @parts, $data[ $num ]->[ $idx[ $num ] ];
90 }# end for()
91
92 # Execute 'func':
93 256 $func->( @parts );
94
95 # Increment or finish:
96 256 my $to_increment = 0;
97 256 INCR: while( 1 )
98 {
99 340 ▶ if( $idx[ $to_increment ] < $sizes[ $to_increment ] )
Branch: 1 branch
BranchTrueFalseTotal
if ($idx[$to_increment] < $sizes[$to_increment]) { } 255 85 340
100 {
101 255 $idx[ $to_increment ]++;
102 255 last INCR;
103 }
104 else
105 {
106 85 $idx[ $to_increment ] = 0;
107 85 ▶ if( $to_increment + 1 < $max )
Branch: 1 branch
BranchTrueFalseTotal
if ($to_increment + 1 < $max) { } 84 1 85
108 {
109 84 $to_increment += 1;
110 84 next INCR;
111 }
112 else
113 {
114 1 last PERMUTATION;
115 }# end if()
116 }# end if()
117 }# end while()
118
119 255 next PERMUTATION;
120 }# end while()
121
122 }# end permute()
123
124
125 #==============================================================================
126 sub get_iterator
127 1 ▶{
Subroutine get_iterator: called CC=1 SCAR=0.0
Pod documented
128 1 my ($class, @data) = @_;
129
130 1 my @idx = map { 0 } 0...scalar(@data) - 1;
131 1 my @sizes = map { scalar(@$_) - 1 } @data;
132 1 my $max = scalar(@data);
133 1 my $is_finished = 0;
134
135 257 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
136 257 ▶ return if $is_finished;
Branch: 1 branch
BranchTrueFalseTotal
if $is_finished 1 256 257
137 # Prepare a 'set':
138 256 my @parts = ();
139 256 for my $num ( 0...$max - 1 )
140 {
141 1024 push @parts, $data[ $num ]->[ $idx[ $num ] ];
142 }# end for()
143
144 # Increment or finish:
145 256 my $to_increment = 0;
146 256 INCR: while( 1 )
147 {
148 341 ▶ if( $idx[ $to_increment ] < $sizes[ $to_increment ] )
Branch: 1 branch
BranchTrueFalseTotal
if ($idx[$to_increment] < $sizes[$to_increment]) { } 256 85 341
149 {
150 256 $idx[ $to_increment ]++;
151 256 last INCR;
152 }
153 else
154 {
155 85 $idx[ $to_increment ] = 0;
156 85 ▶ if( $to_increment + 1 < $max )
Branch: 1 branch
BranchTrueFalseTotal
if ($to_increment + 1 < $max) { } 84 1 85
157 {
158 84 $to_increment += 1;
159 84 next INCR;
160 }
161 else
162 {
163 1 $is_finished = 1;
164 }# end if()
165 }# end if()
166 }# end while()
167
168 # Finally return the parts:
169 256 return @parts;
170 1 };# end sub{...}
171 }# end get_iterator()
172
173 1 1; #return true:
174
175 __END__
↑ Summary