blib/lib/Iterator/Util.pm

filter: Statementstmt 100% 141 / 141 Branchbran 78.5% 55 / 70 Conditioncond 90.0% 27 / 30 MC/DCmcdc 81.2% 13 / 16 Subroutinesub 100% 30 / 30 Podpod 100% 11 / 11 totaltotal 92.9% 277 / 298 CC 49 SCAR 12.5
CC
49
Cov
93%
__ANON__
6.0
ihead
6.0
iskip_until
5.1
CRAP 3.5
↑ Summary
1 =for gpg
2 -----BEGIN PGP SIGNED MESSAGE-----
3 Hash: SHA1
4  
5 =head1 NAME
6  
7 Iterator::Util - Essential utilities for the Iterator class.
8  
9 =head1 VERSION
10  
11 This documentation describes version 0.02 of Iterator::Util, August 23, 2005.
12  
13 =cut
14  
15 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 package Iterator::Util;
18 9 our $VERSION = '0.02';
19  
20 9 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 9 ▶use vars qw/@EXPORT @EXPORT_OK %EXPORT_TAGS/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 9 @EXPORT = qw(imap igrep irange ilist iarray ihead iappend
24 ipairwise iskip iskip_until imesh izip iuniq);
25  
26 9 @EXPORT_OK = (@EXPORT);
27  
28 9 ▶use Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # Function name: imap
31 # Synopsis: $iter = imap {code} $another_iterator;
32 # Description: Transforms an iterator.
33 # Created: 07/27/2005 by EJR
34 # Parameters: code - Transformation code
35 # $another_iterator - any other iterator.
36 # Returns: Transformed iterator.
37 # Exceptions: Iterator::X::Parameter_Error
38 # Iterator::X::Am_Now_Exhausted
39 sub imap (&$)
40 12 ▶{
Subroutine imap: called CC=2 SCAR=6.9
Pod documented
41 12 my ($transformation, $iter) = @_;
42  
43 12 ▶ Iterator::X::Parameter_Error->throw(q{Argument to imap must be an Iterator object})
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($iter, "Iterator") 1 11 12
44 unless UNIVERSAL::isa($iter, 'Iterator');
45  
46 return Iterator->new( sub
47 42 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
48 42 ▶ Iterator::is_done if ($iter->is_exhausted);
Branch: 1 branch
BranchTrueFalseTotal
if $iter->is_exhausted 2 40 42
49  
50 40 local $_ = $iter->value ();
51 40 return $transformation-> ();
52 11 });
53 }
54  
55  
56 # Function name: igrep
57 # Synopsis: $iter = igrep {code} $another_iterator;
58 # Description: Filters an iterator.
59 # Created: 07/27/2005 by EJR
60 # Parameters: code - Filter condition.
61 # $another_iterator - any other iterator.
62 # Returns: Filtered iterator.
63 # Exceptions: Iterator::X::Parameter_Error
64 # Iterator::X::Am_Now_Exhausted
65 sub igrep (&$)
66 2 ▶{
Subroutine igrep: called CC=2 SCAR=6.9
Pod documented
67 2 my ($test, $iter) = @_;
68  
69 2 ▶ Iterator::X::Parameter_Error->throw(q{Argument to imap must be an Iterator object})
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($iter, "Iterator") 0 2 2
70 unless UNIVERSAL::isa($iter, 'Iterator');
71  
72 return Iterator->new(sub
73 7 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.0
74 7 ▶ while ($iter->isnt_exhausted ())
Branch: 1 branch
BranchTrueFalseTotal
if ($iter->isnt_exhausted) 16 2 18
75 {
76 16 local $_ = $iter->value ();
77 16 ▶ return $_ if $test-> ();
Branch: 1 branch
BranchTrueFalseTotal
if &$test() 5 11 16
78 }
79  
80 2 Iterator::is_done();
81 2 });
82 }
83  
84  
85 # Function name: irange
86 # Synopsis: $iter = irange ($start, $end, $step);
87 # Description: Generates an arithmetic sequence of numbers.
88 # Created: 07/27/2005 by EJR
89 # Parameters: $start - First value.
90 # $end - Final value. (may be omitted)
91 # $step - Increment value. (may be omitted)
92 # Returns: Sequence iterator
93 # Exceptions: Iterator::X::Am_Now_Exhausted
94 # Notes: If the $end value is omitted, iterator is unbounded.
95 # If $step is omitted, it defaults to 1.
96 # $step may be negative (or even zero).
97 sub irange
98 38 ▶{
Subroutine irange: called CC=2 SCAR=6.9
Pod documented
99 38 my ($from, $to, $step) = @_;
100 38 ▶ $step = 1 unless defined $step;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $step 24 14 38
101  
102 return Iterator->new (sub
103 285 ▶ {
Subroutine __ANON__: called CC=6 SCAR=17.9
104 # Reached limit?
105 285 ▶ Iterator::is_done
Branch: 1 branch
BranchTrueFalseTotal
if defined $to and $step > 0 && $from > $to || $step < 0 && $from < $to 19 266 285
Condition100%12/12
defined $to and $step > 0 && $from > $to || $step < 0 && $from < $to
!ll&&!rl&&r
2026419
$step > 0 and $from > $to or $step < 0 and $from < $to
l!l&&r!l&&!r
15464
$step > 0 and $from > $to
!ll&&!rl&&r
264215
$step < 0 and $from < $to
!ll&&!rl&&r
53114
MC/DC100%5/5
defined $to and $step > 0 && $from > $to || $step < 0 && $from < $to
defined $to $step > 0 $from > $to $step < 0 $from < $to
Truth table75%6/8
A and B and C or D and E
A= defined $toB= $step > 0C= $from > $toD= $step < 0E= $from < $to
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
106 if (defined($to)
107 && ($step>0 && $from>$to || $step<0 && $from<$to) );
108  
109 # This iteration's return value
110 266 my $retval = $from;
111  
112 266 $from += $step;
113 266 return $retval;
114 38 });
115 }
116  
117 # Function name: ilist
118 # Synopsis: $iter = ilist (@list);
119 # Description: Creates an iterator from a list
120 # Created: 07/28/2005 by EJR
121 # Parameters: @list - list of values to iterate over
122 # Returns: Array (list) iterator
123 # Exceptions: Iterator::X::Am_Now_Exhausted
124 # Notes: Makes an internal copy of the list.
125 sub ilist
126 11 ▶{
Subroutine ilist: called CC=1 SCAR=0.0
Pod documented
127 11 my @items = @_;
128 11 my $index=0;
129 return Iterator->new( sub
130 43 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
131 43 ▶ Iterator::is_done if ($index >= @items);
Branch: 1 branch
BranchTrueFalseTotal
if $index >= @items 9 34 43
132 34 return $items[$index++];
133 11 });
134 }
135  
136 # Function name: iarray
137 # Synopsis: $iter = iarray ($a_ref);
138 # Description: Creates an iterator from an array reference
139 # Created: 07/28/2005 by EJR
140 # Parameters: $a_ref - Reference to array to iterate over
141 # Returns: Array iterator
142 # Exceptions: Iterator::X::Parameter_Error
143 # Iterator::X::Am_Now_Exhausted
144 # Notes: Does not make an internal copy of the list.
145 sub iarray ($)
146 2 ▶{
Subroutine iarray: called CC=2 SCAR=7.0
Pod documented
147 2 my $items = shift;
148 2 my $index=0;
149  
150 2 ▶ Iterator::X::Parameter_Error->throw->
Branch: 1 branch
BranchTrueFalseTotal
if ref $items ne "ARRAY" 0 2 2
151 (q{Argument to iarray must be an array reference})
152 if ref $items ne 'ARRAY';
153  
154 return Iterator->new( sub
155 11 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
156 11 ▶ Iterator::is_done if $index >= @$items;
Branch: 1 branch
BranchTrueFalseTotal
if $index >= @$items 2 9 11
157 9 return $items->[$index++];
158 2 });
159 }
160  
161 # Function name: ihead
162 # Synopsis: $iter = ihead ($num, $some_other_iterator);
163 # Synopsis: @valuse = ihead ($num, $iterator);
164 # Description: Returns at most $num items from other iterator.
165 # Created: 07/28/2005 by EJR
166 # 08/02/2005 EJR: combined with ahead, per Will Coleda
167 # Parameters: $num - Max number of items to return
168 # $some_other_iterator - another iterator
169 # Returns: limited iterator
170 # Exceptions: Iterator::X::Parameter_Error
171 # Iterator::X::Am_Now_Exhausted
172 sub ihead
173 10 ▶{
Subroutine ihead: called CC=6 SCAR=17.9
Pod documented
174 10 my $num = shift;
175 10 my $iter = shift;
176  
177 10 ▶ Iterator::X::Parameter_Error->throw
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($iter, "Iterator") 2 8 10
178 (q{Second parameter for ihead must be an Iterator})
179 unless UNIVERSAL::isa($iter, 'Iterator');
180  
181 # List context? Return the first $num elements.
182 8 ▶ if (wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 4 4 8
183 {
184 4 my @a;
185 4 ▶ while ($iter->isnt_exhausted && (!defined($num) || $num-- > 0))
Branch: 1 branch
BranchTrueFalseTotal
if ($iter->isnt_exhausted and !defined($num) || $num-- > 0) 8 4 12
Condition100%6/6
$iter->isnt_exhausted and !defined($num) || $num-- > 0
!ll&&!rl&&r
138
not defined $num or $num-- > 0
l!l&&r!l&&!r
353
MC/DC100%3/3
$iter->isnt_exhausted and !defined($num) || $num-- > 0
$iter->isnt_exhausted not defined $num $num-- > 0
Truth table100%4/4
A and not(B) or C
A= $iter->isnt_exhaustedB= not defined $numC= $num-- > 0
ABCresult
0XX0
1000
11X1
1011
186 {
187 8 push @a, $iter->value;
188 }
189 4 return @a;
190 }
191  
192 # Scalar context: return an iterator to return at most $num elements.
193 return Iterator->new(sub
194 14 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
195 14 ▶ Iterator::is_done if $num <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $num <= 0 4 10 14
196  
197 10 $num--;
198 10 return $iter->value;
199 4 });
200 }
201  
202 # Function name: iappend
203 # Synopsis: $iter = iappend (@iterators);
204 # Description: Joins a bunch of iterators together.
205 # Created: 07/28/2005 by EJR
206 # Parameters: @iterators - any number of other iterators
207 # Returns: A "merged" iterator.
208 # Exceptions: Iterator::X::Parameter_Error
209 # Iterator::X::Am_Now_Exhausted
210 sub iappend
211 3 ▶{
Subroutine iappend: called CC=4 SCAR=13.9
Pod documented
212 3 my @its = @_;
213  
214 # Check types
215 3 foreach (@its)
216 {
217 7 ▶ Iterator::X::Parameter_Error->throw
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($_, "Iterator") 0 7 7
218 (q{All parameters for iarray must be Iterators})
219 unless UNIVERSAL::isa($_, 'Iterator');
220 }
221  
222 # Passthru, if there's only one.
223 3 ▶ return $its[0] if @its == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @its == 1 0 3 3
224  
225 return Iterator->new (sub
226 19 ▶ {
Subroutine __ANON__: called CC=4 SCAR=13.9
227 19 my $val;
228  
229 # Any empty iterators at front of list? Remove'em.
230 19 ▶ while (@its && $its[0]->is_exhausted)
Branch: 1 branch
BranchTrueFalseTotal
if (@its and $its[0]->is_exhausted) 5 19 24
Condition100%3/3
@its and $its[0]->is_exhausted
!ll&&!rl&&r
1185
MC/DC100%2/2
@its and $its[0]->is_exhausted
@its $its[0]->is_exhausted
Truth table100%3/3
A and B
A= @itsB= $its[0]->is_exhausted
ABresult
0X0
100
111
231 {
232 5 shift @its;
233 }
234  
235 # No more iterators? Then we're done.
236 Iterator::is_done
237 19 ▶ if @its == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @its == 0 1 18 19
238  
239 # Return the next value of the iterator at the head of the list.
240 18 return $its[0]->value;
241 3 });
242 }
243  
244 # Function name: ipairwise
245 # Synopsis: $iter = ipairwise {code} ($iter1, $iter2);
246 # Description: Applies an operation to pairs of values from iterators.
247 # Created: 07/28/2005 by EJR
248 # Parameters: code - transformation, may use $a and $b
249 # $iter1 - First iterator; "$a" value.
250 # $iter2 - First iterator; "$b" value.
251 # Returns: Iterator
252 # Exceptions: Iterator::X::Parameter_Error
253 # Iterator::X::Am_Now_Exhausted
254 sub ipairwise (&$$)
255 2 ▶{
Subroutine ipairwise: called CC=3 SCAR=11.0
Pod documented
256 2 my $op = shift;
257 2 my $iterA = shift;
258 2 my $iterB = shift;
259  
260 # Check types
261 2 for ($iterA, $iterB)
262 {
263 4 ▶ Iterator::X::Parameter_Error->throw
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($_, "Iterator") 0 4 4
264 (q{Second and third parameters for ipairwise must be Iterators})
265 unless UNIVERSAL::isa($_, 'Iterator');
266 }
267  
268 return Iterator->new(sub
269 10 ▶ {
Subroutine __ANON__: called CC=5 SCAR=16.2
270 10 ▶ Iterator::is_done
Branch: 1 branch
BranchTrueFalseTotal
if $iterA->is_exhausted or $iterB->is_exhausted 0 10 10
Condition33%1/3
$iterA->is_exhausted or $iterB->is_exhausted
l!l&&r!l&&!r
0010
MC/DC0%0/2
$iterA->is_exhausted or $iterB->is_exhausted
$iterA->is_exhausted $iterB->is_exhausted
Truth table33%1/3
A or B
A= $iterA->is_exhaustedB= $iterB->is_exhausted
ABresult
1X1
011
000
271 if $iterA->is_exhausted || $iterB->is_exhausted;
272  
273 # Localize $a and $b
274 # My thanks to Benjamin Goldberg for this little bit of evil.
275 my ($caller_a, $caller_b) = do
276 10 {
277 10 my $pkg;
278 10 my $i = 1;
279 10 while (1)
280 {
281 24 $pkg = caller($i++);
282 24 ▶ last if $pkg ne 'Iterator' && $pkg ne 'Iterator::Util';
Branch: 1 branch
BranchTrueFalseTotal
if $pkg ne "Iterator" and $pkg ne "Iterator::Util" 10 14 24
Condition100%3/3
$pkg ne "Iterator" and $pkg ne "Iterator::Util"
!ll&&!rl&&r
12210
MC/DC100%2/2
$pkg ne "Iterator" and $pkg ne "Iterator::Util"
$pkg ne "Iterator" $pkg ne "Iterator::Util"
Truth table100%3/3
A and B
A= $pkg ne "Iterator"B= $pkg ne "Iterator::Util"
ABresult
0X0
100
111
283 }
284 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
285 10 \*{$pkg.'::a'}, \*{$pkg.'::b'};
286 };
287  
288 # Set caller's $a and $b
289 10 local (*$caller_a, *$caller_b) = \($iterA->value, $iterB->value);
290  
291 # Invoke caller's operation
292 10 return $op->();
293 2 });
294 }
295  
296 # Function name: iskip
297 # Synopsis: $iter = iskip $num, $another_iterator
298 # Description: Skips the first $num values of another iterator
299 # Created: 07/28/2005 by EJR
300 # Parameters: $num - how many values to skip
301 # $another_iterator - another iterator
302 # Returns: Sequence iterator
303 # Exceptions: None
304 sub iskip
305 2 ▶{
Subroutine iskip: called CC=4 SCAR=14.0
Pod documented
306 2 my $num = shift;
307 2 my $it = shift;
308  
309 2 ▶ Iterator::X::Parameter_Error->throw
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($it, "Iterator") 0 2 2
310 (q{Second parameter for iskip must be an Iterator})
311 unless UNIVERSAL::isa($it, 'Iterator');
312  
313 # Discard first $num values
314 2 ▶ $it->value while $it->isnt_exhausted && $num-->0;
Branch: 1 branch
BranchTrueFalseTotal
$it->isnt_exhausted and $num-- > 0 and do { $it->value } 6 2 8
Condition66%2/3
$it->isnt_exhausted and $num-- > 0
!ll&&!rl&&r
026
MC/DC50%1/2
$it->isnt_exhausted and $num-- > 0
$it->isnt_exhausted $num-- > 0
Truth table66%2/3
A and B
A= $it->isnt_exhaustedB= $num-- > 0
ABresult
0X0
100
111
315  
316 2 return $it;
317 }
318  
319  
320 # Function name: iskip_until
321 # Synopsis: $iter = iskip_until {code}, $another_iterator
322 # Description: Skips values of another iterator until {code} is true.
323 # Created: 07/28/2005 by EJR
324 # Parameters: {code} - Determines when to start returning values
325 # $another_iterator - another iterator
326 # Returns: Sequence iterator
327 # Exceptions: Iterator::X::Am_Now_Exhausted
328 sub iskip_until (&$)
329 2 ▶{
Subroutine iskip_until: called CC=5 SCAR=16.2
Pod documented
330 2 my $code = shift;
331 2 my $iter = shift;
332 2 my $value;
333 2 my $found_it = 0;
334  
335 2 ▶ Iterator::X::Parameter_Error->throw
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($iter, "Iterator") 0 2 2
336 (q{Second parameter for iskip_until must be an Iterator})
337 unless UNIVERSAL::isa($iter, 'Iterator');
338  
339 # Discard first $num values
340 2 ▶ while ($iter->isnt_exhausted)
Branch: 1 branch
BranchTrueFalseTotal
if ($iter->isnt_exhausted) 9 0 9
341 {
342 9 local $_ = $iter->value;
343 9 ▶ if ($code->())
Branch: 1 branch
BranchTrueFalseTotal
if (&$code()) 2 7 9
344 {
345 2 $found_it = 1;
346 2 $value = $_;
347 2 last;
348 }
349 }
350  
351 # Didn't find it? Pity.
352 Iterator::is_done
353 2 ▶ unless $found_it;
Branch: 1 branch
BranchTrueFalseTotal
unless $found_it 0 2 2
354  
355 # Return an iterator with this value, and all remaining values.
356 2 return iappend ilist($value), $iter;
357 }
358  
359  
360 # Function name: imesh / izip
361 # Synopsis: $iter = imesh ($iter1, $iter2, ...)
362 # Description: Merges other iterators together.
363 # Created: 07/30/2005 by EJR
364 # Parameters: Any number of other iterators.
365 # Returns: Sequence iterator
366 # Exceptions: Iterator::X::Parameter_Error
367 # Iterator::X::Am_Now_Exhausted
368 9 foreach my $sub (qw/imesh izip/)
369 {
370 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
371 *$sub = sub
372 2 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.0
373 9 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
374  
375 2 my @iterators = @_;
376 2 my $it_index = 0;
377  
378 2 foreach my $iter (@iterators)
379 {
380 6 ▶ Iterator::X::Parameter_Error->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($iter, "Iterator") 0 6 6
381 "Argument to $sub is not an iterator")
382 unless UNIVERSAL::isa($iter, 'Iterator');
383 }
384  
385 return Iterator->new (sub
386 16 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.0
387 16 ▶ Iterator::is_done
Branch: 1 branch
BranchTrueFalseTotal
if $iterators[$it_index]->is_exhausted 0 16 16
388 if $iterators[$it_index]->is_exhausted();
389  
390 16 my $retval = $iterators[$it_index]->value();
391  
392 16 ▶ if (++$it_index >= @iterators)
Branch: 1 branch
BranchTrueFalseTotal
if (++$it_index >= @iterators) 4 12 16
393 {
394 4 $it_index = 0;
395 }
396  
397 16 return $retval;
398 2 });
399 18 };
400 }
401  
402 # Function name: iuniq
403 # Synopsis: $iter = iuniq ($another_iterator);
404 # Description: Removes duplicate entries from an iterator.
405 # Created: 07/30/2005 by EJR
406 # Parameters: Another iterator.
407 # Returns: Sequence iterator
408 # Exceptions: Iterator::X::Parameter_Error
409 # Iterator::X::Am_Now_Exhausted
410 sub iuniq
411 1 ▶{
Subroutine iuniq: called CC=4 SCAR=14.0
Pod documented
412 1 ▶ Iterator::X::Parameter_Error->throw ("Too few parameters to iuniq")
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 1 0 1 1
413 if @_ < 1;
414 1 ▶ Iterator::X::Parameter_Error->throw ("Too many parameters to iuniq")
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 1 1
415 if @_ > 1;
416  
417 1 my $iter = shift;
418 1 ▶ Iterator::X::Parameter_Error->throw("Argument to iuniq is not an iterator")
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($iter, "Iterator") 0 1 1
419 unless UNIVERSAL::isa($iter, 'Iterator');
420  
421 1 my %did_see;
422 return Iterator->new (sub
423 5 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.0
424 5 my $value;
425 5 while (1)
426 {
427 7 ▶ Iterator::is_done
Branch: 1 branch
BranchTrueFalseTotal
if $iter->is_exhausted 1 6 7
428 if $iter->is_exhausted;
429  
430 6 $value = $iter->value;
431 6 ▶ last if !$did_see{$value}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $did_see{$value}++ 4 2 6
432 }
433 4 return $value;
434 1 });
435 }
436  
437 9 1;
438 __END__
↑ Summary