Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upEnable e.g. `x[...] = y[...]` #345
Comments
|
Adding this operator solves the issue SubsetProxy& operator=(const SubsetProxy& other) {
if (other.indices_n == 1) {
for (int i=0; i < indices_n; ++i) {
lhs[ indices[i] ] = other.lhs[other.indices[0]];
}
}
else if (indices_n == other.indices_n) {
for (int i=0; i < indices_n; ++i)
lhs[ indices[i] ] = other.lhs[other.indices[i]];
}
else {
stop("index error");
}
return *this;
} |
|
By the way, I think that template <int OtherRTYPE, template <class> class OtherStoragePolicy>
SubsetProxy& operator=(const Vector<OtherRTYPE, OtherStoragePolicy>& other) {
int n = other.size();
if (indices_n != n) stop("index error");
if (n == 1) {
for (int i=0; i < n; ++i) {
lhs[ indices[i] ] = other[0];
}
} else if (n == indices_n) {
for (int i=0; i < n; ++i) {
lhs[ indices[i] ] = other[i];
}
} else {
stop("index error");
}
return *this;
}is incorrect and should be replaced by template <int OtherRTYPE, template <class> class OtherStoragePolicy>
SubsetProxy& operator=(const Vector<OtherRTYPE, OtherStoragePolicy>& other) {
int n = other.size();
if (n == 1) {
for (int i=0; i < indices_n; ++i) {
lhs[ indices[i] ] = other[0];
}
} else if (n == indices_n) {
for (int i=0; i < n; ++i) {
lhs[ indices[i] ] = other[i];
}
} else {
stop("index error");
}
return *this;
} |
|
And the error message from "index error" to "number of items to replace is not a multiple of replacement length" |
|
However, this code doesn't produce the expected result #include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector valuesOverThree(NumericVector b){
NumericVector a(b.size());
NumericVector temp = b[b > 3];
a[b <= 3] = temp;
return a;
}
// [[Rcpp::export]]
NumericVector valuesOverThree2(NumericVector b){
NumericVector a(b.size());
a[b <= 3] = b[b > 3];
return a;
}
/*** R
valuesOverThree(1:6)
valuesOverThree2(1:6)
*/
After investigation, I discovered that "SubsetProxy& operator=(bool other)" is called instead of "SubsetProxy& operator=(const SubsetProxy& other) " |
|
You might need to add template parameters for the other I think you might be right about the |
|
Adding template parameters makes all the attributes of the other object private. |
|
This is because the implicit converter, https://github.com/RcppCore/Rcpp/blob/master/inst/include/Rcpp/vector/Subsetter.h#L104 |
|
I think in your test case |
|
Good point. After enabling debugging: |
|
Some pseudo-code:
This seems like a step in the right direction, but does require the appropriate |
|
I don't understand how the assign method will have an access to the other object attributes since it does not have the same type as 'this'. |
|
Here is a dirty solution which creates a temporary vector from 'other' before assigning its values to 'this'. template <int RTYPE_OTHER, template <class> class StoragePolicyOther,int RHS_RTYPE_OTHER, bool RHS_NA_OTHER, typename RHS_T_OTHER>
SubsetProxy& operator=(const SubsetProxy<RTYPE_OTHER, StoragePolicyOther, RHS_RTYPE_OTHER, RHS_NA_OTHER, RHS_T_OTHER>& other) {
Vector<RTYPE_OTHER, StoragePolicyOther> other_vec = other;
if (other_vec.length() == 1) {
for (int i=0; i < indices_n; ++i) {
lhs[ indices[i] ] = other_vec[0];
}
}
else if (indices_n == other_vec.length())
{
for (int i=0; i < indices_n; ++i)
lhs[ indices[i] ] = other_vec[i];
}
else {
stop("index error");
}
return *this;
} |
|
Is there any reason why this wouldn't work? template <int RTYPE_OTHER, template <class> class StoragePolicyOther,int RHS_RTYPE_OTHER, bool RHS_NA_OTHER, typename RHS_T_OTHER>
SubsetProxy& operator=(const SubsetProxy<RTYPE_OTHER, StoragePolicyOther, RHS_RTYPE_OTHER, RHS_NA_OTHER, RHS_T_OTHER>& other) {
if (other.length() == 1) {
for (int i=0; i < indices_n; ++i) {
lhs[ indices[i] ] = get(other, 0);
}
}
else if (indices_n == other.length())
{
for (int i=0; i < indices_n; ++i)
lhs[ indices[i] ] = get(other, i);
}
else {
stop("index error");
}
return *this;
}It seems like this would be fine, given appropriate definitions of |
Re: http://stackoverflow.com/questions/31992883/in-rcpp-why-cant-i-assign-a-subsetted-vector-to-another-subsetted-vector
It's possible that this could be implemented with an appropriate
operator=taking aSubsetProxyobject: https://github.com/RcppCore/Rcpp/blob/master/inst/include/Rcpp/vector/Subsetter.h