< Summary

Class:Itinero.IO.Osm.Tiles.GlobalIdMap
Assembly:Itinero.IO.Osm.Tiles
File(s):/home/runner/work/routing2/routing2/src/Itinero.IO.Osm.Tiles/GlobalIdMap.cs
Covered lines:36
Uncovered lines:13
Coverable lines:49
Total lines:104
Line coverage:73.4% (36 of 49)
Covered branches:6
Total branches:10
Branch coverage:60% (6 of 10)
Tag:224_14471318300

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor()100%1100%
Set(...)100%1100%
TryGet(...)100%1100%
GetEnumerator()0%20%
System.Collections.IEnumerable.GetEnumerator()100%10%
WriteTo(...)100%2100%
ReadFrom(...)66.66%680%

File(s)

/home/runner/work/routing2/routing2/src/Itinero.IO.Osm.Tiles/GlobalIdMap.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using System.IO;
 4using System.Runtime.CompilerServices;
 5using Itinero.Network;
 6
 7[assembly: InternalsVisibleTo("Itinero.Tests")]
 8[assembly: InternalsVisibleTo("Itinero.Tests.Benchmarks")]
 9[assembly: InternalsVisibleTo("Itinero.Tests.Functional")]
 10
 11namespace Itinero.IO.Osm.Tiles;
 12
 13/// <summary>
 14/// A data structure to keep mappings between global vertex ids and vertices.
 15/// </summary>
 16internal class GlobalIdMap : IEnumerable<(long globalId, VertexId vertex)>
 17{
 218    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)
 426    {
 427        _vertexPerId[globalVertexId] = vertex;
 428    }
 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)
 237    {
 238        return _vertexPerId.TryGetValue(globalVertexId, out vertex);
 239    }
 40
 41    /// <inheritdoc/>
 42    public IEnumerator<(long globalId, VertexId vertex)> GetEnumerator()
 043    {
 044        foreach (var pair in _vertexPerId)
 045        {
 046            yield return (pair.Key, pair.Value);
 047        }
 048    }
 49
 50    IEnumerator IEnumerable.GetEnumerator()
 051    {
 052        return this.GetEnumerator();
 053    }
 54
 55    internal long WriteTo(Stream stream)
 156    {
 157        var p = stream.Position;
 58
 59        // write header and version.
 160        stream.WriteWithSize($"{nameof(GlobalIdMap)}");
 161        stream.WriteByte(1);
 62
 63        // write data.
 164        stream.WriteVarInt64(_vertexPerId.Count);
 765        foreach (var pair in _vertexPerId)
 266        {
 267            stream.WriteVarInt64(pair.Key);
 268            stream.WriteVarUInt32(pair.Value.TileId);
 269            stream.WriteVarUInt32(pair.Value.LocalId);
 270        }
 71
 172        return stream.Position - p;
 173    }
 74
 75    internal static GlobalIdMap ReadFrom(Stream stream)
 176    {
 77        // read & verify header.
 178        var header = stream.ReadWithSizeString();
 179        var version = stream.ReadByte();
 180        if (header != nameof(GlobalIdMap))
 081        {
 082            throw new InvalidDataException($"Cannot read {nameof(GlobalIdMap)}: Header invalid.");
 83        }
 84
 185        if (version != 1)
 086        {
 087            throw new InvalidDataException($"Cannot read {nameof(GlobalIdMap)}: Version # invalid.");
 88        }
 89
 90        // read size first
 191        var globalIdMap = new GlobalIdMap();
 192        var size = stream.ReadVarInt64();
 693        for (var p = 0; p < size; p++)
 294        {
 295            var nodeId = stream.ReadVarInt64();
 296            var tileId = stream.ReadVarUInt32();
 297            var localId = stream.ReadVarUInt32();
 98
 299            globalIdMap.Set(nodeId, new VertexId(tileId, localId));
 2100        }
 101
 1102        return globalIdMap;
 1103    }
 104}