Skip to content

Commit

Permalink
Merge pull request #606 from SebKrantz/development
Browse files Browse the repository at this point in the history
Replace STRING_PTR.
  • Loading branch information
SebKrantz authored Jun 23, 2024
2 parents 7e19079 + 537ad7e commit f912549
Show file tree
Hide file tree
Showing 20 changed files with 125 additions and 122 deletions.
38 changes: 19 additions & 19 deletions src/TRA.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ SEXP ret1(SEXP x, SEXP xAG, SEXP g, int set) {
}
case STRSXP:
{
SEXP *pout = STRING_PTR(out);
SEXP *pout = SEXPPTR(out);
if(nog) {
SEXP AG = asChar(xAG);
#pragma omp simd
for(int i = 0; i < l; ++i) pout[i] = AG;
} else {
SEXP *AG = STRING_PTR(xAG)-1;
SEXP *AG = SEXPPTR(xAG)-1;
#pragma omp simd
for(int i = 0; i < l; ++i) pout[i] = AG[pg[i]];
}
Expand Down Expand Up @@ -237,13 +237,13 @@ SEXP ret2(SEXP x, SEXP xAG, SEXP g, int set) {
break;
}
case STRSXP: {
SEXP *pout = STRING_PTR(out);
SEXP *pout = SEXPPTR(out);
if(nog) {
SEXP AG = asChar(xAG);
#pragma omp simd
for(int i = 0; i < l; ++i) pout[i] = ISNAN(px[i]) ? NA_STRING : AG;
} else {
SEXP *AG = STRING_PTR(xAG)-1;
SEXP *AG = SEXPPTR(xAG)-1;
#pragma omp simd
for(int i = 0; i < l; ++i) pout[i] = ISNAN(px[i]) ? NA_STRING : AG[pg[i]];
}
Expand Down Expand Up @@ -287,13 +287,13 @@ SEXP ret2(SEXP x, SEXP xAG, SEXP g, int set) {
break;
}
case STRSXP: {
SEXP *pout = STRING_PTR(out);
SEXP *pout = SEXPPTR(out);
if(nog) {
SEXP AG = asChar(xAG);
#pragma omp simd
for(int i = 0; i < l; ++i) pout[i] = (px[i] == NA_INTEGER) ? NA_STRING : AG;
} else {
SEXP *AG = STRING_PTR(xAG)-1;
SEXP *AG = SEXPPTR(xAG)-1;
#pragma omp simd
for(int i = 0; i < l; ++i) pout[i] = (px[i] == NA_INTEGER) ? NA_STRING : AG[pg[i]];
}
Expand All @@ -306,7 +306,7 @@ SEXP ret2(SEXP x, SEXP xAG, SEXP g, int set) {
}
case STRSXP:
{
SEXP *px = STRING_PTR(x);
SEXP *px = SEXPPTR(x);
switch(txAG) {
case REALSXP: {
double *pout = REAL(out);
Expand Down Expand Up @@ -336,13 +336,13 @@ SEXP ret2(SEXP x, SEXP xAG, SEXP g, int set) {
break;
}
case STRSXP: {
SEXP *pout = STRING_PTR(out);
SEXP *pout = SEXPPTR(out);
if(nog) {
SEXP AG = asChar(xAG);
#pragma omp simd
for(int i = 0; i < l; ++i) pout[i] = (px[i] == NA_STRING) ? NA_STRING : AG;
} else {
SEXP *AG = STRING_PTR(xAG)-1;
SEXP *AG = SEXPPTR(xAG)-1;
#pragma omp simd
for(int i = 0; i < l; ++i) pout[i] = (px[i] == NA_STRING) ? NA_STRING : AG[pg[i]];
}
Expand Down Expand Up @@ -448,7 +448,7 @@ SEXP ret0(SEXP x, SEXP xAG, SEXP g, int set) {
}
case STRSXP:
{
SEXP *px = STRING_PTR(x), *pout = STRING_PTR(out);
SEXP *px = SEXPPTR(x), *pout = SEXPPTR(out);
if(nog) {
SEXP AG = asChar(xAG);
#pragma omp simd
Expand All @@ -459,7 +459,7 @@ SEXP ret0(SEXP x, SEXP xAG, SEXP g, int set) {
case LGLSXP:
case INTSXP: error("Cannot replace missing values in string with numeric data");
case STRSXP: {
SEXP *AG = STRING_PTR(xAG)-1;
SEXP *AG = SEXPPTR(xAG)-1;
#pragma omp simd
for(int i = 0; i < l; ++i) pout[i] = (px[i] == NA_STRING) ? AG[pg[i]] : px[i];
break;
Expand Down Expand Up @@ -728,7 +728,7 @@ SEXP TRAlC(SEXP x, SEXP xAG, SEXP g, SEXP Rret, SEXP Rset) {
break;
}
case STRSXP: {
SEXP *pAG = STRING_PTR(xAG);
SEXP *pAG = SEXPPTR(xAG);
RETLOOPS(ScalarString(pAG[j]))
break;
}
Expand Down Expand Up @@ -814,7 +814,7 @@ SEXP TRAmC(SEXP x, SEXP xAG, SEXP g, SEXP Rret, SEXP Rset) {
}
case STRSXP:
{
SEXP *pout = STRING_PTR(out), *pAG = STRING_PTR(xAG);
SEXP *pout = SEXPPTR(out), *pAG = SEXPPTR(xAG);
if(nog) {
for(int j = 0; j != col; ++j) {
int s = j * row, e = s + row;
Expand Down Expand Up @@ -883,7 +883,7 @@ SEXP TRAmC(SEXP x, SEXP xAG, SEXP g, SEXP Rret, SEXP Rset) {
}
case STRSXP:
{
SEXP *pout = STRING_PTR(out), *pAG = STRING_PTR(xAG);
SEXP *pout = SEXPPTR(out), *pAG = SEXPPTR(xAG);
if(nog) {
for(int j = 0; j != col; ++j) {
int s = j * row, e = s + row;
Expand Down Expand Up @@ -951,7 +951,7 @@ SEXP TRAmC(SEXP x, SEXP xAG, SEXP g, SEXP Rret, SEXP Rset) {
}
case STRSXP:
{
SEXP *pout = STRING_PTR(out), *pAG = STRING_PTR(xAG);
SEXP *pout = SEXPPTR(out), *pAG = SEXPPTR(xAG);
if(nog) {
for(int j = 0; j != col; ++j) {
int s = j * row, e = s + row;
Expand All @@ -975,7 +975,7 @@ SEXP TRAmC(SEXP x, SEXP xAG, SEXP g, SEXP Rret, SEXP Rset) {
}
case STRSXP:
{
SEXP *px = STRING_PTR(x);
SEXP *px = SEXPPTR(x);
switch(txAG) {
case REALSXP:
{
Expand Down Expand Up @@ -1018,7 +1018,7 @@ SEXP TRAmC(SEXP x, SEXP xAG, SEXP g, SEXP Rret, SEXP Rset) {
}
case STRSXP:
{
SEXP *pout = STRING_PTR(out), *pAG = STRING_PTR(xAG);
SEXP *pout = SEXPPTR(out), *pAG = SEXPPTR(xAG);
if(nog) {
for(int j = 0; j != col; ++j) {
int s = j * row, e = s + row;
Expand Down Expand Up @@ -1165,14 +1165,14 @@ SEXP TRAmC(SEXP x, SEXP xAG, SEXP g, SEXP Rret, SEXP Rset) {
}
case STRSXP:
{
SEXP *px = STRING_PTR(x), *pout = STRING_PTR(out);
SEXP *px = SEXPPTR(x), *pout = SEXPPTR(out);
switch(txAG) {
case REALSXP:
case INTSXP:
case LGLSXP: error("Cannot replace missing values in string with numeric data");
case STRSXP:
{
SEXP *pAG = STRING_PTR(xAG);
SEXP *pAG = SEXPPTR(xAG);
if(nog) {
for(int j = 0; j != col; ++j) {
int s = j * row, e = s + row;
Expand Down
2 changes: 1 addition & 1 deletion src/base_radixsort.c
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ static int StrCmp(SEXP x, SEXP y) // also used by bmerge and chmatch
void checkEncodings(SEXP x) // static
{
cetype_t ce;
SEXP *px = STRING_PTR(x);
SEXP *px = SEXPPTR(x);
int i, lx = length(x);
for (i = 0; i != lx && px[i] == NA_STRING; ++i);

Expand Down
2 changes: 2 additions & 0 deletions src/base_radixsort.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// #define IS_ASCII(x) ((x)->sxpinfo.gp & ASCII_MASK)
// #define IS_ASCII(x) (LEVELS(x) & ASCII_MASK)

#define SEXPPTR(x) ((SEXP *)DATAPTR(x)) // Replacing STRING_PTR

// NOTE: All of this is copied from Defn.h: https://github.com/wch/r-source/blob/28de75af0541f93832c5899139b969d290bf422e/src/include/Defn.h
// to avoid checking for ALTREP in TRUELENGTH, which slows down the code unnecessarily...

Expand Down
5 changes: 2 additions & 3 deletions src/data.table.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
and licensed under a Mozilla Public License 2.0 (MPL-2.0) license.
*/

#include <R.h>
#define USE_RINTERNALS
#include <Rinternals.h>
#include <stdint.h> // for uint64_t rather than unsigned long long
#include "base_radixsort.h"
// #include <stdint.h> // for uint64_t rather than unsigned long long
#include <stdbool.h>
// #include "types.h"

Expand Down
16 changes: 8 additions & 8 deletions src/data.table_rbindlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void writeNA(SEXP v, const int from, const int n)
for (int i=from; i<=to; ++i) vd[i] = NA_CPLX;
} break;
case STRSXP: {
SEXP *vd = STRING_PTR(v);
SEXP *vd = SEXPPTR(v);
for (int i=from; i<=to; ++i) vd[i] = NA_STRING;
} break;
case VECSXP:
Expand Down Expand Up @@ -262,7 +262,7 @@ SEXP rbindlist(SEXP l, SEXP usenamesArg, SEXP fillArg, SEXP idcolArg)
if (isNull(li) || !LENGTH(li)) continue;
const SEXP cn = getAttrib(li, R_NamesSymbol);
if (!length(cn)) continue;
const SEXP *cnp = STRING_PTR(cn);
const SEXP *cnp = SEXPPTR(cn);
for (int j=0; j<thisncol; j++) {
SEXP s = cnp[j];
if (TRUELENGTH(s)<0) continue; // seen this name before
Expand Down Expand Up @@ -293,7 +293,7 @@ SEXP rbindlist(SEXP l, SEXP usenamesArg, SEXP fillArg, SEXP idcolArg)
if (thisncol==0) continue;
const SEXP cn = getAttrib(li, R_NamesSymbol);
if (!length(cn)) continue;
const SEXP *cnp = STRING_PTR(cn);
const SEXP *cnp = SEXPPTR(cn);
memset(counts, 0, nuniq*sizeof(int));
for (int j=0; j<thisncol; j++) {
SEXP s = cnp[j];
Expand Down Expand Up @@ -333,7 +333,7 @@ SEXP rbindlist(SEXP l, SEXP usenamesArg, SEXP fillArg, SEXP idcolArg)
if (!length(cn)) {
for (int j=0; j<thisncol; j++) colMapRaw[i*ncol + j] = j;
} else {
const SEXP *cnp = STRING_PTR(cn);
const SEXP *cnp = SEXPPTR(cn);
memset(counts, 0, nuniq*sizeof(int));
for (int j=0; j<thisncol; j++) {
SEXP s = cnp[j];
Expand Down Expand Up @@ -540,7 +540,7 @@ SEXP rbindlist(SEXP l, SEXP usenamesArg, SEXP fillArg, SEXP idcolArg)
// c( a<c<b, c<b, 'c,b' ) => a<c<b because the regular factor/character items c and b exist in the ordered levels
// c( a<c<b, c<b, 'c,d' ) => a<c<b<d 'd' from non-ordered item added on the end of longest ordered levels
// c( a<c<b, c<b<d<e ) => regular factor because this case isn't yet implemented. a<c<b<d<e would be possible in future (extending longest at the beginning or end)
const SEXP *sd = STRING_PTR(longestLevels);
const SEXP *sd = SEXPPTR(longestLevels);
nLevel = allocLevel = longestLen;
levelsRaw = (SEXP *)malloc(nLevel * sizeof(SEXP));
if (!levelsRaw) { savetl_end(); error("Failed to allocate working memory for %d ordered factor levels of result column %d", nLevel, idcol+j+1); }
Expand All @@ -556,7 +556,7 @@ SEXP rbindlist(SEXP l, SEXP usenamesArg, SEXP fillArg, SEXP idcolArg)
SEXP thisCol = VECTOR_ELT(VECTOR_ELT(l, i), w);
if (isOrdered(thisCol)) {
SEXP levels = getAttrib(thisCol, R_LevelsSymbol);
const SEXP *levelsD = STRING_PTR(levels);
const SEXP *levelsD = SEXPPTR(levels);
const int n = length(levels);
for (int k=0, last=0; k<n; ++k) {
SEXP s = levelsD[k];
Expand Down Expand Up @@ -595,7 +595,7 @@ SEXP rbindlist(SEXP l, SEXP usenamesArg, SEXP fillArg, SEXP idcolArg)
SEXP thisCol = VECTOR_ELT(li, w);
SEXP thisColStr = isFactor(thisCol) ? getAttrib(thisCol, R_LevelsSymbol) : (isString(thisCol) ? thisCol : VECTOR_ELT(coercedForFactor, i));
const int n = length(thisColStr);
const SEXP *thisColStrD = STRING_PTR(thisColStr); // D for data
const SEXP *thisColStrD = SEXPPTR(thisColStr); // D for data
for (int k=0; k<n; ++k) {
SEXP s = thisColStrD[k];
if (s==NA_STRING || // remove NA from levels; test 1979 found by package emil when revdep testing 1.12.2 (#3473)
Expand Down Expand Up @@ -662,7 +662,7 @@ SEXP rbindlist(SEXP l, SEXP usenamesArg, SEXP fillArg, SEXP idcolArg)
}
}
} else {
const SEXP *sd = STRING_PTR(thisColStr);
const SEXP *sd = SEXPPTR(thisColStr);
if (length(thisCol)<=1) {
const int val = (length(thisCol)==1 && sd[0]!=NA_STRING) ? -TRUELENGTH(sd[0]) : NA_INTEGER;
for (int r=0; r<thisnrow; ++r) targetd[ansloc+r] = val;
Expand Down
10 changes: 5 additions & 5 deletions src/data.table_subset.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ static SEXP shallow(SEXP dt, SEXP cols, R_len_t n)
SEXP names = PROTECT(getAttrib(dt, R_NamesSymbol)); protecti++;
SEXP newnames = PROTECT(allocVector(STRSXP, n)); protecti++;

const SEXP *pdt = SEXPPTR_RO(dt), *pnam = STRING_PTR(names);
SEXP *pnewdt = SEXPPTR(newdt), *pnnam = STRING_PTR(newnames);
const SEXP *pdt = SEXPPTR_RO(dt), *pnam = SEXPPTR(names);
SEXP *pnewdt = SEXPPTR(newdt), *pnnam = SEXPPTR(newnames);

const int l = isNull(cols) ? LENGTH(dt) : length(cols);
if (isNull(cols)) {
Expand Down Expand Up @@ -215,7 +215,7 @@ void subsetVectorRaw(SEXP ans, SEXP source, SEXP idx, const bool anyNA)
// TODO - discuss with Luke Tierney. Produce benchmarks on integer/double to see if it's worth making a safe
// API interface for package use for STRSXP.
// Aside: setkey() is a separate special case (a permutation) and does do this in parallel without using SET_*.
SEXP *restrict sp = STRING_PTR(source)-1, *restrict ap = STRING_PTR(ans);
SEXP *restrict sp = SEXPPTR(source)-1, *restrict ap = SEXPPTR(ans);
PARLOOP(NA_STRING);
} break;
case VECSXP : {
Expand Down Expand Up @@ -423,7 +423,7 @@ SEXP subsetCols(SEXP x, SEXP cols, SEXP checksf) { // SEXP fretall
// sf data frames: Need to add sf_column
if(oxl && asLogical(checksf) && INHERITS(x, char_sf)) {
int sfcoln = NA_INTEGER, sf_col_sel = 0;
SEXP *pnam = STRING_PTR(nam), sfcol = asChar(getAttrib(x, sym_sf_column));
SEXP *pnam = SEXPPTR(nam), sfcol = asChar(getAttrib(x, sym_sf_column));
for(int i = l; i--; ) {
if(pnam[i] == sfcol) {
sfcoln = i + 1;
Expand Down Expand Up @@ -511,7 +511,7 @@ SEXP subsetDT(SEXP x, SEXP rows, SEXP cols, SEXP checkrows) { // , SEXP fastret
if(oxl && INHERITS(x, char_sf)) {
int sfcoln = NA_INTEGER, sf_col_sel = 0;
SEXP nam = PROTECT(getAttrib(x, R_NamesSymbol));
SEXP *pnam = STRING_PTR(nam), sfcol = asChar(getAttrib(x, sym_sf_column));
SEXP *pnam = SEXPPTR(nam), sfcol = asChar(getAttrib(x, sym_sf_column));
for(int i = l; i--; ) {
if(pnam[i] == sfcol) {
sfcoln = i + 1;
Expand Down
10 changes: 5 additions & 5 deletions src/data.table_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ SEXP setnames(SEXP x, SEXP nam) {
return x;
}
SEXP newnam = PROTECT(allocVector(STRSXP, n)),
*pnn = STRING_PTR(newnam), *pn = STRING_PTR(nam);
*pnn = SEXPPTR(newnam), *pn = SEXPPTR(nam);
for(int i = 0; i < l; ++i) pnn[i] = pn[i];
SETLENGTH(newnam, l);
SET_TRUELENGTH(newnam, n);
Expand Down Expand Up @@ -58,7 +58,7 @@ bool allNA(SEXP x, bool errorForBadType) {
}
return true;
case STRSXP: {
const SEXP *xd = STRING_PTR(x);
const SEXP *xd = SEXPPTR(x);
for (int i=0; i != n; ++i) if (xd[i]!=NA_STRING) {
return false;
}
Expand Down Expand Up @@ -159,7 +159,7 @@ SEXP dt_na(SEXP x, SEXP cols, SEXP Rprop, SEXP Rcount) {
for (int j=0; j != n; ++j) ians[j] += (iv[j] == NA_INTEGER);
} break;
case STRSXP: {
const SEXP *sv = STRING_PTR(v);
const SEXP *sv = SEXPPTR(v);
for (int j=0; j != n; ++j) ians[j] += (sv[j] == NA_STRING);
} break;
case REALSXP: {
Expand Down Expand Up @@ -204,7 +204,7 @@ SEXP dt_na(SEXP x, SEXP cols, SEXP Rprop, SEXP Rcount) {
for (int j=0; j != n; ++j) ians[j] |= (iv[j] == NA_INTEGER);
} break;
case STRSXP: {
const SEXP *sv = STRING_PTR(v);
const SEXP *sv = SEXPPTR(v);
for (int j=0; j != n; ++j) ians[j] |= (sv[j] == NA_STRING);
} break;
case REALSXP: {
Expand Down Expand Up @@ -294,7 +294,7 @@ SEXP setcolorder(SEXP x, SEXP o) {
}
Free(seen);

SEXP *tmp = Calloc(ncol, SEXP), *namesd = STRING_PTR(names);
SEXP *tmp = Calloc(ncol, SEXP), *namesd = SEXPPTR(names);
const SEXP *xd = SEXPPTR_RO(x);
for (int i=0; i != ncol; ++i) tmp[i] = xd[od[i]-1];
for (int i=0; i != ncol; ++i) SET_VECTOR_ELT(x, i, tmp[i]);
Expand Down
Loading

0 comments on commit f912549

Please sign in to comment.