[Rcpp-devel] [Rcpp-commits] r281 - in pkg: inst inst/unitTests src src/Rcpp

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jan 5 16:24:52 CET 2010


Author: romain
Date: 2010-01-05 16:24:52 +0100 (Tue, 05 Jan 2010)
New Revision: 281

Added:
   pkg/inst/unitTests/runit.NumericVector.R
   pkg/src/NumericVector.cpp
   pkg/src/Rcpp/NumericVector.h
Modified:
   pkg/inst/ChangeLog
   pkg/src/IntegerVector.cpp
   pkg/src/Rcpp.h
   pkg/src/Rcpp/IntegerVector.h
Log:
new class Rcpp::NumericVector

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2010-01-05 15:04:26 UTC (rev 280)
+++ pkg/inst/ChangeLog	2010-01-05 15:24:52 UTC (rev 281)
@@ -1,5 +1,20 @@
 2010-01-05  Romain Francois <francoisromain at free.fr>
 
+	* src/Rcpp/IntegerVector.h new class Rcpp::IntegerVector 
+	to manage integer vector (INTSXP). IntegerVector can be
+	constructed from SEXP of appropriate type, from an int which 
+	simply allocates a vector that big, or using an 
+	initialization list (the last is GCC >= 4.4 only). 
+	Additionally IntegerVector defines begin() and end() so that 
+	they can be used in STL algorithms
+	* src/IntegerVector.cpp : implementation
+	* inst/unitTests/runit.IntegerVector.R: unit tests
+	
+	* src/Rcpp/NumericVector.h : same as above, but for numeric 
+	vectors (REALSXP)
+	* src/NumericVector.cpp : implementation
+	* inst/unitTests/runit.NumericVector.R: unit tests
+	
 	* src/RcppCommon.h: improve the conditional compiling logic
 	with macros HAS_VARIADIC_TEMPLATES and HAS_INIT_LISTS instead
 	of CXX0X. This ensures the package can be compiled with 

