Last active
February 12, 2023 19:25
-
-
Save mu578/2338b1a49e732f601c147271a20db80c to your computer and use it in GitHub Desktop.
rotate_forward.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
# define stl_scope_begin do { | |
# define stl_scope_end break; } while (0) | |
# define stl_basic_swap(_Tp, __a, __b) \ | |
stl_scope_begin \ | |
_Tp __stl_c = (__a); \ | |
(__a) = (__b); \ | |
(__b) = __stl_c; \ | |
stl_scope_end | |
# define stl_rotate_forward(_Tp, __first, __n_first, __last, __result) \ | |
stl_scope_begin \ | |
_Tp * __stl_i; \ | |
if ((__first) == (__n_first)) { \ | |
(__result) = (__last); \ | |
} else if ((__n_first) == (__last)) { \ | |
(__result) = (__first); \ | |
} else { \ | |
__stl_i = (__n_first); \ | |
while (1) { \ | |
stl_basic_swap(_Tp, *(__first), *__stl_i); \ | |
++(__first); \ | |
if (++__stl_i == __last) { \ | |
break; \ | |
} \ | |
if ((__first) == (__n_first)) { \ | |
(__n_first) = __stl_i; \ | |
} \ | |
} \ | |
(__result) = (__first); \ | |
if ((__first) != (__n_first)) { \ | |
__stl_i = (__n_first); \ | |
while (1) { \ | |
stl_basic_swap(_Tp, *(__first), *__stl_i); \ | |
++(__first); \ | |
if (++__stl_i == __last) { \ | |
if ((__first) == (__n_first)) { \ | |
break; \ | |
} \ | |
__stl_i = (__n_first); \ | |
} else if ((__first) == (__n_first)) { \ | |
(__n_first) = __stl_i; \ | |
} \ | |
} \ | |
} \ | |
} \ | |
stl_scope_end | |
int main(int argc, const char ** argv) | |
{ | |
int vec1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; | |
int vec2[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; | |
int * vec1_begin = vec1; | |
int * vec1_rotl = vec1 + 3; | |
int * vec1_end = vec1 + 9; | |
int * vec1_result; | |
int * vec2_begin = vec2; | |
int * vec2_rotr = vec2 + 9 - 4; | |
int * vec2_end = vec2 + 9; | |
int * vec2_result; | |
fprintf(stderr, "initial vector1 :\n[ "); | |
for (int i = 0; i < 9; i++) { | |
fprintf(stderr, "%d ", vec1[i]); | |
} | |
fprintf(stderr, "]\n"); | |
stl_rotate_forward(int | |
, vec1_begin | |
, vec1_rotl | |
, vec1_end | |
, vec1_result | |
); | |
fprintf(stderr, "after left rotation :\n[ "); | |
for (int i = 0; i < 9; i++) { | |
fprintf(stderr, "%d ", vec1[i]); | |
} | |
fprintf(stderr, "]\n"); | |
fprintf(stderr, "left rotation result:\n[ "); | |
while (vec1_result != vec1_end) { | |
fprintf(stderr, "%d ", *vec1_result); | |
++vec1_result; | |
} | |
fprintf(stderr, "]\n"); | |
fprintf(stderr, "\ninitial vector2 :\n[ "); | |
for (int i = 0; i < 9; i++) { | |
fprintf(stderr, "%d ", vec2[i]); | |
} | |
fprintf(stderr, "]\n"); | |
stl_rotate_forward(int | |
, vec2_begin | |
, vec2_rotr | |
, vec2_end | |
, vec2_result | |
); | |
fprintf(stderr, "after right rotation :\n[ "); | |
for (int i = 0; i < 9; i++) { | |
fprintf(stderr, "%d ", vec2[i]); | |
} | |
fprintf(stderr, "]\n"); | |
fprintf(stderr, "right rotation result:\n[ "); | |
while (vec2_result != vec2_end) { | |
fprintf(stderr, "%d ", *vec2_result); | |
++vec2_result; | |
} | |
fprintf(stderr, "]\n"); | |
return 0; | |
} | |
/* EOF */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment