[Rcpp-commits] r1311 - in pkg/RcppGSL: . inst inst/include inst/unitTests src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue May 25 10:53:14 CEST 2010


Author: romain
Date: 2010-05-25 10:53:13 +0200 (Tue, 25 May 2010)
New Revision: 1311

Modified:
   pkg/RcppGSL/DESCRIPTION
   pkg/RcppGSL/inst/ChangeLog
   pkg/RcppGSL/inst/include/RcppGSLForward.h
   pkg/RcppGSL/inst/unitTests/runit.gsl.R
   pkg/RcppGSL/src/RcppGSL.cpp
Log:
adding STL capabilities to RcppGSL::vector

Modified: pkg/RcppGSL/DESCRIPTION
===================================================================
--- pkg/RcppGSL/DESCRIPTION	2010-05-25 07:58:36 UTC (rev 1310)
+++ pkg/RcppGSL/DESCRIPTION	2010-05-25 08:53:13 UTC (rev 1311)
@@ -1,7 +1,7 @@
 Package: RcppGSL
 Type: Package
 Title: Glue between Rcpp and the GNU GSL
-Version: 0.0.2
+Version: 0.0.3
 Date: 2010-04-04
 Author: Romain Francois and Dirk Eddelbuettel
 Maintainer: Dirk Eddelbuettel and Romain Francois <RomainAndDirk at r-enthusiasts.com>

Modified: pkg/RcppGSL/inst/ChangeLog
===================================================================
--- pkg/RcppGSL/inst/ChangeLog	2010-05-25 07:58:36 UTC (rev 1310)
+++ pkg/RcppGSL/inst/ChangeLog	2010-05-25 08:53:13 UTC (rev 1311)
@@ -1,3 +1,8 @@
+2010-05-25  Romain Francois <romain at r-enthusiasts.com>
+
+	* inst/include/RcppGSLForward.h : add indexing operator, stl iterator and 
+	begin() and end() methods to RcppGSL::vector using proxy classes
+
 2010-05-13  Dirk Eddelbuettel  <edd at debian.org>
 
 	* R/fastLm.R: fastLm is now generic and behaves similar to lm():

Modified: pkg/RcppGSL/inst/include/RcppGSLForward.h
===================================================================
--- pkg/RcppGSL/inst/include/RcppGSLForward.h	2010-05-25 07:58:36 UTC (rev 1310)
+++ pkg/RcppGSL/inst/include/RcppGSLForward.h	2010-05-25 08:53:13 UTC (rev 1311)
@@ -98,13 +98,28 @@
 	template <typename T> class matrix ;
 	
 #undef _RCPPGSL_SPEC
-#define _RCPPGSL_SPEC(__T__,__SUFFIX__,__CAST__)      	                        \
+#define _RCPPGSL_SPEC(__T__,__SUFFIX__,__CAST__)                                 \
 template <> class vector<__T__>  {           	                                   \
 public:                                      	                                   \
 	typedef __T__ type ;                     	                                   \
 	typedef __T__* pointer ;                 	                                   \
 	typedef gsl_vector##__SUFFIX__ gsltype ; 	                                   \
-	gsltype* data ;                          	                                   \
+	gsltype* data ;                            	                               \
+	class Proxy {                                                                \
+	public:                                                                      \
+		Proxy( gsltype* data_, int index_ ) :  index(index_), parent(data_){}    \
+		Proxy& operator=( type x) {                                              \
+			gsl_vector##__SUFFIX__##_set( parent, index, x ) ;                     \
+			return *this ;                                                       \
+		}                                                                        \
+		inline operator type() {                                                 \
+			return gsl_vector##__SUFFIX__##_get( parent, index ) ;               \
+		}                                                                        \
+		inline void move(int d){ index += d ; }                                  \
+		int index ;                                                              \
+		gsltype* parent ;                                                        \
+	} ;                                                                          \
+	typedef ::Rcpp::internal::Proxy_Iterator<Proxy> iterator ;                   \
 	const static int RTYPE = ::Rcpp::traits::r_sexptype_traits<type>::rtype ;    \
 	vector( SEXP x) throw(::Rcpp::not_compatible) : data(0) {                    \
 		SEXP y = ::Rcpp::r_cast<RTYPE>(x) ;                                      \
@@ -125,8 +140,13 @@
 		data = other.data ;                                                      \
 		return *this ;                                                           \
 	}                                                                            \
-	inline size_t size(){ return data->size ; }                                  \
-	void free(){                                                                 \
+    inline Proxy operator[]( int i ) {                                           \
+    	return Proxy( data, i ) ;                                                \
+    }                                                                            \
+	inline iterator begin(){ return iterator( Proxy(*this, 0 ) ) ; }             \
+    inline iterator end(){ return iterator( Proxy(*this, data->size ) ) ; }      \
+    inline size_t size(){ return data->size ; }                                  \
+	inline void free(){                                                          \
 		gsl_vector##__SUFFIX__##_free(data) ;                                    \
 	}                                                                            \
 } ;                                                                              \
@@ -176,7 +196,7 @@
 _RCPPGSL_SPEC(gsl_complex_long_double  , _complex_long_double  , gsl_complex_long_double )
 
 #undef _RCPPGSL_SPEC
- 
+
 }
 
 

Modified: pkg/RcppGSL/inst/unitTests/runit.gsl.R
===================================================================
--- pkg/RcppGSL/inst/unitTests/runit.gsl.R	2010-05-25 07:58:36 UTC (rev 1310)
+++ pkg/RcppGSL/inst/unitTests/runit.gsl.R	2010-05-25 08:53:13 UTC (rev 1311)
@@ -122,3 +122,14 @@
 	checkEquals( res, 0:9, msg = "RcppGSL::vector<int> -> IntegerVector" )
 }
 
+test.gsl.RcppGSL.vector.indexing <- function(){
+	res <- .Call( "test_gsl_vector_indexing", seq(0.5, 10.5), PACKAGE = "RcppGSL" )
+	checkEquals( res, seq( 1.5, 11.5 ) )
+}
+
+test.gsl.RcppGSL.vector.iterating <- function(){
+	x   <-  seq(0.5, 10.5)
+	res <- .Call( "test_gsl_vector_iterating", x , PACKAGE = "RcppGSL" )
+	checkEquals( res, sum(x) )
+}
+

Modified: pkg/RcppGSL/src/RcppGSL.cpp
===================================================================
--- pkg/RcppGSL/src/RcppGSL.cpp	2010-05-25 07:58:36 UTC (rev 1310)
+++ pkg/RcppGSL/src/RcppGSL.cpp	2010-05-25 08:53:13 UTC (rev 1311)
@@ -248,3 +248,14 @@
 	return x ;
 }
 
+RCPP_FUNCTION_1(RcppGSL::vector<double>, test_gsl_vector_indexing, RcppGSL::vector<double> vec ){
+	for( size_t i=0; i< vec.size(); i++){
+		vec[i] = vec[i] + 1.0 ;
+	}
+	return vec ;
+}
+
+RCPP_FUNCTION_1(double, test_gsl_vector_iterating, RcppGSL::vector<double> vec ){
+	return std::accumulate( vec.begin(), vec.end(), 0.0 ); 
+}
+



More information about the Rcpp-commits mailing list