[Rcpp-devel] [Rcpp-commits] r285 - in pkg: inst inst/unitTests src src/Rcpp
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Jan 6 09:17:15 CET 2010
Author: romain
Date: 2010-01-06 09:17:09 +0100 (Wed, 06 Jan 2010)
New Revision: 285
Added:
pkg/inst/unitTests/runit.wrap.R
Modified:
pkg/inst/ChangeLog
pkg/inst/unitTests/runit.RObject.R
pkg/src/Rcpp/wrap.h
pkg/src/RcppCommon.h
pkg/src/wrap.cpp
Log:
wrap( std::initializer_list<> )
Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog 2010-01-05 17:46:03 UTC (rev 284)
+++ pkg/inst/ChangeLog 2010-01-06 08:17:09 UTC (rev 285)
@@ -1,5 +1,12 @@
2010-01-05 Romain Francois <francoisromain at free.fr>
+ * src/Rcpp/wrap.h : wrap can now use initializer lists (available
+ on on GCC >= 4.4).
+ * src/wrap.cpp: implementation
+ * int/unitTests/runit.wrap.R: unit tests
+
+2010-01-05 Romain Francois <francoisromain at free.fr>
+
* src/Rcpp/GenericVector.h : new class Rcpp::GenericVector
and an alias Rcpp::List to handle lists (VECSXP), aka generic
vectors
Modified: pkg/inst/unitTests/runit.RObject.R
===================================================================
--- pkg/inst/unitTests/runit.RObject.R 2010-01-05 17:46:03 UTC (rev 284)
+++ pkg/inst/unitTests/runit.RObject.R 2010-01-06 08:17:09 UTC (rev 285)
@@ -290,4 +290,3 @@
checkTrue( !funx(.GlobalEnv), msg = "RObject.isNULL(environment) -> false" )
}
-
Added: pkg/inst/unitTests/runit.wrap.R
===================================================================
--- pkg/inst/unitTests/runit.wrap.R (rev 0)
+++ pkg/inst/unitTests/runit.wrap.R 2010-01-06 08:17:09 UTC (rev 285)
@@ -0,0 +1,66 @@
+#!/usr/bin/r -t
+#
+# Copyright (C) 2009 - 2010 Romain Francois
+#
+# 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/>.
+
+.setUp <- function(){
+ suppressMessages( require( inline ) )
+}
+
+if( Rcpp:::capabilities()[["initializer lists"]] ){
+
+ test.wrap.initializerlist.int <- function(){
+ funx <- cfunction(signature(), '
+ return Rcpp::wrap( {0,1,2} );
+ ', Rcpp=TRUE, verbose=FALSE)
+ checkEquals( funx(), 0:2,
+ msg = "wrap( initializer_list<int> )" )
+ }
+
+ test.wrap.initializerlist.double <- function(){
+ funx <- cfunction(signature(), '
+ return Rcpp::wrap( {0.0,1.0,2.0} );
+ ', Rcpp=TRUE, verbose=FALSE)
+ checkEquals( funx(), as.numeric(0:2),
+ msg = "wrap( initializer_list<double> )" )
+ }
+
+ test.wrap.initializerlist.bool <- function(){
+ funx <- cfunction(signature(), '
+ return Rcpp::wrap( {false, true, false} );
+ ', Rcpp=TRUE, verbose=FALSE)
+ checkEquals( funx(), c(FALSE, TRUE, FALSE),
+ msg = "wrap( initializer_list<bool> )" )
+ }
+
+ test.wrap.initializerlist.Rbyte <- function(){
+ funx <- cfunction(signature(), '
+ return Rcpp::wrap( { (Rbyte)0, (Rbyte)1 } );
+ ', Rcpp=TRUE, verbose=FALSE)
+ checkEquals( funx(), as.raw(0:1),
+ msg = "wrap( initializer_list<Rbyte> )" )
+ }
+
+ # test.wrap.initializerlist.RObject <- function(){
+ # funx <- cfunction(signature(), '
+ # return Rcpp::wrap( { Rcpp::wrap(1), Rcpp::wrap("foo"), Rcpp::wrap(1.0) } );
+ # ', Rcpp=TRUE, verbose=FALSE)
+ # checkEquals( funx(), list(1L, "foo", 1.0) ,
+ # msg = "wrap( initializer_list<SEXP> )" )
+ # }
+
+}
Modified: pkg/src/Rcpp/wrap.h
===================================================================
--- pkg/src/Rcpp/wrap.h 2010-01-05 17:46:03 UTC (rev 284)
+++ pkg/src/Rcpp/wrap.h 2010-01-06 08:17:09 UTC (rev 285)
@@ -22,9 +22,18 @@
#ifndef Rcpp_wrap_h
#define Rcpp_wrap_h
+#ifdef HAS_INIT_LISTS
+#include <initializer_list>
+#endif
+
#include <RcppCommon.h>
#include <Rcpp/RObject.h>
#include <set>
+#include <Rcpp/IntegerVector.h>
+#include <Rcpp/RawVector.h>
+#include <Rcpp/NumericVector.h>
+#include <Rcpp/LogicalVector.h>
+#include <Rcpp/GenericVector.h>
namespace Rcpp{
@@ -50,6 +59,12 @@
RObject wrap(const std::set<std::string> & v);
RObject wrap(const std::set<Rbyte> & v);
+IntegerVector wrap( std::initializer_list<int> list) ;
+NumericVector wrap( std::initializer_list<double> list) ;
+LogicalVector wrap( std::initializer_list<bool> list) ;
+RawVector wrap(std::initializer_list<Rbyte> list) ;
+// List wrap( std::initializer_list<RObject> list) ;
+
} // namespace Rcpp
#endif
Modified: pkg/src/RcppCommon.h
===================================================================
--- pkg/src/RcppCommon.h 2010-01-05 17:46:03 UTC (rev 284)
+++ pkg/src/RcppCommon.h 2010-01-06 08:17:09 UTC (rev 285)
@@ -45,7 +45,6 @@
// include R headers, but set R_NO_REMAP and access everything via Rf_ prefixes
#define R_NO_REMAP
-
#include <R.h>
#include <Rinternals.h>
#include <R_ext/Callbacks.h>
Modified: pkg/src/wrap.cpp
===================================================================
--- pkg/src/wrap.cpp 2010-01-05 17:46:03 UTC (rev 284)
+++ pkg/src/wrap.cpp 2010-01-06 08:17:09 UTC (rev 285)
@@ -19,9 +19,19 @@
// You should have received a copy of the GNU General Public License
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
+#include <RcppCommon.h>
#include <Rcpp/wrap.h>
#include <Rcpp/Symbol.h>
#include <Rcpp/Environment.h>
+#include <algorithm>
+#ifdef HAS_INIT_LISTS
+#include <initializer_list>
+#endif
+#include <Rcpp/IntegerVector.h>
+#include <Rcpp/RawVector.h>
+#include <Rcpp/NumericVector.h>
+#include <Rcpp/LogicalVector.h>
+#include <Rcpp/GenericVector.h>
namespace Rcpp{
@@ -175,4 +185,38 @@
return o ;
}
+#ifdef HAS_INIT_LISTS
+IntegerVector wrap( std::initializer_list<int> list) {
+ SEXP x = PROTECT( Rf_allocVector( INTSXP, list.size() ) ) ;
+ std::copy( list.begin(), list.end(), INTEGER(x) ) ;
+ UNPROTECT(1) ;
+ return IntegerVector( x ) ;
+}
+NumericVector wrap( std::initializer_list<double> list) {
+ SEXP x = PROTECT( Rf_allocVector( REALSXP, list.size() ) ) ;
+ std::copy( list.begin(), list.end(), REAL(x) ) ;
+ UNPROTECT(1) ;
+ return NumericVector( x ) ;
+}
+LogicalVector wrap( std::initializer_list<bool> list) {
+ SEXP x = PROTECT( Rf_allocVector( LGLSXP, list.size() ) ) ;
+ std::copy( list.begin(), list.end(), LOGICAL(x) ) ;
+ UNPROTECT(1) ;
+ return LogicalVector( x ) ;
+}
+RawVector wrap(std::initializer_list<Rbyte> list){
+ SEXP x = PROTECT( Rf_allocVector( RAWSXP, list.size() ) ) ;
+ std::copy( list.begin(), list.end(), RAW(x) ) ;
+ UNPROTECT(1) ;
+ return RawVector( x ) ;
+}
+// List wrap( std::initializer_list<RObject> list){
+// SEXP x = PROTECT( Rf_allocVector( VECSXP, list.size() ) ) ;
+// std::copy( list.begin(), list.end(), VECTOR_PTR(x) ) ;
+// UNPROTECT(1) ;
+// return List( x ) ;
+// }
+#endif
+
+
} // namespace Rcpp
_______________________________________________
Rcpp-commits mailing list
Rcpp-commits at lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-commits
More information about the Rcpp-devel
mailing list