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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jan 28 11:55:02 CET 2010


Author: romain
Date: 2010-01-28 11:55:02 +0100 (Thu, 28 Jan 2010)
New Revision: 502

Added:
   pkg/src/Dimension.cpp
   pkg/src/Rcpp/Dimension.h
Modified:
   pkg/NEWS
   pkg/inst/ChangeLog
   pkg/inst/unitTests/runit.CharacterVector.R
   pkg/inst/unitTests/runit.IntegerVector.R
   pkg/src/CharacterVector.cpp
   pkg/src/Rcpp.h
   pkg/src/Rcpp/CharacterVector.h
   pkg/src/Rcpp/SEXP_Vector.h
   pkg/src/Rcpp/SimpleVector.h
   pkg/src/Rcpp/wrap.h
   pkg/src/RcppCommon.h
Log:
add and use Rcpp::Dimension

Modified: pkg/NEWS
===================================================================
--- pkg/NEWS	2010-01-28 09:30:43 UTC (rev 501)
+++ pkg/NEWS	2010-01-28 10:55:02 UTC (rev 502)
@@ -1,6 +1,10 @@
 
 0.7.4	(under development)
 
+    o	new class Rcpp::Dimension to support creation of vectors with 
+    	dimensions. All vector classes gain a constructor taking a 
+    	Dimension reference.
+
     o	matrix matrix-like indexing using operator() for all vector 
     	types : IntegerVector, NumericVector, RawVector, CharacterVector
     	LogicalVector, GenericVector and ExpressionVector. 

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2010-01-28 09:30:43 UTC (rev 501)
+++ pkg/inst/ChangeLog	2010-01-28 10:55:02 UTC (rev 502)
@@ -1,5 +1,17 @@
 2010-01-28  Romain Francois <francoisromain at free.fr>
 
+	* src/Rcpp/Dimension.h: new class Rcpp::Dimension to support
+	creation of vectors with dimensions
+	
+	* src/Rcpp/*Vector.h: using Rcpp::Dimension
+	
+	* src/Rcpp/SimpleVector.h: data is initialized with 0 when 
+	the object is constructed from size or Dimension constructor
+	
+	* inst/runit.CharacterVector.R: new unit test to test 
+	constructors using Dimension
+	* inst/runit.IntegerVector.R: idem
+	
 	* R/unit.tests.R: new unexported function "test" to trigger
 	unit tests using installed test cases
 

Modified: pkg/inst/unitTests/runit.CharacterVector.R
===================================================================
--- pkg/inst/unitTests/runit.CharacterVector.R	2010-01-28 09:30:43 UTC (rev 501)
+++ pkg/inst/unitTests/runit.CharacterVector.R	2010-01-28 10:55:02 UTC (rev 502)
@@ -142,3 +142,28 @@
 	checkEquals( funx(), c("foo", "bar", "bling", "boom"), msg = "assign(char**, char**)" )
 }
 
+test.CharacterVector.Dimension.constructor <- function(){
+
+	funx <- cfunction(signature(), '
+		return CharacterVector( Dimension( 5 ) ) ;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	checkEquals( funx(), 
+		character(5) , 
+		msg = "CharacterVector( Dimension(5))" )
+	
+	funx <- cfunction(signature(), '
+		return CharacterVector( Dimension( 5, 5 ) ) ;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	checkEquals( funx(), 
+		matrix( "", ncol = 5, nrow = 5) , 
+		msg = "CharacterVector( Dimension(5,5))" )
+	
+	funx <- cfunction(signature(), '
+		return CharacterVector( Dimension( 2, 3, 4) ) ;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	checkEquals( funx(), 
+		array( "", dim = c(2,3,4) ) , 
+		msg = "CharacterVector( Dimension(2,3,4))" )
+}
+
+

Modified: pkg/inst/unitTests/runit.IntegerVector.R
===================================================================
--- pkg/inst/unitTests/runit.IntegerVector.R	2010-01-28 09:30:43 UTC (rev 501)
+++ pkg/inst/unitTests/runit.IntegerVector.R	2010-01-28 10:55:02 UTC (rev 502)
@@ -78,7 +78,30 @@
 	
 	y <- as.vector( x )
 	checkException( funx(y) , msg = "not a matrix" )
+}
+
+test.IntegerVector.Dimension.constructor <- function(){
+
+	funx <- cfunction(signature(), '
+		return IntegerVector( Dimension( 5 ) ) ;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	checkEquals( funx(), 
+		integer(5) , 
+		msg = "IntegerVector( Dimension(5))" )
 	
+	funx <- cfunction(signature(), '
+		return IntegerVector( Dimension( 5, 5 ) ) ;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	checkEquals( funx(), 
+		matrix( 0L, ncol = 5, nrow = 5) , 
+		msg = "IntegerVector( Dimension(5,5))" )
 	
+	funx <- cfunction(signature(), '
+		return IntegerVector( Dimension( 2, 3, 4) ) ;
+	', Rcpp = TRUE, includes = "using namespace Rcpp;"  )
+	checkEquals( funx(), 
+		array( 0L, dim = c(2,3,4) ) , 
+		msg = "IntegerVector( Dimension(2,3,4))" )
 }
 
+

Modified: pkg/src/CharacterVector.cpp
===================================================================
--- pkg/src/CharacterVector.cpp	2010-01-28 09:30:43 UTC (rev 501)
+++ pkg/src/CharacterVector.cpp	2010-01-28 10:55:02 UTC (rev 502)
@@ -20,6 +20,7 @@
 // along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
 
 #include <Rcpp/CharacterVector.h>
+#include <Rcpp/wrap.h>
 
 namespace Rcpp{
 
@@ -41,8 +42,12 @@
 CharacterVector::CharacterVector( const std::vector<std::string>& x): VectorBase() {
 	assign( x.begin(), x.end() ) ;
 }
-	
 
+CharacterVector::CharacterVector( const Dimension& dims): VectorBase(){
+	setSEXP( Rf_allocVector( STRSXP, dims.prod() ) ) ;
+	if( dims.size() > 1 ) attr( "dim" ) = dims ;
+}
+
 /* proxy stuff */
 
 CharacterVector::StringProxy::StringProxy(CharacterVector& v, int i) :
