|
Rcpp Version 0.9.10
|
00001 #!/usr/bin/r -t 00002 # 00003 # Copyright (C) 2009 - 2010 Dirk Eddelbuettel and Romain Francois 00004 # 00005 # This file is part of Rcpp. 00006 # 00007 # Rcpp is free software: you can redistribute it and/or modify it 00008 # under the terms of the GNU General Public License as published by 00009 # the Free Software Foundation, either version 2 of the License, or 00010 # (at your option) any later version. 00011 # 00012 # Rcpp is distributed in the hope that it will be useful, but 00013 # WITHOUT ANY WARRANTY; without even the implied warranty of 00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 # GNU General Public License for more details. 00016 # 00017 # You should have received a copy of the GNU General Public License 00018 # along with Rcpp. If not, see <http://www.gnu.org/licenses/>. 00019 00020 ## NB This file is mostly historic and predates the unit tests. 00021 ## Yet it still provides useful examples -- but the unitTests/ 00022 ## for vastly larger coverage 00023 00024 suppressMessages(library(Rcpp)) 00025 suppressMessages(library(inline)) 00026 00027 cat("===Doubles\n") 00028 foo <- ' 00029 double d = Rcpp::as<double>(x); 00030 std::cout << "Returning twice the value of " << d << " : "; 00031 return(Rcpp::wrap( 2*d ) ); 00032 ' 00033 funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE) 00034 cat(funx(x=2.123), "\n") 00035 cat(funx(x=2), "\n") 00036 ##funx(x='2') ## throws as expected 00037 ##funx(x=2:3) ## throws as expected 00038 00039 00040 cat("\n===Int\n") 00041 foo <- ' 00042 int i = Rcpp::as<int>(x); 00043 std::cout << "Returning twice the value of " << i << " : "; 00044 return(Rcpp::wrap( 2*i ) ); 00045 ' 00046 funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE) 00047 cat(funx(x=2), "\n") 00048 cat(funx(x=2.2), "\n") 00049 funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE) 00050 cat(funx(x=as.raw(2)), "\n") 00051 00052 cat("\n===String\n") 00053 foo <- ' 00054 std::string s = Rcpp::as<std::string>(x); 00055 std::cout << "Returning twice the value of " << s << " : "; 00056 return(Rcpp::wrap( s+s ) ); 00057 ' 00058 funx <- cfunction(signature(x="character"), foo, Rcpp=TRUE, verbose=FALSE) 00059 cat(funx(x="abc"), "\n") 00060 00061 cat("\n===Raw (bytes)\n") 00062 foo <- ' 00063 Rbyte i = Rcpp::as<Rbyte>(x) ; 00064 std::cout << "Returning twice the value of " << (int)i << " : "; 00065 return(Rcpp::wrap( (Rbyte)(2*i) ) ); 00066 ' 00067 funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE) 00068 cat( funx(x=2), "\n") 00069 funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE) 00070 cat( funx(x=2L), "\n") 00071 funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE) 00072 cat( funx(x=as.raw(2)), "\n") 00073 00074 cat("\n=== logical \n") 00075 foo <- ' 00076 bool b = Rcpp::as<bool>(x); 00077 std::cout << "flip " << ( b ? "TRUE" : "FALSE" ) << " : "; 00078 return(Rcpp::wrap( !b )); 00079 ' 00080 funx <- cfunction(signature(x="logical"), foo, Rcpp=TRUE, verbose=FALSE) 00081 cat( res <- funx(x=TRUE) , "\n") ; stopifnot( !res ) 00082 cat( res <- funx(x=FALSE), "\n" ) ; stopifnot( res) 00083 funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE) 00084 cat( res <- funx(x=2) , "\n") ; stopifnot( !res ) 00085 cat( res <- funx(x=0.0), "\n") ; stopifnot( res) 00086 funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE) 00087 cat( res <- funx(x=2L), "\n") ; stopifnot( !res ) 00088 cat( res <- funx(x=0L), "\n") ; stopifnot( res) 00089 funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE) 00090 cat( res <- funx(x=as.raw(2)), "\n") ; stopifnot( !res ) 00091 cat( res <- funx(x=as.raw(0)), "\n") ; stopifnot( res) 00092 00093 ### vectors 00094 00095 cat("\n===Int Vector via wrap\n") 00096 foo <- ' 00097 std::vector<int> iv = Rcpp::as< std::vector<int> >(x) ; 00098 std::cout << "Returning twice the value of vector : "; 00099 for (size_t i=0; i<iv.size(); i++) { 00100 iv[i] = 2*iv[i]; 00101 } 00102 return(Rcpp::wrap(iv)); 00103 ' 00104 funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE) 00105 print(funx(x=2:5)) 00106 funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE) 00107 print(funx(x=2:5)) 00108 funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE) 00109 print(funx(x=as.raw(2:5))) 00110 00111 cat("\n===Int Vector\n") 00112 foo <- ' 00113 std::vector<int> iv = Rcpp::as< std::vector<int> >(x) ; 00114 std::cout << "Returning twice the value of vector : "; 00115 for (size_t i=0; i<iv.size(); i++) { 00116 iv[i] = 2*iv[i]; 00117 } 00118 return(Rcpp::wrap( iv ) ); 00119 ' 00120 funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE) 00121 print(funx(x=2:5+.1)) 00122 funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE) 00123 print(funx(x=2:5)) 00124 funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE) 00125 print(funx(x=as.raw(2:5))) 00126 00127 00128 cat("\n===Double Vector\n") 00129 foo <- ' 00130 std::vector<double> iv = Rcpp::as< std::vector<double> >(x) ; 00131 std::cout << "Returning twice the value of vector : "; 00132 for (size_t i=0; i<iv.size(); i++) { 00133 iv[i] = 2*iv[i]; 00134 } 00135 return(Rcpp::wrap( iv )); 00136 ' 00137 funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE) 00138 print(funx(x=0.1+2:5)) 00139 funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE) 00140 print(funx(x=2:5)) 00141 funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE) 00142 print(funx(x=as.raw(2:5))) 00143 00144 cat("\n===Raw Vector\n") 00145 foo <- ' 00146 std::vector<Rbyte> iv = Rcpp::as< std::vector<Rbyte> >(x) ; 00147 std::cout << "Returning twice the value of vector : "; 00148 for (size_t i=0; i<iv.size(); i++) { 00149 iv[i] = 2*iv[i]; 00150 } 00151 return(Rcpp::wrap( iv )); 00152 ' 00153 funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE) 00154 print(funx(x=as.raw(0:9))) 00155 funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE) 00156 print(funx(x=0:9)) 00157 funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE) 00158 print(funx(x=0:9+.1)) 00159 00160 cat("\n=== vector<bool>\n") 00161 foo <- ' 00162 std::vector<bool> bv = Rcpp::as< std::vector<bool> >(x) ; 00163 std::cout << "Flip the value of vector : "; 00164 for (size_t i=0; i<bv.size(); i++) { 00165 bv[i].flip() ; 00166 } 00167 return(Rcpp::wrap( bv )); 00168 ' 00169 funx <- cfunction(signature(x="logical"), foo, Rcpp=TRUE, verbose=FALSE) 00170 print(funx(x=c(TRUE,FALSE))) 00171 funx <- cfunction(signature(x="raw"), foo, Rcpp=TRUE, verbose=FALSE) 00172 print(funx(x=as.raw(0:9))) 00173 funx <- cfunction(signature(x="integer"), foo, Rcpp=TRUE, verbose=FALSE) 00174 print(funx(x=0:9)) 00175 funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE) 00176 print(funx(x=as.numeric(0:9))) 00177 00178 00179 cat("\n===String Vector\n") 00180 foo <- ' 00181 std::vector<std::string> iv = Rcpp::as< std::vector<std::string> >(x); 00182 std::cout << "Returning twice the value of vector : "; 00183 for (size_t i=0; i<iv.size(); i++) { 00184 iv[i] = iv[i] + iv[i]; 00185 } 00186 return(Rcpp::wrap( iv )); 00187 ' 00188 funx <- cfunction(signature(x="character"), foo, Rcpp=TRUE, verbose=FALSE) 00189 print(funx(x=c("foo", "bar"))) 00190 00191 ### using std::set 00192 cat("\n=== set<int>\n") 00193 foo <- ' 00194 std::set<int> iv ; 00195 iv.insert( 0 ) ; 00196 iv.insert( 1 ) ; 00197 iv.insert( 0 ) ; 00198 return Rcpp::wrap( iv );' 00199 funx <- cfunction(signature(), foo, Rcpp=TRUE, verbose=FALSE, includes = "#include <set>" ) 00200 print(res <- funx()) 00201 stopifnot( identical( res, 0:1 ) ) 00202 00203 cat("\n=== set<double>\n") 00204 foo <- ' 00205 std::set<double> ds; 00206 ds.insert( 0.0 ); 00207 ds.insert( 1.0 ); 00208 ds.insert( 0.0 ); 00209 return(Rcpp::wrap( ds )); ' 00210 funx <- cfunction(signature(), foo, Rcpp=TRUE, verbose=FALSE, includes = "#include <set>") 00211 print( res <- funx() ) 00212 stopifnot( identical( res, as.numeric(0:1))) 00213 00214 cat("\n=== set<raw>\n") 00215 foo <- ' 00216 std::set<Rbyte> bs ; 00217 bs.insert( (Rbyte)0 ) ; 00218 bs.insert( (Rbyte)1 ) ; 00219 bs.insert( (Rbyte)0 ) ; 00220 return(Rcpp::wrap( bs )); ' 00221 funx <- cfunction(signature(), foo, Rcpp=TRUE, verbose=FALSE, includes = "#include <set>") 00222 print( res <- funx() ) 00223 stopifnot( identical( res, as.raw(0:1))) 00224 00225 cat("\n=== set<string> \n") 00226 foo <- ' 00227 std::set<std::string> ss ; 00228 ss.insert( "foo" ) ; 00229 ss.insert( "bar" ) ; 00230 ss.insert( "foo" ) ; 00231 return(Rcpp::wrap( ss )); ' 00232 funx <- cfunction(signature(), foo, Rcpp=TRUE, verbose=FALSE, include = "#include <set>" ) 00233 print( res <- funx() ) 00234 stopifnot( identical( res, c("bar","foo")) ) 00235 00236 00237 #========= attributes 00238 00239 funx <- cfunction( 00240 signature(x="data.frame"), ' 00241 std::vector<std::string> iv = Rcpp::RObject(x).attributeNames(); 00242 return(Rcpp::wrap( iv )); 00243 ', Rcpp=TRUE, verbose=FALSE) 00244 res <- funx( iris ) 00245 stopifnot( all( c("names", "row.names", "class" ) %in% res ) ) 00246 00247 funx <- cfunction(signature(x="data.frame"), ' 00248 bool has_class = Rcpp::RObject(x).hasAttribute( "class" ) ; 00249 return Rcpp::wrap( has_class ) ; 00250 ', Rcpp=TRUE, verbose=FALSE) 00251 res <- funx( iris ) 00252 stopifnot( res ) 00253 00254 funx <- cfunction(signature(x="data.frame"), ' 00255 return Rcpp::RObject(x).attr( "row.names" ) ; 00256 ', Rcpp=TRUE, verbose=FALSE) 00257 res <- funx( iris ) 00258 stopifnot( identical(res, 1:150) ) 00259 00260 #============ NULL 00261 funx <- cfunction(signature(x="ANY"), ' 00262 bool is_null = Rcpp::RObject(x).isNULL() ; 00263 return Rcpp::wrap( is_null ) ; 00264 ', Rcpp=TRUE, verbose=FALSE) 00265 res <- funx( iris ) 00266 stopifnot( !res ) 00267 res <- funx( NULL ) 00268 stopifnot( res ) 00269 00270 00271