MCPcopy Create free account
hub / github.com/numpy/numpy / _UpdateContiguousFlags

Function _UpdateContiguousFlags

numpy/core/src/multiarray/flagsobject.c:114–159  ·  view source on GitHub ↗

* Check whether the given array is stored contiguously * in memory. And update the passed in ap flags appropriately. * * The traditional rule is that for an array to be flagged as C contiguous, * the following must hold: * * strides[-1] == itemsize * strides[i] == shape[i+1] * strides[i + 1] * * And for an array to be flagged as F contiguous, the obvious reversal: * * strides[0] == item

Source from the content-addressed store, hash-verified

112 * The above rules thus only apply when ignoring all size 1 dimensions.
113 */
114static void
115_UpdateContiguousFlags(PyArrayObject *ap)
116{
117 npy_intp sd;
118 npy_intp dim;
119 int i;
120 npy_bool is_c_contig = 1;
121
122 sd = PyArray_ITEMSIZE(ap);
123 for (i = PyArray_NDIM(ap) - 1; i >= 0; --i) {
124 dim = PyArray_DIMS(ap)[i];
125 /* contiguous by definition */
126 if (dim == 0) {
127 PyArray_ENABLEFLAGS(ap, NPY_ARRAY_C_CONTIGUOUS);
128 PyArray_ENABLEFLAGS(ap, NPY_ARRAY_F_CONTIGUOUS);
129 return;
130 }
131 if (dim != 1) {
132 if (PyArray_STRIDES(ap)[i] != sd) {
133 is_c_contig = 0;
134 }
135 sd *= dim;
136 }
137 }
138 if (is_c_contig) {
139 PyArray_ENABLEFLAGS(ap, NPY_ARRAY_C_CONTIGUOUS);
140 }
141 else {
142 PyArray_CLEARFLAGS(ap, NPY_ARRAY_C_CONTIGUOUS);
143 }
144
145 /* check if fortran contiguous */
146 sd = PyArray_ITEMSIZE(ap);
147 for (i = 0; i < PyArray_NDIM(ap); ++i) {
148 dim = PyArray_DIMS(ap)[i];
149 if (dim != 1) {
150 if (PyArray_STRIDES(ap)[i] != sd) {
151 PyArray_CLEARFLAGS(ap, NPY_ARRAY_F_CONTIGUOUS);
152 return;
153 }
154 sd *= dim;
155 }
156 }
157 PyArray_ENABLEFLAGS(ap, NPY_ARRAY_F_CONTIGUOUS);
158 return;
159}
160
161static void
162arrayflags_dealloc(PyArrayFlagsObject *self)

Callers 1

PyArray_UpdateFlagsFunction · 0.85

Calls 6

PyArray_ITEMSIZEFunction · 0.85
PyArray_NDIMFunction · 0.85
PyArray_DIMSFunction · 0.85
PyArray_ENABLEFLAGSFunction · 0.85
PyArray_STRIDESFunction · 0.85
PyArray_CLEARFLAGSFunction · 0.85

Tested by

no test coverage detected