Keep It Simple, Stupid | |
PerlMonks |
Re: Powerset short-circuit optimizationby grinder (Bishop) |
on Oct 03, 2006 at 16:48 UTC ( [id://576122]=note: print w/replies, xml ) | Need Help?? |
You can think of elements within a powerset being chosen or not according to a bit vector. 5 elements requires five bits. You start at 00000, so your powerset has no elements. Then you increment, and get 00001. Thus, you take the last element. Some time later, after having incremented the counter for a while, you get to 01101, so you take the second, third and fifth elements. Finally, you get to 11111, and you take all elements. And then you stop. This is how my module Data::PowerSet is implemented (except that it starts at 11111 and counts down. Looking at the implementation, I don't see an aha! solution to make it jump over a region, but I'm sure with a savant dose of binary and'ing and or'ing you could make it do that. Patches welcome! • another intruder with the mooring in the heart of the Perl
In Section
Seekers of Perl Wisdom
|
|