Exemplo n.º 1
0
        public static IEnumerable <TopologicalNode <T> > Sort <T>(TopologicalNode <T> rootNode, IList <TopologicalNode <T> > nodes)
        {
            var sorted  = new List <TopologicalNode <T> >();
            var visited = new Dictionary <TopologicalNode <T>, bool>();

            Visit(rootNode, sorted, visited);
            nodes.ForEach(n => Visit(n, sorted, visited));

            return(sorted);
        }
 public bool Equals(TopologicalNode <T> other)
 {
     if (ReferenceEquals(null, other))
     {
         return(false);
     }
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return(EqualityComparer <T> .Default.Equals(Item, other.Item));
 }
Exemplo n.º 3
0
        public bool Equals(TopologicalNode <T> other)
        {
            if (ReferenceEquals(null, other))
            {
                return(false);
            }
            if (ReferenceEquals(this, other))
            {
                return(true);
            }

            return(Item.Equals(other.Item));
        }
Exemplo n.º 4
0
        private static void Visit <T>(TopologicalNode <T> node, IList <TopologicalNode <T> > sorted, IDictionary <TopologicalNode <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);
            }
        }
Exemplo n.º 5
0
        public TopologicalTree(T rootItem, IList <TopologicalNode <T> > nodes)
        {
            var rootNode = new TopologicalNode <T>(rootItem);

            Nodes = TopologicalSort.Sort(rootNode, nodes);
        }