1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

compiler: fix depgraph. will also make it work with new b-tree map

This commit is contained in:
joe-conigliaro 2020-01-24 07:08:17 +11:00 committed by Alexander Medvednikov
parent 20f6cdc53a
commit 6d6b2fdda8
4 changed files with 78 additions and 51 deletions

View File

@ -12,34 +12,63 @@ mut:
} }
struct DepGraph { struct DepGraph {
// pub: pub mut:
mut:
acyclic bool acyclic bool
nodes []DepGraphNode nodes []DepGraphNode
} }
struct DepSet { struct OrderedDepMap {
mut: mut:
items []string keys []string
data map[string][]string
} }
pub fn (dset mut DepSet) add(item string) { pub fn (o mut OrderedDepMap) set(name string, deps []string) {
dset.items << item if !(name in o.data) {
o.keys << name
}
o.data[name] = deps
} }
pub fn (dset &DepSet) diff(otherset DepSet) DepSet { pub fn (o mut OrderedDepMap) add(name string, deps []string) {
mut diff := DepSet{ mut d := o.data[name]
} for dep in deps {
for item in dset.items { if !(dep in d) {
if !item in otherset.items { d << dep
diff.items << item
} }
} }
return diff o.set(name, d)
} }
pub fn (dset &DepSet) size() int { pub fn (o &OrderedDepMap) get(name string) []string {
return dset.items.len return o.data[name]
}
pub fn (o mut OrderedDepMap) delete(name string) {
if !(name in o.data) {
panic('delete: no such key: $name')
}
for i, _ in o.keys {
if o.keys[i] == name {
o.keys.delete(i)
break
}
}
o.data.delete(name)
}
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
} }
pub fn new_dep_graph() &DepGraph { pub fn new_dep_graph() &DepGraph {
@ -56,40 +85,34 @@ pub fn (graph mut DepGraph) add(mod string, deps []string) {
} }
pub fn (graph &DepGraph) resolve() &DepGraph { pub fn (graph &DepGraph) resolve() &DepGraph {
mut node_names := map[string]DepGraphNode mut node_names := OrderedDepMap{}
mut node_deps := map[string]DepSet for node in graph.nodes {
for _, node in graph.nodes { node_names.add(node.name, node.deps)
node_names[node.name] = node
mut dep_set := DepSet{
}
for _, dep in node.deps {
dep_set.add(dep)
}
node_deps[node.name] = dep_set
} }
mut node_deps := node_names
mut resolved := new_dep_graph() mut resolved := new_dep_graph()
for node_deps.size != 0 { for node_deps.size() != 0 {
mut ready_set := DepSet{ mut ready_set := []string
} for name in node_deps.keys {
for name, deps in node_deps { deps := node_deps.data[name]
if deps.size() == 0 { if deps.len == 0 {
ready_set.add(name) ready_set << name
} }
} }
if ready_set.size() == 0 { if ready_set.len == 0 {
mut g := new_dep_graph() mut g := new_dep_graph()
g.acyclic = false g.acyclic = false
for name, _ in node_deps { for name in node_deps.keys {
g.nodes << node_names[name] g.add(name, node_names.data[name])
} }
return g return g
} }
for name in ready_set.items { for name in ready_set {
node_deps.delete(name) node_deps.delete(name)
resolved.nodes << node_names[name] resolved.add(name, node_names.data[name])
} }
for name, deps in node_deps { for name in node_deps.keys {
node_deps[name] = deps.diff(ready_set) node_deps.apply_diff(name, ready_set)
} }
} }
return resolved return resolved
@ -128,4 +151,3 @@ pub fn (graph &DepGraph) display_cycles() string {
} }
return out return out
} }

View File

@ -1,9 +1,5 @@
module filepath module filepath
import (
os
)
// ext returns the extension in the file `path`. // ext returns the extension in the file `path`.
pub fn ext(path string) string { pub fn ext(path string) string {
pos := path.last_index('.') or { pos := path.last_index('.') or {
@ -28,15 +24,12 @@ pub fn join(base string, dirs ...string) string {
for d in dirs { for d in dirs {
result << d result << d
} }
return result.join(os.path_separator) return result.join(path_separator)
} }
// dir returns all but the last element of path, typically the path's directory. // dir returns all but the last element of path, typically the path's directory.
pub fn dir(path string) string { pub fn dir(path string) string {
if path == '.' { pos := path.last_index(path_separator) or {
return os.getwd()
}
pos := path.last_index(os.path_separator) or {
return '.' return '.'
} }
return path[..pos] return path[..pos]
@ -44,7 +37,7 @@ pub fn dir(path string) string {
// basedir returns a directory name from path // basedir returns a directory name from path
pub fn basedir(path string) string { pub fn basedir(path string) string {
pos := path.last_index(os.path_separator) or { pos := path.last_index(path_separator) or {
return path return path
} }
// NB: *without* terminating / // NB: *without* terminating /
@ -53,5 +46,5 @@ pub fn basedir(path string) string {
// filename returns a file name from path // filename returns a file name from path
pub fn filename(path string) string { pub fn filename(path string) string {
return path.all_after(os.path_separator) return path.all_after(path_separator)
} }

View File

@ -0,0 +1,6 @@
module filepath
const (
path_separator = '/'
)

View File

@ -0,0 +1,6 @@
module filepath
const (
path_separator = '\\'
)