NUMPY_API * Ravel * Returns a contiguous array */
| 887 | * Returns a contiguous array |
| 888 | */ |
| 889 | NPY_NO_EXPORT PyObject * |
| 890 | PyArray_Ravel(PyArrayObject *arr, NPY_ORDER order) |
| 891 | { |
| 892 | PyArray_Dims newdim = {NULL,1}; |
| 893 | npy_intp val[1] = {-1}; |
| 894 | |
| 895 | newdim.ptr = val; |
| 896 | |
| 897 | if (order == NPY_KEEPORDER) { |
| 898 | /* This handles some corner cases, such as 0-d arrays as well */ |
| 899 | if (PyArray_IS_C_CONTIGUOUS(arr)) { |
| 900 | order = NPY_CORDER; |
| 901 | } |
| 902 | else if (PyArray_IS_F_CONTIGUOUS(arr)) { |
| 903 | order = NPY_FORTRANORDER; |
| 904 | } |
| 905 | } |
| 906 | else if (order == NPY_ANYORDER) { |
| 907 | order = PyArray_ISFORTRAN(arr) ? NPY_FORTRANORDER : NPY_CORDER; |
| 908 | } |
| 909 | |
| 910 | if (order == NPY_CORDER && PyArray_IS_C_CONTIGUOUS(arr)) { |
| 911 | return PyArray_Newshape(arr, &newdim, NPY_CORDER); |
| 912 | } |
| 913 | else if (order == NPY_FORTRANORDER && PyArray_IS_F_CONTIGUOUS(arr)) { |
| 914 | return PyArray_Newshape(arr, &newdim, NPY_FORTRANORDER); |
| 915 | } |
| 916 | /* For KEEPORDER, check if we can make a flattened view */ |
| 917 | else if (order == NPY_KEEPORDER) { |
| 918 | npy_stride_sort_item strideperm[NPY_MAXDIMS]; |
| 919 | npy_intp stride; |
| 920 | int i, ndim = PyArray_NDIM(arr); |
| 921 | |
| 922 | PyArray_CreateSortedStridePerm(PyArray_NDIM(arr), |
| 923 | PyArray_STRIDES(arr), strideperm); |
| 924 | |
| 925 | /* The output array must be contiguous, so the first stride is fixed */ |
| 926 | stride = PyArray_ITEMSIZE(arr); |
| 927 | |
| 928 | for (i = ndim-1; i >= 0; --i) { |
| 929 | if (PyArray_DIM(arr, strideperm[i].perm) == 1) { |
| 930 | /* A size one dimension does not matter */ |
| 931 | continue; |
| 932 | } |
| 933 | if (strideperm[i].stride != stride) { |
| 934 | break; |
| 935 | } |
| 936 | stride *= PyArray_DIM(arr, strideperm[i].perm); |
| 937 | } |
| 938 | |
| 939 | /* If all the strides matched a contiguous layout, return a view */ |
| 940 | if (i < 0) { |
| 941 | stride = PyArray_ITEMSIZE(arr); |
| 942 | val[0] = PyArray_SIZE(arr); |
| 943 | |
| 944 | Py_INCREF(PyArray_DESCR(arr)); |
| 945 | return PyArray_NewFromDescrAndBase( |
| 946 | Py_TYPE(arr), PyArray_DESCR(arr), |
no test coverage detected