in reply to Re^4: [OT] Swapping buffers in place.
in thread [OT] Swapping buffers in place.

Here is my next try. Based on the know how from which place a particular value needs to replaced. This requires an explicit loop and some temp integers as well. Still in Perl but easy to translate to C and pointer arithmetics.

use strict; use warnings; my @buffer = qw[X0 X1 X2 X3 X4 Y0 Y1 Y2 Y3 Y4 Y5 Y6 Y7]; sub swapbuffers { my ($n1, $buf) = @_; my $n2 = @$buf-$n1; my $here = 0; my $tmp = $$buf[$here]; my $istart = $here; for (0..@$buf-1) { my $from = $here < $n2 ? $here+$n1 : $here-$n2; if( $from == $istart ) { # loop finished $$buf[$here] = $tmp; $here = ++$istart; $tmp = $$buf[$istart] } else { $$buf[$here] = $$buf[$from]; $here = $from; } print "@$buf\n"; } } print "@buffer\n"; swapbuffers 5, \@buffer; print "@buffer\n";

Replies are listed 'Best First'.
Re^6: [OT] Swapping buffers in place.
by BrowserUk (Patriarch) on Mar 01, 2015 at 18:01 UTC

    That is a fascinating algorithm! Here's my attempt to grok what's going on (swapped the Xn and Yn for alphas and numbers as X1 & Y1 look far too similar):

    C:\test>1118256 0 1 2 3 4 5 6 7 8 9 0 1 2 3 tmp a b c d e f g h 1 2 3 4 5 6 ist her frm a [1]b c d e f g h 2 3 4 5 6 0 8 8 a 1 b d e f g h[c]2 3 4 5 6 0 2 2 a 1 b[3]d e f g h c 2 4 5 6 0 10 10 a 1 b 3 d f g h c 2[e]4 5 6 0 4 4 a 1 b 3 d[5]f g h c 2 e 4 6 0 12 12 a 1 b 3 d 5 f h c 2 e 4[g]6 0 6 6 b 1 3 d 5 f[a]h c 2 e 4 g 6 1 1 0 b 1[2]3 d 5 f a h c e 4 g 6 1 9 9 b 1 2 3 5 f a h c[d]e 4 g 6 1 3 3 b 1 2 3[4]5 f a h c d e g 6 1 11 11 b 1 2 3 4 5 a h c d e[f]g 6 1 5 5 b 1 2 3 4 5[6]a h c d e f g 1 13 13 b 1 2 3 4 5 6 a c d e f g[h] 1 7 7 3 1 2 4 5 6 a[b]c d e f g h 2 2 1 1 2[3]4 5 6 a b c d e f g h

    But, it still needs a little work, I Ican't see how to fix it. Here's one of my basket case tests:

    C:\test>1118256 0 1 2 3 4 5 6 7 8 tmp a b c d e f 1 2 3 ist her frm a [3]b c d e f 1 2 0 8 8 a 3 b c d e f 1 [2] 0 7 7 a 3 b c d e f [1]2 0 6 6 a 3 b c d e [f]1 2 0 5 5 a 3 b c d [e]f 1 2 0 4 4 a 3 b c [d]e f 1 2 0 3 3 a 3 b [c]d e f 1 2 0 2 2 a 3 [b]c d e f 1 2 0 1 1 a 3[a]b c d e f 1 2 1 1 0 3 a b c d e f 1 2

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

      Your latter example a b c d e f 1 2 3 works for me calling swapbuffers 6, \@buffer. Your example looks as if you called swapbuffers 8, \@buffer. It also seems to be very similar to graff's proposal 1118234 that I do not quite understand still.

        Your example looks as if you called swapbuffers 8, \@buffer.

        You're exactly right! I changed the array, but not the number. I apologise. And thank you.

        It also seems to be very similar to graff's proposal 1118234 that I do not quite understand still.

        I "understand" his logic; but coding it defeated me before I needed sleep.

        And I now appear to have two working solutions, so I may never go back to it...


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
        In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked