Rcpp Version 0.9.10
pow.h
Go to the documentation of this file.
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
00002 //
00003 // pow.h: Rcpp R/C++ interface class library -- pow
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__pow_h
00023 #define Rcpp__sugar__pow_h
00024 
00025 namespace Rcpp{
00026 namespace sugar{
00027 
00028 template <bool NA, int RTYPE, typename EXPONENT_TYPE>
00029 class pow__impl{
00030 public:
00031         typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
00032         pow__impl( EXPONENT_TYPE exponent_) : exponent(exponent_){}
00033         
00034         inline double get( STORAGE x) const {
00035                 return Rcpp::traits::is_na<RTYPE>(x) ? NA_INTEGER : ::pow(x, exponent) ;
00036         }
00037 private:
00038         EXPONENT_TYPE exponent ;
00039 } ;
00040 
00041 template <int RTYPE, typename EXPONENT_TYPE>
00042 class pow__impl<false,RTYPE,EXPONENT_TYPE>{
00043 public:
00044         typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
00045         pow__impl( EXPONENT_TYPE exponent_) : exponent(exponent_){}
00046         
00047         inline double get( STORAGE x) const {
00048                 return ::pow(x, exponent) ;
00049         }
00050 private:
00051         EXPONENT_TYPE exponent ;
00052 } ;
00053 
00054         
00055 template <int RTYPE, bool NA, typename T, typename EXPONENT_TYPE>
00056 class Pow : public Rcpp::VectorBase< REALSXP ,NA, Pow<RTYPE,NA,T,EXPONENT_TYPE> > {
00057 public:
00058         typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
00059         typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
00060         typedef pow__impl<NA,RTYPE, typename Rcpp::traits::remove_const<EXPONENT_TYPE>::type > OPERATOR ;
00061         
00062         Pow( const VEC_TYPE& object_, EXPONENT_TYPE exponent ) : 
00063                 object(object_), op(exponent) {}
00064         
00065         inline double operator[]( int i ) const {
00066                 return op.get( object[i] );
00067         }
00068         inline int size() const { return object.size() ; }
00069                  
00070 private:
00071         const VEC_TYPE& object ;
00072         OPERATOR op ;
00073 } ;
00074         
00075 } // sugar
00076 
00077 template <bool NA, typename T, typename EXPONENT_TYPE >
00078 inline sugar::Pow<INTSXP,NA,T,EXPONENT_TYPE> pow( 
00079         const VectorBase<INTSXP,NA,T>& t, 
00080         EXPONENT_TYPE exponent 
00081 ){
00082         return sugar::Pow<INTSXP,NA,T,EXPONENT_TYPE>( t , exponent ) ;
00083 }
00084 
00085 template <bool NA, typename T, typename EXPONENT_TYPE>
00086 inline sugar::Pow<REALSXP,NA,T,EXPONENT_TYPE> pow( 
00087         const VectorBase<REALSXP,NA,T>& t, 
00088         EXPONENT_TYPE exponent
00089 ){
00090         return sugar::Pow<REALSXP,NA,T,EXPONENT_TYPE>( t, exponent ) ;
00091 }
00092 
00093 
00094 } // Rcpp
00095 #endif
00096 
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines