Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
91 changes: 91 additions & 0 deletions include/xtensor/xview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,12 @@ namespace xt
template <class E, class... S>
auto view(E&& e, S&&... slices);

template <class E>
auto row(E&& e, const int index);
Comment thread
kolibri91 marked this conversation as resolved.

template <class E>
auto col(E&& e, const int index);

/*****************************
* xview_stepper declaration *
*****************************/
Expand Down Expand Up @@ -1661,6 +1667,91 @@ namespace xt
return detail::make_view_impl(std::forward<E>(e), std::make_index_sequence<sizeof...(S)>(), std::forward<S>(slices)...);
}

namespace detail
{
class row_impl
{
public:
template<class E>
static inline auto make(E&& e, const int index)
{
check_dimension(e.shape());
return view(e, index, xt::all());
}

private:
template<class S>
static inline void check_dimension(const S& shape)
{
if (shape.size() != 2)
{
XTENSOR_THROW(std::invalid_argument, "A row can only be accessed on an expression with exact two dimensions");
}
}

template<class T, int N>
static inline void check_dimension(const std::array<T, N>&)
{
Comment thread
kolibri91 marked this conversation as resolved.
static_assert(N == 2, "A row can only be accessed on an expression with exact two dimensions");
}
};

class column_impl
{
public:
template<class E>
static inline auto make(E&& e, const int index)
{
check_dimension(e.shape());
return view(e, xt::all(), index);
}

private:
template<class S>
static inline void check_dimension(const S& shape)
{
if (shape.size() != 2)
{
XTENSOR_THROW(std::invalid_argument, "A column can only be accessed on an expression with exact two dimensions");
}
}

template<class T, int N>
static inline void check_dimension(const std::array<T, N>&)
Comment thread
kolibri91 marked this conversation as resolved.
{
static_assert(N == 2, "A column can only be accessed on an expression with exact two dimensions");
}
};
}

/**
* Constructs and returns a row (sliced view) on the specified expression.
* Users should not directly construct the slices but call helper functions
* instead. This function is only allowed on expressions with two dimensions.
* @param e the xexpression to adapt
* @param index 0-based index of the row
* @throws std::invalid_argument if the expression has more than 2 dimensions.
*/
template <class E>
inline auto row(E&& e, int index)
{
return detail::row_impl::make(e, index);
}

/**
* Constructs and returns a column (sliced view) on the specified expression.
* Users should not directly construct the slices but call helper functions
* instead. This function is only allowed on expressions with two dimensions.
* @param e the xexpression to adapt
* @param index 0-based index of the column
* @throws std::invalid_argument if the expression has more than 2 dimensions.
*/
template <class E>
inline auto col(E&& e, int index)
{
return detail::column_impl::make(e, index);
}

/***************
* stepper api *
***************/
Expand Down
132 changes: 132 additions & 0 deletions test/test_xview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1489,4 +1489,136 @@ namespace xt
EXPECT_FALSE(a(1, 0));
EXPECT_FALSE(a(1, 1));
}

TEST(xview, row_on_2dim_xarray)
{
xt::xarray<int> array{
{ 1, 2 },
{ 3, 4 },
};

const auto row0 = xt::row(array, 0);
const auto row1 = xt::row(array, 1);

EXPECT_EQ(row0(0), 1);
EXPECT_EQ(row0(1), 2);
EXPECT_EQ(row1(0), 3);
EXPECT_EQ(row1(1), 4);
}

TEST(xiew, row_on_2dim_xtensor)
{
xt::xtensor<int, 2> tensor{
{ 1, 2 },
{ 3, 4 },
};

std::cout << tensor.shape().size() << std::endl;

const auto row0 = xt::row(tensor, 0);
const auto row1 = xt::row(tensor, 1);

EXPECT_EQ(row0(0), 1);
EXPECT_EQ(row0(1), 2);
EXPECT_EQ(row1(0), 3);
EXPECT_EQ(row1(1), 4);
}

TEST(xiew, row_on_2dim_xtensor_fixed)
{
xt::xtensor_fixed<int, xshape<2, 2>> tensor_fixed{
{ 1, 2 },
{ 3, 4 },
};

const auto row0 = xt::row(tensor_fixed, 0);
const auto row1 = xt::row(tensor_fixed, 1);

EXPECT_EQ(row0(0), 1);
EXPECT_EQ(row0(1), 2);
EXPECT_EQ(row1(0), 3);
EXPECT_EQ(row1(1), 4);
}

TEST(xview, row_on_3dim_array)
{
xt::xarray<int> arr{
{ { 1, 2 }, { 3, 4 } },
{ { 5, 6 }, { 7, 8 } },
};

XT_ASSERT_THROW(
const auto row = xt::row(arr, 0),
std::invalid_argument
);
}

TEST(xview, col_on_2dim_xarray)
{
xt::xarray<int> array{
{ 1, 2 },
{ 3, 4 },
};

const auto col0 = xt::col(array, 0);
const auto col1 = xt::col(array, 1);

EXPECT_EQ(col0(0), 1);
EXPECT_EQ(col0(1), 3);
EXPECT_EQ(col1(0), 2);
EXPECT_EQ(col1(1), 4);
}

TEST(xview, col_on_2dim_xtensor)
{
xt::xtensor<int, 2> tensor{
{ 1, 2 },
{ 3, 4 },
};

const auto col0 = xt::col(tensor, 0);
const auto col1 = xt::col(tensor, 1);

EXPECT_EQ(col0(0), 1);
EXPECT_EQ(col0(1), 3);
EXPECT_EQ(col1(0), 2);
EXPECT_EQ(col1(1), 4);
}

TEST(xview, col_on_2dim_xtensor_fixed)
{
xt::xtensor_fixed<int, xshape<2, 2>> tensor_fixed{
{ 1, 2 },
{ 3, 4 },
};

const auto col0 = xt::col(tensor_fixed, 0);
const auto col1 = xt::col(tensor_fixed, 1);

EXPECT_EQ(col0(0), 1);
EXPECT_EQ(col0(1), 3);
EXPECT_EQ(col1(0), 2);
EXPECT_EQ(col1(1), 4);
}

TEST(xview, col_on_3dim_array)
{
xt::xarray<int> arr{
{ { 1, 2 }, { 3, 4 } },
{ { 5, 6 }, { 7, 8 } },
};

XT_ASSERT_THROW(
const auto col = xt::col(arr, 0),
std::invalid_argument
);
}

// This code should not compile!
//TEST(xview, col_on_3dim_xtensor)
//{
// xt::xtensor<int, 3> tensor;
// xt::row(tensor, 0);
// xt::col(tensor, 0);
//}
}