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).


In reply to Re: [OT] Swapping buffers in place. by davies
in thread [OT] Swapping buffers in place. by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.