Skip to content

Commit

Permalink
Introduce R:I:IteratePairFromIterator
Browse files Browse the repository at this point in the history
Abstract logic from List.pairs, so that we can use it for 1dimmed
shaped arrays also.
  • Loading branch information
lizmat committed Nov 6, 2016
1 parent 608de26 commit e829aa1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
24 changes: 1 addition & 23 deletions src/core/List.pm
Expand Up @@ -819,29 +819,7 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
Seq.new(Rakudo::Internals.IterateKeyValueFromIterator(self.iterator))
}
multi method pairs(List:D:) {
Seq.new(class :: does Iterator {
has Mu $!iter;
has int $!key;

method !SET-SELF(\iter) { $!iter := iter; $!key = -1; self }
method new(\iter) { nqp::create(self)!SET-SELF(iter) }

method pull-one() is raw {
nqp::if(
nqp::eqaddr((my $pulled := $!iter.pull-one),IterationEnd),
IterationEnd,
Pair.new(($!key = nqp::add_i($!key,1)),$pulled)
)
}
method push-all($target --> IterationEnd) {
my $pulled;
my int $key = -1;
nqp::until(
nqp::eqaddr(($pulled := $!iter.pull-one),IterationEnd),
$target.push(Pair.new(($key = nqp::add_i($key,1)),$pulled))
)
}
}.new(self.iterator))
Seq.new(Rakudo::Internals.IteratePairFromIterator(self.iterator))
}
multi method antipairs(List:D:) {
my $laze = self.is-lazy;
Expand Down
27 changes: 27 additions & 0 deletions src/core/Rakudo/Internals.pm
Expand Up @@ -659,6 +659,33 @@ my class Rakudo::Internals {
}.new(iterator)
}

# iterate 1dimensional Pair from iterator
method IteratePairFromIterator(\iterator) {
class :: does Iterator {
has Mu $!iter;
has int $!key;

method !SET-SELF(\iter) { $!iter := iter; $!key = -1; self }
method new(\iter) { nqp::create(self)!SET-SELF(iter) }

method pull-one() is raw {
nqp::if(
nqp::eqaddr((my $pulled := $!iter.pull-one),IterationEnd),
IterationEnd,
Pair.new(($!key = nqp::add_i($!key,1)),$pulled)
)
}
method push-all($target --> IterationEnd) {
my $pulled;
my int $key = -1;
nqp::until(
nqp::eqaddr(($pulled := $!iter.pull-one),IterationEnd),
$target.push(Pair.new(($key = nqp::add_i($key,1)),$pulled))
)
}
}.new(iterator)
}

method EmptyIterator() {
BEGIN class :: does Iterator {
method new() { nqp::create(self) }
Expand Down

0 comments on commit e829aa1

Please sign in to comment.