diff options
author | Campbell Barton <ideasman42@gmail.com> | 2015-04-27 21:10:31 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2015-04-27 21:12:48 +0300 |
commit | 278ff15c7f3c4a91ba049b7a13ecf8a4183ebf67 (patch) | |
tree | e3de79327764d875dff3960746ec899041c6e808 /source/blender/blenlib | |
parent | 6ada7a1a0b76d7c1d1203800c0bd84c5d1dc0f86 (diff) |
BLI_array: add permute utility function
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_array_utils.h | 8 | ||||
-rw-r--r-- | source/blender/blenlib/intern/array_utils.c | 32 |
2 files changed, 40 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_array_utils.h b/source/blender/blenlib/BLI_array_utils.h index 0dd1439e58c..ff7976dc701 100644 --- a/source/blender/blenlib/BLI_array_utils.h +++ b/source/blender/blenlib/BLI_array_utils.h @@ -34,6 +34,14 @@ void _bli_array_wrap(void *arr, unsigned int arr_len, size_t arr_stride, int dir #define BLI_array_wrap(arr, arr_len, dir) \ _bli_array_wrap(arr, arr_len, sizeof(*(arr)), dir) +void _bli_array_permute( + void *arr, const unsigned int arr_len, const size_t arr_stride, + const unsigned int *index, void *arr_temp); +#define BLI_array_permute(arr, arr_len, order) \ + _bli_array_permute(arr, arr_len, sizeof(*(arr)), order, NULL) +#define BLI_array_permute_ex(arr, arr_len, index, arr_temp) \ + _bli_array_permute(arr, arr_len, sizeof(*(arr)), order, arr_temp) + int _bli_array_findindex(const void *arr, unsigned int arr_len, size_t arr_stride, const void *p); #define BLI_array_findindex(arr, arr_len, p) \ _bli_array_findindex(arr, arr_len, sizeof(*(arr)), p) diff --git a/source/blender/blenlib/intern/array_utils.c b/source/blender/blenlib/intern/array_utils.c index 173effbc434..efa107ed703 100644 --- a/source/blender/blenlib/intern/array_utils.c +++ b/source/blender/blenlib/intern/array_utils.c @@ -25,6 +25,8 @@ #include <string.h> #include <stdlib.h> +#include "MEM_guardedalloc.h" + #include "BLI_array_utils.h" #include "BLI_sys_types.h" @@ -69,6 +71,36 @@ void _bli_array_wrap(void *arr_v, unsigned int arr_len, size_t arr_stride, int d } } +void _bli_array_permute( + void *arr_v, const unsigned int arr_len, const size_t arr_stride, + const unsigned int *order, void *arr_temp) +{ + const size_t len = arr_len * arr_stride; + const unsigned int arr_stride_uint = arr_stride; + void *arr_orig; + unsigned int i; + + if (arr_temp == NULL) { + arr_orig = MEM_mallocN(len, __func__); + } + else { + arr_orig = arr_temp; + } + + memcpy(arr_orig, arr_v, len); + + for (i = 0; i < arr_len; i++) { + BLI_assert(order[i] < arr_len); + memcpy(POINTER_OFFSET(arr_v, arr_stride_uint * i), + POINTER_OFFSET(arr_orig, arr_stride_uint * order[i]), + arr_stride); + } + + if (arr_temp == NULL) { + MEM_freeN(arr_orig); + } +} + /** * \note Not efficient, use for error checks/asserts. */ |