Rcpp Version 1.0.9
SugarMath.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // SugarBlock.h: Rcpp R/C++ interface class library -- sugar functions
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_SUGARMATH_H
23 #define RCPP_SUGAR_SUGARMATH_H
24 
25 namespace Rcpp{
26 namespace sugar{
27 
28 template <bool NA, typename RESULT_TYPE, typename U1, typename T1, typename FunPtr>
30  Rcpp::traits::r_sexptype_traits<RESULT_TYPE>::rtype ,
31  NA,
32  SugarMath_1<NA,RESULT_TYPE,U1,T1,FunPtr>
33  > {
34 public:
35 
37 
38  SugarMath_1( FunPtr ptr_, const VEC_TYPE & vec_) : ptr(ptr_), vec(vec_){}
39 
40  inline RESULT_TYPE operator[]( R_xlen_t i) const {
41  U1 x = vec[i] ;
42  if( ISNAN(x) ) return x;
43  return ptr( x ) ;
44  }
45  inline R_xlen_t size() const { return vec.size() ; }
46 
47 private:
48  FunPtr ptr ;
49  const VEC_TYPE& vec ;
50 };
51 
52 template <bool NA, typename RESULT_TYPE, typename T1, typename FunPtr>
53 class SugarMath_1<NA,RESULT_TYPE,int,T1,FunPtr> : public Rcpp::VectorBase<
54  Rcpp::traits::r_sexptype_traits<RESULT_TYPE>::rtype ,
55  NA,
56  SugarMath_1<NA,RESULT_TYPE,int,T1,FunPtr>
57  > {
58 public:
60 
61  SugarMath_1( FunPtr ptr_, const VEC_TYPE & vec_) : ptr(ptr_), vec(vec_){}
62 
63  inline RESULT_TYPE operator[]( R_xlen_t i) const {
64  int x = vec[i] ;
66  return ptr( x ) ;
67  }
68  inline R_xlen_t size() const { return vec.size() ; }
69 
70 private:
71  FunPtr ptr ;
72  const VEC_TYPE& vec ;
73 };
74 
75 template <typename RESULT_TYPE, typename T1, typename FunPtr>
76 class SugarMath_1<false,RESULT_TYPE,int,T1,FunPtr> : public
78  Rcpp::traits::r_sexptype_traits<RESULT_TYPE>::rtype ,
79  false,
80  SugarMath_1<false,RESULT_TYPE,int,T1,FunPtr>
81 > {
82 public:
84  SugarMath_1( FunPtr ptr_, const VEC_TYPE & vec_) : ptr(ptr_), vec(vec_){}
85 
86  inline RESULT_TYPE operator[]( R_xlen_t i) const {
87  return ptr( vec[i] ) ;
88  }
89  inline R_xlen_t size() const { return vec.size() ; }
90 
91 private:
92  FunPtr ptr ;
93  const VEC_TYPE& vec ;
94 };
95 
96 
97 } // sugar
98 } // Rcpp
99 
100 #define SUGAR_MATH_1(__NAME__,__SYMBOL__) \
101  namespace Rcpp{ \
102  template <bool NA, typename T> \
103  inline sugar::SugarMath_1<NA,double,double,T, double (*)(double) > \
104  __NAME__( \
105  const VectorBase<REALSXP,NA,T>& t \
106  ){ \
107  return sugar::SugarMath_1<NA,double,double,T, double (*)(double)>( \
108  &__SYMBOL__ , t \
109  ) ; \
110  } \
111  inline sugar::SugarMath_1<true,double,double,NumericVector,double(*)(double)> \
112  __NAME__( SEXP x){ return __NAME__( NumericVector( x ) ) ; } \
113  template <bool NA, typename T> \
114  inline sugar::SugarMath_1<NA,double,int,T, double (*)(double) > \
115  __NAME__( \
116  const VectorBase<INTSXP,NA,T>& t \
117  ){ \
118  return sugar::SugarMath_1<NA,double,int,T, double (*)(double)>( \
119  &__SYMBOL__ , t \
120  ) ; \
121  } \
122  }
123 
124 #endif
R_xlen_t size() const
Definition: VectorBase.h:49
SugarMath_1(FunPtr ptr_, const VEC_TYPE &vec_)
Definition: SugarMath.h:61
SugarMath_1(FunPtr ptr_, const VEC_TYPE &vec_)
Definition: SugarMath.h:38
RESULT_TYPE operator[](R_xlen_t i) const
Definition: SugarMath.h:40
const VEC_TYPE & vec
Definition: SugarMath.h:49
R_xlen_t size() const
Definition: SugarMath.h:45
Rcpp::VectorBase< Rcpp::traits::r_sexptype_traits< RESULT_TYPE >::rtype,NA, T1 > VEC_TYPE
Definition: SugarMath.h:36
double get_na< REALSXP >()
Definition: get_na.h:39
bool is_na< INTSXP >(int x)
Definition: is_na.h:37
Rcpp API.
Definition: algo.h:28
static Na_Proxy NA
Definition: Na_Proxy.h:52