NUMPY_API * Sets the iterator position to the specified iterindex, * which matches the iteration order of the iterator. * * Returns NPY_SUCCEED on success, NPY_FAIL on failure. */
| 617 | * Returns NPY_SUCCEED on success, NPY_FAIL on failure. |
| 618 | */ |
| 619 | NPY_NO_EXPORT int |
| 620 | NpyIter_GotoIterIndex(NpyIter *iter, npy_intp iterindex) |
| 621 | { |
| 622 | npy_uint32 itflags = NIT_ITFLAGS(iter); |
| 623 | /*int ndim = NIT_NDIM(iter);*/ |
| 624 | int iop, nop = NIT_NOP(iter); |
| 625 | |
| 626 | if (itflags&NPY_ITFLAG_EXLOOP) { |
| 627 | PyErr_SetString(PyExc_ValueError, |
| 628 | "Cannot call GotoIterIndex on an iterator which " |
| 629 | "has the flag EXTERNAL_LOOP"); |
| 630 | return NPY_FAIL; |
| 631 | } |
| 632 | |
| 633 | if (iterindex < NIT_ITERSTART(iter) || iterindex >= NIT_ITEREND(iter)) { |
| 634 | if (NIT_ITERSIZE(iter) < 0) { |
| 635 | PyErr_SetString(PyExc_ValueError, "iterator is too large"); |
| 636 | return NPY_FAIL; |
| 637 | } |
| 638 | PyErr_SetString(PyExc_IndexError, |
| 639 | "Iterator GotoIterIndex called with an iterindex outside the " |
| 640 | "iteration range."); |
| 641 | return NPY_FAIL; |
| 642 | } |
| 643 | |
| 644 | if (itflags&NPY_ITFLAG_BUFFER) { |
| 645 | NpyIter_BufferData *bufferdata = NIT_BUFFERDATA(iter); |
| 646 | npy_intp bufiterend, size; |
| 647 | |
| 648 | size = NBF_SIZE(bufferdata); |
| 649 | bufiterend = NBF_BUFITEREND(bufferdata); |
| 650 | /* Check if the new iterindex is already within the buffer */ |
| 651 | if (!(itflags&NPY_ITFLAG_REDUCE) && iterindex < bufiterend && |
| 652 | iterindex >= bufiterend - size) { |
| 653 | npy_intp *strides, delta; |
| 654 | char **ptrs; |
| 655 | |
| 656 | strides = NBF_STRIDES(bufferdata); |
| 657 | ptrs = NBF_PTRS(bufferdata); |
| 658 | delta = iterindex - NIT_ITERINDEX(iter); |
| 659 | |
| 660 | for (iop = 0; iop < nop; ++iop) { |
| 661 | ptrs[iop] += delta * strides[iop]; |
| 662 | } |
| 663 | |
| 664 | NIT_ITERINDEX(iter) = iterindex; |
| 665 | } |
| 666 | /* Start the buffer at the provided iterindex */ |
| 667 | else { |
| 668 | /* Write back to the arrays */ |
| 669 | if (npyiter_copy_from_buffers(iter) < 0) { |
| 670 | return NPY_FAIL; |
| 671 | } |
| 672 | |
| 673 | npyiter_goto_iterindex(iter, iterindex); |
| 674 | |
| 675 | /* Prepare the next buffers and set iterend/size */ |
| 676 | if (npyiter_copy_to_buffers(iter, NULL) < 0) { |
no test coverage detected