Blender  V2.93
array_utils.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include "MEM_guardedalloc.h"
29 
30 #include "BLI_alloca.h"
31 #include "BLI_math_base.h"
32 #include "BLI_strict_flags.h"
33 #include "BLI_sys_types.h"
34 #include "BLI_utildefines.h"
35 
36 #include "BLI_array_utils.h"
37 
43 void _bli_array_reverse(void *arr_v, unsigned int arr_len, size_t arr_stride)
44 {
45  const unsigned int arr_stride_uint = (unsigned int)arr_stride;
46  const unsigned int arr_half_stride = (arr_len / 2) * arr_stride_uint;
47  unsigned int i, i_end;
48  char *arr = arr_v;
49  char *buf = BLI_array_alloca(buf, arr_stride);
50 
51  for (i = 0, i_end = (arr_len - 1) * arr_stride_uint; i < arr_half_stride;
52  i += arr_stride_uint, i_end -= arr_stride_uint) {
53  memcpy(buf, &arr[i], arr_stride);
54  memcpy(&arr[i], &arr[i_end], arr_stride);
55  memcpy(&arr[i_end], buf, arr_stride);
56  }
57 }
58 
65 void _bli_array_wrap(void *arr_v, unsigned int arr_len, size_t arr_stride, int dir)
66 {
67  char *arr = arr_v;
68  char *buf = BLI_array_alloca(buf, arr_stride);
69 
70  if (dir == -1) {
71  memcpy(buf, arr, arr_stride);
72  memmove(arr, arr + arr_stride, arr_stride * (arr_len - 1));
73  memcpy(arr + (arr_stride * (arr_len - 1)), buf, arr_stride);
74  }
75  else if (dir == 1) {
76  memcpy(buf, arr + (arr_stride * (arr_len - 1)), arr_stride);
77  memmove(arr + arr_stride, arr, arr_stride * (arr_len - 1));
78  memcpy(arr, buf, arr_stride);
79  }
80  else {
81  BLI_assert(0);
82  }
83 }
84 
91 void _bli_array_permute(void *arr,
92  const unsigned int arr_len,
93  const size_t arr_stride,
94  const unsigned int *order,
95  void *arr_temp)
96 {
97  const size_t len = arr_len * arr_stride;
98  const unsigned int arr_stride_uint = (unsigned int)arr_stride;
99  void *arr_orig;
100  unsigned int i;
101 
102  if (arr_temp == NULL) {
103  arr_orig = MEM_mallocN(len, __func__);
104  }
105  else {
106  arr_orig = arr_temp;
107  }
108 
109  memcpy(arr_orig, arr, len);
110 
111  for (i = 0; i < arr_len; i++) {
112  BLI_assert(order[i] < arr_len);
113  memcpy(POINTER_OFFSET(arr, arr_stride_uint * i),
114  POINTER_OFFSET(arr_orig, arr_stride_uint * order[i]),
115  arr_stride);
116  }
117 
118  if (arr_temp == NULL) {
119  MEM_freeN(arr_orig);
120  }
121 }
122 
130 int _bli_array_findindex(const void *arr, unsigned int arr_len, size_t arr_stride, const void *p)
131 {
132  const char *arr_step = (const char *)arr;
133  for (unsigned int i = 0; i < arr_len; i++, arr_step += arr_stride) {
134  if (memcmp(arr_step, p, arr_stride) == 0) {
135  return (int)i;
136  }
137  }
138  return -1;
139 }
140 
144 int _bli_array_rfindindex(const void *arr, unsigned int arr_len, size_t arr_stride, const void *p)
145 {
146  const char *arr_step = (const char *)arr + (arr_stride * arr_len);
147  for (unsigned int i = arr_len; i-- != 0;) {
148  arr_step -= arr_stride;
149  if (memcmp(arr_step, p, arr_stride) == 0) {
150  return (int)i;
151  }
152  }
153  return -1;
154 }
155 
157  void *arr, const void *arr_a, const void *arr_b, unsigned int arr_len, size_t arr_stride)
158 {
159  char *dst = arr;
160  const char *src_a = arr_a;
161  const char *src_b = arr_b;
162 
163  size_t i = arr_stride * arr_len;
164  while (i--) {
165  *(dst++) = *(src_a++) & *(src_b++);
166  }
167 }
168 
170  void *arr, const void *arr_a, const void *arr_b, unsigned int arr_len, size_t arr_stride)
171 {
172  char *dst = arr;
173  const char *src_a = arr_a;
174  const char *src_b = arr_b;
175 
176  size_t i = arr_stride * arr_len;
177  while (i--) {
178  *(dst++) = *(src_a++) | *(src_b++);
179  }
180 }
181 
198 bool _bli_array_iter_span(const void *arr,
199  unsigned int arr_len,
200  size_t arr_stride,
201  bool use_wrap,
202  bool use_delimit_bounds,
203  bool (*test_fn)(const void *arr_item, void *user_data),
204  void *user_data,
205  unsigned int span_step[2],
206  unsigned int *r_span_len)
207 {
208  if (arr_len == 0) {
209  return false;
210  }
211  if (use_wrap && (span_step[0] != arr_len) && (span_step[0] > span_step[1])) {
212  return false;
213  }
214 
215  const unsigned int arr_stride_uint = (unsigned int)arr_stride;
216  const void *item_prev;
217  bool test_prev;
218 
219  unsigned int i_curr;
220 
221  if ((span_step[0] == arr_len) && (span_step[1] == arr_len)) {
222  if (use_wrap) {
223  item_prev = POINTER_OFFSET(arr, (arr_len - 1) * arr_stride_uint);
224  i_curr = 0;
225  test_prev = test_fn(item_prev, user_data);
226  }
227  else if (use_delimit_bounds == false) {
228  item_prev = arr;
229  i_curr = 1;
230  test_prev = test_fn(item_prev, user_data);
231  }
232  else {
233  item_prev = NULL;
234  i_curr = 0;
235  test_prev = false;
236  }
237  }
238  else if ((i_curr = span_step[1] + 2) < arr_len) {
239  item_prev = POINTER_OFFSET(arr, (span_step[1] + 1) * arr_stride_uint);
240  test_prev = test_fn(item_prev, user_data);
241  }
242  else {
243  return false;
244  }
245  BLI_assert(i_curr < arr_len);
246 
247  const void *item_curr = POINTER_OFFSET(arr, i_curr * arr_stride_uint);
248 
249  while (i_curr < arr_len) {
250  bool test_curr = test_fn(item_curr, user_data);
251  if ((test_prev == false) && (test_curr == true)) {
252  unsigned int span_len;
253  unsigned int i_step_prev = i_curr;
254 
255  if (use_wrap) {
256  unsigned int i_step = i_curr + 1;
257  if (UNLIKELY(i_step == arr_len)) {
258  i_step = 0;
259  }
260  while (test_fn(POINTER_OFFSET(arr, i_step * arr_stride_uint), user_data)) {
261  i_step_prev = i_step;
262  i_step++;
263  if (UNLIKELY(i_step == arr_len)) {
264  i_step = 0;
265  }
266  }
267 
268  if (i_step_prev < i_curr) {
269  span_len = (i_step_prev + (arr_len - i_curr)) + 1;
270  }
271  else {
272  span_len = (i_step_prev - i_curr) + 1;
273  }
274  }
275  else {
276  unsigned int i_step = i_curr + 1;
277  while ((i_step != arr_len) &&
278  test_fn(POINTER_OFFSET(arr, i_step * arr_stride_uint), user_data)) {
279  i_step_prev = i_step;
280  i_step++;
281  }
282 
283  span_len = (i_step_prev - i_curr) + 1;
284 
285  if ((use_delimit_bounds == false) && (i_step_prev == arr_len - 1)) {
286  return false;
287  }
288  }
289 
290  span_step[0] = i_curr;
291  span_step[1] = i_step_prev;
292  *r_span_len = span_len;
293 
294  return true;
295  }
296 
297  test_prev = test_curr;
298 
299  item_prev = item_curr;
300  item_curr = POINTER_OFFSET(item_curr, arr_stride_uint);
301  i_curr++;
302  }
303 
304  return false;
305 }
306 
310 bool _bli_array_is_zeroed(const void *arr_v, unsigned int arr_len, size_t arr_stride)
311 {
312  const char *arr_step = (const char *)arr_v;
313  size_t i = arr_stride * arr_len;
314  while (i--) {
315  if (*(arr_step++)) {
316  return false;
317  }
318  }
319  return true;
320 }
321 
329 bool _bli_array_iter_spiral_square(const void *arr_v,
330  const int arr_shape[2],
331  size_t elem_size,
332  const int center[2],
333  bool (*test_fn)(const void *arr_item, void *user_data),
334  void *user_data)
335 {
336  BLI_assert(center[0] >= 0 && center[1] >= 0 && center[0] < arr_shape[0] &&
337  center[1] < arr_shape[1]);
338 
339  const char *arr = arr_v;
340  const int stride[2] = {arr_shape[0] * (int)elem_size, (int)elem_size};
341 
342  /* Test center first. */
343  int ofs[2] = {center[0] * stride[1], center[1] * stride[0]};
344  if (test_fn(arr + ofs[0] + ofs[1], user_data)) {
345  return true;
346  }
347 
348  /* #steps_in and #steps_out are the "diameters" of the inscribed and circumscribed squares in the
349  * rectangle. Each step smaller than #steps_in does not need to check bounds. */
350  int steps_in, steps_out;
351  {
352  int x_minus = center[0];
353  int x_plus = arr_shape[0] - center[0] - 1;
354  int y_minus = center[1];
355  int y_plus = arr_shape[1] - center[1] - 1;
356 
357  steps_in = 2 * min_iiii(x_minus, x_plus, y_minus, y_plus);
358  steps_out = 2 * max_iiii(x_minus, x_plus, y_minus, y_plus);
359  }
360 
361  /* For check_bounds. */
362  int limits[2] = {(arr_shape[0] - 1) * stride[0], stride[0] - stride[1]};
363 
364  int steps = 0;
365  while (steps < steps_out) {
366  steps += 2;
367 
368  /* Move one step to the diagonal of the negative quadrant. */
369  ofs[0] -= stride[0];
370  ofs[1] -= stride[1];
371 
372  bool check_bounds = steps > steps_in;
373 
374  /* sign: 0 neg; 1 pos; */
375  for (int sign = 2; sign--;) {
376  /* axis: 0 x; 1 y; */
377  for (int axis = 2; axis--;) {
378  int ofs_step = stride[axis];
379  if (!sign) {
380  ofs_step *= -1;
381  }
382 
383  int ofs_iter = ofs[axis] + ofs_step;
384  int ofs_dest = ofs[axis] + steps * ofs_step;
385  int ofs_other = ofs[!axis];
386 
387  ofs[axis] = ofs_dest;
388  if (check_bounds) {
389  if (ofs_other < 0 || ofs_other > limits[!axis]) {
390  /* Out of bounds. */
391  continue;
392  }
393 
394  CLAMP(ofs_iter, 0, limits[axis]);
395  CLAMP(ofs_dest, 0, limits[axis]);
396  }
397 
398  while (true) {
399  if (test_fn(arr + ofs_other + ofs_iter, user_data)) {
400  return true;
401  }
402  if (ofs_iter == ofs_dest) {
403  break;
404  }
405  ofs_iter += ofs_step;
406  }
407  }
408  }
409  }
410  return false;
411 }
#define BLI_array_alloca(arr, realsize)
Definition: BLI_alloca.h:36
Generic array manipulation API.
#define BLI_assert(a)
Definition: BLI_assert.h:58
MINLINE int max_iiii(int a, int b, int c, int d)
MINLINE int min_iiii(int a, int b, int c, int d)
Strict compiler flags for areas of code we want to ensure don't do conversions without us knowing abo...
#define UNLIKELY(x)
#define POINTER_OFFSET(v, ofs)
NSNotificationCenter * center
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei stride
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint order
Read Guarded memory(de)allocation.
Group RGB to Bright Vector Camera CLAMP
bool _bli_array_iter_span(const void *arr, unsigned int arr_len, size_t arr_stride, bool use_wrap, bool use_delimit_bounds, bool(*test_fn)(const void *arr_item, void *user_data), void *user_data, unsigned int span_step[2], unsigned int *r_span_len)
Definition: array_utils.c:198
bool _bli_array_is_zeroed(const void *arr_v, unsigned int arr_len, size_t arr_stride)
Definition: array_utils.c:310
void _bli_array_reverse(void *arr_v, unsigned int arr_len, size_t arr_stride)
Definition: array_utils.c:43
int _bli_array_rfindindex(const void *arr, unsigned int arr_len, size_t arr_stride, const void *p)
Definition: array_utils.c:144
void _bli_array_wrap(void *arr_v, unsigned int arr_len, size_t arr_stride, int dir)
Definition: array_utils.c:65
void _bli_array_permute(void *arr, const unsigned int arr_len, const size_t arr_stride, const unsigned int *order, void *arr_temp)
Definition: array_utils.c:91
bool _bli_array_iter_spiral_square(const void *arr_v, const int arr_shape[2], size_t elem_size, const int center[2], bool(*test_fn)(const void *arr_item, void *user_data), void *user_data)
Definition: array_utils.c:329
void _bli_array_binary_and(void *arr, const void *arr_a, const void *arr_b, unsigned int arr_len, size_t arr_stride)
Definition: array_utils.c:156
int _bli_array_findindex(const void *arr, unsigned int arr_len, size_t arr_stride, const void *p)
Definition: array_utils.c:130
void _bli_array_binary_or(void *arr, const void *arr_a, const void *arr_b, unsigned int arr_len, size_t arr_stride)
Definition: array_utils.c:169
void * user_data
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
double sign(double arg)
Definition: utility.h:250
static const int steps
Definition: sky_nishita.cpp:28
uint len