[Rcpp-commits] r3942 - in pkg/Rcpp: . inst/include/Rcpp/vector

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Nov 12 13:47:58 CET 2012


Author: romain
Date: 2012-11-12 13:47:58 +0100 (Mon, 12 Nov 2012)
New Revision: 3942

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/vector/00_forward_proxy.h
   pkg/Rcpp/inst/include/Rcpp/vector/CharacterVectorExtractionIterator.h
   pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h
   pkg/Rcpp/inst/include/Rcpp/vector/Vector.h
   pkg/Rcpp/inst/include/Rcpp/vector/proxy.h
   pkg/Rcpp/inst/include/Rcpp/vector/traits.h
Log:
added const_iterator and associated begin() and end() methods

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2012-11-11 23:07:45 UTC (rev 3941)
+++ pkg/Rcpp/ChangeLog	2012-11-12 12:47:58 UTC (rev 3942)
@@ -1,3 +1,9 @@
+2012-11-12  Romain Francois <romain at r-enthusiasts.com>
+
+        * include/Rcpp/vector/Vector.h : adding const_iterator and associated 
+        begin() and end() methods
+        * include/Rcpp/vector/Matrix.h : idem
+        
 2012-11-11  JJ Allaire <jj at rstudio.org>
 
         * src/Attributes.cpp: fully qualify _ in generated code
@@ -5,7 +11,7 @@
 2012-11-11  Romain Francois <romain at r-enthusiasts.com>
 
         * include/Rcpp/iostream/Rstreambuf.h: implementing sync() so that
-	flush works
+        flush works
         * src/Rstreambuf.cpp: implementation
         * src/Rostream.cpp : added Rcerr that forwards to REprintf
 

Modified: pkg/Rcpp/inst/include/Rcpp/vector/00_forward_proxy.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/00_forward_proxy.h	2012-11-11 23:07:45 UTC (rev 3941)
+++ pkg/Rcpp/inst/include/Rcpp/vector/00_forward_proxy.h	2012-11-12 12:47:58 UTC (rev 3942)
@@ -2,7 +2,7 @@
 //
 // proxy.h: Rcpp R/C++ interface class library -- proxies
 //
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -22,6 +22,9 @@
 #ifndef Rcpp__vector__forward_proxy_h
 #define Rcpp__vector__forward_proxy_h
 
+// const iterator for CharacterVector
+class CharacterVectorExtractionIterator ;
+
 namespace internal{
 	template <int RTYPE> class string_proxy ;
 	template <int RTYPE> class generic_proxy ;
@@ -31,10 +34,11 @@
 }
 
 namespace traits {
-
+    template <int RTYPE> struct r_vector_cache_type ;
 	template <int RTYPE> struct r_vector_name_proxy ;
 	template <int RTYPE> struct r_vector_proxy ;
 	template <int RTYPE> struct r_vector_iterator ;
+	template <int RTYPE> struct r_vector_const_iterator ;
 	template <int RTYPE> class r_vector_cache ;
 }
 

Modified: pkg/Rcpp/inst/include/Rcpp/vector/CharacterVectorExtractionIterator.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/CharacterVectorExtractionIterator.h	2012-11-11 23:07:45 UTC (rev 3941)
+++ pkg/Rcpp/inst/include/Rcpp/vector/CharacterVectorExtractionIterator.h	2012-11-12 12:47:58 UTC (rev 3942)
@@ -22,6 +22,9 @@
 #ifndef Rcpp__vector__CharacterVectorExtractionIterator_h
 #define Rcpp__vector__CharacterVectorExtractionIterator_h
 
+// const iterator for CharacterVector
+// this is faster than the regular proxy iterator because it works directly 
+// on a SEXP*. 
 class CharacterVectorExtractionIterator {
 public:
     typedef int difference_type;
@@ -62,8 +65,4 @@
     
 };
 
-inline CharacterVectorExtractionIterator input_iterator( const CharacterVector& vec){
-    return CharacterVectorExtractionIterator(vec) ;  
-}
-
 #endif

Modified: pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h	2012-11-11 23:07:45 UTC (rev 3941)
+++ pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h	2012-11-12 12:47:58 UTC (rev 3942)
@@ -2,7 +2,7 @@
 //
 // Matrix.h: Rcpp R/C++ interface class library -- matrices
 //
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -30,6 +30,7 @@
         
     typedef Vector<RTYPE> VECTOR ;
     typedef typename VECTOR::iterator iterator ;
