Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: [OT] Swapping buffers in place.

by roboticus (Chancellor)
on Mar 01, 2015 at 19:10 UTC ( [id://1118303]=note: print w/replies, xml ) Need Help??


in reply to [OT] Swapping buffers in place.

BrowserUk:

Here's what I came up with:

#!/usr/bin/env perl use strict; use warnings; for my $X (3 .. 7) { for my $Y (2 .. 8) { my @orig = ((map {"X$_"} 0 .. $X),(map {"Y$_"} 0 .. $Y)); my @wanted = ((map {"Y$_"} 0 .. $Y),(map {"X$_"} 0 .. $X)); print "@orig\n"; swap($X+1, \@orig, \@wanted); my $fl = 'Y'; for (0 .. $#wanted) { if ($orig[$_] ne $wanted[$_]) { $fl='N'; $orig[$_]=lc($orig[$_]); } } print "@orig ($fl)\n\n"; } } sub swap { my ($ofs, $list) = @_; my ($cnt, $src, $dst, $cmp, $tmp); $cnt = @$list; $dst = $cmp = 0; $tmp = $list->[$dst]; while ($cnt--) { $src = ($dst+$ofs) % @$list; if ($src == $cmp) { # Loop detected, finish it and start next one $list->[$dst] = $tmp; $dst = ++$cmp; $tmp = $list->[$dst]; } else { $list->[$dst] = $list->[$src]; $dst = $src; } } }

The two tricks are: (1) You never have to subtract from your pointers, and (2) you have to detect loops when your pointers have a smaller loop size than the buffer size.

Update: It took me a bit of time to get back to this, and I posted it before reviewing the thread. It turns out that graf already described the algorithm, as did hdb.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

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

    Seems I found the same independently 1118280. I like your modulus calculation to find the next loaction.

Re^2: [OT] Swapping buffers in place.
by BrowserUk (Patriarch) on Mar 01, 2015 at 21:06 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1118303]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-19 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found