コード例 #1
0
        public void Clear()
        {
            int verticesRemoved = 0;
            int edgesRemoved    = 0;

            var graph = new UndirectedGraph <int, Edge <int> >();

            // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
            graph.VertexRemoved += v =>
            {
                Assert.IsNotNull(v);
                // ReSharper disable once AccessToModifiedClosure
                ++verticesRemoved;
            };
            // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
            graph.EdgeRemoved += e =>
            {
                Assert.IsNotNull(e);
                // ReSharper disable once AccessToModifiedClosure
                ++edgesRemoved;
            };

            AssertEmptyGraph(graph);

            graph.Clear();
            AssertEmptyGraph(graph);
            CheckCounters(0, 0);

            graph.AddVerticesAndEdge(new Edge <int>(1, 2));
            graph.AddVerticesAndEdge(new Edge <int>(2, 3));
            graph.AddVerticesAndEdge(new Edge <int>(3, 1));

            graph.Clear();
            AssertEmptyGraph(graph);
            CheckCounters(3, 3);

            #region Local function

            void CheckCounters(int expectedVerticesRemoved, int expectedEdgesRemoved)
            {
                Assert.AreEqual(expectedVerticesRemoved, verticesRemoved);
                Assert.AreEqual(expectedEdgesRemoved, edgesRemoved);
                verticesRemoved = 0;
                edgesRemoved    = 0;
            }

            #endregion
        }
コード例 #2
0
ファイル: Board.cs プロジェクト: vfridell/HiveLib
        private void RefreshAdjacencyList()
        {
            _adjacencyGraph.Clear();
            var finishedVertices = new HashSet <Piece>();

            foreach (KeyValuePair <Piece, Hex> kvp in _playedPieces)
            {
                foreach (Hex directionHex in Neighborhood.neighborDirections)
                {
                    // don't do the center
                    if (directionHex.Equals(new Hex(0, 0)))
                    {
                        continue;
                    }

                    Hex   adjacentHex = kvp.Value + directionHex;
                    Piece adjacentPiece;
                    if (TryGetPieceAtHex(adjacentHex, out adjacentPiece))
                    {
                        if (!finishedVertices.Contains(adjacentPiece))
                        {
                            _adjacencyGraph.AddVerticesAndEdge(new UndirectedEdge <Piece>(kvp.Key, adjacentPiece));
                        }
                    }
                }
                finishedVertices.Add(kvp.Key);
            }
        }
コード例 #3
0
        public void clearTest()
        {
            var graph = new UndirectedGraph <int, IEdge <int> >();

            graph.AddVerticesAndEdge(new EquatableEdge <int>(1, 2));
            graph.AddVerticesAndEdge(new EquatableEdge <int>(2, 3));
            Assert.AreEqual(graph.EdgeCount, 2);
            Assert.AreEqual(graph.VertexCount, 3);
            Assert.IsFalse(graph.IsEdgesEmpty);
            Assert.IsFalse(graph.IsVerticesEmpty);
            graph.Clear();
            Assert.AreEqual(graph.EdgeCount, 0);
            Assert.AreEqual(graph.VertexCount, 0);
            Assert.IsTrue(graph.IsEdgesEmpty);
            Assert.IsTrue(graph.IsVerticesEmpty);
        }
コード例 #4
0
        public void Create_Undirected()
        {
            var graph = new UndirectedGraph <int, EquatableEdge <int> >();

            // With self edge
            int v = 0;

            RandomGraphFactory.Create(
                graph,
                () => ++ v,
                (source, target) => new EquatableEdge <int>(source, target),
                new Random(123456),
                5,
                10,
                true);
            AssertHasVertices(graph, new[] { 1, 2, 3, 4, 5 });
            AssertHasEdges(
                graph,
                new[]
            {
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(2, 1),
                new EquatableEdge <int>(3, 5),
                new EquatableEdge <int>(3, 1),
                new EquatableEdge <int>(4, 5),
                new EquatableEdge <int>(4, 4),
                new EquatableEdge <int>(4, 1),
                new EquatableEdge <int>(5, 3)
            });

            // Without self edge
            graph.Clear();
            v = 0;
            RandomGraphFactory.Create(
                graph,
                () => ++ v,
                (source, target) => new EquatableEdge <int>(source, target),
                new Random(123456),
                5,
                10,
                false);
            AssertHasVertices(graph, new[] { 1, 2, 3, 4, 5 });
            AssertHasEdges(
                graph,
                new[]
            {
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(2, 1),
                new EquatableEdge <int>(3, 5),
                new EquatableEdge <int>(3, 1),
                new EquatableEdge <int>(3, 1),
                new EquatableEdge <int>(4, 5),
                new EquatableEdge <int>(4, 1),
                new EquatableEdge <int>(5, 3)
            });

            // Different seed change generated graph
            graph.Clear();
            v = 0;
            RandomGraphFactory.Create(
                graph,
                () => ++ v,
                (source, target) => new EquatableEdge <int>(source, target),
                new Random(456789),
                5,
                10,
                true);
            AssertHasVertices(graph, new[] { 1, 2, 3, 4, 5 });
            AssertHasEdges(
                graph,
                new[]
            {
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(2, 2),
                new EquatableEdge <int>(2, 5),
                new EquatableEdge <int>(3, 4),
                new EquatableEdge <int>(3, 2),
                new EquatableEdge <int>(4, 5),
                new EquatableEdge <int>(4, 2),
                new EquatableEdge <int>(4, 2),
                new EquatableEdge <int>(5, 2),
                new EquatableEdge <int>(5, 3)
            });

            // On non empty graph, keep existing stuff
            graph.Clear();
            graph.AddVerticesAndEdge(new EquatableEdge <int>(6, 7));
            v = 0;
            RandomGraphFactory.Create(
                graph,
                () => ++ v,
                (source, target) => new EquatableEdge <int>(source, target),
                new Random(123456),
                5,
                10,
                true);
            AssertHasVertices(graph, new[] { 1, 2, 3, 4, 5, 6, 7 });
            AssertHasEdges(
                graph,
                new[]
            {
                new EquatableEdge <int>(6, 7),
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(2, 1),
                new EquatableEdge <int>(3, 5),
                new EquatableEdge <int>(3, 1),
                new EquatableEdge <int>(4, 5),
                new EquatableEdge <int>(4, 4),
                new EquatableEdge <int>(4, 1),
                new EquatableEdge <int>(5, 3)
            });
        }