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 | |
by hdb (Monsignor) on Mar 01, 2015 at 18:09 UTC | |
by BrowserUk (Patriarch) on Mar 01, 2015 at 18:35 UTC |