/// <summary> /// Disposes of all native resources associated with this memory dynamic graph. /// </summary> public void Dispose() { _coordinates.Dispose(); _coordinates = null; _edges.Dispose(); _edges = null; _edgeData.Dispose(); _edgeData = null; _vertices.Dispose(); _vertices = null; _shapes.Dispose(); _shapes = null; }
/// <summary> /// Creates a new memory mapped file dynamic graph. /// </summary> /// <param name="estimatedSize"></param> /// <param name="coordinates"></param> /// <param name="vertices"></param> /// <param name="edges"></param> /// <param name="edgeData"></param> /// <param name="edgeShapes"></param> public MemoryMappedGraph(long estimatedSize, MemoryMappedHugeArray <GeoCoordinateSimple> coordinates, MemoryMappedHugeArray <uint> vertices, MemoryMappedHugeArray <uint> edges, MemoryMappedHugeArray <TEdgeData> edgeData, HugeCoordinateCollectionIndex edgeShapes) : base(estimatedSize, coordinates, vertices, edges, edgeData, edgeShapes) { _coordinates = coordinates; _vertices = vertices; _edges = edges; _edgeData = edgeData; _shapes = edgeShapes; }
/// <summary> /// Creates a graph using the existing data in the given arrays. /// </summary> /// <param name="coordinateArray"></param> /// <param name="vertexArray"></param> /// <param name="edgesArray"></param> /// <param name="edgeDataArray"></param> /// <param name="edgeShapeArray"></param> public DirectedGraph( HugeArrayBase <GeoCoordinateSimple> coordinateArray, HugeArrayBase <uint> vertexArray, HugeArrayBase <uint> edgesArray, HugeArrayBase <TEdgeData> edgeDataArray, HugeCoordinateCollectionIndex edgeShapeArray) { _vertices = vertexArray; _coordinates = coordinateArray; _edges = edgesArray; _edgeData = edgeDataArray; _edgeShapes = edgeShapeArray; _nextVertexId = (uint)(_vertices.Length / VERTEX_SIZE); _nextEdgeId = (uint)(edgesArray.Length / EDGE_SIZE); }
/// <summary> /// Creates a new in-memory graph. /// </summary> /// <param name="sizeEstimate"></param> /// <param name="coordinateArray"></param> /// <param name="vertexArray"></param> /// <param name="edgesArray"></param> /// <param name="edgeDataArray"></param> /// <param name="edgeShapeArray"></param> public DirectedGraph(long sizeEstimate, HugeArrayBase <GeoCoordinateSimple> coordinateArray, HugeArrayBase <uint> vertexArray, HugeArrayBase <uint> edgesArray, HugeArrayBase <TEdgeData> edgeDataArray, HugeCoordinateCollectionIndex edgeShapeArray) { _nextVertexId = 1; _nextEdgeId = 0; _vertices = vertexArray; _vertices.Resize(sizeEstimate); _coordinates = coordinateArray; _coordinates.Resize(sizeEstimate); _edges = edgesArray; _edges.Resize(sizeEstimate * 3 * EDGE_SIZE); _edgeData = edgeDataArray; _edgeData.Resize(sizeEstimate * 3); _edgeShapes = edgeShapeArray; _edgeShapes.Resize(sizeEstimate * 3); }
public void TestTrim() { OsmSharp.Math.Random.StaticRandomGenerator.Set(116542346); var box = new GeoCoordinateBox( new GeoCoordinate(90, 180), new GeoCoordinate(-90, -180)); var size = 100; var maxCollectionSize = 4; var referenceDictionary = new Dictionary <long, ICoordinateCollection>(); var coordinates = new HugeCoordinateCollectionIndex(400); for (int idx = 0; idx < size; idx++) { var currentSize = OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(maxCollectionSize) + 1; var coordinatesArray = new GeoCoordinate[currentSize]; while (currentSize > 0) { coordinatesArray[currentSize - 1] = box.GenerateRandomIn(OsmSharp.Math.Random.StaticRandomGenerator.Get()); currentSize--; } var coordinatesCollection = new CoordinateArrayCollection <GeoCoordinate>(coordinatesArray); referenceDictionary[idx] = coordinatesCollection; coordinates[idx] = coordinatesCollection; } // execute the trim. coordinates.Trim(); // check result. for (int idx = 0; idx < size; idx++) { var referenceCollection = referenceDictionary[idx]; var collection = coordinates[idx]; referenceCollection.Reset(); collection.Reset(); while (referenceCollection.MoveNext()) { Assert.IsTrue(collection.MoveNext()); Assert.AreEqual(referenceCollection.Latitude, collection.Latitude); Assert.AreEqual(referenceCollection.Longitude, collection.Longitude); } Assert.IsFalse(collection.MoveNext()); } }
/// <summary> /// Creates a new in-memory graph. /// </summary> /// <param name="sizeEstimate"></param> /// <param name="coordinateArray"></param> /// <param name="vertexArray"></param> /// <param name="edgesArray"></param> /// <param name="edgeDataArray"></param> /// <param name="edgeShapeArray"></param> protected MemoryGraph(long sizeEstimate, IHugeArray <GeoCoordinateSimple> coordinateArray, IHugeArray <uint> vertexArray, IHugeArray <uint> edgesArray, IHugeArray <TEdgeData> edgeDataArray, HugeCoordinateCollectionIndex edgeShapeArray) { _nextVertexId = 1; _nextEdgeId = 0; _vertices = vertexArray; _vertices.Resize(sizeEstimate); for (int idx = 0; idx < sizeEstimate; idx++) { _vertices[idx] = NO_EDGE; } _coordinates = coordinateArray; _coordinates.Resize(sizeEstimate); _edges = edgesArray; _edges.Resize(sizeEstimate * 3 * EDGE_SIZE); for (int idx = 0; idx < sizeEstimate * 3 * EDGE_SIZE; idx++) { _edges[idx] = NO_EDGE; } _edgeData = edgeDataArray; _edgeData.Resize(sizeEstimate * 3); _edgeShapes = edgeShapeArray; _edgeShapes.Resize(sizeEstimate * 3); }
public void TestSwitch() { OsmSharp.Math.Random.StaticRandomGenerator.Set(116542346); var box = new GeoCoordinateBox( new GeoCoordinate(90, 180), new GeoCoordinate(-90, -180)); var size = 100; var maxCollectionSize = 4; var referenceDictionary = new Dictionary <long, ICoordinateCollection>(); var coordinates = new HugeCoordinateCollectionIndex(400); for (int idx = 0; idx < size; idx++) { var currentSize = OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(maxCollectionSize) + 1; var coordinatesArray = new GeoCoordinate[currentSize]; while (currentSize > 0) { coordinatesArray[currentSize - 1] = box.GenerateRandomIn(OsmSharp.Math.Random.StaticRandomGenerator.Get()); currentSize--; } var coordinatesCollection = new CoordinateArrayCollection <GeoCoordinate>(coordinatesArray); referenceDictionary[idx] = coordinatesCollection; coordinates[idx] = coordinatesCollection; } // generate a sequence of two id's and switch. for (var i = 0; i < 20; i++) { var id1 = OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(size); var id2 = OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(size - 1); if (id1 <= id2) { id2++; } var temp = referenceDictionary[id1]; referenceDictionary[id1] = referenceDictionary[id2]; referenceDictionary[id2] = temp; coordinates.Switch(id1, id2); } // check result. for (int idx = 0; idx < size; idx++) { var referenceCollection = referenceDictionary[idx]; var collection = coordinates[idx]; referenceCollection.Reset(); collection.Reset(); while (referenceCollection.MoveNext()) { Assert.IsTrue(collection.MoveNext()); Assert.AreEqual(referenceCollection.Latitude, collection.Latitude); Assert.AreEqual(referenceCollection.Longitude, collection.Longitude); } Assert.IsFalse(collection.MoveNext()); } }
public void TestSerialize() { OsmSharp.Math.Random.StaticRandomGenerator.Set(116542346); var box = new GeoCoordinateBox( new GeoCoordinate(90, 180), new GeoCoordinate(-90, -180)); var size = 5; var maxCollectionSize = 4; var referenceDictionary = new Dictionary <long, ICoordinateCollection>(); var coordinates = new HugeCoordinateCollectionIndex(100); for (int idx = 0; idx < size; idx++) { var currentSize = OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(maxCollectionSize) + 1; var coordinatesArray = new GeoCoordinate[currentSize]; while (currentSize > 0) { coordinatesArray[currentSize - 1] = box.GenerateRandomIn(OsmSharp.Math.Random.StaticRandomGenerator.Get()); currentSize--; } var coordinatesCollection = new CoordinateArrayCollection <GeoCoordinate>(coordinatesArray); referenceDictionary[idx] = coordinatesCollection; coordinates[idx] = coordinatesCollection; } coordinates.Trim(); coordinates.Compress(); byte[] data = null; using (var stream = new MemoryStream()) { long length = coordinates.Serialize(stream); data = stream.ToArray(); Assert.AreEqual(168, length); Assert.AreEqual(data.Length, length); } var result = HugeCoordinateCollectionIndex.Deserialize(new MemoryStream(data)); // check result. for (int idx = 0; idx < size; idx++) { var referenceCollection = referenceDictionary[idx]; var collection = result[idx]; referenceCollection.Reset(); collection.Reset(); while (referenceCollection.MoveNext()) { Assert.IsTrue(collection.MoveNext()); Assert.AreEqual(referenceCollection.Latitude, collection.Latitude); Assert.AreEqual(referenceCollection.Longitude, collection.Longitude); } Assert.IsFalse(collection.MoveNext()); } result = HugeCoordinateCollectionIndex.Deserialize(new MemoryStream(data), true); // check result. for (int idx = 0; idx < size; idx++) { var referenceCollection = referenceDictionary[idx]; var collection = result[idx]; referenceCollection.Reset(); collection.Reset(); while (referenceCollection.MoveNext()) { Assert.IsTrue(collection.MoveNext()); Assert.AreEqual(referenceCollection.Latitude, collection.Latitude); Assert.AreEqual(referenceCollection.Longitude, collection.Longitude); } Assert.IsFalse(collection.MoveNext()); } }
/// <summary> /// Deserializes a graph from the given stream. /// </summary> /// <param name="stream">The stream to read from. Reading will start at position 0.</param> /// <param name="edgeDataSize">The edge data size.</param> /// <param name="mapFrom">The map from for the edge data.</param> /// <param name="mapTo">The map to for the edge data.</param> /// <param name="copy">Flag to make an in-memory copy.</param> /// <returns></returns> public new static DirectedGraph <TEdgeData> Deserialize(System.IO.Stream stream, int edgeDataSize, MappedHugeArray <TEdgeData, uint> .MapFrom mapFrom, MappedHugeArray <TEdgeData, uint> .MapTo mapTo, bool copy) { // read sizes. long position = 0; stream.Seek(4, System.IO.SeekOrigin.Begin); position = position + 4; var longBytes = new byte[8]; stream.Read(longBytes, 0, 8); position = position + 8; var vertexLength = BitConverter.ToInt64(longBytes, 0); stream.Read(longBytes, 0, 8); position = position + 8; var edgeLength = BitConverter.ToInt64(longBytes, 0); var bufferSize = 32; var cacheSize = MemoryMappedHugeArrayUInt32.DefaultCacheSize; var file = new MemoryMappedStream(new OsmSharp.IO.LimitedStream(stream)); var vertexArray = new MemoryMappedHugeArrayUInt32(file, (vertexLength + 1) * VERTEX_SIZE, (vertexLength + 1) * VERTEX_SIZE, bufferSize / 4, cacheSize * 4); position = position + ((vertexLength + 1) * VERTEX_SIZE * 4); var vertexCoordinateArray = new MappedHugeArray <GeoCoordinateSimple, float>( new MemoryMappedHugeArraySingle(file, (vertexLength + 1) * 2, (vertexLength + 1) * 2, bufferSize / 4, cacheSize * 4), 2, (array, idx, coordinate) => { array[idx] = coordinate.Latitude; array[idx + 1] = coordinate.Longitude; }, (Array, idx) => { return(new GeoCoordinateSimple() { Latitude = Array[idx], Longitude = Array[idx + 1] }); }); position = position + ((vertexLength + 1) * 2 * 4); var edgeArray = new MemoryMappedHugeArrayUInt32(file, edgeLength * EDGE_SIZE, edgeLength * EDGE_SIZE, bufferSize / 2, cacheSize * 4); position = position + (edgeLength * EDGE_SIZE * 4); var edgeDataArray = new MappedHugeArray <TEdgeData, uint>( new MemoryMappedHugeArrayUInt32(file, edgeLength * edgeDataSize, edgeLength * edgeDataSize, bufferSize * 2, cacheSize * 2), edgeDataSize, mapTo, mapFrom); position = position + (edgeLength * edgeDataSize * 4); // deserialize shapes. stream.Seek(position, System.IO.SeekOrigin.Begin); var cappedStream = new OsmSharp.IO.LimitedStream(stream); var shapes = HugeCoordinateCollectionIndex.Deserialize(cappedStream, copy); if (copy) { // copy the data. var vertexArrayCopy = new HugeArray <uint>(vertexArray.Length); vertexArrayCopy.CopyFrom(vertexArray); var vertexCoordinateArrayCopy = new HugeArray <GeoCoordinateSimple>(vertexCoordinateArray.Length); vertexCoordinateArrayCopy.CopyFrom(vertexCoordinateArray); var edgeArrayCopy = new HugeArray <uint>(edgeArray.Length); edgeArrayCopy.CopyFrom(edgeArray); var edgeDataArrayCopy = new HugeArray <TEdgeData>(edgeDataArray.Length); edgeDataArrayCopy.CopyFrom(edgeDataArray); file.Dispose(); return(new DirectedGraph <TEdgeData>(vertexCoordinateArrayCopy, vertexArrayCopy, edgeArrayCopy, edgeDataArrayCopy, shapes)); } return(new DirectedGraph <TEdgeData>(vertexCoordinateArray, vertexArray, edgeArray, edgeDataArray, shapes)); }
public void TestHugeCoordinateCollectionIndexSmall() { OsmSharp.Math.Random.StaticRandomGenerator.Set(116542346); var box = new GeoCoordinateBox( new GeoCoordinate(90, 180), new GeoCoordinate(-90, -180)); var size = 100; var maxCollectionSize = 4; var referenceDictionary = new Dictionary <long, ICoordinateCollection>(); var coordinates = new HugeCoordinateCollectionIndex(400); for (int idx = 0; idx < size; idx++) { var currentSize = OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(maxCollectionSize) + 1; var coordinatesArray = new GeoCoordinate[currentSize]; while (currentSize > 0) { coordinatesArray[currentSize - 1] = box.GenerateRandomIn(OsmSharp.Math.Random.StaticRandomGenerator.Get()); currentSize--; } var coordinatesCollection = new CoordinateArrayCollection <GeoCoordinate>(coordinatesArray); referenceDictionary[idx] = coordinatesCollection; coordinates[idx] = coordinatesCollection; } // check result. for (int idx = 0; idx < size; idx++) { var referenceCollection = referenceDictionary[idx]; var collection = coordinates[idx]; referenceCollection.Reset(); collection.Reset(); while (referenceCollection.MoveNext()) { Assert.IsTrue(collection.MoveNext()); Assert.AreEqual(referenceCollection.Latitude, collection.Latitude); Assert.AreEqual(referenceCollection.Longitude, collection.Longitude); } Assert.IsFalse(collection.MoveNext()); } // generate new randoms. for (int idx = 0; idx < size; idx++) { var currentSize = OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(maxCollectionSize) + 1; var coordinatesArray = new GeoCoordinate[currentSize]; while (currentSize > 0) { coordinatesArray[currentSize - 1] = box.GenerateRandomIn(OsmSharp.Math.Random.StaticRandomGenerator.Get()); currentSize--; } var coordinatesCollection = new CoordinateArrayCollection <GeoCoordinate>(coordinatesArray); referenceDictionary[idx] = coordinatesCollection; coordinates[idx] = coordinatesCollection; var referenceCollection = referenceDictionary[idx]; var collection = coordinates[idx]; referenceCollection.Reset(); collection.Reset(); while (referenceCollection.MoveNext()) { Assert.IsTrue(collection.MoveNext()); Assert.AreEqual(referenceCollection.Latitude, collection.Latitude); Assert.AreEqual(referenceCollection.Longitude, collection.Longitude); } Assert.IsFalse(collection.MoveNext()); } // check again. for (int idx = 0; idx < size; idx++) { var referenceCollection = referenceDictionary[idx]; var collection = coordinates[idx]; referenceCollection.Reset(); collection.Reset(); while (referenceCollection.MoveNext()) { Assert.IsTrue(collection.MoveNext()); Assert.AreEqual(referenceCollection.Latitude, collection.Latitude); Assert.AreEqual(referenceCollection.Longitude, collection.Longitude); } Assert.IsFalse(collection.MoveNext()); } // randomly remove stuff. for (int idx = 0; idx < size; idx++) { if (OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(2) > 1) { referenceDictionary[idx] = null; coordinates[idx] = null; } } // check again. for (int idx = 0; idx < size; idx++) { var referenceCollection = referenceDictionary[idx]; var collection = coordinates[idx]; if (referenceCollection == null) { Assert.IsNull(collection); } else { referenceCollection.Reset(); collection.Reset(); while (referenceCollection.MoveNext()) { Assert.IsTrue(collection.MoveNext()); Assert.AreEqual(referenceCollection.Latitude, collection.Latitude); Assert.AreEqual(referenceCollection.Longitude, collection.Longitude); } Assert.IsFalse(collection.MoveNext()); } } // generate new randoms. for (int idx = 0; idx < size; idx++) { var currentSize = OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(maxCollectionSize) + 1; var coordinatesArray = new GeoCoordinate[currentSize]; while (currentSize > 0) { coordinatesArray[currentSize - 1] = box.GenerateRandomIn(OsmSharp.Math.Random.StaticRandomGenerator.Get()); currentSize--; } var coordinatesCollection = new CoordinateArrayCollection <GeoCoordinate>(coordinatesArray); referenceDictionary[idx] = coordinatesCollection; coordinates[idx] = coordinatesCollection; var referenceCollection = referenceDictionary[idx]; var collection = coordinates[idx]; referenceCollection.Reset(); collection.Reset(); while (referenceCollection.MoveNext()) { Assert.IsTrue(collection.MoveNext()); Assert.AreEqual(referenceCollection.Latitude, collection.Latitude); Assert.AreEqual(referenceCollection.Longitude, collection.Longitude); } Assert.IsFalse(collection.MoveNext()); } // check again. for (int idx = 0; idx < size; idx++) { var referenceCollection = referenceDictionary[idx]; var collection = coordinates[idx]; referenceCollection.Reset(); collection.Reset(); while (referenceCollection.MoveNext()) { Assert.IsTrue(collection.MoveNext()); Assert.AreEqual(referenceCollection.Latitude, collection.Latitude); Assert.AreEqual(referenceCollection.Longitude, collection.Longitude); } Assert.IsFalse(collection.MoveNext()); } }