Rcpp Version 1.0.9
sapply.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // sapply.h: Rcpp R/C++ interface class library -- sapply
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__sapply_h
23 #define Rcpp__sugar__sapply_h
24 
25 #if defined(RCPP_USING_CXX0X_OR_LATER)
26  #include <type_traits> // ::std::result_of
27 #endif
28 
29 namespace Rcpp{
30 namespace sugar{
31 
32 template <typename Function, typename SugarExpression>
34 {
35 #if defined(RCPP_USING_CXX0X_OR_LATER)
36  typedef typename ::std::result_of<Function(typename SugarExpression::stored_type)>::type type;
37 #else
38  typedef typename ::Rcpp::traits::result_of<Function>::type type;
39 #endif
40 } ;
41 
42 // template <typename Function, typename SugarExpression>
43 // using sapply_application_result_of_t = typename sapply_application_result_of<Function, SugarExpression>::type;
44 
45 template <int RTYPE, bool NA, typename T, typename Function, bool NO_CONVERSION>
46 class Sapply : public VectorBase<
47  Rcpp::traits::r_sexptype_traits<
48  typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type
49  >::rtype ,
50  true ,
51  Sapply<RTYPE,NA,T,Function,NO_CONVERSION>
52 > {
53 public:
54  typedef typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type result_type ;
55  const static int RESULT_R_TYPE =
57 
61 
63 
64  Sapply( const VEC& vec_, Function fun_ ) : vec(vec_.get_ref()), fun(fun_){
65  RCPP_DEBUG_1( "Sapply With Converter = %s", DEMANGLE(Sapply) )
66  RCPP_DEBUG_1( "Sapply Converter = %s", DEMANGLE(converter_type) )
67  }
68 
69  inline STORAGE operator[]( R_xlen_t i ) const {
70  STORAGE res = converter_type::get( fun( vec[i] ) );
71  return res ;
72  }
73  inline R_xlen_t size() const { return vec.size() ; }
74 
75 private:
76  const EXT& vec ;
78 
79 } ;
80 
81 
82 template <int RTYPE, bool NA, typename T, typename Function>
83 class Sapply<RTYPE,NA,T,Function,true> : public VectorBase<
84  Rcpp::traits::r_sexptype_traits<
85  typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type
86  >::rtype ,
87  true ,
88  Sapply<RTYPE,NA,T,Function,true>
89 > {
90 public:
91  typedef typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type result_type ;
92  const static int RESULT_R_TYPE =
94 
97 
99 
100  Sapply( const VEC& vec_, Function fun_ ) : vec(vec_.get_ref()), fun(fun_){
101  RCPP_DEBUG_1( "Sapply = %s", DEMANGLE(Sapply) )
102  }
103 
104  inline STORAGE operator[]( R_xlen_t i ) const {
105  return fun( vec[i] ) ;
106  }
107  inline R_xlen_t size() const { return vec.size() ; }
108 
109 private:
110  const EXT& vec ;
112 
113 } ;
114 
115 
116 } // sugar
117 
118 template <int RTYPE, bool NA, typename T, typename Function >
119 inline sugar::Sapply<
120  RTYPE,NA,T,Function,
122  typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type ,
124  >::value
125 >
127  return sugar::Sapply<RTYPE,NA,T,Function,
129  typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type ,
131  >::value >( t, fun ) ;
132 }
133 
134 } // Rcpp
135 
136 #endif
static target get(const T &input)
Definition: converter.h:33
Rcpp::traits::storage_type< RESULT_R_TYPE >::type STORAGE
Definition: sapply.h:96
Rcpp::VectorBase< RTYPE, NA, T > VEC
Definition: sapply.h:95
::Rcpp::sugar::sapply_application_result_of< Function, T >::type result_type
Definition: sapply.h:91
Rcpp::traits::Extractor< RTYPE, NA, T >::type EXT
Definition: sapply.h:98
Sapply(const VEC &vec_, Function fun_)
Definition: sapply.h:100
::Rcpp::sugar::sapply_application_result_of< Function, T >::type result_type
Definition: sapply.h:54
Rcpp::VectorBase< RTYPE, NA, T > VEC
Definition: sapply.h:58
R_xlen_t size() const
Definition: sapply.h:73
static const int RESULT_R_TYPE
Definition: sapply.h:55
Rcpp::traits::storage_type< RESULT_R_TYPE >::type STORAGE
Definition: sapply.h:60
Rcpp::traits::Extractor< RTYPE, NA, T >::type EXT
Definition: sapply.h:62
Function fun
Definition: sapply.h:77
Rcpp::traits::r_vector_element_converter< RESULT_R_TYPE >::type converter_type
Definition: sapply.h:59
STORAGE operator[](R_xlen_t i) const
Definition: sapply.h:69
Sapply(const VEC &vec_, Function fun_)
Definition: sapply.h:64
const EXT & vec
Definition: sapply.h:76
#define RCPP_DEBUG_1(fmt, MSG)
Definition: debug.h:44
#define DEMANGLE(__TYPE__)
Definition: exceptions.h:382
Rcpp API.
Definition: algo.h:28
Function_Impl< PreserveStorage > Function
Definition: Function.h:122
sugar::Sapply< RTYPE, NA, T, Function, traits::same_type< typename ::Rcpp::sugar::sapply_application_result_of< Function, T >::type, typename Rcpp::traits::storage_type< traits::r_sexptype_traits< typename ::Rcpp::sugar::sapply_application_result_of< Function, T >::type >::rtype >::type >::value > sapply(const Rcpp::VectorBase< RTYPE, NA, T > &t, Function fun)
Definition: sapply.h:126
static Na_Proxy NA
Definition: Na_Proxy.h:52
::Rcpp::traits::result_of< Function >::type type
Definition: sapply.h:38