Skip to content

Commit 584235b

Browse files
author
Mykola Vankovych
committed
adding xt::detail::has_fixed_size, replacing is_array in xt::adapt.
1 parent d1499d9 commit 584235b

File tree

4 files changed

+71
-32
lines changed

4 files changed

+71
-32
lines changed

include/xtensor/xadapt.hpp

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ namespace xt
4848
using default_allocator_for_ptr_t = typename default_allocator_for_ptr<P>::type;
4949

5050
template <class T>
51-
using not_an_array = xtl::negation<is_array<T>>;
51+
using has_dynamic_size = xtl::negation<has_fixed_size<T>>;
5252

5353
template <class T>
5454
using not_a_pointer = xtl::negation<std::is_pointer<T>>;
@@ -71,8 +71,8 @@ namespace xt
7171
* @param l the layout_type of the xarray_adaptor
7272
*/
7373
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class C, class SC,
74-
XTL_REQUIRES(detail::not_an_array<std::decay_t<SC>>,
75-
detail::not_a_pointer<C>)>
74+
XTL_REQUIRES(detail::has_dynamic_size<std::decay_t<SC>>,
75+
detail::not_a_pointer<std::remove_reference_t<C>>)>
7676
inline xarray_adaptor<xtl::closure_type_t<C>, L, std::decay_t<SC>>
7777
adapt(C&& container, const SC& shape, layout_type l = L)
7878
{
@@ -88,8 +88,8 @@ namespace xt
8888
* @param l the layout_type of the xarray_adaptor
8989
*/
9090
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class C, class SC,
91-
XTL_REQUIRES(detail::not_an_array<std::decay_t<SC>>,
92-
std::is_pointer<C>)>
91+
XTL_REQUIRES(detail::has_dynamic_size<std::decay_t<SC>>,
92+
std::is_pointer<std::remove_reference_t<C>>)>
9393
inline auto adapt(C&& pointer, const SC& shape, layout_type l = L)
9494
{
9595
static_assert(!xtl::is_integral<SC>::value, "shape cannot be a integer");
@@ -107,7 +107,7 @@ namespace xt
107107
* @param strides the strides of the xarray_adaptor
108108
*/
109109
template <class C, class SC, class SS,
110-
XTL_REQUIRES(detail::not_an_array<std::decay_t<SC>>,
110+
XTL_REQUIRES(detail::has_dynamic_size<std::decay_t<SC>>,
111111
detail::not_a_layout<std::decay_t<SS>>)>
112112
inline xarray_adaptor<xtl::closure_type_t<C>, layout_type::dynamic, std::decay_t<SC>>
113113
adapt(C&& container, SC&& shape, SS&& strides)
@@ -131,7 +131,7 @@ namespace xt
131131
* @param alloc the allocator used for allocating / deallocating the dynamic array
132132
*/
133133
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class P, class O, class SC, class A = detail::default_allocator_for_ptr_t<P>,
134-
XTL_REQUIRES(detail::not_an_array<std::decay_t<SC>>)>
134+
XTL_REQUIRES(detail::has_dynamic_size<std::decay_t<SC>>)>
135135
inline xarray_adaptor<xbuffer_adaptor<xtl::closure_type_t<P>, O, A>, L, SC>
136136
adapt(P&& pointer, typename A::size_type size, O ownership, const SC& shape, layout_type l = L, const A& alloc = A())
137137
{
@@ -155,7 +155,7 @@ namespace xt
155155
* @param alloc the allocator used for allocating / deallocating the dynamic array
156156
*/
157157
template <class P, class O, class SC, class SS, class A = detail::default_allocator_for_ptr_t<P>,
158-
XTL_REQUIRES(detail::not_an_array<std::decay_t<SC>>,
158+
XTL_REQUIRES(detail::has_dynamic_size<std::decay_t<SC>>,
159159
detail::not_a_layout<std::decay_t<SS>>)>
160160
inline xarray_adaptor<xbuffer_adaptor<xtl::closure_type_t<P>, O, A>, layout_type::dynamic, std::decay_t<SC>>
161161
adapt(P&& pointer, typename A::size_type size, O ownership, SC&& shape, SS&& strides, const A& alloc = A())
@@ -178,7 +178,7 @@ namespace xt
178178
* @param l the layout_type of the xarray_adaptor
179179
*/
180180
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class T, std::size_t N, class SC,
181-
XTL_REQUIRES(detail::not_an_array<std::decay_t<SC>>)>
181+
XTL_REQUIRES(detail::has_dynamic_size<std::decay_t<SC>>)>
182182
inline auto adapt(T (&c_array)[N], const SC& shape, layout_type l = L)
183183
{
184184
return adapt(&c_array[0], N, xt::no_ownership(), shape, l);
@@ -192,7 +192,7 @@ namespace xt
192192
* @param strides the strides of the xarray_adaptor
193193
*/
194194
template <class T, std::size_t N, class SC, class SS,
195-
XTL_REQUIRES(detail::not_an_array<std::decay_t<SC>>,
195+
XTL_REQUIRES(detail::has_dynamic_size<std::decay_t<SC>>,
196196
detail::not_a_layout<std::decay_t<SS>>)>
197197
inline auto adapt(T (&c_array)[N], SC&& shape, SS&& strides)
198198
{
@@ -228,13 +228,13 @@ namespace xt
228228
* @param l the layout_type of the xtensor_adaptor
229229
*/
230230
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class C, class SC,
231-
XTL_REQUIRES(detail::is_array<std::decay_t<SC>>,
232-
detail::not_a_pointer<C>)>
233-
inline xtensor_adaptor<C, detail::array_size<SC>::value, L>
231+
XTL_REQUIRES(detail::has_fixed_size<std::decay_t<SC>>,
232+
detail::not_a_pointer<std::remove_reference_t<C>>)>
233+
inline xtensor_adaptor<C, std::tuple_size<std::decay_t<SC>>::value, L>
234234
adapt(C&& container, const SC& shape, layout_type l = L)
235235
{
236236
static_assert(!xtl::is_integral<SC>::value, "shape cannot be a integer");
237-
constexpr std::size_t N = detail::array_size<SC>::value;
237+
constexpr std::size_t N = std::tuple_size<std::decay_t<SC>>::value;
238238
using return_type = xtensor_adaptor<xtl::closure_type_t<C>, N, L>;
239239
return return_type(std::forward<C>(container), shape, l);
240240
}
@@ -246,13 +246,13 @@ namespace xt
246246
* @param l the layout_type of the xtensor_adaptor
247247
*/
248248
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class C, class SC,
249-
XTL_REQUIRES(detail::is_array<std::decay_t<SC>>,
250-
std::is_pointer<C>)>
249+
XTL_REQUIRES(detail::has_fixed_size<std::decay_t<SC>>,
250+
std::is_pointer<std::remove_reference_t<C>>)>
251251
inline auto adapt(C&& pointer, const SC& shape, layout_type l = L)
252252
{
253253
static_assert(!xtl::is_integral<SC>::value, "shape cannot be a integer");
254254
using buffer_type = xbuffer_adaptor<C, xt::no_ownership, detail::default_allocator_for_ptr_t<C>>;
255-
constexpr std::size_t N = detail::array_size<SC>::value;
255+
constexpr std::size_t N = std::tuple_size<std::decay_t<SC>>::value;
256256
using return_type = xtensor_adaptor<buffer_type, N, L>;
257257
return return_type(buffer_type(pointer, compute_size(shape)), shape, l);
258258
}
@@ -265,13 +265,13 @@ namespace xt
265265
* @param strides the strides of the xtensor_adaptor
266266
*/
267267
template <class C, class SC, class SS,
268-
XTL_REQUIRES(detail::is_array<std::decay_t<SC>>,
268+
XTL_REQUIRES(detail::has_fixed_size<std::decay_t<SC>>,
269269
detail::not_a_layout<std::decay_t<SS>>)>
270-
inline xtensor_adaptor<C, detail::array_size<SC>::value, layout_type::dynamic>
270+
inline xtensor_adaptor<C, std::tuple_size<std::decay_t<SC>>::value, layout_type::dynamic>
271271
adapt(C&& container, SC&& shape, SS&& strides)
272272
{
273273
static_assert(!xtl::is_integral<std::decay_t<SC>>::value, "shape cannot be a integer");
274-
constexpr std::size_t N = detail::array_size<SC>::value;
274+
constexpr std::size_t N = std::tuple_size<std::decay_t<SC>>::value;
275275
using return_type = xtensor_adaptor<xtl::closure_type_t<C>, N, layout_type::dynamic>;
276276
return return_type(std::forward<C>(container),
277277
xtl::forward_sequence<typename return_type::inner_shape_type, SC>(shape),
@@ -312,14 +312,14 @@ namespace xt
312312
* @param alloc the allocator used for allocating / deallocating the dynamic array
313313
*/
314314
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class P, class O, class SC, class A = detail::default_allocator_for_ptr_t<P>,
315-
XTL_REQUIRES(detail::is_array<std::decay_t<SC>>)>
316-
inline xtensor_adaptor<xbuffer_adaptor<xtl::closure_type_t<P>, O, A>, detail::array_size<SC>::value, L>
315+
XTL_REQUIRES(detail::has_fixed_size<std::decay_t<SC>>)>
316+
inline xtensor_adaptor<xbuffer_adaptor<xtl::closure_type_t<P>, O, A>, std::tuple_size<std::decay_t<SC>>::value, L>
317317
adapt(P&& pointer, typename A::size_type size, O ownership, const SC& shape, layout_type l = L, const A& alloc = A())
318318
{
319319
static_assert(!xtl::is_integral<SC>::value, "shape cannot be a integer");
320320
(void)ownership;
321321
using buffer_type = xbuffer_adaptor<xtl::closure_type_t<P>, O, A>;
322-
constexpr std::size_t N = detail::array_size<SC>::value;
322+
constexpr std::size_t N = std::tuple_size<std::decay_t<SC>>::value;
323323
using return_type = xtensor_adaptor<buffer_type, N, L>;
324324
buffer_type buf(std::forward<P>(pointer), size, alloc);
325325
return return_type(std::move(buf), shape, l);
@@ -337,15 +337,15 @@ namespace xt
337337
* @param alloc the allocator used for allocating / deallocating the dynamic array
338338
*/
339339
template <class P, class O, class SC, class SS, class A = detail::default_allocator_for_ptr_t<P>,
340-
XTL_REQUIRES(detail::is_array<std::decay_t<SC>>,
340+
XTL_REQUIRES(detail::has_fixed_size<std::decay_t<SC>>,
341341
detail::not_a_layout<std::decay_t<SS>>)>
342-
inline xtensor_adaptor<xbuffer_adaptor<xtl::closure_type_t<P>, O, A>, detail::array_size<SC>::value, layout_type::dynamic>
342+
inline xtensor_adaptor<xbuffer_adaptor<xtl::closure_type_t<P>, O, A>, std::tuple_size<std::decay_t<SC>>::value, layout_type::dynamic>
343343
adapt(P&& pointer, typename A::size_type size, O ownership, SC&& shape, SS&& strides, const A& alloc = A())
344344
{
345345
static_assert(!xtl::is_integral<std::decay_t<SC>>::value, "shape cannot be a integer");
346346
(void)ownership;
347347
using buffer_type = xbuffer_adaptor<xtl::closure_type_t<P>, O, A>;
348-
constexpr std::size_t N = detail::array_size<SC>::value;
348+
constexpr std::size_t N = std::tuple_size<std::decay_t<SC>>::value;
349349
using return_type = xtensor_adaptor<buffer_type, N, layout_type::dynamic>;
350350
buffer_type buf(std::forward<P>(pointer), size, alloc);
351351
return return_type(std::move(buf),
@@ -361,7 +361,7 @@ namespace xt
361361
* @param l the layout_type of the xarray_adaptor
362362
*/
363363
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class T, std::size_t N, class SC,
364-
XTL_REQUIRES(detail::is_array<std::decay_t<SC>>)>
364+
XTL_REQUIRES(detail::has_fixed_size<std::decay_t<SC>>)>
365365
inline auto adapt(T (&c_array)[N], const SC& shape, layout_type l = L)
366366
{
367367
return adapt(&c_array[0], N, xt::no_ownership(), shape, l);
@@ -375,7 +375,7 @@ namespace xt
375375
* @param strides the strides of the xarray_adaptor
376376
*/
377377
template <class T, std::size_t N, class SC, class SS,
378-
XTL_REQUIRES(detail::is_array<std::decay_t<SC>>,
378+
XTL_REQUIRES(detail::has_fixed_size<std::decay_t<SC>>,
379379
detail::not_a_layout<std::decay_t<SS>>)>
380380
inline auto adapt(T (&c_array)[N], SC&& shape, SS&& strides)
381381
{
@@ -557,7 +557,7 @@ namespace xt
557557
* @return xarray_adaptor for memory
558558
*/
559559
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class P, class SC,
560-
XTL_REQUIRES(detail::not_an_array<std::decay_t<SC>>)>
560+
XTL_REQUIRES(detail::has_dynamic_size<std::decay_t<SC>>)>
561561
auto adapt_smart_ptr(P&& smart_ptr, const SC& shape, layout_type l = L)
562562
{
563563
using buffer_adaptor = xbuffer_adaptor<decltype(smart_ptr.get()), smart_ownership,
@@ -619,7 +619,7 @@ namespace xt
619619
* @return xarray_adaptor on the memory
620620
*/
621621
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class P, class SC, class D,
622-
XTL_REQUIRES(detail::not_an_array<std::decay_t<SC>>,
622+
XTL_REQUIRES(detail::has_dynamic_size<std::decay_t<SC>>,
623623
detail::not_a_layout<std::decay_t<D>>)>
624624
auto adapt_smart_ptr(P&& data_ptr, const SC& shape, D&& smart_ptr, layout_type l = L)
625625
{

include/xtensor/xeval.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ namespace xt
8080
template <class E>
8181
constexpr bool has_fixed_dims()
8282
{
83-
return detail::is_array<typename std::decay_t<E>::shape_type>::value;
83+
return detail::has_fixed_size<typename std::decay_t<E>::shape_type>::value;
8484
}
8585

8686
template <class E>

include/xtensor/xfixed.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,4 +950,32 @@ namespace xt
950950
}
951951
}
952952

953+
/******************************
954+
* std::tuple_size extensions *
955+
******************************/
956+
957+
#if defined(__clang__)
958+
# pragma clang diagnostic push
959+
# pragma clang diagnostic ignored "-Wmismatched-tags"
960+
#endif
961+
962+
namespace std
963+
{
964+
template <class ET, class S, xt::layout_type L, bool SH, class Tag>
965+
class tuple_size<xt::xfixed_container<ET, S, L, SH, Tag>> :
966+
public integral_constant<std::size_t, xt::detail::fixed_compute_size<S>::value>
967+
{
968+
};
969+
970+
template <class ET, class S, xt::layout_type L, bool SH, class Tag>
971+
class tuple_size<xt::xfixed_adaptor<ET, S, L, SH, Tag>> :
972+
public integral_constant<std::size_t, xt::detail::fixed_compute_size<S>::value>
973+
{
974+
};
975+
}
976+
977+
#if defined(__clang__)
978+
# pragma clang diagnostic pop
979+
#endif
980+
953981
#endif

include/xtensor/xshape.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,19 @@ namespace xt
375375
static constexpr bool value = true;
376376
};
377377

378+
template <class E, class = void>
379+
struct has_fixed_size : std::false_type
380+
{
381+
};
382+
383+
template <class E>
384+
struct has_fixed_size<E, std::void_t<decltype(std::tuple_size<E>::value)>>
385+
: std::true_type
386+
{
387+
};
388+
378389
template <class... S>
379-
using only_array = xtl::conjunction<xtl::disjunction<is_array<S>, is_fixed<S>>...>;
390+
using only_array = xtl::conjunction<xtl::disjunction<is_array<S>, is_fixed<S>, has_fixed_size<S>>...>;
380391

381392
// test that at least one argument is a fixed shape. If yes, then either argument has to be fixed or scalar
382393
template <class... S>

0 commit comments

Comments
 (0)