Skip to content
Open
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
3 changes: 3 additions & 0 deletions include/xtensor/views/xslice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#ifndef XTENSOR_SLICE_HPP
#define XTENSOR_SLICE_HPP

#include <algorithm>
#include <cstddef>
#include <map>
#include <type_traits>
Expand Down Expand Up @@ -1506,6 +1507,8 @@ namespace xt
m_indices[i] = m_raw_indices[i] < 0 ? static_cast<size_type>(shape) + m_raw_indices[i]
: m_raw_indices[i];
}
std::sort(m_indices.begin(), m_indices.end());
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
49 changes: 49 additions & 0 deletions test/test_xview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,55 @@ namespace xt
EXPECT_EQ(a(1, 1, 1), 456);
}

TEST(xview, drop_unsorted)
{
xtensor<int, 1> a = {0, 1, 2, 3, 4, 5};
xtensor<int, 1> expected = {1, 3, 4, 5};
auto v = xt::view(a, drop(2, 0));
EXPECT_EQ(v, expected);
test_view_iter(v, expected);

std::vector<std::ptrdiff_t> indices = {2, 0};
xtensor<int, 1> result = xt::view(a, drop(indices));
EXPECT_EQ(result, expected);

xtensor<int, 1> kept = xt::view(a, keep(2, 0));
xtensor<int, 1> expected_kept = {2, 0};
EXPECT_EQ(kept, expected_kept);

v = -1;
xtensor<int, 1> expected_a = {0, -1, 2, -1, -1, -1};
EXPECT_EQ(a, expected_a);
}

TEST(xview, drop_unsorted_negative)
{
xtensor<int, 2> a = {{0, 1, 2, 3, 4, 5}, {6, 7, 8, 9, 10, 11}};
xtensor<int, 2> expected = {{1, 2, 3, 4}, {7, 8, 9, 10}};
auto v = xt::view(a, all(), drop(-1, 0));
EXPECT_EQ(v, expected);
test_view_iter(v, expected);

xtensor<int, 2> result = v;
EXPECT_EQ(result, expected);
}

TEST(xview, drop_normalize_reuse)
{
auto slice = drop(-2, -1);
slice.normalize(4);
EXPECT_EQ(slice.size(), 2);
EXPECT_EQ(slice(0), 0);
EXPECT_EQ(slice(1), 1);

slice.normalize(6);
EXPECT_EQ(slice.size(), 4);
for (std::ptrdiff_t i = 0; i < slice.size(); ++i)
{
EXPECT_EQ(slice(i), i);
}
}

TEST(xview, drop_negative)
{
xtensor<double, 3, layout_type::row_major> a = {
Expand Down