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

Problem:One small change (delete the last entry in the second part of the buffer):

my @buffer = qw[x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 y0 y1 y2 y3 y4 y5 y6];
and the output is:
C:\test>1118256 10 7 17 3 x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 y0 y1 y2 y3 y4 y5 y6 y0 y1 y2 y3 y4 y5 y6 x7 x8 x9 x0 x1 x2 x3 x4 x5 x6 y0 y1 y2 y3 y4 y5 y6 x4 x5 x6 x0 x1 x2 x3 x7 x8 x9 y0 y1 y2 y3 y4 y5 y6 x1 x2 x3 x0 x4 x5 x6 x7 x8 x9 y0 y1 y2 y3 y4 y5 y6 x2 x3 x0 x1 x4 x5 x6 x7 x8 x9 y0 y1 y2 y3 y4 x2 x3 x0 y6 y5 x1 x4 x5 x6 x7 x8 x9

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^3: [OT] Swapping buffers in place.
by hdb (Monsignor) on Mar 01, 2015 at 11:51 UTC

    Which means that the logic also needs to be different for odd length of buffers.

      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

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