Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: Argentina
Fetching contributors…

Cannot retrieve contributors at this time

file 84 lines (77 sloc) 3.556 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
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, :$sink) {
        unless nqp::isconcrete($!reified) {
            my $eager = nqp::p6bool(nqp::istype($n, Whatever));
            my $flattens = 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::islt_i(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::islt_i(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(nqp::sub_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);
            $reified := $!list.REIFY($reified) if nqp::isconcrete($!list) && !$sink;
            nqp::push(
                    nqp::getattr($reified, Parcel, '$!storage'),
                    nqp::bindattr(self, ListIter, '$!nextiter',
                                  nqp::p6listiter($!rest, $!list)))
                if $!rest;
            nqp::bindattr(self, ListIter, '$!reified', $reified);
            # update $!list's nextiter
            nqp::bindattr($!list, List, '$!nextiter', $!nextiter) if nqp::isconcrete($!list);
            # 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() ~ ')'
          ~ ')'
    }
         
}
Something went wrong with that request. Please try again.