/// <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); } }
public void TestDeserialize() { var graph = new DirectedMetaGraph(1, 1, 10); graph.AddEdge(0, 1, 1, 100); // serialize. using (var stream = new System.IO.MemoryStream()) { var size = graph.Serialize(stream); stream.Seek(0, System.IO.SeekOrigin.Begin); var deserializedGraph = DirectedMetaGraph.Deserialize(stream, DirectedMetaGraphProfile.Aggressive40); 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.AreEqual(0, edges.Count()); } graph = new DirectedMetaGraph(1, 1, 10); graph.AddEdge(0, 1, 1, 100); graph.AddEdge(0, 2, 2, 200); graph.AddEdge(0, 3, 3, 300); graph.AddEdge(0, 4, 4, 400); graph.AddEdge(5, 1, 5, 500); graph.AddEdge(5, 2, 6, 600); graph.AddEdge(5, 3, 7, 700); graph.AddEdge(5, 4, 8, 800); // serialize. using (var stream = new System.IO.MemoryStream()) { var size = graph.Serialize(stream); stream.Seek(0, System.IO.SeekOrigin.Begin); var deserializedGraph = DirectedMetaGraph.Deserialize(stream, DirectedMetaGraphProfile.Aggressive40); Assert.AreEqual(size, stream.Position); Assert.AreEqual(6, deserializedGraph.VertexCount); Assert.AreEqual(8, deserializedGraph.EdgeCount); } }
public void TestSerialize() { var graph = new DirectedMetaGraph(1, 1, 10); // add and compress. graph.AddEdge(0, 1, 1, 100); graph.Compress(); var expectedSize = 1 + 1 + 8 + 8 + 4 + 4 + // the header: two longs representing vertex and edge count and one int for edge size and one for vertex size. graph.VertexCount * 2 * 4 + // the bytes for the vertex-index: 2 uint's. graph.EdgeCount * 2 * 4 + // the bytes for the edges: one edge 1 uint. 4 + 4 + // the header for the meta-data: 2 uint for sizes. graph.EdgeCount * 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); } // 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 DirectedMetaGraph(1, 1, 10); // add and compress. graph.AddEdge(0, 1, 10, 100); graph.AddEdge(1, 2, 20, 200); graph.AddEdge(2, 3, 30, 300); graph.AddEdge(3, 4, 40, 400); graph.RemoveEdge(1, 2); graph.Compress(); expectedSize = 1 + 1 + 8 + 8 + 4 + 4 + // the header: two longs representing vertex and edge count and one int for edge size and one for vertex size. graph.VertexCount * 2 * 4 + // the bytes for the vertex-index: 2 uint's. graph.EdgeCount * 2 * 4 + // the bytes for the edges: one edge 1 uint. 4 + 4 + // the header for the meta-data: 2 uint for sizes. graph.EdgeCount * 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); } }