Skip to content

Commit 985ef22

Browse files
more style tweaks
1 parent c3a450d commit 985ef22

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

R/mergelist.R

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ onkeys = function(x, y) {
2424
NULL # nocov. Internal error is being called later in mergepair
2525
}
2626

27+
# column index selection helper
2728
someCols = function(x, cols, drop=character(), keep=character(), retain.order=FALSE) {
2829
keep = colnamesInt(x, keep)
2930
drop = colnamesInt(x, drop)
@@ -36,7 +37,7 @@ someCols = function(x, cols, drop=character(), keep=character(), retain.order=FA
3637
hasindex = function(x, by, retGrp=FALSE) {
3738
index = attr(x, "index", TRUE)
3839
if (is.null(index)) return(FALSE)
39-
idx_name = paste0("__",by,collapse="")
40+
idx_name = paste0("__", by, collapse="")
4041
idx = attr(index, idx_name, TRUE)
4142
if (is.null(idx)) return(FALSE)
4243
if (!retGrp) return(TRUE)
@@ -46,7 +47,7 @@ hasindex = function(x, by, retGrp=FALSE) {
4647
# fdistinct applies mult='first|last'
4748
# for mult='first' it is unique(x, by=on)[, c(on, cols), with=FALSE]
4849
# it may not copy when copy=FALSE and x is unique by 'on'
49-
fdistinct = function(x, on=key(x), mult=c("first","last"), cols=seq_along(x), copy=TRUE) {
50+
fdistinct = function(x, on=key(x), mult=c("first", "last"), cols=seq_along(x), copy=TRUE) {
5051
if (!perhaps.data.table(x))
5152
stopf("'x' must be data.table")
5253
if (!is.character(on) || !length(on) || anyNA(on) || !all(on %chin% names(x)))
@@ -69,9 +70,9 @@ fdistinct = function(x, on=key(x), mult=c("first","last"), cols=seq_along(x), co
6970
return(ans)
7071
}
7172
f = attr(o, "starts", exact=TRUE)
72-
if (mult=="last") {
73+
if (mult == "last") {
7374
if (!sort) internal_error("sort must be TRUE when computing mult='last'") # nocov
74-
f = c(f[-1L]-1L, nrow(x)) ## last of each group
75+
f = c(f[-1L] - 1L, nrow(x)) ## last of each group
7576
}
7677
if (length(o)) f = o[f]
7778
if (sort && length(o <- forderv(f))) f = f[o] ## this rolls back to original order
@@ -81,35 +82,44 @@ fdistinct = function(x, on=key(x), mult=c("first","last"), cols=seq_along(x), co
8182
# extra layer over bmerge to provide ready to use row indices (or NULL for 1:nrow)
8283
# NULL to avoid extra copies in downstream code, it turned out that avoiding copies precisely is costly and enormously complicates code, need #4409 and/or handle 1:nrow in subsetDT
8384
dtmerge = function(x, i, on, how, mult, join.many, void=FALSE, verbose) {
84-
nomatch = switch(how, "inner"=, "semi"=, "anti"=, "cross"= 0L, "left"=, "right"=, "full"= NA_integer_)
85+
nomatch = switch(how,
86+
inner=, semi=, anti=, cross= 0L,
87+
left=, right=, full=NA_integer_)
8588
nomatch0 = identical(nomatch, 0L)
8689
if (is.null(mult))
87-
mult = switch(how, "semi"=, "anti"= "last", "cross"= "all", "inner"=, "left"=, "right"=, "full"= "error")
88-
if (void && mult!="error")
89-
internal_error("void must be used with mult='error'") # nocov
90-
if (how=="cross") { ## short-circuit bmerge results only for cross join
91-
if (length(on) || mult!="all" || !join.many)
90+
mult = switch(how,
91+
semi=, anti="last",
92+
cross="all",
93+
inner=, left=, right=, full="error")
94+
if (void && mult != "error")
95+
internal_error("'void' must be used with mult='error'") # nocov
96+
if (how == "cross") { ## short-circuit bmerge results only for cross join
97+
if (length(on) || mult != "all" || !join.many)
9298
stopf("cross join must be used with zero-length on, mult='all', join.many=TRUE")
9399
if (void)
94100
internal_error("cross join must be used with void=FALSE") # nocov
95101
ans = list(allLen1=FALSE, starts=rep.int(1L, nrow(i)), lens=rep.int(nrow(x), nrow(i)), xo=integer())
96102
} else {
97103
if (!length(on))
98104
stopf("'on' must be non-zero length character vector")
99-
if (mult=="all" && (how=="semi" || how=="anti"))
105+
if (mult == "all" && (how == "semi" || how == "anti"))
100106
stopf("semi and anti joins must be used with mult!='all'")
101107
icols = colnamesInt(i, on, check_dups=TRUE)
102108
xcols = colnamesInt(x, on, check_dups=TRUE)
103109
ans = bmerge(i, x, icols, xcols, roll=0, rollends=c(FALSE, TRUE), nomatch=nomatch, mult=mult, ops=rep.int(1L, length(on)), verbose=verbose)
104110
if (void) { ## void=T is only for the case when we want raise error for mult='error', and that would happen in above line
105111
return(invisible(NULL))
106-
} else if (how=="semi" || how=="anti") { ## semi and anti short-circuit
107-
irows = which(if (how=="semi") ans$lens!=0L else ans$lens==0L) ## we will subset i rather than x, thus assign to irows, not to xrows
108-
if (length(irows)==length(ans$lens)) irows = NULL
112+
} else if (how == "semi" || how == "anti") { ## semi and anti short-circuit
113+
## we will subset i rather than x, thus assign to irows, not to xrows
114+
if (how == "semi")
115+
irows = which(ans$lens != 0L)
116+
else
117+
irows = which(ans$lens == 0L)
118+
if (length(irows) == length(ans$lens)) irows = NULL
109119
return(list(ans=ans, irows=irows))
110-
} else if (mult=="all" && !ans$allLen1 && !join.many && ## join.many, like allow.cartesian, check
111-
!(length(ans$starts)==1L && ans$lens==nrow(x)) && ## special case of scalar i match to const duplicated x, not handled by anyDuplicate: data.table(x=c(1L,1L))[data.table(x=1L), on="x"]
112-
anyDuplicated(ans$starts, incomparables=c(0L,NA_integer_))
120+
} else if (mult == "all" && !ans$allLen1 && !join.many && ## join.many, like allow.cartesian, check
121+
!(length(ans$starts) == 1L && ans$lens == nrow(x)) && ## special case of scalar i match to const duplicated x, not handled by anyDuplicate: data.table(x=c(1L,1L))[data.table(x=1L), on="x"]
122+
anyDuplicated(ans$starts, incomparables=c(0L, NA_integer_))
113123
)
114124
stopf("Joining resulted in many-to-many join. Perform quality check on your data, use mult!='all', or set 'datatable.join.many' option to TRUE to allow rows explosion.")
115125
}
@@ -120,14 +130,14 @@ dtmerge = function(x, i, on, how, mult, join.many, void=FALSE, verbose) {
120130
len.x = length(xrows) ## as of now cannot optimize to NULL, search for #4409 here
121131

122132
## irows, join-from
123-
irows = if (!(ans$allLen1 && (!nomatch0 || len.x==length(ans$starts)))) seqexp(ans$lens)
133+
irows = if (!(ans$allLen1 && (!nomatch0 || len.x == length(ans$starts)))) seqexp(ans$lens)
124134
len.i = if (is.null(irows)) nrow(i) else length(irows)
125135

126136
if (length(ans$xo) && length(xrows))
127137
xrows = ans$xo[xrows]
128138
len.x = length(xrows)
129139

130-
if (len.i!=len.x)
140+
if (len.i != len.x)
131141
internal_error("dtmerge out len.i != len.x") # nocov
132142

133143
return(list(ans=ans, irows=irows, xrows=xrows))

0 commit comments

Comments
 (0)