NUMPY_API * If the iterator is tracking an index, sets the iterator * to the specified index. * * Returns NPY_SUCCEED on success, NPY_FAIL on failure. */
| 529 | * Returns NPY_SUCCEED on success, NPY_FAIL on failure. |
| 530 | */ |
| 531 | NPY_NO_EXPORT int |
| 532 | NpyIter_GotoIndex(NpyIter *iter, npy_intp flat_index) |
| 533 | { |
| 534 | npy_uint32 itflags = NIT_ITFLAGS(iter); |
| 535 | int idim, ndim = NIT_NDIM(iter); |
| 536 | int nop = NIT_NOP(iter); |
| 537 | |
| 538 | npy_intp iterindex, factor; |
| 539 | NpyIter_AxisData *axisdata; |
| 540 | npy_intp sizeof_axisdata; |
| 541 | |
| 542 | if (!(itflags&NPY_ITFLAG_HASINDEX)) { |
| 543 | PyErr_SetString(PyExc_ValueError, |
| 544 | "Cannot call GotoIndex on an iterator without " |
| 545 | "requesting a C or Fortran index in the constructor"); |
| 546 | return NPY_FAIL; |
| 547 | } |
| 548 | |
| 549 | if (itflags&NPY_ITFLAG_BUFFER) { |
| 550 | PyErr_SetString(PyExc_ValueError, |
| 551 | "Cannot call GotoIndex on an iterator which " |
| 552 | "is buffered"); |
| 553 | return NPY_FAIL; |
| 554 | } |
| 555 | |
| 556 | if (itflags&NPY_ITFLAG_EXLOOP) { |
| 557 | PyErr_SetString(PyExc_ValueError, |
| 558 | "Cannot call GotoIndex on an iterator which " |
| 559 | "has the flag EXTERNAL_LOOP"); |
| 560 | return NPY_FAIL; |
| 561 | } |
| 562 | |
| 563 | if (flat_index < 0 || flat_index >= NIT_ITERSIZE(iter)) { |
| 564 | PyErr_SetString(PyExc_IndexError, |
| 565 | "Iterator GotoIndex called with an out-of-bounds " |
| 566 | "index"); |
| 567 | return NPY_FAIL; |
| 568 | } |
| 569 | |
| 570 | axisdata = NIT_AXISDATA(iter); |
| 571 | sizeof_axisdata = NIT_AXISDATA_SIZEOF(itflags, ndim, nop); |
| 572 | |
| 573 | /* Compute the iterindex corresponding to the flat_index */ |
| 574 | iterindex = 0; |
| 575 | factor = 1; |
| 576 | for (idim = 0; idim < ndim; ++idim) { |
| 577 | npy_intp i, shape, iterstride; |
| 578 | |
| 579 | iterstride = NAD_STRIDES(axisdata)[nop]; |
| 580 | shape = NAD_SHAPE(axisdata); |
| 581 | |
| 582 | /* Extract the index from the flat_index */ |
| 583 | if (iterstride == 0) { |
| 584 | i = 0; |
| 585 | } |
| 586 | else if (iterstride < 0) { |
| 587 | i = shape - (flat_index/(-iterstride))%shape - 1; |
| 588 | } |
no test coverage detected