Skip to content

Commit 3215cb4

Browse files
Apply Ben's suggested changes
Co-authored-by: Benjamin Schwendinger <[email protected]>
1 parent fc134af commit 3215cb4

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

R/mergelist.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cbindlist = function(l, copy=TRUE) {
22
ans = .Call(Ccbindlist, l, copy)
33
if (anyDuplicated(names(ans))) { ## invalidate key and index
44
setattr(ans, "sorted", NULL)
5-
setattr(ans, "index", integer())
5+
setattr(ans, "index", NULL)
66
}
77
setDT(ans)
88
ans

inst/tests/mergelist.Rraw

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,6 @@ ans = cbindlist(l)
7070
test(13.04, key(ans), "id1")
7171
test(13.05, indices(ans), c("id1","id2","id3","id1__id2__id3","id6","id7","id9"))
7272
test(13.06, ii, lapply(l, indices)) ## this tests that original indices have not been touched, shallow_duplicate in mergeIndexAttrib
73+
ans = cbindlist(list(data.table(a=1:2), data.table(b=3:4, key="b")))
74+
test(13.07, ans, data.table(a=1:2, b=3:4, key="b"))
75+
test(13.08, key(ans), "b")

man/cbindlist.Rd

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
\code{\link{data.table}}, \code{\link{rbindlist}}
2727
}
2828
\examples{
29-
l = list(
30-
d1 = data.table(x=1:3, v1=1L),
31-
d2 = data.table(y=3:1, v2=2L),
32-
d3 = data.table(z=2:4, v3=3L)
33-
)
34-
cbindlist(l)
29+
d1 = data.table(x=1:3, v1=1L, key="x")
30+
d2 = data.table(y=3:1, v2=2L, key="y")
31+
d3 = data.table(z=2:4, v3=3L)
32+
cbindlist(list(d1, d2, d3))
33+
cbindlist(list(d1, d1))
34+
d4 = cbindlist(list(d1), copy=FALSE)
35+
d4[, v1:=2L]
36+
identical(d4, d1)
3537
}
3638
\keyword{ data }

src/mergelist.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ void mergeIndexAttrib(SEXP to, SEXP from) {
66
if (isNull(from))
77
return;
88
SEXP t = ATTRIB(to), f = ATTRIB(from);
9-
if (isNull(f))
9+
if (isNull(f)) // nothing to merge
1010
return;
11-
if (isNull(t))
11+
if (isNull(t)) // target has no attributes -> overwrite
1212
SET_ATTRIB(to, shallow_duplicate(f));
1313
else {
14-
for (t = ATTRIB(to); CDR(t) != R_NilValue; t = CDR(t));
14+
for (t = ATTRIB(to); CDR(t) != R_NilValue; t = CDR(t)); // traverse to end of attributes list of to
1515
SETCDR(t, shallow_duplicate(f));
1616
}
17-
return;
1817
}
1918

2019
SEXP cbindlist(SEXP x, SEXP copyArg) {
@@ -72,6 +71,8 @@ SEXP cbindlist(SEXP x, SEXP copyArg) {
7271
key = getAttrib(thisx, sym_sorted);
7372
UNPROTECT(protecti);
7473
}
74+
if (isNull(ATTRIB(index)))
75+
setAttrib(ans, sym_index, R_NilValue);
7576
setAttrib(ans, R_NamesSymbol, names);
7677
setAttrib(ans, sym_sorted, key);
7778
if (verbose)

0 commit comments

Comments
 (0)