2019-09-03 19:11:21 +03:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
// Directed acyclic graph
|
|
|
|
// this implementation is specifically suited to ordering dependencies
|
2019-10-13 16:37:43 +03:00
|
|
|
module compiler
|
2019-09-03 19:11:21 +03:00
|
|
|
|
2019-12-20 00:29:37 +03:00
|
|
|
struct DepGraphNode {
|
2019-09-03 19:11:21 +03:00
|
|
|
mut:
|
|
|
|
name string
|
|
|
|
deps []string
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DepGraph {
|
2020-01-23 23:08:17 +03:00
|
|
|
pub mut:
|
2019-09-03 19:11:21 +03:00
|
|
|
acyclic bool
|
|
|
|
nodes []DepGraphNode
|
|
|
|
}
|
|
|
|
|
2020-01-23 23:08:17 +03:00
|
|
|
struct OrderedDepMap {
|
2019-12-20 00:29:37 +03:00
|
|
|
mut:
|
2020-01-23 23:08:17 +03:00
|
|
|
keys []string
|
|
|
|
data map[string][]string
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (o mut OrderedDepMap) set(name string, deps []string) {
|
|
|
|
if !(name in o.data) {
|
|
|
|
o.keys << name
|
|
|
|
}
|
|
|
|
o.data[name] = deps
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (o mut OrderedDepMap) add(name string, deps []string) {
|
|
|
|
mut d := o.data[name]
|
|
|
|
for dep in deps {
|
|
|
|
if !(dep in d) {
|
|
|
|
d << dep
|
|
|
|
}
|
|
|
|
}
|
|
|
|
o.set(name, d)
|
2019-09-03 19:11:21 +03:00
|
|
|
}
|
|
|
|
|
2020-01-23 23:08:17 +03:00
|
|
|
pub fn (o &OrderedDepMap) get(name string) []string {
|
|
|
|
return o.data[name]
|
2019-09-03 19:11:21 +03:00
|
|
|
}
|
|
|
|
|
2020-01-23 23:08:17 +03:00
|
|
|
pub fn (o mut OrderedDepMap) delete(name string) {
|
|
|
|
if !(name in o.data) {
|
|
|
|
panic('delete: no such key: $name')
|
2019-12-20 00:29:37 +03:00
|
|
|
}
|
2020-01-23 23:08:17 +03:00
|
|
|
for i, _ in o.keys {
|
|
|
|
if o.keys[i] == name {
|
|
|
|
o.keys.delete(i)
|
|
|
|
break
|
2019-09-03 19:11:21 +03:00
|
|
|
}
|
|
|
|
}
|
2020-01-23 23:08:17 +03:00
|
|
|
o.data.delete(name)
|
2019-09-03 19:11:21 +03:00
|
|
|
}
|
|
|
|
|
2020-01-23 23:08:17 +03:00
|
|
|
pub fn (o mut OrderedDepMap) apply_diff(name string, deps []string) {
|
|
|
|
mut diff := []string
|
|
|
|
for dep in o.data[name] {
|
|
|
|
if !(dep in deps) {
|
|
|
|
diff << dep
|
|
|
|
}
|
|
|
|
}
|
|
|
|
o.set(name, diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (o &OrderedDepMap) size() int {
|
|
|
|
return o.data.size
|
2019-09-03 19:11:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_dep_graph() &DepGraph {
|
|
|
|
return &DepGraph{
|
|
|
|
acyclic: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 00:29:37 +03:00
|
|
|
pub fn (graph mut DepGraph) add(mod string, deps []string) {
|
2019-09-03 19:11:21 +03:00
|
|
|
graph.nodes << DepGraphNode{
|
2019-12-20 00:29:37 +03:00
|
|
|
name: mod
|
2019-09-08 02:53:33 +03:00
|
|
|
deps: deps.clone()
|
2019-09-03 19:11:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 00:29:37 +03:00
|
|
|
pub fn (graph &DepGraph) resolve() &DepGraph {
|
2020-01-23 23:08:17 +03:00
|
|
|
mut node_names := OrderedDepMap{}
|
|
|
|
for node in graph.nodes {
|
|
|
|
node_names.add(node.name, node.deps)
|
2019-09-03 19:11:21 +03:00
|
|
|
}
|
2020-01-23 23:08:17 +03:00
|
|
|
mut node_deps := node_names
|
2019-09-03 19:11:21 +03:00
|
|
|
mut resolved := new_dep_graph()
|
2020-01-23 23:08:17 +03:00
|
|
|
for node_deps.size() != 0 {
|
|
|
|
mut ready_set := []string
|
|
|
|
for name in node_deps.keys {
|
|
|
|
deps := node_deps.data[name]
|
|
|
|
if deps.len == 0 {
|
|
|
|
ready_set << name
|
2019-09-03 19:11:21 +03:00
|
|
|
}
|
|
|
|
}
|
2020-01-23 23:08:17 +03:00
|
|
|
if ready_set.len == 0 {
|
2019-09-03 19:11:21 +03:00
|
|
|
mut g := new_dep_graph()
|
|
|
|
g.acyclic = false
|
2020-01-23 23:08:17 +03:00
|
|
|
for name in node_deps.keys {
|
|
|
|
g.add(name, node_names.data[name])
|
2019-09-03 19:11:21 +03:00
|
|
|
}
|
|
|
|
return g
|
|
|
|
}
|
2020-01-23 23:08:17 +03:00
|
|
|
for name in ready_set {
|
2019-09-03 19:11:21 +03:00
|
|
|
node_deps.delete(name)
|
2020-01-23 23:08:17 +03:00
|
|
|
resolved.add(name, node_names.data[name])
|
2019-09-03 19:11:21 +03:00
|
|
|
}
|
2020-01-23 23:08:17 +03:00
|
|
|
for name in node_deps.keys {
|
|
|
|
node_deps.apply_diff(name, ready_set)
|
2019-09-03 19:11:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return resolved
|
|
|
|
}
|
|
|
|
|
2019-12-20 00:29:37 +03:00
|
|
|
pub fn (graph &DepGraph) last_node() DepGraphNode {
|
|
|
|
return graph.nodes[graph.nodes.len - 1]
|
2019-09-03 19:11:21 +03:00
|
|
|
}
|
|
|
|
|
2019-12-20 00:29:37 +03:00
|
|
|
pub fn (graph &DepGraph) display() string {
|
2019-10-24 07:53:39 +03:00
|
|
|
mut out := '\n'
|
|
|
|
for node in graph.nodes {
|
|
|
|
for dep in node.deps {
|
|
|
|
out += ' * $node.name -> $dep\n'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out
|
2019-09-03 19:11:21 +03:00
|
|
|
}
|
|
|
|
|
2019-12-20 00:29:37 +03:00
|
|
|
pub fn (graph &DepGraph) display_cycles() string {
|
2019-10-24 07:53:39 +03:00
|
|
|
mut node_names := map[string]DepGraphNode
|
|
|
|
for node in graph.nodes {
|
|
|
|
node_names[node.name] = node
|
|
|
|
}
|
|
|
|
mut out := '\n'
|
|
|
|
for node in graph.nodes {
|
2019-09-03 19:11:21 +03:00
|
|
|
for dep in node.deps {
|
2019-12-20 00:29:37 +03:00
|
|
|
if !(dep in node_names) {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-24 07:53:39 +03:00
|
|
|
dn := node_names[dep]
|
|
|
|
if node.name in dn.deps {
|
|
|
|
out += ' * $node.name -> $dep\n'
|
2019-09-03 19:11:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-24 07:53:39 +03:00
|
|
|
return out
|
2019-09-03 19:11:21 +03:00
|
|
|
}
|