Rcpp Version 1.1.2
Loading...
Searching...
No Matches
sum.h
Go to the documentation of this file.
1// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2//
3// sum.h: Rcpp R/C++ interface class library -- sum
4//
5// Copyright (C) 2010 - 2025 Dirk Eddelbuettel and Romain Francois
6// Copyright (C) 2026 Dirk Eddelbuettel, Romain Francois and IƱaki Ucar
7//
8// This file is part of Rcpp.
9//
10// Rcpp is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 2 of the License, or
13// (at your option) any later version.
14//
15// Rcpp is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
22
23#ifndef Rcpp__sugar__sum_h
24#define Rcpp__sugar__sum_h
25
26namespace Rcpp{
27namespace sugar{
28
29template <int RTYPE, bool NA, typename T>
30class Sum : public Lazy< typename Rcpp::traits::storage_type<RTYPE>::type , Sum<RTYPE,NA,T> > {
31public:
35
36 Sum( const VEC_TYPE& object_ ) : object(object_.get_ref()){}
37
38 STORAGE get() const {
39 STORAGE result = 0 ;
40 R_xlen_t n = object.size() ;
41 STORAGE current ;
42 for( R_xlen_t i=0; i<n; i++){
43 current = object[i] ;
44 if( Rcpp::traits::is_na<RTYPE>(current) )
46 result = RCPP_SAFE_ADD(result, current);
47 }
48 return result ;
49 }
50private:
51 const VEC_EXT& object ;
52} ;
53// RTYPE = REALSXP
54template <bool NA, typename T>
55class Sum<REALSXP,NA,T> : public Lazy< double , Sum<REALSXP,NA,T> > {
56public:
59
60 Sum( const VEC_TYPE& object_ ) : object(object_.get_ref()){}
61
62 double get() const {
63 double result = 0 ;
64 R_xlen_t n = object.size() ;
65 for( R_xlen_t i=0; i<n; i++){
66 result += object[i] ;
67 }
68 return result ;
69 }
70private:
71 const VEC_EXT& object ;
72} ;
73
74
75template <int RTYPE, typename T>
76class Sum<RTYPE,false,T> : public Lazy< typename Rcpp::traits::storage_type<RTYPE>::type , Sum<RTYPE,false,T> > {
77public:
81
82 Sum( const VEC_TYPE& object_ ) : object(object_.get_ref()){}
83
84 STORAGE get() const {
85 STORAGE result = 0 ;
86 R_xlen_t n = object.size() ;
87 for( R_xlen_t i=0; i<n; i++){
88 result = RCPP_SAFE_ADD(result, object[i]);
89 }
90 return result ;
91 }
92private:
93 const VEC_EXT& object ;
94} ;
95
96} // sugar
97
98template <bool NA, typename T>
102
103template <bool NA, typename T>
107
108template <bool NA, typename T>
112
113} // Rcpp
114#endif
115
Rcpp::VectorBase< REALSXP, NA, T > VEC_TYPE
Definition sum.h:57
Rcpp::traits::Extractor< REALSXP, NA, T >::type VEC_EXT
Definition sum.h:58
Sum(const VEC_TYPE &object_)
Definition sum.h:60
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition sum.h:79
Rcpp::VectorBase< RTYPE, false, T > VEC_TYPE
Definition sum.h:78
Sum(const VEC_TYPE &object_)
Definition sum.h:82
Rcpp::traits::Extractor< RTYPE, false, T >::type VEC_EXT
Definition sum.h:80
Rcpp::traits::Extractor< RTYPE, NA, T >::type VEC_EXT
Definition sum.h:34
STORAGE get() const
Definition sum.h:38
Rcpp::VectorBase< RTYPE, NA, T > VEC_TYPE
Definition sum.h:32
const VEC_EXT & object
Definition sum.h:51
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition sum.h:33
Sum(const VEC_TYPE &object_)
Definition sum.h:36
storage_type< RTYPE >::type get_na()
bool is_na(typename storage_type< RTYPE >::type)
Definition is_na.h:32
Rcpp API.
Definition algo.h:28
static Na_Proxy NA
Definition Na_Proxy.h:52
sugar::Sum< INTSXP, NA, T > sum(const VectorBase< INTSXP, NA, T > &t)
Definition sum.h:99
#define RCPP_SAFE_ADD(a, b)
Definition safe_math.h:33