forked from r-lib/cpp11
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum_int.cpp
More file actions
36 lines (29 loc) · 698 Bytes
/
sum_int.cpp
File metadata and controls
36 lines (29 loc) · 698 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <numeric>
#include "cpp11/integers.hpp"
[[cpp11::register]] double sum_int_for_(cpp11::integers x) {
double sum = 0.;
R_xlen_t n = x.size();
for (R_xlen_t i = 0; i < n; ++i) {
sum += x[i];
}
return sum;
}
[[cpp11::register]] double sum_int_for2_(SEXP x_) {
cpp11::integers x(x_, false);
double sum = 0.;
R_xlen_t n = x.size();
for (R_xlen_t i = 0; i < n; ++i) {
sum += x[i];
}
return sum;
}
[[cpp11::register]] double sum_int_foreach_(cpp11::integers x) {
double sum = 0.;
for (auto v : x) {
sum += v;
}
return sum;
}
[[cpp11::register]] double sum_int_accumulate_(cpp11::integers x) {
return std::accumulate(x.cbegin(), x.cend(), 0.);
}