[Rcpp-commits] r4011 - in pkg/Rcpp: . inst inst/include/Rcpp/macros inst/include/Rcpp/sugar/functions inst/include/Rcpp/vector

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Nov 21 19:49:27 CET 2012


Author: romain
Date: 2012-11-21 19:49:27 +0100 (Wed, 21 Nov 2012)
New Revision: 4011

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/NEWS.Rd
   pkg/Rcpp/inst/include/Rcpp/macros/unroll.h
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmax.h
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmin.h
   pkg/Rcpp/inst/include/Rcpp/vector/Vector.h
Log:
added clip

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2012-11-21 14:59:36 UTC (rev 4010)
+++ pkg/Rcpp/ChangeLog	2012-11-21 18:49:27 UTC (rev 4011)
@@ -20,6 +20,7 @@
         * include/Rcpp/sugar/functions/mapply/mapply_3.h: more flexible
         * include/Rcpp/vector/Vector.h: More efficient assign_object, which is 
         used in the assignment operator
+        * include/Rcpp/sugar/functions/clip.h: added clip function
 
 2012-11-20  JJ Allaire <jj at rstudio.org>
 

Modified: pkg/Rcpp/inst/NEWS.Rd
===================================================================
--- pkg/Rcpp/inst/NEWS.Rd	2012-11-21 14:59:36 UTC (rev 4010)
+++ pkg/Rcpp/inst/NEWS.Rd	2012-11-21 18:49:27 UTC (rev 4011)
@@ -9,6 +9,8 @@
           \item New functions: \code{setdiff}, \code{union_}, \code{intersect}
             \code{setequal}, \code{in}, \code{min}, \code{max}, \code{range}, 
             \code{match}
