GatherTake/LazyList.pm

filter: Statementstmt 92.7% 51 / 55 Branchbran 83.3% 10 / 12 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 86.6% 13 / 15 Podpod n/a totaltotal 88.5% 77 / 87 CC 8 SCAR 9.7
CC
8
Cov
90%
_compute
6.2
FETCHSIZE
2.0
EXISTS
2.0
CRAP 2.6
↑ Summary GatherTake.pm »
1 package # hide from PAUSE indexer
2 Perl6::GatherTake::LazyList;
3  
4 =head1 NAME
5  
6 C<Perl6::GatherTake::LazyList> - Lazy tied array for C<Perl6::GatherTake>.
7  
8 =head1 SYNOPSIS
9  
10 You shouldn't use this module. C<Perl6::GatherTake> does that transparently
11 for you.
12  
13 use Coro;
14 use Coro::Channel;
15 use Perl6::GatherTake::LazyList;
16  
17 my $queue = Coro::Channel->new(1);
18  
19 my $coro = async {
20 for (1 .. 100){
21 my $result;
22 # do some heavy computations here
23 $queue->put($result);
24 }
25 };
26  
27 my @results;
28 tie @results, 'Perl6::GatherTake::LazyList', $coro, $queue;
29  
30 =head1 DESCRIPTION
31  
32 Tied array implementation for C<Perl6::GatherTake>. Again: don't use this
33 yourself unless you really know what you're doing (and you don't).
34  
35 =head1 LICENSE
36  
37 Same as C<Perl6::GatherTake>.
38  
39 =head1 AUTHOR
40  
41 Moritz Lenz, L<http://moritz.faui2k.org>, L<http://perl-6.de/>.
42 E-Mail E<lt>moritz@faui2k3.orgE<gt>.
43  
44 =cut
45  
46 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 6 ▶use Carp qw(confess cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
49 6 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
50 #use Data::Dumper;
51  
52 6 ▶use Coro;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 6 our %_ties;
54  
55 6 our @ISA;
56  
57 6 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
58 6 require Tie::Array;
59 6 @ISA = qw(Tie::Array);
60 }
61  
62 8 ▶sub TIEARRAY {
Subroutine TIEARRAY: called CC=1 SCAR=0.0
63 8 my ($class, $coro, $queue) = @_;
64 8 my $self = bless {
65 coro => $coro,
66 queue => $queue,
67 computed => [],
68 exhausted => 0,
69 }, $class;
70 8 $_ties{$coro} = $self;
71  
72 2 ▶ $coro->on_destroy( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
73 #print "Exhausted iterator\n";
74 2 $self->{exhausted} = 1 ;
75 # this is tricky: the coro will not put another item into
76 # the queue when it end, but _compute calls ->get(), thus
77 # waits for one - which is a deadlock.
78 # so we have to put another value, which _computed will remove
79 2 $self->{queue}->put(undef);
80 8 });
81  
82 8 return $self;
83 }
84  
85 26 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
86 26 my ($self, $index) = @_;
87 # warn "Fetching item $index ($self->{exhausted})\n";
88 # print Dumper $self->{computed};
89 26 $self->_compute($index);
90 26 return $self->{computed}->[$index];
91 }
92  
93 1 ▶sub STORE {
Subroutine STORE: called CC=1 SCAR=0.0
94 1 my ($self, $index, $value) = @_;
95 1 $self->_compute($index);
96 1 $self->{computed}[$index] = $value;
97 }
98  
99 # XXX this is ugly and wrong
100 1 ▶sub FETCHSIZE {
Subroutine FETCHSIZE: called CC=2 SCAR=7.1
101 1 my $self = shift;
102 # warn "# FETCHSIZE called\n";
103 1 ▶ return ($self->{exhausted} ? 0 : 1) + scalar @{$self->{computed}};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'exhausted'} ? : 1 0 1
104 # return 2;
105  
106 # while (!$self->{exhausted}){
107 # $self->_compute();
108 # }
109 # return scalar @{$self->{computed}};
110 }
111  
112 0 ▶sub STORESIZE {
Subroutine STORESIZE: not called CC=1 SCAR=0.0
113 # do nothing
114 }
115  
116 2 ▶sub EXISTS {
Subroutine EXISTS: called CC=2 SCAR=6.9
117 2 my ($self, $index) = @_;
118 # warn "EXISTS($index) called\n";
119 2 $self->_compute($index);
120 2 ▶ return @{$self->{computed}} > $index ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'computed'};} > $index ? : 1 1 2
121 }
122  
123 29 ▶sub _compute {
Subroutine _compute: called CC=6 SCAR=18.2
124 29 my $self = shift;
125 29 ▶ return if $self->{exhausted};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"exhausted"} 2 27 29
126 # print "Size: ", $self->{queue}->size, "\n";
127  
128 # local $Coro::idle = sub { $self->{queue}->put(undef) };
129  
130  
131 27 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 27 0 27
132 27 my $index = shift;
133 27 ▶ while(@{$self->{computed}} <= $index && !$self->{exhausted}){
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"computed"};} <= $index and not $self->{"exhausted"}) 29 27 56
Condition66%2/3
@{$self->{"computed"};} <= $index and not $self->{"exhausted"}
!ll&&!rl&&r
27029
MC/DC50%1/2
@{$self->{"computed"};} <= $index and not $self->{"exhausted"}
@{$self->{"computed"};} <= $index not $self->{"exhausted"}
Truth table66%2/3
A and not(B)
A= @{$self->{"computed"};} <= $indexB= not $self->{"exhausted"}
ABresult
0X0
100
111
134 29 push @{$self->{computed}}, $self->{queue}->get();
135 }
136 } else {
137 0 push @{$self->{computed}}, $self->{queue}->get();
138 }
139 27 ▶ if ($self->{exhausted}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"exhausted"}) 1 26 27
140 # see comment in sub TIEARRAY - the last item is pushed
141 # by the on_destroy handler that also set the exhausted flag
142 1 pop @{$self->{computed}};
143 }
144 # print Dumper $self->{computed};
145 }
146  
147 0 ▶sub UNTIE {
Subroutine UNTIE: not called CC=1 SCAR=6.9
148 0 my $self = shift;
149 0 delete $self->{computed};
150 }
151  
152 6 1;
↑ Summary GatherTake.pm »