< Summary

Class:Itinero.Network.Tiles.Standalone.StandaloneNetworkTile
Assembly:Itinero
File(s):/home/runner/work/routing2/routing2/src/Itinero/Network/Tiles/Standalone/StandaloneNetworkTile.Attributes.cs
/home/runner/work/routing2/routing2/src/Itinero/Network/Tiles/Standalone/StandaloneNetworkTile.Boundaries.cs
/home/runner/work/routing2/routing2/src/Itinero/Network/Tiles/Standalone/StandaloneNetworkTile.cs
/home/runner/work/routing2/routing2/src/Itinero/Network/Tiles/Standalone/StandaloneNetworkTile.Global.cs
/home/runner/work/routing2/routing2/src/Itinero/Network/Tiles/Standalone/StandaloneNetworkTile.Serialization.cs
Covered lines:107
Uncovered lines:184
Coverable lines:291
Total lines:478
Line coverage:36.7% (107 of 291)
Covered branches:20
Total branches:76
Branch coverage:26.3% (20 of 76)
Tag:224_14471318300

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%1100%
SetAttributes(...)62.5%862.5%
GetAttributes()83.33%688.23%
AddOrGetString(...)66.66%670.58%
WriteAttributesTo(...)0%40%
ReadAttributesFrom(...)0%40%
.ctor(...)100%1100%
AddBoundaryCrossing(...)50%493.33%
GetBoundaryCrossings()100%2100%
.ctor(...)100%1100%
get_NetworkTile()100%1100%
GetEnumerator()100%1100%
get_TileId()100%1100%
.ctor(...)100%1100%
AddGlobalTurnCosts(...)0%120%
GetGlobalTurnCost()0%80%
AddGlobalIdFor(...)100%2100%
AddGlobalIdFor(...)0%20%
GetGlobalEdgeIds()0%40%
WriteTo(...)100%10%
WriteEdgesAndVerticesTo(...)0%20%
WriteGlobal(...)0%40%
ReadFrom(...)0%20%
ReadEdgesAndVerticesFrom(...)0%20%
ReadGlobal(...)0%40%

File(s)

/home/runner/work/routing2/routing2/src/Itinero/Network/Tiles/Standalone/StandaloneNetworkTile.Attributes.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.IO;
 3using Itinero.IO;
 4using Itinero.Network.Storage;
 5using Reminiscence.Arrays;
 6
 7namespace Itinero.Network.Tiles.Standalone;
 8
 9public partial class StandaloneNetworkTile
 10{
 11    /// <summary>
 12    /// Stores the attributes, starting with the number of attributes and then alternating key-value pairs.
 13    /// </summary>
 14    private readonly ArrayBase<byte> _attributes;
 15
 316    private uint _nextAttributePointer = 0;
 17
 18    /// <summary>
 19    /// Stores each string once.
 20    /// </summary>
 21    private readonly ArrayBase<string> _strings;
 22
 323    private uint _nextStringId = 0;
 24
 25    private uint SetAttributes(IEnumerable<(string key, string value)> attributes)
 126    {
 127        var start = _nextAttributePointer;
 28
 129        long cPos = start;
 130        long p = start + 1;
 131        var c = 0;
 732        foreach (var (key, value) in attributes)
 233        {
 234            if (_attributes.Length <= p + 16)
 035            {
 036                _attributes.Resize(_attributes.Length + 256);
 037            }
 38
 239            var id = this.AddOrGetString(key);
 240            p += _attributes.SetDynamicUInt32(p, id);
 241            id = this.AddOrGetString(value);
 242            p += _attributes.SetDynamicUInt32(p, id);
 43
 244            c++;
 245            if (c == 255)
 046            {
 047                _attributes[cPos] = 255;
 048                c = 0;
 049                cPos = p;
 050                p++;
 051            }
 252        }
 53
 154        if (_attributes.Length <= cPos)
 055        {
 056            _attributes.Resize(_attributes.Length + 256);
 057        }
 58
 159        _attributes[cPos] = (byte)c;
 60
 161        _nextAttributePointer = (uint)p;
 62
 163        return start;
 164    }
 65
 66    internal IEnumerable<(string key, string value)> GetAttributes(uint? pointer)
 167    {
 168        if (pointer == null)
 069        {
 070            yield break;
 71        }
 72
 173        var p = pointer.Value;
 74
 175        var count = -1;
 76        do
 177        {
 178            count = _attributes[p];
 179            p++;
 80
 681            for (var i = 0; i < count; i++)
 282            {
 283                p += (uint)_attributes.GetDynamicUInt32(p, out var keyId);
 284                p += (uint)_attributes.GetDynamicUInt32(p, out var valId);
 85
 286                yield return (_strings[keyId], _strings[valId]);
 287            }
 288        } while (count == 255);
 189    }
 90
 91    private uint AddOrGetString(string s)
 492    {
 2093        for (uint i = 0; i < _nextStringId; i++)
 694        {
 695            var existing = _strings[i];
 696            if (existing == s)
 097            {
 098                return i;
 99            }
 6100        }
 101
 4102        if (_strings.Length <= _nextStringId)
 0103        {
 0104            _strings.Resize(_strings.Length + 256);
 0105        }
 106
 4107        var id = _nextStringId;
 4108        _nextStringId++;
 109
 4110        _strings[id] = s;
 4111        return id;
 4112    }
 113
 114    private void WriteAttributesTo(Stream stream)
 0115    {
 0116        stream.WriteVarUInt32(_nextAttributePointer);
 0117        for (var i = 0; i < _nextAttributePointer; i++)
 0118        {
 0119            stream.WriteByte(_attributes[i]);
 0120        }
 121
 0122        stream.WriteVarUInt32(_nextStringId);
 0123        for (var i = 0; i < _nextStringId; i++)
 0124        {
 0125            stream.WriteWithSize(_strings[i]);
 0126        }
 0127    }
 128
 129    private void ReadAttributesFrom(Stream stream)
 0130    {
 0131        _nextAttributePointer = stream.ReadVarUInt32();
 0132        _attributes.Resize(_nextAttributePointer);
 0133        for (var i = 0; i < _nextAttributePointer; i++)
 0134        {
 0135            _attributes[i] = (byte)stream.ReadByte();
 0136        }
 137
 0138        _nextStringId = stream.ReadVarUInt32();
 0139        _strings.Resize(_nextStringId);
 0140        for (var i = 0; i < _nextStringId; i++)
 0141        {
 0142            _strings[i] = stream.ReadWithSizeString();
 0143        }
 0144    }
 145}

