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

Toni Giorgino toni.giorgino at gmail.com
Wed Jun 5 15:37:55 CEST 2013


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.

The single-argument erase() also seems to behave unexpectedly (below).

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<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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130605/4e78dea0/attachment.html>


More information about the Rcpp-devel mailing list