+    typedef typename VECTOR::const_iterator const_iterator ;
     typedef typename VECTOR::converter_type converter_type ;
     typedef typename VECTOR::stored_type stored_type ;
     typedef typename VECTOR::Proxy Proxy ;
@@ -213,8 +214,10 @@
     inline Row row( int i ){ return Row( *this, i ) ; }
     inline Column column( int i ){ return Column(*this, i ) ; }
         
-    inline iterator begin() const{ return VECTOR::begin() ; }
-    inline iterator end() const{ return VECTOR::end() ; }
+    inline const_iterator begin() const{ return VECTOR::begin() ; }
+    inline const_iterator end() const{ return VECTOR::end() ; }
+    inline iterator begin() { return VECTOR::begin() ; }
+    inline iterator end() { return VECTOR::end() ; }
     
 private:
     int nrows ; 

Modified: pkg/Rcpp/inst/include/Rcpp/vector/Vector.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/Vector.h	2012-11-11 23:07:45 UTC (rev 3941)
+++ pkg/Rcpp/inst/include/Rcpp/vector/Vector.h	2012-11-12 12:47:58 UTC (rev 3942)
@@ -33,6 +33,7 @@
     typedef typename traits::r_vector_name_proxy<RTYPE>::type NameProxy ;
     typedef typename traits::r_vector_proxy<RTYPE>::type value_type ;
     typedef typename traits::r_vector_iterator<RTYPE>::type iterator ;
+    typedef typename traits::r_vector_const_iterator<RTYPE>::type const_iterator ;
     typedef typename traits::init_type<RTYPE>::type init_type ;
     typedef typename traits::r_vector_element_converter<RTYPE>::type converter_type ;
     typedef typename traits::storage_type<RTYPE>::type stored_type ;
@@ -388,8 +389,10 @@
         return NamesProxy(*this) ;
     }
     
-    inline iterator begin() const{ return cache.get() ; }
-    inline iterator end() const{ return cache.get(size()) ; }
+    inline iterator begin() { return cache.get() ; }
+    inline iterator end() { return cache.get() + size() ; }
+	inline const_iterator begin() const{ return cache.get_const() ; }
+    inline const_iterator end() const{ return cache.get_const() + size() ; }
 	
     inline Proxy operator[]( int i ){ return cache.ref(i) ; }
     inline Proxy operator[]( int i ) const { return cache.ref(i) ; }
@@ -476,13 +479,13 @@
 	
     template <typename T>
     iterator insert( int position, const T& object){
-        return insert__impl( cache.get(position), converter_type::get(object), 
+        return insert__impl( cache.get() + position, converter_type::get(object), 
                              typename traits::same_type<stored_type,SEXP>()
                              ); 
     }
 	
     iterator erase( int position){
-        return erase_single__impl( cache.get(position) ) ;
+        return erase_single__impl( cache.get() + position) ;
     }
 	
     iterator erase( iterator position){
@@ -490,7 +493,8 @@
     }
 	
     iterator erase( int first, int last){
-        return erase_range__impl( cache.get(first), cache.get(last) ) ;
+        iterator start = cache.get() ;
+        return erase_range__impl( start + first, start + last ) ;
     }
 	
     iterator erase( iterator first, iterator last){
@@ -651,7 +655,7 @@
         update_vector() ;
     }
 	
-    traits::r_vector_cache<RTYPE> cache ;
+    typename traits::r_vector_cache_type<RTYPE>::type cache ;
 
 public:
 	

Modified: pkg/Rcpp/inst/include/Rcpp/vector/proxy.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/proxy.h	2012-11-11 23:07:45 UTC (rev 3941)
+++ pkg/Rcpp/inst/include/Rcpp/vector/proxy.h	2012-11-12 12:47:58 UTC (rev 3942)
@@ -2,7 +2,7 @@
 //
 // proxy.h: Rcpp R/C++ interface class library -- proxies
 //
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -229,6 +229,11 @@
 	struct r_vector_iterator {
 		typedef typename storage_type<RTYPE>::type* type ;
 	};
+	template <int RTYPE>
+	struct r_vector_const_iterator {
+		typedef typename storage_type<RTYPE>::type* type ;
+	};
+	
 	template <int RTYPE> struct proxy_based_iterator{
 		typedef ::Rcpp::internal::Proxy_Iterator< typename r_vector_proxy<RTYPE>::type > type ;
 	} ;
@@ -236,6 +241,13 @@
 	template<> struct r_vector_iterator<EXPRSXP> : proxy_based_iterator<EXPRSXP>{} ;
 	template<> struct r_vector_iterator<STRSXP> : proxy_based_iterator<STRSXP>{} ;
 
+	template<> struct r_vector_const_iterator<VECSXP> : proxy_based_iterator<VECSXP>{} ;
+	template<> struct r_vector_const_iterator<EXPRSXP> : proxy_based_iterator<EXPRSXP>{} ;
+	
+	template<> struct r_vector_const_iterator<STRSXP> {
+	    typedef CharacterVectorExtractionIterator type ;
+	} ;
+
 }  // traits
 
 