/home/runner/work/routing2/routing2/src/Itinero/Network/Tiles/Standalone/StandaloneNetworkTile.Boundaries.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Diagnostics.Tracing;
 4using Itinero.Data;
 5using Itinero.Network.Storage;
 6using Reminiscence.Arrays;
 7
 8namespace Itinero.Network.Tiles.Standalone;
 9
 10public partial class StandaloneNetworkTile
 11{
 312    private readonly ArrayBase<byte> _crossings = new MemoryArray<byte>(1024);
 313    private uint _crossingsPointer = 0;
 14
 15    internal BoundaryEdgeId AddBoundaryCrossing(bool isToTile, long globalIdFrom, long globalIdTo, VertexId vertex,
 16        IEnumerable<(string key, string value)> attributes, uint edgeTypeId, uint length)
 117    {
 118        if (vertex.TileId != this.NetworkTile.TileId)
 019            throw new ArgumentException("Can only add boundary crossings that cross into the tile");
 20
 121        var id = new BoundaryEdgeId(_crossingsPointer);
 122        _crossings.EnsureMinimumSize(_crossingsPointer + 36);
 123        _crossingsPointer += (uint)_crossings.SetDynamicUInt32(_crossingsPointer, isToTile ? (uint)1 : 0);
 124        _crossingsPointer += (uint)_crossings.SetDynamicInt64(_crossingsPointer, globalIdFrom);
 125        _crossingsPointer += (uint)_crossings.SetDynamicInt64(_crossingsPointer, globalIdTo);
 126        _crossingsPointer += (uint)_crossings.SetDynamicUInt32(_crossingsPointer, vertex.LocalId);
 127        _crossingsPointer += (uint)_crossings.SetDynamicUInt32(_crossingsPointer, edgeTypeId);
 128        _crossingsPointer += (uint)_crossings.SetDynamicUInt32(_crossingsPointer, length);
 29
 130        var a = this.SetAttributes(attributes);
 131        _crossingsPointer += (uint)_crossings.SetDynamicUInt32(_crossingsPointer, a);
 32
 133        return id;
 134    }
 35
 36    /// <summary>
 37    /// Gets all boundary crossing edges.
 38    /// </summary>
 39    /// <returns>An enumerable with all boundary crossing edges.</returns>
 40    public IEnumerable<(BoundaryEdgeId id, bool isToTile, long globalIdFrom, long globalIdTo, VertexId vertex, uint edge
 41        length,
 42        IEnumerable<(string key, string value)> attributes)> GetBoundaryCrossings()
 143    {
 144        var pointer = 0L;
 245        while (pointer < _crossingsPointer)
 146        {
 147            var id = new BoundaryEdgeId((uint)pointer);
 148            pointer += _crossings.GetDynamicUInt32(pointer, out var direction);
 149            pointer += _crossings.GetDynamicInt64(pointer, out var globalIdFrom);
 150            pointer += _crossings.GetDynamicInt64(pointer, out var globalIdTo);
 151            pointer += _crossings.GetDynamicUInt32(pointer, out var vertexLocalId);
 152            pointer += _crossings.GetDynamicUInt32(pointer, out var edgeTypeId);
 153            pointer += _crossings.GetDynamicUInt32(pointer, out var length);
 154            pointer += _crossings.GetDynamicUInt32(pointer, out var a);
 55
 156            yield return (id, direction != 0, globalIdFrom, globalIdTo,
 157                new VertexId(this.NetworkTile.TileId, vertexLocalId), edgeTypeId, length, this.GetAttributes(a));
 158        }
 159    }
 60}

