public void TestBasics() { using (var map = new MemoryMapStream()) { var accessor = map.CreateInt16(123456); Assert.AreEqual(true, accessor.CanWrite); Assert.AreEqual(123456 * 2, accessor.Capacity); Assert.AreEqual(123456, accessor.CapacityElements); Assert.AreEqual(2, accessor.ElementSize); Assert.AreEqual(true, accessor.ElementSizeFixed); var data = new short[1024]; for (ushort i = 0; i < 1024; i++) { data[i] = (short)(i ^ 6983); } accessor.WriteArray(15214, data, 0, 1024); var readData = new short[1024]; accessor.ReadArray(15214, readData, 0, 1024); for (ushort i = 0; i < 1024; i++) { Assert.AreEqual(data[i], readData[i]); } } }
/// <summary> /// Deserializes an shapes index from the given stream. /// </summary> public static ShapesArray CreateFrom(Stream stream, bool copy, out long size, bool hasElevation = false) { var initialPosition = stream.Position; size = 0; var longBytes = new byte[8]; stream.Read(longBytes, 0, 8); size += 8; var indexLength = BitConverter.ToInt64(longBytes, 0); stream.Read(longBytes, 0, 8); size += 8; var coordinatesLength = BitConverter.ToInt64(longBytes, 0); ArrayBase <ulong> index; ArrayBase <float> coordinates; ArrayBase <short> elevation = null; if (copy) { // just create arrays and read the data. index = Context.ArrayFactory.CreateMemoryBackedArray <ulong>(indexLength); index.CopyFrom(stream); size += indexLength * 8; coordinates = Context.ArrayFactory.CreateMemoryBackedArray <float>(coordinatesLength); size += coordinatesLength * 4; coordinates.CopyFrom(stream); if (hasElevation) { elevation = Context.ArrayFactory.CreateMemoryBackedArray <short>(coordinatesLength / 2); size += coordinatesLength; elevation.CopyFrom(stream); } } 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, indexLength * 8)); index = new Array <ulong>(map1.CreateUInt64(indexLength)); size += indexLength * 8; var map2 = new MemoryMapStream(new CappedStream(stream, position + indexLength * 8, coordinatesLength * 4)); coordinates = new Array <float>(map2.CreateSingle(coordinatesLength)); size += coordinatesLength * 4; if (hasElevation) { var map3 = new MemoryMapStream(new CappedStream(stream, position + indexLength * 8 + coordinatesLength * 4, coordinatesLength)); elevation = new Array <short>(map3.CreateInt16(coordinatesLength / 2)); size += coordinatesLength; } } // make stream is positioned correctly. stream.Seek(initialPosition + size, System.IO.SeekOrigin.Begin); return(new ShapesArray(index, coordinates, elevation)); }
/// <summary> /// Deserializes a graph from the stream. /// </summary> public static GeometricGraph Deserialize(System.IO.Stream stream, GeometricGraphProfile profile) { var version = stream.ReadByte(); if (version != 1 && version != 2) { throw new Exception(string.Format("Cannot deserialize geometric graph: Invalid version #: {0}.", version)); } // read elevation flag if any. var hasElevation = false; if (version >= 2) { hasElevation = (stream.ReadByte() == 1); } // read data. var graph = Graph.Deserialize(stream, profile == null ? null : profile.GraphProfile); var size = 0L; var initialPosition = stream.Position; ArrayBase <float> coordinates; ArrayBase <short> elevation = null; ShapesArray shapes; if (profile == null) { // don't use the stream, the read from it. coordinates = Context.ArrayFactory.CreateMemoryBackedArray <float>(graph.VertexCount * 2); coordinates.CopyFrom(stream); size += graph.VertexCount * 2 * 4; if (hasElevation) { elevation = Context.ArrayFactory.CreateMemoryBackedArray <short>(graph.VertexCount); elevation.CopyFrom(stream); size += graph.VertexCount * 2; } long shapeSize; shapes = ShapesArray.CreateFrom(stream, true, out shapeSize, hasElevation); size += shapeSize; } else { // use the stream as a map. var position = stream.Position; var map = new MemoryMapStream(new CappedStream(stream, position, graph.VertexCount * 4 * 2)); coordinates = new Array <float>(map.CreateSingle(graph.VertexCount * 2), profile.CoordinatesProfile); size += graph.VertexCount * 2 * 4; stream.Seek(position + graph.VertexCount * 4 * 2, System.IO.SeekOrigin.Begin); if (hasElevation) { position = stream.Position; map = new MemoryMapStream(new CappedStream(stream, position, graph.VertexCount * 2)); elevation = new Array <short>(map.CreateInt16(graph.VertexCount), profile.CoordinatesProfile); size += graph.VertexCount * 2; stream.Seek(position + graph.VertexCount * 2, System.IO.SeekOrigin.Begin); } long shapeSize; shapes = ShapesArray.CreateFrom(stream, false, out shapeSize, hasElevation); size += shapeSize; } // make stream is positioned correctly. stream.Seek(initialPosition + size, System.IO.SeekOrigin.Begin); return(new GeometricGraph(graph, coordinates, shapes, elevation)); }