[Rcpp-devel] Deep copy of a NumericVector, IntegerVector, etc. without using clone

Douglas Bates bates at stat.wisc.edu
Sun May 1 17:28:28 CEST 2011

I'm working on translating Tim Davis's CSparse library of C functions
into templated C++ classes.  The purpose is to allow spare matrix
manipulation both using Rcpp objects and using other types of vector
objects, such as std::vector<double>.

The templates are in terms of the vector objects.  The basic
representations involve two sets of indices and the contents, all of
which are vector objects.  In Rcpp we end up with triplet or
compressed sparse column matrices defined as

    typedef CxxSp::spTriplet<Rcpp::IntegerVector, Rcpp::NumericVector>
    typedef CxxSp::spCompCol<Rcpp::IntegerVector, Rcpp::NumericVector>

At times I want to create a deep copy of a vector object in such a way
that it will work for both std::vector<double> and an
Rcpp::NumericVector.  Assuming that the template parameter for the
typename of a  vector of data values is Td, I am using

    Td d_x(x.begin(), x.end());

instead of

    Td d_x(x);

to define the data member d_x from an argument x to the constructor.
My impression is that the first form will cause a deep copy for
Rcpp::NumericVector objects while the second form will copy the
characteristics of the structure but not the contents.  For a
std::vector<double> I think both forms do a deep copy.

Am I correct about shallow copy and deep copy of Rcpp vectors?

More information about the Rcpp-devel mailing list