Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use feature qw(state);
- use strict;
- use warnings;
- sub powerset($$&@) {
- my $bitmask = shift;
- my $limit = shift;
- my $callback = shift;
- my $bytes = @_/8;
- {
- my @indices = grep vec($$bitmask, $_, 1), 0..$#_;
- return if $callback->( $limit, @_[@indices] );
- ++vec($$bitmask, $_, 8) and last for 0 .. $bytes;
- redo if @indices != @_;
- }
- }
- my $bitmask = '';
- my @seq = 1..21;
- my $callback = sub {
- state $counter = 0;
- my $limit = shift;
- if (++$counter > $limit) {
- $counter = 0;
- print "limit reached\n";
- return 1;
- }
- print "[@_]\n";
- return 0;
- };
- my $limit = 6;
- powerset \$bitmask, $limit, \&{$callback}, @seq;
- powerset \$bitmask, $limit, \&{$callback}, @seq;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement