diff --git a/docs/source/view.rst b/docs/source/view.rst index 95b6365b8..446154e25 100644 --- a/docs/source/view.rst +++ b/docs/source/view.rst @@ -30,6 +30,12 @@ Slices can be specified in the following ways: - :cpp:func:`xt::keep(i0, i1, i2, ...) ` a slice selecting non-contiguous indices to keep on the underlying expression - :cpp:func:`xt::drop(i0, i1, i2, ...) ` a slice selecting non-contiguous indices to drop on the underlying expression +For ``xt::drop``, indices must be in increasing order after resolving negative indices +relative to the sliced axis. For an axis of size 6, ``xt::drop(0, -1)`` gives the ordered +positions 0 and 5, whereas ``xt::drop(-1, 0)`` does not satisfy this requirement. +The library does not sort or check the order. When reusing a slice with a different +axis size, its resolved indices must still satisfy this requirement. + .. code:: #include diff --git a/include/xtensor/views/xslice.hpp b/include/xtensor/views/xslice.hpp index cdae97c9f..6cd2865a4 100644 --- a/include/xtensor/views/xslice.hpp +++ b/include/xtensor/views/xslice.hpp @@ -478,6 +478,9 @@ namespace xt * xt::view(a, xt::drop(0, 2); // => {{3, 4, 5}} * @endcode * + * The indices must be in increasing order after resolving negative indices + * relative to the sliced axis. The library does not sort or check their order. + * * @param indices The container of indices to drop * @return instance of xdrop_slice */ @@ -1506,6 +1509,7 @@ namespace xt m_indices[i] = m_raw_indices[i] < 0 ? static_cast(shape) + m_raw_indices[i] : m_raw_indices[i]; } + m_inc.clear(); size_type cum = size_type(0); size_type prev_cum = cum; for (std::size_t i = 0; i < sz; ++i) diff --git a/test/test_xview.cpp b/test/test_xview.cpp index 6547b9df1..8069bb016 100644 --- a/test/test_xview.cpp +++ b/test/test_xview.cpp @@ -1259,6 +1259,15 @@ namespace xt EXPECT_EQ(a(1, 1, 1), 456); } + TEST(xview, drop_normalize_reuse) + { + auto slice = drop(-2, -1); + slice.normalize(4); + EXPECT_EQ(slice(1), 1); + slice.normalize(6); + EXPECT_EQ(slice(2), 2); + } + TEST(xview, drop_negative) { xtensor a = {