[Rcpp-devel] "transposing" a list
Antonio Piccolboni
antonio at piccolboni.info
Tue Nov 26 23:26:27 CET 2013
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));}
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
Antonio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20131126/590758b1/attachment.html>
More information about the Rcpp-devel
mailing list