Skip to content

Commit 40a830d

Browse files
committed
update
1 parent 96fa525 commit 40a830d

File tree

2 files changed

+16
-54
lines changed

2 files changed

+16
-54
lines changed

inst/tests/tests.Rraw

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18119,9 +18119,9 @@ test(2233.25, copy(DT)[a!=4, v := head(b, 3L), a, verbose=TRUE], copy(DT)[a!=4,
1811918119
DT = data.table(a=1:3,b=(1:9)/10)
1812018120
test(2233.26, DT[, c("v1","v2") := .(min(b), max(b)), a, verbose=TRUE], data.table(a=1:3, b=(1:9)/10, v1=(1:3)/10, v2=(7:9)/10), output="GForce optimized j to")
1812118121
test(2233.27, DT[, c("v1","v2") := .(head(b,3L), tail(b,3L)), a, verbose=TRUE], data.table(a=1:3, b=(1:9)/10, v1=(1:9)/10, v2=(1:9)/10), output="GForce optimized j to")
18122-
test(2233.28, DT[, c("v1","v2") := .(head(b,3L), tail(b,2L)), a], error="Supplied 6 items to be assigned to 9 items of column 'v2'.")
18123-
test(2233.29, DT[, c("v1","v2") := .(head(b,2L), tail(b,3L)), a], error="Supplied 6 items to be assigned to 9 items of column 'v1'.")
18124-
test(2233.30, DT[, c("v1","v2") := .(head(b,2L), tail(b,2L)), a], error="Supplied 6 items to be assigned to 9 items of column 'v1'.")
18122+
test(2233.28, DT[, c("v1","v2") := .(head(b,3L), tail(b,2L)), a], error="Supplied 6 items.*9")
18123+
test(2233.29, DT[, c("v1","v2") := .(head(b,2L), tail(b,3L)), a], error="Supplied 6 items.*9")
18124+
test(2233.30, DT[, c("v1","v2") := .(head(b,2L), tail(b,2L)), a], error="Supplied 6 items.*9.")
1812518125
test(2233.31, DT[, c("v1","v2") := .(min(b), max(b)), a, verbose=TRUE], DT[, c("v1","v2") := .(base::min(b), base::max(b)), a ], output="GForce optimized j to")
1812618126
test(2233.32, DT[, c("v1","v2") := .(head(b,3L), tail(b,3L)), a, verbose=TRUE], DT[, c("v1","v2") := .(utils::head(b,3L), utils::tail(b,3L)), a], output="GForce optimized j to")
1812718127

src/gsumm.c

Lines changed: 13 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ SEXP gforce(SEXP env, SEXP jsub, SEXP o, SEXP f, SEXP l, SEXP irowsArg, SEXP grp
335335
}
336336

337337
if (!lens) {
338+
Rprintf("no lens!\n");
338339
// every group is length-1 (no dynamic or fixed_over_1 results) so can just use the gans as-is and we're done
339340
for (int i=0; i<ngans; ++i) {
340341
SET_VECTOR_ELT(ans, ngrpcol+i, VECTOR_ELT(gans, i));
@@ -365,57 +366,18 @@ SEXP gforce(SEXP env, SEXP jsub, SEXP o, SEXP f, SEXP l, SEXP irowsArg, SEXP grp
365366
// TODO: expand subsetVector to accept 'rep=' and 'len=' so that subset and rep.int can be done in one step avoiding intermediate subset of first_each_group, and pass len=anslen which we know already
366367
}
367368

368-
for (int i=0; i<ngans; ++i) {
369-
SEXP tt = VECTOR_ELT(gans, i);
370-
SEXP att = getAttrib(tt, sym_gforce_dynamic); // how long the items are, we won't parallelize within column so we can sweep forwards
371-
if (isNull(att)) {
372-
// e.g. the mean in .(mean(colA), first(colB, n=2)); TODO test
373-
SET_VECTOR_ELT(ans, ngrpcol+i, eval(PROTECT(lang3(install("rep.int"), tt, lens)), R_GlobalEnv));
374-
UNPROTECT(1);
375-
} else {
376-
// att can be i) empty lens vec in which case this ans col's shape is min(grpsize,w). If this matches the end result shape (i.e. all ans columns are the same, then no need to allocate copy)
377-
// ii) presence of lens vec always needs a copy and pad? (or could loop it and test if the same since looping through ngrp is much faster than nrow vector)
378-
const int *ss = isNull(VECTOR_ELT(att,0)) ? NULL : INTEGER(VECTOR_ELT(att,0));
379-
const bool first = LOGICAL(VECTOR_ELT(att, 1))[0];
380-
const int w = INTEGER(VECTOR_ELT(att,2))[0];
381-
SEXP newcol = PROTECT(allocVector(TYPEOF(tt), anslen));
382-
copyMostAttrib(tt, newcol);
383-
setAttrib(newcol, sym_gforce_dynamic, R_NilValue);
384-
int ansi=0, k=0;
385-
386-
#define DOGANS(CTYPE, RTYPE, RNA, ASSIGN) { \
387-
const CTYPE *xd = (const CTYPE *)RTYPE(tt); \
388-
CTYPE *ansd = (CTYPE *)RTYPE(newcol); \
389-
CTYPE val = RNA; \
390-
for (int g=0; g<ngrp; ++g) { \
391-
const int grp_allocated = MIN(grpsize[g], w); \
392-
const int thislen = ss ? ss[g] : grp_allocated; \
393-
const int targetlen = lensp[g]; \
394-
const int napad = targetlen-thislen; \
395-
k += (!first)*(grp_allocated-thislen); \
396-
for (int i=0; i<thislen; ++i) { val=xd[k++]; ASSIGN; } \
397-
val=RNA; for (int i=0; i<napad; ++i) ASSIGN; \
398-
k += (first)*(grp_allocated-thislen); \
399-
} \
400-
ansd++; /* just to suppress unused-variable warning in STRSXP and VECSXP cases */ \
401-
} break;
402-
403-
switch(TYPEOF(tt)) {
404-
case RAWSXP: DOGANS(Rbyte, RAW, 0, ansd[ansi++]=val)
405-
case LGLSXP: DOGANS(int, LOGICAL, NA_LOGICAL, ansd[ansi++]=val)
406-
case INTSXP: DOGANS(int, INTEGER, NA_INTEGER, ansd[ansi++]=val)
407-
case REALSXP: if (INHERITS(tt, char_integer64)) {
408-
DOGANS(int64_t, REAL, NA_INTEGER64, ansd[ansi++]=val)
409-
} else { DOGANS(double, REAL, NA_REAL, ansd[ansi++]=val) }
410-
case CPLXSXP: DOGANS(Rcomplex, COMPLEX, NA_CPLX, ansd[ansi++]=val)
411-
case STRSXP: DOGANS(SEXP, STRING_PTR, NA_STRING, SET_STRING_ELT(newcol,ansi++,val))
412-
case VECSXP: DOGANS(SEXP, SEXPPTR_RO, ScalarLogical(NA_LOGICAL), SET_VECTOR_ELT(newcol,ansi++,val)) /* TODO: global replace ScalarLogical() with fixed constant R_NAValue, depending on R dependency */
413-
default:
414-
error(_("Type '%s' is not supported by gforce padding."), type2char(TYPEOF(tt)));
415-
}
416-
SET_VECTOR_ELT(ans, ngrpcol+i, newcol);
417-
UNPROTECT(1); // newcol
418-
}
369+
// Now copy gans into ans like in dogropus
370+
SEXP source;
371+
int thislen;
372+
for (int j=0; j<ngans; ++j) {
373+
source = VECTOR_ELT(gans,j);
374+
thislen = length(source);
375+
if (thislen!=anslen)
376+
error(_("Supplied %d items for column %d which has %d rows. The RHS length must match the LHS length exactly. If you wish to 'recycle' the RHS please use rep() explicitly to make this intent clear to readers of your code."), thislen, ngrpcol+j, anslen);
377+
// clear attributes
378+
if (!isNull(getAttrib(source, sym_gforce_dynamic)))
379+
setAttrib(source, sym_gforce_dynamic, R_NilValue);
380+
SET_VECTOR_ELT(ans, j+ngrpcol, source);
419381
}
420382
}
421383
UNPROTECT(nprotect);

0 commit comments

Comments
 (0)