| Iterable(3pm) | User Contributed Perl Documentation | Iterable(3pm) |
Tie::Array::Iterable - Allows creation of iterators for lists and arrays
use Tie::Array::Iterable qw( quick );
my $iterarray = new Tie::Array::Iterable( 1..10 );
for( my $iter = $iterarray->start() ; !$iter->at_end() ; $iter->next() ) {
print $iter->index(), " : ", $iter->value();
if ( $iter->value() == 3 ) {
unshift @$iterarray, (11..15);
}
}
my @array = ( 1..10 );
for( my $iter = iterator_from_start( @array ) ;
!$iter->at_end() ;
$iter->next() ) { ... }
for( my $iter = iterate_from_end( @array ) ;
!$iter->at_end() ;
$iter->next() ) { ... }
"Tie::Hash::Iterable" allows one to create iterators for lists and arrays. The concept of iterators is borrowed from the C++ STL [1], in which most of the collections have iterators, though this class does not attempt to fully mimic it.
Typically, in C/C++ or Perl, the 'easy' way to visit each item on a list is to use a counter, and then a for( ;; ) loop. However, this requires knowledge on how long the array is to know when to end. In addition, if items are removed or inserted into the array during the loop, then the counter will be incorrect on the next run through the loop, and will cause problems.
While some aspects of this are fixed in Perl by the use of for or foreach, these commands still suffer when items are removed or added to the array while in these loops. Also, if one wished to use break to step out of a foreach loop, then restart where they left at some later point, there is no way to do this without maintaining some additional state information.
The concept of iterators is that each iterator is a bookmark to a spot, typically considered between two elements. While there is some overhead to the use of iterators, it allows elements to be added or removed from the list, with the iterator adjusting appropriate, and allows the state of a list traversal to be saved when needed.
For example, the following Perl code will drop into an endless block (this mimics the functionality of the above code):
my @array = (0..10);
for my $i ( @a ) {
print "$i\n";
if ( $i == 3 ) { unshift @a, ( 11..15 ); }
}
However, the synopsis code will not be impaired when the unshift operation is performed; the iteration will simply continue at the next element, being 4 in this case.
Tie::Array::Iterable does this by first tying the desired list to this class as well as blessing it in order to give it functionality. When a new iterator is requested via the iterable array object, a new object is generated from either Tie::Array::Iterable::ForwardIterator or Tie::Array::Iterable::BackwardIterator. These objects are then used in associated for loops to move through the array and to access values. When changes in the positions of elements of the initial array are made, the tied variable does the appropriate bookkeeping with any iterators that have been created to make sure they point to the appropriate elements.
Note that the iterable array object is also a tied array, and thus, you can use all standard array operations on it (with arrow notation due to the reference, of course).
The logic behind how iterators will 'move' depending on actions are listed here. Given the list
0 1 2 3 4 5 6 7 8 9 10
^
Forward iterator current position
Several possible cases can be considered:
This is only for the forward iterator; the backwards iterator would work similarly.
The iterators that are generated by the functions above have the following functions associated with them.
The 'quick' export will export "iterate_from_start", "iterate_from_end", "iterate_forward_from", and "iterate_backward_from" functions into the global namespace. Optionally, you may import these functions individually.
You should not directly tie your array to this class, nor use the ForwardIterator or BackwardIterator classes directly. There are factory-like methods for these classes that you should use instead.
You might run in to trouble if you use more than MAXINT (typically 2^32 on most 32-bit machines) iterators during a single instance of the program. If this is a practical concern, please let me know; that can be fixed though with some time consumption.
Michael K. Neylon <mneylon-pm@masemware.com>
I'd like to thank Chip Salzenberg for a useful suggesting in helping to remove the reference problem without having to resort to weak references on Perlmonks.
[1] A reference guide to the C++ STL can be found at
http://www.cs.rpi.edu/projects/STL/htdocs/stl.html
Copyright 2001 by Michael K. Neylon <mneylon-pm@masemware.com>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html
| 2018-03-31 | perl v5.26.1 |