<div dir="ltr">You have a point about push_back being amortized constant. In my code I do everything with Lists, which may explain why I needed to preallocate them. Unfortunately I forgot the motivation for using Lists instead of std:vector, which I must have considered at some point. In fact if you look at this commit (<a href="https://github.com/RevolutionAnalytics/rmr2/commit/f8d4b0766ea71fe056a4b3eb0a4ef5ed0b7c6eec">https://github.com/RevolutionAnalytics/rmr2/commit/f8d4b0766ea71fe056a4b3eb0a4ef5ed0b7c6eec</a> for the html-challenged)  I had the push_back in a previous version, but it didn't work. I was using a vector<vector<RObject>> to hold the data, and if I can remember that hit some performance wall. The commit message doesn't really refresh my memory, but it says something about protecting SEXPs. That may be why I didn't want to use a vector<SEXP>. I may have had stability issues and, right or wrong, thought that change was helpful.<div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, May 4, 2015 at 12:23 PM, Tim Keitt <span dir="ltr"><<a href="mailto:tkeitt@utexas.edu" target="_blank">tkeitt@utexas.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote"><span class="">On Sun, May 3, 2015 at 11:29 PM, Antonio Piccolboni <span dir="ltr"><<a href="mailto:antonio@piccolboni.info" target="_blank">antonio@piccolboni.info</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Check <a href="https://github.com/RevolutionAnalytics/rmr2/blob/master/pkg/src/t-list.cpp" target="_blank">here</a>  for something similar to Tim's solution that preallocates all vectors to avoid the costly push_back. Still needs the unlists in R, so it's expensive in that dimension, the number of lists in the output.</div></blockquote><div><br></div></span><div>I may have a way around the unlist part; still needs testing.<br></div><div><br></div><div>push_back is amortized constant so only a little costly.</div><div><br></div><div>THK</div><div><div class="h5"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><span><font color="#888888"><div><br></div><div><br></div><div>Antonio</div></font></span></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div>On Sun, May 3, 2015 at 4:22 PM, Tim Keitt <span dir="ltr"><<a href="mailto:tkeitt@utexas.edu" target="_blank">tkeitt@utexas.edu</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div dir="ltr">A slightly improved version:<div><br></div><div><span><div><font face="monospace, monospace">library(Rcpp)</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">code = '</font></div><div><font face="monospace, monospace">SEXP test(List a)</font></div><div><font face="monospace, monospace">{</font></div></span><div><font face="monospace, monospace">  auto l = Rf_length(a[0]);</font></div><div><font face="monospace, monospace">  using svec = std::vector<SEXP>;</font></div><div><font face="monospace, monospace">  std::vector<svec> x(l);</font></div><div><font face="monospace, monospace">  for (List b : a)</font></div><div><font face="monospace, monospace">  {</font></div><div><font face="monospace, monospace">    if (b.size() != l)</font></div><div><font face="monospace, monospace">      stop("Ragged input");</font></div><span><div><font face="monospace, monospace">    for (int i = 0; i != l; ++i)</font></div></span><span><div><font face="monospace, monospace">      x[i].push_back(b[i]);</font></div><div><font face="monospace, monospace">  }</font></div><div><font face="monospace, monospace">  return wrap(x);</font></div><div><font face="monospace, monospace">}'</font></div><div><font face="monospace, monospace"><br></font></div></span><div><font face="monospace, monospace">f = cppFunction(code = code, plugins = "cpp11")</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">res = lapply(f(list(list(T, 1, 'a'),</font></div><div><font face="monospace, monospace">                    list(F, 2, 'b'))),</font></div><div><font face="monospace, monospace">             unlist)</font></div><div><br></div></div></div><div class="gmail_extra"><div><div><br><div class="gmail_quote">On Sun, May 3, 2015 at 11:57 AM, Tim Keitt <span dir="ltr"><<a href="mailto:tkeitt@utexas.edu" target="_blank">tkeitt@utexas.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Here's a really bare-bones version. Not very pretty or complete, but it does do the job. Could the 'unlist' part be converted to Rcpp?<div><br></div><div>THK</div><div><br></div><div><div>library(Rcpp)</div><div><br></div><div>code = '</div><div>SEXP test(List a)</div><div>{</div><div>  int l = Rf_length(a[0]);</div><div>  typedef std::vector<SEXP> svec;</div><div>  std::vector<svec> x(l);</div><div>  for (int i = 0; i != l; ++i)</div><div>    for (int j = 0; j != a.size(); ++j)</div><div>    {</div><div>      List b = a[j];</div><div>      x[i].push_back(b[i]);</div><div>    }</div><div>  return wrap(x);</div><div>}'</div><div><br></div><div>f = cppFunction(code = code)</div><div><br></div><div>res = lapply(f(list(list(1, 'a'), list(2, 'b'))), unlist)</div></div><div><br></div></div><div class="gmail_extra"><div><div><br><div class="gmail_quote">On Sat, May 2, 2015 at 1:18 PM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span><br>
On 2 May 2015 at 10:49, William Dunlap wrote:<br>
| Since translation from R to Rcpp is "seamless" I will leave that to you.<br>
<br>
</span>One problem is with the strongly typed nature of C++.  Rearranging dynamicly<br>
growing objects can be done.  I think I used Boost's variant type a few<br>
years.  I am sure there are other possibilities.  We should collect a few and<br>
compare.   But in C++ please :)<br>
<span><font color="#888888"><br>
Dirk<br>
<br>
--<br>
<a href="http://dirk.eddelbuettel.com" target="_blank">http://dirk.eddelbuettel.com</a> | @eddelbuettel | <a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a><br>
</font></span></blockquote></div><br><br clear="all"><div><br></div></div></div><span><font color="#888888">-- <br><div><div dir="ltr"><a href="http://www.keittlab.org/" target="_blank">http://www.keittlab.org/</a></div></div>
</font></span></div>
</blockquote></div><br><br clear="all"><div><br></div></div></div><span><font color="#888888">-- <br><div><div dir="ltr"><a href="http://www.keittlab.org/" target="_blank">http://www.keittlab.org/</a></div></div>
</font></span></div>
<br></div></div><span>_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br></span></blockquote></div><br></div>
<br>_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br></blockquote></div></div></div><span class="HOEnZb"><font color="#888888"><br><br clear="all"><div><br></div>-- <br><div><div dir="ltr"><a href="http://www.keittlab.org/" target="_blank">http://www.keittlab.org/</a></div></div>
</font></span></div></div>
</blockquote></div><br></div>