Rcpp Version 1.0.9
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 
25 namespace Rcpp{
26 namespace sugar{
27 
28 template <int RTYPE, bool NA, typename T>
29 class Sum : public Lazy< typename Rcpp::traits::storage_type<RTYPE>::type , Sum<RTYPE,NA,T> > {
30 public:
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  }
49 private:
50  const VEC_EXT& object ;
51 } ;
52 // RTYPE = REALSXP
53 template <bool NA, typename T>
54 class Sum<REALSXP,NA,T> : public Lazy< double , Sum<REALSXP,NA,T> > {
55 public:
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  }
69 private:
70  const VEC_EXT& object ;
71 } ;
72 
73 
74 template <int RTYPE, typename T>
75 class Sum<RTYPE,false,T> : public Lazy< typename Rcpp::traits::storage_type<RTYPE>::type , Sum<RTYPE,false,T> > {
76 public:
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  }
91 private:
92  const VEC_EXT& object ;
93 } ;
94 
95 } // sugar
96 
97 template <bool NA, typename T>
99  return sugar::Sum<INTSXP,NA,T>( t ) ;
100 }
101 
102 template <bool NA, typename T>
104  return sugar::Sum<REALSXP,NA,T>( t ) ;
105 }
106 
107 template <bool NA, typename T>
109  return sugar::Sum<LGLSXP,NA,T>( t ) ;
110 }
111 
112 } // Rcpp
113 #endif
114 
Rcpp::traits::Extractor< REALSXP, NA, T >::type VEC_EXT
Definition: sum.h:57
const VEC_EXT & object
Definition: sum.h:70
Rcpp::VectorBase< REALSXP, NA, T > VEC_TYPE
Definition: sum.h:56
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
const VEC_EXT & object
Definition: sum.h:92
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
sugar::Sum< INTSXP, NA, T > sum(const VectorBase< INTSXP, NA, T > &t)
Definition: sum.h:98
static Na_Proxy NA
Definition: Na_Proxy.h:52