Skip to content

Commit

Permalink
fix dependency sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
snixtho committed Sep 29, 2024
1 parent 807fc5e commit b348b24
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions src/EvoSC.Modules/Util/SortedModuleCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,54 +33,57 @@ IEnumerator IEnumerable.GetEnumerator()

private List<T> GetSortedModules()
{
var dependencyGraph = MakeDependencyGraph();
EnsureDependenciesExists(dependencyGraph);
var graph = MakeDependencyGraph();
EnsureDependenciesExists(graph);

var sortedDependencies = new List<T>();

while (true)
{
string? selected = null;

foreach (var dependent in dependencyGraph)
bool found = false;
foreach (var node in graph)
{
if (dependent.Value.Count != 0)
if (node.Value.Count > 0)
{
continue;
}

sortedDependencies.Add(_modules[dependent.Key]);
RemoveDependent(dependencyGraph, dependent);

selected = dependent.Key;

sortedDependencies.Add(_modules[node.Key]);
RemoveNodeReferences(graph, node.Key);
found = true;
}

if (selected == null)
if (!found)
{
break;
}
}

DetectCycle(dependencyGraph);

if (graph.Count > 0)
{
throw new DependencyCycleException(graph);
}

return sortedDependencies;
}

private static void RemoveDependent(DependencyGraph dependencyGraph, KeyValuePair<string, IList<string>> dependent)
private static void RemoveNodeReferences(DependencyGraph graph, string nodeName)
{
foreach (var dependencies in dependencyGraph)
if (graph.ContainsKey(nodeName))
{
if (dependencies.Value.Contains(dependencies.Key))
{
dependencies.Value.Remove(dependent.Key);
}
graph.Remove(nodeName);
}

if (dependencyGraph.ContainsKey(dependent.Key))
foreach (var node in graph)
{
dependencyGraph.Remove(dependent.Key);
if (node.Value.Contains(nodeName))
{
node.Value.Remove(nodeName);
}
}
}

private DependencyGraph MakeDependencyGraph()
{
var adjList = new Dictionary<string, IList<string>>();
Expand Down

0 comments on commit b348b24

Please sign in to comment.