Rcpp Version 0.9.10
outer.h
Go to the documentation of this file.
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
00002 //
00003 // outer.h: Rcpp R/C++ interface class library -- outer
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__outer_h
00023 #define Rcpp__sugar__outer_h
00024 
00025 namespace Rcpp{
00026 namespace sugar{
00027 
00028 template <int RTYPE, 
00029           bool LHS_NA, typename LHS_T,
00030           bool RHS_NA, typename RHS_T,
00031           typename Function >
00032 class Outer : public MatrixBase< 
00033     Rcpp::traits::r_sexptype_traits<
00034         typename ::Rcpp::traits::result_of<Function>::type
00035         >::rtype , 
00036             true ,
00037     Outer<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T,Function>
00038 > {
00039 public:
00040     typedef typename ::Rcpp::traits::result_of<Function>::type result_type ;
00041     const static int RESULT_R_TYPE = 
00042         Rcpp::traits::r_sexptype_traits<result_type>::rtype ;
00043     
00044     typedef Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T> LHS_TYPE ;
00045     typedef Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T> RHS_TYPE ;
00046         
00047     typedef Rcpp::internal::LazyVector<LHS_TYPE> LHS_LAZY ;
00048     typedef Rcpp::internal::LazyVector<RHS_TYPE> RHS_LAZY ;
00049         
00050     typedef typename Rcpp::traits::r_vector_element_converter<RESULT_R_TYPE>::type converter_type ;
00051     typedef typename Rcpp::traits::storage_type<RESULT_R_TYPE>::type STORAGE ;
00052         
00053     Outer( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_, Function fun_ ) : 
00054         lhs(lhs_), rhs(rhs_), fun(fun_), nr(lhs_.size()), nc(rhs_.size()) {}
00055         
00056     inline STORAGE operator()( int i, int j ) const {
00057         return converter_type::get( fun( lhs[i], rhs[j] ) );
00058     }
00059         
00060     inline int size() const { return nr * nc ; }
00061     inline int nrow() const { return nr; }
00062     inline int ncol() const { return nc; }
00063                  
00064 private:
00065               
00066     LHS_LAZY lhs ;
00067     RHS_LAZY rhs ;
00068         
00069     Function fun ;
00070     int nr, nc ;
00071 } ;
00072         
00073 } // sugar
00074 
00075 template <int RTYPE, 
00076           bool LHS_NA, typename LHS_T,
00077           bool RHS_NA, typename RHS_T,
00078           typename Function >
00079 inline sugar::Outer<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T,Function> 
00080 outer( 
00081       const Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T>& lhs,
00082       const Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T>& rhs,
00083       Function fun ){
00084 
00085     return sugar::Outer<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T,Function>( lhs, rhs, fun ) ;
00086 }
00087 
00088 } // Rcpp
00089 
00090 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines