Skip to content

Commit

Permalink
making adapt(std::array) return xfixed_adaptor.
Browse files Browse the repository at this point in the history
  • Loading branch information
vakokako committed Apr 6, 2021
1 parent be8fe08 commit 299be1d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
19 changes: 18 additions & 1 deletion include/xtensor/xadapt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ namespace xt
* @param container the container to adapt
* @param l the layout_type of the xtensor_adaptor
*/
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class C>
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class C,
XTL_REQUIRES(detail::not_an_array<std::decay_t<C>>)>
inline xtensor_adaptor<C, 1, L>
adapt(C&& container, layout_type l = L)
{
Expand All @@ -218,6 +219,22 @@ namespace xt
return return_type(std::forward<C>(container), shape, l);
}

/**
* Constructs a 1-D xtensor_adaptor of the given stl-like container,
* with the specified layout_type.
* @param container the container to adapt
* @param l the layout_type of the xtensor_adaptor
*/
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class C,
XTL_REQUIRES(detail::is_array<std::decay_t<C>>)>
inline auto
adapt(C&& container)
{
// const std::array<typename std::decay_t<C>::size_type, 1> shape{container.size()};
using return_type = xfixed_adaptor<xtl::closure_type_t<C>, fixed_shape<std::tuple_size<std::decay_t<C>>::value>, L>;
return return_type(std::forward<C>(container));
}

/**
* Constructs an xtensor_adaptor of the given stl-like container,
* with the specified shape and layout_type.
Expand Down
2 changes: 1 addition & 1 deletion include/xtensor/xfixed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ namespace xt
struct xcontainer_inner_types<xfixed_adaptor<EC, S, L, SH, Tag>>
{
using storage_type = std::remove_reference_t<EC>;
using reference = typename storage_type::reference;
using reference = inner_reference_t<storage_type>;
using const_reference = typename storage_type::const_reference;
using size_type = typename storage_type::size_type;
using shape_type = S;
Expand Down
17 changes: 17 additions & 0 deletions test/test_xadapt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,23 @@ namespace xt
}
}

TEST(xtensor_fixed_adaptor, array)
{
std::array<int, 8> a({1,2,3,4,5,6,7,8});
auto xa = adapt(a);
xa(0) = 100;
xa(3) = 1000;
EXPECT_EQ(a[0], 100);
EXPECT_EQ(a[3], 1000);
bool truthy = std::is_same<decltype(xa)::shape_type, xshape<8>>::value;
EXPECT_TRUE(truthy);

const std::array<int, 4> b({5,5,19,5});
auto xb = adapt(b);
EXPECT_EQ(xb(2), 19);
EXPECT_EQ(xb(0), 5);
}

namespace xadapt_test
{
struct Buffer {
Expand Down

0 comments on commit 299be1d

Please sign in to comment.