Algorithm-Combinatorics
view release on metacpan or search on metacpan
Combinatorics.pm view on Meta::CPAN
croak("Missing parameter data");
}
unless ($k_is_not_required || defined $k) {
croak("Missing parameter k");
}
my $type = reftype $data;
if (!defined($type) || $type ne "ARRAY") {
croak("Parameter data is not an arrayref");
}
carp("Parameter k is negative") if !$k_is_not_required && $k < 0;
}
# Given an iterator that responds to the next() method this
# subrutine returns the iterator in scalar context, loops
# over the iterator to build and return an array of results
# in list context, and does nothing but issue a warning in
# void context.
sub __contextualize {
my $iter = shift;
my $w = wantarray;
if (defined $w) {
if ($w) {
my @result = ();
while (my $c = $iter->next) {
push @result, $c;
}
return @result;
} else {
return $iter;
}
} else {
my $sub = (caller(1))[3];
carp("Useless use of $sub in void context");
}
}
sub __null_iter {
return Algorithm::Combinatorics::Iterator->new(sub { return });
}
sub __once_iter {
my $tuple = shift;
$tuple ? Algorithm::Combinatorics::Iterator->new(sub { return }, $tuple) :
Algorithm::Combinatorics::Iterator->new(sub { return }, []);
}
# This is a bit dirty by now, the objective is to be able to
# pass an initial sequence to the iterator and avoid a test
# in each iteration saying whether the sequence was already
# returned or not, since that might potentially be done a lot
# of times.
#
# The solution is to return an iterator that has a first sequence
# associated. The first time you call it that sequence is returned
# and the iterator rebless itself to become just a wrapped coderef.
#
# Note that the public contract is that responds to next(), no
# iterator class name is documented.
package Algorithm::Combinatorics::Iterator;
sub new {
my ($class, $coderef, $first_seq) = @_;
if (defined $first_seq) {
return bless [$coderef, $first_seq], $class;
} else {
return bless $coderef, 'Algorithm::Combinatorics::JustCoderef';
}
}
sub next {
my ($self) = @_;
$_[0] = $self->[0];
bless $_[0], 'Algorithm::Combinatorics::JustCoderef';
return $self->[1];
}
package Algorithm::Combinatorics::JustCoderef;
sub next {
my ($self) = @_;
return $self->();
}
1;
__END__
=head1 NAME
Algorithm::Combinatorics - Efficient generation of combinatorial sequences
=head1 SYNOPSIS
use Algorithm::Combinatorics qw(permutations);
my @data = qw(a b c);
# scalar context gives an iterator
my $iter = permutations(\@data);
while (my $p = $iter->next) {
# ...
}
# list context slurps
my @all_permutations = permutations(\@data);
=head1 VERSION
This documentation refers to Algorithm::Combinatorics version 0.26.
=head1 DESCRIPTION
Algorithm::Combinatorics is an efficient generator of combinatorial sequences. Algorithms are selected from the literature (work in progress, see L</REFERENCES>). Iterators do not use recursion, nor stacks, and are written in C.
Tuples are generated in lexicographic order, except in C<subsets()>.
=head1 SUBROUTINES
Algorithm::Combinatorics provides these subroutines:
permutations(\@data)
circular_permutations(\@data)
derangements(\@data)
complete_permutations(\@data)
variations(\@data, $k)
variations_with_repetition(\@data, $k)
tuples(\@data, $k)
tuples_with_repetition(\@data, $k)
combinations(\@data, $k)
combinations_with_repetition(\@data, $k)
( run in 0.475 second using v1.01-cache-2.11-cpan-e1769b4cff6 )