< Summary

Class:Itinero.Network.Search.Islands.Islands
Assembly:Itinero
File(s):/home/runner/work/routing2/routing2/src/Itinero/Network/Search/Islands/Islands.cs
Covered lines:31
Uncovered lines:20
Coverable lines:51
Total lines:160
Line coverage:60.7% (31 of 51)
Covered branches:2
Total branches:4
Branch coverage:50% (2 of 4)
Tag:284_38118444584

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor()100%1100%
.ctor(...)100%10%
SetTileDone(...)100%1100%
GetTileDone(...)100%1100%
IsEdgeOnIsland(...)100%1100%
SetEdgeOnIsland(...)100%1100%
IsEdgeOnIsland(...)100%2100%
SetEdgeOnIsland(...)0%20%
ClearNonLocalIslandEdges()100%10%
IsEdgeLocal(...)100%1100%
SetEdgeLocal(...)100%1100%
Clone()100%10%

File(s)

/home/runner/work/routing2/routing2/src/Itinero/Network/Search/Islands/Islands.cs

#LineLine coverage
 1using System.Collections.Concurrent;
 2using System.Collections.Generic;
 3using System.Linq;
 4
 5namespace Itinero.Network.Search.Islands;
 6
 7internal class Islands
 8{
 9    // Read once per edge relaxation from every routing thread, written only
 10    // during classification. ContainsKey takes no lock, so readers never
 11    // contend. The byte value is unused — ConcurrentDictionary is the only
 12    // lock-free set in the framework.
 13    private readonly ConcurrentDictionary<uint, byte> _tiles; // tiles that have been processed.
 14    private readonly ConcurrentDictionary<EdgeId, byte> _islandEdges;
 15    private readonly ConcurrentDictionary<EdgeId, byte> _localEdges;
 16
 17    // Transient state used during the NonLocal classification pass: each
 18    // edge the classifier identifies as Island in the N-only subgraph is
 19    // recorded here. Once both passes complete and the coordinator has
 20    // computed _localEdges from the gap (NonLocal-Island ∩ NotIsland-Full
 21    // ∩ non-L), this set can be cleared. Not part of the persistent
 22    // storage contract.
 23    private readonly ConcurrentDictionary<EdgeId, byte> _nonLocalIslandEdges;
 24
 4425    internal Islands()
 4426    {
 4427        _tiles = new ConcurrentDictionary<uint, byte>();
 4428        _islandEdges = new ConcurrentDictionary<EdgeId, byte>();
 4429        _localEdges = new ConcurrentDictionary<EdgeId, byte>();
 4430        _nonLocalIslandEdges = new ConcurrentDictionary<EdgeId, byte>();
 4431    }
 32
 033    private Islands(IEnumerable<uint> tiles, IEnumerable<EdgeId> islandEdges, IEnumerable<EdgeId> localEdges)
 034    {
 035        _tiles = new ConcurrentDictionary<uint, byte>(tiles.Select(t => new KeyValuePair<uint, byte>(t, 0)));
 036        _islandEdges = new ConcurrentDictionary<EdgeId, byte>(islandEdges.Select(e => new KeyValuePair<EdgeId, byte>(e, 
 037        _localEdges = new ConcurrentDictionary<EdgeId, byte>(localEdges.Select(e => new KeyValuePair<EdgeId, byte>(e, 0)
 038        _nonLocalIslandEdges = new ConcurrentDictionary<EdgeId, byte>();
 039    }
 40
 41    /// <summary>
 42    /// Sets the tile as done.
 43    /// </summary>
 44    /// <param name="tileId">Sets the tile as done.</param>
 45    /// <returns>True if the tile is done.</returns>
 46    /// <remarks>
 47    /// Called last, after every SetEdgeOnIsland and SetEdgeLocal for the tile.
 48    /// Readers depend on that: a tile seen as done means its edge writes are
 49    /// already visible.
 50    /// </remarks>
 51    public bool SetTileDone(uint tileId)
 6452    {
 6453        return _tiles.TryAdd(tileId, 0);
 6454    }
 55
 56    /// <summary>
 57    /// Returns true if the given tile is done.
 58    /// </summary>
 59    /// <param name="tileId"></param>
 60    /// <returns></returns>
 61    public bool GetTileDone(uint tileId)
 1355162    {
 1355163        return _tiles.ContainsKey(tileId);
 1355164    }
 65
 66    /// <summary>
 67    /// Returns true if the given edge is on an island.
 68    /// </summary>
 69    /// <param name="edge"></param>
 70    /// <returns></returns>
 71    public bool IsEdgeOnIsland(EdgeId edge)
 8030172    {
 8030173        return _islandEdges.ContainsKey(edge);
 8030174    }
 75
 76    /// <summary>
 77    /// Marks the given edge as on an island.
 78    /// </summary>
 79    /// <param name="edge"></param>
 80    /// <returns></returns>
 81    public bool SetEdgeOnIsland(EdgeId edge)
 2582    {
 2583        return _islandEdges.TryAdd(edge, 0);
 2584    }
 85
 86    /// <summary>
 87    /// Kind-aware overload. Reads from <c>_islandEdges</c> for Full and from
 88    /// the transient <c>_nonLocalIslandEdges</c> for NonLocal. The latter is
 89    /// only meaningful while a NonLocal classification pass is in progress
 90    /// or hasn't been collapsed into <c>_localEdges</c> yet.
 91    /// </summary>
 92    public bool IsEdgeOnIsland(EdgeId edge, IslandKind kind)
 893    {
 894        if (kind == IslandKind.NonLocal)
 495        {
 496            return _nonLocalIslandEdges.ContainsKey(edge);
 97        }
 98
 499        return this.IsEdgeOnIsland(edge);
 8100    }
 101
 102    /// <summary>
 103    /// Kind-aware overload. Writes to <c>_islandEdges</c> for Full and to the
 104    /// transient <c>_nonLocalIslandEdges</c> for NonLocal.
 105    /// </summary>
 106    public bool SetEdgeOnIsland(EdgeId edge, IslandKind kind)
 0107    {
 0108        if (kind == IslandKind.NonLocal)
 0109        {
 0110            return _nonLocalIslandEdges.TryAdd(edge, 0);
 111        }
 112
 0113        return this.SetEdgeOnIsland(edge);
 0114    }
 115
 116    /// <summary>
 117    /// Discards the transient <c>_nonLocalIslandEdges</c> set used during
 118    /// NonLocal classification. Called by the build coordinator after
 119    /// <c>_localEdges</c> has been computed from the gap. After this, the
 120    /// only persistent state is <c>_islandEdges</c> and <c>_localEdges</c>.
 121    /// </summary>
 122    internal void ClearNonLocalIslandEdges()
 0123    {
 0124        _nonLocalIslandEdges.Clear();
 0125    }
 126
 127    /// <summary>
 128    /// Returns true if the edge is reachable but only via local-access edges —
 129    /// i.e. it is not an island, but every route to it from the main network
 130    /// crosses at least one L-tagged edge first. A typical example is an
 131    /// N-tagged road inside a residential subdivision whose only connection
 132    /// to the main road network goes through an <c>access=destination</c>
 133    /// gate road.
 134    ///
 135    /// L-tagged edges themselves are NOT stored here — they are recognised
 136    /// by their tag, which is build-time information on <c>EdgeFactor</c>.
 137    /// </summary>
 138    public bool IsEdgeLocal(EdgeId edge)
 77556139    {
 77556140        return _localEdges.ContainsKey(edge);
 77556141    }
 142
 143    /// <summary>
 144    /// Marks the given edge as reachable only via local-access edges. See
 145    /// <see cref="IsEdgeLocal"/> for what that means.
 146    /// </summary>
 147    public bool SetEdgeLocal(EdgeId edge)
 7148    {
 7149        return _localEdges.TryAdd(edge, 0);
 7150    }
 151
 152    internal Islands Clone()
 0153    {
 154        // Snapshot _tiles before the edge sets, so a tile seen as done always
 155        // comes with its edges. Reversed, the clone could hold a done tile whose
 156        // island edges are missing, which IsMainN reads as main-N.
 0157        var tiles = _tiles.Keys;
 0158        return new Islands(tiles, _islandEdges.Keys, _localEdges.Keys);
 0159    }
 160}