Rcpp Version 0.9.10
SugarMath.h
Go to the documentation of this file.
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
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_SUGARMATH_H
00023 #define RCPP_SUGAR_SUGARMATH_H
00024 
00025 namespace Rcpp{
00026 namespace sugar{
00027         
00028 template <bool NA, typename OUT, typename U1, typename T1, typename FunPtr>
00029 class SugarMath_1 : public Rcpp::VectorBase< 
00030         Rcpp::traits::r_sexptype_traits<OUT>::rtype , 
00031         NA, 
00032         SugarMath_1<NA,OUT,U1,T1,FunPtr>
00033         > {
00034 public:
00035         
00036         typedef Rcpp::VectorBase< Rcpp::traits::r_sexptype_traits<OUT>::rtype ,NA,T1> VEC_TYPE ;
00037         
00038         SugarMath_1( FunPtr ptr_, const VEC_TYPE & vec_) : ptr(ptr_), vec(vec_){}
00039         
00040         inline OUT operator[]( int i) const { 
00041                 U1 x = vec[i] ;
00042                 if( ISNAN(x) ) return x;
00043                 return ptr( x ) ;
00044         }
00045         inline int size() const { return vec.size() ; }
00046         
00047 private:
00048         FunPtr ptr ;
00049         const VEC_TYPE& vec ;
00050 };
00051 
00052 template <bool NA, typename OUT, typename T1, typename FunPtr>
00053 class SugarMath_1<NA,OUT,int,T1,FunPtr> : public Rcpp::VectorBase< 
00054         Rcpp::traits::r_sexptype_traits<OUT>::rtype , 
00055         NA, 
00056         SugarMath_1<NA,OUT,int,T1,FunPtr>
00057         > {
00058 public:
00059         typedef Rcpp::VectorBase< INTSXP ,NA,T1> VEC_TYPE ;
00060         
00061         SugarMath_1( FunPtr ptr_, const VEC_TYPE & vec_) : ptr(ptr_), vec(vec_){}
00062         
00063         inline OUT operator[]( int i) const { 
00064                 int x = vec[i] ;
00065                 if( Rcpp::traits::is_na<INTSXP>(x) ) return Rcpp::traits::get_na<REALSXP>( ) ;
00066                 return ptr( x ) ;
00067         }
00068         inline int size() const { return vec.size() ; }
00069         
00070 private:
00071         FunPtr ptr ;
00072         const VEC_TYPE& vec ;
00073 };
00074 
00075 template <typename OUT, typename T1, typename FunPtr>
00076 class SugarMath_1<false,OUT,int,T1,FunPtr> : public 
00077 Rcpp::VectorBase< 
00078         Rcpp::traits::r_sexptype_traits<OUT>::rtype , 
00079         false, 
00080         SugarMath_1<false,OUT,int,T1,FunPtr>
00081 > {
00082 public:
00083         typedef Rcpp::VectorBase< INTSXP ,false,T1> VEC_TYPE ;
00084         SugarMath_1( FunPtr ptr_, const VEC_TYPE & vec_) : ptr(ptr_), vec(vec_){}
00085         
00086         inline OUT operator[]( int i) const { 
00087                 return ptr( vec[i] ) ;
00088         }
00089         inline int size() const { return vec.size() ; }
00090         
00091 private:
00092         FunPtr ptr ;
00093         const VEC_TYPE& vec ;
00094 };
00095 
00096 
00097 } // sugar
00098 } // Rcpp
00099 
00100 #define SUGAR_MATH_1(__NAME__,__SYMBOL__)                                    \
00101         namespace Rcpp{                                                          \
00102         template <bool NA, typename T>                                           \
00103         inline sugar::SugarMath_1<NA,double,double,T, double (*)(double) >       \
00104         __NAME__(                                                                \
00105                 const VectorBase<REALSXP,NA,T>& t                                    \
00106         ){                                                                       \
00107                 return sugar::SugarMath_1<NA,double,double,T, double (*)(double)>(   \
00108                         &__SYMBOL__ , t                                                   \
00109                 ) ;                                                                  \
00110         }                                                                        \
00111         inline sugar::SugarMath_1<true,double,double,NumericVector,double(*)(double)>   \
00112         __NAME__( SEXP x){ return __NAME__( NumericVector( x ) ) ; }             \
00113         template <bool NA, typename T>                                           \
00114         inline sugar::SugarMath_1<NA,double,int,T, double (*)(double) >          \
00115         __NAME__(                                                                \
00116                 const VectorBase<INTSXP,NA,T>& t                                     \
00117         ){                                                                       \
00118                 return sugar::SugarMath_1<NA,double,int,T, double (*)(double)>(      \
00119                         &__SYMBOL__ , t                                                   \
00120                 ) ;                                                                  \
00121         }                                                                        \
00122         }
00123 
00124 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines