Last active
May 4, 2016 06:54
-
-
Save typeless/bc55a4303fb017ea901e89c617433dd0 to your computer and use it in GitHub Desktop.
Deptree - Generating symbol dependency and accumulative size information in JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"encoding/json" | |
"fmt" | |
"io" | |
"log" | |
"os" | |
) | |
type Dep struct { | |
Size int64 | |
Parent string | |
Symbol string | |
} | |
type adep struct { | |
Dep | |
Acc int64 | |
Lv int64 | |
} | |
type depmap map[string][]string | |
func main() { | |
w := bufio.NewWriter(os.Stdout) | |
enc := json.NewEncoder(w) | |
dec := json.NewDecoder(bufio.NewReader(os.Stdin)) | |
//deps := []dep{} | |
//adeps := []adep{} | |
symmap := map[string]adep{} | |
tree := depmap{} | |
for { | |
d := adep{} | |
if err := dec.Decode(&d.Dep); err == io.EOF { | |
break | |
} else if err != nil { | |
log.Fatal(err) | |
} | |
//deps = append(deps, d) | |
symmap[d.Symbol] = d | |
} | |
// Create a tree | |
fmt.Println(len(symmap)) | |
for _, v := range symmap { | |
xs := tree[v.Parent] | |
xs = append(xs, v.Symbol) | |
tree[v.Parent] = xs | |
} | |
//accmap := map[string]int64{} | |
dfs := []string{} | |
var postorder func(string, depmap) int64 | |
postorder = func(root string, d depmap) (acc int64) { | |
for _, v := range d[root] { | |
acc += postorder(v, d) | |
} | |
acc += symmap[root].Size | |
v := symmap[root] | |
v.Acc = acc | |
symmap[root] = v | |
dfs = append(dfs, root) | |
return acc | |
} | |
_ = postorder("_", tree) | |
fmt.Printf("{\"dfs\":%d}\n", len(dfs)) | |
// Create levels | |
var label func(x string, d depmap, lv int64) | |
label = func(x string, d depmap, lv int64) { | |
for _, v := range d[x] { | |
label(v, d, lv+1) | |
} | |
v := symmap[x] | |
v.Lv = lv | |
symmap[x] = v | |
} | |
label("_", tree, 0) | |
fmt.Printf("{\"level\":%d}\n", len(dfs)) | |
// BFS | |
//bfs := []adep{} | |
bfs := []string{} | |
bfsExt := []adep{} | |
x, q := "", []string{"_"} | |
for len(q) > 0 { | |
x, q = q[0], q[1:] | |
for _, v := range tree[x] { | |
q = append(q, v) | |
} | |
bfs = append(bfs, x) | |
bfsExt = append(bfsExt, symmap[x]) | |
} | |
fmt.Printf("{\"bfs\":%d}\n", len(bfs)) | |
fmt.Printf("{\"bfsExt\":%d}\n", len(bfsExt)) | |
//enc.Encode(deps) | |
//enc.Encode(symmap) | |
//enc.Encode(tree) | |
//enc.Encode(bfs) | |
enc.Encode(bfsExt) | |
//enc.Encode(bfs) | |
//enc.Encode(symmap) | |
w.Flush() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment