Skip to content

Commit 19e11a2

Browse files
committed
Require ordered drop indices and avoid internal sorting
Document ordering after negative-index resolution and retain the repeated-normalization offset-map fix with its regression. Generated with OpenAI Codex
1 parent 29d00c9 commit 19e11a2

3 files changed

Lines changed: 10 additions & 43 deletions

File tree

‎docs/source/view.rst‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ Slices can be specified in the following ways:
3030
- :cpp:func:`xt::keep(i0, i1, i2, ...) <xt::keep>` a slice selecting non-contiguous indices to keep on the underlying expression
3131
- :cpp:func:`xt::drop(i0, i1, i2, ...) <xt::drop>` a slice selecting non-contiguous indices to drop on the underlying expression
3232

33+
For ``xt::drop``, indices must be in increasing order after resolving negative indices
34+
relative to the sliced axis. For an axis of size 6, ``xt::drop(0, -1)`` gives the ordered
35+
positions 0 and 5, whereas ``xt::drop(-1, 0)`` does not satisfy this requirement.
36+
The library does not sort or check the order. When reusing a slice with a different
37+
axis size, its resolved indices must still satisfy this requirement.
38+
3339
.. code::
3440
3541
#include <vector>

‎include/xtensor/views/xslice.hpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#ifndef XTENSOR_SLICE_HPP
1111
#define XTENSOR_SLICE_HPP
1212

13-
#include <algorithm>
1413
#include <cstddef>
1514
#include <map>
1615
#include <type_traits>
@@ -479,6 +478,9 @@ namespace xt
479478
* xt::view(a, xt::drop(0, 2); // => {{3, 4, 5}}
480479
* @endcode
481480
*
481+
* The indices must be in increasing order after resolving negative indices
482+
* relative to the sliced axis. The library does not sort or check their order.
483+
*
482484
* @param indices The container of indices to drop
483485
* @return instance of xdrop_slice
484486
*/
@@ -1507,7 +1509,6 @@ namespace xt
15071509
m_indices[i] = m_raw_indices[i] < 0 ? static_cast<size_type>(shape) + m_raw_indices[i]
15081510
: m_raw_indices[i];
15091511
}
1510-
std::sort(m_indices.begin(), m_indices.end());
15111512
m_inc.clear();
15121513
size_type cum = size_type(0);
15131514
size_type prev_cum = cum;

‎test/test_xview.cpp‎

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,53 +1259,13 @@ namespace xt
12591259
EXPECT_EQ(a(1, 1, 1), 456);
12601260
}
12611261

1262-
TEST(xview, drop_unsorted)
1263-
{
1264-
xtensor<int, 1> a = {0, 1, 2, 3, 4, 5};
1265-
xtensor<int, 1> expected = {1, 3, 4, 5};
1266-
auto v = xt::view(a, drop(2, 0));
1267-
EXPECT_EQ(v, expected);
1268-
test_view_iter(v, expected);
1269-
1270-
std::vector<std::ptrdiff_t> indices = {2, 0};
1271-
xtensor<int, 1> result = xt::view(a, drop(indices));
1272-
EXPECT_EQ(result, expected);
1273-
1274-
xtensor<int, 1> kept = xt::view(a, keep(2, 0));
1275-
xtensor<int, 1> expected_kept = {2, 0};
1276-
EXPECT_EQ(kept, expected_kept);
1277-
1278-
v = -1;
1279-
xtensor<int, 1> expected_a = {0, -1, 2, -1, -1, -1};
1280-
EXPECT_EQ(a, expected_a);
1281-
}
1282-
1283-
TEST(xview, drop_unsorted_negative)
1284-
{
1285-
xtensor<int, 2> a = {{0, 1, 2, 3, 4, 5}, {6, 7, 8, 9, 10, 11}};
1286-
xtensor<int, 2> expected = {{1, 2, 3, 4}, {7, 8, 9, 10}};
1287-
auto v = xt::view(a, all(), drop(-1, 0));
1288-
EXPECT_EQ(v, expected);
1289-
test_view_iter(v, expected);
1290-
1291-
xtensor<int, 2> result = v;
1292-
EXPECT_EQ(result, expected);
1293-
}
1294-
12951262
TEST(xview, drop_normalize_reuse)
12961263
{
12971264
auto slice = drop(-2, -1);
12981265
slice.normalize(4);
1299-
EXPECT_EQ(slice.size(), 2);
1300-
EXPECT_EQ(slice(0), 0);
13011266
EXPECT_EQ(slice(1), 1);
1302-
13031267
slice.normalize(6);
1304-
EXPECT_EQ(slice.size(), 4);
1305-
for (std::ptrdiff_t i = 0; i < slice.size(); ++i)
1306-
{
1307-
EXPECT_EQ(slice(i), i);
1308-
}
1268+
EXPECT_EQ(slice(2), 2);
13091269
}
13101270

13111271
TEST(xview, drop_negative)

0 commit comments

Comments
 (0)