Skip to content

Commit

Permalink
removing using namespace (tidyverse#4293)
Browse files Browse the repository at this point in the history
* Protect call

* remove using namespace Rcpp in main.h, and fix consequences

* Move check_not_groups() to a method of the *DataFrame classes

* Shield on R < 3.5

* rm `using namespace` directives

* remove using namespace dplyr from header file, and adapt

* remove using namespace Rcpp

* - using namespace
  • Loading branch information
romainfrancois authored Mar 19, 2019
1 parent 1bb7454 commit e08f051
Show file tree
Hide file tree
Showing 55 changed files with 1,191 additions and 1,193 deletions.
83 changes: 42 additions & 41 deletions inst/include/dplyr/Collecter.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

#include <tools/all_na.h>
#include <tools/hash.h>

#include <tools/vector_class.h>
#include <tools/collapse.h>
#include <tools/SlicingIndex.h>
#include <tools/utils.h>

#include <dplyr/symbols.h>

Expand Down Expand Up @@ -107,7 +108,7 @@ class Collecter_Impl : public Collecter {
}

protected:
Vector<RTYPE> data;
Rcpp::Vector<RTYPE> data;

private:
void collect_logicalNA(const SlicingIndex& index) {
Expand All @@ -118,7 +119,7 @@ class Collecter_Impl : public Collecter {

void collect_sexp(const SlicingIndex& index, SEXP v, int offset = 0) {
warn_loss_attr(v);
Vector<RTYPE> source(v);
Rcpp::Vector<RTYPE> source(v);
STORAGE* source_ptr = Rcpp::internal::r_vector_start<RTYPE>(source);
source_ptr = source_ptr + offset;
for (int i = 0; i < index.size(); i++) {
Expand All @@ -135,7 +136,7 @@ class Collecter_Impl<REALSXP> : public Collecter {

void collect(const SlicingIndex& index, SEXP v, int offset = 0) {
warn_loss_attr(v);
NumericVector source(v);
Rcpp::NumericVector source(v);
double* source_ptr = source.begin() + offset;
for (int i = 0; i < index.size(); i++) {
data[index[i]] = source_ptr[i];
Expand All @@ -162,7 +163,7 @@ class Collecter_Impl<REALSXP> : public Collecter {
}

protected:
NumericVector data;
Rcpp::NumericVector data;

};

Expand All @@ -180,7 +181,7 @@ class Collecter_Impl<STRSXP> : public Collecter {
} else if (all_logical_na(v, TYPEOF(v))) {
collect_logicalNA(index, v);
} else {
CharacterVector vec(v);
Rcpp::CharacterVector vec(v);
collect_strings(index, vec, offset);
}
}
Expand All @@ -202,18 +203,18 @@ class Collecter_Impl<STRSXP> : public Collecter {
}

protected:
CharacterVector data;
Rcpp::CharacterVector data;

private:

void collect_logicalNA(const SlicingIndex& index, LogicalVector) {
void collect_logicalNA(const SlicingIndex& index, Rcpp::LogicalVector) {
int n = index.size();
for (int i = 0; i < n; i++) {
SET_STRING_ELT(data, index[i], NA_STRING);
}
}

void collect_strings(const SlicingIndex& index, CharacterVector source,
void collect_strings(const SlicingIndex& index, Rcpp::CharacterVector source,
int offset = 0) {
SEXP* p_source = Rcpp::internal::r_vector_start<STRSXP>(source) + offset;
int n = index.size();
Expand All @@ -222,9 +223,9 @@ class Collecter_Impl<STRSXP> : public Collecter {
}
}

void collect_factor(const SlicingIndex& index, IntegerVector source,
void collect_factor(const SlicingIndex& index, Rcpp::IntegerVector source,
int offset = 0) {
CharacterVector levels = get_levels(source);
Rcpp::CharacterVector levels = get_levels(source);
Rf_warning("binding character and factor vector, coercing into character vector");
for (int i = 0; i < index.size(); i++) {
if (source[i] == NA_INTEGER) {
Expand All @@ -244,7 +245,7 @@ class Collecter_Impl<INTSXP> : public Collecter {

void collect(const SlicingIndex& index, SEXP v, int offset = 0) {
warn_loss_attr(v);
IntegerVector source(v);
Rcpp::IntegerVector source(v);
int* source_ptr = source.begin() + offset;
for (int i = 0; i < index.size(); i++) {
data[index[i]] = source_ptr[i];
Expand All @@ -269,7 +270,7 @@ class Collecter_Impl<INTSXP> : public Collecter {
}

protected:
IntegerVector data;
Rcpp::IntegerVector data;

};

Expand All @@ -280,7 +281,7 @@ class Collecter_Impl<RAWSXP> : public Collecter {

void collect(const SlicingIndex& index, SEXP v, int offset = 0) {
warn_loss_attr(v);
RawVector source(v);
Rcpp::RawVector source(v);
Rbyte* source_ptr = source.begin() + offset;
for (int i = 0; i < index.size(); i++) {
data[index[i]] = source_ptr[i];
Expand All @@ -307,7 +308,7 @@ class Collecter_Impl<RAWSXP> : public Collecter {
}

protected:
RawVector data;
Rcpp::RawVector data;

};

Expand All @@ -320,13 +321,13 @@ class TypedCollecter : public Collecter_Impl<RTYPE> {
Collecter_Impl<RTYPE>(n), types(types_) {}

inline SEXP get() {
Vector<RTYPE> data = Collecter_Impl<RTYPE>::data;
Rcpp::Vector<RTYPE> data = Collecter_Impl<RTYPE>::data;
set_class(data, types);
return data;
}

inline bool compatible(SEXP x) {
String type = STRING_ELT(types, 0);
Rcpp::String type = STRING_ELT(types, 0);
return Rf_inherits(x, type.get_cstring()) || all_logical_na(x, TYPEOF(x));
}

Expand Down Expand Up @@ -380,7 +381,7 @@ class POSIXctCollecter : public Collecter_Impl<REALSXP> {

private:
void update_tz(SEXP v) {
RObject v_tz(Rf_getAttrib(v, symbols::tzone));
Rcpp::RObject v_tz(Rf_getAttrib(v, symbols::tzone));
// if the new tz is NULL, keep previous value
if (v_tz.isNULL()) return;

Expand All @@ -397,7 +398,7 @@ class POSIXctCollecter : public Collecter_Impl<REALSXP> {
}
}

RObject tz;
Rcpp::RObject tz;
};

class DifftimeCollecter : public Collecter_Impl<REALSXP> {
Expand All @@ -417,7 +418,7 @@ class DifftimeCollecter : public Collecter_Impl<REALSXP> {

inline SEXP get() {
Rf_classgets(Parent::data, types);
Rf_setAttrib(Parent::data, symbols::units, Shield<SEXP>(Rf_mkString(units.c_str())));
Rf_setAttrib(Parent::data, symbols::units, Rcpp::Shield<SEXP>(Rf_mkString(units.c_str())));
return Parent::data;
}

Expand All @@ -434,12 +435,12 @@ class DifftimeCollecter : public Collecter_Impl<REALSXP> {
}

private:
bool is_valid_difftime(RObject x) {
bool is_valid_difftime(Rcpp::RObject x) {
if (!Rf_inherits(x, "difftime") || TYPEOF(x) != REALSXP) {
return false;
}

Shield<SEXP> units(Rf_getAttrib(x, symbols::units));
Rcpp::Shield<SEXP> units(Rf_getAttrib(x, symbols::units));
if (TYPEOF(units) != STRSXP) {
return false;
}
Expand All @@ -450,11 +451,11 @@ class DifftimeCollecter : public Collecter_Impl<REALSXP> {
}


void collect_difftime(const SlicingIndex& index, RObject v, int offset = 0) {
void collect_difftime(const SlicingIndex& index, Rcpp::RObject v, int offset = 0) {
if (!is_valid_difftime(v)) {
stop("Invalid difftime object");
Rcpp::stop("Invalid difftime object");
}
Shield<SEXP> units_attr(Rf_getAttrib(v, symbols::units));
Rcpp::Shield<SEXP> units_attr(Rf_getAttrib(v, symbols::units));
std::string v_units = Rcpp::as<std::string>(units_attr);
if (!get_units_map().is_valid_difftime_unit(units)) {
// if current unit is NULL, grab the new one
Expand All @@ -479,7 +480,7 @@ class DifftimeCollecter : public Collecter_Impl<REALSXP> {
units = "secs";
double factor_v = get_units_map().time_conversion_factor(v_units);
if (Rf_length(v) < index.size()) {
stop("Wrong size of vector to collect");
Rcpp::stop("Wrong size of vector to collect");
}
for (int i = 0; i < index.size(); i++) {
Parent::data[index[i]] = factor_v * (REAL(v)[i + offset]);
Expand Down Expand Up @@ -519,7 +520,7 @@ class DifftimeCollecter : public Collecter_Impl<REALSXP> {
double time_conversion_factor(const std::string& v_units) const {
units_map::const_iterator it = valid_units.find(v_units);
if (it == valid_units.end()) {
stop("Invalid difftime units (%s).", v_units.c_str());
Rcpp::stop("Invalid difftime units (%s).", v_units.c_str());
}

return it->second;
Expand All @@ -543,7 +544,7 @@ class FactorCollecter : public Collecter {
typedef dplyr_hash_map<SEXP, int> LevelsMap;

FactorCollecter(int n, SEXP model_):
data(n, IntegerVector::get_na()),
data(n, Rcpp::IntegerVector::get_na()),
model(model_),
levels(get_levels(model_)),
levels_map()
Expand All @@ -557,7 +558,7 @@ class FactorCollecter : public Collecter {
}

void collect(const SlicingIndex& index, SEXP v, int offset = 0) {
if (offset != 0) stop("Nonzero offset ot supported by FactorCollecter");
if (offset != 0) Rcpp::stop("Nonzero offset ot supported by FactorCollecter");
if (Rf_inherits(v, "factor") && has_same_levels_as(v)) {
collect_factor(index, v);
} else if (all_logical_na(v, TYPEOF(v))) {
Expand All @@ -581,7 +582,7 @@ class FactorCollecter : public Collecter {
}

inline bool has_same_levels_as(SEXP x) const {
CharacterVector levels_other = get_levels(x);
Rcpp::CharacterVector levels_other = get_levels(x);

int nlevels = levels_other.size();
if (nlevels != (int)levels_map.size()) return false;
Expand All @@ -597,16 +598,16 @@ class FactorCollecter : public Collecter {
}

private:
IntegerVector data;
RObject model;
CharacterVector levels;
Rcpp::IntegerVector data;
Rcpp::RObject model;
Rcpp::CharacterVector levels;
LevelsMap levels_map;

void collect_factor(const SlicingIndex& index, SEXP v) {
// here we can assume that v is a factor with the right levels
// we however do not assume that they are in the same order
IntegerVector source(v);
CharacterVector levels = get_levels(source);
Rcpp::IntegerVector source(v);
Rcpp::CharacterVector levels = get_levels(source);
SEXP* levels_ptr = Rcpp::internal::r_vector_start<STRSXP>(levels);
int* source_ptr = Rcpp::internal::r_vector_start<INTSXP>(source);
for (int i = 0; i < index.size(); i++) {
Expand Down Expand Up @@ -653,7 +654,7 @@ inline Collecter* collecter(SEXP model, int n) {
if (Rf_inherits(model, "Date"))
return new TypedCollecter<REALSXP>(n, get_date_classes());
if (Rf_inherits(model, "integer64"))
return new TypedCollecter<REALSXP>(n, CharacterVector::create("integer64"));
return new TypedCollecter<REALSXP>(n, Rcpp::CharacterVector::create("integer64"));
return new Collecter_Impl<REALSXP>(n);
case CPLXSXP:
return new Collecter_Impl<CPLXSXP>(n);
Expand All @@ -663,10 +664,10 @@ inline Collecter* collecter(SEXP model, int n) {
return new Collecter_Impl<STRSXP>(n);
case VECSXP:
if (Rf_inherits(model, "POSIXlt")) {
stop("POSIXlt not supported");
Rcpp::stop("POSIXlt not supported");
}
if (Rf_inherits(model, "data.frame")) {
stop("Columns of class data.frame not supported");
Rcpp::stop("Columns of class data.frame not supported");
}
return new Collecter_Impl<VECSXP>(n);
case RAWSXP:
Expand All @@ -675,7 +676,7 @@ inline Collecter* collecter(SEXP model, int n) {
break;
}

stop("is of unsupported type %s", Rf_type2char(TYPEOF(model)));
Rcpp::stop("is of unsupported type %s", Rf_type2char(TYPEOF(model)));
}

inline Collecter* promote_collecter(SEXP model, int n, Collecter* previous) {
Expand Down Expand Up @@ -706,7 +707,7 @@ inline Collecter* promote_collecter(SEXP model, int n, Collecter* previous) {
if (Rf_inherits(model, "Date"))
return new TypedCollecter<REALSXP>(n, get_date_classes());
if (Rf_inherits(model, "integer64"))
return new TypedCollecter<REALSXP>(n, CharacterVector::create("integer64"));
return new TypedCollecter<REALSXP>(n, Rcpp::CharacterVector::create("integer64"));
return new Collecter_Impl<REALSXP>(n);
case LGLSXP:
return new Collecter_Impl<LGLSXP>(n);
Expand All @@ -717,7 +718,7 @@ inline Collecter* promote_collecter(SEXP model, int n, Collecter* previous) {
default:
break;
}
stop("is of unsupported type %s", Rf_type2char(TYPEOF(model)));
Rcpp::stop("is of unsupported type %s", Rf_type2char(TYPEOF(model)));
}

}
Expand Down
14 changes: 0 additions & 14 deletions inst/include/dplyr/Groups.h

This file was deleted.

12 changes: 6 additions & 6 deletions inst/include/dplyr/checks.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ inline std::string type_name(SEXP x) {
}
}

inline SupportedType check_supported_type(SEXP x, const SymbolString& name = String()) {
inline SupportedType check_supported_type(SEXP x, const SymbolString& name = Rcpp::String()) {
switch (TYPEOF(x)) {
case LGLSXP:
return DPLYR_LGLSXP;
Expand All @@ -77,19 +77,19 @@ inline SupportedType check_supported_type(SEXP x, const SymbolString& name = Str
Rcpp::stop("is of unsupported type %s", type_name(x));
} else {
bad_col(name, "is of unsupported type {type}",
_["type"] = type_name(x));
Rcpp::_["type"] = type_name(x));
}
}
}

inline void check_length(const int actual, const int expected, const char* comment, const SymbolString& name) {
if (actual == expected || actual == 1) return;

static Function check_length_col("check_length_col", Environment::namespace_env("dplyr"));
static Function identity("identity", Environment::base_env());
String message = check_length_col(actual, expected, CharacterVector::create(name.get_sexp()), std::string(comment), _[".abort"] = identity);
static Rcpp::Function check_length_col("check_length_col", Rcpp::Environment::namespace_env("dplyr"));
static Rcpp::Function identity("identity", Rcpp::Environment::base_env());
Rcpp::String message = check_length_col(actual, expected, Rcpp::CharacterVector::create(name.get_sexp()), std::string(comment), Rcpp::_[".abort"] = identity);
message.set_encoding(CE_UTF8);
stop(message.get_cstring());
Rcpp::stop(message.get_cstring());
}

inline void check_not_null(SEXP result, const SymbolString& name) {
Expand Down
Loading

0 comments on commit e08f051

Please sign in to comment.