From c5cf12560b9908667b009baf8bf24effccde1672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Plaza=20O=C3=B1ate?= Date: Fri, 14 Aug 2015 13:47:21 +0200 Subject: [PATCH 1/6] Add assignment operator to DimNameProxy --- inst/include/Rcpp/vector/DimNameProxy.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/inst/include/Rcpp/vector/DimNameProxy.h b/inst/include/Rcpp/vector/DimNameProxy.h index 1debd7fb4..305c2fd98 100644 --- a/inst/include/Rcpp/vector/DimNameProxy.h +++ b/inst/include/Rcpp/vector/DimNameProxy.h @@ -56,6 +56,31 @@ namespace internal{ return *this; } + inline DimNameProxy& operator=(const DimNameProxy& other) { + SEXP dim = Rf_getAttrib(data_, R_DimSymbol); + if (INTEGER(dim)[dim_] != INTEGER(dim)[other.dim_]) { + std::stringstream s; + s << "dimension extent is '" + << INTEGER(dim)[dim_] + << "' while length of names is '" + << INTEGER(dim)[other.dim_] + << "'"; + stop(s.str()); + } + SEXP dimnames = Rf_getAttrib(data_, R_DimNamesSymbol); + SEXP dims = Rf_getAttrib(data_, R_DimSymbol); + SEXP other_dimnames = SEXP(other); + if (Rf_isNull(dimnames)) { + Shield new_dimnames(Rf_allocVector(VECSXP, Rf_length(dims))); + SET_VECTOR_ELT(new_dimnames, dim_, other_dimnames); + Rf_setAttrib(data_, R_DimNamesSymbol, new_dimnames); + } else { + SET_VECTOR_ELT(dimnames, dim_, other_dimnames); + } + return *this; + } + + inline operator SEXP() const { SEXP dimnames = Rf_getAttrib(data_, R_DimNamesSymbol); return Rf_isNull(dimnames) ? (R_NilValue) : (VECTOR_ELT(dimnames, dim_)); From 268a5c5ba11c7f15dd9104a82ee6aaa0bc050707 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Plaza=20O=C3=B1ate?= Date: Fri, 14 Aug 2015 16:20:58 +0200 Subject: [PATCH 2/6] Remove duplicated code --- inst/include/Rcpp/vector/DimNameProxy.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/inst/include/Rcpp/vector/DimNameProxy.h b/inst/include/Rcpp/vector/DimNameProxy.h index 305c2fd98..75d4d9235 100644 --- a/inst/include/Rcpp/vector/DimNameProxy.h +++ b/inst/include/Rcpp/vector/DimNameProxy.h @@ -34,18 +34,17 @@ namespace internal{ data_(other.data_), dim_(other.dim_) {} inline DimNameProxy& operator=(SEXP other) { - SEXP dim = Rf_getAttrib(data_, R_DimSymbol); - if (INTEGER(dim)[dim_] != Rf_length(other)) { + SEXP dims = Rf_getAttrib(data_, R_DimSymbol); + if (INTEGER(dims)[dim_] != Rf_length(other)) { std::stringstream s; s << "dimension extent is '" - << INTEGER(dim)[dim_] + << INTEGER(dims)[dim_] << "' while length of names is '" << Rf_length(other) << "'"; stop(s.str()); } SEXP dimnames = Rf_getAttrib(data_, R_DimNamesSymbol); - SEXP dims = Rf_getAttrib(data_, R_DimSymbol); if (Rf_isNull(dimnames)) { Shield new_dimnames(Rf_allocVector(VECSXP, Rf_length(dims))); SET_VECTOR_ELT(new_dimnames, dim_, other); @@ -55,20 +54,20 @@ namespace internal{ } return *this; } + - inline DimNameProxy& operator=(const DimNameProxy& other) { - SEXP dim = Rf_getAttrib(data_, R_DimSymbol); - if (INTEGER(dim)[dim_] != INTEGER(dim)[other.dim_]) { + inline DimNameProxy& operator=(const DimNameProxy& other) { + SEXP dims = Rf_getAttrib(data_, R_DimSymbol); + if (INTEGER(dims)[dim_] != INTEGER(dims)[other.dim_]) { std::stringstream s; s << "dimension extent is '" - << INTEGER(dim)[dim_] + << INTEGER(dims)[dim_] << "' while length of names is '" - << INTEGER(dim)[other.dim_] + << INTEGER(dims)[other.dim_] << "'"; stop(s.str()); } SEXP dimnames = Rf_getAttrib(data_, R_DimNamesSymbol); - SEXP dims = Rf_getAttrib(data_, R_DimSymbol); SEXP other_dimnames = SEXP(other); if (Rf_isNull(dimnames)) { Shield new_dimnames(Rf_allocVector(VECSXP, Rf_length(dims))); @@ -79,6 +78,7 @@ namespace internal{ } return *this; } + inline operator SEXP() const { From 271e03dbc37de42700f5739ceac23192cdd8e7b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Plaza=20O=C3=B1ate?= Date: Fri, 14 Aug 2015 16:35:31 +0200 Subject: [PATCH 3/6] Remove std::stringstream usage --- inst/include/Rcpp/vector/DimNameProxy.h | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/inst/include/Rcpp/vector/DimNameProxy.h b/inst/include/Rcpp/vector/DimNameProxy.h index 75d4d9235..d0964e07f 100644 --- a/inst/include/Rcpp/vector/DimNameProxy.h +++ b/inst/include/Rcpp/vector/DimNameProxy.h @@ -36,13 +36,7 @@ namespace internal{ inline DimNameProxy& operator=(SEXP other) { SEXP dims = Rf_getAttrib(data_, R_DimSymbol); if (INTEGER(dims)[dim_] != Rf_length(other)) { - std::stringstream s; - s << "dimension extent is '" - << INTEGER(dims)[dim_] - << "' while length of names is '" - << Rf_length(other) - << "'"; - stop(s.str()); + stop("dimension extent is '%d' while length of names is '%d'", INTEGER(dims)[dim_], Rf_length(other)); } SEXP dimnames = Rf_getAttrib(data_, R_DimNamesSymbol); if (Rf_isNull(dimnames)) { @@ -55,17 +49,10 @@ namespace internal{ return *this; } - - inline DimNameProxy& operator=(const DimNameProxy& other) { + inline DimNameProxy& operator=(const DimNameProxy& other) { SEXP dims = Rf_getAttrib(data_, R_DimSymbol); if (INTEGER(dims)[dim_] != INTEGER(dims)[other.dim_]) { - std::stringstream s; - s << "dimension extent is '" - << INTEGER(dims)[dim_] - << "' while length of names is '" - << INTEGER(dims)[other.dim_] - << "'"; - stop(s.str()); + stop("dimension extent is '%d' while length of names is '%d'", INTEGER(dims)[dim_], INTEGER(dims)[other.dim_]); } SEXP dimnames = Rf_getAttrib(data_, R_DimNamesSymbol); SEXP other_dimnames = SEXP(other); From 3b54aa1a079bd530f58641e1213f2f4a02a457c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Plaza=20O=C3=B1ate?= Date: Fri, 14 Aug 2015 16:54:45 +0200 Subject: [PATCH 4/6] Factorize code --- inst/include/Rcpp/vector/DimNameProxy.h | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/inst/include/Rcpp/vector/DimNameProxy.h b/inst/include/Rcpp/vector/DimNameProxy.h index d0964e07f..4cc24e5c5 100644 --- a/inst/include/Rcpp/vector/DimNameProxy.h +++ b/inst/include/Rcpp/vector/DimNameProxy.h @@ -32,8 +32,8 @@ namespace internal{ DimNameProxy(SEXP data, int dim): data_(data), dim_(dim) {} DimNameProxy(DimNameProxy const& other): data_(other.data_), dim_(other.dim_) {} - - inline DimNameProxy& operator=(SEXP other) { + + inline DimNameProxy& assign(SEXP other) { SEXP dims = Rf_getAttrib(data_, R_DimSymbol); if (INTEGER(dims)[dim_] != Rf_length(other)) { stop("dimension extent is '%d' while length of names is '%d'", INTEGER(dims)[dim_], Rf_length(other)); @@ -47,27 +47,16 @@ namespace internal{ SET_VECTOR_ELT(dimnames, dim_, other); } return *this; + } + + inline DimNameProxy& operator=(SEXP other) { + return assign(other); } inline DimNameProxy& operator=(const DimNameProxy& other) { - SEXP dims = Rf_getAttrib(data_, R_DimSymbol); - if (INTEGER(dims)[dim_] != INTEGER(dims)[other.dim_]) { - stop("dimension extent is '%d' while length of names is '%d'", INTEGER(dims)[dim_], INTEGER(dims)[other.dim_]); - } - SEXP dimnames = Rf_getAttrib(data_, R_DimNamesSymbol); - SEXP other_dimnames = SEXP(other); - if (Rf_isNull(dimnames)) { - Shield new_dimnames(Rf_allocVector(VECSXP, Rf_length(dims))); - SET_VECTOR_ELT(new_dimnames, dim_, other_dimnames); - Rf_setAttrib(data_, R_DimNamesSymbol, new_dimnames); - } else { - SET_VECTOR_ELT(dimnames, dim_, other_dimnames); - } - return *this; + return assign(SEXP(other)); } - - inline operator SEXP() const { SEXP dimnames = Rf_getAttrib(data_, R_DimNamesSymbol); return Rf_isNull(dimnames) ? (R_NilValue) : (VECTOR_ELT(dimnames, dim_)); From 8b993cfc38051f42544faa91c876430cac6bf2d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Plaza=20O=C3=B1ate?= Date: Fri, 14 Aug 2015 19:45:54 +0200 Subject: [PATCH 5/6] Allow to assign empty vector or NULL to dimnames --- inst/include/Rcpp/vector/DimNameProxy.h | 31 +++++++++++++++---------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/inst/include/Rcpp/vector/DimNameProxy.h b/inst/include/Rcpp/vector/DimNameProxy.h index 4cc24e5c5..9c543c94a 100644 --- a/inst/include/Rcpp/vector/DimNameProxy.h +++ b/inst/include/Rcpp/vector/DimNameProxy.h @@ -34,18 +34,25 @@ namespace internal{ data_(other.data_), dim_(other.dim_) {} inline DimNameProxy& assign(SEXP other) { - SEXP dims = Rf_getAttrib(data_, R_DimSymbol); - if (INTEGER(dims)[dim_] != Rf_length(other)) { - stop("dimension extent is '%d' while length of names is '%d'", INTEGER(dims)[dim_], Rf_length(other)); - } - SEXP dimnames = Rf_getAttrib(data_, R_DimNamesSymbol); - if (Rf_isNull(dimnames)) { - Shield new_dimnames(Rf_allocVector(VECSXP, Rf_length(dims))); - SET_VECTOR_ELT(new_dimnames, dim_, other); - Rf_setAttrib(data_, R_DimNamesSymbol, new_dimnames); - } else { - SET_VECTOR_ELT(dimnames, dim_, other); - } + if (Rf_length(other) == 0) + { + Rf_setAttrib(data_, R_DimNamesSymbol, R_NilValue); + } else { + SEXP dims = Rf_getAttrib(data_, R_DimSymbol); + + if (INTEGER(dims)[dim_] != Rf_length(other)) { + stop("dimension extent is '%d' while length of names is '%d'", INTEGER(dims)[dim_], Rf_length(other)); + } + + SEXP dimnames = Rf_getAttrib(data_, R_DimNamesSymbol); + if (Rf_isNull(dimnames)) { + Shield new_dimnames(Rf_allocVector(VECSXP, Rf_length(dims))); + SET_VECTOR_ELT(new_dimnames, dim_, other); + Rf_setAttrib(data_, R_DimNamesSymbol, new_dimnames); + } else { + SET_VECTOR_ELT(dimnames, dim_, other); + } + } return *this; } From 73974dbca470466e7b0e099ced6c85e6c748199d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Plaza=20O=C3=B1ate?= Date: Fri, 14 Aug 2015 19:50:39 +0200 Subject: [PATCH 6/6] Fix wrong indent --- inst/include/Rcpp/vector/DimNameProxy.h | 41 ++++++++++++------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/inst/include/Rcpp/vector/DimNameProxy.h b/inst/include/Rcpp/vector/DimNameProxy.h index 9c543c94a..4735568ed 100644 --- a/inst/include/Rcpp/vector/DimNameProxy.h +++ b/inst/include/Rcpp/vector/DimNameProxy.h @@ -32,34 +32,33 @@ namespace internal{ DimNameProxy(SEXP data, int dim): data_(data), dim_(dim) {} DimNameProxy(DimNameProxy const& other): data_(other.data_), dim_(other.dim_) {} - + inline DimNameProxy& assign(SEXP other) { - if (Rf_length(other) == 0) - { - Rf_setAttrib(data_, R_DimNamesSymbol, R_NilValue); - } else { - SEXP dims = Rf_getAttrib(data_, R_DimSymbol); - - if (INTEGER(dims)[dim_] != Rf_length(other)) { - stop("dimension extent is '%d' while length of names is '%d'", INTEGER(dims)[dim_], Rf_length(other)); - } + if (Rf_length(other) == 0) + { + Rf_setAttrib(data_, R_DimNamesSymbol, R_NilValue); + } else { + SEXP dims = Rf_getAttrib(data_, R_DimSymbol); + if (INTEGER(dims)[dim_] != Rf_length(other)) { + stop("dimension extent is '%d' while length of names is '%d'", INTEGER(dims)[dim_], Rf_length(other)); + } - SEXP dimnames = Rf_getAttrib(data_, R_DimNamesSymbol); - if (Rf_isNull(dimnames)) { - Shield new_dimnames(Rf_allocVector(VECSXP, Rf_length(dims))); - SET_VECTOR_ELT(new_dimnames, dim_, other); - Rf_setAttrib(data_, R_DimNamesSymbol, new_dimnames); - } else { - SET_VECTOR_ELT(dimnames, dim_, other); - } - } + SEXP dimnames = Rf_getAttrib(data_, R_DimNamesSymbol); + if (Rf_isNull(dimnames)) { + Shield new_dimnames(Rf_allocVector(VECSXP, Rf_length(dims))); + SET_VECTOR_ELT(new_dimnames, dim_, other); + Rf_setAttrib(data_, R_DimNamesSymbol, new_dimnames); + } else { + SET_VECTOR_ELT(dimnames, dim_, other); + } + } return *this; - } + } inline DimNameProxy& operator=(SEXP other) { return assign(other); } - + inline DimNameProxy& operator=(const DimNameProxy& other) { return assign(SEXP(other)); }