-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Topographical sorting of compositions .
- Loading branch information
Showing
5 changed files
with
241 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace uSync.Core.Dependency; | ||
|
||
/// <summary> | ||
/// builds a graph of dependencies, so things can be installed in order. | ||
/// </summary> | ||
public static class DependencyGraph | ||
{ | ||
public static List<T> TopologicalSort<T>(this ICollection<T> nodes, ICollection<GraphEdge<T>> edges) | ||
where T : IEquatable<T> | ||
{ | ||
var sortedList = new List<T>(); | ||
|
||
// all items where they don't have a dependency on themselves. | ||
var queue = new Queue<T>( | ||
nodes.Where(x => edges.All(e => e.Node.Equals(x) == false))); | ||
|
||
|
||
while (queue.Any()) | ||
{ | ||
// remove this item add it to the queue. | ||
var next = queue.Dequeue(); | ||
sortedList.Add(next); | ||
|
||
// look for any edges for this queue. | ||
foreach(var edge in edges.Where(e => e.Edge.Equals(next)).ToList()) | ||
{ | ||
var dependency = edge.Node; | ||
edges.Remove(edge); | ||
|
||
if (edges.All(x => x.Node.Equals(dependency) == false)) | ||
{ | ||
queue.Enqueue(dependency); | ||
} | ||
} | ||
} | ||
|
||
if (edges.Any()) | ||
{ | ||
return null; | ||
} | ||
|
||
return sortedList; | ||
|
||
} | ||
} | ||
|
||
public class GraphEdge<T> | ||
where T : IEquatable<T> | ||
{ | ||
public T Node { get; set; } | ||
public T Edge { get; set; } | ||
|
||
} | ||
|
||
public class GraphEdge | ||
{ | ||
public static GraphEdge<T> Create<T>(T node, T edge) where T : IEquatable<T> | ||
=> new GraphEdge<T> { Node = node, Edge = edge }; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
using NUnit.Framework; | ||
|
||
using Umbraco.Cms.Core.WebAssets; | ||
|
||
using uSync.Core.Dependency; | ||
|
||
namespace uSync.Tests.Extensions; | ||
|
||
[TestFixture] | ||
public class SortingTests | ||
{ | ||
private HashSet<int> _nodes = new HashSet<int> | ||
{ | ||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10 | ||
}; | ||
|
||
private List<Guid> _guidNodes = new List<Guid> | ||
{ | ||
Guid.Parse("{5E37F691-FF91-45DA-9F53-8641FD9FE233}"), // 0 | ||
Guid.Parse("{5A35701C-349C-4AAE-BBCE-964B5C196989}"), // 1 | ||
Guid.Parse("{C70BE6CF-4923-4E2B-8742-B90FB7BBAFCB}"), // 2 | ||
Guid.Parse("{DE77C37C-DBC7-4806-8B0F-332BC38A87A4}"), // 3 | ||
Guid.Parse("{3C90BAD8-09F2-486E-BAA7-474F0D423C4D}") // 4 | ||
}; | ||
|
||
[Test] | ||
public void GraphSortGuidNodes() | ||
{ | ||
var graph = new List<GraphEdge<Guid>>(); | ||
|
||
graph.Add(GraphEdge.Create(_guidNodes[2], _guidNodes[1])); | ||
graph.Add(GraphEdge.Create(_guidNodes[2], _guidNodes[4])); | ||
|
||
var result = _guidNodes.TopologicalSort(graph); | ||
|
||
var expected = new List<Guid> | ||
{ | ||
Guid.Parse("{5E37F691-FF91-45DA-9F53-8641FD9FE233}"), // 0 | ||
Guid.Parse("{5A35701C-349C-4AAE-BBCE-964B5C196989}"), // 1 | ||
Guid.Parse("{DE77C37C-DBC7-4806-8B0F-332BC38A87A4}"), // 3 | ||
Guid.Parse("{3C90BAD8-09F2-486E-BAA7-474F0D423C4D}"), // 4 | ||
Guid.Parse("{C70BE6CF-4923-4E2B-8742-B90FB7BBAFCB}"), // 2 | ||
}; | ||
|
||
Assert.AreEqual(expected, result); | ||
} | ||
|
||
[Test] | ||
public void GraphSortNodes() | ||
{ | ||
var graph = new HashSet<GraphEdge<int>>( | ||
new[] | ||
{ | ||
GraphEdge.Create(2,4), | ||
GraphEdge.Create(4,7), | ||
GraphEdge.Create(5,8), | ||
GraphEdge.Create(6,9), | ||
}); | ||
|
||
|
||
var result = _nodes.TopologicalSort(graph); | ||
var expected = new List<int> { | ||
1, 3, 7, 8, 9, 10, 4, 5, 6, 2 | ||
}; | ||
|
||
Assert.AreEqual(expected, result); | ||
} | ||
|
||
[Test] | ||
public void CircularDependencyReturnsNull() | ||
{ | ||
var graph = new HashSet<GraphEdge<int>>( | ||
new[] | ||
{ | ||
GraphEdge.Create(2,4), | ||
GraphEdge.Create(4,7), | ||
GraphEdge.Create(5,8), | ||
GraphEdge.Create(6,9), | ||
GraphEdge.Create(7,2), // 7 can't depend on 2 and it depends on 4 which depends on 7 | ||
}); | ||
|
||
var result = _nodes.TopologicalSort(graph); | ||
|
||
Assert.IsNull(result); | ||
} | ||
} |