Skip to content

Commit 22a30c9

Browse files
author
Mykola Vankovych
committed
replacing is_array, tuple_size with has_fixed_size, get_fixed_size.
1 parent 3084585 commit 22a30c9

File tree

7 files changed

+124
-61
lines changed

7 files changed

+124
-61
lines changed

include/xtensor/xadapt.hpp

Lines changed: 28 additions & 28 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,7 +88,7 @@ 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>>,
91+
XTL_REQUIRES(detail::has_dynamic_size<std::decay_t<SC>>,
9292
std::is_pointer<std::remove_reference_t<C>>)>
9393
inline auto adapt(C&& pointer, const SC& shape, layout_type l = L)
9494
{
@@ -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, detail::get_fixed_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 = detail::get_fixed_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>>,
249+
XTL_REQUIRES(detail::has_fixed_size<std::decay_t<SC>>,
250250
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 = detail::get_fixed_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, detail::get_fixed_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 = detail::get_fixed_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>, detail::get_fixed_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 = detail::get_fixed_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>, detail::get_fixed_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 = detail::get_fixed_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: 2 additions & 2 deletions
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>
@@ -102,7 +102,7 @@ namespace xt
102102

103103
template <class E, layout_type L>
104104
using as_xtensor_container_t = xtensor<typename std::decay_t<E>::value_type,
105-
std::tuple_size<typename std::decay_t<E>::shape_type>::value,
105+
detail::get_fixed_size<typename std::decay_t<E>::shape_type>::value,
106106
layout_remove_any(L)>;
107107
}
108108

include/xtensor/xfixed.hpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ namespace xt
228228
constexpr T get_backstrides(const S& shape, const T& strides) noexcept
229229
{
230230
return detail::get_backstrides_impl(shape, strides,
231-
std::make_index_sequence<std::tuple_size<T>::value>{});
231+
std::make_index_sequence<detail::get_fixed_size<T>::value>{});
232232
}
233233

234234
template <class V, class S>
@@ -314,7 +314,7 @@ namespace xt
314314
using temporary_type = typename semantic_base::temporary_type;
315315
using expression_tag = Tag;
316316

317-
constexpr static std::size_t N = std::tuple_size<shape_type>::value;
317+
constexpr static std::size_t N = detail::get_fixed_size<shape_type>::value;
318318
constexpr static std::size_t rank = N;
319319

320320
xfixed_container() = default;
@@ -950,4 +950,29 @@ 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+
template <class ET, class S, xt::layout_type L, bool SH, class Tag>
963+
class std::tuple_size<xt::xfixed_container<ET, S, L, SH, Tag>> :
964+
public std::integral_constant<std::size_t, xt::detail::fixed_compute_size<S>::value>
965+
{
966+
};
967+
968+
template <class ET, class S, xt::layout_type L, bool SH, class Tag>
969+
class std::tuple_size<xt::xfixed_adaptor<ET, S, L, SH, Tag>> :
970+
public std::integral_constant<std::size_t, xt::detail::fixed_compute_size<S>::value>
971+
{
972+
};
973+
974+
#if defined(__clang__)
975+
# pragma clang diagnostic pop
976+
#endif
977+
953978
#endif

include/xtensor/xfunction.hpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <xtl/xtype_traits.hpp>
2323

2424
#include "xaccessible.hpp"
25+
#include "xaccumulator.hpp"
2526
#include "xexpression_traits.hpp"
2627
#include "xiterable.hpp"
2728
#include "xlayout.hpp"
@@ -913,7 +914,7 @@ namespace xt
913914
// Optimization: no need to compare each subiterator since they all
914915
// are incremented decremented together.
915916
constexpr std::size_t temp = xtl::mpl::find_if<is_not_xdummy_iterator, data_type>::value;
916-
constexpr std::size_t index = (temp == std::tuple_size<data_type>::value) ? 0 : temp;
917+
constexpr std::size_t index = (temp == detail::get_fixed_size<data_type>::value) ? 0 : temp;
917918
return std::get<index>(m_it) == std::get<index>(rhs.m_it);
918919
}
919920

@@ -923,7 +924,7 @@ namespace xt
923924
// Optimization: no need to compare each subiterator since they all
924925
// are incremented decremented together.
925926
constexpr std::size_t temp = xtl::mpl::find_if<is_not_xdummy_iterator, data_type>::value;
926-
constexpr std::size_t index = (temp == std::tuple_size<data_type>::value) ? 0 : temp;
927+
constexpr std::size_t index = (temp == detail::get_fixed_size<data_type>::value) ? 0 : temp;
927928
return std::get<index>(m_it) < std::get<index>(rhs.m_it);
928929
}
929930

