Skip to content

Commit

Permalink
Make List.combinations() 1.5x, List.combinations(3..5) 2x faster
Browse files Browse the repository at this point in the history
By using the new R:It.SequentialIterators and R:It.Callable functionality
  • Loading branch information
lizmat committed Jan 14, 2017
1 parent 717b37f commit 502fc77
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions src/core/List.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1133,8 +1133,34 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP

proto method combinations(|) is nodal {*}
multi method combinations() {
Rakudo::Internals.SeqFromSeqs(
Range.new(0,self.elems).map( { self.combinations($_) } )
nqp::stmts(
(my int $elems = self.elems), # reifies
(my int $i = -1),
Seq.new(
Rakudo::Iterator.SequentialIterators(
Rakudo::Iterator.Callable( {
nqp::if(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
Rakudo::Iterator.ListIndexes( # basically .combinations($i)
self,
Rakudo::Iterator.Combinations($elems, $i, 1)
),
nqp::if(
nqp::iseq_i($i,$elems),
Rakudo::Iterator.OneValue( # last one is self
nqp::p6bindattrinvres( # but must be a (new) List
nqp::create(List), # so transplant innards
List,
'$!reified',
nqp::getattr(self,List,'$!reified')
)
),
IterationEnd
)
)
} )
)
)
)
}

Expand All @@ -1146,11 +1172,25 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
)
}
multi method combinations(Range:D $ofrange) {
Rakudo::Internals.SeqFromSeqs(
Range.new(
($ofrange.first max 0),
(($ofrange.first(:end) // -1) min self.elems)
).map( { self.combinations($_) } )
nqp::stmts(
(my int $elems = self.elems), # reifies
((my int $i, my int $to) = $ofrange.int-bounds),
($i = nqp::if(nqp::islt_i($i,0),-1,nqp::sub_i($i,1))),
nqp::if(nqp::isgt_i($to,$elems),($to = $elems)),
Seq.new(
Rakudo::Iterator.SequentialIterators(
Rakudo::Iterator.Callable( {
nqp::if(
nqp::isle_i(($i = nqp::add_i($i,1)),$to),
Rakudo::Iterator.ListIndexes( # basically .combinations($i)
self,
Rakudo::Iterator.Combinations($elems, $i, 1)
),
IterationEnd
)
} )
)
)
)
}

Expand Down

0 comments on commit 502fc77

Please sign in to comment.