Skip to content

Commit e0ea6ed

Browse files
committed
Merge pull request #339 from fplaza/DimNameProxy-patch1
Add assignment operator to DimNameProxy
2 parents 686e04a + 73974db commit e0ea6ed

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

inst/include/Rcpp/vector/DimNameProxy.h

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,37 @@ namespace internal{
3232
DimNameProxy(SEXP data, int dim): data_(data), dim_(dim) {}
3333
DimNameProxy(DimNameProxy const& other):
3434
data_(other.data_), dim_(other.dim_) {}
35-
36-
inline DimNameProxy& operator=(SEXP other) {
37-
SEXP dim = Rf_getAttrib(data_, R_DimSymbol);
38-
if (INTEGER(dim)[dim_] != Rf_length(other)) {
39-
std::stringstream s;
40-
s << "dimension extent is '"
41-
<< INTEGER(dim)[dim_]
42-
<< "' while length of names is '"
43-
<< Rf_length(other)
44-
<< "'";
45-
stop(s.str());
46-
}
47-
SEXP dimnames = Rf_getAttrib(data_, R_DimNamesSymbol);
48-
SEXP dims = Rf_getAttrib(data_, R_DimSymbol);
49-
if (Rf_isNull(dimnames)) {
50-
Shield<SEXP> new_dimnames(Rf_allocVector(VECSXP, Rf_length(dims)));
51-
SET_VECTOR_ELT(new_dimnames, dim_, other);
52-
Rf_setAttrib(data_, R_DimNamesSymbol, new_dimnames);
35+
36+
inline DimNameProxy& assign(SEXP other) {
37+
if (Rf_length(other) == 0)
38+
{
39+
Rf_setAttrib(data_, R_DimNamesSymbol, R_NilValue);
5340
} else {
54-
SET_VECTOR_ELT(dimnames, dim_, other);
41+
SEXP dims = Rf_getAttrib(data_, R_DimSymbol);
42+
if (INTEGER(dims)[dim_] != Rf_length(other)) {
43+
stop("dimension extent is '%d' while length of names is '%d'", INTEGER(dims)[dim_], Rf_length(other));
44+
}
45+
46+
SEXP dimnames = Rf_getAttrib(data_, R_DimNamesSymbol);
47+
if (Rf_isNull(dimnames)) {
48+
Shield<SEXP> new_dimnames(Rf_allocVector(VECSXP, Rf_length(dims)));
49+
SET_VECTOR_ELT(new_dimnames, dim_, other);
50+
Rf_setAttrib(data_, R_DimNamesSymbol, new_dimnames);
51+
} else {
52+
SET_VECTOR_ELT(dimnames, dim_, other);
53+
}
5554
}
5655
return *this;
5756
}
5857

58+
inline DimNameProxy& operator=(SEXP other) {
59+
return assign(other);
60+
}
61+
62+
inline DimNameProxy& operator=(const DimNameProxy& other) {
63+
return assign(SEXP(other));
64+
}
65+
5966
inline operator SEXP() const {
6067
SEXP dimnames = Rf_getAttrib(data_, R_DimNamesSymbol);
6168
return Rf_isNull(dimnames) ? (R_NilValue) : (VECTOR_ELT(dimnames, dim_));

0 commit comments

Comments
 (0)