Your main task is to transform the column with the x in it, so concentrate on that first. To transform to the pattern you have in mind, I would first recode the elements in the column.
First, I would assign each element a number corresponding to its row: 0,.., $dim-1. Then I would add $dim to each element if it was equal to 'x' and 2*$dim if it was equal to '*'.
With this recoding, your transforms amount to a descending sort, and you can recover the permutation needed to create that sort by modding each element with $dim ($ele % $dim).
Now that you have the permutation, simply map all the other columns using the permutation. If you have many columns, I'd suggest creating a lookup table for the mapping. You are done.
I do not know how it compares to your code, but I would think the algorithm above is efficient. Recoding is O($dim), sorting is O($dim log $dim), and mapping is O(2**$dim). I expect that the mapping will dominate for large $dim, but the mapping copies each bit just once, which is about the minimum.
In the broader picture, your transformations induce an automorphism (permutation) on the array of bits, which by projection down to your $args array, induce an automorphism of the $args array itself. An interesting exercise would be to act directly on the $args array itself, skipping the bitwise sorting entirely.
-Mark
In reply to Re: better algorithm: bitwise reordering
by kvale
in thread better algorithm: bitwise reordering
by hv
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |