| 1 | package Data::CircularList::Iterator; | ||||||||||
| 2 | |||||||||||
| 3 | 5 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 5 | ▶ | use warnings FATAL => 'all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 5 | ▶ | use parent qw/Class::Accessor/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 5 | __PACKAGE__->mk_accessors(qw/p header rotate rotate_count/); | |||||||||
| 8 | 5 | ▶ | use Scalar::Util qw/blessed/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 5 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | sub DEBUG() {0}; # {0} when done | ||||||||||
| 11 | |||||||||||
| 12 | =head1 NAME | ||||||||||
| 13 | |||||||||||
| 14 | Data::CircularList::Iterator - iterator for Data::CircularList's object. | ||||||||||
| 15 | |||||||||||
| 16 | =head1 VERSION | ||||||||||
| 17 | |||||||||||
| 18 | Version 0.03 | ||||||||||
| 19 | |||||||||||
| 20 | =cut | ||||||||||
| 21 | |||||||||||
| 22 | 5 | our $VERSION = '0.03'; | |||||||||
| 23 | |||||||||||
| 24 | =head1 SYNOPSIS | ||||||||||
| 25 | |||||||||||
| 26 | You can see Data::CircularList module's SYNOPSIS as a example. | ||||||||||
| 27 | |||||||||||
| 28 | =cut | ||||||||||
| 29 | |||||||||||
| 30 | =head1 SUBROUTINES/METHODS | ||||||||||
| 31 | |||||||||||
| 32 | =head2 new | ||||||||||
| 33 | |||||||||||
| 34 | constructor. reguire one argument (not necessary) as rotate. | ||||||||||
| 35 | |||||||||||
| 36 | =cut | ||||||||||
| 37 | |||||||||||
| 38 | 6 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 39 | 6 | my ($class, $circular_list, $rotate) = @_; | |||||||||
| 40 | 6 | ▶ | my $self = { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | p => $circular_list->header, | ||||||||||
| 42 | header => $circular_list->header, | ||||||||||
| 43 | rotate => defined $rotate ? $rotate : undef, | ||||||||||
| 44 | rotate_count => 0, | ||||||||||
| 45 | }; | ||||||||||
| 46 | 6 | bless $self => $class; | |||||||||
| 47 | 6 | return $self; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | =head2 has_next | ||||||||||
| 51 | |||||||||||
| 52 | return boolean value(1 or 0). | ||||||||||
| 53 | If the linkedList has next cell, this method return 1. | ||||||||||
| 54 | If the linkedList has not next cell, this method return 0. | ||||||||||
| 55 | |||||||||||
| 56 | =cut | ||||||||||
| 57 | |||||||||||
| 58 | 43 | ▶ | sub has_next { | ||||||||
|
Subroutine
has_next: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 59 | 43 | my $self = shift; | |||||||||
| 60 | |||||||||||
| 61 | # if rotate is not defined, return true eternary. | ||||||||||
| 62 | 43 | ▶ | return 1 if (!defined($self->rotate)); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | |||||||||||
| 64 | 19 | ▶ | if ( ! blessed($self->p->next->data) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 8 | $self->rotate_count($self->rotate_count + 1); | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | # case of the rotate is defined | ||||||||||
| 69 | 19 | ▶ | if ( $self->rotate_count < $self->rotate ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | # skip header | ||||||||||
| 71 | 11 | return 1; | |||||||||
| 72 | } else { | ||||||||||
| 73 | 8 | return 0; | |||||||||
| 74 | } | ||||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | =head2 next | ||||||||||
| 78 | |||||||||||
| 79 | return next cell(Data::CircularList::Cell) of the CircularList. | ||||||||||
| 80 | |||||||||||
| 81 | =head3 caution | ||||||||||
| 82 | |||||||||||
| 83 | If next method called, iterator progresses next cell. | ||||||||||
| 84 | So you should generally call has_next method and next method alternately each once respectively. | ||||||||||
| 85 | |||||||||||
| 86 | my $list = Data::CircularList->new; | ||||||||||
| 87 | my $iter = $list->iterator; | ||||||||||
| 88 | while ($iter->has_next) { | ||||||||||
| 89 | print $iter->next->data . "\n"; | ||||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | =cut | ||||||||||
| 93 | |||||||||||
| 94 | 34 | ▶ | sub next { | ||||||||
|
Subroutine
next: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||
| 95 | 34 | my $self = shift; | |||||||||
| 96 | |||||||||||
| 97 | 34 | ▶ | if ( ! defined($self->p->next) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 0 | return undef; | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 34 | ▶ | if ( ! blessed($self->p->next->data) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | # skip header | ||||||||||
| 103 | 3 | $self->p($self->p->next->next); | |||||||||
| 104 | } else { | ||||||||||
| 105 | 31 | $self->p($self->p->next); | |||||||||
| 106 | } | ||||||||||
| 107 | 34 | return $self->p->data; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | # free memory of cicular data | ||||||||||
| 111 | 6 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||
| 112 | 6 | my $self = shift; | |||||||||
| 113 | 6 | delete $self->{'header'}; | |||||||||
| 114 | 6 | delete $self->{'p'}; | |||||||||
| 115 | 6 | if (DEBUG) { | |||||||||
| 116 | carp "destroying $self\n"; | ||||||||||
| 117 | } | ||||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | =head1 AUTHOR | ||||||||||
| 121 | |||||||||||
| 122 | shinchit, C<< <shinchi.xx at gmail.com> >> | ||||||||||
| 123 | |||||||||||
| 124 | =head1 BUGS | ||||||||||
| 125 | |||||||||||
| 126 | Please report any bugs or feature requests to C<bug-datastructure-circularlist at rt.cpan.org>, or through | ||||||||||
| 127 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-CircularList>. I will be notified, and then you'll | ||||||||||
| 128 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 129 | |||||||||||
| 130 | |||||||||||
| 131 | |||||||||||
| 132 | |||||||||||
| 133 | =head1 SUPPORT | ||||||||||
| 134 | |||||||||||
| 135 | You can find documentation for this module with the perldoc command. | ||||||||||
| 136 | |||||||||||
| 137 | perldoc Data::CircularList::Iterator | ||||||||||
| 138 | |||||||||||
| 139 | |||||||||||
| 140 | You can also look for information at: | ||||||||||
| 141 | |||||||||||
| 142 | =over 4 | ||||||||||
| 143 | |||||||||||
| 144 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||
| 145 | |||||||||||
| 146 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-CircularList> | ||||||||||
| 147 | |||||||||||
| 148 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 149 | |||||||||||
| 150 | L<http://annocpan.org/dist/Data-CircularList> | ||||||||||
| 151 | |||||||||||
| 152 | =item * CPAN Ratings | ||||||||||
| 153 | |||||||||||
| 154 | L<http://cpanratings.perl.org/d/Data-CircularList> | ||||||||||
| 155 | |||||||||||
| 156 | =item * Search CPAN | ||||||||||
| 157 | |||||||||||
| 158 | L<http://search.cpan.org/dist/Data-CircularList/> | ||||||||||
| 159 | |||||||||||
| 160 | =back | ||||||||||
| 161 | |||||||||||
| 162 | |||||||||||
| 163 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 164 | |||||||||||
| 165 | |||||||||||
| 166 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 167 | |||||||||||
| 168 | Copyright 2014 shinchit. | ||||||||||
| 169 | |||||||||||
| 170 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 171 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||
| 172 | copy of the full license at: | ||||||||||
| 173 | |||||||||||
| 174 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||
| 175 | |||||||||||
| 176 | Any use, modification, and distribution of the Standard or Modified | ||||||||||
| 177 | Versions is governed by this Artistic License. By using, modifying or | ||||||||||
| 178 | distributing the Package, you accept this license. Do not use, modify, | ||||||||||
| 179 | or distribute the Package, if you do not accept this license. | ||||||||||
| 180 | |||||||||||
| 181 | If your Modified Version has been derived from a Modified Version made | ||||||||||
| 182 | by someone other than you, you are nevertheless required to ensure that | ||||||||||
| 183 | your Modified Version complies with the requirements of this license. | ||||||||||
| 184 | |||||||||||
| 185 | This license does not grant you the right to use any trademark, service | ||||||||||
| 186 | mark, tradename, or logo of the Copyright Holder. | ||||||||||
| 187 | |||||||||||
| 188 | This license includes the non-exclusive, worldwide, free-of-charge | ||||||||||
| 189 | patent license to make, have made, use, offer to sell, sell, import and | ||||||||||
| 190 | otherwise transfer the Package with respect to any patent claims | ||||||||||
| 191 | licensable by the Copyright Holder that are necessarily infringed by the | ||||||||||
| 192 | Package. If you institute patent litigation (including a cross-claim or | ||||||||||
| 193 | counterclaim) against any party alleging that the Package constitutes | ||||||||||
| 194 | direct or contributory patent infringement, then this Artistic License | ||||||||||
| 195 | to you shall terminate on the date that such litigation is filed. | ||||||||||
| 196 | |||||||||||
| 197 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||||||||||
| 198 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||||||||||
| 199 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||||||||||
| 200 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||||||||||
| 201 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||||||||||
| 202 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||||||||||
| 203 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||||||||||
| 204 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||
| 205 | |||||||||||
| 206 | |||||||||||
| 207 | =cut | ||||||||||
| 208 | |||||||||||
| 209 | 5 | 1; # End of Data::CircularList::Iterator | |||||||||