From 7dc5369ab0bbacd52049a8e44447b01eed501db7 Mon Sep 17 00:00:00 2001 From: Maximilian Muecke Date: Sun, 14 Sep 2025 13:29:36 +0200 Subject: [PATCH] refactor(topo): match data.table style to mlr3 --- R/topo_sort.R | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/R/topo_sort.R b/R/topo_sort.R index b673809d..d4ee9a72 100644 --- a/R/topo_sort.R +++ b/R/topo_sort.R @@ -33,30 +33,28 @@ topo_sort = function(nodes) { nodes = copy(nodes) # copy ref to be sure n = nrow(nodes) # sort nodes with few parent to start - nodes = nodes[order(lengths(parents), decreasing = FALSE)] + nodes = nodes[order(lengths(get("parents")), decreasing = FALSE)] nodes[, `:=`(topo = NA_integer_, depth = NA_integer_)] # cols for topo-index and depth layer in sort j = 1L topo_count = 1L depth_count = 0L - topo = depth = parents = id = NULL - . = NULL # nolint while (topo_count <= n) { # if element is not sorted and has no deps (anymore), we sort it in if (is.na(nodes$topo[j]) && length(nodes$parents[[j]]) == 0L) { - nodes[j, topo := topo_count] + nodes[j, "topo" := topo_count] topo_count = topo_count + 1L - nodes[j, depth := depth_count] + nodes[j, "depth" := depth_count] } j = (j %% n) + 1L # inc j, but wrap around end if (j == 1L) { # we wrapped, lets remove nodes of current layer from deps - layer = nodes[.(depth_count), id, on = "depth", nomatch = NULL] + layer = nodes[list(depth_count), get("id"), on = "depth", nomatch = NULL] if (length(layer) == 0L) { stop("Cycle detected, this is not a DAG!") } - nodes[, parents := map(parents, function(x) setdiff(x, layer))] + nodes[, "parents" := map(get("parents"), function(x) setdiff(x, layer))] depth_count = depth_count + 1L } } - nodes[order(topo), c("id", "depth")] # sort by topo, and then remove topo-col + nodes[order(get("topo")), c("id", "depth")] # sort by topo, and then remove topo-col }