Esempio n. 1
0
        // TODO: Double check these when you are less tired
        private void LoadIteratorCache()
        {
            var visited = new List <GraphNode <T> >();
            IOneInOneOutCollection <GraphNode <T> > remaining;

            switch (GraphSearchAlgorithm)
            {
            case GraphSearchAlgorithm.BreadthFirstSearch:
                remaining = new FirstInFirstOutCollection <GraphNode <T> >();
                _cache    = new FirstInFirstOutCollection <GraphNode <T> >();
                break;

            case GraphSearchAlgorithm.DepthFirstSearch:
                remaining = new LastInFirstOutCollection <GraphNode <T> >();
                _cache    = new FirstInFirstOutCollection <GraphNode <T> >();
                break;

            case Utility.Graph.GraphSearchAlgorithm.TopographicalSearch:
                if (!Graph.IsAcyclic)
                {
                    throw new NotSupportedException("Cannot invoke a topographical search on a graph with cycles");
                }

                remaining = new LastInFirstOutCollection <GraphNode <T> >();
                _cache    = new LastInFirstOutCollection <GraphNode <T> >();
                break;

            case Utility.Graph.GraphSearchAlgorithm.ReverseTopographicalSearch:
                if (!Graph.IsAcyclic)
                {
                    throw new NotSupportedException("Cannot invoke a reverse topographical search on a graph with cycles");
                }

                remaining = new LastInFirstOutCollection <GraphNode <T> >();
                _cache    = new FirstInFirstOutCollection <GraphNode <T> >();
                break;

            default:
                throw new InvalidOperationException("Unknown graph search algorithm");
            }

            remaining.AddRange(Graph.RootNodes);

            while (remaining.Count > 0)
            {
                GraphNode <T> currentNode = remaining.Remove();
                visited.Add(currentNode);

                foreach (GraphEdge <T> outgoingEdge in currentNode.OutgoingEdges)
                {
                    GraphNode <T> successor = outgoingEdge.Sink;
                    if (!visited.Contains(successor) && !remaining.FastContains(successor))
                    {
                        remaining.Add(successor);
                    }
                }

                _cache.Add(currentNode);
            }
        }
Esempio n. 2
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         Current = default(T);
         Graph   = null;
         _cache  = null;
     }
 }
Esempio n. 3
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         Current    = default(TReturn);
         Root       = default(TFollow);
         _visited   = null;
         _remaining = null;
     }
 }
Esempio n. 4
0
        public GraphEnumerator(TFollow root, GraphSearchAlgorithm graphSearchAlgorithm, Func <TFollow, IEnumerable <TFollow> > childrenProjector)
        {
            Root = root;
            GraphSearchAlgorithm = graphSearchAlgorithm;
            ChildrenProjector    = childrenProjector;

            _visited = new HashSet <TFollow>();
            switch (GraphSearchAlgorithm)
            {
            case GraphSearchAlgorithm.BreadthFirstSearch:
                _remaining = new FirstInFirstOutCollection <TFollow>();
                break;

            case GraphSearchAlgorithm.DepthFirstSearch:
                _remaining = new LastInFirstOutCollection <TFollow>();
                break;

            default:
                throw new ArgumentException("Unknown graph search algorithm", "graphSearchAlgorithm");
            }

            _remaining.Add(Root);
        }