| 1 | package HOI::Comprehensions; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 4 | 1 | our @ISA = qw( Exporter ); | |||||||||||||||||||||||||||
| 5 | 1 | our @EXPORT_OK = qw( comp ); | |||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.045'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | sub comp { | ||||||||||||||||||||||||||
|
Subroutine
comp: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 10 | 3 | my $computation = shift; | |||||||||||||||||||||||||||
| 11 | 3 | my $generators_ = \@_; | |||||||||||||||||||||||||||
| 12 | 3 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 13 | 3 | my @guards = @_; | |||||||||||||||||||||||||||
| 14 | 3 | my %generators; | |||||||||||||||||||||||||||
| 15 | 3 | my ($evalstr, $postfix) = ("", ""); | |||||||||||||||||||||||||||
| 16 | 3 | my $self_; | |||||||||||||||||||||||||||
| 17 | 3 | ▶ | while ($#$generators_ > -1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 7 | my ($key, $value) = (shift @$generators_, shift @$generators_); | |||||||||||||||||||||||||||
| 19 | 7 | $evalstr .= '$self->{generators}->{'.$key.'}->('; | |||||||||||||||||||||||||||
| 20 | 7 | $postfix .= ')'; | |||||||||||||||||||||||||||
| 21 | $generators{$key} = | ||||||||||||||||||||||||||||
| 22 | (ref($value) eq 'ARRAY') ? | ||||||||||||||||||||||||||||
| 23 | 4 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 4 | my $idx = 0; | |||||||||||||||||||||||||||
| 25 | 60 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 26 | 60 | ▶ | if ($#_ == -1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 6 | ▶ | my $last_ret = (defined $value->[$idx]) ? { $key => $value->[$idx] } : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 6 | $idx++; | |||||||||||||||||||||||||||
| 29 | 6 | my $last_done = ($idx > $#$value); | |||||||||||||||||||||||||||
| 30 | 6 | ▶ | $idx %= ($#$value + 1) if ($#$value > -1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 6 | return ($last_done, $last_ret); | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 54 | my ($done, $res) = @_; | |||||||||||||||||||||||||||
| 34 | 54 | my $ret = { %$res, $key => $value->[$idx] }; | |||||||||||||||||||||||||||
| 35 | 54 | ▶ | $idx++ if ($done); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 54 | my $self_done = ($idx > $#$value); | |||||||||||||||||||||||||||
| 37 | 54 | $idx %= ($#$value + 1); | |||||||||||||||||||||||||||
| 38 | 54 | ($self_done, $ret); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 4 | }->() : | |||||||||||||||||||||||||||
| 41 | ( (ref($value) eq 'HOI::Comprehensions') ? | ||||||||||||||||||||||||||||
| 42 | 1 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 1 | my $value_ = $value; | |||||||||||||||||||||||||||
| 44 | 1 | my $idx = 0; | |||||||||||||||||||||||||||
| 45 | 27 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 46 | my $forward = | ||||||||||||||||||||||||||||
| 47 | 27 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 48 | 27 | my ($ret, $forward_done); | |||||||||||||||||||||||||||
| 49 | 27 | ▶ | if ($value_->{all_done}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 24 | ($ret) = $value_->{list}->[$idx]; | |||||||||||||||||||||||||||
| 51 | 24 | $idx++; | |||||||||||||||||||||||||||
| 52 | 24 | $forward_done = ($idx > $#{$value_->{list}}); | |||||||||||||||||||||||||||
| 53 | 24 | $idx %= ($#{$value_->{list}} + 1); | |||||||||||||||||||||||||||
| 54 | 24 | return ($forward_done, $ret); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 3 | ($ret, $forward_done) = $value_->next(0); | |||||||||||||||||||||||||||
| 57 | 3 | ($forward_done, $ret) | |||||||||||||||||||||||||||
| 58 | 27 | }; | |||||||||||||||||||||||||||
| 59 | 27 | ▶ | if ($#_ == -1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | my ($last_done, $last_res) = $forward->(); | |||||||||||||||||||||||||||
| 61 | 0 | return ($last_done, { $key => $last_res }); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 27 | my ($done, $res) = @_; | |||||||||||||||||||||||||||
| 64 | 27 | my ($self_done, $self_res) = $forward->(); | |||||||||||||||||||||||||||
| 65 | 27 | my $ret = { %$res, $key => $self_res }; | |||||||||||||||||||||||||||
| 66 | 27 | ($self_done * $done, $ret); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 1 | }->() : | |||||||||||||||||||||||||||
| 69 | 30 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 70 | 30 | ▶ | if ($#_ == -1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 27 | my ($last_res, $last_done) = $value->(); | |||||||||||||||||||||||||||
| 72 | 27 | ▶ | $last_done = 1 if (not defined $last_done); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 27 | return ($last_done, { $key => $last_res }); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 3 | my ($done, $res) = @_; | |||||||||||||||||||||||||||
| 76 | my ($self_res, $self_done) = | ||||||||||||||||||||||||||||
| 77 | 3 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 78 | 3 | my $scopestr = ''; | |||||||||||||||||||||||||||
| 79 | 3 | my ($package_name) = $self_->{caller}; | |||||||||||||||||||||||||||
| 80 | 3 | local $AttrPrefix = $package_name.'::'; | |||||||||||||||||||||||||||
| 81 | 3 | for my $elt (keys %$res) { | |||||||||||||||||||||||||||
| 82 | 3 | $scopestr = 'local $'."$AttrPrefix"."$elt"." = \$res->{$elt}; "; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 3 | eval $scopestr.'$value->()'; | |||||||||||||||||||||||||||
| 85 | #$value->(); | ||||||||||||||||||||||||||||
| 86 | 3 | }->(); | |||||||||||||||||||||||||||
| 87 | 3 | ▶ | $self_done = 1 if (not defined $self_done); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 3 | my $ret = { %$res, $key => $self_res }; | |||||||||||||||||||||||||||
| 89 | 3 | ($self_done * $done, $ret); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 7 | ▶ | ); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | $self_ = | ||||||||||||||||||||||||||||
| 94 | 3 | bless | |||||||||||||||||||||||||||
| 95 | { | ||||||||||||||||||||||||||||
| 96 | computation => $computation, | ||||||||||||||||||||||||||||
| 97 | generators => \%generators, | ||||||||||||||||||||||||||||
| 98 | all_done => 0, | ||||||||||||||||||||||||||||
| 99 | geneitr => $evalstr.$postfix, | ||||||||||||||||||||||||||||
| 100 | guards => \@guards, | ||||||||||||||||||||||||||||
| 101 | list => [], | ||||||||||||||||||||||||||||
| 102 | caller => caller() | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 3 | } | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 2 | ▶ | sub get_member { | ||||||||||||||||||||||||||
|
Subroutine
get_member: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | 2 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 109 | 2 | $self->{$name} | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 1 | ▶ | sub get_list { | ||||||||||||||||||||||||||
|
Subroutine
get_list: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 114 | 1 | $self->get_member('list') | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 1 | ▶ | sub is_over { | ||||||||||||||||||||||||||
|
Subroutine
is_over: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 119 | 1 | $self->get_member('all_done') | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 33 | ▶ | sub step_next_lazy { | ||||||||||||||||||||||||||
|
Subroutine
step_next_lazy: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 123 | 33 | my ($self, $flag) = @_; | |||||||||||||||||||||||||||
| 124 | 33 | ▶ | return ($self->{list}, 1) if ($self->{all_done}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 33 | my ($done, $arguments) = eval $self->{geneitr}; | |||||||||||||||||||||||||||
| 126 | 33 | $self->{all_done} = $done; | |||||||||||||||||||||||||||
| 127 | 33 | my ($package_name) = $self->{caller}; | |||||||||||||||||||||||||||
| 128 | 33 | local $AttrPrefix = $package_name.'::'; | |||||||||||||||||||||||||||
| 129 | 33 | my $evalstr = ''; | |||||||||||||||||||||||||||
| 130 | 33 | for my $key (keys %$arguments) { | |||||||||||||||||||||||||||
| 131 | 117 | $evalstr .= 'local $'."$AttrPrefix"."$key".' = $arguments->{'."$key".'}; '; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | my %switches = ( | ||||||||||||||||||||||||||||
| 134 | 33 | ▶ | full => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 135 | 33 | my $guards_ok = 1; | |||||||||||||||||||||||||||
| 136 | 33 | eval '{'.$evalstr.'($_->($arguments) or $guards_ok = 0) for (@{$self->{guards}}); '.'push @{$self->{list}}, $self->{computation}->($arguments) if ($guards_ok); '.'}'; | |||||||||||||||||||||||||||
| 137 | 33 | $guards_ok | |||||||||||||||||||||||||||
| 138 | }, | ||||||||||||||||||||||||||||
| 139 | 33 | ); | |||||||||||||||||||||||||||
| 140 | 33 | my $guard = 0; | |||||||||||||||||||||||||||
| 141 | 33 | ▶ | $guard = $switches{$flag}->() if (scalar(keys %$arguments) == scalar(keys %{$self->{generators}})); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 33 | ($self->{list}, $done, $guard) | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | sub next { | ||||||||||||||||||||||||||||
| 146 | my ($l_, $done, $guard); | ||||||||||||||||||||||||||||
| 147 | for my $cnt (0..$_[1]) { | ||||||||||||||||||||||||||||
| 148 | do { | ||||||||||||||||||||||||||||
| 149 | ($l_, $done, $guard) = $_[0]->step_next_lazy('full'); | ||||||||||||||||||||||||||||
| 150 | } until ($done or $guard); | ||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | ($l_->[$#$l_], $done, $guard) | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 24 | ▶ | sub next { | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 156 | 24 | my ($l_, $done, $guard); | |||||||||||||||||||||||||||
| 157 | #print "cnt to $_[1]\n"; | ||||||||||||||||||||||||||||
| 158 | 24 | for my $cnt (0..$_[1]) { | |||||||||||||||||||||||||||
| 159 | 24 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$done or $guard
MC/DC100%2/2
$done or $guard
$done
$guard
Truth table100%3/3
A or B
A= $doneB= $guard
| |||||||||||||||||||||||||||||
| 160 | 33 | ($l_, $done, $guard) = $_[0]->step_next_lazy('full'); | |||||||||||||||||||||||||||
| 161 | } until ($done or $guard); | ||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 24 | ($l_->[$#$l_], $done, $guard) | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | use overload | ||||||||||||||||||||||||||||
| 167 | 21 | ▶ | '<>' => sub { my @ret = $_[0]->next(0); \@ret }, | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 168 | '+' => | ||||||||||||||||||||||||||||
| 169 | 18 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 170 | #print(scalar(@{$_[0]->{list}}), ' ', $_[1], "\n"); | ||||||||||||||||||||||||||||
| 171 | 0 | ▶ | my @ret = (scalar(@{$_[0]->{list}}) - 1 >= $_[1]) ? ($_[0]->{list}->[$_[1]], $_[0]->{all_done}) : ($_[0]->next($_[1] - scalar(@{$_[0]->{list}}))); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | \@ret | ||||||||||||||||||||||||||||
| 173 | 18 | }, | |||||||||||||||||||||||||||
| 174 | 1 | ; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 1 | 1; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 1 | ▶ | sub force { | ||||||||||||||||||||||||||
|
Subroutine
force: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 179 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 180 | 1 | ▶ | if (not $self->{all_done}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 1 | my ($elt, $done); | |||||||||||||||||||||||||||
| 182 | 1 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 3 | ($elt, $done) = @{<$self>}; | |||||||||||||||||||||||||||
| 184 | } while (not $done); | ||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | $self->{list} | ||||||||||||||||||||||||||||
| 187 | 1 | } | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | __END__ | ||||||||||||||||||||||||||||