Array-Iterator-0.135/0000755000175000000240000000000014756230666013043 5ustar njhstaffArray-Iterator-0.135/LICENSE0000644000175000000240000004325414756072013014046 0ustar njhstaff GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. Array-Iterator-0.135/Changes0000644000175000000240000001030614756230502014323 0ustar njhstaff0.135 Fri Feb 21 21:06:54 EST 2025 Added t/30-basics.t Fixed RT#126034 - note that this is a change in the way Bidirectional works, and will break old code The old code is in Array::Iterator::LegacyBiDirectional if that API is needed Make a local copy of the array so that changes to the array don't break things 0.134 Fri Feb 21 09:45:27 EST 2025 Added support for GitHub Actions Ensure the subclasses also have versions 0.133 Fri Feb 21 08:18:02 EST 2025 New Maintainer 0.132 2023-11-21 Released-By: PERLANCAR; Urgency: low - No functional changes. - Mark distribution as up for adoption since I'm no longer using it. 0.131 2021-09-26 Released-By: PERLANCAR; Urgency: low - No functional changes. - [doc] Fix a few minor typos in POD (committer: Florian Schlichting). 0.130 2021-08-09 Released-By: PERLANCAR; Urgency: medium - Remove requirement of input list must not be of length 1 (GH#4). 0.12 2017-07-04 Released-By: PERLANCAR - No functional changes. - Re-release to switch PAUSE account. 0.11 2013-09-18 Released-By: SHARYANTO - No functional changes. Rerelease due to inclusion of unneeded files (steven--). 0.10 2013-09-18 Released-By: SHARYANTO - No functional changes. Apply spelling patch from Debian maintainer Gregor Herman [RT#88745]. 0.09 2013-08-22 Released-By: SHARYANTO - No functional changes. Reformat Changes to be more conformant to CPAN::Changes::Spec (thanks Neil Bowers). 0.08 2012-03-28 Released-By: SHARYANTO - peek(), look_back(), has_next(), has_previous() now accept optional integer argument for arbitrary lookup, e.g. peek(2) looks at the next next item, peek(1) is the same as peek() (implemented by Alexey Surikov, github#2). 0.07 2011-09-09 Released-By: SHARYANTO - Take over maintenance from Stevan Little. - Now uses Dist::Zilla and git. - Add lowercase method name aliases (has_next() as well as hasNext(), etc). The lowercase method names are now the documented ones. - Add iterated() to check whether an iteration has been done (i.e. next(), or get_next(), or previous(), etc has been called). 0.06 2005-07-08 Released-By: STEVAN - Fixed bug in Array::Iterator::peek(). Thanks to Hugo Cornelis for pointing it out - added tests for this - Added patch from Phillip Moore to support *single element iteration* using the hash-ref constructor option. - added tests and docs for this (also from Phillip :) 0.05 2004-07-15 Released-By: STEVAN - added a getLegnth method and tested it - changed how currentIndex deals with index of 0, it now does it correctly. - made current use currentIndex to get the current index - made Array::Iterator more subclass friendly by adding some 'protected' methods to access some fields with - added some subclasses: Array::Iterator::BiDirectional, Array::Iterator::Circular, Array::Iterator::Reusable - created tests for all these new modules 0.04 2004-05-06 Released-By: STEVAN - Changed current and currentIndex to refer to the same value (and index) of the last item dispensed by the next method. This is more in line with what they should do. Prior to this version they returned the current index which was actually the one past the last call to next. - tested these changes and altered tests which used the old versions. - updated documentation to reflect change 0.03 2004-05-02 Released-By: STEVAN - Added currentIndex method, and added tests for it. - Added getNext method and added tests for it. - altered the behavior of peek to not throw an exception. - updated all documentation. 0.02 2004-04-12 Released-By: STEVAN - error in the Makefile.PL file, no changes on this release 0.01 2004-03-17 Released-By: STEVAN - original version; created by h2xs 1.22 with options -X -n Array::Iterator Array-Iterator-0.135/MANIFEST0000644000175000000240000000113714756230666014176 0ustar njhstaffChanges lib/Array/Iterator.pm lib/Array/Iterator/BiDirectional.pm lib/Array/Iterator/Circular.pm lib/Array/Iterator/LegacyBiDirectional.pm lib/Array/Iterator/Reusable.pm LICENSE Makefile.PL MANIFEST This list of files MANIFEST.SKIP README.md t/00-load.t t/10-compile.t t/20-new.t t/30-basics.t t/bidirectional.t t/circular.t t/eof.t t/eol.t t/exceptions.t t/iterator.t t/legacy_bidirectional.t t/pod-coverage.t t/reusable.t t/rt126034.t META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) Array-Iterator-0.135/t/0000755000175000000240000000000014756230666013306 5ustar njhstaffArray-Iterator-0.135/t/bidirectional.t0000644000175000000240000001037414756226545016311 0ustar njhstaff#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 51; use Test::Exception; BEGIN { use_ok('Array::Iterator::BiDirectional') } my @control = (1 .. 5); can_ok("Array::Iterator::BiDirectional", 'new'); my $iterator = Array::Iterator::BiDirectional->new(@control); isa_ok($iterator, 'Array::Iterator::BiDirectional'); isa_ok($iterator, 'Array::Iterator'); # check out public methods can_ok($iterator, 'hasPrevious'); can_ok($iterator, 'has_previous'); can_ok($iterator, 'previous'); can_ok($iterator, 'lookBack'); can_ok($iterator, 'look_back'); can_ok($iterator, 'getPrevious'); can_ok($iterator, 'get_previous'); # now check the behavior # move our counter to the end $iterator->next() while $iterator->hasNext(); ok(!$iterator->hasNext(), 'Have reached the end'); for (my $i = $#control; $i > 0; $i--) { # we should still have another one ok($iterator->hasPrevious(), '... we have some previous items'); # and out iterator peek should match our control + 1 unless (($i - 1) <= 0) { cmp_ok($iterator->lookBack(), '==', $control[$i - 1], '... our control should match our iterator->lookBack'); } else { ok(!defined($iterator->lookBack()), '... this should return undef now'); } # and out iterator should match our control cmp_ok($iterator->previous(), '==', $control[$i], '... our control should match our iterator->previous'); } # we should have one more ok($iterator->hasPrevious(), '... we should have one left'); cmp_ok($iterator->previous(), '==', 1, 'Back to the start'); # we should have no more ok(!$iterator->hasPrevious(), '... we should have no more'); # now use an array ref in the constructor # and try using it in this style loop my $iterator2 = Array::Iterator::BiDirectional->new(\@control); isa_ok($iterator2, 'Array::Iterator::BiDirectional'); isa_ok($iterator2, 'Array::Iterator'); # move our iterator to the end $iterator2->next() while $iterator2->hasNext(); for (my $i = $iterator2; $i->hasPrevious(); $i->getPrevious()) { cmp_ok($i->current(), '==', $control[$i->currentIndex()], '... these should be equal'); } ok(!defined($iterator2->getPrevious()), '... this should return undef'); throws_ok { $iterator2->previous(); } qr/Out Of Bounds\: no more elements/, '... this should die if i try again'; my $iterator3 = Array::Iterator::BiDirectional->new(@control); # when not iterated() ok(!$iterator3->has_previous(1), '... should be the same as has_previous()'); ok(!$iterator3->has_previous(2), '... should not have 2nd previous element'); ok(!$iterator3->has_previous(3), '... should not have 3rd previous element'); ok(!defined($iterator3->look_back(1)), '... should be the same as look_back()'); ok(!defined($iterator3->look_back(2)), '... look_back() outside of the bounds should return undef'); ok(!defined($iterator3->look_back(5)), '... look_back() outside of the bounds should return undef'); $iterator3->next while $iterator3->has_next; # when iterated() ok($iterator3->has_previous(1), '... should be the same as has_previous() after iterating'); ok($iterator3->has_previous(2), '... should have 2nd previous element'); cmp_ok($iterator3->look_back(1), '==', $iterator3->look_back, '... should be the same as look_back() after iterating'); cmp_ok($iterator3->look_back(2), '==', 3, '... should get 2nd previous element after iterating'); cmp_ok($iterator3->look_back(3), '==', 2, '... should get 3rd previous element after iterating'); ok(!defined($iterator3->look_back(6)), '... look_back() outside of the bounds should return undef after iterating'); # check arbitrary lookup edge cases throws_ok { $iterator3->has_previous(0) } qr/\Qhas_previous(0) doesn't make sense/, '... should not be able to call has_previous() with zero argument'; throws_ok { $iterator3->has_previous(-1) } qr/\Qhas_previous() with negative argument doesn't make sense/, '... should not be able to call has_previous() with negative argument'; throws_ok { $iterator3->look_back(0) } qr/\Qlook_back(0) doesn't make sense/, '... should not be able to call look_back() with zero argument'; throws_ok { $iterator3->look_back(-1) } qr/\Qlook_back() with negative argument doesn't make sense/, '... should not be able to call look_back() with negative argument'; Array-Iterator-0.135/t/rt126034.t0000755000175000000240000000107014756154237014600 0ustar njhstaff#!/usr/bin/env perl use strict; use warnings; use Test::Most; BEGIN { use_ok('Array::Iterator::BiDirectional') } my @set = qw/ one two three four /; my $iterator = Array::Iterator::BiDirectional->new(@set); cmp_ok($iterator->getNext(), 'eq', 'one'); cmp_ok($iterator->getNext(), 'eq', 'two'); cmp_ok($iterator->getNext(), 'eq', 'three'); cmp_ok($iterator->getPrevious(), 'eq', 'two'); cmp_ok($iterator->getPrevious(), 'eq', 'one'); ok(!$iterator->hasPrevious(), '... we should have no more'); ok($iterator->hasNext(), '... we should have more'); done_testing(); Array-Iterator-0.135/t/circular.t0000644000175000000240000000212314756072035015267 0ustar njhstaff#!/usr/bin/perl use strict; use warnings; use Test::More tests => 25; BEGIN { use_ok('Array::Iterator::Circular') }; can_ok("Array::Iterator::Circular", 'new'); my $i = Array::Iterator::Circular->new(1 .. 5); isa_ok($i, 'Array::Iterator::Circular'); isa_ok($i, 'Array::Iterator'); can_ok($i, 'getLoopCount'); can_ok($i, 'get_loop_count'); can_ok($i, 'isStart'); can_ok($i, 'is_start'); can_ok($i, 'isEnd'); can_ok($i, 'is_end'); can_ok($i, 'getNext'); can_ok($i, 'get_next'); ok($i->isStart(), '... we are at the start of the array'); my $total_count = 0; while ($i->getLoopCount() < 5) { if ($total_count && (($total_count % 5) == 0)) { ok($i->isEnd(), '... we are at the end of the array'); ok($i->hasNext(), '... we should still get true from hasNext'); } defined($i->getNext()) || fail('... this should never return undef'); $total_count++; } cmp_ok($i->getLoopCount(), '==', 5, '... we have looped 5 times'); # this should be 1 past because of how the loop # above it structured, it is correct. cmp_ok($total_count, '==', 26, '... we have looped 5 times'); Array-Iterator-0.135/t/10-compile.t0000644000175000000240000000022714706211127015324 0ustar njhstaff#!perl -w use strict; use warnings; use Test::Needs 'Test::Compile'; my $test = Test::Compile->new(); $test->all_files_ok(); $test->done_testing(); Array-Iterator-0.135/t/20-new.t0000644000175000000240000000055614756230207014500 0ustar njhstaff#!perl -w use strict; # use lib 'lib'; use Test::Most tests => 3; BEGIN { use_ok('Array::Iterator') } isa_ok(Array::Iterator->new(1..5), 'Array::Iterator', 'Creating Array::Iterator object with ARRAY'); isa_ok(Array::Iterator->new({ __array__ => [1..2] }), 'Array::Iterator', 'Creating Array::Iterator object with HASH'); # ok(!defined(Array::Iterator::new())); Array-Iterator-0.135/t/30-basics.t0000644000175000000240000000451114756157521015156 0ustar njhstaff#!/usr/bin/env perl use strict; use warnings; use Test::Most; BEGIN { use_ok('Array::Iterator') } # Create an iterator from a list subtest 'Constructor and Basic Iteration' => sub { my @array = (1, 2, 3, 4, 5); my $iterator = Array::Iterator->new(@array); isa_ok($iterator, 'Array::Iterator', 'Object is an instance of Array::Iterator'); is($iterator->current_index(), 0, 'Initial index should be 0'); ok($iterator->has_next(), 'Iterator should have next element'); is($iterator->next(), 1, 'First call to next() should return first element'); is($iterator->current(), 1, 'current() should return the current element'); }; # Iteration through all elements subtest 'Full Iteration' => sub { my @array = (10, 20, 30); my $iterator = Array::Iterator->new(@array); my @collected; while ($iterator->has_next()) { push @collected, $iterator->next(); } is_deeply(\@collected, \@array, 'Iterated elements should match input array'); ok(!$iterator->has_next(), 'Iterator should be exhausted'); is($iterator->get_next(), undef, 'get_next() should return undef when exhausted'); }; # Peeking ahead subtest 'Peek' => sub { my @array = qw(a b c d e); my $iterator = Array::Iterator->new(@array); is($iterator->peek(1), 'a', 'peek(1) should return first element without advancing'); is($iterator->next(), 'a', 'next() should return first element after peek'); is($iterator->peek(2), 'c', 'peek(2) should return the second element ahead'); lives_ok { $iterator->peek(10) } 'Peeking out of bounds no longer dies'; ok(!defined($iterator->peek(10))); }; # Reset functionality subtest 'Reset' => sub { my @array = (100, 200, 300); my $iterator = Array::Iterator->new(@array); $iterator->next(); $iterator->reset(); is($iterator->current_index(), 0, 'After reset, index should be 0'); is($iterator->next(), 100, 'After reset, next() should return the first element again'); }; # Handling empty array subtest 'Empty Iterator' => sub { my @empty; dies_ok { Array::Iterator->new(@empty) } 'Dies when given empty array to iterate over'; }; # Hash reference initialization subtest 'Iterator with Hash Reference' => sub { my %hash = ( __array__ => [ 'apple', 'banana', 'cherry' ] ); my $iterator = Array::Iterator->new(\%hash); is($iterator->next(), 'apple', 'Iterator should work with hash reference input'); }; # Run all tests done_testing(); Array-Iterator-0.135/t/exceptions.t0000644000175000000240000000647114756100570015652 0ustar njhstaff#!/usr/bin/perl use strict; use warnings; use Test::More tests => 18; use Test::Exception; BEGIN { use_ok('Array::Iterator') }; # test the exceptions # test that the constructor cannot be empty throws_ok { my $i = Array::Iterator->new(); } qr/^Insufficient Arguments\: you must provide something to iterate over/, '... we got the error we expected'; # check that it does not allow non-array ref paramaters throws_ok { my $i = Array::Iterator->new({}); } qr/^Incorrect type\: HASH reference must contain the key __array__/, '... we got the error we expected'; # verify the HASH ref sanity checks throws_ok { my $i = Array::Iterator->new({ no_array_key => [] }); } qr/^Incorrect type\: HASH reference must contain the key __array__/, '... we got the error we expected'; throws_ok { my $i = Array::Iterator->new({ __array__ => "not an array ref" }); } qr/^Incorrect type\: __array__ value must be an ARRAY reference/, '... we got the error we expected'; throws_ok { Array::Iterator->_init(undef, 1); } qr/^Insufficient Arguments\: you must provide an length and an iteratee/, '... we got the error we expected'; throws_ok { Array::Iterator->_init(1); } qr/^Insufficient Arguments\: you must provide an length and an iteratee/, '... we got the error we expected'; # now test the next & peek exceptions my @control = (1 .. 5); my $iterator = Array::Iterator->new(@control); isa_ok($iterator, 'Array::Iterator'); my @_control; push @_control => $iterator->next() while $iterator->hasNext(); ok(!$iterator->hasNext(), '... we are out of elements'); ok(eq_array(\@control, \@_control), '.. make sure all are exhausted'); # test that next will croak if it is called passed the end throws_ok { $iterator->next(); } qr/^Out Of Bounds\: no more elements/, '... we got the error we expected'; # test arbitrary lookups edge cases { my $iterator2 = Array::Iterator->new(@control); throws_ok { $iterator2->has_next(0) } qr/\Qhas_next(0) doesn't make sense/, '... should not be able to call has_next() with zero argument'; throws_ok { $iterator2->has_next(-1) } qr/\Qhas_next() with negative argument doesn't make sense/, '... should not be able to call has_next() with negative argument'; throws_ok { $iterator2->peek(0) } qr/\Qpeek(0) doesn't make sense/, '... should not be able to call peek() with zero argument'; throws_ok { $iterator2->peek(-1) } qr/\Qpeek() with negative argument doesn't make sense/, '... should not be able to call peek() with negative argument'; } # check our protected methods throws_ok { $iterator->_current_index(); } qr/Illegal Operation/, '... got the error we expected'; throws_ok { $iterator->_iteratee(); } qr/Illegal Operation/, '... got the error we expected'; throws_ok { $iterator->_getItem(); } qr/Illegal Operation/, '... got the error we expected'; # ----------------------------------------------- # NOTE: # Test removed, peek no longer dies when it reaches # beyond the iterators bounds, it returns undef instead # ----------------------------------------------- # test that peek will croak if it is called passed the end # throws_ok { # $iterator->peek(); # } qr/^Out Of Bounds\: cannot peek past the end of the array/, # '... we got the error we expected'; # ----------------------------------------------- Array-Iterator-0.135/t/eof.t0000644000175000000240000000035114703752171014233 0ustar njhstaff#!/usr/bin/env perl use strict; use warnings; use Test::DescribeMe qw(author); use Test::Most; use Test::Needs 'Test::EOF'; Test::EOF->import(); all_perl_files_ok({ minimum_newlines => 1, maximum_newlines => 4 }); done_testing(); Array-Iterator-0.135/t/eol.t0000644000175000000240000000030514703752246014243 0ustar njhstaff#!/usr/bin/env perl use strict; use warnings; use Test::DescribeMe qw(author); use Test::Most; use Test::Needs 'Test::EOL'; Test::EOL->import(); all_perl_files_ok({ trailing_whitespace => 1 }); Array-Iterator-0.135/t/00-load.t0000644000175000000240000000037414756107265014631 0ustar njhstaff#!perl -w use warnings; use strict; use Test::Most tests => 2; BEGIN { use_ok('Array::Iterator') || print 'Bail out!'; } require_ok('Array::Iterator') || print 'Bail out!'; diag("Testing Array::Iterator $Array::Iterator::VERSION, Perl $], $^X"); Array-Iterator-0.135/t/pod-coverage.t0000644000175000000240000000033314737554760016050 0ustar njhstaffuse strict; use warnings; use Test::DescribeMe qw(author); use Test::Needs { 'Test::Pod::Coverage' => '1.08', 'Pod::Coverage' => 0.18 }; Test::Pod::Coverage->import(); Pod::Coverage->import(); all_pod_coverage_ok(); Array-Iterator-0.135/t/iterator.t0000644000175000000240000001354714756072035015330 0ustar njhstaff#!/usr/bin/perl use strict; use warnings; use Test::More tests => 99; BEGIN { use_ok('Array::Iterator') }; my @control = (1 .. 5); can_ok("Array::Iterator", 'new'); my $iterator = Array::Iterator->new(@control); isa_ok($iterator, 'Array::Iterator'); # check my private methods can_ok($iterator, '_init'); # check my protected methods can_ok($iterator, '_getItem'); can_ok($iterator, '_current_index'); can_ok($iterator, '_iteratee'); # check out public methods can_ok($iterator, 'hasNext'); can_ok($iterator, 'has_next'); can_ok($iterator, 'next'); can_ok($iterator, 'peek'); can_ok($iterator, 'getNext'); can_ok($iterator, 'get_next'); can_ok($iterator, 'current'); can_ok($iterator, 'currentIndex'); can_ok($iterator, 'current_index'); can_ok($iterator, 'getLength'); can_ok($iterator, 'get_length'); can_ok($iterator, 'iterated'); # now check the behavior ok(!$iterator->iterated(), '... not yet iterated, iterated() is false'); cmp_ok($iterator->getLength(), '==', 5, '... got the right length'); for (my $i = 0; $i < scalar @control; $i++) { # we should still have another one ok($iterator->hasNext(), '... we have more elements'); # and out iterator peek should match our control index # (since we have not incremented the iterator's counter) unless ($i >= (scalar(@control))) { cmp_ok($iterator->peek(), '==', $control[$i], '... our control should match our iterator->peek'); } else { ok(!defined($iterator->peek()), '... this should return undef now'); } # and out iterator should match our control cmp_ok($iterator->next(), '==', $control[$i], '... our control should match our iterator->next'); # and out iterator peek should match our control + 1 (now that we have incremented the counter) unless (($i + 1) >= (scalar(@control))) { cmp_ok($iterator->peek(), '==', $control[$i + 1], '... our control should match our iterator->peek'); } else { ok(!defined($iterator->peek()), '... this should return undef now'); } } ok($iterator->iterated(), '... has been iterated, iterated() is true'); # we should have no more ok(!$iterator->hasNext(), '... we should have no more'); # now use an array ref in the constructor # and try using it in this style loop for (my $i = Array::Iterator->new(\@control); $i->hasNext(); $i->next()) { cmp_ok($i->current(), '==', $control[$i->currentIndex()], '... these should be equal'); } my $iterator2 = Array::Iterator->new(@control); my @acc; push @acc, => $iterator2->next() while $iterator2->hasNext(); # our accumulation and control should be the same ok(eq_array(\@acc, \@control), '... these arrays should be equal'); # we should have no more ok(!$iterator2->hasNext(), '... we should have no more'); { my $iterator3 = Array::Iterator->new(\@control); my $current; while ($current = $iterator3->getNext()) { if ($iterator3->currentIndex() + 1 < (scalar(@control))) { cmp_ok($iterator3->peek(), '==', $control[$iterator3->currentIndex() + 1], '... these should be equal (peek & currentIndex + 1)'); } else { ok(!defined($iterator3->peek()), '... this should return undef now'); } cmp_ok($current, '==', $control[$iterator3->currentIndex()], '... these should be equal (getNext)'); cmp_ok($current, '==', $iterator3->current(), '... these should be equal (getNext)'); } ok(!defined($iterator3->getNext()), '... we should get undef'); # we should have no more ok(!$iterator3->hasNext(), '... we should have no more'); } { # verify that we can pass a hash ref as well my $iterator4 = Array::Iterator->new({ __array__ => \@control }); isa_ok($iterator, 'Array::Iterator'); my $current; while ($current = $iterator4->getNext()) { if ($iterator4->currentIndex() + 1 < (scalar(@control))) { cmp_ok($iterator4->peek(), '==', $control[$iterator4->currentIndex() + 1], '... these should be equal (peek & currentIndex + 1)'); } else { ok(!defined($iterator4->peek()), '... this should return undef now'); } cmp_ok($current, '==', $control[$iterator4->currentIndex()], '... these should be equal (getNext)'); cmp_ok($current, '==', $iterator4->current(), '... these should be equal (getNext)'); } ok(!defined($iterator4->getNext()), '... we should get undef'); # we should have no more ok(!$iterator4->hasNext(), '... we should have no more'); } { # check arbitrary position lookups my $iterator5 = Array::Iterator->new(@control); # when not iterated() ok($iterator5->has_next, '... we should have next element'); ok($iterator5->has_next(1), '... should be the same as has_next()'); ok($iterator5->has_next(2), '... we should have 2nd next element'); ok($iterator5->has_next(5), '... we should have 5th next element'); ok(!$iterator5->has_next(6), '... we should not have 6th next element'); cmp_ok($iterator5->peek(1), '==', $iterator5->peek, '... should be the same as peek()'); cmp_ok($iterator5->peek(2), '==', 2, '... we should get 2nd next element'); cmp_ok($iterator5->peek(5), '==', 5, '... we should get 5th next element'); ok(!defined($iterator5->peek(6)), '... peek() outside of the bounds should return undef'); $iterator5->next; # when iterated() ok($iterator5->has_next(4), '... we should have 4th next element after iterating'); ok(!$iterator5->has_next(5), '... we should not have 5th next element after iterating'); cmp_ok($iterator5->peek(1), '==', $iterator5->peek, '... should be the same as peek() after iterating'); cmp_ok($iterator5->peek(2), '==', 3, '... we should get 2nd next element after iterating'); ok(!defined($iterator5->peek(5)), '... peek() outside of the bounds should return undef after iterating'); } Array-Iterator-0.135/t/reusable.t0000644000175000000240000000121014756072035015261 0ustar njhstaff#!/usr/bin/perl use strict; use warnings; use Test::More tests => 12; BEGIN { use_ok('Array::Iterator::Reusable') }; can_ok("Array::Iterator::Reusable", 'new'); my $i = Array::Iterator::Reusable->new(1 .. 5); isa_ok($i, 'Array::Iterator::Reusable'); isa_ok($i, 'Array::Iterator'); can_ok($i, 'getNext'); can_ok($i, 'get_next'); # exhaust our iterator 1 while $i->getNext(); can_ok($i, 'hasNext'); can_ok($i, 'has_next'); ok(!$i->hasNext(), '... our iterator is exhausted'); can_ok($i, 'reset'); $i->reset(); ok($i->hasNext(), '... our iterator has been reset'); cmp_ok($i->currentIndex(), '==', 0, '... we are back to the begining'); Array-Iterator-0.135/t/legacy_bidirectional.t0000755000175000000240000001014314756227072017626 0ustar njhstaff#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 48; use Test::Exception; BEGIN { use_ok('Array::Iterator::LegacyBiDirectional') }; my @control = (1 .. 5); can_ok('Array::Iterator::LegacyBiDirectional', 'new'); my $iterator = Array::Iterator::LegacyBiDirectional->new(@control); isa_ok($iterator, 'Array::Iterator::LegacyBiDirectional'); isa_ok($iterator, 'Array::Iterator'); # check out public methods can_ok($iterator, 'hasPrevious'); can_ok($iterator, 'has_previous'); can_ok($iterator, 'previous'); can_ok($iterator, 'lookBack'); can_ok($iterator, 'look_back'); can_ok($iterator, 'getPrevious'); can_ok($iterator, 'get_previous'); # now check the behavior # move our counter to the end $iterator->next() while $iterator->hasNext(); for (my $i = $#control; $i > 0; $i--) { # we should still have another one ok($iterator->hasPrevious(), '... we have some previous items'); # and out iterator peek should match our control + 1 unless (($i - 1) <= 0) { cmp_ok($iterator->lookBack(), '==', $control[$i - 1], '... our control should match our iterator->lookBack'); } else { ok(!defined($iterator->lookBack()), '... this should return undef now'); } # and out iterator should match our control cmp_ok($iterator->previous(), '==', $control[$i], '... our control should match our iterator->previous'); } # we should have no more ok(!$iterator->hasPrevious(), '... we should have no more'); # now use an array ref in the constructor # and try using it in this style loop my $iterator2 = Array::Iterator::LegacyBiDirectional->new(\@control); isa_ok($iterator2, 'Array::Iterator::LegacyBiDirectional'); isa_ok($iterator2, 'Array::Iterator'); # move our iterator to the end $iterator2->next() while $iterator2->hasNext(); for (my $i = $iterator2; $i->hasPrevious(); $i->getPrevious()) { cmp_ok($i->current(), '==', $control[$i->currentIndex()], '... these should be equal'); } ok(!defined($iterator2->getPrevious()), '... this should return undef'); throws_ok { $iterator2->previous(); } qr/Out Of Bounds \: no more elements/, '... this should die if i try again'; my $iterator3 = Array::Iterator::LegacyBiDirectional->new(@control); # when not iterated() ok(!$iterator3->has_previous(1), '... should be the same as has_previous()'); ok(!$iterator3->has_previous(2), '... should not have 2nd previous element'); ok(!$iterator3->has_previous(3), '... should not have 3rd previous element'); ok(!defined($iterator3->look_back(1)), '... should be the same as look_back()'); ok(!defined($iterator3->look_back(2)), '... look_back() outside of the bounds should return undef'); ok(!defined($iterator3->look_back(5)), '... look_back() outside of the bounds should return undef'); $iterator3->next while $iterator3->has_next; # when iterated() ok($iterator3->has_previous(1), '... should be the same as has_previous() after iterating'); ok($iterator3->has_previous(2), '... should have 2nd previous element'); cmp_ok($iterator3->look_back(1), '==', $iterator3->look_back, '... should be the same as look_back() after iterating'); cmp_ok($iterator3->look_back(2), '==', 3, '... should get 2nd previous element after iterating'); cmp_ok($iterator3->look_back(3), '==', 2, '... should get 3rd previous element after iterating'); ok(!defined($iterator3->look_back(6)), '... look_back() outside of the bounds should return undef after iterating'); # check arbitrary lookup edge cases throws_ok { $iterator3->has_previous(0) } qr/\Qhas_previous(0) doesn't make sense/, '... should not be able to call has_previous() with zero argument'; throws_ok { $iterator3->has_previous(-1) } qr/\Qhas_previous() with negative argument doesn't make sense/, '... should not be able to call has_previous() with negative argument'; throws_ok { $iterator3->look_back(0) } qr/\Qlook_back(0) doesn't make sense/, '... should not be able to call look_back() with zero argument'; throws_ok { $iterator3->look_back(-1) } qr/\Qlook_back() with negative argument doesn't make sense/, '... should not be able to call look_back() with negative argument'; Array-Iterator-0.135/README.md0000644000175000000240000002661414756230553014326 0ustar njhstaff# NAME Array::Iterator - A simple class for iterating over Perl arrays # VERSION Version 0.135 # SYNOPSIS `Array::Iterator` is a Perl module that provides a simple, uni-directional iterator interface for traversing arrays. It allows users to iterate over arrays, array references, or hash references containing an array, offering methods like next, has\_next, peek, and current to facilitate controlled access to elements. The iterator maintains an internal pointer, ensuring elements are accessed sequentially without modifying the underlying array. Tt offers a clean, object-oriented approach to iteration, inspired by Java’s Iterator interface. The module is extendable, allowing subclassing for custom behaviour. use Array::Iterator; # create an iterator with an array my $i = Array::Iterator->new(1 .. 100); # create an iterator with an array reference my $i = Array::Iterator->new(\@array); # create an iterator with a hash reference my $i = Array::Iterator->new({ __array__ => \@array }); # a base iterator example while ($i->has_next()) { if ($i->peek() < 50) { # ... do something because # the next element is over 50 } my $current = $i->next(); # ... do something with current } # shortcut style my @accumulation; push @accumulation => { item => $iterator->next() } while $iterator->has_next(); # C++ ish style iterator for (my $i = Array::Iterator->new(@array); $i->has_next(); $i->next()) { my $current = $i->current(); # .. do something with current } # common perl iterator idiom my $current; while ($current = $i->get_next()) { # ... do something with $current } It is not recommended to alter the array during iteration, however no attempt is made to enforce this (although I will if I can find an efficient means of doing so). This class only intends to provide a clear and simple means of generic iteration, nothing more (yet). ## new (@array | $array\_ref | $hash\_ref) The constructor can be passed either a plain Perl array, an array reference, or a hash reference (with the array specified as a single key of the hash, \_\_array\_\_). Single-element arrays are not supported by either of the first two calling conventions, since it is not possible to distinguish between an array of a single-element which happens to be an array reference and an array reference of a single element, thus previous versions of the constructor would raise an exception. If you expect to pass arrays to the constructor which may have only a single element, then the array can be passed as the element of a HASH reference, with the key, \_\_array\_\_: my $i = Array::Iterator->new({ __array__ => \@array }); ## \_current\_index An lvalue-ed subroutine that allows access to the iterator's internal pointer. This can be used in a subclass to access the value. ## \_iteratee This returns the item being iterated over, in our case an array. ## \_get\_item ($iteratee, $index) This method is used by all other routines to access items. Given the iteratee and an index, it will return the item being stored in the `$iteratee` at the index of `$index`. ## iterated Access to the \_iterated status, for subclasses ## has\_next(\[$n\]) This method returns a boolean. True (1) if there are still more elements in the iterator, false (0) if there are not. Takes an optional positive integer (> 0) that specifies the position you want to check. This allows you to check if there an element at an arbitrary position. Think of it as an ordinal number you want to check: $i->has_next(2); # 2nd next element $i->has_next(10); # 10th next element Note that `has_next(1)` is the same as `has_next()`. Throws an exception if `$n` <= 0. ## hasNext Alternative name for has\_next ## next This method returns the next item in the iterator, be sure to only call this once per iteration as it will advance the index pointer to the next item. If this method is called after all elements have been exhausted, an exception will be thrown. ## get\_next This method returns the next item in the iterator, be sure to only call this once per iteration as it will advance the index pointer to the next item. If this method is called after all elements have been exhausted, it will return undef. This method was added to allow for a fairly common Perl iterator idiom of: my $current; while ($current = $i->get_next()) { ... } In this, the loop terminates once `$current` is assigned to a false value. The only problem with this idiom for me is that it does not allow for undefined or false values in the iterator. Of course, if this fits your data, then there is no problem. Otherwise I would recommend the `has_next`/`next` idiom instead. ## getNext Alternative name for get\_next ## peek(\[$n\]) This method can be used to peek ahead at the next item in the iterator. It is non-destructive, meaning it does not advance the internal pointer. If this method is called and attempts to reach beyond the bounds of the iterator, it will return undef. Takes an optional positive integer (> 0) that specifies how far ahead you want to peek: $i->peek(2); # gives you 2nd next element $i->peek(10); # gives you 10th next element Note that `peek(1)` is the same as `peek()`. Throws an exception if `$n` <= 0. **NOTE:** Before version 0.03 this method would throw an exception if called out of bounds. I decided this was not a good practice, as it made it difficult to be able to peek ahead effectively. This is not the case when calling with an argument that is <= 0 though, as it's clearly a sign of incorrect usage. ## current This method can be used to get the current item in the iterator. It is non-destructive, meaning that it does not advance the internal pointer. This value will match the last value dispensed by `next` or `get_next`. ## current\_index This method can be used to get the current index in the iterator. It is non-destructive, meaning that it does not advance the internal pointer. This value will match the index of the last value dispensed by `next` or `get_next`. ## currentIndex Alternative name for current\_index ## reset Reset index to allow iteration from the start ## get\_length This is a basic accessor for getting the length of the array being iterated over. ## getLength Alternative name for get\_length # TODO - Improve BiDirectional Test suite I want to test the back-and-forth a little more and make sure they work well with one another. - Other Iterators Array::Iterator::BiDirectional::Circular, Array::Iterator::Skipable and Array::Iterator::BiDirectional::Skipable are just a few ideas I have had. I am going to hold off for now until I am sure they are actually useful. # SEE ALSO This module now includes several subclasses of Array::Iterator which add certain behaviors to Array::Iterator, they are: - `Array::Iterator::BiDirectional` Adds the ability to move backward and forward through the array. - `Array::Iterator::Circular` When this iterator reaches the end of its list, it will loop back to the start again. - `Array::Iterator::Reusable` This iterator can be reset to its beginning and used again. The Design Patterns book by the Gang of Four, specifically the Iterator pattern. Some of the interface for this class is based on the Java Iterator interface. # OTHER ITERATOR MODULES There are several on CPAN with the word Iterator in them. Most of them are actually iterators included inside other modules, and only really useful within that parent module's context. There are, however, some other modules out there that are just for pure iteration. I have provided a list below of the ones I have found if perhaps you don't happen to like the way I do it. - Tie::Array::Iterable This module ties the array, something we do not do. But it also makes an attempt to account for, and allow the array to be changed during iteration. It accomplishes this control because the underlying array is tied. As we all know, tie-ing things can be a performance issue, but if you need what this module provides, then it will likely be an acceptable compromise. Array::Iterator makes no attempt to deal with this mid-iteration manipulation problem. In fact, it is recommended to not alter your array with Array::Iterator, and if possible we will enforce this in later versions. - Data::Iter This module allows for simple iteration over both hashes and arrays. It does it by importing several functions that can be used to loop over either type (hash or array) in the same way. It is an interesting module, it differs from Array::Iterator in paradigm (Array::Iterator is more OO) and intent. - Class::Iterator This is essentially a wrapper around a closure-based iterator. This method can be very flexible, but at times is difficult to manage due to the inherent complexity of using closures. I actually was a closure-as-iterator fan for a while but eventually moved away from it in favor of the more plain vanilla means of iteration, like that found Array::Iterator. - Class::Iter This is part of the Class::Visitor module and is a Visitor and Iterator extension to Class::Template. Array::Iterator is a standalone module that is not associated with others. - **Data::Iterator::EasyObj** Data::Iterator::EasyObj makes your array of arrays into iterator objects. It also can further nest additional data structures including Data::Iterator::EasyObj objects. Array::Iterator is one-dimensional only and does not attempt to do many of the more advanced features of this module. # ACKNOWLEDGEMENTS - Thanks to Hugo Cornelis for pointing out a bug in `peek()` - Thanks to Phillip Moore for providing the patch to allow single element iteration through the hash-ref constructor parameter. # ORIGINAL AUTHOR stevan little, # ORIGINAL COPYRIGHT AND LICENSE Copyright 2004, 2005 by Infinity Interactive, Inc. [http://www.iinteractive.com](http://www.iinteractive.com) This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. # PREVIOUS MAINTAINER Maintained 2017 to 2025 PERLANCAR # SUPPORT This module is provided as-is without any warranty. Please report any bugs or feature requests to `bug-array-iterator at rt.cpan.org`, or through the web interface at [http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Array-Iterator](http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Array-Iterator). I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. You can find documentation for this module with the perldoc command. perldoc Array::Iterator You can also look for information at: - MetaCPAN [https://metacpan.org/dist/Array-Iterator](https://metacpan.org/dist/Array-Iterator) - RT: CPAN's request tracker [https://rt.cpan.org/NoAuth/Bugs.html?Dist=Array-Iterator](https://rt.cpan.org/NoAuth/Bugs.html?Dist=Array-Iterator) - CPAN Testers' Matrix [http://matrix.cpantesters.org/?dist=Array-Iterator](http://matrix.cpantesters.org/?dist=Array-Iterator) - CPAN Testers Dependencies [http://deps.cpantesters.org/?module=Array::Iterator](http://deps.cpantesters.org/?module=Array::Iterator) # POD ERRORS Hey! **The above document had some coding errors, which are explained below:** - Around line 24: Non-ASCII character seen before =encoding in 'Java’s'. Assuming UTF-8 Array-Iterator-0.135/MANIFEST.SKIP0000644000175000000240000000211414756076477014747 0ustar njhstaff #!start included /usr/local/perls/perl-5.18.1/lib/5.18.1/ExtUtils/MANIFEST.SKIP # Avoid version control files. \bRCS\b \bCVS\b \bSCCS\b ,v$ \B\.svn\b \B\.git\b \B\.github\b \B\.gitignore\b \b_darcs\b \B\.cvsignore$ # Avoid VMS specific MakeMaker generated files \bDescrip.MMS$ \bDESCRIP.MMS$ \bdescrip.mms$ # Avoid Makemaker generated and utility files. \bMANIFEST\.bak \bMakefile$ \bblib/ \bMakeMaker-\d \bpm_to_blib\.ts$ \bpm_to_blib$ \bblibdirs\.ts$ # 6.18 through 6.25 generated this # Avoid Module::Build generated and utility files. \bBuild$ \b_build/ \bBuild.bat$ \bBuild.COM$ \bBUILD.COM$ \bbuild.com$ # Avoid temp and backup files. ~$ \.old$ \#$ \b\.# \.bak$ \.tmp$ \.# \.rej$ \.yml$ \.swp$ # Avoid OS-specific files/dirs # Mac OSX metadata \B\.DS_Store # Mac OSX SMB mount metadata files \B\._ # Avoid Devel::Cover and Devel::CoverX::Covered files. \bcover_db\b \bcovered\b # Avoid MYMETA files ^MYMETA\. #!end included /usr/local/perls/perl-5.18.1/lib/5.18.1/ExtUtils/MANIFEST.SKIP \.travis\.yml \.appveyor\.yml \.releaserc \.lwpcookies Array-.* .test-script-* typescript Array-Iterator-0.135/META.yml0000644000175000000240000000125214756230666014314 0ustar njhstaff--- abstract: 'A simple class for iterating over Perl arrays' author: - 'Nigel Horne ' build_requires: ExtUtils::MakeMaker: '0' Test::DescribeMe: '0' Test::Most: '0' Test::Needs: '0' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 7.70, CPAN::Meta::Converter version 2.150010' license: open_source meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Array-Iterator no_index: directory: - t - inc requires: Carp: '0' ExtUtils::MakeMaker: '6.64' resources: {} version: '0.135' x_serialization_backend: 'CPAN::Meta::YAML version 0.020' Array-Iterator-0.135/lib/0000755000175000000240000000000014756230666013611 5ustar njhstaffArray-Iterator-0.135/lib/Array/0000755000175000000240000000000014756230666014667 5ustar njhstaffArray-Iterator-0.135/lib/Array/Iterator.pm0000644000175000000240000003660014756230415017013 0ustar njhstaffpackage Array::Iterator; use strict; use warnings; =head1 NAME Array::Iterator - A simple class for iterating over Perl arrays =head1 VERSION Version 0.135 =cut our $VERSION = '0.135'; =head1 SYNOPSIS C is a Perl module that provides a simple, uni-directional iterator interface for traversing arrays. It allows users to iterate over arrays, array references, or hash references containing an array, offering methods like next, has_next, peek, and current to facilitate controlled access to elements. The iterator maintains an internal pointer, ensuring elements are accessed sequentially without modifying the underlying array. Tt offers a clean, object-oriented approach to iteration, inspired by Java’s Iterator interface. The module is extendable, allowing subclassing for custom behaviour. use Array::Iterator; # create an iterator with an array my $i = Array::Iterator->new(1 .. 100); # create an iterator with an array reference my $i = Array::Iterator->new(\@array); # create an iterator with a hash reference my $i = Array::Iterator->new({ __array__ => \@array }); # a base iterator example while ($i->has_next()) { if ($i->peek() < 50) { # ... do something because # the next element is over 50 } my $current = $i->next(); # ... do something with current } # shortcut style my @accumulation; push @accumulation => { item => $iterator->next() } while $iterator->has_next(); # C++ ish style iterator for (my $i = Array::Iterator->new(@array); $i->has_next(); $i->next()) { my $current = $i->current(); # .. do something with current } # common perl iterator idiom my $current; while ($current = $i->get_next()) { # ... do something with $current } It is not recommended to alter the array during iteration, however no attempt is made to enforce this (although I will if I can find an efficient means of doing so). This class only intends to provide a clear and simple means of generic iteration, nothing more (yet). =head2 new (@array | $array_ref | $hash_ref) The constructor can be passed either a plain Perl array, an array reference, or a hash reference (with the array specified as a single key of the hash, __array__). Single-element arrays are not supported by either of the first two calling conventions, since it is not possible to distinguish between an array of a single-element which happens to be an array reference and an array reference of a single element, thus previous versions of the constructor would raise an exception. If you expect to pass arrays to the constructor which may have only a single element, then the array can be passed as the element of a HASH reference, with the key, __array__: my $i = Array::Iterator->new({ __array__ => \@array }); =cut sub new { my ($_class, @array) = @_; (@array) || die 'Insufficient Arguments: you must provide something to iterate over'; my $class = ref($_class) || $_class; my $_array; if (scalar @array == 1) { if (ref $array[0] eq 'ARRAY') { $_array = $array[0]; } elsif (ref $array[0] eq 'HASH') { die 'Incorrect type: HASH reference must contain the key __array__' unless exists $array[0]->{__array__}; die 'Incorrect type: __array__ value must be an ARRAY reference' unless ref $array[0]->{__array__} eq 'ARRAY'; $_array = $array[0]->{__array__}; } } else { $_array = \@array; } my $iterator = { _current_index => 0, _length => 0, _iteratee => [], _iterated => 0, # -1 when going backwards, +1 when going forwards }; bless($iterator, $class); $iterator->_init(scalar(@{$_array}), $_array); return $iterator; } sub _init { my ($self, $length, $iteratee) = @_; (defined($length) && defined($iteratee)) || die 'Insufficient Arguments: you must provide an length and an iteratee'; $self->{_current_index} = 0; $self->{_length} = $length; # $self->{_iteratee} = $iteratee; # Store a private copy to prevent modifications $self->{_iteratee} = [@{$iteratee}]; } =head2 _current_index An lvalue-ed subroutine that allows access to the iterator's internal pointer. This can be used in a subclass to access the value. =cut # We need to alter this so it's an lvalue sub _current_index : lvalue { (UNIVERSAL::isa((caller)[0], __PACKAGE__)) || die 'Illegal Operation: This method can only be called by a subclass'; $_[0]->{_current_index} } =head2 _iteratee This returns the item being iterated over, in our case an array. =cut # This we should never need to alter so we don't make it a lvalue sub _iteratee { (UNIVERSAL::isa((caller)[0], __PACKAGE__)) || die 'Illegal Operation: This method can only be called by a subclass'; $_[0]->{_iteratee} } # we move this from a private method # to a protected one, and check our access # as well sub _getItem { (UNIVERSAL::isa((caller)[0], __PACKAGE__)) || die 'Illegal Operation: This method can only be called by a subclass'; my ($self, $iteratee, $index) = @_; return $iteratee->[$index]; } =head2 _get_item ($iteratee, $index) This method is used by all other routines to access items. Given the iteratee and an index, it will return the item being stored in the C<$iteratee> at the index of C<$index>. =cut sub _get_item { my $self = shift; $self->_getItem(@_) } # we need to alter this so it's an lvalue sub _iterated : lvalue { (UNIVERSAL::isa((caller)[0], __PACKAGE__)) || die 'Illegal Operation: This method can only be called by a subclass'; $_[0]->{_iterated} } =head2 iterated Access to the _iterated status, for subclasses =cut sub iterated { my ($self) = @_; return $self->{_iterated}; } =head2 has_next([$n]) This method returns a boolean. True (1) if there are still more elements in the iterator, false (0) if there are not. Takes an optional positive integer (E 0) that specifies the position you want to check. This allows you to check if there an element at an arbitrary position. Think of it as an ordinal number you want to check: $i->has_next(2); # 2nd next element $i->has_next(10); # 10th next element Note that C is the same as C. Throws an exception if C<$n> E= 0. =cut sub has_next { my ($self, $n) = @_; if(not defined $n) { $n = 1 } elsif(not $n) { die "has_next(0) doesn't make sense, did you mean current()?" } elsif($n < 0) { die "has_next() with negative argument doesn't make sense, perhaps you should use a BiDirectional iterator" } my $idx = $self->{_current_index} + ($n - 1); return ($idx < $self->{_length}) ? 1 : 0; } =head2 hasNext Alternative name for has_next =cut sub hasNext { my $self = shift; $self->has_next(@_) } =head2 next This method returns the next item in the iterator, be sure to only call this once per iteration as it will advance the index pointer to the next item. If this method is called after all elements have been exhausted, an exception will be thrown. =cut sub next { my $self = shift; ($self->{_current_index} < $self->{_length}) || die 'Out Of Bounds: no more elements'; $self->{_iterated} = 1; return $self->_getItem($self->{_iteratee}, $self->{_current_index}++); } =head2 get_next This method returns the next item in the iterator, be sure to only call this once per iteration as it will advance the index pointer to the next item. If this method is called after all elements have been exhausted, it will return undef. This method was added to allow for a fairly common Perl iterator idiom of: my $current; while ($current = $i->get_next()) { ... } In this, the loop terminates once C<$current> is assigned to a false value. The only problem with this idiom for me is that it does not allow for undefined or false values in the iterator. Of course, if this fits your data, then there is no problem. Otherwise I would recommend the C/C idiom instead. =cut sub get_next { my ($self) = @_; $self->{_iterated} = 1; return undef unless ($self->{_current_index} < $self->{_length}); ## no critic: Subroutines::ProhibitExplicitReturnUndef return $self->_getItem($self->{_iteratee}, $self->{_current_index}++); } =head2 getNext Alternative name for get_next =cut sub getNext { my $self = shift; $self->get_next(@_) } =head2 peek([$n]) This method can be used to peek ahead at the next item in the iterator. It is non-destructive, meaning it does not advance the internal pointer. If this method is called and attempts to reach beyond the bounds of the iterator, it will return undef. Takes an optional positive integer (E 0) that specifies how far ahead you want to peek: $i->peek(2); # gives you 2nd next element $i->peek(10); # gives you 10th next element Note that C is the same as C. Throws an exception if C<$n> E= 0. B Before version 0.03 this method would throw an exception if called out of bounds. I decided this was not a good practice, as it made it difficult to be able to peek ahead effectively. This is not the case when calling with an argument that is E= 0 though, as it's clearly a sign of incorrect usage. =cut sub peek { my ($self, $n) = @_; if(not defined $n) { $n = 1 } elsif(not $n) { die "peek(0) doesn't make sense, did you mean get_next()?" } elsif($n < 0) { die "peek() with negative argument doesn't make sense, perhaps you should use a BiDirectional iterator" } my $idx = $self->{_current_index} + ($n - 1); return undef unless ($idx < $self->{_length}); ## no critic: Subroutines::ProhibitExplicitReturnUndef return $self->_getItem($self->{_iteratee}, $idx); } =head2 current This method can be used to get the current item in the iterator. It is non-destructive, meaning that it does not advance the internal pointer. This value will match the last value dispensed by C or C. =cut sub current { my ($self) = @_; return $self->_getItem($self->{_iteratee}, $self->currentIndex()); } =head2 current_index This method can be used to get the current index in the iterator. It is non-destructive, meaning that it does not advance the internal pointer. This value will match the index of the last value dispensed by C or C. =cut sub current_index { my ($self) = @_; return ($self->{_current_index} != 0) ? $self->{_current_index} - 1 : 0; } =head2 currentIndex Alternative name for current_index =cut sub currentIndex { my $self = shift; $self->current_index(@_) } =head2 reset Reset index to allow iteration from the start =cut sub reset { my $self = shift; $self->{'_current_index'} = 0; } =head2 get_length This is a basic accessor for getting the length of the array being iterated over. =cut sub get_length { my $self = shift; return $self->{_length}; } =head2 getLength Alternative name for get_length =cut sub getLength { my $self = shift; $self->get_length(@_) } 1; =head1 TODO =over 4 =item Improve BiDirectional Test suite I want to test the back-and-forth a little more and make sure they work well with one another. =item Other Iterators Array::Iterator::BiDirectional::Circular, Array::Iterator::Skipable and Array::Iterator::BiDirectional::Skipable are just a few ideas I have had. I am going to hold off for now until I am sure they are actually useful. =back =head1 SEE ALSO This module now includes several subclasses of Array::Iterator which add certain behaviors to Array::Iterator, they are: =over 4 =item C Adds the ability to move backward and forward through the array. =item C When this iterator reaches the end of its list, it will loop back to the start again. =item C This iterator can be reset to its beginning and used again. =back The Design Patterns book by the Gang of Four, specifically the Iterator pattern. Some of the interface for this class is based on the Java Iterator interface. =head1 OTHER ITERATOR MODULES There are several on CPAN with the word Iterator in them. Most of them are actually iterators included inside other modules, and only really useful within that parent module's context. There are, however, some other modules out there that are just for pure iteration. I have provided a list below of the ones I have found if perhaps you don't happen to like the way I do it. =over 4 =item Tie::Array::Iterable This module ties the array, something we do not do. But it also makes an attempt to account for, and allow the array to be changed during iteration. It accomplishes this control because the underlying array is tied. As we all know, tie-ing things can be a performance issue, but if you need what this module provides, then it will likely be an acceptable compromise. Array::Iterator makes no attempt to deal with this mid-iteration manipulation problem. In fact, it is recommended to not alter your array with Array::Iterator, and if possible we will enforce this in later versions. =item Data::Iter This module allows for simple iteration over both hashes and arrays. It does it by importing several functions that can be used to loop over either type (hash or array) in the same way. It is an interesting module, it differs from Array::Iterator in paradigm (Array::Iterator is more OO) and intent. =item Class::Iterator This is essentially a wrapper around a closure-based iterator. This method can be very flexible, but at times is difficult to manage due to the inherent complexity of using closures. I actually was a closure-as-iterator fan for a while but eventually moved away from it in favor of the more plain vanilla means of iteration, like that found Array::Iterator. =item Class::Iter This is part of the Class::Visitor module and is a Visitor and Iterator extension to Class::Template. Array::Iterator is a standalone module that is not associated with others. =item B Data::Iterator::EasyObj makes your array of arrays into iterator objects. It also can further nest additional data structures including Data::Iterator::EasyObj objects. Array::Iterator is one-dimensional only and does not attempt to do many of the more advanced features of this module. =back =head1 ACKNOWLEDGEMENTS =over 4 =item Thanks to Hugo Cornelis for pointing out a bug in C =item Thanks to Phillip Moore for providing the patch to allow single element iteration through the hash-ref constructor parameter. =back =head1 ORIGINAL AUTHOR stevan little, Estevan@iinteractive.comE =head1 ORIGINAL COPYRIGHT AND LICENSE Copyright 2004, 2005 by Infinity Interactive, Inc. L This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 PREVIOUS MAINTAINER Maintained 2017 to 2025 PERLANCAR =head1 SUPPORT This module is provided as-is without any warranty. Please report any bugs or feature requests to C, or through the web interface at L. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. You can find documentation for this module with the perldoc command. perldoc Array::Iterator You can also look for information at: =over 4 =item * MetaCPAN L =item * RT: CPAN's request tracker L =item * CPAN Testers' Matrix L =item * CPAN Testers Dependencies L =back =cut Array-Iterator-0.135/lib/Array/Iterator/0000755000175000000240000000000014756230666016460 5ustar njhstaffArray-Iterator-0.135/lib/Array/Iterator/LegacyBiDirectional.pm0000644000175000000240000001026114756230424022643 0ustar njhstaffpackage Array::Iterator::LegacyBiDirectional; use strict; use warnings; use Array::Iterator; # AUTHORITY # DATE # DIST =head1 VERSION Version 0.135 =cut our $VERSION = '0.135'; =head1 SYNOPSIS use Array::Iterator::LegacyBiDirectional; # create an instance of the iterator my $i = Array::Iterator::LegacyBiDirectional->new(1 .. 100); while ($some_condition_exists) { # get the latest item from # the iterator my $current = $i->get_next(); # ... if ($something_happens) { # back up the iterator $current = $i->get_previous(); } } =head1 DESCRIPTION This is the old BiDirectional code. It is kept for users who want the old way that the pointer was kept. See RT#126034 for further details. Occasionally it is useful for an iterator to go in both directions, forward and backward. One example would be token processing. When looping though tokens it is sometimes necessary to advance forward looking for a match to a rule. If the match fails, a bi-directional iterator can be moved back so that the next rule can be tried. =cut our @ISA = qw(Array::Iterator); sub has_previous { my ($self, $n) = @_; if(not defined $n) { $n = 1 } elsif(not $n) { die "has_previous(0) doesn't make sense, did you mean current()?" } elsif($n < 0) { die "has_previous() with negative argument doesn't make sense, did you mean has_next()?" } my $idx = $self->_current_index - $n; return ($idx > 0) ? 1 : 0; } sub hasPrevious { my $self = shift; $self->has_previous(@_) } sub previous { my ($self) = @_; (($self->_current_index - 1) > 0) || die "Out Of Bounds : no more elements"; $self->_iterated = 1; return $self->_getItem($self->_iteratee, --$self->_current_index); } sub get_previous { my ($self) = @_; return undef unless (($self->_current_index - 1) > 0); ## no critic: Subroutines::ProhibitExplicitReturnUndef $self->_iterated = 1; return $self->_getItem($self->_iteratee, --$self->_current_index); } sub getPrevious { my $self = shift; $self->get_previous(@_) } sub look_back { my ($self, $n) = @_; if(not defined $n) { $n = 1 } elsif(not $n) { die "look_back(0) doesn't make sense, did you mean get_previous()?" } elsif($n < 0) { die "look_back() with negative argument doesn't make sense, did you mean get_next()?" } my $idx = $self->_current_index - ($n + 1); return undef unless ($idx > 0); ## no critic: Subroutines::ProhibitExplicitReturnUndef $self->_iterated = 1; return $self->_getItem($self->_iteratee, $idx); } sub lookBack { my $self = shift; $self->look_back(@_) } 1; #ABSTRACT: A subclass of Array::Iterator to allow forwards and backwards iteration =for Pod::Coverage .+ =head1 METHODS This is a subclass of Array::Iterator, only those methods that have been added are documented here, refer to the Array::Iterator documentation for more information. =over 4 =item B This method works much like C does, it will return true (C<1>) unless the beginning of the array has been reached, and false (C<0>) otherwise. Optional argument has the same meaning except that it specifies C<$n>th previous element. =item B This method is much like C. It will return the previous item in the iterator, and throw an exception if it attempts to reach past the beginning of the array. =item B This method is much like C. It will return the previous item in the iterator, and return undef if it attempts to reach past the beginning of the array. =item B This is the counterpart to C, it will return the previous items in the iterator, but will not affect the internal counter. Optional argument has the same meaning except that it specifies C<$n>th previous element. =back =head1 SEE ALSO This is a subclass of B, please refer to it for more documentation. =head1 ORIGINAL AUTHOR stevan little, Estevan@iinteractive.comE =head1 ORIGINAL COPYRIGHT AND LICENSE Copyright 2004 by Infinity Interactive, Inc. L This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut Array-Iterator-0.135/lib/Array/Iterator/Reusable.pm0000644000175000000240000000326314756230432020553 0ustar njhstaff package Array::Iterator::Reusable; use strict; use warnings; use Array::Iterator; # AUTHORITY # DATE # DIST =head1 VERSION Version 0.135 =cut our $VERSION = '0.135'; our @ISA = qw(Array::Iterator); sub reset { my ($self) = @_; $self->_iterated = 0; $self->_current_index = 0; } 1; #ABSTRACT: A subclass of Array::Iterator to allow reuse of iterators =for Pod::Coverage .+ =head1 SYNOPSIS use Array::Iterator::Reusable; # create an iterator with an array my $i = Array::Iterator::Reusable->new(1 .. 100); # do something with the iterator my @accumulation; push @accumulation => { item => $iterator->next() } while $iterator->has_next(); # now reset the iterator so we can do it again $iterator->reset(); =head1 DESCRIPTION Sometimes you don't want to have to throw out your iterator each time you have exhausted it. This class adds the C method to allow reuse of an iterator. This is a very simple addition to the Array::Iterator class of a single method. =head1 METHODS This is a subclass of Array::Iterator, only those methods that have been added are documented here, refer to the Array::Iterator documentation for more information. =over 4 =item B This resets the internal counter of the iterator back to the start of the array. =back =head1 SEE ALSO This is a subclass of B, please refer to it for more documentation. =head1 ORIGINAL AUTHOR stevan little, Estevan@iinteractive.comE =head1 ORIGINAL COPYRIGHT AND LICENSE Copyright 2004 by Infinity Interactive, Inc. L This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut Array-Iterator-0.135/lib/Array/Iterator/BiDirectional.pm0000644000175000000240000001055514756230437021530 0ustar njhstaffpackage Array::Iterator::BiDirectional; use strict; use warnings; use Array::Iterator; use Carp; # AUTHORITY # DATE # DIST =head1 VERSION Version 0.135 =cut our $VERSION = '0.135'; =head1 SYNOPSIS Occasionally it is useful for an iterator to go in both directions, forward and backward. One example would be token processing. When looping though tokens it is sometimes necessary to advance forward looking for a match to a rule. If the match fails, a bi-directional iterator can be moved back so that the next rule can be tried. use Array::Iterator::BiDirectional; # create an instance of the iterator my $i = Array::Iterator::BiDirectional->new(1 .. 100); while ($some_condition_exists) { # get the latest item from # the iterator my $current = $i->get_next(); # ... if ($something_happens) { # back up the iterator $current = $i->get_previous(); } } =cut our @ISA = qw(Array::Iterator); sub has_previous { my ($self, $n) = @_; if(not defined $n) { $n = 1 } elsif(not $n) { die "has_previous(0) doesn't make sense, did you mean current()?" } elsif($n < 0) { die "has_previous() with negative argument doesn't make sense, did you mean has_next()?" } my $idx = $self->_current_index - $n; if(!defined($self->{_iterated}) || ($self->{_iterated} >= 0)) { return ($idx > 0) ? 1 : 0; } return ($idx >= 0) ? 1 : 0; } sub hasPrevious { my $self = shift; $self->has_previous(@_) } sub previous { my $self = shift; if($self->{'_iterated'} >= 0) { (($self->_current_index - 1) > 0) || Carp::croak('previous: Out Of Bounds: no more elements'); } else { (($self->_current_index - 1) >= 0) || Carp::croak('previous: Out Of Bounds: no more elements'); } $self->_iterated = -1; return $self->_getItem($self->_iteratee, --$self->_current_index); } sub get_previous { my $self = shift; return undef unless $self->hasPrevious(); ## no critic: Subroutines::ProhibitExplicitReturnUndef if($self->_iterated == 1) { # RT126034 --$self->{_current_index}; } $self->_iterated = -1; return undef unless $self->hasPrevious(); ## no critic: Subroutines::ProhibitExplicitReturnUndef return $self->_getItem($self->_iteratee, --$self->{_current_index}); } sub getPrevious { my $self = shift; $self->get_previous(@_) } sub look_back { my ($self, $n) = @_; if(not defined $n) { $n = 1 } elsif(not $n) { die "look_back(0) doesn't make sense, did you mean get_previous()?" } elsif($n < 0) { die "look_back() with negative argument doesn't make sense, did you mean get_next()?" } my $idx = $self->_current_index - ($n + 1); return undef unless ($idx > 0); ## no critic: Subroutines::ProhibitExplicitReturnUndef return $self->_getItem($self->_iteratee, $idx); } sub lookBack { my $self = shift; $self->look_back(@_) } 1; #ABSTRACT: A subclass of Array::Iterator to allow forwards and backwards iteration =for Pod::Coverage .+ =head1 METHODS This is a subclass of Array::Iterator, only those methods that have been added are documented here, refer to the Array::Iterator documentation for more information. =over 4 =item B This method works much like C does, it will return true (C<1>) unless the beginning of the array has been reached, and false (C<0>) otherwise. Optional argument has the same meaning except that it specifies C<$n>th previous element. =item B This method is much like C. It will return the previous item in the iterator, and throw an exception if it attempts to reach past the beginning of the array. =item B This method is much like C. It will return the previous item in the iterator, and return undef if it attempts to reach past the beginning of the array. =item B This is the counterpart to C, it will return the previous items in the iterator, but will not affect the internal counter. Optional argument has the same meaning except that it specifies C<$n>th previous element. =back =head1 SEE ALSO This is a subclass of B, please refer to it for more documentation. =head1 ORIGINAL AUTHOR stevan little, Estevan@iinteractive.comE =head1 ORIGINAL COPYRIGHT AND LICENSE Copyright 2004 by Infinity Interactive, Inc. L This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut Array-Iterator-0.135/lib/Array/Iterator/Circular.pm0000644000175000000240000000704114756230444020556 0ustar njhstaff package Array::Iterator::Circular; use strict; use warnings; use Array::Iterator; # AUTHORITY # DATE # DIST =head1 VERSION Version 0.135 =cut our $VERSION = '0.135'; our @ISA = qw(Array::Iterator); sub _init { my ($self, @args) = @_; $self->{loop_counter} = 0; $self->SUPER::_init(@args); } # always return true, since # we just keep looping sub has_next { 1 } sub next { my ($self) = @_; unless ($self->_current_index < $self->getLength()) { $self->_current_index = 0; $self->{loop_counter}++; } $self->_iterated = 1; return $self->_getItem($self->_iteratee(), $self->_current_index++); } # since neither of them will # ever stop dispensing items # they can just be aliases of # one another. *get_next = \&next; sub is_start { my ($self) = @_; return ($self->_current_index() == 0); } sub isStart { my $self = shift; $self->is_start(@_) } sub is_end { my ($self) = @_; return ($self->_current_index() == $self->getLength()); } sub isEnd { my $self = shift; $self->is_end(@_) } sub get_loop_count { my ($self) = @_; return $self->{loop_counter}; } sub getLoopCount { my $self = shift; $self->get_loop_count(@_) } 1; #ABSTRACT: A subclass of Array::Iterator to allow circular iteration =for Pod::Coverage .+ =head1 SYNOPSIS use Array::Iterator::Circular; # create an instance with a # small array my $color_iterator = Array::Iterator::Circular->new(qw(red green blue orange)); # this is a large list of # arbitrary items my @long_list_of_items = ( ... ); # as we loop through the items ... foreach my $item (@long_list_of_items) { # we assign color from our color # iterator, which will keep dispensing # as it loops through its set $item->set_color($color_iterator->next()); } # tell us how many times the set # was looped through print $color_iterator->get_loop_count(); =head1 DESCRIPTION This iterator will loop continuosly as long as C or C is called. The C method will always return true (C<1>), since the list will always loop back. This is useful when you need a list to repeat itself, but don't want to (or care to) know that it is doing so. =head1 METHODS This is a subclass of Array::Iterator, only those methods that have been added or altered are documented here, refer to the Array::Iterator documentation for more information. =over 4 =item B Since we endlessly loop, this will always return true (C<1>). =item B This will return the next item in the array, and when it reaches the end of the array, it will loop back to the beginning again. =item B This method is now defined in terms of C, since neither will even stop dispensing items, there is no need to differentiate. =item B If at anytime during your looping, you want to know if you have arrived back at the start of you list, you can ask this method. =item B If at anytime during your looping, you want to know if you have gotten to the end of you list, you can ask this method. =item B This method will tell you how many times the iterator has looped back to its start. =back =head1 SEE ALSO This is a subclass of B, please refer to it for more documentation. =head1 ORIGINAL AUTHOR stevan little, Estevan@iinteractive.comE =head1 ORIGINAL COPYRIGHT AND LICENSE Copyright 2004 by Infinity Interactive, Inc. L This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut Array-Iterator-0.135/Makefile.PL0000644000175000000240000000215714756156125015017 0ustar njhstaffuse strict; use warnings; use ExtUtils::MakeMaker; my $dist = { COMPRESS => 'gzip -9f', # Compress tarball using gzip SUFFIX => 'gz', # File suffix for tarball }; if($^O eq 'darwin') { $dist->{'TAR'} = 'gtar'; } WriteMakefile( ABSTRACT_FROM => 'lib/Array/Iterator.pm', AUTHOR => 'Nigel Horne ', NAME => 'Array::Iterator', VERSION_FROM => 'lib/Array/Iterator.pm', # finds $VERSION in the module ((defined($ExtUtils::MakeMaker::VERSION) && ($ExtUtils::MakeMaker::VERSION >= 6.3002)) ? ('LICENSE'=> 'GPL') : ()), PREREQ_PM => { 'Carp' => 0, 'ExtUtils::MakeMaker' => 6.64, # Minimum version for TEST_REQUIRES }, META_MERGE => { resources => { repository => { type => 'git', url => 'https://github.com/nigelhorne/Array-Iterator.git', web => 'https://github.com/nigelhorne/Array-Iterator', }, bugtracker => { web => 'https://github.com/nigelhorne/Array-Iterator/issues', }, }, }, TEST_REQUIRES => { 'Test::DescribeMe' => 0, 'Test::Most' => 0, 'Test::Needs' => 0, }, dist => $dist, clean => { FILES => 'Array-Iterator-*' }, # Clean up distribution files ); Array-Iterator-0.135/META.json0000644000175000000240000000223114756230666014462 0ustar njhstaff{ "abstract" : "A simple class for iterating over Perl arrays", "author" : [ "Nigel Horne " ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 7.70, CPAN::Meta::Converter version 2.150010", "license" : [ "open_source" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "Array-Iterator", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "Carp" : "0", "ExtUtils::MakeMaker" : "6.64" } }, "test" : { "requires" : { "Test::DescribeMe" : "0", "Test::Most" : "0", "Test::Needs" : "0" } } }, "release_status" : "stable", "resources" : {}, "version" : "0.135", "x_serialization_backend" : "JSON::PP version 4.16" }