Rcpp Version 1.1.2
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 - 2024 Dirk Eddelbuettel and Romain Francois
5// Copyright (C) 2025 Dirk Eddelbuettel, Romain Francois and IƱaki Ucar
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
25namespace Rcpp{
26namespace sugar{
27
28template <int RTYPE, bool NA, typename T, typename Function, bool NO_CONVERSION>
29class Sapply : public VectorBase<
30 Rcpp::traits::r_sexptype_traits<
31 typename ::Rcpp::traits::result_of<Function, T>::type
32 >::rtype ,
33 true ,
34 Sapply<RTYPE,NA,T,Function,NO_CONVERSION>
35> {
36public:
37 typedef typename ::Rcpp::traits::result_of<Function, T>::type result_type ;
38 const static int RESULT_R_TYPE =
40
44
46
47 Sapply( const VEC& vec_, Function fun_ ) : vec(vec_.get_ref()), fun(fun_){
48 RCPP_DEBUG_1( "Sapply With Converter = %s", DEMANGLE(Sapply) )
49 RCPP_DEBUG_1( "Sapply Converter = %s", DEMANGLE(converter_type) )
50 }
51
52 inline STORAGE operator[]( R_xlen_t i ) const {
53 STORAGE res = converter_type::get( fun( vec[i] ) );
54 return res ;
55 }
56 inline R_xlen_t size() const { return vec.size() ; }
57
58private:
59 const EXT& vec ;
61
62} ;
63
64
65template <int RTYPE, bool NA, typename T, typename Function>
66class Sapply<RTYPE,NA,T,Function,true> : public VectorBase<
67 Rcpp::traits::r_sexptype_traits<
68 typename ::Rcpp::traits::result_of<Function, T>::type
69 >::rtype ,
70 true ,
71 Sapply<RTYPE,NA,T,Function,true>
72> {
73public:
74 typedef typename ::Rcpp::traits::result_of<Function, T>::type result_type ;
75 const static int RESULT_R_TYPE =
77
80
82
83 Sapply( const VEC& vec_, Function fun_ ) : vec(vec_.get_ref()), fun(fun_){
84 RCPP_DEBUG_1( "Sapply = %s", DEMANGLE(Sapply) )
85 }
86
87 inline STORAGE operator[]( R_xlen_t i ) const {
88 return fun( vec[i] ) ;
89 }
90 inline R_xlen_t size() const { return vec.size() ; }
91
92private:
93 const EXT& vec ;
95
96} ;
97
98
99} // sugar
100
101template <int RTYPE, bool NA, typename T, typename Function >
102inline sugar::Sapply<
103 RTYPE,NA,T,Function,
105 typename ::Rcpp::traits::result_of<Function, T>::type ,
107 >::value
108>
110 return sugar::Sapply<RTYPE,NA,T,Function,
112 typename ::Rcpp::traits::result_of<Function, T>::type ,
114 >::value >( t, fun ) ;
115}
116
117} // Rcpp
118
119#endif
static target get(const T &input)
Definition converter.h:33
Rcpp::traits::Extractor< RTYPE, NA, T >::type EXT
Definition sapply.h:81
Rcpp::traits::storage_type< RESULT_R_TYPE >::type STORAGE
Definition sapply.h:79
::Rcpp::traits::result_of< Function, T >::type result_type
Definition sapply.h:74
Rcpp::VectorBase< RTYPE, NA, T > VEC
Definition sapply.h:78
Sapply(const VEC &vec_, Function fun_)
Definition sapply.h:83
Rcpp::VectorBase< RTYPE, NA, T > VEC
Definition sapply.h:41
R_xlen_t size() const
Definition sapply.h:56
::Rcpp::traits::result_of< Function, T >::type result_type
Definition sapply.h:37
static const int RESULT_R_TYPE
Definition sapply.h:38
Rcpp::traits::Extractor< RTYPE, NA, T >::type EXT
Definition sapply.h:45
Rcpp::traits::storage_type< RESULT_R_TYPE >::type STORAGE
Definition sapply.h:43
Rcpp::traits::r_vector_element_converter< RESULT_R_TYPE >::type converter_type
Definition sapply.h:42
STORAGE operator[](R_xlen_t i) const
Definition sapply.h:52
Sapply(const VEC &vec_, Function fun_)
Definition sapply.h:47
const EXT & vec
Definition sapply.h:59
#define RCPP_DEBUG_1(fmt, MSG)
Definition debug.h:44
#define DEMANGLE(__TYPE__)
Definition exceptions.h:403
Rcpp API.
Definition algo.h:28
Function_Impl< PreserveStorage > Function
Definition Function.h:144
sugar::Sapply< RTYPE, NA, T, Function, traits::same_type< typename ::Rcpp::traits::result_of< Function, T >::type, typename Rcpp::traits::storage_type< traits::r_sexptype_traits< typename ::Rcpp::traits::result_of< Function, T >::type >::rtype >::type >::value > sapply(const Rcpp::VectorBase< RTYPE, NA, T > &t, Function fun)
Definition sapply.h:109
static Na_Proxy NA
Definition Na_Proxy.h:52
::Rcpp::internal::element_converter< RTYPE > type
Definition converter.h:95