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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jan 5 18:07:21 CET 2010


Author: romain
Date: 2010-01-05 18:07:20 +0100 (Tue, 05 Jan 2010)
New Revision: 283

Added:
   pkg/inst/unitTests/runit.RawVector.R
   pkg/src/LogicalVector.cpp
   pkg/src/Rcpp/LogicalVector.h
Modified:
   pkg/src/Rcpp.h
Log:
+Rcpp::LogicalVector

Added: pkg/inst/unitTests/runit.RawVector.R
===================================================================
--- pkg/inst/unitTests/runit.RawVector.R	                        (rev 0)
+++ pkg/inst/unitTests/runit.RawVector.R	2010-01-05 17:07:20 UTC (rev 283)
@@ -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.RawVector <- function(){
+	funx <- cfunction(signature(), '
+	RawVector x(10) ;
+	for( int i=0; i<10; i++) x[i] = (Rbyte)i ;
+	return x ;', 
+		Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;" )
+	checkEquals( funx(), as.raw(0:9), msg = "RawVector(int)" )
+}
+
+test.RawVector.REALSXP <- function(){
+	funx <- cfunction(signature(vec = "raw" ), '
+	RawVector x(vec) ;
+	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.raw(0:9)), as.raw(2*0:9), msg = "RawVector( RAWSXP) " )
+}
+
+test.RawVector.initializer.list <- function(){
+	if( Rcpp:::capabilities()[["initializer lists"]] ){
+		funx <- cfunction(signature(), '
+		RawVector x = {0,1,2,3} ;
+		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.raw(2*0:3), msg = "RawVector( initializer list) " )
+	}
+}
+
+

Added: pkg/src/LogicalVector.cpp
===================================================================
--- pkg/src/LogicalVector.cpp	                        (rev 0)
+++ pkg/src/LogicalVector.cpp	2010-01-05 17:07:20 UTC (rev 283)
@@ -0,0 +1,79 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// LogicalVector.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/LogicalVector.h>
+#include <algorithm>
+
+namespace Rcpp{
+	
+	LogicalVector::LogicalVector(SEXP x) throw(not_compatible) : RObject() {
+		switch( TYPEOF( x ) ){
+			case LGLSXP:
+				setSEXP( x ) ;
+				break ;
+			case RAWSXP:
+			case INTSXP:
+			case REALSXP:
+				setSEXP( Rf_coerceVector( x, LGLSXP) ) ;
+				break ;
+			default:
+				throw not_compatible( "cannot convert to intrger vector" ) ;
+		}
+	}
+	
+	LogicalVector::LogicalVector(int size) : RObject() {
+		setSEXP( Rf_allocVector(LGLSXP, size) ) ;
+	}
+
+#ifdef HAS_INIT_LISTS
+	LogicalVector::LogicalVector( std::initializer_list<int> list ) {
+		SEXP x = PROTECT( Rf_allocVector( INTSXP, list.size() ) ) ;
+		std::copy( list.begin(), list.end(), INTEGER(x) ); 
+		setSEXP( Rf_coerceVector( x, LGLSXP ) ) ;
+		UNPROTECT( 1 ); /* x */
+	}
+	LogicalVector::LogicalVector( std::initializer_list<Rboolean> list ) {
+		SEXP x = PROTECT( Rf_allocVector( LGLSXP, list.size() ) ) ;
+		std::copy( list.begin(), list.end(), LOGICAL(x) ); 
+		setSEXP(x) ;
+		UNPROTECT( 1 ); /* x */
+	}
+	LogicalVector::LogicalVector( std::initializer_list<bool> list ) {
+		SEXP x = PROTECT( Rf_allocVector( LGLSXP, list.size() ) ) ;
+		std::copy( list.begin(), list.end(), LOGICAL(x) ); 
+		setSEXP(x) ;
+		UNPROTECT( 1 ); /* x */
+	}
+#endif
+
+int& LogicalVector::operator[]( int i ) const { 
+	return LOGICAL(m_sexp)[i] ;
+}
+int* LogicalVector::begin() const { 
+	return LOGICAL(m_sexp) ;
+}
+int* LogicalVector::end() const { 
+	return LOGICAL(m_sexp) + LENGTH(m_sexp);
+}
+
+} // namespace 

Added: pkg/src/Rcpp/LogicalVector.h
===================================================================
--- pkg/src/Rcpp/LogicalVector.h	                        (rev 0)
+++ pkg/src/Rcpp/LogicalVector.h	2010-01-05 17:07:20 UTC (rev 283)
@@ -0,0 +1,68 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// LogicalVector.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_LogicalVector_h
+#define Rcpp_LogicalVector_h
+
+#include <RcppCommon.h>
+#include <Rcpp/RObject.h>
+
+#ifdef HAS_INIT_LISTS
+#include <initializer_list>
+#include <algorithm>
+#endif
+
+namespace Rcpp{ 
+
+class LogicalVector : public RObject {     
+public:
+
+	LogicalVector(SEXP x) throw(not_compatible);
+	LogicalVector( int size) ;
+	
+#ifdef HAS_INIT_LISTS	
+	LogicalVector( std::initializer_list<int> list ) ;
+	LogicalVector( std::initializer_list<Rboolean> list ) ;
+	LogicalVector( std::initializer_list<bool> 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 ) ; }
+	
+	typedef Rboolean* iterator ;
+	typedef Rboolean value_type ;
+	
+	int& operator[]( int i ) const ;
+	int* begin() const ; 
+	int* end() const ;
+	
+} ;
+
+} // namespace
+
+#endif

Modified: pkg/src/Rcpp.h
===================================================================
--- pkg/src/Rcpp.h	2010-01-05 16:50:36 UTC (rev 282)
+++ pkg/src/Rcpp.h	2010-01-05 17:07:20 UTC (rev 283)
@@ -59,5 +59,6 @@
 #include <Rcpp/IntegerVector.h>
 #include <Rcpp/NumericVector.h>
 #include <Rcpp/RawVector.h>
+#include <Rcpp/LogicalVector.h>
 
 #endif



More information about the Rcpp-commits mailing list