Skip to content

Commit

Permalink
Use --/++$i instead of $i = nqp::add/sub_i($i,1)
Browse files Browse the repository at this point in the history
Array/List related
  • Loading branch information
lizmat committed Feb 16, 2022
1 parent c92b44e commit 2bf8466
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 41 deletions.
21 changes: 11 additions & 10 deletions src/core.c/Array.pm6
Expand Up @@ -103,7 +103,7 @@ my class Array { # declared in BOOTSTRAP

method pull-one() is raw {
nqp::ifnull(
nqp::atpos($!reified,$!i = nqp::add_i($!i,1)),
nqp::atpos($!reified,++$!i),
nqp::if(
nqp::islt_i($!i,nqp::elems($!reified)),
self!hole($!i),
Expand Down Expand Up @@ -139,7 +139,7 @@ my class Array { # declared in BOOTSTRAP
(my int $elems = $!todo.reify-until-lazy),
(my int $i = $!i), # lexicals faster than attributes
nqp::while( # doesn't sink
nqp::islt_i($i = nqp::add_i($i,1),$elems),
nqp::islt_i(++$i,$elems),
target.push(nqp::atpos($!reified,$i))
),
nqp::if(
Expand All @@ -158,7 +158,7 @@ my class Array { # declared in BOOTSTRAP
($elems = nqp::elems($!reified)),
($i = $!i),
nqp::while( # doesn't sink
nqp::islt_i($i = nqp::add_i($i,1),$elems),
nqp::islt_i(++$i,$elems),
target.push(
nqp::ifnull(nqp::atpos($!reified,$i),self!hole($i))
)
Expand Down Expand Up @@ -212,7 +212,7 @@ my class Array { # declared in BOOTSTRAP
my int $i = -1;
my \reified := nqp::create(IterationBuffer);
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::islt_i(++$i,$elems),
nqp::bindpos(
reified, $i,
nqp::p6scalarwithvalue(
Expand Down Expand Up @@ -391,7 +391,7 @@ my class Array { # declared in BOOTSTRAP
nqp::stmts(
(my int $i = -1),
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::islt_i(++$i,$elems),
nqp::if(
nqp::isnull(nqp::atpos($reified,$i)),
nqp::bindpos(
Expand Down Expand Up @@ -444,7 +444,7 @@ my class Array { # declared in BOOTSTRAP
(my $cow := nqp::setelems(nqp::create(IterationBuffer),$elems)),
(my int $i = -1),
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::islt_i(++$i,$elems),
nqp::bindpos($cow,$i,nqp::ifnull(nqp::decont(nqp::atpos($reified,$i)),Nil)),
),
$cow.List
Expand Down Expand Up @@ -651,13 +651,14 @@ my class Array { # declared in BOOTSTRAP
)
}

method !remove-nulls-from-end(uint $i is copy --> Nil) {
method !remove-nulls-from-end(uint $from --> Nil) {
my int $i = $from;
nqp::unless(
nqp::isconcrete(nqp::getattr(self,List,'$!todo')),
nqp::stmts(
(my $reified := nqp::getattr(self,List,'$!reified')),
nqp::while(
(nqp::isge_i(($i = nqp::sub_i($i,1)),0)
(nqp::isge_i(--$i,0)
&& nqp::not_i(nqp::existspos($reified,$i))),
nqp::null
),
Expand Down Expand Up @@ -1242,7 +1243,7 @@ my class Array { # declared in BOOTSTRAP
(my int $elems = nqp::elems($new)),
(my int $i = -1),
nqp::while(
(nqp::islt_i(($i = nqp::add_i($i,1)),$elems)
(nqp::islt_i(++$i,$elems)
&& nqp::istype(nqp::atpos($new,$i),$expected)),
nqp::null
),
Expand Down Expand Up @@ -1336,7 +1337,7 @@ my class Array { # declared in BOOTSTRAP
nqp::if(
$!count && nqp::elems(nqp::getattr($!array,List,'$!reified')),
nqp::stmts(
($!count = nqp::sub_i($!count,1)),
--$!count,
$!array.GRAB_ONE
),
IterationEnd
Expand Down
54 changes: 23 additions & 31 deletions src/core.c/List.pm6
Expand Up @@ -279,7 +279,7 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
(my int $i = -1),
(my int $b = 0),
nqp::while(
nqp::islt_i($i = nqp::add_i($i,1),$elems),
nqp::islt_i(++$i,$elems),
nqp::if(
nqp::iscont(my $consider := nqp::atpos($vm-tuple,$i)),
nqp::bindpos($future,$i,$consider),
Expand All @@ -292,7 +292,7 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
),
nqp::stmts(
nqp::bindpos($future,$i,$consider),
($b = nqp::add_i($b,1))
++$b
)
)
)
Expand Down Expand Up @@ -380,7 +380,7 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
(my $sum := 0),
(my int $i = -1),
nqp::while(
nqp::islt_i($i = nqp::add_i($i,1),$elems),
nqp::islt_i(++$i,$elems),
($sum := $sum + nqp::ifnull(nqp::atpos($!reified,$i),0))
),
$sum
Expand All @@ -398,7 +398,7 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
(my $strings := nqp::setelems(nqp::list_s,$elems)),
(my int $i = -1),
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::islt_i(++$i,$elems),
nqp::bindpos_s($strings,$i,nqp::atpos($list,$i).Str)
),
nqp::p6box_s(nqp::join(' ',$strings))
Expand Down Expand Up @@ -430,7 +430,7 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
2
),
nqp::while( # only a single %s
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::islt_i(++$i,$elems),
nqp::bindpos_s($strings,$i,
nqp::if(
nqp::istype((my $elem := nqp::atpos($list,$i)),List),
Expand All @@ -440,7 +440,7 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
)
),
nqp::while( # something else
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::islt_i(++$i,$elems),
nqp::bindpos_s($strings,$i,
nqp::if(
nqp::istype(($elem := nqp::atpos($list,$i)),List),
Expand Down Expand Up @@ -592,7 +592,7 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP

method pull-one() is raw {
nqp::ifnull(
nqp::atpos($!reified,$!i = nqp::add_i($!i,1)),
nqp::atpos($!reified,++$!i),
nqp::if(
nqp::isconcrete($!todo),
nqp::if(
Expand All @@ -618,7 +618,7 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
nqp::stmts( # something to reify still
(my int $elems = $!todo.reify-until-lazy),
nqp::while( # doesn't sink
nqp::islt_i($!i = nqp::add_i($!i,1),$elems),
nqp::islt_i(++$!i,$elems),
target.push(nqp::atpos($!reified,$!i))
),
nqp::if(
Expand All @@ -633,7 +633,7 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
nqp::stmts( # already fully reified
($elems = nqp::elems($!reified)),
nqp::while( # doesn't sink
nqp::islt_i($!i = nqp::add_i($!i,1),$elems),
nqp::islt_i(++$!i,$elems),
target.push(nqp::atpos($!reified,$!i))
),
IterationEnd
Expand Down Expand Up @@ -716,7 +716,7 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
self.is-lazy,
nqp::stmts(
(my int $i = -1),
Rakudo::Iterator.Callable( { $i = nqp::add_i($i,1) }, True )
Rakudo::Iterator.Callable( { ++$i }, True )
),
Rakudo::Iterator.IntRange(0, self.elems - 1)
))
Expand Down Expand Up @@ -906,7 +906,7 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
nqp::istype(($v := nqp::atpos($!reified, $i)),Pair)
?? nqp::bindkey($hash, $v.key.Str, $v.value)
!! nqp::push($list,$v)
while nqp::islt_i($i = nqp::add_i($i,1),$elems);
while nqp::islt_i(++$i,$elems);
nqp::bindattr($capture,Capture,'@!list',$list) if nqp::elems($list);
nqp::bindattr($capture,Capture,'%!hash',$hash) if nqp::elems($hash);
$capture
Expand Down Expand Up @@ -977,17 +977,13 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
}
method pull-one() {
nqp::if(
($!number = nqp::sub_i($!number,1)),
--$!number,
nqp::stmts(
(my \tmp = nqp::atpos(
$!list,
my int $i = nqp::floor_n(nqp::rand_n($!elems))
)),
nqp::bindpos(
$!list,
$i,
nqp::atpos($!list,($!elems = nqp::sub_i($!elems,1)))
),
nqp::bindpos($!list,$i,nqp::atpos($!list,--$!elems)),
tmp
),
IterationEnd
Expand All @@ -999,17 +995,13 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
(my int $number = $!number),
(my int $elems = $!elems),
nqp::while(
($number = nqp::sub_i($number,1)),
--$number,
nqp::stmts( # doesn't sink
target.push(nqp::atpos(
$list,
(my int $i = nqp::floor_n(nqp::rand_n($elems)))
)),
nqp::bindpos(
$list,
$i,
nqp::atpos($list,($elems = nqp::sub_i($elems,1)))
)
nqp::bindpos($list,$i,nqp::atpos($list,--$elems))
)
),
($!number = $number),
Expand Down Expand Up @@ -1071,7 +1063,7 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
nqp::create(self)!SET-SELF(list, $todo)
}
method pull-one() is raw {
($!todo = nqp::sub_i($!todo,1))
--$!todo
?? nqp::atpos($!list,nqp::floor_n(nqp::rand_n($!elems)))
!! IterationEnd
}
Expand All @@ -1080,7 +1072,7 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
(my int $todo = $!todo),
(my int $elems = $!elems),
nqp::while(
($todo = nqp::sub_i($todo,1)),
--$todo,
target.push(nqp::atpos(
$!list,
nqp::floor_n(nqp::rand_n($elems))
Expand Down Expand Up @@ -1126,7 +1118,7 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
Seq.new(
Rakudo::Iterator.SequentialIterators(
Rakudo::Iterator.Callable( {
nqp::islt_i(($i = nqp::add_i($i,1)),$elems)
nqp::islt_i(++$i,$elems)
?? Rakudo::Iterator.ListIndexes( # .combinations($i)
self,
Rakudo::Iterator.Combinations($elems, $i, 1)
Expand Down Expand Up @@ -1177,7 +1169,7 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
Rakudo::Iterator.SequentialIterators(
Rakudo::Iterator.Callable( {
nqp::if(
nqp::isle_i(($i = nqp::add_i($i,1)),$to),
nqp::isle_i(++$i,$to),
Rakudo::Iterator.ListIndexes( # basically .combinations($i)
self,
Rakudo::Iterator.Combinations($elems, $i, 1)
Expand Down Expand Up @@ -1217,7 +1209,7 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
(my $strings := nqp::list_s),
(my int $i = -1),
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::islt_i(++$i,$elems),
nqp::stmts( # something left to check
(my $tmp := nqp::ifnull(
nqp::atpos($!reified,$i),
Expand Down Expand Up @@ -1496,7 +1488,7 @@ multi sub infix:<,>(|) {
my int $i = -1;
my int $elems = nqp::elems(in);
nqp::while(
(nqp::islt_i(($i = nqp::add_i($i,1)),$elems)
(nqp::islt_i(++$i,$elems)
&& nqp::not_i(nqp::istype(nqp::atpos(in,$i),Slip))),
nqp::null
);
Expand All @@ -1509,7 +1501,7 @@ multi sub infix:<,>(|) {
($i = -1),
(my $reified := nqp::setelems(nqp::create(IterationBuffer),$elems)),
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::islt_i(++$i,$elems),
nqp::bindpos($reified,$i,nqp::shift(in))
),
# now set up the List with a future
Expand Down Expand Up @@ -1570,7 +1562,7 @@ multi sub infix:<xx>(&x, Int:D $n) {
my int $todo = $n;
my Mu $list := nqp::create(IterationBuffer);
nqp::while(
nqp::isgt_i($todo = nqp::sub_i($todo,1),-1),
nqp::isgt_i(--$todo,-1),
nqp::if(
nqp::istype((my $pulled := x()),Slip),
$pulled.iterator.push-all($list),
Expand Down

0 comments on commit 2bf8466

Please sign in to comment.