Rcpp Version 1.0.14
Loading...
Searching...
No Matches
sapply.h
Go to the documentation of this file.
1
2// sapply.h: Rcpp R/C++ interface class library -- sapply
3//
4// Copyright (C) 2010 - 2023 Dirk Eddelbuettel and Romain Francois
5//
6// This file is part of Rcpp.
7//
8// Rcpp is free software: you can redistribute it and/or modify it
9// under the terms of the GNU General Public License as published by
10// the Free Software Foundation, either version 2 of the License, or
11// (at your option) any later version.
12//
13// Rcpp is distributed in the hope that it will be useful, but
14// WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17//
18// You should have received a copy of the GNU General Public License
19// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
20
21#ifndef Rcpp__sugar__sapply_h
22#define Rcpp__sugar__sapply_h
23
24#if defined(RCPP_USING_CXX0X_OR_LATER)
25 #include <type_traits> // ::std::result_of
26#endif
27
28namespace Rcpp{
29namespace sugar{
30
31template <typename Function, typename SugarExpression>
33{
34#if defined(RCPP_USING_CXX0X_OR_LATER)
35 #if __cplusplus < 201703L
36 // deprecated by C++17, removed by C++2020, see https://en.cppreference.com/w/cpp/types/result_of
37 typedef typename ::std::result_of<Function(typename SugarExpression::stored_type)>::type type;
38 #else
39 // since C++17, see https://en.cppreference.com/w/cpp/types/result_of
40 typedef typename ::std::invoke_result<Function, typename SugarExpression::stored_type>::type type;
41 #endif
42#else
43 typedef typename ::Rcpp::traits::result_of<Function>::type type;
44#endif
45} ;
46
47// template <typename Function, typename SugarExpression>
48// using sapply_application_result_of_t = typename sapply_application_result_of<Function, SugarExpression>::type;
49
50template <int RTYPE, bool NA, typename T, typename Function, bool NO_CONVERSION>
51class Sapply : public VectorBase<
52 Rcpp::traits::r_sexptype_traits<
53 typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type
54 >::rtype ,
55 true ,
56 Sapply<RTYPE,NA,T,Function,NO_CONVERSION>
57> {
58public:
59 typedef typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type result_type ;
60 const static int RESULT_R_TYPE =
62
66
68
70 RCPP_DEBUG_1( "Sapply With Converter = %s", DEMANGLE(Sapply) )
71 RCPP_DEBUG_1( "Sapply Converter = %s", DEMANGLE(converter_type) )
72 }
73
74 inline STORAGE operator[]( R_xlen_t i ) const {
75 STORAGE res = converter_type::get( fun( vec[i] ) );
76 return res ;
77 }
78 inline R_xlen_t size() const { return vec.size() ; }
79
80private:
81 const EXT& vec ;
83
84} ;
85
86
87template <int RTYPE, bool NA, typename T, typename Function>
88class Sapply<RTYPE,NA,T,Function,true> : public VectorBase<
89 Rcpp::traits::r_sexptype_traits<
90 typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type
91 >::rtype ,
92 true ,
93 Sapply<RTYPE,NA,T,Function,true>
94> {
95public:
96 typedef typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type result_type ;
97 const static int RESULT_R_TYPE =
99
102
104
106 RCPP_DEBUG_1( "Sapply = %s", DEMANGLE(Sapply) )
107 }
108
109 inline STORAGE operator[]( R_xlen_t i ) const {
110 return fun( vec[i] ) ;
111 }
112 inline R_xlen_t size() const { return vec.size() ; }
113
114private:
115 const EXT& vec ;
117
118} ;
119
120
121} // sugar
122
123template <int RTYPE, bool NA, typename T, typename Function >
124inline sugar::Sapply<
125 RTYPE,NA,T,Function,
127 typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type ,
129 >::value
130>
132 return sugar::Sapply<RTYPE,NA,T,Function,
134 typename ::Rcpp::sugar::sapply_application_result_of<Function, T>::type ,
136 >::value >( t, fun ) ;
137}
138
139} // Rcpp
140
141#endif
static target get(const T &input)
Definition converter.h:33
::Rcpp::sugar::sapply_application_result_of< Function, T >::type result_type
Definition sapply.h:96
Rcpp::traits::Extractor< RTYPE, NA, T >::type EXT
Definition sapply.h:103
Rcpp::traits::storage_type< RESULT_R_TYPE >::type STORAGE
Definition sapply.h:101
Rcpp::VectorBase< RTYPE, NA, T > VEC
Definition sapply.h:100
Sapply(const VEC &vec_, Function fun_)
Definition sapply.h:105
Rcpp::VectorBase< RTYPE, NA, T > VEC
Definition sapply.h:63
R_xlen_t size() const
Definition sapply.h:78
static const int RESULT_R_TYPE
Definition sapply.h:60
Rcpp::traits::Extractor< RTYPE, NA, T >::type EXT
Definition sapply.h:67
Rcpp::traits::storage_type< RESULT_R_TYPE >::type STORAGE
Definition sapply.h:65
::Rcpp::sugar::sapply_application_result_of< Function, T >::type result_type
Definition sapply.h:59
Rcpp::traits::r_vector_element_converter< RESULT_R_TYPE >::type converter_type
Definition sapply.h:64
STORAGE operator[](R_xlen_t i) const
Definition sapply.h:74
Sapply(const VEC &vec_, Function fun_)
Definition sapply.h:69
const EXT & vec
Definition sapply.h:81
#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:131
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:131
T as(SEXP x)
Definition as.h:151
static Na_Proxy NA
Definition Na_Proxy.h:52
::Rcpp::traits::result_of< Function >::type type
Definition sapply.h:43