blib/lib/Quantum/Superpositions/Lazy/Operation/Logical.pm

filter: Statementstmt 100% 88 / 88 Branchbran 91.6% 22 / 24 Conditioncond 81.8% 18 / 22 MC/DCmcdc 61.5% 8 / 13 Subroutinesub 100% 29 / 29 Podpod 0.0% 0 / 3 totaltotal 92.1% 165 / 179 CC 22 SCAR 12.3
CC
22
Cov
96%
valid_states
8.0
__ANON__
6.0
run
5.0
CRAP 3.4
« blib/lib/Quantum/Superpositions/Lazy/Role/Operation.pm ↑ Summary blib/lib/Quantum/Superpositions/Lazy/Statistics.pm »
1 package Quantum::Superpositions::Lazy::Operation::Logical;
2 15 $Quantum::Superpositions::Lazy::Operation::Logical::VERSION = '1.12';
3 15 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use Quantum::Superpositions::Lazy::Superposition;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Quantum::Superpositions::Lazy::Util qw(is_collapsible get_iterator);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Types::Standard qw(Enum);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 my %types = (
11  
12 # type => number of parameters, code, forced reducer type
13 3 ▶ q{!} => [1, sub { !$_[0] }, "all"],
Subroutine __ANON__: called CC=1 SCAR=0.0
14  
15 39405 ▶ q{==} => [2, sub { $_[0] == $_[1] }],
Subroutine __ANON__: called CC=1 SCAR=0.0
16 1663 ▶ q{!=} => [2, sub { $_[0] != $_[1] }],
Subroutine __ANON__: called CC=1 SCAR=0.0
17 23 ▶ q{>} => [2, sub { $_[0] > $_[1] }],
Subroutine __ANON__: called CC=1 SCAR=0.0
18 1 ▶ q{>=} => [2, sub { $_[0] >= $_[1] }],
Subroutine __ANON__: called CC=1 SCAR=0.0
19 6 ▶ q{<} => [2, sub { $_[0] < $_[1] }],
Subroutine __ANON__: called CC=1 SCAR=0.0
20 9 ▶ q{<=} => [2, sub { $_[0] <= $_[1] }],
Subroutine __ANON__: called CC=1 SCAR=0.0
21  
22 41 ▶ q{eq} => [2, sub { $_[0] eq $_[1] }],
Subroutine __ANON__: called CC=1 SCAR=0.0
23 1 ▶ q{ne} => [2, sub { $_[0] ne $_[1] }],
Subroutine __ANON__: called CC=1 SCAR=0.0
24 1 ▶ q{gt} => [2, sub { $_[0] gt $_[1] }],
Subroutine __ANON__: called CC=1 SCAR=0.0
25 1 ▶ q{ge} => [2, sub { $_[0] ge $_[1] }],
Subroutine __ANON__: called CC=1 SCAR=0.0
26 1 ▶ q{lt} => [2, sub { $_[0] lt $_[1] }],
Subroutine __ANON__: called CC=1 SCAR=0.0
27 9 ▶ q{le} => [2, sub { $_[0] le $_[1] }],
Subroutine __ANON__: called CC=1 SCAR=0.0
28  
29 q{_compare} => [
30 [2,],
31 41 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
32 41 local $_ = shift;
33 41 my $sub = shift;
34 41 $sub->($_, @_);
35 }
36 15 ],
37 );
38  
39 # TODO: should "one" reducer run after every iterator pair
40 # or after an element is compared with the entire superposition?
41 my %reducer_types = (
42  
43 # type => short circuit value, code
44 1675 ▶ q{all} => [0, sub { ($_[0] // 1) && $_[1] }],
Subroutine __ANON__: called CC=3 SCAR=11.2
Branch: 1 branch
BranchTrueFalseTotal
$_[0] // 1 and $_[1] 1675 0 1675
Condition100%2/2
$_[0] // 1
l!l
164827
MC/DC100%1/1
$_[0] // 1
$_[0]
Truth table100%2/2
A
A= $_[0]
Aresult
11
00
45 39454 ▶ q{any} => [1, sub { $_[0] || $_[1] }],
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0] or $_[1] 0 39454 39454
46 q{one} => [
47 undef,
48 76 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
49 76 ▶ my $val = $_[0] // ($_[1] ? 1 : undef);
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 8 27 35
Condition100%2/2
$_[0] // ($_[1] ? 1 : undef)
l!l
4135
MC/DC100%1/1
$_[0] // ($_[1] ? 1 : undef)
$_[0]
Truth table100%2/2
A
A= $_[0]
Aresult
11
00
50 76 ▶ $val -= ($_[1] ? 1 : 0) if defined $_[0] && $val;
Branch: 2 branches
BranchTrueFalseTotal
if defined $_[0] and $val 26 50 76
$_[1] ? : 4 22 26
Condition100%3/3
defined $_[0] and $val
!ll&&!rl&&r
351526
MC/DC100%2/2
defined $_[0] and $val
defined $_[0] $val
Truth table100%3/3
A and B
A= defined $_[0]B= $val
ABresult
0X0
100
111
51 76 return $val;
52 }
53 15 ],
54 );
55  
56 sub extract_state
57 220 ▶{
Subroutine extract_state: called CC=3 SCAR=11.0
58 220 my ($ref, $index) = @_;
59  
60 220 ▶ my $values = is_collapsible($ref) ? $ref->states : [$ref];
Branch: 1 branch
BranchTrueFalseTotal
is_collapsible($ref) ? : 152 68 220
61  
62 220 ▶ return $values unless defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 190 30 220
63 30 return $values->[$index];
64 }
65  
66 15 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 15 with "Quantum::Superpositions::Lazy::Role::Operation";
69  
70 15 has "+sign" => (
71 is => "ro",
72 isa => Enum [keys %types],
73 required => 1,
74 );
75  
76 has "reducer" => (
77 is => "ro",
78 isa => Enum [keys %reducer_types],
79 writer => "set_reducer",
80 93 ▶ default => sub { $Quantum::Superpositions::Lazy::global_reducer_type },
Subroutine __ANON__: called CC=1 SCAR=0.0
81 15 );
82  
83 sub supported_types
84 15 ▶{
Subroutine supported_types: called CC=1 SCAR=0.0
Pod undocumented
85 15 my ($self) = @_;
86 15 return keys %types;
87 }
88  
89 sub run
90 80 ▶{
Subroutine run: called CC=5 SCAR=16.1
Pod undocumented
91 80 my ($self, @parameters) = @_;
92  
93 80 my ($param_num, $code, $forced_reducer) = $types{$self->sign}->@*;
94 80 $self->_clear_parameters($param_num, @parameters);
95  
96 80 my $carry;
97 80 ▶ my $reducer = $reducer_types{$forced_reducer // $self->reducer};
Condition66%2/3
$forced_reducer // $self->reducer
l!l&&r!l&&!r
3770
MC/DC0%0/2
$forced_reducer // $self->reducer
$forced_reducer $self->reducer
Truth table66%2/3
A // B
A= $forced_reducerB= $self->reducer
ABresult
1X1
011
000
98 80 my $iterator = get_iterator map { extract_state $_ } @parameters;
99  
100 80 ▶ while (my @params = $iterator->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@params) = &$iterator()) 1821 16 1837
101  
102 1821 @params = ($code->(@params));
103 1821 unshift @params, $carry;
104  
105 1821 $carry = $reducer->[1](@params);
106  
107 # short circuit if possible
108 1821 ▶ return $carry if defined $reducer->[0] && !$carry eq !$reducer->[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $reducer->[0] and !$carry eq !$reducer->[0] 64 1757 1821
Condition100%3/3
defined $reducer->[0] and !$carry eq !$reducer->[0]
!ll&&!rl&&r
76168164
MC/DC100%2/2
defined $reducer->[0] and !$carry eq !$reducer->[0]
defined $reducer->[0] !$carry eq !$reducer->[0]
Truth table100%3/3
A and not(B)
A= defined $reducer->[0]B= !$carry eq !$reducer->[0]
ABresult
0X0
100
111
109 }
110  
111 16 return !!$carry;
112 }
113  
114 sub valid_states
115 13 ▶{
Subroutine valid_states: called CC=8 SCAR=20.8
Pod undocumented
116 13 my ($self, @parameters) = @_;
117  
118 13 my ($param_num, $code, $forced_reducer) = $types{$self->sign}->@*;
119 13 $self->_clear_parameters($param_num, @parameters);
120  
121 13 my %results;
122 13 ▶ my $reducer = $reducer_types{$forced_reducer // $self->reducer};
Condition33%1/3
$forced_reducer // $self->reducer
l!l&&r!l&&!r
0130
MC/DC0%0/2
$forced_reducer // $self->reducer
$forced_reducer $self->reducer
Truth table33%1/3
A // B
A= $forced_reducerB= $self->reducer
ABresult
1X1
011
000
123 13 my $iterator = get_iterator map { extract_state $_ } @parameters;
124  
125 13 ▶ while (my ($key_a, $val_a, @params) = $iterator->(1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$iterator(1)) 39529 13 39542
126 39529 ▶ if (!defined $reducer->[0] || !defined $results{$key_a} || !$results{$key_a} ne !$reducer->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $reducer->[0] && defined $results{$key_a} or !$results{$key_a} ne !$reducer->[0]) 39384 145 39529
Condition83%5/6
not defined $reducer->[0] && defined $results{$key_a} or !$results{$key_a} ne !$reducer->[0]
l!l&&r!l&&!r
377101674145
defined $reducer->[0] && defined $results{$key_a}
!ll&&!rl&&r
0377101819
MC/DC66%2/3
not defined $reducer->[0] && defined $results{$key_a} or !$results{$key_a} ne !$reducer->[0]
defined $reducer->[0] defined $results{$key_a} !$results{$key_a} ne !$reducer->[0]
Truth table75%3/4
not(A && B) or not(C)
A= defined $reducer->[0]B= defined $results{$key_a}C= !$results{$key_a} ne !$reducer->[0]
ABCresult
0XX1
10X1
1111
1100
127  
128 39384 @params = map { $params[$_] } grep { $_ % 2 == 1 } keys @params;
129 39384 @params = ($code->($val_a, @params));
130 39384 unshift @params, $results{$key_a};
131  
132 39384 $results{$key_a} = $reducer->[1](@params);
133 }
134 }
135  
136 13 my @carry;
137 13 for my $key_a (keys %results) {
138 37710 ▶ if ($results{$key_a}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($results{$key_a}) 30 37680 37710
139 30 push @carry, extract_state($parameters[0], $key_a);
140 }
141 }
142  
143 13 return Quantum::Superpositions::Lazy::Superposition->new(
144 states => [@carry]
145 );
146 }
147  
148 15 1;
149  
« blib/lib/Quantum/Superpositions/Lazy/Role/Operation.pm ↑ Summary blib/lib/Quantum/Superpositions/Lazy/Statistics.pm »