2 // DependencyGraph.cs: linker dependencies graph
5 // Radek Doulik (rodo@xamarin.com)
7 // Copyright 2015 Xamarin Inc (http://www.xamarin.com).
10 using System.Collections.Generic;
12 using System.IO.Compression;
15 namespace LinkerAnalyzer.Core
17 public class VertexData {
19 public List<int> parentIndexes;
22 public string DepsCount {
24 if (parentIndexes == null || parentIndexes.Count < 1)
26 return string.Format (" [{0} deps]", parentIndexes.Count);
31 public class DependencyGraph
33 protected List<VertexData> vertices = new List<VertexData> ();
34 public List<VertexData> Types = new List<VertexData> ();
35 Dictionary<string, int> indexes = new Dictionary<string, int> ();
36 protected Dictionary<string, int> counts = new Dictionary<string, int> ();
38 public void Load (string filename)
40 Console.WriteLine ("Loading dependency tree from: {0}", filename);
42 using (var fileStream = File.OpenRead (filename))
43 using (var zipStream = new GZipStream (fileStream, CompressionMode.Decompress)) {
47 Console.WriteLine ("Unable to open and read the dependecies.");
53 void Load (GZipStream zipStream) {
54 using (XmlReader reader = XmlReader.Create (zipStream)) {
55 while (reader.Read ()) {
56 switch (reader.NodeType) {
57 case XmlNodeType.Element:
58 //Console.WriteLine (reader.Name);
59 if (reader.Name == "edge" && reader.IsStartElement ()) {
60 string b = reader.GetAttribute ("b");
61 string e = reader.GetAttribute ("e");
62 //Console.WriteLine ("edge value " + b + " --> " + e);
65 VertexData begin = Vertex (b, true);
66 VertexData end = Vertex (e, true);
68 if (end.parentIndexes == null)
69 end.parentIndexes = new List<int> ();
70 if (!end.parentIndexes.Contains (begin.index)) {
71 end.parentIndexes.Add (begin.index);
72 //Console.WriteLine (" end parent index: {0}", end.parentIndexes);
78 //Console.WriteLine ("node: " + reader.NodeType);
85 public VertexData Vertex (string vertexName, bool create = false)
90 vertex = vertices [indexes [vertexName]];
91 } catch (KeyNotFoundException) {
93 int index = vertices.Count;
94 vertex = new VertexData () { value = vertexName, index = index };
95 vertices.Add (vertex);
96 indexes.Add (vertexName, index);
97 string prefix = vertexName.Substring (0, vertexName.IndexOf (':'));
98 if (counts.ContainsKey (prefix))
102 //Console.WriteLine ("prefix " + prefix + " count " + counts[prefix]);
103 if (prefix == "TypeDef") {
113 public VertexData Vertex (int index)
115 return vertices [index];