Skip to content

Commit 08d5582

Browse files
committed
for_each() takes a functor by reference; returns void
1 parent e808dfc commit 08d5582

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

gallery/simd-variance.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ class SumOfSquaresAccumulator
7979
pack_ += boost::simd::sqr(data - mean_);
8080
}
8181

82-
// Provide a 'conversion to double' operator -- this lets
83-
// us easily extract the resulting value after computation.
84-
// This is also where we combine our scalar, and packed,
85-
// representations of the data.
86-
operator double() const {
82+
// We can use 'value()' to extract the result of the
83+
// computation, after providing this object to
84+
// 'simd::for_each()'.
85+
double value() const
86+
{
8787
return result_ + boost::simd::sum(pack_);
8888
}
8989

@@ -101,7 +101,8 @@ class SumOfSquaresAccumulator
101101
*/
102102

103103
// [[Rcpp::export]]
104-
double simdVar(NumericVector data) {
104+
double simdVar(NumericVector data)
105+
{
105106

106107
// Pull in the 'boost::simd' namespace for easier access
107108
// to the functions we need to use.
@@ -118,9 +119,11 @@ double simdVar(NumericVector data) {
118119
double mean = total / n;
119120

120121
// Use our accumulator to compute the sum of squares.
121-
double ssq = simd::for_each(data.begin(),
122-
data.end(),
123-
SumOfSquaresAccumulator(mean));
122+
SumOfSquaresAccumulator accumulator(mean);
123+
simd::for_each(data.begin(),
124+
data.end(),
125+
accumulator);
126+
double ssq = accumulator.value();
124127

125128
// Divide by 'n - 1', and we're done!
126129
return ssq / (n - 1);

inst/include/RcppParallel/simd/for_each.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace boost {
55
namespace simd {
66

77
template <typename T, typename F>
8-
F for_each(const T* it, const T* end, F f)
8+
void for_each(const T* it, const T* end, F& f)
99
{
1010
typedef boost::simd::pack<T> vT;
1111
static const std::size_t N = vT::static_size;
@@ -20,8 +20,6 @@ F for_each(const T* it, const T* end, F f)
2020

2121
for (; it != end; ++it)
2222
f(*it);
23-
24-
return f;
2523
}
2624

2725
} // namespace simd

0 commit comments

Comments
 (0)