forked from devcat-studio/VSCodeLuaDebug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDependencyGraph.cs
More file actions
86 lines (68 loc) · 2.49 KB
/
Copy pathDependencyGraph.cs
File metadata and controls
86 lines (68 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GiderosPlayerRemote
{
class DependencyGraph
{
class Vertex
{
public Vertex(string code, bool excludeFromExecution)
{
this.code = code;
this.excludeFromExecution = excludeFromExecution;
this.visited = false;
}
public string code;
public bool excludeFromExecution;
public bool visited;
public HashSet<Vertex> dependencies = new HashSet<Vertex>();
}
Dictionary<KeyValuePair<int, string>, Vertex> vertices = new Dictionary<KeyValuePair<int, string>, Vertex>();
public void Clear()
{
vertices.Clear();
}
public void AddCode(string code, bool excludeFromExecution)
{
vertices[_(code)] = new Vertex(code, excludeFromExecution);
}
public void AddDependency(string code0, string code1)
{
Vertex vertex0 = vertices.FindR(_(code0));
Vertex vertex1 = vertices.FindR(_(code1));
vertex0.dependencies.Add(vertex1);
}
public List<KeyValuePair<string, bool>> TopologicalSort()
{
List<KeyValuePair<string, bool>> result = new List<KeyValuePair<string, bool>>();
foreach (var v in vertices)
v.Value.visited = false;
var sortedVertices = vertices
.OrderBy(a => a.Key.Key.ToString() + a.Key.Value, StringComparer.Ordinal)
.Select(a => a.Value);
foreach (var v in sortedVertices)
TopologicalSortHelper(v, result);
return result;
}
void TopologicalSortHelper(Vertex vertex, List<KeyValuePair<string, bool>> result)
{
if (vertex.visited == true)
return;
vertex.visited = true;
foreach (var iter in vertex.dependencies)
TopologicalSortHelper(iter, result);
result.Add(new KeyValuePair<string, bool>(vertex.code, vertex.excludeFromExecution));
}
KeyValuePair<int, string> _(string str)
{
if (str == "init.lua")
return new KeyValuePair<int, string>(0, str);
if (str == "main.lua")
return new KeyValuePair<int, string>(2, str);
return new KeyValuePair<int, string>(1, str);
}
}
}