Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
improve permutations
implement the XOR swap algorithm in a single statement.  Not really necessary but still pretty cool.  It should also provide a small performance improvement as we can then put the C<until> in the end, thus avoiding creating a lexical scope.
  • Loading branch information
grondilu committed Dec 12, 2015
1 parent 091f0ce commit 1f45b22
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions src/core/native_array.pm
Expand Up @@ -738,17 +738,12 @@ sub permutations(int $n where $n > 0) {
$l-- until @!a[$k] < @!a[$l];
# use L<https://en.wikipedia.org/wiki/XOR_swap_algorithm>
# @!a[$k, $l].=reverse
@!a[$k] +^= @!a[$l];
@!a[$k] +^= @!a[$l] = @!a[$k] +^ @!a[$l];

(@!a[$k] +^= @!a[$l]) +^= @!a[$l] +^= @!a[$k];

# @!a[$k+1 .. @!a.end].=reverse;
$l = $!n;
until ++$k >= --$l {
@!a[$k] +^= @!a[$l];
@!a[$k] +^= @!a[$l] = @!a[$k] +^ @!a[$l];
}
@!a.List;
}
(@!a[$k] +^= @!a[$l]) +^= @!a[$l] +^= @!a[$k] until ++$k >= --$l;
@!a.List; }
method count-only { [*] 1 .. $!n }
}.new(:$n)
);
Expand Down

0 comments on commit 1f45b22

Please sign in to comment.