Added: pkg/inst/unitTests/runit.NumericVector.R
===================================================================
--- pkg/inst/unitTests/runit.NumericVector.R	                        (rev 0)
+++ pkg/inst/unitTests/runit.NumericVector.R	2010-01-05 15:24:52 UTC (rev 281)
@@ -0,0 +1,55 @@
+#!/usr/bin/r -t
+#
+# Copyright (C) 2010	Dirk Eddelbuettel and 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 ) )
+}
+
+test.NumericVector <- function(){
+	funx <- cfunction(signature(), '
+	NumericVector x(10) ;
+	for( int i=0; i<10; i++) x[i] = i ;
+	return x ;', 
+		Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;" )
+	checkEquals( funx(), as.numeric(0:9), msg = "NumericVector(int)" )
+}
+
+test.NumericVector.REALSXP <- function(){
+	funx <- cfunction(signature(vec = "numeric" ), '
+	NumericVector x(vec) ;
+	for( int i=0; i<x.size(); i++) { 
+		x[i] = x[i]*2.0 ;
+	}
+	return x ;', 
+		Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;" )
+	checkEquals( funx(as.numeric(0:9)), 2*0:9, msg = "NumericVector( REALSXP) " )
+}
+
+test.NumericVector.initializer.list <- function(){
+	if( Rcpp:::capabilities()[["initializer lists"]] ){
+		funx <- cfunction(signature(), '
+		NumericVector x = {0.0,1.0,2.0,3.0} ;
+		for( int i=0; i<x.size(); i++) x[i] = x[i]*2 ;
+		return x ;', 
+			Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;" )
+		checkEquals( funx(), as.numeric(2*0:3), msg = "NumericVector( initializer list) " )
+	}
+}
+
+

Modified: pkg/src/IntegerVector.cpp
===================================================================
--- pkg/src/IntegerVector.cpp	2010-01-05 15:04:26 UTC (rev 280)
+++ pkg/src/IntegerVector.cpp	2010-01-05 15:24:52 UTC (rev 281)
@@ -38,12 +38,18 @@
 	}
 
 #ifdef HAS_INIT_LISTS	
-IntegerVector::IntegerVector( std::initializer_list<int> list ) {
+	IntegerVector::IntegerVector( std::initializer_list<int> list ) {
 		SEXP x = PROTECT( Rf_allocVector( INTSXP, list.size() ) ) ;
 		std::copy( list.begin(), list.end(), INTEGER(x) ); 
 		setSEXP(x) ;
 		UNPROTECT( 1 ); /* x */
 	}
+	IntegerVector::IntegerVector( std::initializer_list<double> list ) {
+		SEXP x = PROTECT( Rf_allocVector( INTSXP, list.size() ) ) ;
+		std::copy( list.begin(), list.end(), INTEGER(x) ); 
+		setSEXP(x) ;
+		UNPROTECT( 1 ); /* x */
+	}
 #endif
 
 int& IntegerVector::operator[]( int i ) const { 

Added: pkg/src/NumericVector.cpp
===================================================================
--- pkg/src/NumericVector.cpp	                        (rev 0)
+++ pkg/src/NumericVector.cpp	2010-01-05 15:24:52 UTC (rev 281)
@@ -0,0 +1,65 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// NumericVector.h: Rcpp R/C++ interface class library -- integer vectors
+//
+// Copyright (C) 2010	Dirk Eddelbuettel and 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/>.
+
+#include <RcppCommon.h>
+#include <Rcpp/RObject.h>
+#include <Rcpp/NumericVector.h>
+
+namespace Rcpp{
+	
+	NumericVector::NumericVector(SEXP x) throw(not_compatible) : RObject() {
+		if( TYPEOF( x ) == REALSXP ){
+			setSEXP( x ) ;
+		} else {
+			throw not_compatible( "cannot convert to intrger vector" ) ;
+		}
+	}
+	
+	NumericVector::NumericVector(int size) : RObject() {
+		setSEXP( Rf_allocVector(REALSXP, size) ) ;
+	}
+
+#ifdef HAS_INIT_LISTS	
+	NumericVector::NumericVector( std::initializer_list<int> list ) {
+		SEXP x = PROTECT( Rf_allocVector( REALSXP, list.size() ) ) ;
+		std::copy( list.begin(), list.end(), REAL(x) ); 
+		setSEXP(x) ;
+		UNPROTECT( 1 ); /* x */
+	}
+	NumericVector::NumericVector( std::initializer_list<double> list ) {
+		SEXP x = PROTECT( Rf_allocVector( REALSXP, list.size() ) ) ;
+		std::copy( list.begin(), list.end(), REAL(x) ); 
+		setSEXP(x) ;
+		UNPROTECT( 1 ); /* x */
+	}
+#endif
+
+double& NumericVector::operator[]( int i ) const { 
+	return REAL(m_sexp)[i] ;
+}
+double* NumericVector::begin() const { 
+	return REAL(m_sexp) ;
+}
+double* NumericVector::end() const { 
+	return REAL(m_sexp) + LENGTH(m_sexp);
+}
+
+} // namespace 

Modified: pkg/src/Rcpp/IntegerVector.h
===================================================================
--- pkg/src/Rcpp/IntegerVector.h	2010-01-05 15:04:26 UTC (rev 280)
+++ pkg/src/Rcpp/IntegerVector.h	2010-01-05 15:24:52 UTC (rev 281)
@@ -39,7 +39,8 @@
 	IntegerVector( int size) ;
 	
 #ifdef HAS_INIT_LISTS	
-	IntegerVector( std::initializer_list<int> list ) ; 
+	IntegerVector( std::initializer_list<int> list ) ;
+	IntegerVector( std::initializer_list<double> list ) ;
 #endif
 	
 	/**

Added: pkg/src/Rcpp/NumericVector.h
===================================================================
--- pkg/src/Rcpp/NumericVector.h	                        (rev 0)
+++ pkg/src/Rcpp/NumericVector.h	2010-01-05 15:24:52 UTC (rev 281)
@@ -0,0 +1,64 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// NumericVector.h: Rcpp R/C++ interface class library -- numeric vectors
+//
+// Copyright (C) 2010	Dirk Eddelbuettel and 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/>.
+
+#ifndef Rcpp_NumericVector_h
+#define Rcpp_NumericVector_h
+
+#include <RcppCommon.h>
+#include <Rcpp/RObject.h>
+
+#ifdef HAS_INIT_LISTS
+#include <initializer_list>
+#include <algorithm>
+#endif
+
+namespace Rcpp{ 
+
+class NumericVector : public RObject {     
+public:
+
+	NumericVector(SEXP x) throw(not_compatible);
+	NumericVector( int size) ;
+	
+#ifdef HAS_INIT_LISTS	
+	NumericVector( std::initializer_list<int> list ) ;
+	NumericVector( std::initializer_list<double> list ) ;
+#endif
+	
+	/**
+	 * the length of the vector, uses Rf_length
+	 */
+	inline int length() const { return Rf_length( m_sexp ) ; }
+	
+	/**
+	 * alias of length
+	 */
+	inline int size() const { return Rf_length( m_sexp ) ; }
+	
+	double& operator[]( int i ) const ;
+	double* begin() const ; 
+	double* end() const ;
+	
+} ;
+
+} // namespace
+
+#endif

Modified: pkg/src/Rcpp.h
===================================================================
--- pkg/src/Rcpp.h	2010-01-05 15:04:26 UTC (rev 280)
+++ pkg/src/Rcpp.h	2010-01-05 15:24:52 UTC (rev 281)
@@ -57,5 +57,6 @@
 #include <Rcpp/Pairlist.h>
 #include <Rcpp/Function.h>
 #include <Rcpp/IntegerVector.h>
+#include <Rcpp/NumericVector.h>
 
 #endif

_______________________________________________
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