| | 1 | | namespace Itinero.IO.Osm.Collections; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// A cache for node coordinates. |
| | 5 | | /// </summary> |
| | 6 | | internal sealed class NodeIndex |
| | 7 | | { |
| | 8 | | private readonly UnsignedNodeIndex _negativeNodeIndex; |
| | 9 | | private readonly UnsignedNodeIndex _positiveNodeIndex; |
| | 10 | |
|
| 0 | 11 | | public NodeIndex() |
| 0 | 12 | | { |
| 0 | 13 | | _negativeNodeIndex = new UnsignedNodeIndex(); |
| 0 | 14 | | _positiveNodeIndex = new UnsignedNodeIndex(); |
| 0 | 15 | | } |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// Adds a node id to the index. |
| | 19 | | /// </summary> |
| | 20 | | public void AddId(long id) |
| 0 | 21 | | { |
| 0 | 22 | | if (id >= 0) |
| 0 | 23 | | { |
| 0 | 24 | | _positiveNodeIndex.AddId(id); |
| 0 | 25 | | } |
| | 26 | | else |
| 0 | 27 | | { |
| 0 | 28 | | _negativeNodeIndex.AddId(-id); |
| 0 | 29 | | } |
| 0 | 30 | | } |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Sorts and converts the index. |
| | 34 | | /// </summary> |
| | 35 | | public void SortAndConvertIndex() |
| 0 | 36 | | { |
| 0 | 37 | | _positiveNodeIndex.SortAndConvertIndex(); |
| 0 | 38 | | _negativeNodeIndex.SortAndConvertIndex(); |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Gets the node id at the given index. |
| | 43 | | /// </summary> |
| | 44 | | public long this[long idx] |
| | 45 | | { |
| | 46 | | get |
| 0 | 47 | | { |
| 0 | 48 | | if (idx >= _negativeNodeIndex.Count) |
| 0 | 49 | | { |
| 0 | 50 | | return _positiveNodeIndex[idx - _negativeNodeIndex.Count]; |
| | 51 | | } |
| | 52 | |
|
| 0 | 53 | | return _negativeNodeIndex[idx]; |
| 0 | 54 | | } |
| | 55 | | } |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Sets a vertex id for the given vertex. |
| | 59 | | /// </summary> |
| | 60 | | public void Set(long id, uint vertex) |
| 0 | 61 | | { |
| 0 | 62 | | if (id >= 0) |
| 0 | 63 | | { |
| 0 | 64 | | _positiveNodeIndex.Set(id, vertex); |
| 0 | 65 | | } |
| | 66 | | else |
| 0 | 67 | | { |
| 0 | 68 | | _negativeNodeIndex.Set(-id, vertex); |
| 0 | 69 | | } |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | /// <summary> |
| | 73 | | /// Gets the coordinate for the given node. |
| | 74 | | /// </summary> |
| | 75 | | public long TryGetIndex(long id) |
| 0 | 76 | | { |
| 0 | 77 | | if (id >= 0) |
| 0 | 78 | | { |
| 0 | 79 | | return _positiveNodeIndex.TryGetIndex(id); |
| | 80 | | } |
| | 81 | | else |
| 0 | 82 | | { |
| 0 | 83 | | var result = _negativeNodeIndex.TryGetIndex(-id); |
| 0 | 84 | | if (result == long.MaxValue) |
| 0 | 85 | | { |
| 0 | 86 | | return long.MaxValue; |
| | 87 | | } |
| | 88 | |
|
| 0 | 89 | | return -(result + 1); |
| | 90 | | } |
| 0 | 91 | | } |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// Sets the coordinate for the given index. |
| | 95 | | /// </summary> |
| | 96 | | public void SetIndex(long idx, float latitude, float longitude) |
| 0 | 97 | | { |
| 0 | 98 | | if (idx >= 0) |
| 0 | 99 | | { |
| 0 | 100 | | _positiveNodeIndex.SetIndex(idx, latitude, longitude); |
| 0 | 101 | | } |
| | 102 | | else |
| 0 | 103 | | { |
| 0 | 104 | | idx = -idx - 1; |
| 0 | 105 | | _negativeNodeIndex.SetIndex(idx, latitude, longitude); |
| 0 | 106 | | } |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | /// <summary> |
| | 110 | | /// Tries to get a core node and it's matching vertex. |
| | 111 | | /// </summary> |
| | 112 | | public bool TryGetCoreNode(long id, out uint vertex) |
| 0 | 113 | | { |
| 0 | 114 | | if (id >= 0) |
| 0 | 115 | | { |
| 0 | 116 | | return _positiveNodeIndex.TryGetCoreNode(id, out vertex); |
| | 117 | | } |
| | 118 | | else |
| 0 | 119 | | { |
| 0 | 120 | | return _negativeNodeIndex.TryGetCoreNode(-id, out vertex); |
| | 121 | | } |
| 0 | 122 | | } |
| | 123 | |
|
| | 124 | | /// <summary> |
| | 125 | | /// Gets all relevant info on the given node. |
| | 126 | | /// </summary> |
| | 127 | | public bool TryGetValue(long id, out float latitude, out float longitude, out bool isCore, out uint vertex, |
| | 128 | | out long idx) |
| 0 | 129 | | { |
| 0 | 130 | | if (id >= 0) |
| 0 | 131 | | { |
| 0 | 132 | | return _positiveNodeIndex.TryGetValue(id, out latitude, out longitude, out isCore, out vertex, out idx); |
| | 133 | | } |
| | 134 | | else |
| 0 | 135 | | { |
| 0 | 136 | | return _negativeNodeIndex.TryGetValue(-id, out latitude, out longitude, out isCore, out vertex, |
| 0 | 137 | | out idx); |
| | 138 | | } |
| 0 | 139 | | } |
| | 140 | | } |