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 | |
by hdb (Monsignor) on Mar 01, 2015 at 11:51 UTC | |
by BrowserUk (Patriarch) on Mar 01, 2015 at 12:23 UTC | |
by hdb (Monsignor) on Mar 01, 2015 at 14:57 UTC | |
by BrowserUk (Patriarch) on Mar 01, 2015 at 18:01 UTC | |
|