in reply to [OT] Swapping buffers in place.

One pass, a buffer the size of the difference.

use strict; use warnings; use diagnostics; use Data::Dumper; my @buffer = qw[x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 y0 y1 y2 y3 y4 y5 y6]; my $bufsize = 10; my $diff = 2 * $bufsize - scalar(@buffer); my @smallbuf; for my $i (0..$#buffer) { if ($i < $bufsize - $diff) { swap($i, $i + $bufsize) } elsif ($i < $bufsize) { push @smallbuf, $buffer[$i]; mv($i+$diff, $i) } elsif ($i <= $#buffer - $diff) { mv($i+$diff, $i) } else { $buffer[$i] = shift @smallbuf; } } print Dumper @buffer; sub swap { my ($i, $j) = @_; $buffer[$i] = $buffer[$i] ^ $buffer[$j]; $buffer[$j] = $buffer[$i] ^ $buffer[$j]; $buffer[$i] = $buffer[$i] ^ $buffer[$j]; } sub mv { my ($from, $to) = @_; $buffer[$to] = $buffer[$from]; }

Assumptions: (1) you know the size of the buffer. (2) The "larger half" always comes first.

You may want to rewrite the swap algorithm, but it doesn't use a buffer, even of one character.

By the time I'd written this, you said you thought you had an answer, but I understand this one & don't know the language of graff's, so I'm posting it anyway.

Regards,

John Davies

Update: the more I look at this, the more sure I am that my if block can be rewritten to calculate the final position of a value from its original position. This means that with a two value buffer in memory, it should be possible to avoid swapping altogether, reducing the number of writes (possibly important for solid state devices). IOW, var1 (using Data::Dumper notation) goes to var8, so read var8 & write var1 there. Var8 goes to var15, so read var15 & write var8 to it and so on through the chain. The question is where to resume when you get to the end of the chain. Once I have sorted this out, I'll post back (or announce failure).

Replies are listed 'Best First'.
Re^2: [OT] Swapping buffers in place.
by davies (Monsignor) on Mar 01, 2015 at 22:07 UTC

    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.

Re^2: [OT] Swapping buffers in place.
by hdb (Monsignor) on Mar 01, 2015 at 19:40 UTC

    See here 1118280 for the answers to the questions in your update.