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

@kevinushey kevinushey commented Aug 15, 2015

@fplaza
Copy link
Contributor

@fplaza fplaza commented Aug 16, 2015

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;
    }
@fplaza
Copy link
Contributor

@fplaza fplaza commented Aug 16, 2015

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;
    }
@fplaza
Copy link
Contributor

@fplaza fplaza commented Aug 16, 2015

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

@fplaza
Copy link
Contributor

@fplaza fplaza commented Aug 16, 2015

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

@kevinushey kevinushey commented Aug 16, 2015

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

@fplaza
Copy link
Contributor

@fplaza fplaza commented Aug 16, 2015

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

@thirdwing
Copy link
Member

@thirdwing thirdwing commented Aug 17, 2015

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

@thirdwing thirdwing commented Aug 17, 2015

@fplaza
Copy link
Contributor

@fplaza fplaza commented Aug 17, 2015

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

@kevinushey kevinushey commented Aug 17, 2015

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)

@fplaza
Copy link
Contributor

@fplaza fplaza commented Aug 17, 2015

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

@fplaza
Copy link
Contributor

@fplaza fplaza commented Aug 17, 2015

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

@kevinushey kevinushey commented Aug 18, 2015

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)

thirdwing pushed a commit to thirdwing/Rcpp that referenced this issue Aug 28, 2015
Qiang Kou
@thirdwing thirdwing closed this Aug 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.