[Rcpp-devel] "transposing" a list

Dirk Eddelbuettel edd at debian.org
Tue Nov 26 23:48:27 CET 2013


On 26 November 2013 at 14:26, Antonio Piccolboni wrote:
| Hi all,
| I have a list of lists, the inner lists all of the same lengths. I would like
| to create a new list  of lists such that ll[[i]][[j]] == tll[[j]][[i]]`,
|  where the old one is ll and the new one is tll. In R this can be achieved by
| the function
| 
| f = splat(mapply.list)
| 
| mapply.list = 
|   function(...) mapply(FUN=list, ..., SIMPLIFY = FALSE)
| 
| In my application these lists can get pretty long (both inner and outer) so
| here's my attempt with Rcpp. The empty case is checked in R code, so the outer
| list is non-empty
| 
| SEXP t_list(SEXP _ll) {
|   Rcpp::List ll(_ll);
|   Rcpp::List first_col(Rcpp::wrap(ll[0]));  
|   std::vector<std::vector<SEXP> > tll(first_col.size());
|   for(int i = 0; i < ll.size(); i++) {
|     Rcpp::List l(Rcpp::wrap(ll[i]));
|     for(int j = 0; j < l.size(); j++)
|       tll[j].push_back(Rcpp::clone(Rcpp::wrap(l[j])));}
|   std::vector<SEXP> results ;
|   for(int i = 0; i < tll.size(); i++) {
|      results.push_back(Rcpp::clone(Rcpp::wrap(tll[i])));}    
|   return(Rcpp::wrap(results));}

What is in the inner lists?  Do you really need STL types, or is this for
convenience?

"Our" types are thin proxies.  Generally no copies. 

You are mixing our types, as<>(), wrap(), push_back() as well as clone()
quite extensively and be almost guaranteed that you will create copies here.
There may be a better way involving few copies, but this is somewhat
dependent on your data and problem.

| It works just fine for a lists of three lists of length 10 or 100, but at 1000
| it starts mixing up or skipping elements and it can also crash the interpreter.
| One set of test lists was created with ll = list(as.list(1:10^n), as.list
| (rnorm(10^n)), as.list(as.factor(1:10^n))) where n was set to 2, 3  or 4 in
| different tests. I suppose this code is somehow reusing memory that should
|  not be reused and when garbage collection happens the errors start, but I
| don't see how that could happen. Suggestions? Thanks

Avoid clone. 

Avoid wrap if you don't need it.  

Dirk
 
 
| Antonio
| 
| 
| ----------------------------------------------------------------------
| _______________________________________________
| 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