Skip to content

Commit 299be1d

Browse files
committed
making adapt(std::array) return xfixed_adaptor.
1 parent be8fe08 commit 299be1d

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

include/xtensor/xadapt.hpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ namespace xt
209209
* @param container the container to adapt
210210
* @param l the layout_type of the xtensor_adaptor
211211
*/
212-
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class C>
212+
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class C,
213+
XTL_REQUIRES(detail::not_an_array<std::decay_t<C>>)>
213214
inline xtensor_adaptor<C, 1, L>
214215
adapt(C&& container, layout_type l = L)
215216
{
@@ -218,6 +219,22 @@ namespace xt
218219
return return_type(std::forward<C>(container), shape, l);
219220
}
220221

222+
/**
223+
* Constructs a 1-D xtensor_adaptor of the given stl-like container,
224+
* with the specified layout_type.
225+
* @param container the container to adapt
226+
* @param l the layout_type of the xtensor_adaptor
227+
*/
228+
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class C,
229+
XTL_REQUIRES(detail::is_array<std::decay_t<C>>)>
230+
inline auto
231+
adapt(C&& container)
232+
{
233+
// const std::array<typename std::decay_t<C>::size_type, 1> shape{container.size()};
234+
using return_type = xfixed_adaptor<xtl::closure_type_t<C>, fixed_shape<std::tuple_size<std::decay_t<C>>::value>, L>;
235+
return return_type(std::forward<C>(container));
236+
}
237+
221238
/**
222239
* Constructs an xtensor_adaptor of the given stl-like container,
223240
* with the specified shape and layout_type.

include/xtensor/xfixed.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ namespace xt
400400
struct xcontainer_inner_types<xfixed_adaptor<EC, S, L, SH, Tag>>
401401
{
402402
using storage_type = std::remove_reference_t<EC>;
403-
using reference = typename storage_type::reference;
403+
using reference = inner_reference_t<storage_type>;
404404
using const_reference = typename storage_type::const_reference;
405405
using size_type = typename storage_type::size_type;
406406
using shape_type = S;

test/test_xadapt.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,23 @@ namespace xt
418418
}
419419
}
420420

421+
TEST(xtensor_fixed_adaptor, array)
422+
{
423+
std::array<int, 8> a({1,2,3,4,5,6,7,8});
424+
auto xa = adapt(a);
425+
xa(0) = 100;
426+
xa(3) = 1000;
427+
EXPECT_EQ(a[0], 100);
428+
EXPECT_EQ(a[3], 1000);
429+
bool truthy = std::is_same<decltype(xa)::shape_type, xshape<8>>::value;
430+
EXPECT_TRUE(truthy);
431+
432+
const std::array<int, 4> b({5,5,19,5});
433+
auto xb = adapt(b);
434+
EXPECT_EQ(xb(2), 19);
435+
EXPECT_EQ(xb(0), 5);
436+
}
437+
421438
namespace xadapt_test
422439
{
423440
struct Buffer {

0 commit comments

Comments
 (0)