1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // Function.cpp: Rcpp R/C++ interface class library -- Rcpp::Function unit tests
4 //
5 // Copyright (C) 2013 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 #include <Rcpp.h>
23 using namespace Rcpp ;
24 
25 // [[Rcpp::export]]
function_(SEXP x)26 Function function_(SEXP x){ return Function(x) ; }
27 
28 // [[Rcpp::export]]
function_cons_env(std::string x,SEXP env)29 Function function_cons_env(std::string x, SEXP env) {
30     return Function(x, env);
31 }
32 
33 // [[Rcpp::export]]
function_cons_ns(std::string x,std::string ns)34 Function function_cons_ns(std::string x, std::string ns) {
35     return Function(x, ns);
36 }
37 
38 // [[Rcpp::export]]
function_variadic(Function sort,NumericVector y)39 NumericVector function_variadic(Function sort, NumericVector y){
40     return sort( y, Named("decreasing", true) ) ;
41 }
42 
43 // [[Rcpp::export]]
function_env(Function fun)44 Environment function_env(Function fun){
45     return fun.environment() ;
46 }
47 
48 // [[Rcpp::export]]
function_unarycall(List x)49 IntegerVector function_unarycall(List x){
50     Function len( "length" ) ;
51     IntegerVector output( x.size() ) ;
52     std::transform(
53         	x.begin(), x.end(),
54         	output.begin(),
55         	unary_call<IntegerVector,int>(len)
56     	) ;
57     return output ;
58 }
59 
60 // [[Rcpp::export]]
function_binarycall(List list,IntegerVector vec)61 List function_binarycall(List list,IntegerVector vec){
62     Function pmin( "pmin" ) ;
63     List output( list.size() ) ;
64     std::transform(
65         	list.begin(), list.end(),
66         	vec.begin(),
67         	output.begin(),
68         	binary_call<IntegerVector,int,IntegerVector>(pmin)
69     	) ;
70     return output ;
71 }
72 
73 // [[Rcpp::export]]
function_namespace_env()74 Function function_namespace_env(){
75     Environment ns = Environment::namespace_env( "stats" ) ;
76     Function fun = ns[".asSparse"] ;  // accesses a non-exported function
77     return fun;
78 }
79 
80 // [[Rcpp::export]]
exec(Function f)81 void exec(Function f) { f(); }
82 
83