Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/source/view.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ Slices can be specified in the following ways:
- :cpp:func:`xt::keep(i0, i1, i2, ...) <xt::keep>` a slice selecting non-contiguous indices to keep on the underlying expression
- :cpp:func:`xt::drop(i0, i1, i2, ...) <xt::drop>` 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 <vector>
Expand Down
4 changes: 4 additions & 0 deletions include/xtensor/views/xslice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -1506,6 +1509,7 @@ namespace xt
m_indices[i] = m_raw_indices[i] < 0 ? static_cast<size_type>(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)
Expand Down
9 changes: 9 additions & 0 deletions test/test_xview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double, 3, layout_type::row_major> a = {
Expand Down
Loading