Skip to content

Commit 385f294

Browse files
committed
reduce diff, increase codecov precision, more tests
1 parent 15e648b commit 385f294

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

R/frollapply.R

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ frollapply = function(X, N, FUN, ..., by.column=TRUE, fill=NA, align=c("right","
209209
cpy = copy
210210
ansMask = function(len, n) {
211211
mask = rep(TRUE, len)
212-
if (n) mask[seq_len(n - 1L)] = FALSE ## handle n==0
212+
if (n) ## handle n==0
213+
mask[seq_len(n-1L)] = FALSE
213214
mask
214215
}
215216
tight0 = function(i, dest, src, n) FUN(dest, ...) ## skip memcpy when n==0
@@ -240,7 +241,7 @@ frollapply = function(X, N, FUN, ..., by.column=TRUE, fill=NA, align=c("right","
240241
if (has.growable) {
241242
tight = function(i, dest, src, n) FUN(.Call(CmemcpyVectoradaptive, dest, src, i, n), ...) # CmemcpyVectoradaptive handles k[i]==0
242243
} else {
243-
tight = function(i, dest, src, n) {stopf("internal error: has.growable should be TRUE, if enabled it requires to implement support for n==0"); FUN(src[(i-n[i]+1L):i], ...)} # nocov
244+
tight = function(i, dest, src, n) {stopf("internal error: has.growable should be TRUE, implement support for n==0"); FUN(src[(i-n[i]+1L):i], ...)} # nocov
244245
}
245246
} else {
246247
if (!list.df) {
@@ -252,9 +253,9 @@ frollapply = function(X, N, FUN, ..., by.column=TRUE, fill=NA, align=c("right","
252253
tight = function(i, dest, src, n) FUN(.Call(CmemcpyDTadaptive, dest, src, i, n), ...) # CmemcpyDTadaptive handles k[i]==0
253254
} else {
254255
if (!list.df) { # nocov
255-
tight = function(i, dest, src, n) {stopf("internal error: has.growable should be TRUE, if enabled it requires to implement support for n==0"); FUN(src[(i-n[i]+1L):i, , drop=FALSE], ...)} # nocov
256+
tight = function(i, dest, src, n) {stopf("internal error: has.growable should be TRUE, implement support for n==0"); FUN(src[(i-n[i]+1L):i, , drop=FALSE], ...)} # nocov
256257
} else {
257-
tight = function(i, dest, src, n) {stopf("internal error: has.growable should be TRUE, if enabled it requires to implement support for n==0"); FUN(lapply(src, `[`, (i-n[i]+1L):i), ...)} # nocov
258+
tight = function(i, dest, src, n) {stopf("internal error: has.growable should be TRUE, implement support for n==0"); FUN(lapply(src, `[`, (i-n[i]+1L):i), ...)} # nocov
258259
}
259260
}
260261
}
@@ -289,7 +290,11 @@ frollapply = function(X, N, FUN, ..., by.column=TRUE, fill=NA, align=c("right","
289290
w = allocWindow(thisx, thisn) ## prepare window, handles adaptive
290291
ansmask = ansMask(thislen, thisn)
291292
ansi = which(ansmask)
292-
tightFUN = if (adaptive || thisn) tight else tight0 ## handle n==0 for !adaptive, for !adaptive thisn should be scalar
293+
tightFUN = if (adaptive || thisn) { ## handle n==0 for !adaptive, for !adaptive thisn should be scalar
294+
tight
295+
} else {
296+
tight0
297+
}
293298
if (use.fork) { ## !windows && getDTthreads()>1L
294299
ths = min(DTths, length(ansi))
295300
ii = split(ansi, sort(rep_len(seq_len(ths), length(ansi)))) ## assign row indexes to threads

inst/tests/froll.Rraw

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ test(6000.084, frollmean(list(1:3, numeric()), 2), list(c(NA_real_, 1.5, 2.5), n
369369
test(6000.085, frollmean(1:3, integer()), error="n must be non 0 length")
370370
test(6000.086, frollmean(list(1:3, 2:4), integer()), error="n must be non 0 length")
371371
#### n==0 (k==0, k[i]==0)
372-
##moved to 6001.
372+
## 6000.87 6000.88 moved to 6001.
373373
#### n<0
374374
test(6000.089, frollmean(1:3, -2), error="n must be non-negative integer values")
375375
#### n[[1L]]>0 && n[[2L]]<0
@@ -1593,8 +1593,10 @@ rollfun = function(x, n, FUN, fill=NA_real_, na.rm=FALSE, nf.rm=FALSE, partial=F
15931593
f = match.fun(FUN)
15941594
if (nf.rm) x[is.infinite(x)] = NA_real_
15951595
for (i in seq_along(x)) {
1596-
ans[i] = if (i >= n)
1597-
f(x[(i-n+1):i], na.rm=na.rm)
1596+
ans[i] = if (n==0)
1597+
f(x[integer()], na.rm=na.rm)
1598+
else if (i >= n)
1599+
f(x[(i-n+1L):i], na.rm=na.rm)
15981600
else if (partial)
15991601
f(x[max((i-n+1), 1L):i], na.rm=na.rm)
16001602
else
@@ -1647,6 +1649,8 @@ x = makeNA(rnorm(1e3), nf=TRUE); n = 51
16471649
base_compare(x, n)
16481650
x = makeNA(rnorm(1e3+1), nf=TRUE); n = 51
16491651
base_compare(x, n)
1652+
x = makeNA(rnorm(1e3), nf=TRUE); n = 0
1653+
base_compare(x, n)
16501654

16511655
#### against zoo
16521656
if (requireNamespace("zoo", quietly=TRUE)) {
@@ -1730,14 +1734,18 @@ arollfun = function(FUN, x, n, na.rm=FALSE, align=c("right","left"), fill=NA, nf
17301734
f = match.fun(FUN)
17311735
if (align=="right") {
17321736
for (i in seq_along(x)) {
1733-
if (i >= n[i])
1737+
if (n[i] == 0)
1738+
ans[i] = f(x[integer()], na.rm=na.rm)
1739+
else if (i >= n[i])
17341740
ans[i] = f(x[(i-n[i]+1L):i], na.rm=na.rm)
17351741
else if (partial)
17361742
ans[i] = f(x[1L:i], na.rm=na.rm)
17371743
}
17381744
} else {
17391745
for (i in seq_along(x)) {
1740-
if (i <= nx-n[i]+1)
1746+
if (n[i] == 0)
1747+
ans[i] = f(x[integer()], na.rm=na.rm)
1748+
else if (i <= nx-n[i]+1)
17411749
ans[i] = f(x[i:(i+n[i]-1L)], na.rm=na.rm)
17421750
else if (partial)
17431751
ans[i] = f(x[i:length(x)], na.rm=na.rm)
@@ -1797,6 +1805,8 @@ x = rnorm(1e3); n = sample(51, length(x), TRUE) # x even, n odd
17971805
afun_compare(x, n)
17981806
x = rnorm(1e3+1); n = sample(51, length(x), TRUE) # x odd, n odd
17991807
afun_compare(x, n)
1808+
x = rnorm(1e3); n = sample(0:49, length(x), TRUE) # x even, n even
1809+
afun_compare(x, n)
18001810
#### leading and trailing NAs
18011811
x = c(rep(NA, 60), rnorm(1e3), rep(NA, 60)); n = sample(50, length(x), TRUE)
18021812
afun_compare(x, n)
@@ -1806,6 +1816,8 @@ x = c(rep(NA, 60), rnorm(1e3), rep(NA, 60)); n = sample(51, length(x), TRUE)
18061816
afun_compare(x, n)
18071817
x = c(rep(NA, 60), rnorm(1e3+1), rep(NA, 60)); n = sample(51, length(x), TRUE)
18081818
afun_compare(x, n)
1819+
x = c(rep(NA, 60), rnorm(1e3), rep(NA, 60)); n = sample(0:49, length(x), TRUE)
1820+
afun_compare(x, n)
18091821
#### random NA
18101822
x = makeNA(rnorm(1e3)); n = sample(50, length(x), TRUE)
18111823
afun_compare(x, n)
@@ -1815,6 +1827,8 @@ x = makeNA(rnorm(1e3)); n = sample(51, length(x), TRUE)
18151827
afun_compare(x, n)
18161828
x = makeNA(rnorm(1e3+1)); n = sample(51, length(x), TRUE)
18171829
afun_compare(x, n)
1830+
x = makeNA(rnorm(1e3)); n = sample(0:49, length(x), TRUE)
1831+
afun_compare(x, n)
18181832
#### random NA non-finites
18191833
x = makeNA(rnorm(1e3), nf=TRUE); n = sample(50, length(x), TRUE)
18201834
afun_compare(x, n)
@@ -1824,4 +1838,6 @@ x = makeNA(rnorm(1e3), nf=TRUE); n = sample(51, length(x), TRUE)
18241838
afun_compare(x, n)
18251839
x = makeNA(rnorm(1e3+1), nf=TRUE); n = sample(51, length(x), TRUE)
18261840
afun_compare(x, n)
1841+
x = makeNA(rnorm(1e3), nf=TRUE); n = sample(0:49, length(x), TRUE)
1842+
afun_compare(x, n)
18271843
rm(num)

0 commit comments

Comments
 (0)