@@ -101,22 +106,5 @@
 	return StringProxy(*this, offset(i,j) ) ;
 }
 
-// void CharacterVector::assign( const char** first, const char** last){
-// 	size_t size = std::distance( first, last ) ;
-// 	SEXP x = m_sexp ;
-// 	bool update = false ;
-// 	if( length() != size ){
-// 		x = Rf_allocVector( STRSXP, size ) ;
-// 		update = true ;
-// 	}
-// 	for( size_t i=0; i<size; i++, ++first){
-// 		SET_STRING_ELT( x, i, Rf_mkChar(*first)) ;
-// 	}
-// 	if( update ) setSEXP( x ) ;
-// }
-	
-// CharacterVector::CharacterVector( const char** const first, const char** const last) : VectorBase(){
-// 	assign( first, last ) ;
-// }
 
 } // namespace 

Added: pkg/src/Dimension.cpp
===================================================================
--- pkg/src/Dimension.cpp	                        (rev 0)
+++ pkg/src/Dimension.cpp	2010-01-28 10:55:02 UTC (rev 502)
@@ -0,0 +1,73 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// Dimension.h: Rcpp R/C++ interface class library -- dimensions
+//
+// 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 <Rcpp/Dimension.h>
+#include <Rcpp/as.h>
+#include <Rcpp/wrap.h>
+
+namespace Rcpp{
+
+	Dimension::Dimension() : dims(){}
+	
+	Dimension::Dimension(SEXP x): dims(){
+		dims = as< std::vector<int> >(x) ;
+	}
+	
+	Dimension::Dimension(const size_t& n1) : dims(1){
+		dims[0] = n1 ;
+	}
+	
+	Dimension::Dimension(const size_t& n1, const size_t& n2) : dims(2){
+		dims[0] = n1 ;
+		dims[1] = n2 ;
+	}
+	
+	Dimension::Dimension(const size_t& n1, const size_t& n2, const size_t& n3) : dims(3){
+		dims[0] = n1 ;
+		dims[1] = n2 ;
+		dims[2] = n3 ;
+	}
+	
+	Dimension::operator SEXP() const {
+		return wrap( dims ) ;
+	}
+	
+	int Dimension::size() const {
+		return static_cast<int>( dims.size() ) ;
+	}
+	
+	int Dimension::prod() const {
+		int prod = 1 ;
+		std::vector<int>::const_iterator begin = dims.begin() ;
+		std::vector<int>::const_iterator end   = dims.end() ;
+		while( begin != end ){
+			prod *= (*begin) ;
+			++begin ;
+		}
+		return prod ;
+	}
+	
+	int& Dimension::operator[](int i) throw(std::range_error){
+		if( i < 0 || i>=static_cast<int>(dims.size()) ) throw std::range_error("index out of bounds") ;
+		return dims.at(i) ;
+	}
+	
+} // namespace Rcpp

Modified: pkg/src/Rcpp/CharacterVector.h
===================================================================
--- pkg/src/Rcpp/CharacterVector.h	2010-01-28 09:30:43 UTC (rev 501)
+++ pkg/src/Rcpp/CharacterVector.h	2010-01-28 10:55:02 UTC (rev 502)
@@ -26,6 +26,7 @@
 #include <Rcpp/RObject.h>
 #include <Rcpp/VectorBase.h>
 #include <Rcpp/r_cast.h>
