<div dir="ltr">That worked, thanks<div><br></div><div><br></div><div>Antonio</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Nov 26, 2013 at 2:48 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"><div class="im"><br>
On 26 November 2013 at 14:26, Antonio Piccolboni wrote:<br>
| Hi all,<br>
| I have a list of lists, the inner lists all of the same lengths. I would like<br>
| to create a new list  of lists such that ll[[i]][[j]] == tll[[j]][[i]]`,<br>
|  where the old one is ll and the new one is tll. In R this can be achieved by<br>
| the function<br>
|<br>
| f = splat(mapply.list)<br>
|<br>
| mapply.list = <br>
|   function(...) mapply(FUN=list, ..., SIMPLIFY = FALSE)<br>
|<br>
| In my application these lists can get pretty long (both inner and outer) so<br>
| here's my attempt with Rcpp. The empty case is checked in R code, so the outer<br>
| list is non-empty<br>
|<br>
| SEXP t_list(SEXP _ll) {<br>
|   Rcpp::List ll(_ll);<br>
|   Rcpp::List first_col(Rcpp::wrap(ll[0]));  <br>
|   std::vector<std::vector<SEXP> > tll(first_col.size());<br>
|   for(int i = 0; i < ll.size(); i++) {<br>
|     Rcpp::List l(Rcpp::wrap(ll[i]));<br>
|     for(int j = 0; j < l.size(); j++)<br>
|       tll[j].push_back(Rcpp::clone(Rcpp::wrap(l[j])));}<br>
|   std::vector<SEXP> results ;<br>
|   for(int i = 0; i < tll.size(); i++) {<br>
|      results.push_back(Rcpp::clone(Rcpp::wrap(tll[i])));}    <br>
|   return(Rcpp::wrap(results));}<br>
<br>
</div>What is in the inner lists?  Do you really need STL types, or is this for<br>
convenience?<br>
<br>
"Our" types are thin proxies.  Generally no copies.<br>
<br>
You are mixing our types, as<>(), wrap(), push_back() as well as clone()<br>
quite extensively and be almost guaranteed that you will create copies here.<br>
There may be a better way involving few copies, but this is somewhat<br>
dependent on your data and problem.<br>
<div class="im"><br>
| It works just fine for a lists of three lists of length 10 or 100, but at 1000<br>
| it starts mixing up or skipping elements and it can also crash the interpreter.<br>
| One set of test lists was created with ll = list(as.list(1:10^n), as.list<br>
| (rnorm(10^n)), as.list(as.factor(1:10^n))) where n was set to 2, 3  or 4 in<br>
| different tests. I suppose this code is somehow reusing memory that should<br>
|  not be reused and when garbage collection happens the errors start, but I<br>
| don't see how that could happen. Suggestions? Thanks<br>
<br>
</div>Avoid clone.<br>
<br>
Avoid wrap if you don't need it.<br>
<br>
Dirk<br>
<br>
<br>
| Antonio<br>
|<br>
|<br>
| ----------------------------------------------------------------------<br>
| _______________________________________________<br>
| Rcpp-devel mailing list<br>
| <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">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 class="HOEnZb"><font color="#888888">--<br>
Dirk Eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a> | <a href="http://dirk.eddelbuettel.com" target="_blank">http://dirk.eddelbuettel.com</a><br>
</font></span></blockquote></div><br></div>