blib/lib/Math/GrahamFunction/SqFacts.pm

filter: Statementstmt 97.6% 82 / 84 Branchbran 88.8% 16 / 18 Conditioncond 100% 7 / 7 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 18 / 18 Podpod 100% 8 / 8 totaltotal 97.1% 135 / 139 SCAR 25.7
CC
13
Cov
96%
mult_by
6.0
_get_sq_facts
6.0
_initialize
3.0
CRAP 13.0
« blib/lib/Math/GrahamFunction.pm ↑ Summary Build »
1 package Math::GrahamFunction::SqFacts;
2 5 $Math::GrahamFunction::SqFacts::VERSION = '0.02003';
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 5 ▶use parent qw(Math::GrahamFunction::Object);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 __PACKAGE__->mk_accessors(qw(n factors));
11  
12 sub _initialize
13 {
14 1771 ▶ my $self = shift;
Subroutine _initialize: called CC=3 SCAR=11.1
15 1771 my $args = shift;
16  
17 1771 ▶ if ( $args->{n} )
Branch: 2 branches
BranchTrueFalseTotal
if ($args->{'n'}) { } 844 927 1771
elsif ($args->{'factors'}) { } 927 0 927
18 {
19 844 $self->n( $args->{n} );
20  
21 844 $self->_calc_sq_factors();
22 }
23 elsif ( $args->{factors} )
24 {
25 927 $self->factors( $args->{factors} );
26 }
27 else
28 {
29 0 die "factors or n must be supplied.";
30 }
31  
32 1771 return 0;
33 }
34  
35  
36 sub clone
37 {
38 182 ▶ my $self = shift;
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
39 182 return __PACKAGE__->new( { 'factors' => [ @{ $self->factors() } ] } );
40 }
41  
42 sub _calc_sq_factors
43 {
44 844 ▶ my $self = shift;
Subroutine _calc_sq_factors: called CC=1 SCAR=0.0
45  
46 844 $self->factors( $self->_get_sq_facts( $self->n() ) );
47  
48 844 return 0;
49 }
50  
51 5 my %gsf_cache = ( 1 => [] );
52  
53 sub _get_sq_facts
54 {
55 973 ▶ my $self = shift;
Subroutine _get_sq_facts: called CC=6 SCAR=17.9
56 973 my $n = shift;
57  
58 973 ▶ if ( exists( $gsf_cache{$n} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $gsf_cache{$n}) 844 129 973
59 {
60 844 return $gsf_cache{$n};
61 }
62  
63 129 ▶ my $start_from = shift || 2;
Condition100%2/2
shift() || 2
l!l
1128
MC/DC100%1/1
shift() || 2
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
64  
65 129 for ( my $p = $start_from ; ; ++$p )
66 {
67 1720 ▶ if ( $n % $p == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($n % $p == 0) 129 1591 1720
68 {
69 # This function is recursive to make better use of the Memoization
70 # feature.
71 129 my $division_factors = $self->_get_sq_facts( ( $n / $p ), $p );
72 129 ▶ if ( @$division_factors && ( $division_factors->[0] == $p ) )
Branch: 1 branch
BranchTrueFalseTotal
if (@$division_factors and $division_factors->[0] == $p) { } 29 100 129
Condition100%3/3
@$division_factors and $division_factors->[0] == $p
!ll&&!rl&&r
386229
MC/DC100%2/2
@$division_factors and $division_factors->[0] == $p
@$division_factors $division_factors->[0] == $p
Truth table100%3/3
A and B
A= @$division_factorsB= $division_factors->[0] == $p
ABresult
0X0
100
111
73 {
74 return ( $gsf_cache{$n} =
75 29 [ @{$division_factors}[ 1 .. $#$division_factors ] ] );
76 }
77 else
78 {
79 100 return ( $gsf_cache{$n} = [ $p, @$division_factors ] );
80 }
81 }
82 }
83 }
84  
85 # Removed because it is too slow - we now use our own custom memoization (
86 # or perhaps it is just called caching)
87 # memoize('get_squaring_factors', 'NORMALIZER' => sub { return $_[0]; });
88  
89 # This function multiplies the squaring factors of $n and $m to receive
90 # the squaring factors of ($n*$m)
91  
92 # OOP-Wise, it should be a multi-method, but since we don't inherit this
93 # object it's all-right.
94  
95  
96 sub mult_by
97 {
98 2042 ▶ my $n_ref = shift;
Subroutine mult_by: called CC=6 SCAR=17.9
Pod documented
99 2042 my $m_ref = shift;
100  
101 2042 my @n = @{ $n_ref->factors() };
102 2042 my @m = eval { @{ $m_ref->factors() }; };
103 2042 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2042 2042
104 {
105 0 print "Hello\n";
106 }
107  
108 2042 my @ret = ();
109  
110 2042 ▶ while ( scalar(@n) && scalar(@m) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @n and scalar @m) 4255 2042 6297
Condition100%2/2
scalar @n and scalar @m
!ll
13364961
MC/DC100%1/1
scalar @n and scalar @m
scalar @n
Truth table100%2/2
A
A= scalar @n
Aresult
00
11
111 {
112 4255 ▶ if ( $n[0] == $m[0] )
Branch: 2 branches
BranchTrueFalseTotal
if ($n[0] == $m[0]) { } 1836 2419 4255
elsif ($n[0] < $m[0]) { } 1315 1104 2419
113 {
114 1836 shift(@n);
115 1836 shift(@m);
116 }
117 elsif ( $n[0] < $m[0] )
118 {
119 1315 push @ret, shift(@n);
120 }
121 else
122 {
123 1104 push @ret, shift(@m);
124 }
125 }
126 2042 push @ret, @n, @m;
127  
128 2042 $n_ref->factors( \@ret );
129  
130 # 0 for success
131 2042 return 0;
132 }
133  
134  
135 sub mult
136 {
137 90 ▶ my $n = shift;
Subroutine mult: called CC=1 SCAR=0.0
Pod documented
138 90 my $m = shift;
139  
140 90 my $result = $n->clone();
141 90 $result->mult_by($m);
142 90 return $result;
143 }
144  
145  
146 sub is_square
147 {
148 1364 ▶ my $self = shift;
Subroutine is_square: called CC=1 SCAR=0.0
Pod documented
149 1364 return ( scalar( @{ $self->factors() } ) == 0 );
150 }
151  
152  
153 sub exists
154 {
155 1949 ▶ my ( $self, $factor ) = @_;
Subroutine exists: called CC=1 SCAR=0.0
Pod documented
156  
157 1949 ▶ return defined( List::Util::first { $_ == $factor } @{ $self->factors() } );
Subroutine __ANON__: called CC=1 SCAR=0.0
158 }
159  
160  
161 sub last
162 {
163 90 ▶ my $self = shift;
Subroutine last: called CC=1 SCAR=0.0
Pod documented
164  
165 90 return $self->factors()->[-1];
166 }
167  
168 5 ▶use vars qw($a $b);
Subroutine BEGIN: called CC=1 SCAR=0.0
169  
170  
171 sub product
172 {
173 90 ▶ my $self = shift;
Subroutine product: called CC=1 SCAR=0.0
Pod documented
174  
175 90 ▶ return ( List::Util::reduce { $a * $b } @{ $self->factors() } );
Subroutine __ANON__: called CC=1 SCAR=0.0
176 }
177  
178  
179 sub first
180 {
181 600 ▶ my $self = shift;
Subroutine first: called CC=1 SCAR=0.0
Pod documented
182  
183 600 return $self->factors()->[0];
184 }
185  
186  
187 5 1;
188  
189 __END__
« blib/lib/Math/GrahamFunction.pm ↑ Summary Build »