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

Function get_view_from_index

numpy/core/src/multiarray/mapping.c:822–904  ·  view source on GitHub ↗

* Get view into an array using all non-array indices. * * For any index, get a view of the subspace into the original * array. If there are no fancy indices, this is the result of * the indexing operation. * Ensure_array allows to fetch a safe subspace view for advanced * indexing. * * @param Array being indexed * @param resulting array (new reference) * @param parsed index information

Source from the content-addressed store, hash-verified

820 * @return 0 on success -1 on failure
821 */
822static int
823get_view_from_index(PyArrayObject *self, PyArrayObject **view,
824 npy_index_info *indices, int index_num, int ensure_array) {
825 npy_intp new_strides[NPY_MAXDIMS];
826 npy_intp new_shape[NPY_MAXDIMS];
827 int i, j;
828 int new_dim = 0;
829 int orig_dim = 0;
830 char *data_ptr = PyArray_BYTES(self);
831
832 /* for slice parsing */
833 npy_intp start, stop, step, n_steps;
834
835 for (i=0; i < index_num; i++) {
836 switch (indices[i].type) {
837 case HAS_INTEGER:
838 if ((check_and_adjust_index(&indices[i].value,
839 PyArray_DIMS(self)[orig_dim], orig_dim,
840 NULL)) < 0) {
841 return -1;
842 }
843 data_ptr += PyArray_STRIDE(self, orig_dim) * indices[i].value;
844
845 new_dim += 0;
846 orig_dim += 1;
847 break;
848 case HAS_ELLIPSIS:
849 for (j=0; j < indices[i].value; j++) {
850 new_strides[new_dim] = PyArray_STRIDE(self, orig_dim);
851 new_shape[new_dim] = PyArray_DIMS(self)[orig_dim];
852 new_dim += 1;
853 orig_dim += 1;
854 }
855 break;
856 case HAS_SLICE:
857 if (PySlice_GetIndicesEx(indices[i].object,
858 PyArray_DIMS(self)[orig_dim],
859 &start, &stop, &step, &n_steps) < 0) {
860 return -1;
861 }
862 if (n_steps <= 0) {
863 /* TODO: Always points to start then, could change that */
864 n_steps = 0;
865 step = 1;
866 start = 0;
867 }
868
869 data_ptr += PyArray_STRIDE(self, orig_dim) * start;
870 new_strides[new_dim] = PyArray_STRIDE(self, orig_dim) * step;
871 new_shape[new_dim] = n_steps;
872 new_dim += 1;
873 orig_dim += 1;
874 break;
875 case HAS_NEWAXIS:
876 new_strides[new_dim] = 0;
877 new_shape[new_dim] = 1;
878 new_dim += 1;
879 break;

Callers 5

array_item_asarrayFunction · 0.85
array_subscriptFunction · 0.85
array_assign_itemFunction · 0.85
array_assign_subscriptFunction · 0.85

Calls 7

PyArray_BYTESFunction · 0.85
check_and_adjust_indexFunction · 0.85
PyArray_DIMSFunction · 0.85
PyArray_STRIDEFunction · 0.85
PyArray_DESCRFunction · 0.85
PyArray_NewFromDescr_intFunction · 0.85
PyArray_FLAGSFunction · 0.85

Tested by

no test coverage detected