Solstice
view release on metacpan or search on metacpan
lib/Solstice/List.pm view on Meta::CPAN
sub _getDataArray {
my ($self) = @_;
return $self->{'_list'};
}
=item _setDataArray(\@data)
Private method for the iterator to use when sorting.
=cut
sub _setDataArray {
my ($self, $list) = @_;
$self->{'_list'} = $list;
}
package Solstice::List::Iterator;
use constant TRUE => 1;
use constant FALSE => 0;
=item List::Iterator::new ($list)
Creates and returns a new iterator over the list.
=cut
sub new {
my ($obj, $list) = @_;
confess("Iterator::new(): Requires a List object") unless (defined $list and $list->isa('Solstice::List'));
my $self = bless {
_list => $list,
_index => 0,
}, $obj;
return $self;
}
=item List::Iterator::sort([$anonymous_sub_ref])
Sorts the values that will come out of the iterator. Resets the position of
the iterator.
=cut
sub sort {
my ($self, $sort_ref) = @_;
my @new_data;
if (defined $sort_ref) {
# $a and $b are package variables, so we want to copy $a and $b
# into the namespace of the caller. Since we're doing this work
# for every sort call, we had to make a block based sort instead
# of a usersub sort, and so we had to explicitely call our sorting
# subroutine reference.
my $calling_package = (caller)[0];
{
no strict 'refs'; ## no critic
@new_data = CORE::sort { ${"${calling_package}::a"} = $a; ${"${calling_package}::b"} = $b; &$sort_ref($a, $b); } @{$self->{'_list'}->_getDataArray()};
}
}
else {
@new_data = CORE::sort @{$self->{'_list'}->_getDataArray()};
}
my $package = ref $self->{'_list'};
my $list = $package->new();
$list->_setDataArray(\@new_data);
$self->{'_list'} = $list;
$self->{'_index'} = 0;
return TRUE;
}
=item List::Iterator::hasNext ()
Returns a boolean describing whether there is another element in the list
after the current cursor position.
=cut
sub hasNext {
my ($self) = @_;
return $self->{'_index'} < $self->{'_list'}->size();
}
=item List::Iterator::next ()
Returns the next element in the list, and increments the cursor.
=cut
sub next {
my ($self) = @_;
return undef if $self->{'_index'} >= $self->{'_list'}->size() || (defined $self->{'_end_index'} && $self->{'_index'} >= $self->{'_end_index'});
return $self->{'_list'}->get($self->{'_index'}++);
}
=item List::Iterator::index ()
Returns the current cursor position.
=cut
sub index {
my ($self) = @_;
return $self->{'_index'} ? $self->{'_index'} - 1 : $self->{'_index'};
}
=item List::Iterator::setStartIndex ()
Moves the pointer of the list to the given position
=cut
sub setStartIndex {
( run in 2.697 seconds using v1.01-cache-2.11-cpan-b16cb0d3907 )