in reply to [OT] Swapping buffers in place.

I've (possibly as a consequence of reading graff's post, but I'm not sure), come up with two patterns that I think will solve all cases, but I'm still having the devil's own trouble translating them into code!

Here are the two graphically, with some (possible, but incomplete) pointer math attempting to describe the swaps.

An bigger even/smaller odd case:

p2+p2len -3-2-1 p2 +0+1+2 p1+p1len-6-5-4-3-2-1 p1 +0+1+2+3+4+5 0 1 2 3 4 5 6 7 8 [a b c d e f 1 2 3] temp [1]>>>>>>[1] temp = *(p2+0) [d]>>>[d] *(p2+0) = *(p1+3) [a]>>>[a] *(p1+3) = *(p1+0) [1]<<<<<<<<<<<<<<<<<<[1] *(p1+0) = temp [2]>>>>[2] temp = *(p2+1) [e]>>>[e] *(p2+1) = *(p1+4) [b]>>>[b] *(p1+4) = *(p1+1) [2]<<<<<<<<<<<<<<<<[2] *(p1+1) = temp [3]>>[3] temp = *(p2+2) [f]>>>[f] *(p2+2) = *(p1+5) [c]>>>[c] *(p1+5) = *(p1+2) [3]<<<<<<<<<<<<<<[3] *(p1+2) = temp [1 2 3 a b c d e f]

The above looks like 3 reps of a loop. (But is that because 6-3 = 3 or just because The smaller is 3; or ... ). And code?

A bigger odd/smaller even case:

p2+p2len -4-3-2-1 p2 +0+1+2+3 p1+p1len-9-8-7-6-5-4-3-2-1 p1 +0+1+2+3+4+5+6+7+8 0 1 2 3 4 5 6 7 8 [a b c d e f g h i 1 2 3 4] temp [4]>>[4] temp = *(p2+3) [i]>>>>>[i] *(p2+3) = *(p1+8) [e]>>>>>[e] *(p1+8) = *(p1+4) [a]>>>>>[a] *(p1+4) = *(p1+0) [1]<<<<<<<<<<<<<<<[1] *(p1+0) = *(p2+0) [f]>>>>>[f] *(p2+0) = *(p1+5) [b]>>>>>[b] *(p1+5) = *(p1+1) [2]<<<<<<<<<<<<<<<[2] *(p1+1) = *(p2+2) [g]>>>>>[g] *(p2+2) = *(p1+6) [c]>>>>>[c] *(p1+6) = *(p1+2) [3]<<<<<<<<<<<<<<<[3] *(p1+2) = *(p2+2) [h]>>>>>[h] *(p2+2) = *(p1+7) [d]>>>>>[d] *(p1+7) = *(p1+3) [4]<<<<<<<<<<<<<<<<<<<<[4] *(p1+3) = temp [1 2 3 4 a b c d e f g h i]

The above looks like 4 iterations (because the smaller is length 4), but the first iteration requires 3 internal steps and the other 3 only 2? (How to code?)

