[Rcpp-devel] List of Lists to List of Vectors

Antonio Piccolboni antonio at piccolboni.info
Mon May 4 21:48:20 CEST 2015


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 (
https://github.com/RevolutionAnalytics/rmr2/commit/f8d4b0766ea71fe056a4b3eb0a4ef5ed0b7c6eec
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.


On Mon, May 4, 2015 at 12:23 PM, Tim Keitt <tkeitt at utexas.edu> wrote:

>
>
> On Sun, May 3, 2015 at 11:29 PM, Antonio Piccolboni <
> antonio at piccolboni.info> wrote:
>
>> Check here
>> <https://github.com/RevolutionAnalytics/rmr2/blob/master/pkg/src/t-list.cpp>
>> 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.
>>
>
> I may have a way around the unlist part; still needs testing.
>
> push_back is amortized constant so only a little costly.
>
> THK
>
>
>>
>>
>> Antonio
>>
>> On Sun, May 3, 2015 at 4:22 PM, Tim Keitt <tkeitt at utexas.edu> wrote:
>>
>>> A slightly improved version:
>>>
>>> library(Rcpp)
>>>
>>> code = '
>>> SEXP test(List a)
>>> {
>>>   auto l = Rf_length(a[0]);
>>>   using svec = std::vector<SEXP>;
>>>   std::vector<svec> x(l);
>>>   for (List b : a)
>>>   {
>>>     if (b.size() != l)
>>>       stop("Ragged input");
>>>     for (int i = 0; i != l; ++i)
>>>       x[i].push_back(b[i]);
>>>   }
>>>   return wrap(x);
>>> }'
>>>
>>> f = cppFunction(code = code, plugins = "cpp11")
>>>
>>> res = lapply(f(list(list(T, 1, 'a'),
>>>                     list(F, 2, 'b'))),
>>>              unlist)
>>>
>>>
>>> On Sun, May 3, 2015 at 11:57 AM, Tim Keitt <tkeitt at utexas.edu> wrote:
>>>
>>>> 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?
>>>>
>>>> THK
>>>>
>>>> library(Rcpp)
>>>>
>>>> code = '
>>>> SEXP test(List a)
>>>> {
>>>>   int l = Rf_length(a[0]);
>>>>   typedef std::vector<SEXP> svec;
>>>>   std::vector<svec> x(l);
>>>>   for (int i = 0; i != l; ++i)
>>>>     for (int j = 0; j != a.size(); ++j)
>>>>     {
>>>>       List b = a[j];
>>>>       x[i].push_back(b[i]);
>>>>     }
>>>>   return wrap(x);
>>>> }'
>>>>
>>>> f = cppFunction(code = code)
>>>>
>>>> res = lapply(f(list(list(1, 'a'), list(2, 'b'))), unlist)
>>>>
>>>>
>>>> On Sat, May 2, 2015 at 1:18 PM, Dirk Eddelbuettel <edd at debian.org>
>>>> wrote:
>>>>
>>>>>
>>>>> On 2 May 2015 at 10:49, William Dunlap wrote:
>>>>> | Since translation from R to Rcpp is "seamless" I will leave that to
>>>>> you.
>>>>>
>>>>> One problem is with the strongly typed nature of C++.  Rearranging
>>>>> dynamicly
>>>>> growing objects can be done.  I think I used Boost's variant type a few
>>>>> years.  I am sure there are other possibilities.  We should collect a
>>>>> few and
>>>>> compare.   But in C++ please :)
>>>>>
>>>>> Dirk
>>>>>
>>>>> --
>>>>> http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> http://www.keittlab.org/
>>>>
>>>
>>>
>>>
>>> --
>>> http://www.keittlab.org/
>>>
>>> _______________________________________________
>>> 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
>>>
>>
>>
>> _______________________________________________
>> 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
>>
>
>
>
> --
> http://www.keittlab.org/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20150504/ffbdc110/attachment.html>


More information about the Rcpp-devel mailing list