public static IEnumerable <DependencyNodeV2 <T> > Sort <T>(DependencyNodeV2 <T> rootNode, IList <DependencyNodeV2 <T> > nodes) { var sorted = new List <DependencyNodeV2 <T> >(); var visited = new Dictionary <DependencyNodeV2 <T>, bool>(); Visit(rootNode, sorted, visited); nodes.ForEach(n => Visit(n, sorted, visited)); return(sorted); }
private static void Visit <T>(DependencyNodeV2 <T> node, IList <DependencyNodeV2 <T> > sorted, IDictionary <DependencyNodeV2 <T>, bool> visited) { bool inProcess; var alreadyVisited = visited.TryGetValue(node, out inProcess); if (alreadyVisited) { if (inProcess) { throw new RecursionException("Node contains a circular dependency: " + node); } } else { visited[node] = true; node.Dependencies.Where(d => !d.Equals(node)) .ForEach(d => Visit(d, sorted, visited)); visited[node] = false; sorted.Add(node); } }
static IEnumerable<DependencyNodeV2<ContributorNotification>> GetCompatibleNodes(IEnumerable<DependencyNodeV2<ContributorNotification>> nodes, DependencyNodeV2<ContributorNotification> notificationNode, Type type) { return from compatibleNode in nodes where !compatibleNode.Equals(notificationNode) && type.IsInstanceOfType(compatibleNode.Item.Contributor) select compatibleNode; }
static IEnumerable <DependencyNodeV2 <ContributorNotification> > GetCompatibleNodes(IEnumerable <DependencyNodeV2 <ContributorNotification> > nodes, DependencyNodeV2 <ContributorNotification> notificationNode, Type type) { return(from compatibleNode in nodes where !compatibleNode.Equals(notificationNode) && type.IsInstanceOfType(compatibleNode.Item.Contributor) select compatibleNode); }