Hi Alexandre, Thank you so much! That worked and helped me understand why it was happening :) The idea of pointers is new to me so I think I need to spend more time learning about that.
Thanks again, Josh On Sat, Jul 7, 2012 at 4:46 PM, Alexandre Bujard <[email protected]> wrote: > Hi Joshua, > > When you declare "NumericVector xx(x)" xx is a copy of the pointer x. But > they both address the same location in the memory. > What you need here is to do a "deep copy" of the vector x. The best was to > do that in Rcpp is to use the function "clone". > Also declaring your vector xx with "const" will guarantee that your vector x > will not be modified by the function. > > So your code should look like this : > ## Rcpp function body > src <- ' > const NumericVector xx(x); > int n = xx.size(); > NumericVector res = clone(xx); > > int toggle = 0; > int tot = 100; > int max = 100; > [...] > > On Sun, Jul 8, 2012 at 1:20 AM, Joshua Wiley <[email protected]> wrote: >> >> Hi, >> >> I am familiar with R but quite new to C++. I am reading a C++ >> textbook, so if this is an obvious question just resulting in my >> ignorance of C++, please let me know. >> >> I tried to translate someone else's R loop that was slow into c++ to >> use with Rcpp and the inline package: >> >> require(Rcpp) >> require(inline) >> >> ## Rcpp function body >> src <- ' >> NumericVector xx(x); >> int n = xx.size(); >> NumericVector res(xx); >> int toggle = 0; >> int tot = 100; >> int max = 100; >> >> typedef NumericVector::iterator vec_iterator; >> vec_iterator ixx = xx.begin(); >> vec_iterator ires = res.begin(); >> for (int i = 0; i < n; i++) { >> if (ixx[i] != 0) { >> if (toggle == 1) { >> ires[i] = 0; >> } >> if (toggle != 1) { >> tot += ixx[i]; >> if (tot > max) { >> max = tot; >> } >> if (tot <= max) { >> if (.98 * max > tot) { >> toggle = 1; >> } >> } >> } >> } >> if (ixx[i] == 0) { >> tot = 100; >> max = 100; >> toggle = 0; >> } >> } >> return res; >> ' >> >> ## compile the function >> foo.rcpp <- cxxfunction(signature(x = "numeric"), src, plugin = "Rcpp") >> >> ## here is a little input vector, the function should >> ## output the same but with a 0 instead of 8 >> > d >> [1] 0 0 0 1 3 4 5 -1 2 3 -5 8 >> > foo.rcpp(d) # great it works! >> [1] 0 0 0 1 3 4 5 -1 2 3 -5 0 >> > d # but now d itself has changed? >> [1] 0 0 0 1 3 4 5 -1 2 3 -5 0 >> >> It seems particularly surprising to me, because I do not think I even >> modify the input vector, I copy it into the numeric vector res and >> alter that. >> >> Thanks, >> >> Josh >> >> >> -- >> Joshua Wiley >> Ph.D. Student, Health Psychology >> Programmer Analyst II, Statistical Consulting Group >> University of California, Los Angeles >> https://joshuawiley.com/ >> _______________________________________________ >> Rcpp-devel mailing list >> [email protected] >> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel > > -- Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/ _______________________________________________ Rcpp-devel mailing list [email protected] https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