+#include <Rcpp/Dimension.h>
 
 namespace Rcpp{ 
 
@@ -62,6 +63,8 @@
 	CharacterVector( const std::string& x );
 	CharacterVector( const std::vector<std::string>& x );
 	
+	CharacterVector( const Dimension& dims) ;
+	
 	template <typename InputIterator>
 	CharacterVector( InputIterator first, InputIterator last): VectorBase() {
 		assign( first, last ) ;

Added: pkg/src/Rcpp/Dimension.h
===================================================================
--- pkg/src/Rcpp/Dimension.h	                        (rev 0)
+++ pkg/src/Rcpp/Dimension.h	2010-01-28 10:55:02 UTC (rev 502)
@@ -0,0 +1,48 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// Dimension.h: Rcpp R/C++ interface class library -- dimensions
+//
+// 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_Dimension_h
+#define Rcpp_Dimension_h
+
+#include <RcppCommon.h>
+
+namespace Rcpp{ 
+
+class Dimension {
+public:
+	Dimension() ;
+	Dimension(SEXP dims);
+	Dimension(const size_t& n1) ;
+	Dimension(const size_t& n1, const size_t& n2) ;
+	Dimension(const size_t& n1, const size_t& n2, const size_t& n3) ;
+	operator SEXP() const ;
+	
+	int size() const ;
+	int prod() const ;
+	
+	int& operator[](int i) throw(std::range_error) ;
+	
+private:
+	std::vector<int> dims ;
+} ;
+
+}
+#endif

Modified: pkg/src/Rcpp/SEXP_Vector.h
===================================================================
--- pkg/src/Rcpp/SEXP_Vector.h	2010-01-28 09:30:43 UTC (rev 501)
+++ pkg/src/Rcpp/SEXP_Vector.h	2010-01-28 10:55:02 UTC (rev 502)
@@ -80,6 +80,11 @@
 	SEXP_Vector(const size_t& size) : VectorBase(){
 		setSEXP( Rf_allocVector( RTYPE, size ) ) ;
 	}
+	
+	SEXP_Vector(const Dimension& dims) : VectorBase(){
+		setSEXP( Rf_allocVector( RTYPE, dims.prod() ) ) ;
+		if( dims.size() > 1) attr( "dim" ) = dims ;
+	}
 
 #ifdef HAS_INIT_LISTS
 	SEXP_Vector( std::initializer_list<SEXP> list) : VectorBase(){

Modified: pkg/src/Rcpp/SimpleVector.h
===================================================================
--- pkg/src/Rcpp/SimpleVector.h	2010-01-28 09:30:43 UTC (rev 501)
+++ pkg/src/Rcpp/SimpleVector.h	2010-01-28 10:55:02 UTC (rev 502)
@@ -26,9 +26,10 @@
 #include <Rcpp/RObject.h>
 #include <Rcpp/VectorBase.h>
 #include <Rcpp/r_cast.h>
+#include <Rcpp/Dimension.h>
 
 namespace Rcpp{
-
+	
 template <int RTYPE, typename CTYPE>
 class SimpleVector : public VectorBase {
 public:
@@ -41,8 +42,17 @@
 	
 	SimpleVector( const size_t& size){
 		setSEXP( Rf_allocVector( RTYPE, size) ) ;
+		init() ;
 	}
 	
+	SimpleVector( const Dimension& dims){
+		setSEXP( Rf_allocVector( RTYPE, dims.prod() ) ) ;
+		init() ;
+		if( dims.size() > 1 ){
+			attr( "dim" ) = dims ;
+		}
+	}
+	
 #ifdef HAS_INIT_LISTS
 	SimpleVector( std::initializer_list<CTYPE> list ) : VectorBase(), start(0){
 		simple_fill( list.begin() , list.end() ) ;
@@ -81,6 +91,14 @@
 		UNPROTECT(1) ;
 	}
 	
+	void init(){
+		size_t n = static_cast<size_t>(length()) ;
+		CTYPE zero = static_cast<CTYPE>(0) ;
+		for( size_t i=0; i<n; i++){
+			start[i] = zero;
+		}
+	}
+	
 } ;
 
 }// namespace Rcpp

Modified: pkg/src/Rcpp/wrap.h
===================================================================
--- pkg/src/Rcpp/wrap.h	2010-01-28 09:30:43 UTC (rev 501)
+++ pkg/src/Rcpp/wrap.h	2010-01-28 10:55:02 UTC (rev 502)
@@ -32,6 +32,8 @@
 #include <Rcpp/LogicalVector.h>
 #include <Rcpp/CharacterVector.h>
 
+#include <Rcpp/Dimension.h>
+
 namespace Rcpp{ 
 
 // factories

Modified: pkg/src/Rcpp.h
===================================================================
--- pkg/src/Rcpp.h	2010-01-28 09:30:43 UTC (rev 501)
+++ pkg/src/Rcpp.h	2010-01-28 10:55:02 UTC (rev 502)
@@ -48,6 +48,7 @@
 #include <Rcpp/grow.h>
 #include <Rcpp/wrap.h>
 #include <Rcpp/as.h>
+#include <Rcpp/Dimension.h>
 #include <Rcpp/RObject.h>
 #include <Rcpp/VectorBase.h>
 #include <Rcpp/SimpleVector.h>

Modified: pkg/src/RcppCommon.h
===================================================================
--- pkg/src/RcppCommon.h	2010-01-28 09:30:43 UTC (rev 501)
+++ pkg/src/RcppCommon.h	2010-01-28 10:55:02 UTC (rev 502)
@@ -109,7 +109,7 @@
 	class Pairlist ;
 	class Function ;
 	class WeakReference;
-
+	
 /* internal namespace for things not intended to be used by the 
    user */
 namespace internal{	



More information about the Rcpp-commits mailing list