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

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.