summaryrefslogtreecommitdiff
path: root/rotate.c
diff options
context:
space:
mode:
Diffstat (limited to 'rotate.c')
-rw-r--r--rotate.c20
1 files changed, 9 insertions, 11 deletions
diff --git a/rotate.c b/rotate.c
index 1815812..6aebf7c 100644
--- a/rotate.c
+++ b/rotate.c
@@ -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;
}
}