[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/josh28x/cpp11/main/cpp11test/src/sum_int.cpp [Back]  [Original]

#include 
#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.);
}

Web Proxy Viewer  |  New URL  |  Original Page