/home/runner/work/routing2/routing2/src/Itinero/Network/Tiles/Standalone/StandaloneNetworkTile.cs

#LineLine coverage
 1using Reminiscence.Arrays;
 2
 3namespace Itinero.Network.Tiles.Standalone;
 4
 5/// <summary>
 6/// A standalone network tile.
 7/// </summary>
 8public partial class StandaloneNetworkTile
 9{
 310    internal StandaloneNetworkTile(NetworkTile networkTile)
 311    {
 312        _attributes = new MemoryArray<byte>(1024);
 313        _strings = new MemoryArray<string>(128);
 14
 315        this.NetworkTile = networkTile;
 316    }
 17
 5618    internal NetworkTile NetworkTile { get; }
 19
 20    /// <summary>
 21    /// Gets an enumerator to access the non-boundary edges.
 22    /// </summary>
 23    /// <returns></returns>
 24    public IStandaloneNetworkTileEnumerator GetEnumerator()
 625    {
 626        var enumerator = new NetworkTileEnumerator();
 627        enumerator.MoveTo(this.NetworkTile);
 628        return enumerator;
 629    }
 30
 31    /// <summary>
 32    /// Gets the tile id.
 33    /// </summary>
 3134    public uint TileId => this.NetworkTile.TileId;
 35}

