| | 1 | | using System; |
| | 2 | |
|
| | 3 | | namespace Itinero.Network; |
| | 4 | |
|
| | 5 | | // TODO: the internal graph structure bleeds out via the tiled ids. |
| | 6 | | /// <summary> |
| | 7 | | /// Represents a vertex ID composed of a tile ID and a vertex ID. |
| | 8 | | /// </summary> |
| | 9 | | public readonly struct VertexId : IEquatable<VertexId> |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Creates a new vertex id. |
| | 13 | | /// </summary> |
| | 14 | | /// <param name="tileId">The tile id.</param> |
| | 15 | | /// <param name="localId">The local id.</param> |
| | 16 | | public VertexId(uint tileId, uint localId) |
| 3107 | 17 | | { |
| 3107 | 18 | | this.TileId = tileId; |
| 3107 | 19 | | this.LocalId = localId; |
| 3107 | 20 | | } |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Gets or sets the tile id. |
| | 24 | | /// </summary> |
| 7608 | 25 | | public uint TileId { get; } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Gets or sets the local id. |
| | 29 | | /// </summary> |
| 5585 | 30 | | public uint LocalId { get; } |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Returns an empty vertex id. |
| | 34 | | /// </summary> |
| 24 | 35 | | public static VertexId Empty => new(uint.MaxValue, uint.MaxValue); |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Returns true if this vertex id is empty. |
| | 39 | | /// </summary> |
| | 40 | | /// <returns></returns> |
| | 41 | | public bool IsEmpty() |
| 58 | 42 | | { |
| 58 | 43 | | return this.TileId == uint.MaxValue; |
| 58 | 44 | | } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Returns a human readable description. |
| | 48 | | /// </summary> |
| | 49 | | /// <returns></returns> |
| | 50 | | public override string ToString() |
| 7 | 51 | | { |
| 7 | 52 | | return $"{this.LocalId} @ {this.TileId}"; |
| 7 | 53 | | } |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Returns true if the two vertices represent the same id. |
| | 57 | | /// </summary> |
| | 58 | | /// <returns></returns> |
| | 59 | | public static bool operator ==(VertexId vertex1, VertexId vertex2) |
| 49 | 60 | | { |
| 49 | 61 | | return vertex1.LocalId == vertex2.LocalId && |
| 49 | 62 | | vertex1.TileId == vertex2.TileId; |
| 49 | 63 | | } |
| | 64 | |
|
| | 65 | | public static bool operator !=(VertexId vertex1, VertexId vertex2) |
| 49 | 66 | | { |
| 49 | 67 | | return !(vertex1 == vertex2); |
| 49 | 68 | | } |
| | 69 | |
|
| | 70 | | public bool Equals(VertexId other) |
| 140 | 71 | | { |
| 140 | 72 | | return this.LocalId == other.LocalId && this.TileId == other.TileId; |
| 140 | 73 | | } |
| | 74 | |
|
| | 75 | | public override bool Equals(object obj) |
| 0 | 76 | | { |
| 0 | 77 | | if (obj is null) |
| 0 | 78 | | { |
| 0 | 79 | | return false; |
| | 80 | | } |
| | 81 | |
|
| 0 | 82 | | return obj is VertexId other && this.Equals(other); |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | public override int GetHashCode() |
| 254 | 86 | | { |
| | 87 | | unchecked |
| 254 | 88 | | { |
| 254 | 89 | | return ((int)this.TileId * 397) ^ (int)this.LocalId; |
| | 90 | | } |
| 254 | 91 | | } |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// Encodes the info in this vertex into one 64bit unsigned integer. |
| | 95 | | /// </summary> |
| | 96 | | /// <returns>An encoded version of this vertex.</returns> |
| | 97 | | internal ulong Encode() |
| 13 | 98 | | { |
| 13 | 99 | | return ((ulong)this.TileId << 32) + this.LocalId; |
| 13 | 100 | | } |
| | 101 | |
|
| | 102 | | /// <summary> |
| | 103 | | /// Decodes the given encoded vertex id. |
| | 104 | | /// </summary> |
| | 105 | | /// <param name="encoded">The encoded version a vertex.</param> |
| | 106 | | /// <param name="tileId">The tile id.</param> |
| | 107 | | /// <param name="localId">The local id.</param> |
| | 108 | | /// <returns>The decoded version of the vertex.</returns> |
| | 109 | | internal static void Decode(ulong encoded, out uint tileId, out uint localId) |
| 14 | 110 | | { |
| 14 | 111 | | tileId = (uint)(encoded >> 32); |
| 14 | 112 | | var tileOffset = (ulong)tileId << 32; |
| 14 | 113 | | localId = (uint)(encoded - tileOffset); |
| 14 | 114 | | } |
| | 115 | |
|
| | 116 | | /// <summary> |
| | 117 | | /// Decodes the given encoded vertex id. |
| | 118 | | /// </summary> |
| | 119 | | /// <param name="encoded">The encoded version a vertex.</param> |
| | 120 | | /// <returns>The decoded version of the vertex.</returns> |
| | 121 | | internal static VertexId Decode(ulong encoded) |
| 0 | 122 | | { |
| 0 | 123 | | Decode(encoded, out var tileId, out var localId); |
| 0 | 124 | | return new VertexId(tileId, localId); |
| 0 | 125 | | } |
| | 126 | | } |