Subject: | 3 bugs related to order of combinations |
Date: | Thu, 14 Dec 2006 10:56:08 -0300 |
To: | bug-Math-Combinatorics [...] rt.cpan.org |
From: | José Micó <jose.mico [...] gmail.com> |
use Math::Combinatorics qw(combine);
# 1. The internal sort performed by 'compare' function sort internal
# references, and this does not returns the combinations always in
# the same order:
for (my $i=0; $i<5; $i++) {
my @combi = combine(3, qw( A B C D ) );
foreach (@combi) {
print join(' ', @{$_}), " ";
}
print "\n";
}
# Output:
# 1 2 3 1 2 4 1 3 4 2 3 4
# 2 1 3 2 1 4 2 3 4 1 3 4
# 3 2 4 3 2 1 3 4 1 2 4 1
# 4 2 1 4 2 3 4 1 3 2 1 3
# 2 3 4 2 3 1 2 4 1 3 4 1
# 2. Specifing a compare parameter to the OO interface make the
# iterator fails:
my $combinator = Math::Combinatorics->new(
count => 3,
data => [ qw( 1 2 3 4 ) ],
compare => sub {
my ($s1, $s2) = @_;
$s1 <=> $s2;
},
);
while (my @combo = $combinator->next_combination) {
print join(' ', @combo), " ";
}
# Outputs only one combination (!):
# 1 2 3
# 3. The a compare function should do nested dereferencing before
# doing the comparison. This workaround seems hacky, but works:
my $combinator = Math::Combinatorics->new(
count => 3,
data => [ qw( 1 2 3 4 ) ],
compare => sub {
my ($s1, $s2) = @_;
$s1 = $$s1 while (ref $s1 eq 'REF');
$s2 = $$s2 while (ref $s2 eq 'REF');
$s1 <=> $s2;
},
);
while (my @combo = $combinator->next_combination) {
print join(' ', @combo), " ";
}
# Output ok:
# 1 2 3 1 2 4 1 3 4 2 3 4