in reply to [OT] Swapping buffers in place.

Here is a program that does the swapping with one temp only:

use strict; use warnings; my @buffer = qw[X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 Y0 Y1 Y2 Y3 Y4 Y5 Y6 Y7] +; sub swapsamesize { my( $s1, $s2, $n, $buf ) = @_; ($$buf[$s1+$_], $$buf[$s2+$_]) = ($$buf[$s2+$_], $$buf[$s1+$_]) for +0..$n-1; # too lazy to use $tmp } sub swapbuffers { my( $n1, $buf ) = @_; my $ntotal = @$buf; my $n2 = $ntotal - $n1; my $d = $n1 - $n2; print "$n1 $n2 $ntotal $d\n"; print "@$buf\n"; my( $s1, $s2, $n ) = ( 0, $n1, $n2 ); swapsamesize( $s1, $s2, $n, $buf ); print "@$buf\n"; ( $s1, $s2, $n ) = ( $n2, $ntotal - $d, $d ); swapsamesize( $s1, $s2, $n, $buf ); print "@$buf\n"; ( $s1, $s2, $n ) = ( $n2, $ntotal - 2 * $d, $d ); swapsamesize( $s1, $s2, $n, $buf ); print "@$buf\n"; ( $s1, $s2, $n ) = ( $n2, $ntotal - 3 * $d, $d ); swapsamesize( $s1, $s2, $n, $buf ); print "@$buf\n"; ( $s1, $s2, $n ) = ( $n2, $ntotal - 4 * $d, $d ); swapsamesize( $s1, $s2, $n, $buf ); print "@$buf\n"; } swapbuffers 10, \@buffer;

Of course, the repeated calls to the utility function swapsamesize should be done in a loop until all is done. It also only works if the overlap when swapping (i.e. n1 - n2) is smaller than the rest of the smaller half. I think the logic can be adapted...

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

    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

      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