I haven't convinced myself I don't need even/even and odd/odd cases yet.


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^2: [OT] Swapping buffers in place. (partial solution:needs more help!)
by bitingduck (Deacon) on Mar 01, 2015 at 09:16 UTC

    I've copied what you did and added the iterator and reexpressed the righthand sides in terms of the lengths (the lefts are just the rights from the previous line) and the pattern starts to show up (at least if you click on "download" to view it spread out).

    p2+p2len -3-2-1 p2 +0+1+2 p1+p1len-6-5-4-3-2-1 p1 +0+1+2+3+4+5 0 1 2 3 4 5 6 7 8 [a b c d e f 1 2 3] temp I RHS [1]>>>>>>[1] temp = *(p2+0) 0 *(p2 ++0) [d]>>>[d] *(p2+0) = *(p1+3) 0 *(p1 ++(p1len-p2len)) [a]>>>[a] *(p1+3) = *(p1+0) 0 *(p1 ++I) [1]<<<<<<<<<<<<<<<<<<[1] *(p1+0) = temp [2]>>>>[2] temp = *(p2+1) 1 *(p2 ++I) [e]>>>[e] *(p2+1) = *(p1+4) 1 *(p1 ++(p1len-p2len)+I) [b]>>>[b] *(p1+4) = *(p1+1) 1 *(p1 ++I) [2]<<<<<<<<<<<<<<<<[2] *(p1+1) = temp [3]>>[3] temp = *(p2+2) 2 *(p2 ++I) [f]>>>[f] *(p2+2) = *(p1+5) 2 *(p1 ++(p1len-p2len)+I) [c]>>>[c] *(p1+5) = *(p1+2) 2 *(p1 ++I) [3]<<<<<<<<<<<<<<[3] *(p1+2) = temp [1 2 3 a b c d e f]

    The pattern doesn't jump out as easily from your second example.

    EDIT: I think I got the second one, too. There was a typo that messed me up for a bit, and you solved this one "backwards" so it's easier to reference from the ends of the buffers than the starts, but again there's a pattern:

    p2+p2len -4-3-2-1 p2 +0+1+2+3 p1+p1len-9-8-7-6-5-4-3-2-1 L1=la +st element of buffer 1 p1 +0+1+2+3+4+5+6+7+8 L2=la +st element of buffer 2 0 1 2 3 4 5 6 7 8 diff= +p1len-p2len=5 [a b c d e f g h i 1 2 3 4] temp I [4]>>[4] temp = *(p2+3) 0 + *(L2+I) [i]>>>>>[i] *(p2+3) = *(p1+8) 0 + *(L1+I) [e]>>>>>[e] *(p1+8) = *(p1+4) 0 + *(L1-diff+1+I) [a]>>>>>[a] *(p1+4) = *(p1+0) 1 + *(L2+I) [1]<<<<<<<<<<<<<<<[1] *(p1+0) = *(p2+0) 1 + *(L1+I) [f]>>>>>[f] *(p2+0) = *(p1+5) 1 + *(L1-diff+1+I) [b]>>>>>[b] *(p1+5) = *(p1+1) 2 + *(L2+I) [2]<<<<<<<<<<<<<<<[2] *(p1+1) = *(p2+1) 2 + *(L1+I) # I think you had a typo here [g]>>>>>[g] *(p2+2) = *(p1+6) 2 + *(L1-diff+1+I) [c]>>>>>[c] *(p1+6) = *(p1+2) 3 + *(L2+I) [3]<<<<<<<<<<<<<<<[3] *(p1+2) = *(p2+2) 3 + *(L1+I) [h]>>>>>[h] *(p2+2) = *(p1+7) 3 + *(L1-diff+1+I) [d]>>>>>[d] *(p1+7) = *(p1+3) 4 + *(L2+I) [4]<<<<<<<<<<<<<<<<<<<<[4] *(p1+3) = temp [1 2 3 4 a b c d e f g h i]
    where the addition has to wrap around as graff describes
      There was a typo that messed me up for a bit,

      Sorry! But they aren't easy to construct. (What I need is a program .... :)

      and you solved this one "backwards"

      Yes. I eventually noticed that I'd switched horses mid way. I went back and re-did them all consistently. I'm not sure if one way is easier or better than the other; and if it is, if I've chosen the right way. But anyhoo, here are the (now) 4 cases I think are significant; all done the same way.

      The first two are nicely consistent in that the shorter elements move in left-to-right order; albeit that in the first they always go through the temp reg, and the second, only the first one does so.

      The other two are neither consistent with the first two; nor each other :(

      There are definitely two nested loops; the shorter is the outer, the longer the inner. And graff is definitely right that molulo (p1 % p2len) arithmetic is key.

      But the decision about when to fetch the temp register; or move from the shorter buffer (and which one) is eluding me for the moment.

      even(6)/odd(3) odd(9)/even(4) + Even(10)/even(4) Odd(11)/Odd(3) p2+p2len -3-2-1 p2+p2len -4-3-2-1 + p2+p2len -4-3-2-1 p2+p2len + -4-3-2-1 p2 +0+1+2 p2 +0+1+2+3 + p2 +0+1+2+3 p2 + +0+1+2+3 p1+p1len-6-5-4-3-2-1 p1+p1len-9-8-7-6-5-4-3-2-1 + p1+p1len-0-9-8-7-6-5-4-3-2-1 p1+p1len-9-8-7-6 +-5-4-3-2-1 p1 +0+1+2+3+4+5 p1 +0+1+2+3+4+5+6+7+8 + p1 +0+1+2+3+4+5+6+7+8 p1 +0+1+2+3 ++4+5+6+7+8 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 + 0 1 2 3 4 5 6 7 8 0 1 2 3 + 4 5 6 7 8 [a b c d e f 1 2 3] temp [a b c d e f g h i 1 2 3 4 +] temp [a b c d e f g h i j 1 2 3 4] temp [a b c d + e f g h i j k 1 2 3] temp [1]>>>>>>[1] [1]>>>>> +>>>[1] [1]>>>>>>>[1] + [1]>>>>>>[1] [d]>>>[d] [f]>>>>>[f] + [g]>>>>>[g] + [i]>>>[i] [a]>>>[a] [b]>>>>>[b] + [c]>>>>>[c] + [f]>>>[f] [1]<<<<<<<<<<<<<<<<<<[1] [2]<<<<<<<<<<<<<<<[2] + [3]<<<<<<<<<<<<<<<<<[3] [c]> +>>[c] [2]>>>>[2] [g]>>>>>[g] + [i]>>>>>[i] [2]< +<<<<<<<<<<<<<<<<[2] [e]>>>[e] [c]>>>>>[c] + [e]>>>>>[e] + [j]>>>[j] [b]>>>[b] [3]<<<<<<<<<<<<<<<[3] + [a]>>>>>[a] + [g]>>>[g] [2]<<<<<<<<<<<<<<<<[2] [h]>>>>>[h] + [1]<<<<<<<<<<<<<<<<<<<<<<<<<<<[1] [d +]>>>[d] [3]>>[3] [d]>>>>>[d] + [2]>>>>>[2] [a]>>>[a +] [f]>>>[f] [4]<<<<<<<<<<<<<<<[4 +] [h]>>>>>[h] [1]<<<<< +<<<<<<<<<<<<<<<<<<<<<<<[1] [c]>>>[c] [i]>>>>>[i +] [d]>>>>>[d] + [3]>>[3] [3]<<<<<<<<<<<<<<[3] [e]>>>>>[e] + [4]<<<<<<<<<<<<<<<<<[4] + [k]>>>[k] [1 2 3 a b c d e f] [a]>>>>>[a] + [j]>>>>>[j] + [h]>>>[h] [1]<<<<<<<<<<<<<<<<<<<<<<< +<<<[1] [f]>>>>>[f] +[e]>>>[e] [1 2 3 4 a b c d e f g h i +] [b]>>>>>[b] [b]>>> +[b] + [2]<<<<<<<<<<<<<<<<<<<<<<<<<[2] [3]<<< +<<<<<<<<<<<<<<<<<<<<<<<[3] + [1 2 3 4 a b c d e f g h i j] [1 2 3 a + b c d e f g h i j k]

      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