| | 1 | | using System.Collections.Generic; |
| | 2 | | using Itinero.Network.Tiles; |
| | 3 | |
|
| | 4 | | namespace Itinero.Network.Enumerators.Vertices; |
| | 5 | |
|
| | 6 | | internal class RoutingNetworkVertexEnumerator |
| | 7 | | { |
| | 8 | | private readonly RoutingNetwork _routingNetwork; |
| | 9 | | private readonly IEnumerator<uint> _tileEnumerator; |
| | 10 | |
|
| 15 | 11 | | public RoutingNetworkVertexEnumerator(RoutingNetwork routingNetwork) |
| 15 | 12 | | { |
| 15 | 13 | | _routingNetwork = routingNetwork; |
| 15 | 14 | | _tileEnumerator = routingNetwork.GetTileEnumerator(); |
| | 15 | |
|
| 15 | 16 | | this.Current = VertexId.Empty; |
| 15 | 17 | | } |
| | 18 | |
|
| 15 | 19 | | private long _localId = -1; |
| 15 | 20 | | private uint _tileId = uint.MaxValue; |
| | 21 | | private NetworkTile? _tile; |
| | 22 | |
|
| | 23 | | private void MoveNexTile() |
| 26 | 24 | | { |
| 729802 | 25 | | while (true) |
| 729802 | 26 | | { |
| 729802 | 27 | | if (!_tileEnumerator.MoveNext()) |
| 12 | 28 | | { |
| 12 | 29 | | return; |
| | 30 | | } |
| | 31 | |
|
| | 32 | | // get tile for reading. |
| 729790 | 33 | | _tileId = _tileEnumerator.Current; |
| 729790 | 34 | | _tile = _routingNetwork.GetTileForRead(_tileId); |
| | 35 | |
|
| 729790 | 36 | | if (_tile != null) |
| 14 | 37 | | { |
| 14 | 38 | | return; |
| | 39 | | } |
| 729776 | 40 | | } |
| 26 | 41 | | } |
| | 42 | |
|
| | 43 | | public bool MoveNext() |
| 35 | 44 | | { |
| 46 | 45 | | while (true) |
| 46 | 46 | | { |
| | 47 | | // when vertex is empty move to the first tile. |
| 46 | 48 | | if (this.Current.IsEmpty()) |
| 15 | 49 | | { |
| 15 | 50 | | this.MoveNexTile(); |
| 15 | 51 | | } |
| | 52 | |
|
| | 53 | | // no current tile here, no data. |
| 46 | 54 | | if (_tile == null) |
| 12 | 55 | | { |
| 12 | 56 | | return false; |
| | 57 | | } |
| | 58 | |
|
| | 59 | | // move to the next vertex. |
| 34 | 60 | | _localId++; |
| 34 | 61 | | this.Current = new VertexId(_tileId, (uint)_localId); |
| | 62 | |
|
| | 63 | | // get vertex, if it exists, return true. |
| | 64 | | // TODO: this check can be done faster without reading coordinates. |
| 34 | 65 | | if (_tile.HasVertex(this.Current)) |
| 23 | 66 | | { |
| 23 | 67 | | return true; |
| | 68 | | } |
| | 69 | |
|
| | 70 | | // vertex doesn't exist, move to the next tile. |
| 11 | 71 | | _localId = -1; |
| 11 | 72 | | this.MoveNexTile(); |
| 11 | 73 | | } |
| 35 | 74 | | } |
| | 75 | |
|
| 148 | 76 | | public VertexId Current { get; private set; } |
| | 77 | |
|
| | 78 | | public void Reset() |
| 0 | 79 | | { |
| 0 | 80 | | this.Current = VertexId.Empty; |
| 0 | 81 | | _localId = -1; |
| 0 | 82 | | _tileId = uint.MaxValue; |
| | 83 | |
|
| 0 | 84 | | _tileEnumerator.Reset(); |
| 0 | 85 | | } |
| | 86 | | } |