Skip to content

Commit 4330a4f

Browse files
committed
use S3 for internal helpers
1 parent 5cced2e commit 4330a4f

2 files changed

Lines changed: 40 additions & 33 deletions

File tree

‎R/data.table.R‎

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,20 +1355,20 @@ replace_dot_alias = function(e) {
13551355
jvnames = NULL
13561356
if (is.name(jsub)) {
13571357
# j is a single unquoted column name
1358-
if (jsub!=".SD") jvnames = .dt_drop_dot(jsub)
1358+
if (jsub!=".SD") jvnames = .drop_dot(jsub)
13591359
# jsub is list()ed after it's eval'd inside dogroups.
13601360
} else { # else maybe a call to transform or something which returns a list.
1361-
jsub_names = .dt_do_j_names(jsub, jvnames)
1361+
jsub_names = .do_j_names(jsub, jvnames)
13621362
jsub = jsub_names$jsub
13631363
jvnames = jsub_names$jvnames
13641364
}
13651365
av = all.vars(jsub,TRUE) # TRUE fixes bug #1294 which didn't see b in j=fns[[b]](c)
13661366
use.I = ".I" %chin% av
13671367
if (any(c(".SD","eval","get","mget") %chin% av)) {
13681368
if (missing_SDcols) {
1369-
# here we need to use '.dt_dupdiff' instead of 'setdiff'. Ex: setdiff(c("x", "x"), NULL) will give 'x'.
1369+
# here we need to use '.dupdiff' instead of 'setdiff'. Ex: setdiff(c("x", "x"), NULL) will give 'x'.
13701370
# slight memory efficiency boost if we only store sdvars if it differs from ansvars, but a bit tricky to examine all the uses here
1371-
ansvars = sdvars = .dt_dupdiff(names_x, union(bynames, allbyvars)) # TO DO: allbyvars here for vars used by 'by'. Document.
1371+
ansvars = sdvars = .dupdiff(names_x, union(bynames, allbyvars)) # TO DO: allbyvars here for vars used by 'by'. Document.
13721372
# just using .SD in j triggers all non-by columns in the subset even if some of
13731373
# those columns are not used. It would be tricky to detect whether the j expression
13741374
# really does use all of the .SD columns or not, hence .SDcols for grouping
@@ -1420,11 +1420,11 @@ replace_dot_alias = function(e) {
14201420
ansvars = sdvars = names_x[ansvals]
14211421
} else if (is.numeric(.SDcols)) {
14221422
.SDcols = as.integer(.SDcols)
1423-
# if .SDcols is numeric, use '.dt_dupdiff' instead of 'setdiff'
1423+
# if .SDcols is numeric, use '.dupdiff' instead of 'setdiff'
14241424
if (length(unique(sign(.SDcols))) > 1L) stopf(".SDcols is numeric but has both +ve and -ve indices")
14251425
if (any(idx <- abs(.SDcols)>ncol(x) | abs(.SDcols)<1L))
14261426
stopf(".SDcols is numeric but out of bounds [1, %d] at: %s", ncol(x), brackify(which(idx)))
1427-
ansvars = sdvars = if (negate_sdcols) .dt_dupdiff(names_x[-.SDcols], bynames) else names_x[.SDcols]
1427+
ansvars = sdvars = if (negate_sdcols) .dupdiff(names_x[-.SDcols], bynames) else names_x[.SDcols]
14281428
ansvals = if (negate_sdcols) setdiff(seq_along(names(x)), c(.SDcols, which(names(x) %chin% bynames))) else .SDcols
14291429
} else {
14301430
if (!is.character(.SDcols)) stopf(".SDcols should be column numbers or names")
@@ -1488,7 +1488,7 @@ replace_dot_alias = function(e) {
14881488
suppPrint = identity
14891489
if (length(av) && av[1L] == ":=") {
14901490
if (.Call(C_islocked, x)) stopf(".SD is locked. Using := in .SD's j is reserved for possible future use; a tortuously flexible way to modify by group. Use := in j directly to modify by group by reference.")
1491-
suppPrint = .dt_suppPrint
1491+
suppPrint = .suppPrint
14921492
# Suppress print when returns ok not on error, bug #2376. Thanks to: http://stackoverflow.com/a/13606880/403310
14931493
# All appropriate returns following this point are wrapped; i.e. return(suppPrint(x)).
14941494

@@ -1838,7 +1838,7 @@ replace_dot_alias = function(e) {
18381838
if (identical(class(jval), c('data.table', 'data.frame')))
18391839
setattr(jval, 'class', class(x)) # fix for #64
18401840
# can jval be sorted by the same key as x? improved for #4498
1841-
shared_keys = .dt_get_shared_keys(jsub, jvnames, sdvars = sdvars, key(x))
1841+
shared_keys = .get_shared_keys(jsub, jvnames, sdvars, key(x))
18421842
if (is.null(irows) && !is.null(shared_keys)) {
18431843
if (!any(shared_keys %chin% duplicated_values(names(jval)))) setattr(jval, 'sorted', shared_keys)
18441844
# potentially inefficient backup -- check if jval is sorted by key(x)
@@ -2047,26 +2047,26 @@ replace_dot_alias = function(e) {
20472047
q3 = 0L
20482048
if (!is.symbol(jsub)) {
20492049
if (jsub %iscall% "list"){
2050-
q3 = max(sapply(jsub, .dt_headTail_arg))
2050+
q3 = max(sapply(jsub, .headTail_arg))
20512051
} else if (length(jsub)==3L) {
2052-
q3 = .dt_headTail_arg(jsub)
2052+
q3 = .headTail_arg(jsub)
20532053
}
20542054
}
20552055
if (q3 > 0L) {
20562056
grplens = pmin.int(q3, len__)
20572057
g = lapply(g, rep.int, times=grplens)
2058-
} else if (.dt_is_nrows(jsub)) {
2058+
} else if (.is_nrows(jsub)) {
20592059
g = lapply(g, rep.int, times=len__)
20602060
# unpack list of lists for nrows functions
20612061
if (all(vapply_1b(ans, is.list))) {
2062-
ans = lapply(ans, .dt_zip_items)
2062+
ans = lapply(ans, .zip_items)
20632063
}
20642064
}
20652065
ans = c(g, ans)
20662066
} else {
20672067
ans = .Call(Cdogroups, x, xcols, groups, grpcols, jiscols, xjiscols, grporder, o__, f__, len__, jsub, SDenv, cols, newnames, !missing_on, verbose, as.integer(showProgress))
20682068
}
2069-
.dt_runlock(ans)
2069+
.runlock(ans)
20702070
if (verbose) {cat(timetaken(last.started.at),"\n"); flush.console()}
20712071
# TO DO: xrows would be a better name for irows: irows means the rows of x that i joins to
20722072
# Grouping by i: icols the joins columns (might not need), isdcols (the non join i and used by j), all __ are length x
@@ -2081,7 +2081,7 @@ replace_dot_alias = function(e) {
20812081
if (length(o__)) jrows = o__[jrows]
20822082
if (length(irows)) jrows = irows[jrows]
20832083
if (length(jvals)==1L) jvals = jvals[[1L]] # unwrap single column jvals for assign
2084-
if (.dt_is_nrows(jsub)) { # 5403 unwrap multicolumn jvals for gfunctions that can return lists
2084+
if (.is_nrows(jsub)) { # 5403 unwrap multicolumn jvals for gfunctions that can return lists
20852085
jvals = if (length(jvals) != length(lhs)) split(unlist(jvals), rep(seq_along(jvals[[1L]]), length(jvals))) else lapply(jvals, unlist)
20862086
}
20872087
.Call(Cassign, x, jrows, lhs, newnames, jvals)
@@ -2141,21 +2141,23 @@ replace_dot_alias = function(e) {
21412141
}
21422142

21432143
# setdiff removes duplicate entries, which'll create issues with duplicated names. Use %chin% instead.
2144-
.dt_dupdiff = function(x, y) x[!x %chin% y]
2144+
.dupdiff = function(x, y) x[!x %chin% y]
2145+
class(.dupdiff) = "dt_query_helper"
21452146

21462147
# auto-name a symbol in j: special symbols .N, .I, .GRP, .NGRP and .BY lose their leading dot
2147-
.dt_drop_dot = function(x) {
2148+
.drop_dot = function(x) {
21482149
if (length(x)!=1L) internal_error("drop_dot passed %d items", length(x)) # nocov
21492150
if (startsWith(x<-as.character(x), ".") && x %chin% c(".N", ".I", ".GRP", ".NGRP", ".BY"))
21502151
substr(x, 2L, nchar(x))
21512152
else
21522153
x
21532154
}
2155+
class(.drop_dot) = "dt_query_helper"
21542156

21552157
# handle auto-naming of last item of j (e.g. within {} or if/else, #2478)
21562158
# e.g. DT[, .(a=sum(v), v, .N), by=] should create columns named a, v, N
21572159
# returns list(jsub=, jvnames=); jvnames is threaded through the recursion so that the names of different branches can be compared
2158-
.dt_do_j_names = function(q, jvnames) {
2160+
.do_j_names = function(q, jvnames) {
21592161
if (!is.call(q) || !is.name(q[[1L]])) return(list(jsub=q, jvnames=jvnames))
21602162
if (q[[1L]] %chin% c('list', '.')) {
21612163
q[[1L]] = quote(list)
@@ -2167,7 +2169,7 @@ replace_dot_alias = function(e) {
21672169
for (jj in which(!nzchar(nm))) {
21682170
thisq = q[[jj + 1L]]
21692171
if (missing(thisq)) stopf("Item %d of the .() or list() passed to j is missing", jj) #3507
2170-
if (is.name(thisq)) nm[jj] = .dt_drop_dot(thisq)
2172+
if (is.name(thisq)) nm[jj] = .drop_dot(thisq)
21712173
# TO DO: if call to a[1] for example, then call it 'a' too
21722174
}
21732175
if (!is.null(jvnames)) {
@@ -2183,7 +2185,7 @@ replace_dot_alias = function(e) {
21832185
}
21842186
if (q[[1L]] == '{') {
21852187
if (!is.null(q[[qlen<-length(q)]])) {
2186-
ans = .dt_do_j_names(q[[qlen]], jvnames)
2188+
ans = .do_j_names(q[[qlen]], jvnames)
21872189
q[[qlen]] = ans$jsub
21882190
jvnames = ans$jvnames
21892191
}
@@ -2192,24 +2194,26 @@ replace_dot_alias = function(e) {
21922194
if (q[[1L]] == 'if') {
21932195
#explicit NULL would return NULL, assigning NULL would delete that from the expression
21942196
if (!is.null(q[[3L]])) {
2195-
ans = .dt_do_j_names(q[[3L]], jvnames)
2197+
ans = .do_j_names(q[[3L]], jvnames)
21962198
q[[3L]] = ans$jsub
21972199
jvnames = ans$jvnames
21982200
}
21992201
if (length(q) == 4L && !is.null(q[[4L]])) {
2200-
ans = .dt_do_j_names(q[[4L]], jvnames)
2202+
ans = .do_j_names(q[[4L]], jvnames)
22012203
q[[4L]] = ans$jsub
22022204
jvnames = ans$jvnames
22032205
}
22042206
return(list(jsub=q, jvnames=jvnames))
22052207
}
22062208
list(jsub=q, jvnames=jvnames)
22072209
}
2210+
class(.do_j_names) = "dt_query_helper"
22082211

2209-
.dt_suppPrint = function(x) { .global$print=address(x); x }
2212+
.suppPrint = function(x) { .global$print=address(x); x }
2213+
class(.suppPrint) = "dt_query_helper"
22102214

22112215
# can jval be sorted by the same key as x? improved for #4498
2212-
.dt_get_shared_keys = function(jsub, jvnames, sdvars, key) {
2216+
.get_shared_keys = function(jsub, jvnames, sdvars, key) {
22132217
if (is.null(key)) return(NULL)
22142218
if (!((SD_only <- jsub == quote(.SD)) || jsub %iscall% "list")) return(NULL)
22152219
if (SD_only)
@@ -2222,38 +2226,43 @@ replace_dot_alias = function(e) {
22222226
if (!length(missing_keys)) return(jvnames[key_idx])
22232227
jvnames[head(key_idx, missing_keys[1L] - 1L)]
22242228
}
2229+
class(.get_shared_keys) = "dt_query_helper"
22252230

22262231
# GForce functions that return all rows instead of one per group
2227-
.dt_is_nrows = function(q) {
2232+
.is_nrows = function(q) {
22282233
nrow_funs = c("gshift")
22292234
if (!is.call(q)) return(FALSE)
22302235
if (q[[1L]] == "list") {
2231-
any(vapply(q, .dt_is_nrows, FALSE))
2236+
any(vapply(q, .is_nrows, FALSE))
22322237
} else {
22332238
q[[1L]] %chin% nrow_funs
22342239
}
22352240
}
2241+
class(.is_nrows) = "dt_query_helper"
22362242

22372243
# n of ghead/gtail(x, n) when n > 1, otherwise 0, #5060 #523
2238-
.dt_headTail_arg = function(q) {
2244+
.headTail_arg = function(q) {
22392245
if (length(q)==3L && length(q3 <- q[[3L]])==1L && is.numeric(q3) &&
22402246
(q[[1L]]) %chin% c("ghead", "gtail") && q3!=1L) q3
22412247
else 0L
22422248
}
2249+
class(.headTail_arg) = "dt_query_helper"
22432250

22442251
# unpack list of lists for nrows functions
2245-
.dt_zip_items = function(ll) do.call(mapply, c(list(FUN = c), ll, SIMPLIFY=FALSE, USE.NAMES=FALSE))
2252+
.zip_items = function(ll) do.call(mapply, c(list(FUN = c), ll, SIMPLIFY=FALSE, USE.NAMES=FALSE))
2253+
class(.zip_items) = "dt_query_helper"
22462254

22472255
# unlock any locked data.table components of the answer, #4159
22482256
# max_depth prevents possible infinite recursion from truly recursive object, #4173
22492257
# TODO: is there an efficient way to get around this max_depth limit?
2250-
.dt_runlock = function(x, current_depth = 1L, max_depth = 5L) {
2258+
.runlock = function(x, current_depth = 1L, max_depth = 5L) {
22512259
if (is.list(x) && current_depth <= max_depth) { # is.list() used to be is.recursive(), #4814
22522260
if (inherits(x, 'data.table')) .Call(C_unlock, x)
2253-
else return(lapply(x, .dt_runlock, current_depth = current_depth + 1L, max_depth = max_depth))
2261+
else return(lapply(x, .runlock, current_depth = current_depth + 1L, max_depth = max_depth))
22542262
}
22552263
invisible()
22562264
}
2265+
class(.runlock) = "dt_query_helper"
22572266

22582267
# can the specified merge of x and i be marked as sorted? return the columns for which this is true, otherwise NULL
22592268
.join_result_key <- function(x, i, ans, on_lhs, ansvars, leftcols, rightcols, names_i, irows, roll) {

‎R/translation.R‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ catf = function(fmt, ..., sep=" ", domain="R-data.table") {
66

77
raise_condition = function(signal, message, classes, immediate=FALSE, appendLF=FALSE) {
88
n = sys.nframe() - 2L # the caller of stopf() etc.
9-
# helpers extracted from [.data.table are named .dt_*; their conditions report the call of [.data.table (or whoever called them)
9+
# helpers extracted from [.data.table carry class "dt_query_helper"; their conditions report the call of [.data.table (or whoever called them)
1010
parents = sys.parents()
11-
while (n > 0L && parents[n] > 0L && .is_dt_helper_call(sys.call(n))) n = parents[n]
11+
while (n > 0L && parents[n] > 0L && inherits(sys.function(n), "dt_query_helper")) n = parents[n]
1212
obj = list(message=message, call=sys.call(n))
1313
# NB: append _after_ translation
1414
if (appendLF) obj$message = paste0(obj$message, "\n")
@@ -21,8 +21,6 @@ raise_condition = function(signal, message, classes, immediate=FALSE, appendLF=F
2121
signal(obj)
2222
}
2323

24-
.is_dt_helper_call = function(call) is.call(call) && is.name(f <- call[[1L]]) && startsWith(as.character(f), ".dt_")
25-
2624
stopf = function(fmt, ..., class=NULL, domain="R-data.table") {
2725
raise_condition(stop, gettextf(fmt, ..., domain=domain), c(class, "simpleError", "error", "condition"))
2826
}

0 commit comments

Comments
 (0)