Skip to content

Commit 200965b

Browse files
Alex-PLACETCopilot
andcommitted
feat: Add lambda_argument and printable_value utilities for enhanced value handling
Co-authored-by: Copilot <copilot@github.com>
1 parent 9a379e2 commit 200965b

2 files changed

Lines changed: 39 additions & 19 deletions

File tree

include/xtensor/core/xmath.hpp

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,19 @@ namespace xt
11851185
{
11861186
};
11871187

1188+
template <typename T>
1189+
inline decltype(auto) lambda_argument(T&& value)
1190+
{
1191+
if constexpr (xtl::is_xmasked_value<std::decay_t<T>>::value)
1192+
{
1193+
return +value;
1194+
}
1195+
else
1196+
{
1197+
return std::forward<T>(value);
1198+
}
1199+
}
1200+
11881201
template <class F>
11891202
struct lambda_adapt
11901203
{
@@ -1194,15 +1207,15 @@ namespace xt
11941207
}
11951208

11961209
template <class... T>
1197-
auto operator()(T... args) const
1210+
auto operator()(T&&... args) const
11981211
{
1199-
return m_lambda(args...);
1212+
return m_lambda(lambda_argument(std::forward<T>(args))...);
12001213
}
12011214

12021215
template <class... T, XTL_REQUIRES(detail::supports<F(T...)>)>
1203-
auto simd_apply(T... args) const
1216+
auto simd_apply(T&&... args) const
12041217
{
1205-
return m_lambda(args...);
1218+
return m_lambda(lambda_argument(std::forward<T>(args))...);
12061219
}
12071220

12081221
F m_lambda;
@@ -1290,30 +1303,32 @@ namespace xt
12901303
struct pow_impl
12911304
{
12921305
template <class T>
1293-
auto operator()(T v) const -> decltype(v * v)
1306+
auto operator()(T&& v) const
12941307
{
1295-
T temp = pow_impl<N / 2>{}(v);
1296-
return temp * temp * pow_impl<N & 1>{}(v);
1308+
auto value = lambda_argument(std::forward<T>(v));
1309+
auto temp = pow_impl<N / 2>{}(value);
1310+
return temp * temp * pow_impl<N & 1>{}(value);
12971311
}
12981312
};
12991313

13001314
template <>
13011315
struct pow_impl<1>
13021316
{
13031317
template <class T>
1304-
auto operator()(T v) const -> T
1318+
decltype(auto) operator()(T&& v) const
13051319
{
1306-
return v;
1320+
return lambda_argument(std::forward<T>(v));
13071321
}
13081322
};
13091323

13101324
template <>
13111325
struct pow_impl<0>
13121326
{
13131327
template <class T>
1314-
auto operator()(T /*v*/) const -> T
1328+
auto operator()(T&& v) const
13151329
{
1316-
return T(1);
1330+
using value_type = std::decay_t<decltype(lambda_argument(std::forward<T>(v)))>;
1331+
return value_type(1);
13171332
}
13181333
};
13191334
}

include/xtensor/io/xio.hpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,18 @@ namespace xt
186186

187187
namespace detail
188188
{
189+
template <typename T, typename B>
190+
inline auto printable_value(const xtl::xmasked_value<T, B>& value)
191+
{
192+
return +value;
193+
}
194+
195+
template <typename T>
196+
inline const T& printable_value(const T& value)
197+
{
198+
return value;
199+
}
200+
189201
template <class E, class F>
190202
std::ostream& xoutput(
191203
std::ostream& out,
@@ -647,14 +659,7 @@ namespace xt
647659
void update(const_reference val)
648660
{
649661
std::stringstream buf;
650-
if constexpr (xtl::is_xmasked_value<value_type>::value)
651-
{
652-
buf << +val;
653-
}
654-
else
655-
{
656-
buf << val;
657-
}
662+
buf << printable_value(val);
658663
std::string s = buf.str();
659664
if (int(s.size()) > m_width)
660665
{

0 commit comments

Comments
 (0)