Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

making adapt(std::array) return xfixed_adaptor. #2350

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
25 changes: 24 additions & 1 deletion include/xtensor/xadapt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ namespace xt
static constexpr std::size_t value = N;
};

template <class T, std::size_t N>
struct array_size_impl<const_array<T, N>>
{
static constexpr std::size_t value = N;
};

template <class C>
using array_size = array_size_impl<std::decay_t<C>>;

Expand Down Expand Up @@ -209,7 +215,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 +225,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
8 changes: 8 additions & 0 deletions include/xtensor/xcontainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ namespace xt
{
using type = std::allocator<T>; // fake allocator for testing
};


template <class T, std::size_t N>
struct allocator_type_impl<const_array<T, N>>
{
using type = std::allocator<T>; // fake allocator for testing
};

}

template <class T>
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
6 changes: 6 additions & 0 deletions include/xtensor/xshape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,12 @@ namespace xt
{
static constexpr bool value = true;
};

template <class T, std::size_t N>
struct is_array<const_array<T, N>>
{
static constexpr bool value = true;
};

template <class S>
struct is_fixed : std::false_type
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