Skip to content

Commit

Permalink
- Optimization: use Math::Prime::Util::forsetproduct in Array `cart…
Browse files Browse the repository at this point in the history
…esian` when no block is given.

Example:

	say cartesian([[1,2], [3,4,5,6]])

Now uses `Math::Prime::Util::forsetproduct()` when `Math::Prime::Util` is available, which is considerably faster than Algorithm::Loops.
  • Loading branch information
trizen committed Jul 19, 2021
1 parent edd3594 commit 6667622
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/Sidef/Types/Array/Array.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3310,6 +3310,19 @@ package Sidef::Types::Array::Array {
sub cartesian {
my ($self, $block) = @_;

if (!defined($block) and Sidef::Types::Number::Number::HAS_PRIME_UTIL) {
my @result;

Math::Prime::Util::forsetproduct(
sub {
push @result, bless [@_]; # don't bless \@_
},
map { [@$_] } @$self
);

return bless \@result;
}

require Algorithm::Loops;

my $iter = Algorithm::Loops::NestedLoops([map { [@$_] } @$self]);
Expand Down

0 comments on commit 6667622

Please sign in to comment.