[Rcpp-commits] r1571 - in pkg/Rcpp/inst: include/Rcpp/sugar include/Rcpp/vector unitTests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jun 17 14:59:00 CEST 2010


Author: romain
Date: 2010-06-17 14:58:59 +0200 (Thu, 17 Jun 2010)
New Revision: 1571

Added:
   pkg/Rcpp/inst/unitTests/runit.sugar.assign.R
Modified:
   pkg/Rcpp/inst/include/Rcpp/sugar/LogicalResult.h
   pkg/Rcpp/inst/include/Rcpp/vector/Vector.h
   pkg/Rcpp/inst/unitTests/runit.RcppDatetime.R
Log:
Vector gains a constructor that takes a sugar expression of the same type

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/LogicalResult.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/LogicalResult.h	2010-06-17 12:29:19 UTC (rev 1570)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/LogicalResult.h	2010-06-17 12:58:59 UTC (rev 1571)
@@ -31,97 +31,14 @@
 	
 	LogicalResult() {} ;
 		
-	inline int operator[]( int i){
-		return static_cast<T*>(this)->operator[]( i ) ;
+	inline int operator[]( int i) const {
+		return static_cast<const T*>(this)->operator[]( i ) ;
 	}
 	
-	inline int size(){
-		return static_cast<T&>(*this).size() ;
+	inline int size() const {
+		return static_cast<const T&>(*this).size() ;
 	}
 	
-// public:
-// 	
-// 	class iterator {
-// 	public:
-// 		typedef int difference_type ;
-// 		typedef int value_type ;
-// 		typedef const int* pointer ;
-// 		typedef int reference ;
-// 		typedef std::random_access_iterator_tag iterator_category ;
-// 		
-// 		iterator( const LogicalResult& comp_, int index_ = 0 ) : 
-// 			comp(comp_), index(index_) {}
-// 		                                                     
-// 		inline difference_type operator-( const iterator& other ){
-// 			return index - other.index ;
-// 		}
-// 		inline bool operator==( const iterator& other ){
-// 			return index == other.index ;
-// 		}
-// 		inline bool operator<( const iterator& other ){
-// 			return index < other.index ;
-// 		}
-// 		inline bool operator>( const iterator& other ){
-// 			return index > other.index ;
-// 		}
-// 		inline bool operator<=( const iterator& other ){
-// 			return index <= other.index ;
-// 		}
-// 		inline bool operator>=( const iterator& other ){
-// 			return index >= other.index ;
-// 		}
-// 		inline bool operator!=( const iterator& other ){
-// 			return index != other.index ;
-// 		}
-// 		
-// 		inline reference operator*(){
-// 			return comp[index] ;
-// 		}
-// 		inline pointer operator->(){
-// 			return &comp[index] ;
-// 		}
-// 
-// 		inline iterator& operator++(){
-// 			index++; 
-// 			return *this ;
-// 		}
-// 		inline iterator& operator++(int){
-// 			index++ ;
-// 			return *this ;
-// 		}
-// 		inline iterator& operator--(){
-// 			index-- ;
-// 			return *this ;
-// 		}
-// 		inline iterator& operator--(int){
-// 			index-- ;
-// 			return *this ;
-// 		}
-// 		inline iterator& operator+=(difference_type n) {
-// 			index += n ;
-// 			return *this ;
-// 		}
-// 		inline iterator& operator-=(difference_type n) {
-// 			index -= n ;
-// 			return *this ;
-// 		}
-// 		
-// 		inline iterator operator+(difference_type n) const {
-// 			return iterator( comp, index + n ) ;
-// 		}
-// 		inline iterator operator-(difference_type n) const {
-// 			return iterator( comp, index - n ) ;
-// 		}
-// 
-// 	private:
-// 		const LogicalResult& comp ;
-// 		int index ;
-// 	} ;
-// 	
-// 	inline iterator begin() const { return iterator(*this, 0 ) ; }
-// 	inline iterator end() const { return iterator(*this, size() ) ; }
-
-	
 } ;
 
 } // namespace sugar

Modified: pkg/Rcpp/inst/include/Rcpp/vector/Vector.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/Vector.h	2010-06-17 12:29:19 UTC (rev 1570)
+++ pkg/Rcpp/inst/include/Rcpp/vector/Vector.h	2010-06-17 12:58:59 UTC (rev 1571)
@@ -87,6 +87,16 @@
 		init() ;
     }
     
+    template <bool __NA__, typename __VEC__>
+    Vector( const VectorBase<RTYPE,__NA__,__VEC__>& other ){
+    	int n = other.size() ;
+    	RObject::setSEXP( Rf_allocVector( RTYPE, other.size() ) ) ;
+		iterator start = begin() ; 
+		for( int i=0; i<n; i++, ++start){
+			*start = other[i] ;
+		}
+    }
+    
     template <typename U>
     Vector( const int& size, const U& u){
     	RObject::setSEXP( Rf_allocVector( RTYPE, size) ) ;

Modified: pkg/Rcpp/inst/unitTests/runit.RcppDatetime.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.RcppDatetime.R	2010-06-17 12:29:19 UTC (rev 1570)
+++ pkg/Rcpp/inst/unitTests/runit.RcppDatetime.R	2010-06-17 12:58:59 UTC (rev 1571)
@@ -55,7 +55,7 @@
 test.RcppDatetime.wrap <- function() {
     src <- 'RcppDatetime dt = RcppDatetime(981183723.123456);
 	    return wrap(dt);';
-    funx <- cppfunction(signature(), src)
+    funx <- cxxfunction(signature(), src, plugin = "Rcpp" )
     checkEquals(as.numeric(funx()), as.numeric(as.POSIXct("2001-02-03 01:02:03.123456")),
                 msg = "RcppDatetime.wrap")
 }

Added: pkg/Rcpp/inst/unitTests/runit.sugar.assign.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.assign.R	                        (rev 0)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.assign.R	2010-06-17 12:58:59 UTC (rev 1571)
@@ -0,0 +1,39 @@
+#!/usr/bin/r -t
+#
+# Copyright (C) 2010	Dirk Eddelbuettel and Romain Francois
+#
+# This file is part of Rcpp.
+#
+# Rcpp is free software: you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+#
+# Rcpp is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
+
+test.sugar.assign <- function( ){
+
+	fx <- cxxfunction( signature( x = "numeric", y = "numeric" ), '
+	
+		NumericVector xx(x) ;
+		NumericVector yy(y) ;
+		
+		LogicalVector res = xx < yy ;
+		return res ;
+		
+	', plugin = "Rcpp" )
+
+
+	checkEquals( fx( 1, 0 ), FALSE )
+	checkEquals( fx( 1:10, 2:11 ), rep(TRUE,10) )
+	checkEquals( fx( 0, 1 ), TRUE )
+	checkTrue( identical( fx( NA, 1 ), NA ) )
+	
+}
+



More information about the Rcpp-commits mailing list