@@ -1059,6 +1060,21 @@ namespace xt
10591060
auto step_leading_lambda = [](auto&& st) { st.step_leading(); };
10601061
for_each(step_leading_lambda, m_st);
10611062
}
1063+
1064+
namespace detail
1065+
{
1066+
template<class F, class... CT>
1067+
struct has_fixed_size<xfunction<F, CT...>, std::enable_if_t<is_fixed<typename xfunction<F, CT...>::shape_type>::value>>
1068+
: std::true_type
1069+
{
1070+
};
1071+
1072+
template<class F, class... CT>
1073+
struct get_fixed_size<xfunction<F, CT...>, std::enable_if_t<has_fixed_size<xfunction<F, CT...>>::value>>
1074+
: std::integral_constant<std::size_t, fixed_compute_size<typename xfunction<F, CT...>::shape_type>::value>
1075+
{
1076+
};
1077+
}
10621078
}
10631079

10641080
#endif

include/xtensor/xreducer.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ namespace xt
117117

118118
reducer_options(const T& tpl)
119119
{
120-
xtl::mpl::static_if<initial_val_idx != std::tuple_size<T>::value>([this, &tpl](auto no_compile) {
120+
xtl::mpl::static_if<initial_val_idx != detail::get_fixed_size<T>::value>([this, &tpl](auto no_compile) {
121121
// use no_compile to prevent compilation if initial_val_idx is out of bounds!
122-
this->initial_value = no_compile(std::get<initial_val_idx != std::tuple_size<T>::value ? initial_val_idx : 0>(tpl)).value();
122+
this->initial_value = no_compile(std::get<initial_val_idx != detail::get_fixed_size<T>::value ? initial_val_idx : 0>(tpl)).value();
123123
},
124124
[](auto /*np_compile*/){}
125125
);
@@ -133,7 +133,7 @@ namespace xt
133133
std::true_type,
134134
std::false_type>;
135135

136-
constexpr static bool has_initial_value = initial_val_idx != std::tuple_size<d_t>::value;
136+
constexpr static bool has_initial_value = initial_val_idx != detail::get_fixed_size<d_t>::value;
137137

138138
R initial_value;
139139

include/xtensor/xshape.hpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525

2626
namespace xt
2727
{
28+
namespace detail
29+
{
30+
template <class E, class Enable = void>
31+
struct get_fixed_size;
32+
}
33+
2834
template <class T>
2935
using dynamic_shape = svector<T, 4>;
3036

@@ -231,9 +237,9 @@ namespace xt
231237
};
232238

233239
template <class T>
234-
struct static_dimension_impl<T, void_t<decltype(std::tuple_size<T>::value)>>
240+
struct static_dimension_impl<T, void_t<decltype(detail::get_fixed_size<T>::value)>>
235241
{
236-
static constexpr std::ptrdiff_t value = static_cast<std::ptrdiff_t>(std::tuple_size<T>::value);
242+
static constexpr std::ptrdiff_t value = static_cast<std::ptrdiff_t>(detail::get_fixed_size<T>::value);
237243
};
238244
}
239245

@@ -281,7 +287,7 @@ namespace xt
281287
};
282288

283289
template <class T, class... Ts>
284-
struct max_array_size<T, Ts...> : std::integral_constant<std::size_t, imax(std::tuple_size<T>::value, max_array_size<Ts...>::value)>
290+
struct max_array_size<T, Ts...> : std::integral_constant<std::size_t, imax(detail::get_fixed_size<T>::value, max_array_size<Ts...>::value)>
285291
{
286292
};
287293

@@ -375,8 +381,27 @@ namespace xt
375381
static constexpr bool value = true;
376382
};
377383

384+
template <class E, class Enable = void>
385+
struct has_fixed_size : std::false_type
386+
{
387+
};
388+
389+
template <class E>
390+
struct has_fixed_size<E, void_t<decltype(std::tuple_size<E>::value)>>
391+
: std::true_type
392+
{
393+
};
394+
395+
template <class E, class Enable>
396+
struct get_fixed_size;
397+
398+
template <class E>
399+
struct get_fixed_size<E, void_t<decltype(std::tuple_size<E>::value)>> : std::integral_constant<std::size_t, std::tuple_size<E>::value>
400+
{
401+
};
402+
378403
template <class... S>
379-
using only_array = xtl::conjunction<xtl::disjunction<is_array<S>, is_fixed<S>>...>;
404+
using only_array = xtl::conjunction<xtl::disjunction<is_array<S>, is_fixed<S>, has_fixed_size<S>>...>;
380405

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

0 commit comments

Comments
 (0)