[Rcpp-commits] r4017 - in pkg/Rcpp/inst/include/Rcpp: sugar/functions vector

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Nov 22 14:14:48 CET 2012


Author: romain
Date: 2012-11-22 14:14:48 +0100 (Thu, 22 Nov 2012)
New Revision: 4017

Modified:
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/match.h
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/unique.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:
new ctor for Vector using transform

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/match.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/match.h	2012-11-22 11:16:57 UTC (rev 4016)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/match.h	2012-11-22 13:14:48 UTC (rev 4017)
@@ -25,13 +25,12 @@
 namespace Rcpp{
 namespace sugar{
 
-template <typename HASH>
+template <typename HASH, typename STORAGE>
 class HashIndexInserter {
 public:
     HashIndexInserter( HASH& hash_ ) : hash(hash_), index(1){}
     
-    template <typename T>
-    inline void operator()( T value ){
+    inline void operator()( STORAGE value ){
         hash.insert( std::make_pair(value, index++) ) ;
     }
     
@@ -39,13 +38,12 @@
     HASH& hash ;
     int index;
 } ; 
-template <typename HASH>
+template <typename HASH, typename STORAGE>
 class HashIndexFinder {
 public:
     HashIndexFinder( HASH& hash_) : hash(hash_), end(hash.end()) {}
     
-    template <typename T>
-    inline int operator()( T value ){
+    inline int operator()( STORAGE value ){
         typename HASH::const_iterator it = hash.find(value);
         if( it == end ){
             return NA_INTEGER ;    
@@ -64,25 +62,22 @@
 template <int RTYPE, typename TABLE_T>        
 class IndexHash {
 public:
+    typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+    
     IndexHash( const TABLE_T& table ): hash() {
-        for_each( get_const_begin(table), get_const_end(table), HashIndexInserter<HASH>(hash) ) ;
+        for_each( table.begin(), table.end(), Inserter(hash) ) ;
     }
     
     template <typename T>
     IntegerVector match( const T& obj ){
-        int n=obj.size() ;
-        IntegerVector out = no_init(n) ;
-        std::transform( 
-            get_const_begin(obj), get_const_end(obj), 
-            out.begin(), 
-            HashIndexFinder<HASH>(hash)
-        ) ;
+        IntegerVector out( obj.begin(), obj.end(), Finder(hash) ) ;
         return out ;
     }
  
 private:
-    typedef RCPP_UNORDERED_MAP< typename Rcpp::traits::storage_type<RTYPE>::type, int > HASH ;
-    typedef typename HASH::const_iterator HASH_iterator ;
+    typedef RCPP_UNORDERED_MAP<STORAGE,int> HASH ;
+    typedef HashIndexInserter<HASH,STORAGE> Inserter ;
+    typedef HashIndexFinder<HASH,STORAGE> Finder ;
     HASH hash ;    
 }; 
     

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/unique.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/unique.h	2012-11-22 11:16:57 UTC (rev 4016)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/unique.h	2012-11-22 13:14:48 UTC (rev 4017)
@@ -25,9 +25,6 @@
 namespace Rcpp{
 namespace sugar{
 
-    
-    
-    
 template <int RTYPE, typename T>
 class Unique {
 public:

Modified: pkg/Rcpp/inst/include/Rcpp/vector/Vector.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/Vector.h	2012-11-22 11:16:57 UTC (rev 4016)
+++ pkg/Rcpp/inst/include/Rcpp/vector/Vector.h	2012-11-22 13:14:48 UTC (rev 4017)
@@ -231,6 +231,18 @@
         assign( first, last ) ;
     }
 
+    template <typename InputIterator, typename Func>
+    Vector( InputIterator first, InputIterator last, Func func) : 
+        RObject( Rf_allocVector( RTYPE, std::distance(first,last) ) )
+    {
+        std::transform( first, last, begin(), func) ;
+    }
+    
+    template <typename InputIterator, typename Func>
+    Vector( InputIterator first, InputIterator last, Func func, int n) : RObject( Rf_allocVector( RTYPE, n ) ){
+        std::transform( first, last, begin(), func) ;
+    }
+
     Vector( const std::string& st ) : RObject(){
         RObject::setSEXP( internal::vector_from_string<RTYPE>(st) );
     }
@@ -413,10 +425,7 @@
 
     template <typename InputIterator, typename F>
     static Vector import_transform( InputIterator first, InputIterator last, F f){
-        int n = std::distance( first, last ) ;
-        Vector v( n ) ;
-        std::transform( first, last, v.begin(), f) ;
-        return v ;
+        return Vector( first, last, f) ;
     }
 	
     template <typename T>

Modified: pkg/Rcpp/inst/include/Rcpp/vector/proxy.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/proxy.h	2012-11-22 11:16:57 UTC (rev 4016)
+++ pkg/Rcpp/inst/include/Rcpp/vector/proxy.h	2012-11-22 13:14:48 UTC (rev 4017)
@@ -228,16 +228,16 @@
 	template <int RTYPE>
 	struct r_vector_const_proxy{
 		typedef const typename storage_type<RTYPE>::type& type ;
-	} ;
-	template<> struct r_vector_const_proxy<STRSXP> {
-		typedef ::Rcpp::internal::string_proxy<STRSXP> type ;
-	} ;
-	template<> struct r_vector_const_proxy<EXPRSXP> {
-		typedef ::Rcpp::internal::generic_proxy<EXPRSXP> type ;
-	} ;
-	template<> struct r_vector_const_proxy<VECSXP> {
-		typedef ::Rcpp::internal::generic_proxy<VECSXP> type ;
-	} ;
+	} ;                                            
+	// template<> struct r_vector_const_proxy<STRSXP> {
+	// 	typedef ::Rcpp::internal::string_proxy<STRSXP> type ;
+	// } ;
+	// template<> struct r_vector_const_proxy<EXPRSXP> {
+	// 	typedef ::Rcpp::internal::generic_proxy<EXPRSXP> type ;
+	// } ;
+	// template<> struct r_vector_const_proxy<VECSXP> {
+	// 	typedef ::Rcpp::internal::generic_proxy<VECSXP> type ;
+	// } ;
 
 	template <int RTYPE>
 	struct r_vector_iterator {
@@ -255,14 +255,14 @@
 	template<> struct r_vector_iterator<EXPRSXP> : proxy_based_iterator<EXPRSXP>{} ;
 	template<> struct r_vector_iterator<STRSXP> : proxy_based_iterator<STRSXP>{} ;
 
-	template <int RTYPE> struct proxy_based_const_iterator{
-		typedef ::Rcpp::internal::SEXP_Iterator<RTYPE, Vector<RTYPE> > type ;
-	} ;
-	template<> struct r_vector_const_iterator<VECSXP> : proxy_based_const_iterator<VECSXP>{} ;
-	template<> struct r_vector_const_iterator<EXPRSXP> : proxy_based_const_iterator<EXPRSXP>{} ;
-	template<> struct r_vector_const_iterator<STRSXP> {
-	    typedef CharacterVectorExtractionIterator type ;
-	} ;
+	// template <int RTYPE> struct proxy_based_const_iterator{
+	// 	typedef ::Rcpp::internal::SEXP_Iterator<RTYPE, Vector<RTYPE> > type ;
+	// } ;
+	// template<> struct r_vector_const_iterator<VECSXP> : proxy_based_const_iterator<VECSXP>{} ;
+	// template<> struct r_vector_const_iterator<EXPRSXP> : proxy_based_const_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-22 11:16:57 UTC (rev 4016)
+++ pkg/Rcpp/inst/include/Rcpp/vector/traits.h	2012-11-22 13:14:48 UTC (rev 4017)
@@ -66,13 +66,16 @@
 			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 const_iterator get_const() const { return const_iterator( *p ) ;}
+		// inline const_iterator get_const() const { return const_iterator( *p ) ;}
+		inline const_iterator get_const() const { return get_vector_ptr(*p) ; }
 		
 		inline proxy ref() { return proxy(*p,0) ; }
 		inline proxy ref(int i) { return proxy(*p,i);}
 		
-		inline const_proxy ref() const { return const_proxy(*p,0) ; }
-		inline const_proxy ref(int i) const { return const_proxy(*p,i);}
+		// inline const_proxy ref() const { return const_proxy(*p,0) ; }
+		// inline const_proxy ref(int i) const { return const_proxy(*p,i);}
+		inline const_proxy ref() const { return *get_vector_ptr(*p) ; }
+		inline const_proxy ref(int i) const { return get_vector_ptr(*p)[i] ;}
 		
 		private:
 			VECTOR* p ;



More information about the Rcpp-commits mailing list