Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the return type should be xtensor_adaptor<xtl::closure_type_t<C>, 1, L>, according to the implementation below.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, but I didn't really want to change existing implementation, as I'm not too deep into it)

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>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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