23#ifndef Rcpp__Algorithm_h
24#define Rcpp__Algorithm_h
46 template< std::
size_t I >
91 template<
typename T >
115 template<
typename T >
139 template<
typename T >
146 static constexpr int RTYPE = REALSXP;
147 static inline double NA() {
return NA_REAL; }
148 static inline constexpr double ZERO() {
return 0.0; }
149 static inline constexpr double ONE() {
return 1.0; }
155 static constexpr int RTYPE = INTSXP;
156 static inline int NA() {
return NA_INTEGER; }
157 static inline constexpr int ZERO() {
return 0; }
158 static inline constexpr int ONE() {
return 1; }
161 template<
typename T >
165 static constexpr int RTYPE = helper_type::RTYPE;
166 static inline T
NA() {
return helper_type::NA(); }
167 static inline constexpr T
ZERO() {
return helper_type::ZERO(); }
168 static inline constexpr T
ONE() {
return helper_type::ONE(); }
172 template<
typename T >
175 return std::log(val);
183 template<
typename T >
186 return std::exp(val);
194 template<
typename T >
197 return std::sqrt(val);
205template<
typename InputIterator >
207 typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type >::type
208 sum(InputIterator begin, InputIterator end) {
214 value_type start = rtype::ZERO();
216 while (begin != end) {
227 return rtype::ZERO();
230template<
typename InputIterator >
232 typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type >::type
239 value_type start = rtype::ZERO();
241 while (begin != end) {
248 return rtype::ZERO();
251template<
typename InputIterator >
253 typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type >::type
254 prod(InputIterator begin, InputIterator end) {
260 value_type start = rtype::ONE();
262 while (begin != end) {
276template<
typename InputIterator >
278 typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type >::type
285 value_type start = rtype::ONE();
287 while (begin != end) {
297template<
typename InputIterator >
299 typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type >::type
300 max(InputIterator begin, InputIterator end) {
306 value_type
max = *begin;
308 while (begin != end) {
310 max = std::max(
max, *begin++);
319 return std::numeric_limits< typename rtype::type >::infinity() * -rtype::ONE();
322template<
typename InputIterator >
324 typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type >::type
331 value_type
max = *begin;
333 while (begin != end) {
334 max = std::max(
max, *begin++);
340 return std::numeric_limits< typename rtype::type >::infinity() * -rtype::ONE();
343template<
typename InputIterator >
345 typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type >::type
346 min(InputIterator begin, InputIterator end) {
352 value_type
min = *begin;
354 while (begin != end) {
356 min = std::min(
min, *begin++);
365 return std::numeric_limits< typename rtype::type >::infinity();
368template<
typename InputIterator >
370 typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type >::type
377 value_type
min = *begin;
379 while (begin != end) {
380 min = std::min(
min, *begin++);
386 return std::numeric_limits< typename rtype::type >::infinity();
390template<
typename InputIterator >
393 mean(InputIterator begin, InputIterator end)
397 std::size_t n = end - begin;
398 long double s = std::accumulate(begin, end, 0.0L);
401 if (R_FINITE((
double) s)) {
402 long double t = 0.0L;
403 while (begin != end) {
417template<
typename InputIterator >
420 mean(InputIterator begin, InputIterator end)
424 std::size_t n = end - begin;
425 long double s = std::accumulate(begin, end, 0.0L);
428 if (R_FINITE((
double) s)) {
429 long double t = 0.0L;
430 while (begin != end) {
444template<
typename InputIterator,
typename OutputIterator >
445void log(InputIterator begin, InputIterator end, OutputIterator out) {
449template<
typename InputIterator,
typename OutputIterator >
450void exp(InputIterator begin, InputIterator end, OutputIterator out) {
454template<
typename InputIterator,
typename OutputIterator >
455void sqrt(InputIterator begin, InputIterator end, OutputIterator out) {
static bool is_na(stored_type x)
long long int rcpp_long_long_type
unsigned long long int rcpp_ulong_long_type
void exp(InputIterator begin, InputIterator end, OutputIterator out)
traits::enable_if< helpers::decays_to_ctype< typenamestd::iterator_traits< InputIterator >::value_type >::value, typenamehelpers::ctype< typenamestd::iterator_traits< InputIterator >::value_type >::type >::type max_nona(InputIterator begin, InputIterator end)
traits::enable_if< helpers::decays_to_ctype< typenamestd::iterator_traits< InputIterator >::value_type >::value &&traits::same_type< typenamehelpers::ctype< typenamestd::iterator_traits< InputIterator >::value_type >::type, double >::value, double >::type mean(InputIterator begin, InputIterator end)
traits::enable_if< helpers::decays_to_ctype< typenamestd::iterator_traits< InputIterator >::value_type >::value, typenamehelpers::ctype< typenamestd::iterator_traits< InputIterator >::value_type >::type >::type sum(InputIterator begin, InputIterator end)
traits::enable_if< helpers::decays_to_ctype< typenamestd::iterator_traits< InputIterator >::value_type >::value, typenamehelpers::ctype< typenamestd::iterator_traits< InputIterator >::value_type >::type >::type prod(InputIterator begin, InputIterator end)
void log(InputIterator begin, InputIterator end, OutputIterator out)
void sqrt(InputIterator begin, InputIterator end, OutputIterator out)
traits::enable_if< helpers::decays_to_ctype< typenamestd::iterator_traits< InputIterator >::value_type >::value, typenamehelpers::ctype< typenamestd::iterator_traits< InputIterator >::value_type >::type >::type max(InputIterator begin, InputIterator end)
traits::enable_if< helpers::decays_to_ctype< typenamestd::iterator_traits< InputIterator >::value_type >::value, typenamehelpers::ctype< typenamestd::iterator_traits< InputIterator >::value_type >::type >::type min(InputIterator begin, InputIterator end)
traits::enable_if< helpers::decays_to_ctype< typenamestd::iterator_traits< InputIterator >::value_type >::value, typenamehelpers::ctype< typenamestd::iterator_traits< InputIterator >::value_type >::type >::type sum_nona(InputIterator begin, InputIterator end)
traits::enable_if< helpers::decays_to_ctype< typenamestd::iterator_traits< InputIterator >::value_type >::value, typenamehelpers::ctype< typenamestd::iterator_traits< InputIterator >::value_type >::type >::type prod_nona(InputIterator begin, InputIterator end)
traits::enable_if< helpers::decays_to_ctype< typenamestd::iterator_traits< InputIterator >::value_type >::value, typenamehelpers::ctype< typenamestd::iterator_traits< InputIterator >::value_type >::type >::type min_nona(InputIterator begin, InputIterator end)
sugar::IsNa< RTYPE, NA, T > is_na(const Rcpp::VectorBase< RTYPE, NA, T > &t)
rcpp_ulong_long_type type
static CTYPE_STRING test(const std::string &)
static CTYPE_LONG_LONG test(const rcpp_long_long_type &)
static CTYPE_LONG test(const long &)
static CTYPE_CHAR test(const char &)
static CTYPE_UNSIGNED_INT test(const unsigned int &)
static CTYPE_INT test(const int &)
static CTYPE_UNSIGNED_LONG_LONG test(const rcpp_ulong_long_type &)
static CTYPE_UNSIGNED_SHORT test(const unsigned short &)
static CTYPE_UNSIGNED_CHAR test(const unsigned char &)
static CTYPE_SHORT test(const short &)
static CTYPE_UNKNOWN test(...)
ctype_helper< sizeof(test(make()))>::type type
static CTYPE_DOUBLE test(const double &)
static CTYPE_FLOAT test(const float &)
static CTYPE_UNSIGNED_LONG test(const unsigned long &)
static CTYPE_LONG_DOUBLE test(const long double &)
static CTYPE_CHAR test(const char &)
static CTYPE_FLOAT test(const float &)
static CTYPE_UNSIGNED_LONG test(const unsigned long &)
static CTYPE_LONG_LONG test(const rcpp_long_long_type &)
static CTYPE_SHORT test(const short &)
static CTYPE_UNSIGNED_INT test(const unsigned int &)
static CTYPE_UNSIGNED_CHAR test(const unsigned char &)
static CTYPE_DOUBLE test(const double &)
static CTYPE_LONG_DOUBLE test(const long double &)
static CTYPE_UNSIGNED_LONG_LONG test(const rcpp_ulong_long_type &)
static CTYPE_STRING test(const std::string &)
static CTYPE_INT test(const int &)
static CTYPE_LONG test(const long &)
static CTYPE_UNKNOWN test(...)
static CTYPE_UNSIGNED_SHORT test(const unsigned short &)
static constexpr int RTYPE
static constexpr double ONE()
static constexpr double ZERO()
static constexpr int ONE()
static constexpr int RTYPE
static constexpr int ZERO()
rtype_helper< typename ctype< T >::type > helper_type
static constexpr int RTYPE
static constexpr T ZERO()
rtype_helper< typenamectype< T >::type >::type type