← Index
NYTProf Performance Profile   « block view • line view • sub view »
For bin/dpath
  Run on Tue Jun 5 15:31:33 2012
Reported on Tue Jun 5 15:31:49 2012

Filename/home/ss5/perl5/perlbrew/perls/perl-5.14.1/lib/site_perl/5.14.1/Iterator/Util.pm
StatementsExecuted 23 statements in 7.67ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
1114.04ms47.1msIterator::Util::::BEGIN@28 Iterator::Util::BEGIN@28
1113.86ms4.41msIterator::Util::::BEGIN@20 Iterator::Util::BEGIN@20
11169µs86µsData::DPath::Context::::BEGIN@15.4Data::DPath::Context::BEGIN@15.4
11140µs108µsIterator::Util::::BEGIN@284 Iterator::Util::BEGIN@284
11134µs226µsIterator::Util::::BEGIN@21 Iterator::Util::BEGIN@21
11131µs57µsData::DPath::Context::::BEGIN@16.5Data::DPath::Context::BEGIN@16.5
11131µs76µsIterator::Util::::BEGIN@370 Iterator::Util::BEGIN@370
11129µs69µsIterator::Util::::BEGIN@373 Iterator::Util::BEGIN@373
0000s0sIterator::Util::::__ANON__[:114] Iterator::Util::__ANON__[:114]
0000s0sIterator::Util::::__ANON__[:133] Iterator::Util::__ANON__[:133]
0000s0sIterator::Util::::__ANON__[:158] Iterator::Util::__ANON__[:158]
0000s0sIterator::Util::::__ANON__[:199] Iterator::Util::__ANON__[:199]
0000s0sIterator::Util::::__ANON__[:241] Iterator::Util::__ANON__[:241]
0000s0sIterator::Util::::__ANON__[:293] Iterator::Util::__ANON__[:293]
0000s0sIterator::Util::::__ANON__[:398] Iterator::Util::__ANON__[:398]
0000s0sIterator::Util::::__ANON__[:399] Iterator::Util::__ANON__[:399]
0000s0sIterator::Util::::__ANON__[:434] Iterator::Util::__ANON__[:434]
0000s0sIterator::Util::::__ANON__[:52] Iterator::Util::__ANON__[:52]
0000s0sIterator::Util::::__ANON__[:81] Iterator::Util::__ANON__[:81]
0000s0sIterator::Util::::iappend Iterator::Util::iappend
0000s0sIterator::Util::::iarray Iterator::Util::iarray
0000s0sIterator::Util::::igrep Iterator::Util::igrep
0000s0sIterator::Util::::ihead Iterator::Util::ihead
0000s0sIterator::Util::::ilist Iterator::Util::ilist
0000s0sIterator::Util::::imap Iterator::Util::imap
0000s0sIterator::Util::::ipairwise Iterator::Util::ipairwise
0000s0sIterator::Util::::irange Iterator::Util::irange
0000s0sIterator::Util::::iskip Iterator::Util::iskip
0000s0sIterator::Util::::iskip_until Iterator::Util::iskip_until
0000s0sIterator::Util::::iuniq Iterator::Util::iuniq
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1=for gpg
2-----BEGIN PGP SIGNED MESSAGE-----
3Hash: SHA1
4
5=head1 NAME
6
7Iterator::Util - Essential utilities for the Iterator class.
8
9=head1 VERSION
10
11This documentation describes version 0.02 of Iterator::Util, August 23, 2005.
12
13=cut
14
152101µs2103µs
# spent 86µs (69+17) within Data::DPath::Context::BEGIN@15.4 which was called: # once (69µs+17µs) by Data::DPath::Context::BEGIN@19 at line 15
use strict;
# spent 86µs making 1 call to Data::DPath::Context::BEGIN@15.4 # spent 17µs making 1 call to strict::import
162305µs282µs
# spent 57µs (31+25) within Data::DPath::Context::BEGIN@16.5 which was called: # once (31µs+25µs) by Data::DPath::Context::BEGIN@19 at line 16
use warnings;
# spent 57µs making 1 call to Data::DPath::Context::BEGIN@16.5 # spent 25µs making 1 call to warnings::import
17package Iterator::Util;
1813µsour $VERSION = '0.02';
19
202455µs24.72ms
# spent 4.41ms (3.86+543µs) within Iterator::Util::BEGIN@20 which was called: # once (3.86ms+543µs) by Data::DPath::Context::BEGIN@19 at line 20
use base 'Exporter';
# spent 4.41ms making 1 call to Iterator::Util::BEGIN@20 # spent 314µs making 1 call to base::import
212196µs2417µs
# spent 226µs (34+192) within Iterator::Util::BEGIN@21 which was called: # once (34µs+192µs) by Data::DPath::Context::BEGIN@19 at line 21
use vars qw/@EXPORT @EXPORT_OK %EXPORT_TAGS/;
# spent 226µs making 1 call to Iterator::Util::BEGIN@21 # spent 192µs making 1 call to vars::import
22
23114µs@EXPORT = qw(imap igrep irange ilist iarray ihead iappend
24 ipairwise iskip iskip_until imesh izip iuniq);
25
2618µs@EXPORT_OK = (@EXPORT);
27
2823.83ms147.1ms
# spent 47.1ms (4.04+43.1) within Iterator::Util::BEGIN@28 which was called: # once (4.04ms+43.1ms) by Data::DPath::Context::BEGIN@19 at line 28
use Iterator;
# spent 47.1ms making 1 call to Iterator::Util::BEGIN@28
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
39sub imap (&$)
40{
41 my ($transformation, $iter) = @_;
42
43 Iterator::X::Parameter_Error->throw(q{Argument to imap must be an Iterator object})
44 unless UNIVERSAL::isa($iter, 'Iterator');
45
46 return Iterator->new( sub
47 {
48 Iterator::is_done if ($iter->is_exhausted);
49
50 local $_ = $iter->value ();
51 return $transformation-> ();
52 });
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
65sub igrep (&$)
66{
67 my ($test, $iter) = @_;
68
69 Iterator::X::Parameter_Error->throw(q{Argument to imap must be an Iterator object})
70 unless UNIVERSAL::isa($iter, 'Iterator');
71
72 return Iterator->new(sub
73 {
74 while ($iter->isnt_exhausted ())
75 {
76 local $_ = $iter->value ();
77 return $_ if $test-> ();
78 }
79
80 Iterator::is_done();
81 });
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).
97sub irange
98{
99 my ($from, $to, $step) = @_;
100 $step = 1 unless defined $step;
101
102 return Iterator->new (sub
103 {
104 # Reached limit?
105 Iterator::is_done
106 if (defined($to)
107 && ($step>0 && $from>$to || $step<0 && $from<$to) );
108
109 # This iteration's return value
110 my $retval = $from;
111
112 $from += $step;
113 return $retval;
114 });
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.
125sub ilist
126{
127 my @items = @_;
128 my $index=0;
129 return Iterator->new( sub
130 {
131 Iterator::is_done if ($index >= @items);
132 return $items[$index++];
133 });
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.
145sub iarray ($)
146{
147 my $items = shift;
148 my $index=0;
149
150 Iterator::X::Parameter_Error->throw->
151 (q{Argument to iarray must be an array reference})
152 if ref $items ne 'ARRAY';
153
154 return Iterator->new( sub
155 {
156 Iterator::is_done if $index >= @$items;
157 return $items->[$index++];
158 });
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
172sub ihead
173{
174 my $num = shift;
175 my $iter = shift;
176
177 Iterator::X::Parameter_Error->throw
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 if (wantarray)
183 {
184 my @a;
185 while ($iter->isnt_exhausted && (!defined($num) || $num-- > 0))
186 {
187 push @a, $iter->value;
188 }
189 return @a;
190 }
191
192 # Scalar context: return an iterator to return at most $num elements.
193 return Iterator->new(sub
194 {
195 Iterator::is_done if $num <= 0;
196
197 $num--;
198 return $iter->value;
199 });
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
210sub iappend
211{
212 my @its = @_;
213
214 # Check types
215 foreach (@its)
216 {
217 Iterator::X::Parameter_Error->throw
218 (q{All parameters for iarray must be Iterators})
219 unless UNIVERSAL::isa($_, 'Iterator');
220 }
221
222 # Passthru, if there's only one.
223 return $its[0] if @its == 1;
224
225 return Iterator->new (sub
226 {
227 my $val;
228
229 # Any empty iterators at front of list? Remove'em.
230 while (@its && $its[0]->is_exhausted)
231 {
232 shift @its;
233 }
234
235 # No more iterators? Then we're done.
236 Iterator::is_done
237 if @its == 0;
238
239 # Return the next value of the iterator at the head of the list.
240 return $its[0]->value;
241 });
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
254sub ipairwise (&$$)
255{
256 my $op = shift;
257 my $iterA = shift;
258 my $iterB = shift;
259
260 # Check types
261 for ($iterA, $iterB)
262 {
263 Iterator::X::Parameter_Error->throw
264 (q{Second and third parameters for ipairwise must be Iterators})
265 unless UNIVERSAL::isa($_, 'Iterator');
266 }
267
268 return Iterator->new(sub
269 {
270 Iterator::is_done
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 {
277 my $pkg;
278 my $i = 1;
279 while (1)
280 {
281 $pkg = caller($i++);
282 last if $pkg ne 'Iterator' && $pkg ne 'Iterator::Util';
283 }
28421.24ms2177µs
# spent 108µs (40+68) within Iterator::Util::BEGIN@284 which was called: # once (40µs+68µs) by Data::DPath::Context::BEGIN@19 at line 284
no strict 'refs';
# spent 108µs making 1 call to Iterator::Util::BEGIN@284 # spent 68µs making 1 call to strict::unimport
285 \*{$pkg.'::a'}, \*{$pkg.'::b'};
286 };
287
288 # Set caller's $a and $b
289 local (*$caller_a, *$caller_b) = \($iterA->value, $iterB->value);
290
291 # Invoke caller's operation
292 return $op->();
293 });
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
304sub iskip
305{
306 my $num = shift;
307 my $it = shift;
308
309 Iterator::X::Parameter_Error->throw
310 (q{Second parameter for iskip must be an Iterator})
311 unless UNIVERSAL::isa($it, 'Iterator');
312
313 # Discard first $num values
314 $it->value while $it->isnt_exhausted && $num-->0;
315
316 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
328sub iskip_until (&$)
329{
330 my $code = shift;
331 my $iter = shift;
332 my $value;
333 my $found_it = 0;
334
335 Iterator::X::Parameter_Error->throw
336 (q{Second parameter for iskip_until must be an Iterator})
337 unless UNIVERSAL::isa($iter, 'Iterator');
338
339 # Discard first $num values
340 while ($iter->isnt_exhausted)
341 {
342 local $_ = $iter->value;
343 if ($code->())
344 {
345 $found_it = 1;
346 $value = $_;
347 last;
348 }
349 }
350
351 # Didn't find it? Pity.
352 Iterator::is_done
353 unless $found_it;
354
355 # Return an iterator with this value, and all remaining values.
356 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
36813µsforeach my $sub (qw/imesh izip/)
369{
3702230µs2121µs
# spent 76µs (31+45) within Iterator::Util::BEGIN@370 which was called: # once (31µs+45µs) by Data::DPath::Context::BEGIN@19 at line 370
no strict 'refs';
# spent 76µs making 1 call to Iterator::Util::BEGIN@370 # spent 45µs making 1 call to strict::unimport
371 *$sub = sub
372 {
37321.21ms2108µs
# spent 69µs (29+39) within Iterator::Util::BEGIN@373 which was called: # once (29µs+39µs) by Data::DPath::Context::BEGIN@19 at line 373
use strict 'refs';
# spent 69µs making 1 call to Iterator::Util::BEGIN@373 # spent 39µs making 1 call to strict::import
374
375 my @iterators = @_;
376 my $it_index = 0;
377
378 foreach my $iter (@iterators)
379 {
380 Iterator::X::Parameter_Error->throw(
381 "Argument to $sub is not an iterator")
382 unless UNIVERSAL::isa($iter, 'Iterator');
383 }
384
385 return Iterator->new (sub
386 {
387 Iterator::is_done
388 if $iterators[$it_index]->is_exhausted();
389
390 my $retval = $iterators[$it_index]->value();
391
392 if (++$it_index >= @iterators)
393 {
394 $it_index = 0;
395 }
396
397 return $retval;
398 });
399246µs };
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
410sub iuniq
411{
412 Iterator::X::Parameter_Error->throw ("Too few parameters to iuniq")
413 if @_ < 1;
414 Iterator::X::Parameter_Error->throw ("Too many parameters to iuniq")
415 if @_ > 1;
416
417 my $iter = shift;
418 Iterator::X::Parameter_Error->throw("Argument to iuniq is not an iterator")
419 unless UNIVERSAL::isa($iter, 'Iterator');
420
421 my %did_see;
422 return Iterator->new (sub
423 {
424 my $value;
425 while (1)
426 {
427 Iterator::is_done
428 if $iter->is_exhausted;
429
430 $value = $iter->value;
431 last if !$did_see{$value}++;
432 }
433 return $value;
434 });
435}
436
437128µs1;
438__END__