| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.IO; |
| | 4 | | using System.Runtime.CompilerServices; |
| | 5 | | using Itinero.Network; |
| | 6 | |
|
| | 7 | | [assembly: InternalsVisibleTo("Itinero.Tests")] |
| | 8 | | [assembly: InternalsVisibleTo("Itinero.Tests.Benchmarks")] |
| | 9 | | [assembly: InternalsVisibleTo("Itinero.Tests.Functional")] |
| | 10 | |
|
| | 11 | | namespace Itinero.IO.Osm.Tiles; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// A data structure to keep mappings between global vertex ids and vertices. |
| | 15 | | /// </summary> |
| | 16 | | internal class GlobalIdMap : IEnumerable<(long globalId, VertexId vertex)> |
| | 17 | | { |
| 2 | 18 | | private readonly Dictionary<long, VertexId> _vertexPerId = new(); |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Sets a new mapping. |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="globalVertexId">The global vertex id.</param> |
| | 24 | | /// <param name="vertex">The local vertex.</param> |
| | 25 | | public void Set(long globalVertexId, VertexId vertex) |
| 4 | 26 | | { |
| 4 | 27 | | _vertexPerId[globalVertexId] = vertex; |
| 4 | 28 | | } |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Gets a mapping if it exists. |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="globalVertexId">The global vertex id.</param> |
| | 34 | | /// <param name="vertex">The vertex associated with the given global vertex, if any.</param> |
| | 35 | | /// <returns>True if a mapping exists, false otherwise.</returns> |
| | 36 | | public bool TryGet(long globalVertexId, out VertexId vertex) |
| 2 | 37 | | { |
| 2 | 38 | | return _vertexPerId.TryGetValue(globalVertexId, out vertex); |
| 2 | 39 | | } |
| | 40 | |
|
| | 41 | | /// <inheritdoc/> |
| | 42 | | public IEnumerator<(long globalId, VertexId vertex)> GetEnumerator() |
| 0 | 43 | | { |
| 0 | 44 | | foreach (var pair in _vertexPerId) |
| 0 | 45 | | { |
| 0 | 46 | | yield return (pair.Key, pair.Value); |
| 0 | 47 | | } |
| 0 | 48 | | } |
| | 49 | |
|
| | 50 | | IEnumerator IEnumerable.GetEnumerator() |
| 0 | 51 | | { |
| 0 | 52 | | return this.GetEnumerator(); |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | internal long WriteTo(Stream stream) |
| 1 | 56 | | { |
| 1 | 57 | | var p = stream.Position; |
| | 58 | |
|
| | 59 | | // write header and version. |
| 1 | 60 | | stream.WriteWithSize($"{nameof(GlobalIdMap)}"); |
| 1 | 61 | | stream.WriteByte(1); |
| | 62 | |
|
| | 63 | | // write data. |
| 1 | 64 | | stream.WriteVarInt64(_vertexPerId.Count); |
| 7 | 65 | | foreach (var pair in _vertexPerId) |
| 2 | 66 | | { |
| 2 | 67 | | stream.WriteVarInt64(pair.Key); |
| 2 | 68 | | stream.WriteVarUInt32(pair.Value.TileId); |
| 2 | 69 | | stream.WriteVarUInt32(pair.Value.LocalId); |
| 2 | 70 | | } |
| | 71 | |
|
| 1 | 72 | | return stream.Position - p; |
| 1 | 73 | | } |
| | 74 | |
|
| | 75 | | internal static GlobalIdMap ReadFrom(Stream stream) |
| 1 | 76 | | { |
| | 77 | | // read & verify header. |
| 1 | 78 | | var header = stream.ReadWithSizeString(); |
| 1 | 79 | | var version = stream.ReadByte(); |
| 1 | 80 | | if (header != nameof(GlobalIdMap)) |
| 0 | 81 | | { |
| 0 | 82 | | throw new InvalidDataException($"Cannot read {nameof(GlobalIdMap)}: Header invalid."); |
| | 83 | | } |
| | 84 | |
|
| 1 | 85 | | if (version != 1) |
| 0 | 86 | | { |
| 0 | 87 | | throw new InvalidDataException($"Cannot read {nameof(GlobalIdMap)}: Version # invalid."); |
| | 88 | | } |
| | 89 | |
|
| | 90 | | // read size first |
| 1 | 91 | | var globalIdMap = new GlobalIdMap(); |
| 1 | 92 | | var size = stream.ReadVarInt64(); |
| 6 | 93 | | for (var p = 0; p < size; p++) |
| 2 | 94 | | { |
| 2 | 95 | | var nodeId = stream.ReadVarInt64(); |
| 2 | 96 | | var tileId = stream.ReadVarUInt32(); |
| 2 | 97 | | var localId = stream.ReadVarUInt32(); |
| | 98 | |
|
| 2 | 99 | | globalIdMap.Set(nodeId, new VertexId(tileId, localId)); |
| 2 | 100 | | } |
| | 101 | |
|
| 1 | 102 | | return globalIdMap; |
| 1 | 103 | | } |
| | 104 | | } |