Skip to content

Commit e66295e

Browse files
alexandrehoffmannAlexandre Hoffmann
andauthored
Adding concept to a part of the code (part 2) (#2846)
# Checklist - [ X] The title and commit message(s) are descriptive. - [ X] Small commits made to fix your PR have been squashed to avoid history pollution. - [ X] Tests have been added for new features or bug fixes. - [ ] API of new functions and classes are documented. # Description This PR C++20's concept and C++17's if constexpr to a moderatly large chunk of xtensor. --------- Co-authored-by: Alexandre Hoffmann <[email protected]>
1 parent abce45a commit e66295e

File tree

10 files changed

+227
-311
lines changed

10 files changed

+227
-311
lines changed

include/xtensor/misc/xtl_concepts.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ namespace xtl
1717
{
1818
template <typename T>
1919
concept integral_concept = xtl::is_integral<T>::value;
20+
2021
template <typename T>
2122
concept non_integral_concept = !xtl::is_integral<T>::value;
23+
2224
template <typename T>
2325
concept complex_concept = xtl::is_complex<typename std::decay<T>::type::value_type>::value;
26+
27+
template <typename T>
28+
concept pointer_concept = std::is_pointer<T>::value;
2429
}
2530

2631
#endif // XTENSOR_CONCEPTS_HPP

include/xtensor/views/xaxis_iterator.hpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,7 @@ namespace xt
6868
value_type m_sv;
6969

7070
template <class T, class CTA>
71-
std::enable_if_t<std::is_pointer<T>::value, T> get_storage_init(CTA&& e) const;
72-
73-
template <class T, class CTA>
74-
std::enable_if_t<!std::is_pointer<T>::value, T> get_storage_init(CTA&& e) const;
71+
T get_storage_init(CTA&& e) const;
7572
};
7673

7774
template <class CT>
@@ -125,16 +122,16 @@ namespace xt
125122

126123
template <class CT>
127124
template <class T, class CTA>
128-
inline std::enable_if_t<std::is_pointer<T>::value, T> xaxis_iterator<CT>::get_storage_init(CTA&& e) const
125+
inline T xaxis_iterator<CT>::get_storage_init(CTA&& e) const
129126
{
130-
return &e;
131-
}
132-
133-
template <class CT>
134-
template <class T, class CTA>
135-
inline std::enable_if_t<!std::is_pointer<T>::value, T> xaxis_iterator<CT>::get_storage_init(CTA&& e) const
136-
{
137-
return e;
127+
if constexpr (xtl::pointer_concept<T>)
128+
{
129+
return &e;
130+
}
131+
else
132+
{
133+
return e;
134+
}
138135
}
139136

140137
/**

include/xtensor/views/xaxis_slice_iterator.hpp

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@ namespace xt
6969
value_type m_sv;
7070

7171
template <class T, class CTA>
72-
std::enable_if_t<std::is_pointer<T>::value, T> get_storage_init(CTA&& e) const;
73-
74-
template <class T, class CTA>
75-
std::enable_if_t<!std::is_pointer<T>::value, T> get_storage_init(CTA&& e) const;
72+
T get_storage_init(CTA&& e) const;
7673
};
7774

7875
template <class CT>
@@ -99,18 +96,16 @@ namespace xt
9996

10097
template <class CT>
10198
template <class T, class CTA>
102-
inline std::enable_if_t<std::is_pointer<T>::value, T>
103-
xaxis_slice_iterator<CT>::get_storage_init(CTA&& e) const
104-
{
105-
return &e;
106-
}
107-
108-
template <class CT>
109-
template <class T, class CTA>
110-
inline std::enable_if_t<!std::is_pointer<T>::value, T>
111-
xaxis_slice_iterator<CT>::get_storage_init(CTA&& e) const
99+
T xaxis_slice_iterator<CT>::get_storage_init(CTA&& e) const
112100
{
113-
return e;
101+
if constexpr (xtl::pointer_concept<T>)
102+
{
103+
return &e;
104+
}
105+
else
106+
{
107+
return e;
108+
}
114109
}
115110

116111
/**

include/xtensor/views/xbroadcast.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ namespace xt
7272
template <class CT, class X>
7373
class xbroadcast;
7474

75+
template <class E>
76+
concept xbroadcast_concept = is_specialization_of<xbroadcast, E>::value;
77+
7578
template <class CT, class X>
7679
struct xiterable_inner_types<xbroadcast<CT, X>>
7780
{
@@ -122,10 +125,9 @@ namespace xt
122125
* overlapping_memory_checker_traits *
123126
*************************************/
124127

125-
template <class E>
126-
struct overlapping_memory_checker_traits<
127-
E,
128-
std::enable_if_t<!has_memory_address<E>::value && is_specialization_of<xbroadcast, E>::value>>
128+
template <xbroadcast_concept E>
129+
requires(without_memory_address_concept<E>)
130+
struct overlapping_memory_checker_traits<E>
129131
{
130132
static bool check_overlap(const E& expr, const memory_range& dst_range)
131133
{
@@ -223,7 +225,7 @@ namespace xt
223225
template <class S>
224226
const_stepper stepper_end(const S& shape, layout_type l) const noexcept;
225227

226-
template <class E, class XCT = CT, class = std::enable_if_t<xt::is_xscalar<XCT>::value>>
228+
template <class E, xscalar_concept XCT = CT>
227229
void assign_to(xexpression<E>& e) const;
228230

229231
template <class E>
@@ -463,7 +465,7 @@ namespace xt
463465
}
464466

465467
template <class CT, class X>
466-
template <class E, class XCT, class>
468+
template <class E, xscalar_concept XCT>
467469
inline void xbroadcast<CT, X>::assign_to(xexpression<E>& e) const
468470
{
469471
auto& ed = e.derived_cast();

include/xtensor/views/xoffset_view.hpp

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,9 @@ namespace xt
3838
return xtl::forward_offset<M, I>(std::forward<T>(t));
3939
}
4040

41-
template <
42-
class align,
43-
class requested_type,
44-
std::size_t N,
45-
class E,
46-
class MF = M,
47-
class = std::enable_if_t<
48-
(std::is_same<MF, double>::value || std::is_same<MF, float>::value) && I <= sizeof(MF),
49-
int>>
41+
template <class align, class requested_type, std::size_t N, class E, class MF = M>
5042
auto proxy_simd_load(const E& expr, std::size_t n) const
43+
requires((std::is_same<MF, double>::value || std::is_same<MF, float>::value) && I <= sizeof(MF))
5144
{
5245
// TODO refactor using shuffle only
5346
auto batch = expr.template load_simd<align, requested_type, N>(n);
@@ -61,15 +54,9 @@ namespace xt
6154
}
6255
}
6356

64-
template <
65-
class align,
66-
class simd,
67-
class E,
68-
class MF = M,
69-
class = std::enable_if_t<
70-
(std::is_same<MF, double>::value || std::is_same<MF, float>::value) && I <= sizeof(MF),
71-
int>>
57+
template <class align, class simd, class E, class MF = M>
7258
auto proxy_simd_store(E& expr, std::size_t n, const simd& batch) const
59+
requires((std::is_same<MF, double>::value || std::is_same<MF, float>::value) && I <= sizeof(MF))
7360
{
7461
auto x = expr.template load_simd<align, double, simd::size>(n);
7562
if (I == 0)

0 commit comments

Comments
 (0)