2using System.Collections.Generic;
3using System.Collections.ObjectModel;
12 public ISet<T>
set =
new HashSet<T>();
13 public IList<List<T>>
cycles =
new List<List<T>>();
15 private string CycleToString(List<T> cycle) =>
"Dependency Cycle: " +
string.Join(
" -> ", cycle);
18 public void Add(List<T> cycle) {
20 foreach (var e
in cycle)
25 public readonly ReadOnlyCollection<T>
list;
26 private IDictionary<T, List<T>>
dependencyDict =
new Dictionary<T, List<T>>();
27 private IDictionary<T, List<T>>
dependentDict =
new Dictionary<T, List<T>>();
30 list = elements.ToList().AsReadOnly();
31 if (dependencies !=
null)
32 foreach (var t
in list)
33 foreach (var dependency
in dependencies(t))
36 if (dependents !=
null)
37 foreach (var t
in list)
38 foreach (var dependent
in dependents(t))
42 public void AddEntry(T dependency, T dependent) {
51 private static void BuildSet(T t, IDictionary<T, List<T>> dict, ISet<T>
set) {
53 if (!dict.TryGetValue(t, out
list))
56 foreach (var entry
in dict[t])
72 var
set =
new HashSet<T>();
78 var
set =
new HashSet<T>();
85 var visiting =
new Stack<T>();
86 var sorted =
new List<T>();
88 Action<T> Visit =
null;
90 if (sorted.Contains(t) || ex.set.Contains(t))
95 if (visiting.Contains(dependency)) {
96 var cycle =
new List<T>();
97 cycle.Add(dependency);
98 cycle.AddRange(visiting.TakeWhile(entry => !EqualityComparer<T>.Default.Equals(entry, dependency)));
99 cycle.Add(dependency);
111 foreach (var t
in list)
114 if (ex.set.Count > 0)
void Add(List< T > cycle)
IList< List< T > > cycles
string CycleToString(List< T > cycle)
void AddEntry(T dependency, T dependent)
IDictionary< T, List< T > > dependentDict
IDictionary< T, List< T > > dependencyDict
ISet< T > AllDependendents(T t)
static void BuildSet(T t, IDictionary< T, List< T > > dict, ISet< T > set)
readonly ReadOnlyCollection< T > list
TopoSort(IEnumerable< T > elements, Func< T, IEnumerable< T > > dependencies=null, Func< T, IEnumerable< T > > dependents=null)
List< T > Dependents(T t)
ISet< T > AllDependencies(T t)
List< T > Dependencies(T t)
@ Environment
Sandstorm, Hell, Above surface during Eclipse, Space