in reply to Better algorithm or data structure?
Essentially, the state of the booleans in @bools will switch from false to true in some undefinable order. Each time one becomes true, it will affect the composite state of one or more sets in @sets. And if all the booleans referenced by any particular set are true, then I need to remove that set from @sets.
So, is it important that a set be removed as soon as the related booleans all turn true? Or could it instead be a two-step process: stuff happens to the booleans, and when that's all done, then you check the sets to see which ones should be removed?
I'm puzzled because the OP code seems to do nothing at all in terms of checking the status of the booleans that relate to a given set, as implied by the description. Instead, the code appears to be deleting elements from the @sets AoA when the nested (inner) array happens to contain no zero values. Now that I've seen the updated version of the OP, it all makes more sense -- thanks!
Another question about the description: is it essential that elements of @sets be deleted, or would it suffice to keep the array a constant size, and "undef" elements when appropriate?
If you can avoid using splice (and if I understand the goal, which is still in doubt), you could build an AoH map of where the various @bools indexes occur in @sets, and only scan the relevant sets as you alter the values of the bools. Something like this:
(But I feel like I'm not really understanding your goal, because I'm pretty sure that you of all people would have figured this out.)my @sets = ... # after values are loaded into the @sets AoA, build a map: my @boolmap; for my $set ( 0 .. $#sets ) { $boolmap[ $_ ]{ $set } = undef for ( @{ $sets[ $set ] } ); } # now, as changes are made to bools, check just the relevant sets: for my $next ( @someOrder ) { $bools[ $next ]++; for my $set ( keys %{ $boolmap[ $next ] } ) { if( defined( $sets[ $set ] ) and grep( { $bools[ $_ ] } @{ $sets[ $set ] } ) == @{ $sets[ $ +set ] } ) { $sets[ $set ] = undef; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Better algorithm or data structure?
by BrowserUk (Patriarch) on Aug 30, 2010 at 00:26 UTC | |
by graff (Chancellor) on Aug 30, 2010 at 00:53 UTC | |
by BrowserUk (Patriarch) on Aug 30, 2010 at 01:17 UTC |