# # This software is Copyright 2005 by Elsevier Inc. You may use it # under the terms of the license at http://perl.plover.com/hop/LICENSE.txt . # ### ### powerset_recurse1 ### ## Chapter 5 section 4.1.1 sub powerset_recurse ($) { my ( $set ) = @_; my $null = { }; my $powerset = { $null, $null }; my $keys = [ keys %{ $set } ]; my $values = [ values %{ $set } ]; my $nmembers = keys %{ $set }; # This many rounds. my $i = 0; # The current round. until ($i == $nmembers) { # Remap. my @powerkeys = keys %{ $powerset }; my @powervalues = values %{ $powerset }; my $powern = @powerkeys; my $j; for ( $j = 0; $j < $powern; $j++ ) { my %subset = ( ); # Copy the old set to the subset. @subset{keys %{ $powerset->{ $powerkeys [ $j ] } }} = values %{ $powerset->{ $powervalues[ $j ] } }; # Add the new member to the subset. $subset{$keys->[ $i ]} = $values->[ $i ]; # Add the new subset to the powerset. $powerset->{ \%subset } = \%subset; } $i++; } return $powerset; }