/home/runner/work/routing2/routing2/src/Itinero/Network/Tiles/Standalone/StandaloneNetworkTile.Global.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using Itinero.Network.Storage;
 4using Itinero.Network.TurnCosts;
 5using Reminiscence.Arrays;
 6
 7namespace Itinero.Network.Tiles.Standalone;
 8
 9public partial class StandaloneNetworkTile
 10{
 311    private uint _turnCostPointer = 0;
 312    private readonly ArrayBase<byte> _turnCosts = new MemoryArray<byte>(0);
 13
 14    internal void AddGlobalTurnCosts((Guid globalEdgeId, bool forward)[] edges, uint[,] costs, uint turnCostType,
 15        IEnumerable<(string key, string value)> attributes)
 016    {
 017        if (edges.Length > OrderCoder.MaxOrderHeadTail)
 018        {
 019            throw new ArgumentException(
 020                $"Cannot add turn costs for vertices with more than {OrderCoder.MaxOrderHeadTail} edges.");
 21        }
 22
 23        // make sure there is space in the turn cost array.
 024        var maxLength = _turnCostPointer + 5 + 5 + 5 +
 025                        (edges.Length * (16 + 5)) +
 026                        (costs.GetLength(0) * costs.GetLength(1) * 5);
 027        while (_turnCosts.Length < maxLength)
 028        {
 029            _turnCosts.Resize(_turnCosts.Length + 256);
 030        }
 31
 32        // add turn.
 033        var a = this.SetAttributes(attributes);
 034        _turnCostPointer += (uint)_turnCosts.SetDynamicUInt32(_turnCostPointer, a);
 035        _turnCostPointer += (uint)_turnCosts.SetDynamicUInt32(_turnCostPointer, turnCostType);
 036        _turnCostPointer += (uint)_turnCosts.SetDynamicUInt32(_turnCostPointer, (uint)edges.Length);
 037        for (var i = 0; i < edges.Length; i++)
 038        {
 039            var (globalEdgeId, forward) = edges[i];
 40
 041            _turnCostPointer += (uint)_turnCosts.SetGuid(_turnCostPointer, globalEdgeId);
 042            _turnCostPointer += (uint)_turnCosts.SetDynamicUInt32(_turnCostPointer, (uint)(forward ? 1 : 0));
 043        }
 44
 045        for (var x = 0; x < costs.GetLength(0); x++)
 046            for (var y = 0; y < costs.GetLength(1); y++)
 047            {
 048                _turnCostPointer += (uint)_turnCosts.SetDynamicUInt32(_turnCostPointer, costs[x, y]);
 049            }
 050    }
 51
 52    /// <summary>
 53    /// Gets all the global turn costs.
 54    /// </summary>
 55    /// <returns></returns>
 56    public IEnumerable<((Guid globalEdgeId, bool forward)[] edges, uint[,] costs, uint turnCostType,
 57        IEnumerable<(string key, string value)> attributes)> GetGlobalTurnCost()
 058    {
 059        var pointer = 0L;
 060        while (pointer < _turnCostPointer)
 061        {
 062            pointer += _turnCosts.GetDynamicUInt32(pointer, out var a);
 063            pointer += _turnCosts.GetDynamicUInt32(pointer, out var turnCostType);
 064            pointer += _turnCosts.GetDynamicUInt32(pointer, out var edgeCount);
 065            var edges = new (Guid globalEdgeId, bool forward)[edgeCount];
 066            for (var i = 0; i < edgeCount; i++)
 067            {
 068                _turnCostPointer += (uint)_turnCosts.GetGuid(_turnCostPointer, out var globalEdgeId);
 069                _turnCostPointer += (uint)_turnCosts.GetDynamicUInt32(_turnCostPointer, out var forwardValue);
 070                var forward = forwardValue == 1;
 71
 072                edges[i] = (globalEdgeId, forward);
 073            }
 74
 075            var costs = new uint[edges.Length, edges.Length];
 076            for (var x = 0; x < costs.GetLength(0); x++)
 077                for (var y = 0; y < costs.GetLength(1); y++)
 078                {
 079                    pointer += _crossings.GetDynamicUInt32(pointer, out var cost);
 080                    costs[x, y] = cost;
 081                }
 82
 083            yield return (edges, costs, turnCostType, this.GetAttributes(a));
 084        }
 085    }
 86
 387    private uint _globalIdPointer = 0;
 388    private readonly ArrayBase<byte> _globalIds = new MemoryArray<byte>(0);
 89
 90    internal void AddGlobalIdFor(EdgeId edgeId, Guid globalEdgeId)
 291    {
 92        // make sure there is space.
 293        var maxLength = _globalIdPointer + 16 + 5;
 394        while (_globalIds.Length < maxLength)
 195        {
 196            _globalIds.Resize(_globalIds.Length + 64);
 197        }
 98
 299        _globalIdPointer += (uint)_globalIds.SetGuid(_globalIdPointer, globalEdgeId);
 2100        _globalIdPointer += (uint)_globalIds.SetDynamicInt32(_globalIdPointer, (int)edgeId.LocalId);
 2101    }
 102
 103    internal void AddGlobalIdFor(BoundaryEdgeId boundaryEdgeId, Guid globalEdgeId)
 0104    {
 105        // make sure there is space.
 0106        var maxLength = _globalIdPointer + 16 + 5;
 0107        while (_globalIds.Length < maxLength)
 0108        {
 0109            _globalIds.Resize(_globalIds.Length + 64);
 0110        }
 111
 0112        _globalIdPointer += (uint)_globalIds.SetGuid(_globalIdPointer, globalEdgeId);
 0113        _globalIdPointer += (uint)_globalIds.SetDynamicInt32(_globalIdPointer, (int)-(boundaryEdgeId.LocalId + 1));
 0114    }
 115
 116    /// <summary>
 117    /// Gets all the global edge ids.
 118    /// </summary>
 119    /// <returns></returns>
 120    public IEnumerable<(Guid globalEdgeId, EdgeId? edgeId, BoundaryEdgeId? boundaryEdgeId)> GetGlobalEdgeIds()
 0121    {
 0122        var pointer = 0L;
 0123        while (pointer < _turnCostPointer)
 0124        {
 0125            pointer += _turnCosts.GetGuid(pointer, out var globalEdgeId);
 0126            pointer += _turnCosts.GetDynamicInt32(pointer, out var localId);
 127
 0128            if (localId >= 0)
 0129            {
 0130                yield return (globalEdgeId, new EdgeId(this.TileId, (uint)localId), null);
 0131            }
 132            else
 0133            {
 0134                yield return (globalEdgeId, null, new BoundaryEdgeId((uint)-localId - 1));
 0135            }
 0136        }
 0137    }
 138}

