|
Rcpp Version 0.9.10
|
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- 00002 // 00003 // SugarBlock.h: Rcpp R/C++ interface class library -- sugar functions 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_VECTORIZEDMATH_H 00023 #define RCPP_SUGAR_VECTORIZEDMATH_H 00024 00025 namespace Rcpp{ 00026 namespace sugar{ 00027 00028 extern "C" typedef double (*DDFun)(double); 00029 00030 template <DDFun Func, bool NA, typename VEC> 00031 class Vectorized : public VectorBase<REALSXP, NA, Vectorized<Func,NA,VEC> >{ 00032 public: 00033 typedef typename Rcpp::VectorBase<REALSXP,NA,VEC> VEC_TYPE ; 00034 typedef typename Rcpp::traits::Extractor<REALSXP,NA,VEC>::type VEC_EXT ; 00035 00036 Vectorized( const VEC_TYPE& object_) : object( object_.get_ref() ){} 00037 inline double operator[]( int i) const { 00038 return Func( object[i] ) ; 00039 } 00040 inline int size() const { return object.size(); } 00041 00042 private: 00043 const VEC_EXT& object ; 00044 } ; 00045 00046 template <DDFun Func, bool NA, typename VEC> 00047 class Vectorized_INTSXP : public VectorBase<REALSXP, NA, Vectorized_INTSXP<Func,NA,VEC> >{ 00048 public: 00049 typedef typename Rcpp::VectorBase<INTSXP,NA,VEC> VEC_TYPE ; 00050 typedef typename Rcpp::traits::Extractor<INTSXP,NA,VEC>::type VEC_EXT ; 00051 00052 Vectorized_INTSXP( const VEC_TYPE& object_) : object( object_.get_ref() ){} 00053 inline double operator[]( int i) const { 00054 int x = object[i] ; 00055 if( x == NA_INTEGER ) return NA_REAL ; 00056 return Func( x ) ; 00057 } 00058 inline int size() const { return object.size(); } 00059 00060 private: 00061 const VEC_EXT& object ; 00062 } ; 00063 template <DDFun Func, typename VEC> 00064 class Vectorized_INTSXP<Func,false,VEC> : 00065 public VectorBase<REALSXP,false, Vectorized_INTSXP<Func,false,VEC> >{ 00066 public: 00067 typedef typename Rcpp::VectorBase<INTSXP,false,VEC> VEC_TYPE ; 00068 typedef typename Rcpp::traits::Extractor<INTSXP,false,VEC>::type VEC_EXT ; 00069 00070 Vectorized_INTSXP( const VEC_TYPE& object_) : object( object_.get_ref() ){} 00071 inline double operator[]( int i) const { 00072 return Func( object[i] ) ; 00073 } 00074 inline int size() const { return object.size(); } 00075 00076 private: 00077 const VEC_EXT& object ; 00078 } ; 00079 00080 } // sugar 00081 } // Rcpp 00082 00083 #define VECTORIZED_MATH_1(__NAME__,__SYMBOL__) \ 00084 namespace Rcpp{ \ 00085 template <bool NA, typename T> \ 00086 inline sugar::Vectorized<__SYMBOL__,NA,T> \ 00087 __NAME__( const VectorBase<REALSXP,NA,T>& t ){ \ 00088 return sugar::Vectorized<__SYMBOL__,NA,T>( t ) ; \ 00089 } \ 00090 inline sugar::Vectorized<__SYMBOL__,true,NumericVector> \ 00091 __NAME__( SEXP x){ return __NAME__( NumericVector( x ) ) ; } \ 00092 template <bool NA, typename T> \ 00093 inline sugar::Vectorized_INTSXP<__SYMBOL__,NA,T> \ 00094 __NAME__( const VectorBase<INTSXP,NA,T>& t ){ \ 00095 return sugar::Vectorized_INTSXP<__SYMBOL__,NA,T>( t ) ; \ 00096 } \ 00097 } 00098 00099 00100 #endif