Rcpp Version 0.9.10
sapply.h
Go to the documentation of this file.
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
00002 //
00003 // sapply.h: Rcpp R/C++ interface class library -- sapply
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__sapply_h
00023 #define Rcpp__sugar__sapply_h
00024 
00025 namespace Rcpp{
00026 namespace sugar{
00027 
00028 template <int RTYPE, bool NA, typename T, typename Function, bool NO_CONVERSION>
00029 class Sapply : public VectorBase< 
00030         Rcpp::traits::r_sexptype_traits<
00031                 typename ::Rcpp::traits::result_of<Function>::type
00032         >::rtype , 
00033         true ,
00034         Sapply<RTYPE,NA,T,Function,NO_CONVERSION>
00035 > {
00036 public:
00037         typedef typename ::Rcpp::traits::result_of<Function>::type result_type ;
00038         const static int RESULT_R_TYPE = 
00039                 Rcpp::traits::r_sexptype_traits<result_type>::rtype ;
00040         
00041         typedef Rcpp::VectorBase<RTYPE,NA,T> VEC ;
00042         typedef typename Rcpp::traits::r_vector_element_converter<RESULT_R_TYPE>::type converter_type ;
00043         typedef typename Rcpp::traits::storage_type<RESULT_R_TYPE>::type STORAGE ;
00044         
00045         typedef typename Rcpp::traits::Extractor< RTYPE, NA, T>::type EXT ;
00046         
00047         Sapply( const VEC& vec_, Function fun_ ) : vec(vec_.get_ref()), fun(fun_){}
00048         
00049         inline STORAGE operator[]( int i ) const {
00050                 return converter_type::get( fun( vec[i] ) );
00051         }
00052         inline int size() const { return vec.size() ; }
00053                  
00054 private:
00055         const EXT& vec ;
00056         Function fun ;
00057         
00058 } ;
00059 
00060 
00061 template <int RTYPE, bool NA, typename T, typename Function>
00062 class Sapply<RTYPE,NA,T,Function,true> : public VectorBase< 
00063         Rcpp::traits::r_sexptype_traits<
00064                 typename ::Rcpp::traits::result_of<Function>::type
00065         >::rtype , 
00066         true ,
00067         Sapply<RTYPE,NA,T,Function,true>
00068 > {
00069 public:
00070         typedef typename ::Rcpp::traits::result_of<Function>::type result_type ;
00071         const static int RESULT_R_TYPE = 
00072                 Rcpp::traits::r_sexptype_traits<result_type>::rtype ;
00073         
00074         typedef Rcpp::VectorBase<RTYPE,NA,T> VEC ;
00075         typedef typename Rcpp::traits::storage_type<RESULT_R_TYPE>::type STORAGE ;
00076         
00077         typedef typename Rcpp::traits::Extractor< RTYPE, NA, T>::type EXT ;
00078         
00079         Sapply( const VEC& vec_, Function fun_ ) : vec(vec_.get_ref()), fun(fun_){}
00080         
00081         inline STORAGE operator[]( int i ) const {
00082                 return fun( vec[i] ) ;
00083         }
00084         inline int size() const { return vec.size() ; }
00085                  
00086 private:
00087         const EXT& vec ;
00088         Function fun ;
00089         
00090 } ;
00091         
00092 
00093 } // sugar
00094 
00095 template <int RTYPE, bool NA, typename T, typename Function >
00096 inline sugar::Sapply<
00097     RTYPE,NA,T,Function, 
00098     traits::same_type< 
00099         typename ::Rcpp::traits::result_of<Function>::type ,  
00100         typename Rcpp::traits::storage_type< traits::r_sexptype_traits< typename ::Rcpp::traits::result_of<Function>::type >::rtype >::type
00101     >::value
00102 > 
00103 sapply( const Rcpp::VectorBase<RTYPE,NA,T>& t, Function fun ){
00104         return sugar::Sapply<RTYPE,NA,T,Function, 
00105         traits::same_type< 
00106         typename ::Rcpp::traits::result_of<Function>::type ,  
00107         typename Rcpp::traits::storage_type< traits::r_sexptype_traits< typename ::Rcpp::traits::result_of<Function>::type >::rtype >::type
00108     >::value >( t, fun ) ;
00109 }
00110 
00111 } // Rcpp
00112 
00113 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines