Chains seem to matter when the small part is a factor of the length of the large part (possibly also when the diff is a factor of the large). But AFAICT, the chain never hits the next word from the starting point unless it covers the entire buffer, i.e. when there is only one chain. Therefore, simply incrementing the start point when the chain loops back on itself is fine in every case I've tried. My solution may be logically the same as hdb's, but I'm certainly not checking tonight. This has taken longer than my usual Sunday killer sudokus. Mind you, it's also been more fun!
use strict; use warnings; use diagnostics; use Data::Dumper; my @buffer = qw[a b c d e f 0 1 2]; my $bufsize = 6; my $diff = 2 * $bufsize - scalar(@buffer); my @smallbuf; my $writes = 0; my $source = 0; my $chainfrom = 0; push @smallbuf, $buffer[$source]; while ($writes < scalar(@buffer)) { my $dest = $source < $bufsize ? $source + $bufsize - $diff : $sour +ce - $bufsize; push @smallbuf, $buffer[$dest]; $buffer[$dest] = $smallbuf[0]; $writes++; shift @smallbuf; if ($dest == $chainfrom) { $chainfrom++; $source = $chainfrom; $smallbuf[0] = $buffer[$chainfrom]; } else { $source = $dest; } } print Dumper @buffer;
Regards,
John Davies
Update 2015-03-12 (too late to matter): I was concerned that a chain might hit the second position in the starting buffer but not a later one. I have satisfied myself that this is not a problem. If a chain starts at one and hits 2, then a chain that starts at 2 will hit 3 and so on. Therefore, if anything can be missed, it will be the next position. So my algorithm will work for all cases unless there's something wrong with this logic. If so, please tell me.
In reply to Re^2: [OT] Swapping buffers in place.
by davies
in thread [OT] Swapping buffers in place.
by BrowserUk
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |