File: mapply_2.h

package info (click to toggle)
rcpp 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,480 kB
  • sloc: cpp: 27,436; ansic: 7,778; sh: 53; makefile: 2
file content (147 lines) | stat: -rw-r--r-- 4,417 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
//
// mapply_2.h: Rcpp R/C++ interface class library -- mapply_2
//
// Copyright (C) 2012 - 2024 Dirk Eddelbuettel and Romain Francois
// Copyright (C) 2025        Dirk Eddelbuettel, Romain Francois and IƱaki Ucar
//
// This file is part of Rcpp.
//
// Rcpp is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Rcpp is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.

#ifndef Rcpp__sugar__mapply_2_h
#define Rcpp__sugar__mapply_2_h

namespace Rcpp{
namespace sugar{

template <int RTYPE,
    bool NA_1, typename T_1,
    bool NA_2, typename T_2,
    typename Function
>
class Mapply_2 : public VectorBase<
	Rcpp::traits::r_sexptype_traits<
		typename ::Rcpp::traits::result_of<Function, T_1, T_2>::type
	>::rtype ,
	true ,
	Mapply_2<RTYPE,NA_1,T_1,NA_2,T_2,Function>
> {
public:
	typedef typename ::Rcpp::traits::result_of<Function, T_1, T_2>::type result_type ;

	Mapply_2( const T_1& vec_1_, const T_2& vec_2_, Function fun_ ) :
		vec_1(vec_1_), vec_2(vec_2_), fun(fun_){}

        inline result_type operator[]( R_xlen_t i ) const {
		return fun( vec_1[i], vec_2[i] );
	}
        inline R_xlen_t size() const { return vec_1.size() ; }

private:
	const T_1& vec_1 ;
	const T_2& vec_2 ;
	Function fun ;
} ;

template <int RTYPE,
    bool NA_1, typename T_1,
    typename PRIM_2 ,
    typename Function
>
class Mapply_2_Vector_Primitive : public
    VectorBase<
	    Rcpp::traits::r_sexptype_traits<
	    	typename ::Rcpp::traits::result_of<Function, T_1>::type
	    >::rtype ,
	    true ,
	    Mapply_2_Vector_Primitive<RTYPE,NA_1,T_1,PRIM_2,Function>
    >
{
public:
	typedef typename ::Rcpp::traits::result_of<Function, T_1>::type result_type ;

	Mapply_2_Vector_Primitive( const T_1& vec_1_, PRIM_2 prim_2_, Function fun_ ) :
		vec_1(vec_1_), prim_2(prim_2_), fun(fun_){}

        inline result_type operator[]( R_xlen_t i ) const {
		return fun( vec_1[i], prim_2 );
	}
        inline R_xlen_t size() const { return vec_1.size() ; }

private:
	const T_1& vec_1 ;
	PRIM_2 prim_2 ;
	Function fun ;
} ;

template <int RTYPE,
    typename PRIM_1,
    bool NA_2, typename T_2,
    typename Function
>
class Mapply_2_Primitive_Vector : public
    VectorBase<
	    Rcpp::traits::r_sexptype_traits<
	    	typename ::Rcpp::traits::result_of<Function, T_2>::type
	    >::rtype ,
	    true ,
	    Mapply_2_Primitive_Vector<RTYPE,PRIM_1,NA_2,T_2,Function>
    >
{
public:
	typedef typename ::Rcpp::traits::result_of<Function, T_2>::type result_type ;

	Mapply_2_Primitive_Vector( PRIM_1 prim_1_, const T_2& vec_2_, Function fun_ ) :
		prim_1(prim_1_), vec_2(vec_2_), fun(fun_){}

        inline result_type operator[]( R_xlen_t i ) const {
		return fun( prim_1, vec_2[i] );
	}
        inline R_xlen_t size() const { return vec_2.size() ; }

private:
	PRIM_1 prim_1 ;
    const T_2& vec_2 ;
	Function fun ;
} ;



} // sugar


template <int RTYPE, bool NA_1, typename T_1, bool NA_2, typename T_2, typename Function >
inline sugar::Mapply_2<RTYPE,NA_1,T_1,NA_2,T_2,Function>
mapply( const Rcpp::VectorBase<RTYPE,NA_1,T_1>& t1, const Rcpp::VectorBase<RTYPE,NA_2,T_2>& t2, Function fun ){
	return sugar::Mapply_2<RTYPE,NA_1,T_1,NA_2,T_2,Function>( t1.get_ref(), t2.get_ref(), fun ) ;
}

template <int RTYPE, bool NA_1, typename T_1, typename Function >
inline sugar::Mapply_2_Vector_Primitive<RTYPE,NA_1,T_1,double,Function>
mapply( const Rcpp::VectorBase<RTYPE,NA_1,T_1>& t1, double t2, Function fun ){
	return sugar::Mapply_2_Vector_Primitive<RTYPE,NA_1,T_1,double,Function>( t1.get_ref(), t2, fun ) ;
}

template <int RTYPE, bool NA_2, typename T_2, typename Function >
inline sugar::Mapply_2_Primitive_Vector<RTYPE,double, NA_2,T_2,Function>
mapply( double t1, const Rcpp::VectorBase<RTYPE,NA_2,T_2>& t2, Function fun ){
	return sugar::Mapply_2_Primitive_Vector<RTYPE,double, NA_2,T_2,Function>( t1, t2.get_ref(), fun ) ;
}



} // Rcpp

#endif