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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jan 11 22:24:20 CET 2010


Author: romain
Date: 2010-01-11 22:24:09 +0100 (Mon, 11 Jan 2010)
New Revision: 347

Modified:
   pkg/inst/ChangeLog
   pkg/inst/unitTests/runit.CharacterVector.R
   pkg/src/CharacterVector.cpp
   pkg/src/Rcpp/CharacterVector.h
Log:
CharacterVector::StringProxy::operator+=

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2010-01-11 21:03:55 UTC (rev 346)
+++ pkg/inst/ChangeLog	2010-01-11 21:24:09 UTC (rev 347)
@@ -1,5 +1,8 @@
 2010-01-11  Romain Francois <francoisromain at free.fr>
 
+	* src/Rcpp/CharacterVector.h: StringProxy gains operator+=
+	* src/CharacterVector.cpp: implementation
+
 	* src/Rcpp/*Vector.h: operator[] now throws index out of bounds
 	exception when needed (FR#770)
 	* src/*Vector.cpp: same

Modified: pkg/inst/unitTests/runit.CharacterVector.R
===================================================================
--- pkg/inst/unitTests/runit.CharacterVector.R	2010-01-11 21:03:55 UTC (rev 346)
+++ pkg/inst/unitTests/runit.CharacterVector.R	2010-01-11 21:24:09 UTC (rev 347)
@@ -53,4 +53,17 @@
 	}
 }
 
+test.CharacterVector.plusequals <- function(){
+	funx <- cfunction(signature(), '
+	CharacterVector x(2) ;
+	x[0] = "foo" ; 
+	x[1] = "bar" ;
+	x[0] += "bar" ;
+	x[1] += "foo" ;
+	return x ;
+	', Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;" )
+	checkEquals( funx(), c("foobar", "barfoo"), 
+		msg = "StringProxy::operator+=" )
+	
+}
 

Modified: pkg/src/CharacterVector.cpp
===================================================================
--- pkg/src/CharacterVector.cpp	2010-01-11 21:03:55 UTC (rev 346)
+++ pkg/src/CharacterVector.cpp	2010-01-11 21:24:09 UTC (rev 347)
@@ -108,11 +108,26 @@
 	return *this ;
 }
 
+CharacterVector::StringProxy& CharacterVector::StringProxy::operator+=( const std::string& rhs){
+	std::string full( CHAR(STRING_ELT(parent,index)) ) ;
+	full += rhs ;
+	SET_STRING_ELT( parent, index, Rf_mkChar( full.c_str() ) ) ;
+	return *this ;
+}
+
+CharacterVector::StringProxy& CharacterVector::StringProxy::operator+=( const StringProxy& rhs){
+	std::string full( CHAR(STRING_ELT(parent,index)) ) ;
+	full += CHAR(STRING_ELT( rhs.parent, rhs.index)) ;
+	SET_STRING_ELT( parent, index, Rf_mkChar(full.c_str()) ) ;
+	return *this ;
+}
+
 CharacterVector::StringProxy& CharacterVector::StringProxy::operator=( const std::string& rhs){
 	SET_STRING_ELT( parent, index, Rf_mkChar( rhs.c_str() ) ) ;
 	return *this ;
 }
 
+
 const CharacterVector::StringProxy CharacterVector::operator[](int i) const throw(index_out_of_bounds){
 	if( i<0 || i>=length()) throw index_out_of_bounds() ;
 	return StringProxy(const_cast<CharacterVector&>(*this), i) ;

Modified: pkg/src/Rcpp/CharacterVector.h
===================================================================
--- pkg/src/Rcpp/CharacterVector.h	2010-01-11 21:03:55 UTC (rev 346)
+++ pkg/src/Rcpp/CharacterVector.h	2010-01-11 21:24:09 UTC (rev 347)
@@ -44,6 +44,9 @@
 		StringProxy& operator=(const StringProxy& rhs) ;
 		StringProxy& operator=(const std::string& rhs) ;
 		
+		StringProxy& operator+=(const StringProxy& rhs) ;
+		StringProxy& operator+=(const std::string& rhs) ;
+		
 		/* rvalue use */
 		operator SEXP() const ;
 		operator char*() const ;



More information about the Rcpp-commits mailing list