[Rcpp-commits] r284 - 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 18:46:03 CET 2010


Author: romain
Date: 2010-01-05 18:46:03 +0100 (Tue, 05 Jan 2010)
New Revision: 284

Added:
   pkg/inst/unitTests/runit.GenericVector.R
   pkg/src/GenericVector.cpp
   pkg/src/Rcpp/GenericVector.h
Modified:
   pkg/inst/ChangeLog
   pkg/src/Rcpp.h
Log:
+ Rcpp::GenericVector (draft)

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2010-01-05 17:07:20 UTC (rev 283)
+++ pkg/inst/ChangeLog	2010-01-05 17:46:03 UTC (rev 284)
@@ -1,6 +1,12 @@
 2010-01-05  Romain Francois <francoisromain at free.fr>
 
-	* src/Rcpp/IntegerVector.h new class Rcpp::IntegerVector 
+	* src/Rcpp/GenericVector.h : new class Rcpp::GenericVector 
+	and an alias Rcpp::List to handle lists (VECSXP), aka generic 
+	vectors
+	* src/NumericVector.cpp : implementation
+	* inst/unitTests/runit.NumericVector.R: unit tests
+
+	* 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 
@@ -20,6 +26,11 @@
 	* src/RawVector.cpp : implementation
 	* inst/unitTests/runit.RawVector.R: unit tests
 
+	* src/Rcpp/LogicalVector.h : same as above, but for numeric 
+	vectors (RAWSXP)
+	* src/LogicalVector.cpp : implementation
+	* inst/unitTests/runit.LogicalVector.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.GenericVector.R
===================================================================
--- pkg/inst/unitTests/runit.GenericVector.R	                        (rev 0)
+++ pkg/inst/unitTests/runit.GenericVector.R	2010-01-05 17:46:03 UTC (rev 284)
@@ -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.List <- function(){
+	funx <- cfunction(signature(), '
+	List x(10) ;
+	for( int i=0; i<10; i++) x.set( i, Rf_ScalarInteger( i * 2) )  ;
+	return x ;', 
+		Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;" )
+	checkEquals( funx(), as.list( 2*0:9), msg = "GenericVector" )
+}
+
+test.List.VECSXP <- function(){
+	funx <- cfunction(signature(vec = "list" ), '
+	List x(vec) ;
+	return x ;', 
+		Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;" )
+	checkEquals( funx(list(1,2)), list(1,2), msg = "GenericVector( VECSXP) " )
+}
+
+test.IntegerVector.initializer.list <- function(){
+	if( Rcpp:::capabilities()[["initializer lists"]] ){
+		funx <- cfunction(signature(), '
+		SEXP x0 = PROTECT( Rf_ScalarInteger( 0 ) ) ;
+		SEXP x1 = PROTECT( Rf_ScalarInteger( 1 ) ) ;
+		SEXP x2 = PROTECT( Rf_ScalarInteger( 2 ) ) ;
+		List x = { x0, x1, x2} ;
+		UNPROTECT(3) ;
+		return x ;', 
+			Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;" )
+		checkEquals( funx(), as.list(0:2), msg = "List( initializer list) " )
+	}
+}
+
+

Added: pkg/src/GenericVector.cpp
===================================================================
--- pkg/src/GenericVector.cpp	                        (rev 0)
+++ pkg/src/GenericVector.cpp	2010-01-05 17:46:03 UTC (rev 284)
@@ -0,0 +1,63 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// GenericVector.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/GenericVector.h>
+#include <algorithm>
+
+namespace Rcpp{
+	
+	GenericVector::GenericVector(SEXP x) throw(not_compatible) : RObject() {
+		switch( TYPEOF( x ) ){
+			case VECSXP:
+				setSEXP( x ) ;
+				break ;
+			default:
+				throw not_compatible( "not a list" ) ;
+		}
+	}
+	
+	GenericVector::GenericVector(int size) : RObject() {
+		setSEXP( Rf_allocVector(VECSXP, size) ) ;
+	}
+
+#ifdef HAS_INIT_LISTS
+	GenericVector::GenericVector( std::initializer_list<RObject> list ) {
+		SEXP x = PROTECT( Rf_allocVector( VECSXP, list.size() ) ) ;
+		const RObject* p = list.begin() ;
+		for( int i=0; i<list.size() ; i++, p++){
+			SET_VECTOR_ELT( x, i, p->asSexp() ) ;
+		}
+		setSEXP( x ) ;
+		UNPROTECT( 1 ); /* x */
+	}
+#endif
+
+SEXP GenericVector::get( const int& i ) const { 
+	return VECTOR_ELT(m_sexp, i) ;
+}
+
+void GenericVector::set( const int& i, SEXP value ){
+	SET_VECTOR_ELT(m_sexp,i,value) ;
+}
+
+} // namespace 

Added: pkg/src/Rcpp/GenericVector.h
===================================================================
--- pkg/src/Rcpp/GenericVector.h	                        (rev 0)
+++ pkg/src/Rcpp/GenericVector.h	2010-01-05 17:46:03 UTC (rev 284)
@@ -0,0 +1,63 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// GenericVector.h: Rcpp R/C++ interface class library -- logical 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_GenericVector_h
+#define Rcpp_GenericVector_h
+
+#include <RcppCommon.h>
+#include <Rcpp/RObject.h>
+
+#ifdef HAS_INIT_LISTS
+#include <initializer_list>
+#include <algorithm>
+#endif
+
+namespace Rcpp{ 
+
+class GenericVector : public RObject {     
+public:
+
+	GenericVector(SEXP x) throw(not_compatible);
+	GenericVector( int size) ;
+	
+#ifdef HAS_INIT_LISTS	
+	GenericVector( std::initializer_list<RObject> 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 ) ; }
+	
+	SEXP get(const int& i) const ;
+	void set(const int& i, SEXP value ) ;
+} ;
+
+typedef GenericVector List ;
+
+} // namespace
+
+#endif

Modified: pkg/src/Rcpp.h
===================================================================
--- pkg/src/Rcpp.h	2010-01-05 17:07:20 UTC (rev 283)
+++ pkg/src/Rcpp.h	2010-01-05 17:46:03 UTC (rev 284)
@@ -60,5 +60,6 @@
 #include <Rcpp/NumericVector.h>
 #include <Rcpp/RawVector.h>
 #include <Rcpp/LogicalVector.h>
+#include <Rcpp/GenericVector.h>
 
 #endif



More information about the Rcpp-commits mailing list