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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Feb 24 11:53:33 CET 2010


Author: romain
Date: 2010-02-24 11:53:33 +0100 (Wed, 24 Feb 2010)
New Revision: 780

Added:
   pkg/Rcpp/inst/unitTests/runit.Column.R
   pkg/Rcpp/src/Rcpp/MatrixColumn.h
Modified:
   pkg/Rcpp/src/Rcpp.h
   pkg/Rcpp/src/Rcpp/CharacterVector.h
   pkg/Rcpp/src/Rcpp/MatrixRow.h
   pkg/Rcpp/src/Rcpp/SEXP_Vector.h
   pkg/Rcpp/src/Rcpp/SimpleVector.h
Log:
+Rcpp::MatrixColumn

Added: pkg/Rcpp/inst/unitTests/runit.Column.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Column.R	                        (rev 0)
+++ pkg/Rcpp/inst/unitTests/runit.Column.R	2010-02-24 10:53:33 UTC (rev 780)
@@ -0,0 +1,62 @@
+#!/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/>.
+
+test.NumericMatrix.column <- function(){
+	funx <- cfunction(signature(x = "matrix" ), '
+		NumericMatrix m(x) ;
+		NumericMatrix::Column col = m.column(0) ;
+		return wrap( std::accumulate( col.begin(), col.end(), 0.0 ) ) ;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	x <- matrix( 1:16 + .5, ncol = 4 )
+	checkEquals( funx( x ), sum( x[,1], msg = "iterating over a column" )
+}
+
+test.CharacterMatrix.column <- function(){
+	funx <- cfunction(signature(x = "matrix" ), '
+		CharacterVector m(x) ;
+		CharacterVector::Column col = m.column(0) ;
+		std::string res( 
+			std::accumulate( 
+				col.begin(), col.end(), std::string() ) ) ;
+		return wrap(res) ;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	
+	m <- matrix( letters, ncol = 2 )
+	checkEquals( funx(m), paste( m[1,], collapse = "" ), msg = "CharacterVector::Column" )
+}
+
+test.List.column <- function(){
+	
+	funx <- cfunction(signature(x = "matrix" ), '
+		List m(x) ;
+		List::Column col = m.column(0) ;
+		IntegerVector out( col.size() ) ;
+		std::transform( 
+			col.begin(), col.end(),
+			out.begin(), 
+			unary_call<SEXP,int>( Function("length" ) ) ) ;
+		return wrap(out) ;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	
+	m <- lapply( 1:16, function(i) seq(from=1, to = i ) )
+	dim( m ) <- c( 4, 4 )
+	checkEquals( funx( m ), 1:4, msg = "List::Column" )
+	
+}
+

Modified: pkg/Rcpp/src/Rcpp/CharacterVector.h
===================================================================
--- pkg/Rcpp/src/Rcpp/CharacterVector.h	2010-02-24 10:36:46 UTC (rev 779)
+++ pkg/Rcpp/src/Rcpp/CharacterVector.h	2010-02-24 10:53:33 UTC (rev 780)
@@ -25,6 +25,7 @@
 #include <RcppCommon.h>
 #include <Rcpp/VectorBase.h>
 #include <Rcpp/MatrixRow.h>
+#include <Rcpp/MatrixColumn.h>
 #include <Rcpp/Dimension.h>
 #include <Rcpp/r_cast.h>
 
@@ -150,6 +151,7 @@
 	
 	typedef StringProxy value_type ;
 	typedef MatrixRow<CharacterVector> Row ;
+	typedef MatrixColumn<CharacterVector> Column ;
 	typedef StringProxy reference ;
 	
 	/**
@@ -283,10 +285,9 @@
 		if( update ) setSEXP(x) ;
 	}
 
-	inline Row row( int i){
-		return Row(*this, i ) ;
-	}
-	
+	inline Row row( int i ){ return Row( *this, i ) ; }
+	inline Column column( int i ){ return Column( *this, i ) ; }
+
 } ;
 
 typedef CharacterVector StringVector ;

Added: pkg/Rcpp/src/Rcpp/MatrixColumn.h
===================================================================
--- pkg/Rcpp/src/Rcpp/MatrixColumn.h	                        (rev 0)
+++ pkg/Rcpp/src/Rcpp/MatrixColumn.h	2010-02-24 10:53:33 UTC (rev 780)
@@ -0,0 +1,67 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// MatrixColumn.h: Rcpp R/C++ interface class library -- columns of matrices
+//
+// 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__MatrixColumn_h
+#define Rcpp__MatrixColumn_h
+
+#include <RcppCommon.h>
+#include <Rcpp/VectorBase.h>
+
+namespace Rcpp{
+
+template <typename VECTOR>
+class MatrixColumn {
+public:
+
+	typedef typename VECTOR::reference reference ;
+	typedef typename VECTOR::value_type value_type ;
+	typedef typename VECTOR::iterator iterator ;
+	
+	MatrixColumn( VECTOR& object, int i ) : parent(object), index(i){
+		if( ! ::Rf_isMatrix(parent) ) throw VectorBase::not_a_matrix() ;
+		if( i < 0 || i >= parent.ncol() ) throw RObject::index_out_of_bounds() ;
+	}
+	
+	reference operator[]( const int& i ){
+		/* TODO: should we cache nrow and ncol */
+		return parent[ index * parent.ncol() + i ] ;
+	}
+	
+	inline iterator begin(){
+		return iterator( parent, index * parent.ncol() ) ;
+	}
+	
+	inline iterator end(){
+		return iterator( parent, index * parent.ncol() + parent.nrow() ) ;
+	}
+	
+	inline int size(){
+		return parent.nrow() ;
+	}
+	
+private:
+	VECTOR& parent; 
+	int index ;
+} ;
+	
+	
+} // namespace Rcpp
+#endif

Modified: pkg/Rcpp/src/Rcpp/MatrixRow.h
===================================================================
--- pkg/Rcpp/src/Rcpp/MatrixRow.h	2010-02-24 10:36:46 UTC (rev 779)
+++ pkg/Rcpp/src/Rcpp/MatrixRow.h	2010-02-24 10:53:33 UTC (rev 780)
@@ -19,8 +19,8 @@
 // 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_Row_h
-#define Rcpp_Row_h
+#ifndef Rcpp__MatrixRow_h
+#define Rcpp__MatrixRow_h
 
 #include <RcppCommon.h>
 #include <Rcpp/VectorBase.h>

Modified: pkg/Rcpp/src/Rcpp/SEXP_Vector.h
===================================================================
--- pkg/Rcpp/src/Rcpp/SEXP_Vector.h	2010-02-24 10:36:46 UTC (rev 779)
+++ pkg/Rcpp/src/Rcpp/SEXP_Vector.h	2010-02-24 10:53:33 UTC (rev 780)
@@ -25,6 +25,7 @@
 #include <RcppCommon.h>
 #include <Rcpp/VectorBase.h>
 #include <Rcpp/MatrixRow.h>
+#include <Rcpp/MatrixColumn.h>
 #include <Rcpp/Environment.h>
 #include <Rcpp/Dimension.h>
 
@@ -116,6 +117,7 @@
 class SEXP_Vector : public SEXP_Vector_Base{
 public:
 	typedef MatrixRow<SEXP_Vector> Row ;
+	typedef MatrixColumn<SEXP_Vector> Column ;
 	typedef Proxy reference ;
 	typedef Proxy value_type ;
 	
@@ -285,6 +287,7 @@
 	}
 	
 	inline Row row( int i){ return Row(*this, i ) ; }
+	inline Column column( int i){ return Column(*this, i ) ; }
 	
 private:
 	

Modified: pkg/Rcpp/src/Rcpp/SimpleVector.h
===================================================================
--- pkg/Rcpp/src/Rcpp/SimpleVector.h	2010-02-24 10:36:46 UTC (rev 779)
+++ pkg/Rcpp/src/Rcpp/SimpleVector.h	2010-02-24 10:53:33 UTC (rev 780)
@@ -24,6 +24,7 @@
 
 #include <RcppCommon.h>
 #include <Rcpp/MatrixRow.h>
+#include <Rcpp/MatrixColumn.h>
 #include <Rcpp/VectorBase.h>
 #include <Rcpp/r_cast.h>
 #include <Rcpp/Dimension.h>
@@ -38,6 +39,7 @@
 	typedef value_type* iterator ;
 	typedef value_type& reference ;
 	typedef MatrixRow<SimpleVector> Row ;
+	typedef MatrixColumn<SimpleVector> Column ;
 	
 	SimpleVector() : VectorBase(), start(0){}
 	
@@ -111,9 +113,8 @@
 		UNPROTECT(1) ;
 	}
 	
-	inline Row row( int i ){
-		return Row( *this, i ) ;
-	}
+	inline Row row( int i ){ return Row( *this, i ) ; }
+	inline Column column( int i ){ return Column( *this, i ) ; }
 
 protected:
 	void init(){

Modified: pkg/Rcpp/src/Rcpp.h
===================================================================
--- pkg/Rcpp/src/Rcpp.h	2010-02-24 10:36:46 UTC (rev 779)
+++ pkg/Rcpp/src/Rcpp.h	2010-02-24 10:53:33 UTC (rev 780)
@@ -54,6 +54,7 @@
 #include <Rcpp/Dimension.h>
 #include <Rcpp/VectorBase.h>
 #include <Rcpp/MatrixRow.h>
+#include <Rcpp/MatrixColumn.h>
 #include <Rcpp/SimpleVector.h>
 #include <Rcpp/SEXP_Vector.h>
 #include <Rcpp/XPtr.h>



More information about the Rcpp-commits mailing list