[Rcpp-devel] Fwd: Possible inconsistency in Rcpp vs stl in erase(iterator, iterator)

Dirk Eddelbuettel edd at debian.org
Wed Jun 5 15:51:13 CEST 2013


On 5 June 2013 at 15:37, Toni Giorgino wrote:
| Dears,
| 
| thanks. In fact I've found the problem while looking for a workaround to the
| push_back() slowness (used here for convenience only). The workaround was to
| preallocate the List, then discard the unneeded portion, for which I looked
| into erase(). This was many orders of magnitude faster.

A valid alternative is using actual STL types, and converting those before
returning (which even works automagically). You can also look into Armadillo
(or Eigen) vectors.
 
| The single-argument erase() also seems to behave unexpectedly (below).

Yes, right now it looks like you found a genuine, ahem, "infelicity". 

Dirk
 
| Best
| Toni
| 
| #include <Rcpp.h>
| #include <iostream>
| 
| // [[Rcpp::export]]
| SEXP truncateTest2() {
| 
|   using namespace std;
|   using namespace Rcpp;
| 
|   Rcpp::List l;
|   std::vector<int> v;
| 
|   for (int i=1; i<=10; i++) {
|     v.push_back(i);
|     l.push_back(i);
|   }
| 
|   // Attempt truncation in another way
|   vector<int>::iterator vi=v.begin()+1;
|   while(vi!=v.end())
|     vi=v.erase(vi);        // This works
| 
|   cout << "std::vector left with " << v.size() << endl;
| 
|   Rcpp::List::iterator li=l.begin()+1;
|   while(li!=l.end())
|     li=l.erase(li);        // This doesn't
| 
|   cout << "Rcpp::List  left with " << l.size() << endl;
| 
|   return(R_NilValue);
| 
| }
| 
| 
| 
| 
| 
| 
| 
| On Wed, Jun 5, 2013 at 2:50 PM, Romain Francois <romain at r-enthusiasts.com>
| wrote:
| 
|     Thanks. I'll have a look.
| 
|     Le 05/06/13 14:09, Toni Giorgino a écrit :
| 
| 
|         Dears,
| 
|         I am a newbie to Rcpp.  I may have found an off-by-one inconsistency
|         in
|         the handling of  List::erase(iter1,iter2) operation with respect to
|         its
|         homologous in the STL (which I suppose is intended to mimic).  In STL,
|         iter2 can be the container's .end(); in Rcpp, the same gives an out of
|         boundary error. (See below)
| 
|         Thanks for the incredible package. Best
| 
|         Test case...
| 
|         #include <Rcpp.h>
|         #include <iostream>
| 
|         // [[Rcpp::export]]
|         SEXP truncateTest() {
|         BEGIN_RCPP
| 
|            using namespace std;
|            Rcpp::List l;
|            std::vector<int> v;
| 
|            for (int i=1; i<=10; i++) {
|              v.push_back(i);
|              l.push_back(i);
|            }
| 
|            v.erase(v.begin()+5,v.end()-1);
|            l.erase(l.begin()+5,l.end()-1); // ?
| 
|            cout << "std::vector left with " << v.size() << endl;
|            cout << "Rcpp::List  left with " << l.size() << endl;
| 
|         END_RCPP
|         }
| 
|         /*** R
|         #  library(Rcpp)
|         #  sourceCpp("truncateTest.cpp")
|         #  truncateTest()
|         #  #prints 6 and 5
|         */
| 
| 
| 
|     --
|     Romain Francois
|     Professional R Enthusiast
|     +33(0) 6 28 91 30 30
| 
|     R Graph Gallery: http://gallery.r-enthusiasts.com
| 
|     blog:            http://blog.r-enthusiasts.com
|     |- http://bit.ly/Zs97qg  : highlight 0.4.1
|     `- http://bit.ly/10X94UM : Mobile version of the graph gallery
| 
| 
| 
| 
| 
| --
| Toni
| 
| 
| 
| 
| 
| --
| Toni
| 
| 
| 
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list