Modified: pkg/Rcpp/inst/include/Rcpp/vector/traits.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/traits.h	2012-11-11 23:07:45 UTC (rev 3941)
+++ pkg/Rcpp/inst/include/Rcpp/vector/traits.h	2012-11-12 12:47:58 UTC (rev 3942)
@@ -2,7 +2,7 @@
 //
 // traits.h: Rcpp R/C++ interface class library -- support traits for vector
 //
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -29,6 +29,7 @@
 	public:
 		typedef typename ::Rcpp::Vector<RTYPE> VECTOR ;
 		typedef typename r_vector_iterator<RTYPE>::type iterator ;
+		typedef typename r_vector_const_iterator<RTYPE>::type const_iterator ;
 		typedef typename r_vector_proxy<RTYPE>::type proxy ;
 		typedef typename storage_type<RTYPE>::type storage_type ;
 		
@@ -38,7 +39,7 @@
 			RCPP_DEBUG_3( " cache<%d>::update( <%p> ), start = <%p>", RTYPE, reinterpret_cast<void*>(v.asSexp()),  reinterpret_cast<void*>(start) ) ;
 		}
 		inline iterator get() const { return start; }
-		inline iterator get(int i) const { return start + i ; }
+		inline const_iterator get_const() const { return start; }
 		
 		inline proxy ref() { return *start ;}
 		inline proxy ref(int i) { return start[i] ; }
@@ -53,6 +54,7 @@
 	public:
 		typedef typename ::Rcpp::Vector<RTYPE> VECTOR ;
 		typedef typename r_vector_iterator<RTYPE>::type iterator ;
+		typedef typename r_vector_const_iterator<RTYPE>::type const_iterator ;
 		typedef typename r_vector_proxy<RTYPE>::type proxy ;
 		
 		proxy_cache(): p(0){}
@@ -62,25 +64,22 @@
 			RCPP_DEBUG_3( " cache<%d>::update( <%p> ), p = <%p>", RTYPE, reinterpret_cast<void*>(v.asSexp()),  reinterpret_cast<void*>(p) ) ;
 		}
 		inline iterator get() const { return iterator( proxy(*p, 0 ) ) ;}
-		inline iterator get(int i) const { return iterator( proxy(*p, i ) ) ;}
+		inline iterator get_const() const { return const_iterator( proxy(*p, 0 ) ) ;}
+		
 		inline proxy ref() const { return proxy(*p,0) ; }
 		inline proxy ref(int i) const { return proxy(*p,i);}
 		
 		private:
 			VECTOR* p ;
 	} ;
-	template<> class r_vector_cache<VECSXP> : public proxy_cache<VECSXP>{
-	public:
-		r_vector_cache() : proxy_cache<VECSXP>(){} ;
-	};
-	template<> class r_vector_cache<EXPRSXP> : public proxy_cache<EXPRSXP>{
-	public:
-		r_vector_cache() : proxy_cache<EXPRSXP>(){} ;
-	};
-	template<> class r_vector_cache<STRSXP> : public proxy_cache<STRSXP>{
-	public:
-		r_vector_cache() : proxy_cache<STRSXP>(){} ;
-	} ;
+	
+	// regular types for INTSXP, REALSXP, ...
+	template <int RTYPE> struct r_vector_cache_type { typedef r_vector_cache<RTYPE> type ;  } ;
+	
+	// proxy types for VECSXP, STRSXP and EXPRSXP
+	template <> struct r_vector_cache_type<VECSXP>  { typedef proxy_cache<VECSXP> type ;  } ;
+	template <> struct r_vector_cache_type<EXPRSXP> { typedef proxy_cache<EXPRSXP> type ; } ;
+	template <> struct r_vector_cache_type<STRSXP>  { typedef proxy_cache<STRSXP> type ;  } ;
 		
 } // traits 
 



More information about the Rcpp-commits mailing list