File: order_struct_by_dependency.R

package info (click to toggle)
r-cran-hdf5r 1.3.3%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 12,344 kB
  • sloc: ansic: 85,341; sh: 51; python: 32; makefile: 13
file content (21 lines) | stat: -rw-r--r-- 1,102 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# we want to order the structs appropriately
order_struct_by_dependency <- function(struct_info) {
    # first we define all vertices
    all_types_used <- sort(c("_root", unique(c(struct_info$type, struct_info$category_name))))
    vertices <- data.frame(name=all_types_used, stringsAsFactors=FALSE)
    # now a data frame with all the edges
    # also note that each node should be connected to the root
    root_edges <- data.frame(from="_root", to=all_types_used, stringsAsFactors=FALSE)
    other_edges <- data.frame(from=struct_info$type, to=struct_info$category_name, stringsAsFactors=FALSE)
    all_edges <- rbind(root_edges, other_edges)

    g <- igraph::graph_from_data_frame(all_edges, directed=TRUE, vertices=vertices)
    # reorder
    g_topo_order <- igraph::topo_sort(g, mode="out")
    priority_df <- data.frame(category_name=names(g_topo_order), stringsAsFactors=FALSE)
    priority_df$priority <- seq_along(priority_df$category_name)
    struct_info <- dplyr::left_join(struct_info, priority_df) 
    struct_info <- dplyr::arrange(struct_info, priority)
    return(struct_info)  
}