+          \item New function: \code{clip} which combines pmin and pmax, e.g. 
+          clip( a, x, b) is the same as pmax( b, pmin(x, a) )
         }
         \item Changes in Rcpp API:
         \itemize{

Modified: pkg/Rcpp/inst/include/Rcpp/macros/unroll.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/macros/unroll.h	2012-11-21 14:59:36 UTC (rev 4010)
+++ pkg/Rcpp/inst/include/Rcpp/macros/unroll.h	2012-11-21 18:49:27 UTC (rev 4011)
@@ -21,7 +21,29 @@
 
 #ifndef Rcpp__macros_unroll_h
 #define Rcpp__macros_unroll_h
-   
+
+#define RCPP_LOOP_UNROLL_PTR(TARGET,SOURCE)    \
+int __trip_count = n >> 2 ;                    \
+int i = 0 ;                                    \
+for ( ; __trip_count > 0 ; --__trip_count) {   \
+	*TARGET++ = SOURCE[i++] ;                  \
+	*TARGET++ = SOURCE[i++] ;                  \
+	*TARGET++ = SOURCE[i++] ;                  \
+	*TARGET++ = SOURCE[i++] ;                  \
+}                                              \
+switch (n - i){                                \
+  case 3:                                      \
+    *TARGET++ = SOURCE[i++] ;                  \
+  case 2:                                      \
+    *TARGET++ = SOURCE[i++] ;                  \
+  case 1:                                      \
+    *TARGET++ = SOURCE[i++] ;                  \
+  case 0:                                      \
+  default:                                     \
+      {}                                       \
+}
+
+
 #define RCPP_LOOP_UNROLL(TARGET,SOURCE)           \
 int __trip_count = n >> 2 ;                       \
 int i = 0 ;                                       \

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2012-11-21 14:59:36 UTC (rev 4010)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2012-11-21 18:49:27 UTC (rev 4011)
@@ -41,6 +41,7 @@
 #include <Rcpp/sugar/functions/ifelse.h>
 #include <Rcpp/sugar/functions/pmin.h>
 #include <Rcpp/sugar/functions/pmax.h>
+#include <Rcpp/sugar/functions/clip.h>
 #include <Rcpp/sugar/functions/minmax.h>
 #include <Rcpp/sugar/functions/sign.h>
 #include <Rcpp/sugar/functions/diff.h>

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmax.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmax.h	2012-11-21 14:59:36 UTC (rev 4010)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmax.h	2012-11-21 18:49:27 UTC (rev 4011)
@@ -2,7 +2,7 @@
 //
 // pmax.h: Rcpp R/C++ interface class library -- pmax
 //
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -25,75 +25,70 @@
 namespace Rcpp{
 namespace sugar{
 
-template <int RTYPE, bool LHS_NA, bool RHS_NA> class pmax_op {
-public:
-	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
-	
-	inline STORAGE operator()( STORAGE left, STORAGE right ) const {
-		if( Rcpp::traits::is_na<RTYPE>(left) ) return left ;
-		if( Rcpp::traits::is_na<RTYPE>(right) ) return right ;
-		return left > right ? left : right ;
+template <int RTYPE, bool LHS_NA, bool RHS_NA> class pmax_op ; 
+
+// specializations for double. 
+// we use the fact that NA < x is false
+template <>
+struct pmax_op<REALSXP,true,true>{
+	inline double operator()( double left, double right ) const {
+		return ( Rcpp::traits::is_na<REALSXP>( left ) || (left > right) ) ? left : right ;
 	}
-	
 } ;
-template <int RTYPE, bool LHS_NA> class pmax_op<RTYPE,LHS_NA,false> {
-public:
-	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
-	
-	inline STORAGE operator()( STORAGE left, STORAGE right ) const {
-		if( Rcpp::traits::is_na<RTYPE>(left) ) return left ;
-		return left > right ? left : right ;
+template <> struct pmax_op<REALSXP,true,false> {
+	inline double operator()( double left, double right ) const {
+		return right > left ? right : left ;
 	}
 } ;
-template <int RTYPE, bool RHS_NA> class pmax_op<RTYPE,false,RHS_NA> {
-public:
-	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
-	
-	inline STORAGE operator()( STORAGE left, STORAGE right ) const {
-		if( Rcpp::traits::is_na<RTYPE>(right) ) return right ;
-		return left > right ? left : right ;
+template <> struct pmax_op<REALSXP,false,true> {
+	inline double operator()( double left, double right ) const {
+		return right > left ? right : left ;
 	}
 } ;
-template <int RTYPE> class pmax_op<RTYPE,false,false> {
-public:
-	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
-	
-	inline STORAGE operator()( STORAGE left, STORAGE right ) const {
+template <> struct pmax_op<REALSXP,false,false> {
+	inline double operator()( double left, double right ) const {
 		return left > right ? left : right ;
 	}
 } ;
 
-template <int RTYPE,bool NA> class pmax_op_Vector_Primitive {
+// specializations for INTSXP. Since NA is represented as the smallest 
+// int, NA is always the smallest, so it is safe to return NA
+template <bool LHS_NA, bool RHS_NA>
+struct pmax_op<INTSXP,LHS_NA,RHS_NA> {
+    inline int operator()(int left, int right) const {
+        return left > right ? left : right ;
+    }
+} ;
+
+
+// general case
+template <int RTYPE, bool NA> class pmax_op_Vector_Primitive {
 public:
 	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
 
-	pmax_op_Vector_Primitive( STORAGE right_ ) : 
-		right(right_) {}
+	pmax_op_Vector_Primitive( STORAGE right_ ) :  right(right_) {}
 	
 	inline STORAGE operator()( STORAGE left ) const {
-		if( Rcpp::traits::is_na<RTYPE>(left) ) return left ;
 		return left > right ? left : right ;
 	}	
 		
 private:
 	STORAGE right ;
 } ;
-
-template <int RTYPE> class pmax_op_Vector_Primitive<RTYPE,false> {
+// only special case we need to take care of
+template <> class pmax_op_Vector_Primitive<REALSXP,true> {
 public:
-	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
-
-	pmax_op_Vector_Primitive( STORAGE right_ ) : 
-		right(right_){}
+	pmax_op_Vector_Primitive( double right_ ) :  right(right_) {}
 	
-	inline STORAGE operator()( STORAGE left ) const {
-		return left > right ? left : right ;
+	inline double operator()( double left ) const {
+		return ( Rcpp::traits::is_na<REALSXP>( left ) || (left > right) ) ? left : right ;
 	}	
 		
 private:
-	STORAGE right ;
+	double right ;
 } ;
 
+    
 
 
 template <
@@ -107,13 +102,10 @@
 	Pmax_Vector_Vector<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T>
 > {
 public:
-	typedef typename Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T> LHS_TYPE ;
-	typedef typename Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T> RHS_TYPE ;
 	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
 	typedef pmax_op<RTYPE,LHS_NA,RHS_NA> OPERATOR ;
 	
-	Pmax_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 
-		lhs(lhs_), rhs(rhs_), op() {}
+	Pmax_Vector_Vector( const LHS_T& lhs_, const RHS_T& rhs_ ) : lhs(lhs_), rhs(rhs_), op() {}
 	
 	inline STORAGE operator[]( int i ) const {
 		return op( lhs[i], rhs[i] ) ;
@@ -121,8 +113,8 @@
 	inline int size() const { return lhs.size() ; }
 	         
 private:
-	const LHS_TYPE& lhs ;
-	const RHS_TYPE& rhs ;
+	const LHS_T& lhs ;
+	const RHS_T& rhs ;
 	OPERATOR op ;
 } ;
 
@@ -138,23 +130,19 @@
 	Pmax_Vector_Primitive<RTYPE,LHS_NA,LHS_T>
 > {
 public:
-	typedef typename Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T> LHS_TYPE ;
 	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
 	typedef pmax_op_Vector_Primitive<RTYPE,LHS_NA> OPERATOR ;
 	
-	Pmax_Vector_Primitive( const LHS_TYPE& lhs_, STORAGE rhs_ ) : 
-		lhs(lhs_), op(rhs_), rhs(rhs_), isna( Rcpp::traits::is_na<RTYPE>(rhs_) ) {}
+	Pmax_Vector_Primitive( const LHS_T& lhs_, STORAGE rhs_ ) : lhs(lhs_), op(rhs_) {}
 	
 	inline STORAGE operator[]( int i ) const {
-		return isna ? rhs : op( lhs[i] ) ;
+		return op( lhs[i] ) ;
 	}
 	inline int size() const { return lhs.size() ; }
 	         
 private:
-	const LHS_TYPE& lhs ;
+	const LHS_T& lhs ;
 	OPERATOR op ;
-	STORAGE rhs ;
-	bool isna ;
 } ;
 
 
@@ -171,7 +159,7 @@
 	const Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T>& lhs, 
 	const Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T>& rhs 
 	){
-	return sugar::Pmax_Vector_Vector<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T>( lhs, rhs ) ;
+	return sugar::Pmax_Vector_Vector<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T>( lhs.get_ref(), rhs.get_ref() ) ;
 }
 
 template <
@@ -183,7 +171,7 @@
 	const Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T>& lhs, 
 	typename Rcpp::traits::storage_type<RTYPE>::type rhs 
 	){
-	return sugar::Pmax_Vector_Primitive<RTYPE,LHS_NA,LHS_T>( lhs, rhs ) ;
+	return sugar::Pmax_Vector_Primitive<RTYPE,LHS_NA,LHS_T>( lhs.get_ref(), rhs ) ;
 }
 
 
@@ -196,7 +184,7 @@
 	typename Rcpp::traits::storage_type<RTYPE>::type lhs,  
 	const Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T>& rhs 
 	){
-	return sugar::Pmax_Vector_Primitive<RTYPE,RHS_NA,RHS_T>( rhs, lhs ) ;
+	return sugar::Pmax_Vector_Primitive<RTYPE,RHS_NA,RHS_T>( rhs.get_ref(), lhs ) ;
 }
 
 

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmin.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmin.h	2012-11-21 14:59:36 UTC (rev 4010)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmin.h	2012-11-21 18:49:27 UTC (rev 4011)
@@ -2,7 +2,7 @@
 //
 // pmin.h: Rcpp R/C++ interface class library -- pmin
 //
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -25,77 +25,72 @@
 namespace Rcpp{
 namespace sugar{
 
-template <int RTYPE, bool LHS_NA, bool RHS_NA> class pmin_op {
-public:
-	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
-	
-	inline STORAGE operator()( STORAGE left, STORAGE right ) const {
-		if( Rcpp::traits::is_na<RTYPE>(left) ) return left ;
-		if( Rcpp::traits::is_na<RTYPE>(right) ) return right ;
-		return left < right ? left : right ;
+template <int RTYPE, bool LHS_NA, bool RHS_NA> class pmin_op ; 
+
+// specializations for double. 
+// we use the fact that NA < x is false
+template <>
+struct pmin_op<REALSXP,true,true>{
+	inline double operator()( double left, double right ) const {
+		return ( Rcpp::traits::is_na<REALSXP>( left ) || (left < right) ) ? left : right ;
 	}
-	
 } ;
-template <int RTYPE, bool LHS_NA> class pmin_op<RTYPE,LHS_NA,false> {
-public:
-	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
-	
-	inline STORAGE operator()( STORAGE left, STORAGE right ) const {
-		if( Rcpp::traits::is_na<RTYPE>(left) ) return left ;
-		return left < right ? left : right ;
+template <> struct pmin_op<REALSXP,true,false> {
+	inline double operator()( double left, double right ) const {
+		return right < left ? right : left ;
 	}
 } ;
-template <int RTYPE, bool RHS_NA> class pmin_op<RTYPE,false,RHS_NA> {
-public:
-	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
-	
-	inline STORAGE operator()( STORAGE left, STORAGE right ) const {
-		if( Rcpp::traits::is_na<RTYPE>(right) ) return right ;
-		return left < right ? left : right ;
+template <> struct pmin_op<REALSXP,false,true> {
+	inline double operator()( double left, double right ) const {
+		return right < left ? right : left ;
 	}
 } ;
-template <int RTYPE> class pmin_op<RTYPE,false,false> {
-public:
-	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
-	
-	inline STORAGE operator()( STORAGE left, STORAGE right ) const {
+template <> struct pmin_op<REALSXP,false,false> {
+	inline double operator()( double left, double right ) const {
 		return left < right ? left : right ;
 	}
 } ;
 
-template <int RTYPE,bool NA> class pmin_op_Vector_Primitive {
+// specializations for INTSXP. Since NA is represented as the smallest 
+// int, NA is always the smallest, so it is safe to return NA
+template <bool LHS_NA, bool RHS_NA>
+struct pmin_op<INTSXP,LHS_NA,RHS_NA> {
+    inline int operator()(int left, int right) const {
+        return left < right ? left : right ;
+    }
+} ;
+
+
+// general case
+template <int RTYPE, bool NA> class pmin_op_Vector_Primitive {
 public:
 	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
 
-	pmin_op_Vector_Primitive( STORAGE right_ ) : 
-		right(right_) {}
+	pmin_op_Vector_Primitive( STORAGE right_ ) :  right(right_) {}
 	
 	inline STORAGE operator()( STORAGE left ) const {
-		if( Rcpp::traits::is_na<RTYPE>(left) ) return left ;
 		return left < right ? left : right ;
 	}	
 		
 private:
 	STORAGE right ;
 } ;
-
-template <int RTYPE> class pmin_op_Vector_Primitive<RTYPE,false> {
+// only special case we need to take care of
+template <> class pmin_op_Vector_Primitive<REALSXP,true> {
 public:
-	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
-
-	pmin_op_Vector_Primitive( STORAGE right_ ) : 
-		right(right_){}
+	pmin_op_Vector_Primitive( double right_ ) :  right(right_) {}
 	
-	inline STORAGE operator()( STORAGE left ) const {
-		return left < right ? left : right ;
+	inline double operator()( double left ) const {
+		return ( Rcpp::traits::is_na<REALSXP>( left ) || (left < right) ) ? left : right ;
 	}	
 		
 private:
-	STORAGE right ;
+	double right ;
 } ;
 
 
 
+
 template <
 	int RTYPE, 
 	bool LHS_NA, typename LHS_T, 
@@ -107,13 +102,10 @@
 	Pmin_Vector_Vector<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T>
 > {
 public:
-	typedef typename Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T> LHS_TYPE ;
-	typedef typename Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T> RHS_TYPE ;
 	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
 	typedef pmin_op<RTYPE,LHS_NA,RHS_NA> OPERATOR ;
 	
-	Pmin_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 
-		lhs(lhs_), rhs(rhs_), op() {}
+	Pmin_Vector_Vector( const LHS_T& lhs_, const RHS_T& rhs_ ) : lhs(lhs_), rhs(rhs_), op() {}
 	
 	inline STORAGE operator[]( int i ) const {
 		return op( lhs[i], rhs[i] ) ;
@@ -121,8 +113,8 @@
 	inline int size() const { return lhs.size() ; }
 	         
 private:
-	const LHS_TYPE& lhs ;
-	const RHS_TYPE& rhs ;
+	const LHS_T& lhs ;
+	const RHS_T& rhs ;
 	OPERATOR op ;
 } ;
 
@@ -138,23 +130,17 @@
 	Pmin_Vector_Primitive<RTYPE,LHS_NA,LHS_T>
 > {
 public:
-	typedef typename Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T> LHS_TYPE ;
 	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
 	typedef pmin_op_Vector_Primitive<RTYPE,LHS_NA> OPERATOR ;
 	
-	Pmin_Vector_Primitive( const LHS_TYPE& lhs_, STORAGE rhs_ ) : 
-		lhs(lhs_), op(rhs_), rhs(rhs_), isna( Rcpp::traits::is_na<RTYPE>(rhs_) ) {}
+	Pmin_Vector_Primitive( const LHS_T& lhs_, STORAGE rhs_ ) : lhs(lhs_), op(rhs_) {}
 	
-	inline STORAGE operator[]( int i ) const {
-		return isna ? rhs : op( lhs[i] ) ;
-	}
+	inline STORAGE operator[]( int i ) const { return op( lhs[i] ) ; }
 	inline int size() const { return lhs.size() ; }
 	         
 private:
-	const LHS_TYPE& lhs ;
+	const LHS_T& lhs ;
 	OPERATOR op ;
-	STORAGE rhs ;
-	bool isna ;
 } ;
 
 
@@ -171,7 +157,7 @@
 	const Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T>& lhs, 
 	const Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T>& rhs 
 	){
-	return sugar::Pmin_Vector_Vector<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T>( lhs, rhs ) ;
+	return sugar::Pmin_Vector_Vector<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T>( lhs.get_ref(), rhs.get_ref() ) ;
 }
 
 template <
@@ -183,7 +169,7 @@
 	const Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T>& lhs, 
 	typename Rcpp::traits::storage_type<RTYPE>::type rhs 
 	){
-	return sugar::Pmin_Vector_Primitive<RTYPE,LHS_NA,LHS_T>( lhs, rhs ) ;
+	return sugar::Pmin_Vector_Primitive<RTYPE,LHS_NA,LHS_T>( lhs.get_ref(), rhs ) ;
 }
 
 
@@ -196,7 +182,7 @@
 	typename Rcpp::traits::storage_type<RTYPE>::type lhs,  
 	const Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T>& rhs 
 	){
-	return sugar::Pmin_Vector_Primitive<RTYPE,RHS_NA,RHS_T>( rhs, lhs ) ;
+	return sugar::Pmin_Vector_Primitive<RTYPE,RHS_NA,RHS_T>( rhs.get_ref(), lhs ) ;
 }
 
 

Modified: pkg/Rcpp/inst/include/Rcpp/vector/Vector.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/Vector.h	2012-11-21 14:59:36 UTC (rev 4010)
+++ pkg/Rcpp/inst/include/Rcpp/vector/Vector.h	2012-11-21 18:49:27 UTC (rev 4011)
@@ -93,27 +93,7 @@
         int n = size() ;
         if( n == x.size() ){
             // just copy the data 
-            iterator start = begin() ;
-            
-            int __trip_count = n >> 2 ;
-            int i = 0 ;
-            for ( ; __trip_count > 0 ; --__trip_count) { 
-            	*start++ = x[i++] ;            
-            	*start++ = x[i++] ;            
-            	*start++ = x[i++] ;            
-            	*start++ = x[i++] ;            
-            }                                            
-            switch (n - i){                          
-            case 3:                                    
-                *start++ = x[i++] ;             
-            case 2:                                    
-                *start++ = x[i++] ;             
-            case 1:                                    
-                *start++ = x[i++] ;             
-            case 0:                                    
-            default:                                   
-                {}                         
-            }
+            import_expression<T>(x, n ) ;
         } else{
             // different size, so we change the memory
             set_sexp( r_cast<RTYPE>( wrap(x) ) ); 
@@ -156,7 +136,7 @@
     Vector( const VectorBase<RTYPE,NA,VEC>& other ) : RObject() {
     	int n = other.size() ;
     	RObject::setSEXP( Rf_allocVector( RTYPE, n ) ) ;
-    	import_expression<NA,VEC>( other, n ) ;
+    	import_expression<VEC>( other.get_ref() , n ) ;
     }
     
     
@@ -164,38 +144,12 @@
 private:
 	  
     // TODO: do some dispatch when VEC == Vector so that we use std::copy
-    template <bool NA, typename VEC>
-    inline void import_expression( const VectorBase<RTYPE,NA,VEC>& other, int n ){
+    template <typename T>
+    inline void import_expression( const T& other, int n ){
         iterator start = begin() ; 
-        const VEC& ref = other.get_ref() ;
-        
-        int __trip_count = n >> 2 ;
-        int i = 0 ;
-    	for ( ; __trip_count > 0 ; --__trip_count) { 
-            start[i] = ref[i] ; i++ ;            
-            start[i] = ref[i] ; i++ ;            
-            start[i] = ref[i] ; i++ ;            
-            start[i] = ref[i] ; i++ ;            
-    	}                                            
-    	switch (n - i){                          
-        case 3:                                    
-            start[i] = ref[i] ; i++ ;             
-        case 2:                                    
-            start[i] = ref[i] ; i++ ;             
-        case 1:                                    
-            start[i] = ref[i] ; i++ ;             
-        case 0:                                    
-        default:                                   
-            {}                         
-    	}
+        RCPP_LOOP_UNROLL_PTR(start,other)
     }
     
-    // template <>
-    // inline void import_expression<true,Vector>( const VectorBase<RTYPE,NA,VEC>& other, int n ){
-    //     const VEC& ref = other.get_ref() ;
-    //     std::copy( ref.begin(), ref.end(), begin() ) ;
-    // }
-    
     template <typename T>
     inline void fill_or_generate( const T& t){
     	fill_or_generate__impl( t, typename traits::is_generator<T>::type() ) ;



More information about the Rcpp-commits mailing list