Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix dependency sorting #297

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading