« 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
Condition100%2/2
$_[0] // 1
MC/DC100%1/1
$_[0] // 1
$_[0]
Truth table100%2/2
A
A= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 39454 | ▶ | q{any} => [1, sub { $_[0] || $_[1] }], | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
$_[0] // ($_[1] ? 1 : undef)
MC/DC100%1/1
$_[0] // ($_[1] ? 1 : undef)
$_[0]
Truth table100%2/2
A
A= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 76 | ▶ | $val -= ($_[1] ? 1 : 0) if defined $_[0] && $val; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
defined $_[0] and $val
MC/DC100%2/2
defined $_[0] and $val
defined $_[0]
$val
Truth table100%3/3
A and B
A= defined $_[0]B= $val
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 220 | ▶ | return $values unless defined $index; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
$forced_reducer // $self->reducer
$forced_reducer
$self->reducer
Truth table66%2/3
A // B
A= $forced_reducerB= $self->reducer
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 80 | my $iterator = get_iterator map { extract_state $_ } @parameters; | |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 80 | ▶ | while (my @params = $iterator->()) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $reducer->[0] and !$carry eq !$reducer->[0]
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
$forced_reducer // $self->reducer
$forced_reducer
$self->reducer
Truth table33%1/3
A // B
A= $forced_reducerB= $self->reducer
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 39529 | ▶ | if (!defined $reducer->[0] || !defined $results{$key_a} || !$results{$key_a} ne !$reducer->[0]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not defined $reducer->[0] && defined $results{$key_a} or !$results{$key_a} ne !$reducer->[0]
defined $reducer->[0] && defined $results{$key_a}
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||||||||||||||||