[Rcpp-commits] r649 - in pkg: inst/unitTests src src/Rcpp

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Feb 9 13:55:53 CET 2010


Author: romain
Date: 2010-02-09 13:55:53 +0100 (Tue, 09 Feb 2010)
New Revision: 649

Modified:
   pkg/inst/unitTests/runit.CharacterVector.R
   pkg/src/CharacterVector.cpp
   pkg/src/Rcpp/CharacterVector.h
Log:
support for swap( StringProxy )

Modified: pkg/inst/unitTests/runit.CharacterVector.R
===================================================================
--- pkg/inst/unitTests/runit.CharacterVector.R	2010-02-09 12:40:44 UTC (rev 648)
+++ pkg/inst/unitTests/runit.CharacterVector.R	2010-02-09 12:55:53 UTC (rev 649)
@@ -198,3 +198,18 @@
 		msg = "CharacterVector::iterator using std::accumulate" )
 	
 }
+
+test.CharacterVector.reverse <- function(){
+	funx <- cfunction(signature(x = "character"), '
+		CharacterVector y(x) ;
+		std::reverse( y.begin(), y.end() ) ;
+		return y ;
+	;
+		', Rcpp = TRUE, includes = "using namespace Rcpp;" )
+	x <- c("foo", "bar", "bling")
+	funx(x)
+	checkEquals( x, c("bling", "bar", "foo"), msg = "reverse" )
+	funx(x)
+	checkEquals( x, c("foo", "bar", "bling"), msg = "reverse" )
+}
+

Modified: pkg/src/CharacterVector.cpp
===================================================================
--- pkg/src/CharacterVector.cpp	2010-02-09 12:40:44 UTC (rev 648)
+++ pkg/src/CharacterVector.cpp	2010-02-09 12:55:53 UTC (rev 649)
@@ -180,6 +180,31 @@
 	return ( this->proxy.index != y.proxy.index ) || ( this->proxy.parent != y.proxy.parent );
 }
 
+bool CharacterVector::iterator::operator<( const iterator& other){
+	/* TODO: deal with the case where this os not iterating over the 
+	         same character vector */
+	return proxy.index < other.proxy.index ;
+}
+
+bool CharacterVector::iterator::operator>( const iterator& other){
+	/* TODO: deal with the case where this os not iterating over the 
+	         same character vector */
+	return proxy.index > other.proxy.index ;
+}
+
+bool CharacterVector::iterator::operator<=( const iterator& other){
+	/* TODO: deal with the case where this os not iterating over the 
+	         same character vector */
+	return proxy.index <= other.proxy.index ;
+}
+
+bool CharacterVector::iterator::operator>=( const iterator& other){
+	/* TODO: deal with the case where this os not iterating over the 
+	         same character vector */
+	return proxy.index >= other.proxy.index ;
+}
+
+
 CharacterVector::iterator::difference_type CharacterVector::iterator::operator-(
 		const CharacterVector::iterator& y ){
 	return y.proxy.index - this->proxy.index ;
@@ -189,4 +214,19 @@
 	return x + static_cast<const char*>(y) ;
 }
 
+void CharacterVector::StringProxy::swap( StringProxy& other){
+	SEXP tmp = PROTECT( STRING_ELT(parent, index)) ;
+	SET_STRING_ELT( parent, index, STRING_ELT(other.parent, other.index) ) ;
+	SET_STRING_ELT( other.parent, other.index, tmp ) ;
+	UNPROTECT(1) ;
+}
+
 } // namespace 
+
+namespace std{
+	template<> void swap<Rcpp::CharacterVector::StringProxy>( Rcpp::CharacterVector::StringProxy& a, Rcpp::CharacterVector::StringProxy& b){
+	a.swap(b) ;
+}
+} ;
+
+

Modified: pkg/src/Rcpp/CharacterVector.h
===================================================================
--- pkg/src/Rcpp/CharacterVector.h	2010-02-09 12:40:44 UTC (rev 648)
+++ pkg/src/Rcpp/CharacterVector.h	2010-02-09 12:55:53 UTC (rev 649)
@@ -100,6 +100,9 @@
 		friend std::ostream& operator<<(std::ostream& os, const StringProxy& proxy);
 		
 		friend class iterator ;
+		
+		void swap( StringProxy& other ) ;
+		
 	private:
 		CharacterVector& parent; 
 		int index ;
@@ -133,6 +136,11 @@
 		
 		bool operator==( const iterator& y) ;
 		bool operator!=( const iterator& y) ;
+		bool operator<( const iterator& other ) ;
+		bool operator>( const iterator& other ) ;
+		bool operator<=( const iterator& other ) ;
+		bool operator>=( const iterator& other ) ;
+		
 		difference_type operator-(const iterator& y) ;
 		
 	private:
@@ -268,7 +276,12 @@
 
 std::string operator+( const std::string& x, const CharacterVector::StringProxy& y ) ;
 
-
 } // namespace
 
+namespace std{
+	template<> void swap<Rcpp::CharacterVector::StringProxy>(Rcpp::CharacterVector::StringProxy& a, Rcpp::CharacterVector::StringProxy& b) ;
+}
+
+
+
 #endif



More information about the Rcpp-commits mailing list