33
44#include "ndarray.h"
55
6- typedef struct NDArrayAxisIterator {
7- int current_index ;
8- NDArray * array ;
9- int axis ;
10- } NDArrayAxisIterator ;
6+ typedef struct NDArrayIter {
7+ int nd_m1 ; /* number of dimensions - 1 */
8+ int index , size ;
9+ int coordinates [NDARRAY_MAX_DIMS ];/* N-dimensional loop */
10+ int dims_m1 [NDARRAY_MAX_DIMS ]; /* ao->dimensions - 1 */
11+ int strides [NDARRAY_MAX_DIMS ]; /* ao->strides or fake */
12+ int backstrides [NDARRAY_MAX_DIMS ];/* how far to jump back */
13+ int factors [NDARRAY_MAX_DIMS ]; /* shape factors */
14+ NDArray * ao ;
15+ char * dataptr ; /* pointer to current item*/
16+ bool contiguous ;
17+ int bounds [NDARRAY_MAX_DIMS ][2 ];
18+ int limits [NDARRAY_MAX_DIMS ][2 ];
19+ int limits_sizes [NDARRAY_MAX_DIMS ];
20+ } NDArrayIter ;
1121
1222NDArray * NDArrayIterator_GET (NDArray * array );
1323void NDArrayIterator_INIT (NDArray * array );
@@ -21,10 +31,60 @@ void NDArrayIteratorPHP_REWIND(NDArray* array);
2131int NDArrayIteratorPHP_ISDONE (NDArray * array );
2232void NDArrayIteratorPHP_NEXT (NDArray * array );
2333
24- NDArray * NDArrayAxisIterator_GET (NDArrayAxisIterator * it );
25- void NDArrayAxisIterator_NEXT (NDArrayAxisIterator * it );
26- void NDArrayAxisIterator_FREE (NDArrayAxisIterator * it );
27- void NDArrayAxisIterator_REWIND (NDArrayAxisIterator * it );
28- int NDArrayAxisIterator_ISDONE (NDArrayAxisIterator * it );
29- NDArrayAxisIterator * NDArrayAxisIterator_INIT (NDArray * array , int axis );
34+ NDArrayIter * NDArray_NewElementWiseIter (NDArray * target );
35+
36+ #define _NDArray_ITER_NEXT1 (it ) do { \
37+ (it)->dataptr += (it)->strides[0]; \
38+ (it)->coordinates[0]++; \
39+ } while (0)
40+
41+ #define _NDArray_ITER_NEXT2 (it ) do { \
42+ if ((it)->coordinates[1] < (it)->dims_m1[1]) { \
43+ (it)->coordinates[1]++; \
44+ (it)->dataptr += (it)->strides[1]; \
45+ } \
46+ else { \
47+ (it)->coordinates[1] = 0; \
48+ (it)->coordinates[0]++; \
49+ (it)->dataptr += (it)->strides[0] - \
50+ (it)->backstrides[1]; \
51+ } \
52+ } while (0)
53+
54+ #define NDArray_ITER_RESET (it ) do { \
55+ (it)->index = 0; \
56+ (it)->dataptr = NDArray_DATA((it)->ao); \
57+ memset((it)->coordinates, 0, \
58+ ((it)->nd_m1+1)*sizeof(int)); \
59+ } while (0)
60+
61+ #define NDArray_ITER_NEXT (it ) do { \
62+ (it)->index++; \
63+ if ((it)->nd_m1 == 0) { \
64+ _NDArray_ITER_NEXT1((it)); \
65+ } \
66+ else if ((it)->contiguous) \
67+ (it)->dataptr += NDArray_DESCRIPTOR((it)->ao)->elsize; \
68+ else if ((it)->nd_m1 == 1) { \
69+ _NDArray_ITER_NEXT2(it); \
70+ } \
71+ else { \
72+ int __nd_i; \
73+ for (__nd_i=(it)->nd_m1; __nd_i >= 0; __nd_i--) { \
74+ if ((it)->coordinates[__nd_i] < \
75+ (it)->dims_m1[__nd_i]) { \
76+ (it)->coordinates[__nd_i]++; \
77+ (it)->dataptr += \
78+ (it)->strides[__nd_i]; \
79+ break; \
80+ } \
81+ else { \
82+ (it)->coordinates[__nd_i] = 0; \
83+ (it)->dataptr -= \
84+ (it)->backstrides[__nd_i]; \
85+ } \
86+ } \
87+ } \
88+ } while (0)
89+ #define NDArray_ITER_DATA (it ) ((void *)((it)->dataptr))
3090#endif //PHPSCI_NDARRAY_ITERATORS_H
0 commit comments