Ejemplo n.º 1
0
 public void Simple()
 {
     var visitor = new CountingVisitor<double>();
     var vector = new VectorN(2);
     vector.AcceptVisitor(visitor);
     Assert.AreEqual(2, visitor.Count);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets a value indicating whether this graph is weakly connected.
        /// </summary>
        /// <returns><c>true</c> if this graph is weakly connected; otherwise, <c>false</c>.</returns>
        /// <exception cref="InvalidOperationException"><see cref="IsDirected"/> is <c>true</c>.</exception>
        /// <exception cref="InvalidOperationException">The <see cref="Graph{T}"/> is empty.</exception>
        /// <example>
        /// <code source="..\..\NGenerics.Examples\DataStructures\General\GraphExamples.cs" region="IsStronglyConnected" lang="cs" title="The following example shows how to use the IsStronglyConnected method."/>
        /// </example>
        public bool IsStronglyConnected()
        {
            #region Validation

            if (_graphIsDirected)
            {
                throw new InvalidOperationException("This operation is only valid on a directed graph. For undirected graphs, rather test for weak connectedness.");
            }

            if (_graphVertices.Count == 0)
            {
                throw new InvalidOperationException(GraphIsEmpty);
            }

            #endregion

            var countingVisitor = new CountingVisitor <Vertex <T> >();

            foreach (var vertex in _graphVertices.Keys)
            {
                BreadthFirstTraversal(countingVisitor, vertex);

                if (countingVisitor.Count != _graphVertices.Count)
                {
                    return(false);
                }

                countingVisitor.ResetCount();
            }

            return(true);
        }
Ejemplo n.º 3
0
 public void AcceptExample()
 {
     var visitor = new CountingVisitor<double>();
     var vector3D = new Vector3D();
     vector3D.AcceptVisitor(visitor);
     Assert.AreEqual(3, visitor.Count);
 }
Ejemplo n.º 4
0
        public void when_reading_assemblies_from_different_platforms_then_succeeds()
        {
            var metro = new FileInfo(@"..\..\..\Demo\DemoMetro\bin\DemoMetro.dll").FullName;
            var sl    = new FileInfo(@"..\..\..\Demo\DemoSilverlight\bin\DemoSilverlight.dll").FullName;
            var wp    = new FileInfo(@"..\..\..\Demo\DemoPhone\bin\DemoPhone.dll").FullName;
            var clr   = new FileInfo(@"..\..\..\Demo\DemoProject\bin\DemoProject.dll").FullName;

            var countMetro = new CountingVisitor("NuDoq-metro");
            var countSl    = new CountingVisitor("NuDoq-sl");
            var countWp    = new CountingVisitor("NuDoq-wp");
            var countClr   = new CountingVisitor("NuDoq-net");

            DocReader.Read(Assembly.LoadFrom(metro)).Accept(countMetro);
            DocReader.Read(Assembly.LoadFrom(sl)).Accept(countSl);
            DocReader.Read(Assembly.LoadFrom(wp)).Accept(countWp);
            DocReader.Read(Assembly.LoadFrom(clr)).Accept(countClr);

            Assert.Equal(countMetro.TypeCount, countClr.TypeCount);
            Assert.Equal(countSl.TypeCount, countClr.TypeCount);
            Assert.Equal(countWp.TypeCount, countClr.TypeCount);

            Assert.Equal(countMetro.ElementCount, countClr.ElementCount);
            Assert.Equal(countSl.ElementCount, countClr.ElementCount);
            Assert.Equal(countWp.ElementCount, countClr.ElementCount);

            Assert.Equal(countMetro.ContainerCount, countClr.ContainerCount);
            Assert.Equal(countSl.ContainerCount, countClr.ContainerCount);
            Assert.Equal(countWp.ContainerCount, countClr.ContainerCount);
        }
Ejemplo n.º 5
0
        public void DepthFirstTraversalExample()
        {
            // Initialize a new graph instance.
            var graph = new Graph <int>(true);

            // Add three vertices to the graph.
            var vertex1 = graph.AddVertex(1);
            var vertex2 = graph.AddVertex(2);
            var vertex3 = graph.AddVertex(3);

            // Add edges between the vertices.
            graph.AddEdge(vertex1, vertex2);
            graph.AddEdge(vertex2, vertex3);

            // Create a new counting visitor, which will keep track
            // of the number of objects that are visited.
            var countingVisitor = new CountingVisitor <Vertex <int> >();

            // Define in which order the objects should be visited -
            // we choose to do a pre-order traversal, however, you can
            // also choose to visit post-order.  Note that In-Order is
            // only available on Binary Trees, and not defined on graphs.
            var orderedVisitor = new PreOrderVisitor <Vertex <int> >(countingVisitor);

            // Perform a depth first traversal on the graph with
            // the ordered visitor, starting from node vertex1.
            graph.DepthFirstTraversal(orderedVisitor, vertex1);

            // The visitor will have visited 3 items.
            Assert.AreEqual(3, countingVisitor.Count);
        }
Ejemplo n.º 6
0
        public void BreadthFirstTraversalExample()
        {
            // Initialize a new graph instance
            var graph = new Graph <int>(true);

            // Add three vertices to the graph
            var vertex1 = graph.AddVertex(1);
            var vertex2 = graph.AddVertex(2);
            var vertex3 = graph.AddVertex(3);

            // Add edges between the vertices
            graph.AddEdge(vertex1, vertex2);
            graph.AddEdge(vertex2, vertex3);
            graph.AddEdge(vertex1, vertex3);

            // Create a counting visitor.  The counting
            // visitor will keep track of the number of
            // items on which Accept was called.
            var visitor = new CountingVisitor <Vertex <int> >();

            // Perform a breadth first traversal of the graph,
            // starting at vertex vertex1.
            graph.BreadthFirstTraversal(visitor, vertex1);

            // The visitor will have visited three vertices.
            Assert.AreEqual(3, visitor.Count);
        }
Ejemplo n.º 7
0
        public void when_reading_assembly_then_succeeds()
        {
            var clr = new FileInfo(@"DemoProject.dll").FullName;

            var countClr = new CountingVisitor("NuDoq-net");

            DocReader.Read(Assembly.LoadFrom(clr)).Accept(countClr);
        }
Ejemplo n.º 8
0
        public void Simple()
        {
            var visitor = new CountingVisitor <double>();
            var vector  = new VectorN(2);

            vector.AcceptVisitor(visitor);
            Assert.AreEqual(2, visitor.Count);
        }
Ejemplo n.º 9
0
        public void AcceptExample()
        {
            var visitor  = new CountingVisitor <double>();
            var vector3D = new Vector3D();

            vector3D.AcceptVisitor(visitor);
            Assert.AreEqual(3, visitor.Count);
        }
Ejemplo n.º 10
0
        private void PrintElementCounts(Shape rootShape)
        {
            var elementCounts = new CountingVisitor();

            rootShape.Accept(elementCounts);

            Console.WriteLine("Circles: {0}, Rectangles: {1}, Composites: {2}",
                              elementCounts.Circles, elementCounts.Rectangles, elementCounts.Composites);
        }
Ejemplo n.º 11
0
        public void AcceptExample()
        {
            var matrix = new ObjectMatrix<double>(2, 2);
            matrix[0, 0] = -2;
            matrix[0, 1] = 3;
            matrix[1, 0] = 4;
            matrix[1, 1] = 6;
            var visitor = new CountingVisitor<double>();

            matrix.AcceptVisitor(visitor);

            Assert.AreEqual(4, visitor.Count);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Gets a value indicating whether this graph is weakly connected.
        /// </summary>
        /// <returns><c>true</c> if this graph is weakly connected; otherwise, <c>false</c>.</returns>
        /// <exception cref="InvalidOperationException">The <see cref="Graph{T}"/> is empty.</exception>
        /// <example>
        /// <code source="..\..\NGenerics.Examples\DataStructures\General\GraphExamples.cs" region="IsWeaklyConnected" lang="cs" title="The following example shows how to use the IsWeaklyConnected method."/>
        /// </example>
        public bool IsWeaklyConnected()
        {
            if (_graphVertices.Count == 0)
            {
                throw new InvalidOperationException(GraphIsEmpty);
            }

            var countingVisitor = new CountingVisitor <Vertex <T> >();

            BreadthFirstTraversal(countingVisitor, GetAnyVertex());

            return(countingVisitor.Count == _graphVertices.Count);
        }
Ejemplo n.º 13
0
        public void AcceptExample()
        {
            var matrix = new ObjectMatrix <double>(2, 2);

            matrix[0, 0] = -2;
            matrix[0, 1] = 3;
            matrix[1, 0] = 4;
            matrix[1, 1] = 6;
            var visitor = new CountingVisitor <double>();

            matrix.AcceptVisitor(visitor);

            Assert.AreEqual(4, visitor.Count);
        }
Ejemplo n.º 14
0
        public void AcceptVisitorExample()
        {
            var heap = new Heap<string>(HeapType.Minimum) {"cat", "dog", "canary"};

            // There should be 3 items in the heap.
            Assert.AreEqual(3, heap.Count);

            // Create a visitor that will simply count the items in the heap.
            var visitor = new CountingVisitor<string>();

            // Make heap call IVisitor<T>.Visit on all items contained.
            heap.AcceptVisitor(visitor);

            // The counting visitor would have visited 3 items.
            Assert.AreEqual(3, visitor.Count);
        }
Ejemplo n.º 15
0
        public void AcceptVisitorExample()
        {
            var skipList = new SkipList<string, int> {{"cat", 1}, {"dog", 2}, {"canary", 3}};

            // There should be 3 items in the SkipList.
            Assert.AreEqual(3, skipList.Count);

            // Create a visitor that will simply count the items in the skipList.
            var visitor =new CountingVisitor<KeyValuePair<string, int>>();

            // Make the skipList call IVisitor<T>.Visit on all items contained.
            skipList.AcceptVisitor(visitor);

            // The counting visitor would have visited 3 items.
            Assert.AreEqual(3, visitor.Count);
        }
Ejemplo n.º 16
0
        public void AcceptVisitorExample()
        {
            var tree = new RedBlackTree<string, int> {{"cat", 1}, {"dog", 2}, {"canary", 3}};

            // There should be 3 items in the tree.
            Assert.AreEqual(3, tree.Count);

            // Create a visitor that will simply count the items in the tree.
            var visitor =
                new CountingVisitor<KeyValuePair<string, int>>();

            // Make the tree call IVisitor<T>.Visit on all items contained.
            tree.AcceptVisitor(visitor);

            // The counting visitor would have visited 3 items.
            Assert.AreEqual(3, visitor.Count);
        }
Ejemplo n.º 17
0
        public void AcceptVisitorExample()
        {
            var sortedList = new SortedList <string> {
                "cat", "dog", "canary"
            };

            // There should be 3 items in the sortedList.
            Assert.AreEqual(3, sortedList.Count);

            // Create a visitor that will simply count the items in the sortedList.
            var visitor = new CountingVisitor <string>();

            // Make sortedList tree call IVisitor<T>.Visit on all items contained.
            sortedList.AcceptVisitor(visitor);

            // The counting visitor would have visited 3 items.
            Assert.AreEqual(3, visitor.Count);
        }
Ejemplo n.º 18
0
        public void AcceptVisitorExample()
        {
            var heap = new Heap <string>(HeapType.Minimum)
            {
                "cat", "dog", "canary"
            };

            // There should be 3 items in the heap.
            Assert.AreEqual(3, heap.Count);

            // Create a visitor that will simply count the items in the heap.
            var visitor = new CountingVisitor <string>();

            // Make heap call IVisitor<T>.Visit on all items contained.
            heap.AcceptVisitor(visitor);

            // The counting visitor would have visited 3 items.
            Assert.AreEqual(3, visitor.Count);
        }
Ejemplo n.º 19
0
        public void AcceptVisitorExample()
        {
            var priorityQueue = new PriorityQueue<string, int>(PriorityQueueType.Minimum);
            priorityQueue.Enqueue("cat");
            priorityQueue.Enqueue("dog");
            priorityQueue.Enqueue("canary");

            // There should be 3 items in the priorityQueue.
            Assert.AreEqual(3, priorityQueue.Count);

            // Create a visitor that will simply count the items in the priorityQueue.
            var visitor =
                new CountingVisitor<string>();

            // Make priorityQueue call IVisitor<T>.Visit on all items contained.
            priorityQueue.AcceptVisitor(visitor);

            // The counting visitor would have visited 3 items.
            Assert.AreEqual(3, visitor.Count);
        }
Ejemplo n.º 20
0
        public void AcceptVisitorExample()
        {
            var deque = new Deque<string>();
            deque.EnqueueHead("cat");
            deque.EnqueueHead("dog");
            deque.EnqueueHead("canary");

            // There should be 3 items in the deque.
            Assert.AreEqual(3, deque.Count);

            // Create a visitor that will simply count the items in the deque.
            var visitor =
                new CountingVisitor<string>();

            // Make deque call IVisitor<T>.Visit on all items contained.
            deque.AcceptVisitor(visitor);

            // The counting visitor would have visited 3 items.
            Assert.AreEqual(3, visitor.Count);
        }
Ejemplo n.º 21
0
        public void AcceptExample()
        {
            // Initialize a new graph instance
            var graph = new Graph <int>(true);

            // Add three vertices to the graph
            graph.AddVertex(1);
            graph.AddVertex(2);
            graph.AddVertex(3);

            // Create a counting visitor.  The counting
            // visitor will keep track of the number of
            // items on which Accept was called.
            var visitor = new CountingVisitor <int>();

            // Let the visitor in the door
            graph.AcceptVisitor(visitor);

            // The visitor will have visited three vertices.
            Assert.AreEqual(3, visitor.Count);
        }
Ejemplo n.º 22
0
        public void AcceptExample()
        {
            // Create a sample PascalSet instance
            var set = new PascalSet(100);

            // Set a couple of values in the PascalSet
            for (var i = 0; i < 100; i += 10)
            {
                set.Add(i);
            }

            // Create a counting visitor that will count the number of
            // items visited.
            var visitor = new CountingVisitor <int>();

            // Open up the door for the visitor
            set.AcceptVisitor(visitor);

            // The visitor will have visited 10 items
            Assert.AreEqual(visitor.Count, 10);
        }
Ejemplo n.º 23
0
        public void AcceptVisitorExample()
        {
            var priorityQueue = new PriorityQueue <string, int>(PriorityQueueType.Minimum);

            priorityQueue.Enqueue("cat");
            priorityQueue.Enqueue("dog");
            priorityQueue.Enqueue("canary");

            // There should be 3 items in the priorityQueue.
            Assert.AreEqual(3, priorityQueue.Count);

            // Create a visitor that will simply count the items in the priorityQueue.
            var visitor =
                new CountingVisitor <string>();

            // Make priorityQueue call IVisitor<T>.Visit on all items contained.
            priorityQueue.AcceptVisitor(visitor);

            // The counting visitor would have visited 3 items.
            Assert.AreEqual(3, visitor.Count);
        }
Ejemplo n.º 24
0
        public void AcceptVisitorExample()
        {
            var circularQueue = new CircularQueue <string>(10);

            circularQueue.Enqueue("cat");
            circularQueue.Enqueue("dog");
            circularQueue.Enqueue("canary");

            // There should be 3 items in the circularQueue.
            Assert.AreEqual(3, circularQueue.Count);

            // Create a visitor that will simply count the items in the circularQueue.
            var visitor =
                new CountingVisitor <string>();

            // Make circularQueue call IVisitor<T>.Visit on all items contained.
            circularQueue.AcceptVisitor(visitor);

            // The counting visitor would have visited 3 items.
            Assert.AreEqual(3, visitor.Count);
        }
Ejemplo n.º 25
0
        public void AcceptExample()
        {
            // Initialize a new graph instance
            var graph = new Graph<int>(true);

            // Add three vertices to the graph
            graph.AddVertex(1);
            graph.AddVertex(2);
            graph.AddVertex(3);

            // Create a counting visitor.  The counting
            // visitor will keep track of the number of
            // items on which Accept was called.
            var visitor = new CountingVisitor<int>();

            // Let the visitor in the door
            graph.AcceptVisitor(visitor);

            // The visitor will have visited three vertices.
            Assert.AreEqual(visitor.Count, 3);
        }
Ejemplo n.º 26
0
        public void AcceptVisitorExample()
        {
            // Create a Bag
            var bag = new Bag<string>
                      	{
                      		"cat",
                      		"dog",
                      		"canary"
                      	};

            // There should be 3 items in the bag.
            Assert.AreEqual(3, bag.Count);

            // Create a visitor that will simply count the items in the bag.
            var visitor = new CountingVisitor<string>();

            // Make bag call IVisitor<T>.Visit on all items contained.
            bag.AcceptVisitor<string>(visitor);

            // The counting visitor would have visited 3 items.
            Assert.AreEqual(3, visitor.Count);
        }
        public void AcceptVisitorExample()
        {
            BinarySearchTreeBase <string, int> tree = new BinarySearchTree <string, int>
            {
                { "cat", 1 },
                { "dog", 2 },
                { "canary", 3 }
            };

            // There should be 3 items in the tree.
            Assert.AreEqual(3, tree.Count);

            // Create a visitor that will simply count the items in the tree.
            var visitor =
                new CountingVisitor <KeyValuePair <string, int> >();

            // Make the tree call IVisitor<T>.Visit on all items contained.
            tree.AcceptVisitor(visitor);

            // The counting visitor would have visited 3 items.
            Assert.AreEqual(3, visitor.Count);
        }
Ejemplo n.º 28
0
        public void AcceptVisitorExample()
        {
            // Create a Bag
            var bag = new Bag <string>
            {
                "cat",
                "dog",
                "canary"
            };

            // There should be 3 items in the bag.
            Assert.AreEqual(3, bag.Count);

            // Create a visitor that will simply count the items in the bag.
            var visitor = new CountingVisitor <string>();

            // Make bag call IVisitor<T>.Visit on all items contained.
            bag.AcceptVisitor <string>(visitor);

            // The counting visitor would have visited 3 items.
            Assert.AreEqual(3, visitor.Count);
        }
Ejemplo n.º 29
0
        public void when_reading_assemblies_from_different_platforms_then_succeeds()
        {
            var metro = new FileInfo(@"..\..\..\Demo\DemoMetro\bin\DemoMetro.dll").FullName;
            var sl = new FileInfo(@"..\..\..\Demo\DemoSilverlight\bin\DemoSilverlight.dll").FullName;
            var wp = new FileInfo(@"..\..\..\Demo\DemoPhone\bin\DemoPhone.dll").FullName;
            var clr = new FileInfo(@"..\..\..\Demo\DemoProject\bin\DemoProject.dll").FullName;

            var countMetro = new CountingVisitor("NuDoq-metro");
            var countSl = new CountingVisitor("NuDoq-sl");
            var countWp = new CountingVisitor("NuDoq-wp");
            var countClr = new CountingVisitor("NuDoq-net");

            DocReader.Read(Assembly.LoadFrom(metro)).Accept(countMetro);
            DocReader.Read(Assembly.LoadFrom(sl)).Accept(countSl);
            DocReader.Read(Assembly.LoadFrom(wp)).Accept(countWp);
            DocReader.Read(Assembly.LoadFrom(clr)).Accept(countClr);

            Assert.Equal(countMetro.TypeCount, countClr.TypeCount);
            Assert.Equal(countSl.TypeCount, countClr.TypeCount);
            Assert.Equal(countWp.TypeCount, countClr.TypeCount);

            Assert.Equal(countMetro.ElementCount, countClr.ElementCount);
            Assert.Equal(countSl.ElementCount, countClr.ElementCount);
            Assert.Equal(countWp.ElementCount, countClr.ElementCount);

            Assert.Equal(countMetro.ContainerCount, countClr.ContainerCount);
            Assert.Equal(countSl.ContainerCount, countClr.ContainerCount);
            Assert.Equal(countWp.ContainerCount, countClr.ContainerCount);
        }
Ejemplo n.º 30
0
        public void DepthFirstTraversalExample()
        {
            // Initialize a new graph instance.
            var graph = new Graph<int>(true);

            // Add three vertices to the graph.
            var vertex1 = graph.AddVertex(1);
            var vertex2 = graph.AddVertex(2);
            var vertex3 = graph.AddVertex(3);

            // Add edges between the vertices.
            graph.AddEdge(vertex1, vertex2);
            graph.AddEdge(vertex2, vertex3);

            // Create a new counting visitor, which will keep track
            // of the number of objects that are visited.
            var countingVisitor = new CountingVisitor<Vertex<int>>();

            // Define in which order the objects should be visited -
            // we choose to do a pre-order traversal, however, you can
            // also choose to visit post-order.  Note that In-Order is
            // only available on Binary Trees, and not defined on graphs.
            var orderedVisitor = new PreOrderVisitor<Vertex<int>>(countingVisitor);

            // Perform a depth first traversal on the graph with
            // the ordered visitor, starting from node vertex1.
            graph.DepthFirstTraversal(orderedVisitor, vertex1);

            // The visitor will have visited 3 items.
            Assert.AreEqual(countingVisitor.Count, 3);
        }
Ejemplo n.º 31
0
        public void BreadthFirstTraversalExample()
        {
            // Initialize a new graph instance
            var graph = new Graph<int>(true);

            // Add three vertices to the graph
            var vertex1 = graph.AddVertex(1);
            var vertex2 = graph.AddVertex(2);
            var vertex3 = graph.AddVertex(3);

            // Add edges between the vertices
            graph.AddEdge(vertex1, vertex2);
            graph.AddEdge(vertex2, vertex3);
            graph.AddEdge(vertex1, vertex3);

            // Create a counting visitor.  The counting
            // visitor will keep track of the number of
            // items on which Accept was called.
            var visitor = new CountingVisitor<Vertex<int>>();

            // Perform a breadth first traversal of the graph,
            // starting at vertex vertex1.
            graph.BreadthFirstTraversal(visitor, vertex1);

            // The visitor will have visited three vertices.
            Assert.AreEqual(visitor.Count, 3);
        }