|
Rcpp Version 0.9.10
|
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*- 00002 // 00003 // sum.h: Rcpp R/C++ interface class library -- sum 00004 // 00005 // Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois 00006 // 00007 // This file is part of Rcpp. 00008 // 00009 // Rcpp is free software: you can redistribute it and/or modify it 00010 // under the terms of the GNU General Public License as published by 00011 // the Free Software Foundation, either version 2 of the License, or 00012 // (at your option) any later version. 00013 // 00014 // Rcpp is distributed in the hope that it will be useful, but 00015 // WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 // GNU General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU General Public License 00020 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>. 00021 00022 #ifndef Rcpp__sugar__sum_h 00023 #define Rcpp__sugar__sum_h 00024 00025 namespace Rcpp{ 00026 namespace sugar{ 00027 00028 template <int RTYPE, bool NA, typename T> 00029 class Sum : public Lazy< typename Rcpp::traits::storage_type<RTYPE>::type , Sum<RTYPE,NA,T> > { 00030 public: 00031 typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ; 00032 typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ; 00033 typedef typename Rcpp::traits::Extractor< RTYPE, NA, T>::type VEC_EXT ; 00034 00035 Sum( const VEC_TYPE& object_ ) : object(object_.get_ref()){} 00036 00037 STORAGE get() const { 00038 STORAGE result = 0 ; 00039 int n = object.size() ; 00040 STORAGE current ; 00041 for( int i=0; i<n; i++){ 00042 current = object[i] ; 00043 if( Rcpp::traits::is_na<RTYPE>(current) ) 00044 return Rcpp::traits::get_na<RTYPE>() ; 00045 result += current ; 00046 } 00047 return result ; 00048 } 00049 private: 00050 const VEC_EXT& object ; 00051 } ; 00052 // RTYPE = REALSXP 00053 template <bool NA, typename T> 00054 class Sum<REALSXP,NA,T> : public Lazy< double , Sum<REALSXP,NA,T> > { 00055 public: 00056 typedef typename Rcpp::VectorBase<REALSXP,NA,T> VEC_TYPE ; 00057 typedef typename Rcpp::traits::Extractor< REALSXP, NA, T>::type VEC_EXT ; 00058 00059 Sum( const VEC_TYPE& object_ ) : object(object_.get_ref()){} 00060 00061 double get() const { 00062 double result = 0 ; 00063 int n = object.size() ; 00064 for( int i=0; i<n; i++){ 00065 result += object[i] ; 00066 } 00067 return result ; 00068 } 00069 private: 00070 const VEC_EXT& object ; 00071 } ; 00072 00073 00074 template <int RTYPE, typename T> 00075 class Sum<RTYPE,false,T> : public Lazy< typename Rcpp::traits::storage_type<RTYPE>::type , Sum<RTYPE,false,T> > { 00076 public: 00077 typedef typename Rcpp::VectorBase<RTYPE,false,T> VEC_TYPE ; 00078 typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ; 00079 typedef typename Rcpp::traits::Extractor< RTYPE, false, T>::type VEC_EXT ; 00080 00081 Sum( const VEC_TYPE& object_ ) : object(object_.get_ref()){} 00082 00083 STORAGE get() const { 00084 STORAGE result = 0 ; 00085 int n = object.size() ; 00086 for( int i=0; i<n; i++){ 00087 result += object[i] ; 00088 } 00089 return result ; 00090 } 00091 private: 00092 const VEC_EXT& object ; 00093 } ; 00094 00095 } // sugar 00096 00097 template <bool NA, typename T> 00098 inline sugar::Sum<INTSXP,NA,T> sum( const VectorBase<INTSXP,NA,T>& t){ 00099 return sugar::Sum<INTSXP,NA,T>( t ) ; 00100 } 00101 00102 template <bool NA, typename T> 00103 inline sugar::Sum<REALSXP,NA,T> sum( const VectorBase<REALSXP,NA,T>& t){ 00104 return sugar::Sum<REALSXP,NA,T>( t ) ; 00105 } 00106 00107 template <bool NA, typename T> 00108 inline sugar::Sum<LGLSXP,NA,T> sum( const VectorBase<LGLSXP,NA,T>& t){ 00109 return sugar::Sum<LGLSXP,NA,T>( t ) ; 00110 } 00111 00112 } // Rcpp 00113 #endif 00114