-
Notifications
You must be signed in to change notification settings - Fork 4
/
DependencySorter.cs
60 lines (53 loc) · 1.39 KB
/
DependencySorter.cs
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
using System;
using System.IO;
using System.Collections.Generic;
namespace Nima
{
public class DependencySorter
{
private HashSet<ActorComponent> m_Perm;
private HashSet<ActorComponent> m_Temp;
private IList<ActorComponent> m_Order;
public DependencySorter()
{
m_Perm = new HashSet<ActorComponent>();
m_Temp = new HashSet<ActorComponent>();
}
public IList<ActorComponent> Sort(ActorComponent root)
{
m_Order = new List<ActorComponent>();
if(!Visit(root))
{
return null;
}
return m_Order;
}
private bool Visit(ActorComponent n)
{
if(m_Perm.Contains(n))
{
return true;
}
if(m_Temp.Contains(n))
{
Console.WriteLine("Dependency cycle!");
return false;
}
m_Temp.Add(n);
IList<ActorComponent> dependents = n.m_Dependents;
if(dependents != null)
{
foreach(ActorComponent d in dependents)
{
if(!Visit(d))
{
return false;
}
}
}
m_Perm.Add(n);
m_Order.Insert(0, n);
return true;
}
}
}