Skip to content

Commit

Permalink
Adding xt::missing functionality to .periodic(...), .at(...), a…
Browse files Browse the repository at this point in the history
…nd `.in_bounds(...)`
  • Loading branch information
tdegeus committed Mar 15, 2022
1 parent 0f6f58a commit 23fb5ee
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
6 changes: 3 additions & 3 deletions include/xtensor/xaccessible.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ namespace xt
template <class... Args>
inline auto xconst_accessible<D>::at(Args... args) const -> const_reference
{
check_access(derived_cast().shape(), static_cast<size_type>(args)...);
check_access(derived_cast().shape(), args...);
return derived_cast().operator()(args...);
}

Expand Down Expand Up @@ -273,7 +273,7 @@ namespace xt
template <class... Args>
inline auto xaccessible<D>::at(Args... args) -> reference
{
check_access(derived_cast().shape(), static_cast<size_type>(args)...);
check_access(derived_cast().shape(), args...);
return derived_cast().operator()(args...);
}

Expand Down Expand Up @@ -316,7 +316,7 @@ namespace xt
inline auto xaccessible<D>::periodic(Args... args) -> reference
{
normalize_periodic(derived_cast().shape(), args...);
return derived_cast()(static_cast<size_type>(args)...);
return derived_cast()(args...);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions include/xtensor/xstrides.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,12 @@ namespace xt
return true;
}

template <class S, std::size_t dim>
inline bool check_in_bounds_impl(const S&, missing_type)
{
return true;
}

template <class S, std::size_t dim, class T, class... Args>
inline bool check_in_bounds_impl(const S& shape, T& arg, Args&... args)
{
Expand Down Expand Up @@ -669,6 +675,11 @@ namespace xt
{
}

template <class S, std::size_t dim>
inline void normalize_periodic_impl(const S&, missing_type)
{
}

template <class S, std::size_t dim, class T, class... Args>
inline void normalize_periodic_impl(const S& shape, T& arg, Args&... args)
{
Expand Down
15 changes: 14 additions & 1 deletion test/test_xtensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ namespace xt
}
}

TEST(xtensor, missign)
TEST(xtensor, missing)
{
xt::xtensor<int, 2> a
{{0, 1, 2},
Expand Down Expand Up @@ -202,6 +202,19 @@ namespace xt
EXPECT_EQ(a(9, 9, 9, 2, 0), 6);
EXPECT_EQ(a(9, 9, 9, 2, 1), 7);
EXPECT_EQ(a(9, 9, 9, 2, 2), 8);

EXPECT_EQ(a.periodic(-1, xt::missing), a(2, xt::missing));
EXPECT_EQ(a.periodic(-2, xt::missing), a(1, xt::missing));
EXPECT_EQ(a.periodic(-3, xt::missing), a(0, xt::missing));

EXPECT_EQ(a.at(0, xt::missing), a.at(0, 0));
EXPECT_EQ(a.at(1, xt::missing), a.at(1, 0));
EXPECT_EQ(a.at(2, xt::missing), a.at(2, 0));

EXPECT_TRUE(a.in_bounds(0, xt::missing));
EXPECT_TRUE(a.in_bounds(1, xt::missing));
EXPECT_TRUE(a.in_bounds(2, xt::missing));
EXPECT_FALSE(a.in_bounds(3, xt::missing));
}

TEST(xtensor, resize)
Expand Down

0 comments on commit 23fb5ee

Please sign in to comment.