diff options
author | Campbell Barton <ideasman42@gmail.com> | 2013-08-14 03:43:44 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2013-08-14 03:43:44 +0400 |
commit | 5ba898d86894e067960a000f3c05f03baa1d2695 (patch) | |
tree | 8046c415b0e5af2354179e269da40b39981cebc7 /source/blender/blenlib/intern | |
parent | 91f04bebf00405223c8e0f6dcbac26a86e31f1af (diff) |
utility functions to reverse and wrap arrays.
Diffstat (limited to 'source/blender/blenlib/intern')
-rw-r--r-- | source/blender/blenlib/intern/BLI_array.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/source/blender/blenlib/intern/BLI_array.c b/source/blender/blenlib/intern/BLI_array.c index 5823b7db3f1..510bf072513 100644 --- a/source/blender/blenlib/intern/BLI_array.c +++ b/source/blender/blenlib/intern/BLI_array.c @@ -59,9 +59,14 @@ */ #include <string.h> +#include <stdlib.h> #include "BLI_array.h" +#include "BLI_sys_types.h" +#include "BLI_utildefines.h" +#include "BLI_alloca.h" + #include "MEM_guardedalloc.h" /** @@ -95,3 +100,40 @@ void _bli_array_grow_func(void **arr_p, const void *arr_static, arr_count += num; #endif } + +void _bli_array_reverse(void *arr_v, unsigned int arr_len, size_t arr_stride) +{ + const unsigned int arr_half_stride = (arr_len / 2) * arr_stride; + unsigned int i, i_end; + char *arr = arr_v; + char *buf = BLI_array_alloca(buf, arr_stride); + + for (i = 0, i_end = (arr_len - 1) * arr_stride; + i < arr_half_stride; + i += arr_stride, i_end -= arr_stride) + { + memcpy(buf, &arr[i], arr_stride); + memcpy(&arr[i], &arr[i_end], arr_stride); + memcpy(&arr[i_end], buf, arr_stride); + } +} + +void _bli_array_wrap(void *arr_v, unsigned int arr_len, size_t arr_stride, int dir) +{ + char *arr = arr_v; + char *buf = BLI_array_alloca(buf, arr_stride); + + if (dir == -1) { + memcpy(buf, arr, arr_stride); + memmove(arr, arr + arr_stride, arr_stride * (arr_len - 1)); + memcpy(arr + (arr_stride * (arr_len - 1)), buf, arr_stride); + } + else if (dir == 1) { + memcpy(buf, arr + (arr_stride * (arr_len - 1)), arr_stride); + memmove(arr + arr_stride, arr, arr_stride * (arr_len - 1)); + memcpy(arr, buf, arr_stride); + } + else { + BLI_assert(0); + } +} |