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