Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: ebc730c249
Fetching contributors…

Cannot retrieve contributors at this time

83 lines (73 sloc) 3.312 kb
my class List { ... }
my class ListIter {
# Attributes defined in BOOTSTRAP.pm:
# has $!reified; # return value for already-reified iterator
# has $!nextiter; # next iterator in sequence, if any
# has Mu $!rest; # RPA of elements remaining to be reified
# has $!list; # List object associated with this iterator
method reify($n = 1) {
unless nqp::isconcrete($!reified) {
my $eager = nqp::p6bool(nqp::istype($n, Whatever));
my $flattens = nqp::p6bool(nqp::isconcrete($!list)) && $!list.flattens;
my int $count = $eager
?? 100000
!! nqp::unbox_i(nqp::istype($n, Int) ?? $n !! $n.Int);
my $rpa := nqp::list();
my Mu $x;
my int $index;
pir::perl6_shiftpush__0PPI($rpa, $!rest, nqp::elems($!rest))
if nqp::istype($!list, LoL);
while $!rest && (nqp::elems($rpa) < $count) {
$index = pir::perl6_rpa_find_type__IPPii(
$!rest, Iterable, 0, $count);
$index = pir::perl6_rpa_find_type__IPPii(
$!rest, Parcel, 0, $index)
if $flattens;
pir::perl6_shiftpush__0PPi($rpa, $!rest, $index);
if $!rest && (nqp::elems($rpa) < $count) {
$x := nqp::shift($!rest);
if nqp::isconcrete($x) {
(nqp::unshift($!rest, $x); last) if $eager && $x.infinite;
$x := $x.iterator.reify(
$eager
?? Whatever
!! nqp::p6box_i($count - nqp::elems($rpa)))
if nqp::istype($x, Iterable);
nqp::splice($!rest, nqp::getattr($x, Parcel, '$!storage'), 0, 0);
}
elsif nqp::not_i(nqp::istype($x, Nil)) {
nqp::push($rpa, $x);
}
}
}
my $reified := nqp::p6parcel($rpa, Any);
nqp::bindattr(self, ListIter, '$!nextiter', nqp::p6listiter($!rest, $!list))
if $!rest;
$reified := $!list.REIFY($reified, $!nextiter) if nqp::isconcrete($!list);
nqp::push( nqp::getattr($reified, Parcel, '$!storage'), $!nextiter)
if $!rest;
nqp::bindattr(self, ListIter, '$!reified', $reified);
# free up $!list and $!rest
nqp::bindattr(self, ListIter, '$!list', Mu);
nqp::bindattr(self, ListIter, '$!rest', Mu);
}
$!reified;
}
method infinite() {
$!rest
?? nqp::istype(nqp::atpos($!rest, 0), Iterable)
&& nqp::atpos($!rest,0).infinite
|| Mu
!! Bool::False
}
method iterator() { self }
method nextiter() { $!nextiter }
multi method DUMP(ListIter:D:) {
self.DUMP-ID() ~ '('
~ ("\x221e " if self.infinite) ~
~ ':reified(' ~ DUMP($!reified) ~ '), '
~ ':rest(' ~ DUMP($!rest) ~ '), '
~ ':list(' ~ $!list.DUMP-ID() ~ ')'
~ ')'
}
}
Jump to Line
Something went wrong with that request. Please try again.