diff options
-rw-r--r-- | rotate.c | 20 |
1 files changed, 9 insertions, 11 deletions
@@ -6,17 +6,15 @@ /* rotates left */ void rotate(char *base, size_t size, size_t shift) { - int dir = 1; - - counts[CURRENT].rotate++; - while (shift) { - while (2*shift <= size) { - swap(base, base + dir*shift, shift); - size -= shift; - base += shift*dir; + size_t a = shift, b = size - shift; + while (a && b) { + if (a <= b) { + swap(base, base + b, a); + b -= a; + } else { + swap(base, base + a, b); + base += b; + a -= b; } - shift = size - shift; - base = dir > 0 ? base + size - shift : base - shift; - dir *= -1; } } |