summaryrefslogtreecommitdiff
path: root/rotate.c
diff options
context:
space:
mode:
Diffstat (limited to 'rotate.c')
-rw-r--r--rotate.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/rotate.c b/rotate.c
new file mode 100644
index 0000000..5eb7696
--- /dev/null
+++ b/rotate.c
@@ -0,0 +1,19 @@
+#include <stddef.h>
+
+#include "common.h"
+
+/* rotates left */
+void rotate(char *base, size_t size, size_t shift)
+{
+ int dir = 1;
+ while (shift) {
+ while (2*shift <= size) {
+ swap(base, base + dir*shift, shift);
+ size -= shift;
+ base += shift*dir;
+ }
+ shift = size - shift;
+ base = dir > 0 ? base + size - shift : base - shift;
+ dir *= -1;
+ }
+}