/// <summary> /// Serializes to a stream. /// </summary> public long Serialize(System.IO.Stream stream) { this.Compress(); // serialize geometric graph. long size = 1; // write the version #. // v1->v2: Added maxEdgeDistance. stream.WriteByte(2); // write maxEdgeDistance. var bytes = BitConverter.GetBytes(_maxEdgeDistance); stream.Write(bytes, 0, 4); size += 4; // write graph. size += _graph.Serialize(stream); // serialize edge data. _edgeData.CopyTo(stream); size += _edgeData.Length * 4; return(size); }
public void TestSerialize() { var graph = new GeometricGraph(1, 100); // add one edge. graph.AddVertex(0, 0, 0); graph.AddVertex(1, 0, 0); graph.AddEdge(0, 1, new uint[] { 10 }, null); // serialize. graph.Compress(); var expectedSize = graph.SizeInBytes; using (var stream = new System.IO.MemoryStream()) { Assert.AreEqual(expectedSize, graph.Serialize(stream)); } graph = new GeometricGraph(1, 100); // add one edge. graph.AddVertex(0, 0, 0); graph.AddVertex(1, 0, 0); graph.AddVertex(2, 0, 0); graph.AddVertex(3, 0, 0); graph.AddVertex(4, 0, 0); graph.AddVertex(5, 0, 0); graph.AddEdge(0, 1, new uint[] { 10 }, null); graph.AddEdge(0, 2, new uint[] { 20 }, null); graph.AddEdge(0, 3, new uint[] { 30 }, null); graph.AddEdge(0, 4, new uint[] { 40 }, null); graph.AddEdge(5, 1, new uint[] { 50 }, null); graph.AddEdge(5, 2, new uint[] { 60 }, null); graph.AddEdge(5, 3, new uint[] { 70 }, null); graph.AddEdge(5, 4, new uint[] { 80 }, null); // serialize. graph.Compress(); expectedSize = graph.SizeInBytes; using (var stream = new System.IO.MemoryStream()) { Assert.AreEqual(expectedSize, graph.Serialize(stream)); } }
public void TestDeserialize() { var graph = new GeometricGraph(1, 100); // add one edge. graph.AddVertex(0, 0, 0); graph.AddVertex(1, 0, 0); graph.AddEdge(0, 1, new uint[] { 1 }, null); // serialize. using (var stream = new System.IO.MemoryStream()) { var size = graph.Serialize(stream); stream.Seek(0, System.IO.SeekOrigin.Begin); var deserializedGraph = GeometricGraph.Deserialize(stream, GeometricGraphProfile.Default); 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().To); edges = deserializedGraph.GetEdgeEnumerator(1); Assert.AreEqual(1, edges.Count()); Assert.AreEqual(true, edges.First().DataInverted); Assert.AreEqual(1, edges.First().Data[0]); Assert.AreEqual(0, edges.First().To); } graph = new GeometricGraph(1, 100); // add one edge. graph.AddVertex(0, 0, 0); graph.AddVertex(1, 0, 0); graph.AddVertex(2, 0, 0); graph.AddVertex(3, 0, 0); graph.AddVertex(4, 0, 0); graph.AddVertex(5, 0, 0); graph.AddEdge(0, 1, new uint[] { 1 }, null); graph.AddEdge(0, 2, new uint[] { 2 }, null); graph.AddEdge(0, 3, new uint[] { 3 }, null); graph.AddEdge(0, 4, new uint[] { 4 }, null); graph.AddEdge(5, 1, new uint[] { 5 }, null); graph.AddEdge(5, 2, new uint[] { 6 }, null); graph.AddEdge(5, 3, new uint[] { 7 }, null); graph.AddEdge(5, 4, new uint[] { 8 }, null); // serialize. using (var stream = new System.IO.MemoryStream()) { var size = graph.Serialize(stream); stream.Seek(0, System.IO.SeekOrigin.Begin); var deserializedGraph = GeometricGraph.Deserialize(stream, GeometricGraphProfile.Default); Assert.AreEqual(size, stream.Position); Assert.AreEqual(6, deserializedGraph.VertexCount); Assert.AreEqual(8, deserializedGraph.EdgeCount); } }