/// <summary> /// Deserializes a graph from the given stream. /// </summary> /// <returns></returns> public static Graph Deserialize(System.IO.Stream stream, GraphProfile profile) { var initialPosition = stream.Position; // read sizes. long size = 1; var version = stream.ReadByte(); if (version != 1) { throw new Exception(string.Format("Cannot deserialize graph: Invalid version #: {0}.", version)); } var bytes = new byte[8]; stream.Read(bytes, 0, 8); size = size + 8; var vertexLength = BitConverter.ToInt64(bytes, 0); stream.Read(bytes, 0, 8); size = size + 8; var edgeLength = BitConverter.ToInt64(bytes, 0); stream.Read(bytes, 0, 4); size = size + 4; var vertexSize = BitConverter.ToInt32(bytes, 0); stream.Read(bytes, 0, 4); size = size + 4; var edgeSize = BitConverter.ToInt32(bytes, 0); ArrayBase <uint> vertices; ArrayBase <uint> edges; if (profile == null) { // just create arrays and read the data. vertices = new MemoryArray <uint>(vertexLength * vertexSize); vertices.CopyFrom(stream); size += vertexLength * vertexSize * 4; edges = new MemoryArray <uint>(edgeLength * edgeSize); edges.CopyFrom(stream); size += edgeLength * edgeSize * 4; } else { // create accessors over the exact part of the stream that represents vertices/edges. var position = stream.Position; var map1 = new MemoryMapStream(new CappedStream(stream, position, vertexLength * vertexSize * 4)); vertices = new Array <uint>(map1.CreateUInt32(vertexLength * vertexSize), profile.VertexProfile); size += vertexLength * vertexSize * 4; var map2 = new MemoryMapStream(new CappedStream(stream, position + vertexLength * vertexSize * 4, edgeLength * edgeSize * 4)); edges = new Array <uint>(map2.CreateUInt32(edgeLength * edgeSize), profile.EdgeProfile); size += edgeLength * edgeSize * 4; } // make sure stream is positioned at the correct location. stream.Seek(initialPosition + size, System.IO.SeekOrigin.Begin); return(new Graph(edgeSize - MINIMUM_EDGE_SIZE, vertices, edges)); }
/// <summary> /// Creates a new using the given file. /// </summary> public Graph(MemoryMap map, GraphProfile profile, int edgeDataSize, long estimatedSize) { _edgeDataSize = edgeDataSize; _edgeSize = MINIMUM_EDGE_SIZE + edgeDataSize; _vertices = new Array <uint>(map, estimatedSize, profile.VertexProfile); for (int i = 0; i < _vertices.Length; i++) { _vertices[i] = Constants.NO_VERTEX; } _edges = new Array <uint>(map, estimatedSize * 3 * _edgeSize, profile.EdgeProfile); for (int i = 0; i < _edges.Length; i++) { _edges[i] = Constants.NO_EDGE; } }