Algorithm-ChooseSubsets
view release on metacpan or search on metacpan
Revision history for Perl extension Algorithm::ChooseSubsets.
0.01 Tue Apr 30 09:36:34 2002
- original version
0.02 Wed Oct 19 2005
- fixed harmless off-by-one typo
ChooseSubsets.pm view on Meta::CPAN
while ($x = $i->next) {
# Do something with @$x
}
$i->reset; # return to the first subset.
=head1 DESCRIPTION
"Subsets" in this context refers to lists with elements taken
from the original list, and in the same order as the elements in the
original list. After creating the object, subsequent calls to next()
will return the next such list in lexicographic order (where the alphabet
is the original list).
If K is specified, only subsets of that size will be returned. If K
is omitted, all subsets will be returned, beginning with the empty set
and ending with the entire set. If the 'all' flag and a value for 'K' are
specified, subsets of size greater than or equal to K will be returned.
If a number, N, is used instead of a list, the list is taken to
be [0..N-1].
=head1 EXAMPLES
ChooseSubsets.pm view on Meta::CPAN
%args = @_;
}
if (!defined($args{'size'})) {
$args{'size'} = 0;
$args{'all'} = 1;
}
bless (+{
_size => ($args{'size'}), # size of the subsets we are returning
_original_size => ($args{'size'}), # ditto, for resetting purposes
_set => ($args{'set'} || croak "Missing set"), # the set
_n => scalar(@{$args{'set'}}), # size of the set
_c => undef, # Current indexes to return.
_all => $args{'all'} # whether to do all or just one K.
},$class);
}
#
# return the next subset.
#
ChooseSubsets.pm view on Meta::CPAN
# Set the internal state, and return the values.
$self->{'_c'} = $c;
return [@$set[@$c]];
}
#
# reset to the first subset.
#
sub reset {
my $self = shift;
$self->{_size} = $self->{_original_size};
$self->{_c} = undef;
}
1;
( run in 0.232 second using v1.01-cache-2.11-cpan-069f9db706d )