Rotate Vector


meRotateVectorUtilInplace(vector& aInput, int iSteps)
is to rotate to the right iSteps, in place
for Input: 1 2 3 4 5 6 7 8 9
the last 3 spots are used as buffer, which keeps replacing the left first 3, then second 3… by swap.
we swap pDst and pSrc, pDst is on the left, pSrc start with 7, the start of buffer. we swap until end().
on next round, we just move pSrc back to the start of buffer, which is pHead, the start of buffer.
pDst reaching start of buffer means we are done, like the 3d from last.
when only portion of buffer is move to the left, due to pDst has been to close to buffer, we shrink the buffer and keep going, buy setting hHead.

Input: with iSteps 2 : 1 2 3 4 5
    aInput: 4 5 3 1 2
    pSrc reset to 1
    only portion of buffer is used, reset pHead, the start of shinked buffer, to 2
    aInput: 4 5 1 2 3
    pSrc reset to 3
    output: 4 5 1 2 3
    started i = 3

Input: with iSteps 3 : 1 2 3 4 5
    only portion of buffer is used, reset pHead, the start of shinked buffer, to 5
    aInput: 3 4 5 2 1
    pSrc reset to 1
    aInput: 3 4 5 1 2
    pSrc reset to 2
    output: 3 4 5 1 2

Download: http://riowing.net/p/wp/SpiralMatrix.cpp

Previous Post
Next Post
Leave a comment

Leave a comment