Skip to content

Commit

Permalink
Don't use nqp::iterator to map over a sort result
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Dec 1, 2020
1 parent 2ce5260 commit 80f9283
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions src/core.c/Map.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -100,39 +100,38 @@ my class Map does Iterable does Associative { # declared in BOOTSTRAP
# Pairs.
my class Iterate-keys does Iterator {
has $!map;
has $!iter;
method new(Mu \map, Mu \keys) {
nqp::p6bindattrinvres(
nqp::p6bindattrinvres(
nqp::create(self),
self,
'$!map',nqp::getattr(map,Map,'$!storage')
),
self,
'$!iter',
nqp::iterator(keys)
)
has $!keys;
has int $!i;

method !SET-SELF(Mu \map, Mu \keys) {
$!map := map;
$!keys := keys;
$!i = -1;
self
}
method new(Mu \map, Mu \keys) { nqp::create(self)!SET-SELF(map,keys) }
method pull-one() {
nqp::if(
$!iter,
nqp::islt_i(($!i = nqp::add_i($!i,1)),nqp::elems($!keys)),
nqp::stmts(
(my \key := nqp::shift($!iter)),
(my \key := nqp::atpos_s($!keys,$!i)),
Pair.new(key,nqp::atkey($!map,key))
),
IterationEnd
)
}
method push-all($target --> IterationEnd) {
my \map := $!map;
my \iter := $!iter;
my \keys := $!keys;
my int $i = $!i;
nqp::while(
iter,
nqp::islt_i(($i = nqp::add_i($i,1)),nqp::elems(keys)),
nqp::stmts(
(my \key := nqp::shift(iter)),
(my \key := nqp::atpos_s(keys,$i)),
$target.push(Pair.new(key,nqp::atkey(map,key)))
)
)
);
$!i = $i;
}
}

Expand Down

0 comments on commit 80f9283

Please sign in to comment.