Skip to content

Commit e4521a1

Browse files
committed
rewrite set
1 parent f95c1c4 commit e4521a1

File tree

2 files changed

+7
-18
lines changed

2 files changed

+7
-18
lines changed

R/data.table.R

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2713,15 +2713,13 @@ setcolorder = function(x, neworder=key(x), before=NULL, after=NULL) # before/af
27132713
invisible(x)
27142714
}
27152715

2716-
set = function(x,i=NULL,j,value) # low overhead, loopable
2717-
{
2718-
if (is.character(j)) {
2719-
name = substitute(x)
2720-
x = .Call(Cassign,x,i,j,NULL,value)
2721-
if (is.name(name))
2722-
assign(as.character(name),x,parent.frame(),inherits=TRUE)
2723-
} else {
2724-
.Call(Cassign,x,i,j,NULL,value)
2716+
set = function(x, i = NULL, j, value) {
2717+
name = as.chracter(substitute(x))
2718+
old_add = address(x)
2719+
x = .Call(Cassign, x, i, j, NULL, value)
2720+
if (old_add != address(x)) {
2721+
# assign is needed to replace x on address change due to possible new allocation
2722+
assign(name, x, envir = parent.frame(), inherits = TRUE)
27252723
}
27262724
invisible(x)
27272725
}

inst/tests/tests.Rraw

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18703,12 +18703,3 @@ test(2234.7, DT[, min(.SD), by=c(.I)], data.table(I=1L:5L, V1=c(1L, 2L, 3L, 2L,
1870318703
test(2234.8, DT[, min(.SD), by=.I%%2L], error="by.*contains .I.*supported") # would be nice to support in future; i.e. by odd/even rows, and by=(.I+1L)%/%2L for pairs of rows; i.e. any expression of .I
1870418704
test(2234.9, DT[, min(.SD), by=somefun(.I)], error="by.*contains .I.*supported")
1870518705

18706-
# re-overallocate in set if quota is reached #496 #4100
18707-
opt = options(datatable.alloccol=1L)
18708-
n = getOption("datatable.alloccol")
18709-
DT = data.table()
18710-
for (i in seq(10*n)) set(DT, j = paste0("V",i), value = i)
18711-
test(2235.1, ncol(DT), 10L*n)
18712-
DT = structure(list(a = 1, b = 2), class = c("data.table", "data.frame"))
18713-
test(2235.2, set(DT, j="c", value=3), data.table(a=1, b=2, c=3))
18714-
options(opt)

0 commit comments

Comments
 (0)