Repository: opencv/opencv
File: modules/dnn/src/layers/nonzero_layer.cpp (C++)

Code before:
    if (rank == 0) {
        return;
    }
    const int lastDimSize = dims[rank - 1];

    cv::parallel_for_(cv::Range(0, nstripes), [&](const cv::Range& range) {
        size_t i = total * (size_t)range.start / (size_t)nstripes;
        const size_t i1 = total * (size_t)range.end   / (size_t)nstripes;
        size_t col = nzstart[(size_t)range.start];

        std::vector<int> coord(rank, 0);
        if (i < i1)
        {
            size_t r = i;
            for (int d = 0; d < rank; ++d)
            {
                const int sd = strides[d];
                const int idx = static_cast<int>(r / (size_t)sd);
                coord[d] = idx;
                r -= (size_t)idx * (size_t)sd;
            }
        }

        while (i < i1)
        {
            const int innerStart = coord[rank - 1];
            const size_t innerAvail = (size_t)(lastDimSize - innerStart);
            const size_t chunk = std::min(innerAvail, i1 - i);

            for (size_t dj = 0; dj < chunk; ++dj)
            {
                const size_t idxLinear = i + dj;
                if (isNonZero(data[idxLinear]))
                {
                    for (int d = 0; d < rank - 1; ++d)
                        y[(size_t)d * nnz_total + col] = (int64)coord[d];
                    y[(size_t)(rank - 1) * nnz_total + col] = (int64)(innerStart + (int)dj);
                    ++col;
                }
            }

            i += chunk;
            if (i >= i1) break;

            coord[rank - 1] = 0;
            if (rank > 1)
            {
                int d = rank - 2;
                for (; d >= 0; --d)
                {
                    coord[d] += 1;

Review comment (suggestion):
do something like this:

```
coord[rank-1] = lastDimSize - 1;
for (;i < i1; i++) {     
     if (++coord[rank-1] >= lastDimSize) {
          // recompute all coordinates (coord) from 'i'
          int idxLinear = i;
          for (int d = rank-1; d >= 0; d--) {
                coord[d] = idxLinear % shape[d];
                idxLinear /= shape[d];
          }
     }
     if (isNonZero(data[i])) {
          for (int d = 0; d < rank; d++) {
                y[(size_t)d * nnz_total + col] = (int64_t)coord[d]; // should it be y[col*nnz_total + d]?
          }
          col++;
     }
}
```

Code after:
    if (rank == 0) {
        return;
    }
    const int lastDimSize = dims[rank - 1];

    cv::parallel_for_(cv::Range(0, nstripes), [&](const cv::Range& range) {
        size_t i = total * (size_t)range.start / (size_t)nstripes;
        const size_t i1 = total * (size_t)range.end / (size_t)nstripes;
        size_t col = nzstart[(size_t)range.start];

        std::array<int, CV_MAX_DIM> coord;
        std::fill_n(coord.begin(), rank, 0);
        if (rank > 0) coord[rank - 1] = lastDimSize - 1;

        for (; i < i1; ++i)
        {
            if (rank > 0 && ++coord[rank - 1] >= lastDimSize)
            {
                size_t idxLinear = i;
                for (int d = rank - 1; d >= 0; --d)
                {
                    coord[d] = static_cast<int>(idxLinear % (size_t)dims[d]);
                    idxLinear /= (size_t)dims[d];
                }
            }
            if (isNonZero(data[i]))
            {
                for (int d = 0; d < rank; ++d)
                    y[(size_t)d * nnz_total + col] = (int64)coord[d];
                ++col;
            }
        }
    });
}

template <typename T>
static void emitIndicesStripes(const T* data,
                               size_t total,
                               int nstripes,
                               int rank,
                               const std::vector<int>& dims,
                               const std::vector<int>& strides,
                               const std::vector<size_t>& nzstart,
                               int64* y)
{
    emitIndicesStripes<T>(data, total, nstripes, rank, dims, strides, nzstart, y, [](T v){ return v != (T)0; });
}
}

class NonZeroLayerImpl CV_FINAL : public NonZeroLayer
{