LINQ/Iterator.pm

filter: Statementstmt 99.0% 99 / 100 Branchbran 97.0% 33 / 34 Conditioncond 78.5% 12 / 14 MC/DCmcdc 83.3% 4 / 6 Subroutinesub 100% 21 / 21 Podpod 100% 5 / 5 totaltotal 96.6% 174 / 180 SCAR 30.9
CC
22
Cov
97%
extend_to
11.1
fetch_ref
3.0
element_at
3.0
CRAP 22.0
« LINQ/FieldSet/Assertion.pm ↑ Summary LINQ.pm »
1 99 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 99 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 99 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 99 ▶if ( $] < 5.010000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.01") 0 99 99
6 0 require UNIVERSAL::DOES;
7 }
8  
9 {
10 package # hide from PAUSE
11 LINQ::Iterator::_LazyList;
12
13 my $_throw_caller_error = sub {
14 2 ▶ shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
15 2 require LINQ::Exception;
16 2 'LINQ::Exception::CallerError'->throw( message => @_ );
17 99 };
18
19 sub __GENERATOR () { 0 }
20 sub __EXHAUSTED () { 1 }
21 sub __VALUES () { 2 }
22 sub __NEXT_SLOT () { 3 }
23
24 sub TIEARRAY {
25 663 ▶ my $class = shift;
Subroutine TIEARRAY: called CC=1 SCAR=0.0
26 663 bless [
27 $_[0],
28 !!0,
29 [],
30 ], $class;
31 }
32
33 sub FETCH {
34 1167 ▶ my $self = shift;
Subroutine FETCH: called CC=2 SCAR=6.9
35 1167 my ( $ix ) = @_;
36 1167 my $cache = $self->[__VALUES];
37
38 1167 $self->extend_to( $ix );
39
40 1167 ▶ $ix >= @$cache ? undef : $cache->[$ix];
Branch: 1 branch
BranchTrueFalseTotal
$ix >= @$cache ? : 1 1166 1167
41 }
42
43 sub fetch_ref {
44 1520 ▶ my $self = shift;
Subroutine fetch_ref: called CC=3 SCAR=11.0
45 1520 my ( $ix ) = @_;
46 1520 my $cache = $self->[__VALUES];
47
48 1520 $self->extend_to( $ix );
49
50 1518 ▶ return if $ix > 0+ $#$cache;
Branch: 1 branch
BranchTrueFalseTotal
if $ix > 0 + $cache->$#* 161 1357 1518
51 1357 ▶ return if $ix < 0 - @$cache;
Branch: 1 branch
BranchTrueFalseTotal
if $ix < 0 - @$cache 2 1355 1357
52 1355 \( $cache->[$ix] );
53 } #/ sub fetch_ref
54
55 sub FETCHSIZE {
56 853 ▶ my $self = shift;
Subroutine FETCHSIZE: called CC=1 SCAR=0.0
57 853 $self->extend_to( -1 );
58 836 scalar @{ $self->[__VALUES] };
59 }
60
61 sub current_extension {
62 2 ▶ my $self = shift;
Subroutine current_extension: called CC=1 SCAR=0.0
63 2 scalar @{ $self->[__VALUES] };
64 }
65
66 sub is_fully_extended {
67 1 ▶ my $self = shift;
Subroutine is_fully_extended: called CC=1 SCAR=0.0
68 1 $self->[__EXHAUSTED];
69 }
70
71 sub extend_to {
72 3540 ▶ require LINQ;
Subroutine extend_to: called CC=11 SCAR=24.0
73
74 3540 my $self = shift;
75 3540 my ( $ix ) = @_;
76 3540 my $cache = $self->[__VALUES];
77
78 EXTEND: {
79 3540 ▶ return if $self->[__EXHAUSTED];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[1] 1836 3928 5764
80 3928 ▶ return if $ix >= 0 && $ix < @$cache;
Branch: 1 branch
BranchTrueFalseTotal
if $ix >= 0 and $ix < @$cache 1232 2696 3928
Condition100%3/3
$ix >= 0 and $ix < @$cache
!ll&&!rl&&r
153811581232
MC/DC100%2/2
$ix >= 0 and $ix < @$cache
$ix >= 0 $ix < @$cache
Truth table100%3/3
A and B
A= $ix >= 0B= $ix < @$cache
ABresult
0X0
100
111
81
82 2696 my @got = $self->[__GENERATOR]->();
83 2679 my $got;
84
85 # Crazy optimized loop to find and handle LINQ::END
86 # within @got
87 # uncoverable condition count:1
88 # uncoverable condition count:5
89 2679 ▶ push( @$cache, shift @got )
Branch: 3 branches
BranchTrueFalseTotal
@got and 2681 2224 4905
and @got ? $self->$_throw_caller_error("Returned values after LINQ::END") : (return ()) 455 2226 2681
@got ? : 2 453 455
Condition81%9/11
and pop @$cache
!ll&&!rl&&r
22260455
push @$cache, shift @got and ref($got = $cache->[-1]) and $got == &LINQ::END() and
!ll
2226455
push @$cache, shift @got and ref($got = $cache->[-1]) and $got == &LINQ::END()
!ll&&!rl&&r
1237989455
push @$cache, shift @got and ref($got = $cache->[-1])
!ll&&!rl&&r
012371444
MC/DC50%2/4
and pop @$cache
-push @$cache, shift @got ref($got = $cache->[-1]) $got == &LINQ::END() pop @$cache
Truth table60%3/5
A and B and C and D
A= push @$cache, shift @gotB= ref($got = $cache->[-1])C= $got == &LINQ::END()D= pop @$cache
ABCDresult
0XXX0
10XX0
110X0
11100
11111
90 and ref( $got = $cache->[-1] )
91 and $got == LINQ::END()
92 and ( $self->[__EXHAUSTED] = !!1 )
93 and pop( @$cache )
94 and (
95 @got
96 ? $self->$_throw_caller_error( 'Returned values after LINQ::END' )
97 : return ()
98 ) while @got;
99
100 2224 redo EXTEND;
101 } #/ EXTEND:
102 } #/ sub extend_to
103 }
104  
105 99 package LINQ::Iterator;
106  
107 99 our $AUTHORITY = 'cpan:TOBYINK';
108 99 our $VERSION = '0.002';
109  
110 99 ▶use Role::Tiny::With ();
Subroutine BEGIN: called CC=1 SCAR=0.0
111 99 ▶use LINQ::Util::Internal ();
Subroutine BEGIN: called CC=1 SCAR=0.0
112  
113 99 Role::Tiny::With::with( qw( LINQ::Collection ) );
114  
115 sub new {
116 533 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
117 533 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 532 1 533
118 532 tie my ( @arr ), 'LINQ::Iterator::_LazyList',
119 LINQ::Util::Internal::assert_code( @_ );
120 532 return bless \@arr, $class;
121 }
122 LINQ::Util::Internal::throw(
123 1 "CallerError",
124 message => "Expected a coderef"
125 );
126 } #/ sub new
127  
128 sub _guts {
129 431 ▶ my $self = shift;
Subroutine _guts: called CC=1 SCAR=0.0
130 431 tied( @$self );
131 }
132  
133 sub to_list {
134 262 ▶ my $self = shift;
Subroutine to_list: called CC=2 SCAR=6.9
Pod documented
135 262 my @list = @$self;
136
137 # We must have exhausted the iterator now,
138 # so remove all the magic and act like a
139 # plain old arrayref.
140 #
141 245 ▶ if ( tied( @$self ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (tied @$self) 181 64 245
142 99 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
143 181 untie( @$self );
144 181 @$self = @list;
145 }
146
147 245 @list;
148 } #/ sub to_list
149  
150  
151 sub to_array {
152 134 ▶ my $self = shift;
Subroutine to_array: called CC=2 SCAR=6.9
Pod documented
153
154 134 ▶ if ( my $guts = $self->_guts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $guts = $self->_guts) 131 3 134
155 131 tie ( my @tied, 'LINQ::Iterator::_LazyList', undef );
156 131 @{ tied( @tied ) } = @$guts;
157 131 return \@tied;
158 }
159
160 3 $self->LINQ::Collection::to_array( @_ );
161 }
162  
163 sub element_at {
164 61 ▶ my $self = shift;
Subroutine element_at: called CC=3 SCAR=11.0
Pod documented
165
166 61 ▶ if ( my $guts = $self->_guts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $guts = $self->_guts) 27 34 61
167 27 my $ref = $guts->fetch_ref( @_ );
168 27 ▶ return $$ref if $ref;
Branch: 1 branch
BranchTrueFalseTotal
if $ref 23 4 27
169 4 require LINQ::Exception;
170 4 'LINQ::Exception::NotFound'->throw( collection => $self );
171 }
172
173 34 $self->LINQ::Collection::element_at( @_ );
174 } #/ sub element_at
175  
176 sub to_iterator {
177 231 ▶ my $self = shift;
Subroutine to_iterator: called CC=2 SCAR=6.9
Pod documented
178
179 231 ▶ if ( my $guts = $self->_guts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $guts = $self->_guts) 229 2 231
180 229 my $idx = 0;
181 229 my $done = 0;
182 return sub {
183 1494 ▶ return if $done;
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if $done 1 1493 1494
184 1493 my $val = $guts->fetch_ref( $idx++ );
185 1491 ▶ return $$val if $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val 1332 159 1491
186 159 ++$done;
187 159 return;
188 229 };
189 } #/ if ( my $guts = $self->...)
190
191 2 $self->LINQ::Collection::to_iterator( @_ );
192 } #/ sub to_iterator
193  
194 99 1;
195  
196 __END__
197  
198 =pod
199  
200 =encoding utf-8
201  
202 =head1 NAME
203  
204 LINQ::Iterator - a LINQ collection with an iterator backend
205  
206 =head1 SYNOPSIS
207  
208 use LINQ qw( LINQ );
209 use LINQ::Iterator;
210
211 my $linq = LINQ( sub { ... } );
212
213 # Same:
214 my $linq = 'LINQ::Iterator'->new( sub { ... } );
215  
216 =head1 METHODS
217  
218 LINQ::Iterator supports all the methods defined in L<LINQ::Collection>.
219  
220 =begin trustme
221  
222 =item new
223  
224 =item element_at
225  
226 =item to_list
227  
228 =item to_array
229  
230 =item to_iterator
231  
232 =end trustme
233  
234 =head1 BUGS
235  
236 Please report any bugs to
237 L<http://rt.cpan.org/Dist/Display.html?Queue=LINQ>.
238  
239 =head1 SEE ALSO
240  
241 L<LINQ>, L<LINQ::Collection>.
242  
243 L<https://en.wikipedia.org/wiki/Language_Integrated_Query>
244  
245 =head1 AUTHOR
246  
247 Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
248  
249 =head1 COPYRIGHT AND LICENCE
250  
251 This software is copyright (c) 2021 by Toby Inkster.
252  
253 This is free software; you can redistribute it and/or modify it under
254 the same terms as the Perl 5 programming language system itself.
255  
256 =head1 DISCLAIMER OF WARRANTIES
257  
258 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
259 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
260 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
« LINQ/FieldSet/Assertion.pm ↑ Summary LINQ.pm »