utility functions to reverse and wrap arrays.
This commit is contained in:
@@ -173,4 +173,14 @@ void _bli_array_grow_func(void **arr_p, const void *arr_static,
|
|||||||
MEM_freeN(arr); \
|
MEM_freeN(arr); \
|
||||||
} (void)0
|
} (void)0
|
||||||
|
|
||||||
|
|
||||||
|
void _bli_array_reverse(void *arr, unsigned int arr_len, size_t arr_stride);
|
||||||
|
#define BLI_array_reverse(arr, arr_len) \
|
||||||
|
_bli_array_reverse(arr, arr_len, sizeof(*(arr)))
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
#endif /* __BLI_ARRAY_H__ */
|
#endif /* __BLI_ARRAY_H__ */
|
||||||
|
@@ -59,9 +59,14 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "BLI_array.h"
|
#include "BLI_array.h"
|
||||||
|
|
||||||
|
#include "BLI_sys_types.h"
|
||||||
|
#include "BLI_utildefines.h"
|
||||||
|
#include "BLI_alloca.h"
|
||||||
|
|
||||||
#include "MEM_guardedalloc.h"
|
#include "MEM_guardedalloc.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -95,3 +100,40 @@ void _bli_array_grow_func(void **arr_p, const void *arr_static,
|
|||||||
arr_count += num;
|
arr_count += num;
|
||||||
#endif
|
#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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user