Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable e.g. x[...] = y[...] #345

Closed
kevinushey opened this issue Aug 15, 2015 · 13 comments
Closed

Enable e.g. x[...] = y[...] #345

kevinushey opened this issue Aug 15, 2015 · 13 comments

Comments

@kevinushey
Copy link
Contributor

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 a SubsetProxy object: https://github.com/RcppCore/Rcpp/blob/master/inst/include/Rcpp/vector/Subsetter.h

@fplazaonate
Copy link
Contributor

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;
    }

@fplazaonate
Copy link
Contributor

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;
    }

@fplazaonate
Copy link
Contributor

And the error message from "index error" to "number of items to replace is not a multiple of replacement length"

@fplazaonate
Copy link
Contributor

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)
*/
> valuesOverThree(1:6)
[1] 4 5 6 0 0 0

> valuesOverThree2(1:6)
[1] 1 1 1 0 0 0

After investigation, I discovered that "SubsetProxy& operator=(bool other)" is called instead of "SubsetProxy& operator=(const SubsetProxy& other) "
I guess this is because the other "SubSetProxy" object as a different type.

@kevinushey
Copy link
Contributor Author

You might need to add template parameters for the other SubsetProxy object used in the operator= method.

I think you might be right about the operator= method (the intention was to enable recycling when assigning an item of length one).

@fplazaonate
Copy link
Contributor

Adding template parameters makes all the attributes of the other object private.

@thirdwing
Copy link
Member

This is because the implicit converter, operator SEXP() const, will be called.

https://github.com/RcppCore/Rcpp/blob/master/inst/include/Rcpp/vector/Subsetter.h#L104

@thirdwing
Copy link
Member

@fplazaonate
Copy link
Contributor

Good point.

After enabling debugging:
b<=3 is a "Rcpp::sugar::Comparator_With_One_Value<14, Rcpp::sugar::less_or_equal<14>, true, Rcpp::Vector<14, Rcpp::PreserveStorage> >"
b>3 is a "Rcpp::sugar::Comparator_With_One_Value<14, Rcpp::sugar::greater<14>, true, Rcpp::Vector<14, Rcpp::PreserveStorage> >"

@kevinushey
Copy link
Contributor Author

Some pseudo-code:

    SubsetProxy& operator=(const SubsetProxy& other)
    {
        for (int i = 0; i < indices_n; ++i)
            lhs[indices[i]] = other.lhs[other.indices[i]];

        return *this;
    }

    template <typename T>
    typename Rcpp::traits::enable_if<
        Rcpp::traits::has_subset_proxy_type<T>::value, SubsetProxy&
    >::type 
    operator=(const T& other)
    {
        for (int i = 0; i < indices_n; ++i)
            assign(lhs, other, i);
        return *this;
    }

This seems like a step in the right direction, but does require the appropriate has_subset_proxy_type trait as well as an assign definition (which will be tricky)

@fplazaonate
Copy link
Contributor

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'.

@fplazaonate
Copy link
Contributor

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;
    }

@kevinushey
Copy link
Contributor Author

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 .length() and get(). (Figuring out the signature of get will be tough without return type deduction though. The ultimate goal is to have the underlying operator= decide whether the assignment really does conform, though)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants