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

for odd length of buffers.

My current understanding is that the logic needs to be different for each of 4 possibilities:

  1. Odd length left/even length right buffers;
  2. Even length right/odd length left buffers;
  3. Even length left/even length right buffers;
  4. odd length left/odd length right buffers

Also, your approach seems to do considerable more moving/swapping than is optimal; and Perl's convenient-but-slow:

cmpthese -1, { a => q[ my @a = 1 .. 1e5; @a[ $_, $_+2 ] = @a[ $_+2, $_ ] for 0 .. $#a-2 ], b => q[ my( @a, $t ) = 1 .. 1e5; $t = $a[ $_ ], $a[ $_ ] = $a[ $_+2 ], $a[ $_+2 ] = $t for 0 .. + $#a-2 ] };; Rate a b a 9.65/s -- -13% b 11.1/s 15% --

array-slice to array-slice assignment, hides a lot of sins.


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

Replies are listed 'Best First'.
Re^5: [OT] Swapping buffers in place.
by hdb (Monsignor) on Mar 01, 2015 at 14:57 UTC

    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";

      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.