15
15
16
16
#include "common.h"
17
17
18
- static int
19
- _IsContiguous (PyArrayObject * ap );
20
-
21
- static int
22
- _IsFortranContiguous (PyArrayObject * ap );
18
+ static void
19
+ _UpdateContiguousFlags (PyArrayObject * ap );
23
20
24
21
/*NUMPY_API
25
22
*
@@ -62,28 +59,9 @@ PyArray_NewFlagsObject(PyObject *obj)
62
59
NPY_NO_EXPORT void
63
60
PyArray_UpdateFlags (PyArrayObject * ret , int flagmask )
64
61
{
65
-
66
- if (flagmask & NPY_ARRAY_F_CONTIGUOUS ) {
67
- if (_IsFortranContiguous (ret )) {
68
- PyArray_ENABLEFLAGS (ret , NPY_ARRAY_F_CONTIGUOUS );
69
- if (PyArray_NDIM (ret ) > 1 ) {
70
- PyArray_CLEARFLAGS (ret , NPY_ARRAY_C_CONTIGUOUS );
71
- }
72
- }
73
- else {
74
- PyArray_CLEARFLAGS (ret , NPY_ARRAY_F_CONTIGUOUS );
75
- }
76
- }
77
- if (flagmask & NPY_ARRAY_C_CONTIGUOUS ) {
78
- if (_IsContiguous (ret )) {
79
- PyArray_ENABLEFLAGS (ret , NPY_ARRAY_C_CONTIGUOUS );
80
- if (PyArray_NDIM (ret ) > 1 ) {
81
- PyArray_CLEARFLAGS (ret , NPY_ARRAY_F_CONTIGUOUS );
82
- }
83
- }
84
- else {
85
- PyArray_CLEARFLAGS (ret , NPY_ARRAY_C_CONTIGUOUS );
86
- }
62
+ /* Always update both, as its not trivial to guess one from the other */
63
+ if (flagmask & (NPY_ARRAY_F_CONTIGUOUS | NPY_ARRAY_C_CONTIGUOUS )) {
64
+ _UpdateContiguousFlags (ret );
87
65
}
88
66
if (flagmask & NPY_ARRAY_ALIGNED ) {
89
67
if (_IsAligned (ret )) {
@@ -110,66 +88,71 @@ PyArray_UpdateFlags(PyArrayObject *ret, int flagmask)
110
88
111
89
/*
112
90
* Check whether the given array is stored contiguously
113
- * (row-wise) in memory.
91
+ * in memory. And update the passed in ap apropriately .
114
92
*
115
- * 0-strided arrays are not contiguous (even if dimension == 1)
93
+ * 0-strided arrays are not contiguous. A dimension == 1 is always ok.
116
94
*/
117
- static int
118
- _IsContiguous (PyArrayObject * ap )
95
+ static void
96
+ _UpdateContiguousFlags (PyArrayObject * ap )
119
97
{
120
98
npy_intp sd ;
121
99
npy_intp dim ;
122
100
int i ;
101
+ npy_bool is_c_contig = 1 ;
123
102
124
103
if (PyArray_NDIM (ap ) == 0 ) {
125
- return 1 ;
104
+ PyArray_ENABLEFLAGS (ap , NPY_ARRAY_C_CONTIGUOUS );
105
+ PyArray_ENABLEFLAGS (ap , NPY_ARRAY_F_CONTIGUOUS );
106
+ return ;
126
107
}
127
108
sd = PyArray_DESCR (ap )-> elsize ;
128
109
if (PyArray_NDIM (ap ) == 1 ) {
129
- return PyArray_DIMS (ap )[0 ] == 1 || sd == PyArray_STRIDES (ap )[0 ];
110
+ if (PyArray_DIMS (ap )[0 ] == 1 || sd == PyArray_STRIDES (ap )[0 ]) {
111
+ PyArray_ENABLEFLAGS (ap , NPY_ARRAY_C_CONTIGUOUS );
112
+ PyArray_ENABLEFLAGS (ap , NPY_ARRAY_F_CONTIGUOUS );
113
+ return ;
114
+ }
115
+ PyArray_CLEARFLAGS (ap , NPY_ARRAY_C_CONTIGUOUS );
116
+ PyArray_CLEARFLAGS (ap , NPY_ARRAY_F_CONTIGUOUS );
117
+ return ;
130
118
}
119
+
131
120
for (i = PyArray_NDIM (ap ) - 1 ; i >= 0 ; -- i ) {
132
121
dim = PyArray_DIMS (ap )[i ];
133
122
/* contiguous by definition */
134
123
if (dim == 0 ) {
135
- return 1 ;
124
+ PyArray_ENABLEFLAGS (ap , NPY_ARRAY_C_CONTIGUOUS );
125
+ PyArray_ENABLEFLAGS (ap , NPY_ARRAY_F_CONTIGUOUS );
126
+ return ;
136
127
}
137
- if (PyArray_STRIDES (ap )[i ] != sd ) {
138
- return 0 ;
128
+ if (dim != 1 ) {
129
+ if (PyArray_STRIDES (ap )[i ] != sd ) {
130
+ is_c_contig = 0 ;
131
+ }
132
+ sd *= dim ;
139
133
}
140
- sd *= dim ;
141
134
}
142
- return 1 ;
143
- }
144
-
145
-
146
- /* 0-strided arrays are not contiguous (even if dimension == 1) */
147
- static int
148
- _IsFortranContiguous (PyArrayObject * ap )
149
- {
150
- npy_intp sd ;
151
- npy_intp dim ;
152
- int i ;
153
-
154
- if (PyArray_NDIM (ap ) == 0 ) {
155
- return 1 ;
135
+ if (is_c_contig ) {
136
+ PyArray_ENABLEFLAGS (ap , NPY_ARRAY_C_CONTIGUOUS );
156
137
}
157
- sd = PyArray_DESCR (ap )-> elsize ;
158
- if (PyArray_NDIM (ap ) == 1 ) {
159
- return PyArray_DIMS (ap )[0 ] == 1 || sd == PyArray_STRIDES (ap )[0 ];
138
+ else {
139
+ PyArray_CLEARFLAGS (ap , NPY_ARRAY_C_CONTIGUOUS );
160
140
}
141
+
142
+ /* check if fortran contiguous */
143
+ sd = PyArray_DESCR (ap )-> elsize ;
161
144
for (i = 0 ; i < PyArray_NDIM (ap ); ++ i ) {
162
145
dim = PyArray_DIMS (ap )[i ];
163
- /* fortran contiguous by definition */
164
- if (dim == 0 ) {
165
- return 1 ;
166
- }
167
- if ( PyArray_STRIDES ( ap )[ i ] != sd ) {
168
- return 0 ;
146
+ if ( dim != 1 ) {
147
+ if (PyArray_STRIDES ( ap )[ i ] != sd ) {
148
+ PyArray_CLEARFLAGS ( ap , NPY_ARRAY_F_CONTIGUOUS ) ;
149
+ return ;
150
+ }
151
+ sd *= dim ;
169
152
}
170
- sd *= dim ;
171
153
}
172
- return 1 ;
154
+ PyArray_ENABLEFLAGS (ap , NPY_ARRAY_F_CONTIGUOUS );
155
+ return ;
173
156
}
174
157
175
158
static void
0 commit comments