[Rcpp-commits] r4493 - in pkg/Rcpp: . inst/include/Rcpp/api/meat inst/include/Rcpp/internal

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Sep 16 11:54:18 CEST 2013


Author: romain
Date: 2013-09-16 11:54:18 +0200 (Mon, 16 Sep 2013)
New Revision: 4493

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/api/meat/export.h
   pkg/Rcpp/inst/include/Rcpp/internal/Exporter.h
Log:
faster as< std::vector<double> >

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2013-09-16 00:50:39 UTC (rev 4492)
+++ pkg/Rcpp/ChangeLog	2013-09-16 09:54:18 UTC (rev 4493)
@@ -1,3 +1,10 @@
+2013-09-16 Romain Francois <romain at r-enthusiasts.com>
+
+        * include/Rcpp/internal/Exporter.h : Specific handling of containers (std::vector,
+        std::deque, and std::list so that we use their faster range constructor when 
+        we can, and so let the STL optimize how data is copied
+        * include/Rcpp/api/meat/export.h : Implementation of the above
+
 2013-09-15  Dirk Eddelbuettel  <edd at debian.org>
 
 	* inst/include/Rcpp/InputParameter.h (Rcpp): Add 'const' case

Modified: pkg/Rcpp/inst/include/Rcpp/api/meat/export.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/api/meat/export.h	2013-09-16 00:50:39 UTC (rev 4492)
+++ pkg/Rcpp/inst/include/Rcpp/api/meat/export.h	2013-09-16 09:54:18 UTC (rev 4493)
@@ -35,6 +35,38 @@
                 
     
 } // namespace internal
+
+    namespace traits{
+        
+        template < template <class, class> class ContainerTemplate, typename T > class ContainerExporter {
+        public:
+            typedef ContainerTemplate<T, std::allocator<T> > Container ;
+            const static int RTYPE = Rcpp::traits::r_sexptype_traits<T>::rtype ;
+            
+            ContainerExporter( SEXP x ) : object(x){}
+            ~ContainerExporter(){}
+            
+            Container get(){
+                if( TYPEOF(object) == RTYPE ){
+                   T* start = Rcpp::internal::r_vector_start<RTYPE>(object) ;
+                   return Container( start, start + Rf_length(object) ) ;
+                }
+                Container vec( ::Rf_length(object) );
+                ::Rcpp::internal::export_range( object, vec.begin() ) ;
+                return vec ;
+            }
+        
+        private:
+        		SEXP object ;
+        } ;
+        template < template<class,class> class Container > struct container_exporter< Container, int >{
+            typedef ContainerExporter< Container, int > type ;     
+        } ;
+        template < template<class,class> class Container > struct container_exporter< Container, double >{
+            typedef ContainerExporter< Container, double > type ;
+        } ;
+        
+    }
 } // namespace Rcpp
 
 #endif

Modified: pkg/Rcpp/inst/include/Rcpp/internal/Exporter.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/internal/Exporter.h	2013-09-16 00:50:39 UTC (rev 4492)
+++ pkg/Rcpp/inst/include/Rcpp/internal/Exporter.h	2013-09-16 09:54:18 UTC (rev 4493)
@@ -1,9 +1,8 @@
 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
-/* :tabSize=4:indentSize=4:noTabs=false:folding=explicit:collapseFolds=1: */
 //
 // exporter.h: Rcpp R/C++ interface class library -- identify if a class has a nested iterator typedef
 //
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -26,33 +25,33 @@
 namespace Rcpp{
     namespace traits{
 
-        template <typename T> class Exporter{
-        public:
-            Exporter( SEXP x ) : t(x){}
-            inline T get(){ return t ; }
-
-        private:
-            T t ;
-        } ;
-
-        template <typename T> class RangeExporter {
-        public:
-            typedef typename T::value_type r_export_type ;
+		template <typename T> class Exporter{
+		public:
+		    Exporter( SEXP x ) : t(x){}
+		    inline T get(){ return t ; }
+		
+		private:
+		    T t ;
+		} ;
+		
+		template <typename T> class RangeExporter {
+		public:
+		    typedef typename T::value_type r_export_type ;
+		
+		    RangeExporter( SEXP x ) : object(x){}
+		    ~RangeExporter(){}
+		
+		    T get(){ 
+		        T vec( ::Rf_length(object) );
+		        ::Rcpp::internal::export_range( object, vec.begin() ) ;
+		        return vec ;
+		    }
+		
+		private:
+		    SEXP object ;
+		} ;
         
-            RangeExporter( SEXP x ) : object(x){}
-            ~RangeExporter(){}
-        
-            T get(){ 
-                T vec( ::Rf_length(object) );
-                ::Rcpp::internal::export_range( object, vec.begin() ) ;
-                return vec ;
-            }
-        
-        private:
-            SEXP object ;
-        } ;
-
-        template <typename T, typename value_type> class IndexingExporter {
+		template <typename T, typename value_type> class IndexingExporter {
         public:
             typedef value_type r_export_type ;
         
@@ -92,20 +91,26 @@
             SEXP object ;
         } ;
 
-
-        template <typename T> class Exporter< std::vector<T> > : public RangeExporter< std::vector<T> > {
+        template < template<class,class> class Container, typename T>
+        struct container_exporter{
+        		typedef RangeExporter< Container<T, std::allocator<T> > > type ;
+        } ;
+        template < template<class,class> class Container > struct container_exporter< Container, int > ;
+        template < template<class,class> class Container > struct container_exporter< Container, double > ;
+        
+        template <typename T> class Exporter< std::vector<T> > : public container_exporter< std::vector, T>::type {
         public:
-            Exporter(SEXP x) : RangeExporter< std::vector<T> >(x){}
+            Exporter(SEXP x) : container_exporter< std::vector, T>::type(x){}
         };
-        template <typename T> class Exporter< std::deque<T> > : public RangeExporter< std::deque<T> > {
+        template <typename T> class Exporter< std::deque<T> > : public container_exporter< std::deque, T>::type {
         public:
-            Exporter(SEXP x) : RangeExporter< std::deque<T> >(x){}
+            Exporter(SEXP x) : container_exporter< std::deque, T>::type(x){}
         };
-        template <typename T> class Exporter< std::list<T> > : public RangeExporter< std::list<T> > {
+        template <typename T> class Exporter< std::list<T> > : public container_exporter< std::list, T>::type {
         public:
-            Exporter(SEXP x) : RangeExporter< std::list<T> >(x){}
+            Exporter(SEXP x) : container_exporter< std::list, T>::type(x){}
         };
-
+        
     } // namespace traits
 } // namespace Rcpp
 #endif



More information about the Rcpp-commits mailing list