[Rcpp-commits] r4330 - in pkg/Rcpp: . inst inst/include/Rcpp/api/meat inst/unitTests inst/unitTests/cpp

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jun 5 16:25:55 CEST 2013


Author: romain
Date: 2013-06-05 16:25:55 +0200 (Wed, 05 Jun 2013)
New Revision: 4330

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/NEWS.Rd
   pkg/Rcpp/inst/include/Rcpp/api/meat/Vector.h
   pkg/Rcpp/inst/unitTests/cpp/Vector.cpp
   pkg/Rcpp/inst/unitTests/runit.Vector.R
Log:
fixed Vector<>::erase( iterator, iterator )

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2013-06-05 11:29:19 UTC (rev 4329)
+++ pkg/Rcpp/ChangeLog	2013-06-05 14:25:55 UTC (rev 4330)
@@ -1,3 +1,9 @@
+2013-06-05 Romain Francois <romain at r-enthusiasts.com>
+
+        * include/Rcpp/api/meat/Vector.h : fixed Vector<>::erase(iterator, iterator)
+        * unitTests/cpp/Vector.cpp : added regression test for the above fix
+        * unitTests/runit.Vector.R : added regression test for the above fix
+        
 2013-05-31 Romain Francois <romain at r-enthusiasts.com>
 
         * unitTests/runit.sugar.R : new test for is_infinite and is_nan

Modified: pkg/Rcpp/inst/NEWS.Rd
===================================================================
--- pkg/Rcpp/inst/NEWS.Rd	2013-06-05 11:29:19 UTC (rev 4329)
+++ pkg/Rcpp/inst/NEWS.Rd	2013-06-05 14:25:55 UTC (rev 4330)
@@ -24,6 +24,9 @@
       \item The \code{operator-()} semantics for \code{Date} and
       \code{Datetime} are now more inline with standard C++ behaviour.
       \item RNGScope counter now uses unsigned long rather than int.
+      \item \code{Vector<*>::erase(iterator, iterator)} was fixed. Now it does 
+      not remove the element pointed by last (similar to what is done on stl types
+      and what was intended initially). Reported on Rcpp-devel by Toni Giorgino. 
     }
     \item Changes in Rcpp sugar:
     \itemize{

Modified: pkg/Rcpp/inst/include/Rcpp/api/meat/Vector.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/api/meat/Vector.h	2013-06-05 11:29:19 UTC (rev 4329)
+++ pkg/Rcpp/inst/include/Rcpp/api/meat/Vector.h	2013-06-05 14:25:55 UTC (rev 4330)
@@ -285,7 +285,7 @@
 		
         iterator it = begin() ;
         iterator this_end = end() ;
-        int nremoved = std::distance(first,last)+1 ;
+        int nremoved = std::distance(first,last) ;
         int target_size = size() - nremoved  ;
         Vector target( target_size ) ;
         iterator target_it = target.begin() ;
@@ -297,7 +297,7 @@
                 *target_it = *it ;
             }
             result = it ;
-            for( it = last +1 ; it < this_end; ++it, ++target_it ){
+            for( it = last ; it < this_end; ++it, ++target_it ){
                 *target_it = *it ;
             }
         } else{
@@ -308,7 +308,7 @@
                 SET_STRING_ELT( newnames, i, STRING_ELT(names, i ) );
             }
             result = it ;
-            for( it = last +1 ; it < this_end; ++it, ++target_it, i++ ){
+            for( it = last ; it < this_end; ++it, ++target_it, i++ ){
                 *target_it = *it ;
                 SET_STRING_ELT( newnames, i, STRING_ELT(names, i + nremoved ) );
             }

Modified: pkg/Rcpp/inst/unitTests/cpp/Vector.cpp
===================================================================
--- pkg/Rcpp/inst/unitTests/cpp/Vector.cpp	2013-06-05 11:29:19 UTC (rev 4329)
+++ pkg/Rcpp/inst/unitTests/cpp/Vector.cpp	2013-06-05 14:25:55 UTC (rev 4330)
@@ -219,6 +219,13 @@
 }
 
 // [[Rcpp::export]]
+List integer_erase_range( IntegerVector x, IntegerVector y ){
+    x.erase(x.begin()+5, x.end()-1 );
+    y.erase(y.begin()+5, y.end()-1 );
+    return List::create( x, y ) ;
+}
+
+// [[Rcpp::export]]
 IntegerVector integer_erase2( IntegerVector y ){
     y.erase(1,2) ;
     return y ;
@@ -382,7 +389,7 @@
 
 // [[Rcpp::export]]
 List list_erase_range( List list ){
-    list.erase( 0, 1 ) ;
+    list.erase( 0, 2 ) ;
     return list ;
 }
 

Modified: pkg/Rcpp/inst/unitTests/runit.Vector.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Vector.R	2013-06-05 11:29:19 UTC (rev 4329)
+++ pkg/Rcpp/inst/unitTests/runit.Vector.R	2013-06-05 14:25:55 UTC (rev 4330)
@@ -211,15 +211,25 @@
     checkEquals( fun(x), target, msg = "IntegerVector erase" )
 }
 
+test.IntegerVector.erase.range <- function(){
+    x <- y <- 1:10
+    names(y) <- letters[1:10]
+    res <- integer_erase_range( x, y )
+    checkEquals( res[[1L]], c(1:5, 10L) , msg = "IntegerVector erase range unnamed" )
+
+    z <- y[-(6:9)]
+    checkEquals( res[[2L]], z , msg = "IntegerVector erase range named" )
+}
+
 test.IntegerVector.erase2 <- function(){
     fun <- integer_erase2
-    checkEquals( fun(1:4), c(1L, 4L), msg = "IntegerVector erase2" )
+    checkEquals( fun(1:4), c(1L, 3L, 4L), msg = "IntegerVector erase2" )
 
     x <- 1:4
     names(x) <- letters[1:4]
 
-    target <- c(1L, 4L)
-    names(target) <- c( "a", "d" )
+    target <- c(1L, 3L, 4L)
+    names(target) <- c( "a", "c", "d" )
 
     checkEquals( fun(x), target, msg = "IntegerVector erase2" )
 }



More information about the Rcpp-commits mailing list