/usr/lib/R/site-library/dplyr/include/dplyr/Result/DelayedProcessor.h is in r-cran-dplyr 0.7.4-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | #ifndef dplyr_Result_DelayedProcessor_H
#define dplyr_Result_DelayedProcessor_H
#include <tools/hash.h>
#include <tools/ShrinkableVector.h>
#include <tools/scalar_type.h>
#include <tools/utils.h>
#include <dplyr/vector_class.h>
#include <dplyr/checks.h>
namespace dplyr {
class IDelayedProcessor {
public:
IDelayedProcessor() {}
virtual ~IDelayedProcessor() {}
virtual bool try_handle(const RObject& chunk) = 0;
virtual IDelayedProcessor* promote(const RObject& chunk) = 0;
virtual SEXP get() = 0;
virtual std::string describe() = 0;
};
template <int RTYPE>
bool valid_conversion(int rtype) {
return rtype == RTYPE;
}
template <>
inline bool valid_conversion<REALSXP>(int rtype) {
switch (rtype) {
case REALSXP:
case INTSXP:
case LGLSXP:
return true;
default:
break;
}
return false;
}
template <>
inline bool valid_conversion<INTSXP>(int rtype) {
switch (rtype) {
case INTSXP:
case LGLSXP:
return true;
default:
break;
}
return false;
}
template <int RTYPE>
inline bool valid_promotion(int) {
return false;
}
template <>
inline bool valid_promotion<INTSXP>(int rtype) {
return rtype == REALSXP;
}
template <>
inline bool valid_promotion<LGLSXP>(int rtype) {
return rtype == REALSXP || rtype == INTSXP;
}
template <int RTYPE, typename CLASS>
class DelayedProcessor : public IDelayedProcessor {
public:
typedef typename traits::scalar_type<RTYPE>::type STORAGE;
typedef Vector<RTYPE> Vec;
DelayedProcessor(const RObject& first_result, int ngroups_, const SymbolString& name_) :
res(no_init(ngroups_)), pos(0), seen_na_only(true), name(name_)
{
if (!try_handle(first_result))
stop("cannot handle result of type %i for column '%s'", first_result.sexp_type(), name.get_utf8_cstring());
copy_most_attributes(res, first_result);
}
DelayedProcessor(int pos_, const RObject& chunk, SEXP res_, const SymbolString& name_) :
res(as<Vec>(res_)), pos(pos_), seen_na_only(false), name(name_)
{
copy_most_attributes(res, chunk);
if (!try_handle(chunk)) {
stop("cannot handle result of type %i in promotion for column '%s'",
chunk.sexp_type(), name.get_utf8_cstring()
);
}
}
virtual bool try_handle(const RObject& chunk) {
check_supported_type(chunk, name);
check_length(Rf_length(chunk), 1, "a summary value", name);
int rtype = TYPEOF(chunk);
if (valid_conversion<RTYPE>(rtype)) {
// copy, and memoize the copied value
const typename Vec::stored_type& converted_chunk = (res[pos++] = as<STORAGE>(chunk));
if (!Vec::is_na(converted_chunk))
seen_na_only = false;
return true;
} else {
return false;
}
}
virtual IDelayedProcessor* promote(const RObject& chunk) {
if (!can_promote(chunk)) {
LOG_VERBOSE << "can't promote";
return 0;
}
int rtype = TYPEOF(chunk);
switch (rtype) {
case LGLSXP:
return new DelayedProcessor<LGLSXP, CLASS>(pos, chunk, res, name);
case INTSXP:
return new DelayedProcessor<INTSXP, CLASS>(pos, chunk, res, name);
case REALSXP:
return new DelayedProcessor<REALSXP, CLASS>(pos, chunk, res, name);
case CPLXSXP:
return new DelayedProcessor<CPLXSXP, CLASS>(pos, chunk, res, name);
case STRSXP:
return new DelayedProcessor<STRSXP, CLASS>(pos, chunk, res, name);
default:
break;
}
return 0;
}
virtual SEXP get() {
return res;
}
virtual std::string describe() {
return vector_class<RTYPE>();
}
private:
bool can_promote(const RObject& chunk) {
return seen_na_only || valid_promotion<RTYPE>(TYPEOF(chunk));
}
private:
Vec res;
int pos;
bool seen_na_only;
const SymbolString name;
};
template <typename CLASS>
class FactorDelayedProcessor : public IDelayedProcessor {
private:
typedef dplyr_hash_map<SEXP, int> LevelsMap;
public:
FactorDelayedProcessor(SEXP first_result, int ngroups, const SymbolString& name_) :
res(no_init(ngroups)), pos(0), name(name_)
{
copy_most_attributes(res, first_result);
CharacterVector levels = get_levels(first_result);
int n = levels.size();
for (int i = 0; i < n; i++) levels_map[ levels[i] ] = i + 1;
if (!try_handle(first_result))
stop("cannot handle factor result for column '%s'", name.get_utf8_cstring());
}
virtual bool try_handle(const RObject& chunk) {
CharacterVector lev = get_levels(chunk);
update_levels(lev);
int val = as<int>(chunk);
if (val != NA_INTEGER) val = levels_map[lev[val - 1]];
res[pos++] = val;
return true;
}
virtual IDelayedProcessor* promote(const RObject&) {
return 0;
}
virtual SEXP get() {
int n = levels_map.size();
CharacterVector levels(n);
LevelsMap::iterator it = levels_map.begin();
for (int i = 0; i < n; i++, ++it) {
levels[it->second - 1] = it->first;
}
set_levels(res, levels);
return res;
}
virtual std::string describe() {
return "factor";
}
private:
void update_levels(const CharacterVector& lev) {
int nlevels = levels_map.size();
int n = lev.size();
for (int i = 0; i < n; i++) {
SEXP s = lev[i];
if (! levels_map.count(s)) {
levels_map.insert(std::make_pair(s, ++nlevels));
}
}
}
IntegerVector res;
int pos;
LevelsMap levels_map;
const SymbolString name;
};
template <typename CLASS>
class DelayedProcessor<VECSXP, CLASS> : public IDelayedProcessor {
public:
DelayedProcessor(SEXP first_result, int ngroups, const SymbolString& name_) :
res(ngroups), pos(0), name(name_)
{
copy_most_attributes(res, first_result);
if (!try_handle(first_result))
stop("cannot handle list result for column '%s'", name.get_utf8_cstring());
}
virtual bool try_handle(const RObject& chunk) {
if (is<List>(chunk) && Rf_length(chunk) == 1) {
res[pos++] = Rf_duplicate(VECTOR_ELT(chunk, 0));
return true;
}
return false;
}
virtual IDelayedProcessor* promote(const RObject&) {
return 0;
}
virtual SEXP get() {
return res;
}
virtual std::string describe() {
return "list";
}
private:
List res;
int pos;
const SymbolString name;
};
template <typename CLASS>
IDelayedProcessor* get_delayed_processor(SEXP first_result, int ngroups, const SymbolString& name) {
check_supported_type(first_result, name);
check_length(Rf_length(first_result), 1, "a summary value", name);
if (Rf_inherits(first_result, "factor")) {
return new FactorDelayedProcessor<CLASS>(first_result, ngroups, name);
} else if (Rcpp::is<int>(first_result)) {
return new DelayedProcessor<INTSXP, CLASS>(first_result, ngroups, name);
} else if (Rcpp::is<double>(first_result)) {
return new DelayedProcessor<REALSXP, CLASS>(first_result, ngroups, name);
} else if (Rcpp::is<Rcpp::String>(first_result)) {
return new DelayedProcessor<STRSXP, CLASS>(first_result, ngroups, name);
} else if (Rcpp::is<bool>(first_result)) {
return new DelayedProcessor<LGLSXP, CLASS>(first_result, ngroups, name);
} else if (Rcpp::is<Rcpp::List>(first_result)) {
return new DelayedProcessor<VECSXP, CLASS>(first_result, ngroups, name);
} else if (TYPEOF(first_result) == CPLXSXP) {
return new DelayedProcessor<CPLXSXP, CLASS>(first_result, ngroups, name);
}
stop("unknown result of type %d for column '%s'", TYPEOF(first_result), name.get_utf8_cstring());
}
}
#endif
|