[Bf-blender-cvs] [278ff15] master: BLI_array: add permute utility function

Campbell Barton noreply at git.blender.org
Mon Apr 27 20:13:11 CEST 2015


Commit: 278ff15c7f3c4a91ba049b7a13ecf8a4183ebf67
Author: Campbell Barton
Date:   Tue Apr 28 04:10:31 2015 +1000
Branches: master
https://developer.blender.org/rB278ff15c7f3c4a91ba049b7a13ecf8a4183ebf67

BLI_array: add permute utility function

===================================================================

M	source/blender/blenlib/BLI_array_utils.h
M	source/blender/blenlib/intern/array_utils.c

===================================================================

diff --git a/source/blender/blenlib/BLI_array_utils.h b/source/blender/blenlib/BLI_array_utils.h
index 0dd1439..ff7976d 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 173effb..efa107e 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.
  */




More information about the Bf-blender-cvs mailing list