Skip to content

Commit 76ecc53

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 7eb537c commit 76ecc53

2 files changed

Lines changed: 39 additions & 12 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;
@@ -1325,30 +1338,32 @@ namespace xt
13251338
struct pow_impl
13261339
{
13271340
template <class T>
1328-
auto operator()(T v) const -> decltype(v * v)
1341+
auto operator()(T&& v) const
13291342
{
1330-
T temp = pow_impl<N / 2>{}(v);
1331-
return temp * temp * pow_impl<N & 1>{}(v);
1343+
auto value = lambda_argument(std::forward<T>(v));
1344+
auto temp = pow_impl<N / 2>{}(value);
1345+
return temp * temp * pow_impl<N & 1>{}(value);
13321346
}
13331347
};
13341348

13351349
template <>
13361350
struct pow_impl<1>
13371351
{
13381352
template <class T>
1339-
auto operator()(T v) const -> T
1353+
decltype(auto) operator()(T&& v) const
13401354
{
1341-
return v;
1355+
return lambda_argument(std::forward<T>(v));
13421356
}
13431357
};
13441358

13451359
template <>
13461360
struct pow_impl<0>
13471361
{
13481362
template <class T>
1349-
auto operator()(T /*v*/) const -> T
1363+
auto operator()(T&& v) const
13501364
{
1351-
return T(1);
1365+
using value_type = std::decay_t<decltype(lambda_argument(std::forward<T>(v)))>;
1366+
return value_type(1);
13521367
}
13531368
};
13541369
}

include/xtensor/io/xio.hpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,18 @@ namespace xt
185185

186186
namespace detail
187187
{
188+
template <typename T, typename B>
189+
inline auto printable_value(const xtl::xmasked_value<T, B>& value)
190+
{
191+
return +value;
192+
}
193+
194+
template <typename T>
195+
inline const T& printable_value(const T& value)
196+
{
197+
return value;
198+
}
199+
188200
template <class E, class F>
189201
std::ostream& xoutput(
190202
std::ostream& out,
@@ -646,7 +658,7 @@ namespace xt
646658
void update(const_reference val)
647659
{
648660
std::stringstream buf;
649-
buf << val;
661+
buf << printable_value(val);
650662
std::string s = buf.str();
651663
if (int(s.size()) > m_width)
652664
{

0 commit comments

Comments
 (0)