+
Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions R/topo_sort.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载