| | | 1 | | using System.Collections; |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using Itinero.Network; |
| | | 5 | | |
| | | 6 | | namespace Itinero.Data; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Abstract definition of a global id map for vertices. |
| | | 10 | | /// </summary> |
| | | 11 | | public sealed class GlobalVertexIdSet : IEnumerable<(long globalId, VertexId vertex)> |
| | | 12 | | { |
| | | 13 | | /// <remarks> |
| | | 14 | | /// Concurrent for the same reason as <see cref="GlobalEdgeIdSet"/>: written from several |
| | | 15 | | /// tile inserts at once. |
| | | 16 | | /// </remarks> |
| | 126 | 17 | | private readonly ConcurrentDictionary<long, VertexId> _set = new(); |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Sets a new mapping. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="globalVertexId">The global vertex id.</param> |
| | | 23 | | /// <param name="vertex">The local vertex.</param> |
| | | 24 | | public void Set(long globalVertexId, VertexId vertex) |
| | 0 | 25 | | { |
| | 0 | 26 | | _set[globalVertexId] = vertex; |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Removes a mapping. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="globalVertexId">The global vertex id.</param> |
| | | 33 | | public void Remove(long globalVertexId) |
| | 0 | 34 | | { |
| | 0 | 35 | | _set.TryRemove(globalVertexId, out _); |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets a mapping if it exists. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="globalVertexId">The global vertex id.</param> |
| | | 42 | | /// <param name="vertex">The vertex associated with the given global vertex, if any.</param> |
| | | 43 | | /// <returns>True if a mapping exists, false otherwise.</returns> |
| | | 44 | | public bool TryGet(long globalVertexId, out VertexId vertex) |
| | 0 | 45 | | { |
| | 0 | 46 | | return _set.TryGetValue(globalVertexId, out vertex); |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Returns an enumerator that iterates through the collection. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <returns>An enumerator that can be used to iterate through the collection.</returns> |
| | | 53 | | public IEnumerator<(long globalId, VertexId vertex)> GetEnumerator() |
| | 0 | 54 | | { |
| | 0 | 55 | | foreach (var (key, value) in _set) |
| | 0 | 56 | | { |
| | 0 | 57 | | yield return (key, value); |
| | 0 | 58 | | } |
| | 0 | 59 | | } |
| | | 60 | | |
| | | 61 | | IEnumerator IEnumerable.GetEnumerator() |
| | 0 | 62 | | { |
| | 0 | 63 | | return this.GetEnumerator(); |
| | 0 | 64 | | } |
| | | 65 | | } |