Skip to content
Snippets Groups Projects
Unverified Commit 940e1fba authored by Tiago Grego's avatar Tiago Grego Committed by GitHub
Browse files

Merge pull request #430 from muffato/master

Add next-value buffering to Bio::EnsEMBL::Utils::Iterator
parents d2c85dfc 4d6570ee
No related branches found
No related tags found
1 merge request!457Patch to support longer assembly names in the mapping session table.
......@@ -141,6 +141,26 @@ sub new {
}
=head2 _fetch_next_value_if_needed
Example : $iterator->_fetch_next_value_if_needed
Description: Helper method used to fill the internal buffer with the next value of the iterator
Returntype : none
Exceptions : none
Caller : general
Status : Experimental
=cut
sub _fetch_next_value_if_needed {
my $self = shift;
unless (exists $self->{next}) {
$self->{next} = $self->{sub}->();
}
}
=head2 next
Example : $obj = $iterator->next
......@@ -155,9 +175,12 @@ sub new {
sub next {
my $self = shift;
$self->{next} = $self->{sub}->() unless defined $self->{next};
return delete $self->{next};
$self->_fetch_next_value_if_needed();
if (defined $self->{next}) {
return delete $self->{next};
}
return;
}
=head2 has_next
......@@ -175,7 +198,7 @@ sub next {
sub has_next {
my $self = shift;
$self->{next} = $self->{sub}->() unless defined $self->{next};
$self->_fetch_next_value_if_needed();
return defined $self->{next};
}
......@@ -196,7 +219,7 @@ sub has_next {
sub peek {
my $self = shift;
$self->{next} = $self->{sub}->() unless defined $self->{next};
$self->_fetch_next_value_if_needed();
return $self->{next};
}
......
......@@ -33,6 +33,23 @@ $it = Bio::EnsEMBL::Utils::Iterator->new(sub {return 3});
is($it->next, 3, "got expected value from iterator created from coderef");
# check that the coderef is not called any more once it returns undef
my $call_counter = 0;
$it = Bio::EnsEMBL::Utils::Iterator->new(sub {
++$call_counter;
if ($call_counter == 1) {
return 1;
} else {
return undef;
}
});
is($it->next, 1, "got expected value from iterator created from coderef");
is($it->next, undef, "got undef from iterator created from coderef once exhausted");
is($it->next, undef, "got undef from iterator created from coderef once exhausted");
is($call_counter, 2, 'Returned undef without calling the coderef any further');
# from now on we create an iterator from an arrayref because it's simpler
$it = Bio::EnsEMBL::Utils::Iterator->new([1,2,3]);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment