Skip to content

Commit c5cf125

Browse files
author
Florian Plaza Oñate
committed
Add assignment operator to DimNameProxy
1 parent 686e04a commit c5cf125

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

inst/include/Rcpp/vector/DimNameProxy.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,31 @@ namespace internal{
5656
return *this;
5757
}
5858

59+
inline DimNameProxy& operator=(const DimNameProxy& other) {
60+
SEXP dim = Rf_getAttrib(data_, R_DimSymbol);
61+
if (INTEGER(dim)[dim_] != INTEGER(dim)[other.dim_]) {
62+
std::stringstream s;
63+
s << "dimension extent is '"
64+
<< INTEGER(dim)[dim_]
65+
<< "' while length of names is '"
66+
<< INTEGER(dim)[other.dim_]
67+
<< "'";
68+
stop(s.str());
69+
}
70+
SEXP dimnames = Rf_getAttrib(data_, R_DimNamesSymbol);
71+
SEXP dims = Rf_getAttrib(data_, R_DimSymbol);
72+
SEXP other_dimnames = SEXP(other);
73+
if (Rf_isNull(dimnames)) {
74+
Shield<SEXP> new_dimnames(Rf_allocVector(VECSXP, Rf_length(dims)));
75+
SET_VECTOR_ELT(new_dimnames, dim_, other_dimnames);
76+
Rf_setAttrib(data_, R_DimNamesSymbol, new_dimnames);
77+
} else {
78+
SET_VECTOR_ELT(dimnames, dim_, other_dimnames);
79+
}
80+
return *this;
81+
}
82+
83+
5984
inline operator SEXP() const {
6085
SEXP dimnames = Rf_getAttrib(data_, R_DimNamesSymbol);
6186
return Rf_isNull(dimnames) ? (R_NilValue) : (VECTOR_ELT(dimnames, dim_));

0 commit comments

Comments
 (0)