Ejemplo n.º 1
0
        /// <summary>
        /// Serializes the given contraction data
        /// </summary>
        public long Serialize(Stream stream, bool toReadonly)
        {
            // write version # first:
            // 1: means regular non-edge contracted data.
            // 2: means regular edge contracted data.
            // 3: means a node-based graph that is edge-based.

            if (_nodeBasedGraph != null)
            {
                if (!_nodeBasedIsEdgeBased)
                {
                    stream.WriteByte(1);
                    return(_nodeBasedGraph.Serialize(stream, toReadonly) + 1);
                }
                else
                {
                    stream.WriteByte(3);
                    return(_nodeBasedGraph.Serialize(stream, toReadonly) + 1);
                }
            }
            else
            {
                stream.WriteByte(2);
                return(_edgeBasedGraph.Serialize(stream, toReadonly) + 1);
            }
        }
Ejemplo n.º 2
0
        public void TestDeserialize()
        {
            var graph = new DirectedDynamicGraph(10, 1);

            graph.AddEdge(0, 1, 1);

            // serialize.
            using (var stream = new System.IO.MemoryStream())
            {
                var size = graph.Serialize(stream);

                stream.Seek(0, System.IO.SeekOrigin.Begin);

                var deserializedGraph = DirectedDynamicGraph.Deserialize(stream, DirectedGraphProfile.Aggressive24);
                Assert.AreEqual(size, stream.Position);

                Assert.AreEqual(2, deserializedGraph.VertexCount);
                Assert.AreEqual(1, deserializedGraph.EdgeCount);

                // verify all edges.
                var edges = deserializedGraph.GetEdgeEnumerator(0);
                Assert.AreEqual(1, edges.Count());
                Assert.AreEqual(1, edges.First().Data[0]);
                Assert.AreEqual(1, edges.First().Neighbour);

                edges = deserializedGraph.GetEdgeEnumerator(1);
                Assert.IsFalse(edges.MoveNext());
            }

            graph = new DirectedDynamicGraph(10, 1);
            graph.AddEdge(0, 1, 1);
            graph.AddEdge(0, 2, 2);
            graph.AddEdge(0, 3, 3);
            graph.AddEdge(0, 4, 4);
            graph.AddEdge(5, 1, 5);
            graph.AddEdge(5, 2, 6);
            graph.AddEdge(5, 3, 7);
            graph.AddEdge(5, 4, 8);

            // serialize.
            using (var stream = new System.IO.MemoryStream())
            {
                var size = graph.Serialize(stream);

                stream.Seek(0, System.IO.SeekOrigin.Begin);

                var deserializedGraph = DirectedDynamicGraph.Deserialize(stream, DirectedGraphProfile.Aggressive24);
                Assert.AreEqual(size, stream.Position);

                Assert.AreEqual(6, deserializedGraph.VertexCount);
                Assert.AreEqual(8, deserializedGraph.EdgeCount);
            }
        }
Ejemplo n.º 3
0
        public void TestSerialize()
        {
            var graph = new DirectedDynamicGraph(10, 1);

            // add and compress.
            graph.AddEdge(0, 1, 1);
            graph.Compress();
            var expectedSize = 1 + 8 + 8 + 8 + 4 +         // the header: version byte two longs representing vertex, edge count and the size of the edge array and one int for minimum edge size.
                               graph.VertexCount * 1 * 4 + // the bytes for the vertex-index: 1 uint.
                               graph.EdgeCount * 2 * 4;    // the bytes for the edges: one edge 2 uint's.

            using (var stream = new System.IO.MemoryStream())
            {
                Assert.AreEqual(expectedSize, graph.Serialize(stream));
                Assert.AreEqual(expectedSize, stream.Position);
            }

            // verify all edges.
            var edges = graph.GetEdgeEnumerator(0);

            Assert.AreEqual(1, edges.Count());
            Assert.AreEqual(1, edges.First().Data[0]);
            Assert.AreEqual(1, edges.First().Neighbour);

            graph = new DirectedDynamicGraph(10, 1);

            // add and compress.
            graph.AddEdge(0, 1, 10);
            graph.AddEdge(1, 2, 20);
            graph.AddEdge(2, 3, 30);
            graph.AddEdge(3, 4, 40);
            graph.RemoveEdge(1, 2);
            graph.Compress();
            expectedSize = 1 + 8 + 8 + 8 + 4 +      // the header: version bytes, three longs representing vertex and edge count and the size of the edge array and one int for fixed edge size.
                           graph.VertexCount * 4 +  // the bytes for the vertex-index: 2 uint's.
                           graph.EdgeCount * 2 * 4; // the bytes for the edges: one edge 1 uint.
            using (var stream = new System.IO.MemoryStream())
            {
                Assert.AreEqual(expectedSize, graph.Serialize(stream));
                Assert.AreEqual(expectedSize, stream.Position);
            }
        }