On 19.10.2018 01:23, bitrex wrote:
> I need an implementation of rotate for a microprocessor platform where I
> have C++11 compiler but really nothing else available to work with. I'd
> like to be able to rotate the elements of a short array of characters of
> arbitrary length e.g. ABCD -> BCDA -> CDAB etc.
If the microprocessor is 32-bit or better, just use its rotate instruction.
> Something like the following should be good enough, but what's good to
> use for "swap"? "algorithm" is a non-starter here, too.
Don't move data by repeated swapping. For moving, move.
> #include <algorithm>
>
> string rotate(string s) {
> for (int i = 1; i < s.size(); i++)
> swap(s[i-1], s[i]);
> return s;
> }
You want a mutating routine for efficiency, not a function.
Like,
void rotate_buffer( char* buf, int size )
{
// Use memmov for the moving.
}
void rotate( string& s )
{
rotate_buffer( s.data(), s.size() );
}
Cheers & hth.,
- Alf