Data-CircularList
view release on metacpan or search on metacpan
lib/Data/CircularList/Iterator.pm view on Meta::CPAN
package Data::CircularList::Iterator;
use 5.006;
use strict;
use warnings FATAL => 'all';
use parent qw/Class::Accessor/;
__PACKAGE__->mk_accessors(qw/p header rotate rotate_count/);
use Scalar::Util qw/blessed/;
use Carp;
sub DEBUG() {0}; # {0} when done
=head1 NAME
Data::CircularList::Iterator - iterator for Data::CircularList's object.
=head1 VERSION
Version 0.03
=cut
our $VERSION = '0.03';
=head1 SYNOPSIS
You can see Data::CircularList module's SYNOPSIS as a example.
=cut
=head1 SUBROUTINES/METHODS
=head2 new
constructor. reguire one argument (not necessary) as rotate.
=cut
sub new {
my ($class, $circular_list, $rotate) = @_;
my $self = {
p => $circular_list->header,
header => $circular_list->header,
rotate => defined $rotate ? $rotate : undef,
rotate_count => 0,
};
bless $self => $class;
return $self;
}
=head2 has_next
return boolean value(1 or 0).
If the linkedList has next cell, this method return 1.
If the linkedList has not next cell, this method return 0.
=cut
sub has_next {
my $self = shift;
# if rotate is not defined, return true eternary.
return 1 if (!defined($self->rotate));
if ( ! blessed($self->p->next->data) ) {
$self->rotate_count($self->rotate_count + 1);
}
# case of the rotate is defined
if ( $self->rotate_count < $self->rotate ) {
# skip header
return 1;
} else {
return 0;
}
}
=head2 next
return next cell(Data::CircularList::Cell) of the CircularList.
=head3 caution
If next method called, iterator progresses next cell.
So you should generally call has_next method and next method alternately each once respectively.
my $list = Data::CircularList->new;
my $iter = $list->iterator;
while ($iter->has_next) {
print $iter->next->data . "\n";
}
=cut
sub next {
my $self = shift;
if ( ! defined($self->p->next) ) {
return undef;
}
if ( ! blessed($self->p->next->data) ) {
# skip header
$self->p($self->p->next->next);
} else {
$self->p($self->p->next);
}
( run in 0.779 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )