Skip to content

Commit 351378a

Browse files
author
Mykola Vankovych
committed
adding xt::detail::get_fixed_size.
1 parent a159751 commit 351378a

File tree

6 files changed

+52
-22
lines changed

6 files changed

+52
-22
lines changed

include/xtensor/xadapt.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,11 @@ namespace xt
230230
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class C, class SC,
231231
XTL_REQUIRES(detail::has_fixed_size<std::decay_t<SC>>,
232232
detail::not_a_pointer<std::remove_reference_t<C>>)>
233-
inline xtensor_adaptor<C, std::tuple_size<std::decay_t<SC>>::value, L>
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 = std::tuple_size<std::decay_t<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
}
@@ -252,7 +252,7 @@ namespace xt
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 = std::tuple_size<std::decay_t<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
}
@@ -267,11 +267,11 @@ namespace xt
267267
template <class C, class SC, class SS,
268268
XTL_REQUIRES(detail::has_fixed_size<std::decay_t<SC>>,
269269
detail::not_a_layout<std::decay_t<SS>>)>
270-
inline xtensor_adaptor<C, std::tuple_size<std::decay_t<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 = std::tuple_size<std::decay_t<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),
@@ -313,13 +313,13 @@ namespace xt
313313
*/
314314
template <layout_type L = XTENSOR_DEFAULT_LAYOUT, class P, class O, class SC, class A = detail::default_allocator_for_ptr_t<P>,
315315
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>
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 = std::tuple_size<std::decay_t<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);
@@ -339,13 +339,13 @@ namespace xt
339339
template <class P, class O, class SC, class SS, class A = detail::default_allocator_for_ptr_t<P>,
340340
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>, std::tuple_size<std::decay_t<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 = std::tuple_size<std::decay_t<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),

include/xtensor/xeval.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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: 2 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;

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: 19 additions & 5 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,17 +381,25 @@ namespace xt
375381
static constexpr bool value = true;
376382
};
377383

378-
template <class E, class = void>
384+
template <class E, class Enable = void>
379385
struct has_fixed_size : std::false_type
380386
{
381387
};
382388

383389
template <class E>
384-
struct has_fixed_size<E, std::void_t<decltype(std::tuple_size<E>::value)>>
390+
struct has_fixed_size<E, void_t<decltype(std::tuple_size<E>::value)>>
385391
: std::true_type
386392
{
387393
};
388394

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+
389403
template <class... S>
390404
using only_array = xtl::conjunction<xtl::disjunction<is_array<S>, is_fixed<S>, has_fixed_size<S>>...>;
391405

0 commit comments

Comments
 (0)