/home/runner/work/routing2/routing2/src/Itinero/Network/Tiles/Standalone/StandaloneNetworkTile.Serialization.cs

#LineLine coverage
 1using System.IO;
 2using Itinero.IO;
 3
 4namespace Itinero.Network.Tiles.Standalone;
 5
 6public partial class StandaloneNetworkTile
 7{
 8    public void WriteTo(Stream stream)
 09    {
 010        var version = 1;
 011        stream.WriteVarInt32(version);
 12
 13        // write base tile.
 014        this.NetworkTile.WriteTo(stream);
 15
 16        // write edges and vertices for boundary crossings.
 017        this.WriteEdgesAndVerticesTo(stream);
 18
 19        // write attributes.
 020        this.WriteAttributesTo(stream);
 21
 22        // write global ids.
 023        this.WriteGlobal(stream);
 024    }
 25
 26    private void WriteEdgesAndVerticesTo(Stream stream)
 027    {
 028        stream.WriteVarUInt32(_crossingsPointer);
 029        for (var i = 0; i < _crossingsPointer; i++)
 030        {
 031            stream.WriteByte(_crossings[i]);
 032        }
 033    }
 34
 35    private void WriteGlobal(Stream stream)
 036    {
 037        stream.WriteVarUInt32(_globalIdPointer);
 038        for (var i = 0; i < _globalIdPointer; i++)
 039        {
 040            stream.WriteByte(_globalIds[i]);
 041        }
 42
 043        stream.WriteVarUInt32(_turnCostPointer);
 044        for (var i = 0; i < _turnCostPointer; i++)
 045        {
 046            stream.WriteByte(_turnCosts[i]);
 047        }
 048    }
 49
 50    public static StandaloneNetworkTile ReadFrom(Stream stream)
 051    {
 052        var version = stream.ReadVarInt32();
 053        if (version != 1)
 054        {
 055            throw new InvalidDataException("Cannot deserialize tiles: Invalid version #.");
 56        }
 57
 058        var networkTile = NetworkTile.ReadFrom(stream);
 059        var standaloneNetworkTile = new StandaloneNetworkTile(networkTile);
 60
 61        // read boundary crossings.
 062        standaloneNetworkTile.ReadEdgesAndVerticesFrom(stream);
 63
 64        // read attributes.
 065        standaloneNetworkTile.ReadAttributesFrom(stream);
 66
 67        // read global.
 068        standaloneNetworkTile.ReadGlobal(stream);
 69
 070        return standaloneNetworkTile;
 071    }
 72
 73    private void ReadEdgesAndVerticesFrom(Stream stream)
 074    {
 75        // read vertex pointers.
 076        _crossingsPointer = stream.ReadVarUInt32();
 077        _crossings.Resize(_crossingsPointer);
 078        for (var i = 0; i < _crossingsPointer; i++)
 079        {
 080            _crossings[i] = (byte)stream.ReadByte();
 081        }
 082    }
 83
 84    private void ReadGlobal(Stream stream)
 085    {
 086        _globalIdPointer = stream.ReadVarUInt32();
 087        _globalIds.Resize(_globalIdPointer);
 088        for (var i = 0; i < _globalIdPointer; i++)
 089        {
 090            _globalIds[i] = (byte)stream.ReadByte();
 091        }
 92
 093        _turnCostPointer = stream.ReadVarUInt32();
 094        _turnCosts.Resize(_turnCostPointer);
 095        for (var i = 0; i < _turnCostPointer; i++)
 096        {
 097            _turnCosts[i] = (byte)stream.ReadByte();
 098        }
 099    }
 100}