| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using Itinero.Routing.DataStructures; |
| | 6 | |
|
| | 7 | | namespace Itinero.Network.Search.Islands; |
| | 8 | |
|
| | 9 | | internal class IslandLabels : IEnumerable<(EdgeId edge, uint label)> |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// The not an island label. |
| | 13 | | /// </summary> |
| | 14 | | public const uint NotAnIslandLabel = 0; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Keeps a label for each edge. |
| | 18 | | /// - Starting each edge starts with it's own unique Id. |
| | 19 | | /// - When edges are bidirectionally connected they get take on the lowest Id of their neighbour. |
| | 20 | | /// </summary> |
| 13 | 21 | | private readonly Dictionary<EdgeId, uint> _labels = new(); |
| 13 | 22 | | private readonly Dictionary<uint, (uint size, bool final)> _islands = new(); // holds the size per island. |
| 13 | 23 | | private readonly IslandLabelGraph _labelGraph = new(); |
| | 24 | | private readonly int _maxIslandSize; |
| | 25 | |
|
| 13 | 26 | | internal IslandLabels(int maxIslandSize) |
| 13 | 27 | | { |
| 13 | 28 | | _maxIslandSize = maxIslandSize; |
| 13 | 29 | | _islands.Add(0, (uint.MaxValue, true)); |
| 13 | 30 | | _labelGraph.AddVertex(); |
| 13 | 31 | | } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Gets all the islands. |
| | 35 | | /// </summary> |
| | 36 | | public IEnumerable<(uint label, uint size, bool final)> Islands |
| | 37 | | { |
| | 38 | | get |
| 3 | 39 | | { |
| 15 | 40 | | foreach (var (label, (size, final)) in _islands) |
| 3 | 41 | | { |
| 3 | 42 | | yield return (label, size, final); |
| 3 | 43 | | } |
| 3 | 44 | | } |
| | 45 | | } |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// Creates a new label. |
| | 49 | | /// </summary> |
| | 50 | | /// <returns></returns> |
| | 51 | | public (uint label, uint size, bool final) AddNew(EdgeId edge, bool final = false) |
| 29 | 52 | | { |
| 29 | 53 | | if (_labels.ContainsKey(edge)) throw new ArgumentOutOfRangeException(nameof(edge)); |
| | 54 | |
|
| 29 | 55 | | var label = _labelGraph.AddVertex(); |
| | 56 | |
|
| 29 | 57 | | _labels[edge] = label; |
| 29 | 58 | | _islands[label] = (1, final); |
| | 59 | |
|
| 29 | 60 | | return (label, 1, final); |
| 29 | 61 | | } |
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// Adds an edge to an already existing island. |
| | 65 | | /// </summary> |
| | 66 | | /// <param name="label"></param> |
| | 67 | | /// <param name="edge"></param> |
| | 68 | | /// <returns></returns> |
| | 69 | | public (uint label, uint size, bool final) AddTo(uint label, EdgeId edge) |
| 3 | 70 | | { |
| 3 | 71 | | if (!_islands.TryGetValue(label, out var islandDetails)) throw new ArgumentOutOfRangeException(nameof(label)); |
| | 72 | |
|
| 3 | 73 | | if (islandDetails.size < uint.MaxValue) islandDetails.size += 1; |
| 3 | 74 | | _islands[label] = (islandDetails.size, final: islandDetails.final); |
| 3 | 75 | | _labels[edge] = label; |
| | 76 | |
|
| 3 | 77 | | if (_islands[label].size >= _maxIslandSize && |
| 3 | 78 | | label != NotAnIslandLabel) |
| 0 | 79 | | { |
| | 80 | | // this island just grew over the maximum. |
| 0 | 81 | | this.Merge([label, NotAnIslandLabel]); |
| 0 | 82 | | var labelDetails = _islands[NotAnIslandLabel]; |
| 0 | 83 | | return (NotAnIslandLabel, labelDetails.size, final: islandDetails.final); |
| | 84 | | } |
| | 85 | | else |
| 3 | 86 | | { |
| 3 | 87 | | var labelDetails = _islands[label]; |
| 3 | 88 | | return (label, labelDetails.size, final: islandDetails.final); |
| | 89 | | } |
| 3 | 90 | | } |
| | 91 | |
|
| | 92 | | /// <summary> |
| | 93 | | /// Connects the islands representing the two labels, tail -> head. |
| | 94 | | /// </summary> |
| | 95 | | /// <param name="tail">The tail label.</param> |
| | 96 | | /// <param name="head">The head label.</param> |
| | 97 | | internal bool ConnectTo(uint tail, uint head) |
| 39 | 98 | | { |
| 45 | 99 | | if (tail == head) return false; |
| | 100 | |
|
| 34 | 101 | | if (_labelGraph.HasEdge(tail, head)) return false; |
| | 102 | |
|
| 32 | 103 | | var headHasEdge = _labelGraph.HasEdge(head); |
| 32 | 104 | | _labelGraph.AddEdge(tail, head); |
| | 105 | |
|
| 46 | 106 | | if (headHasEdge) return this.FindLoops(head); |
| 18 | 107 | | return false; |
| 39 | 108 | | } |
| | 109 | |
|
| | 110 | | /// <summary> |
| | 111 | | /// Merges the two given islands. |
| | 112 | | /// </summary> |
| | 113 | | /// <param name="tail"></param> |
| | 114 | | /// <param name="head"></param> |
| | 115 | | internal void Merge(uint tail, uint head) |
| 0 | 116 | | { |
| 0 | 117 | | if (tail == head) return; |
| | 118 | |
|
| 0 | 119 | | if (!_labelGraph.HasVertex(tail)) throw new ArgumentException("Label does not exist", nameof(tail)); |
| 0 | 120 | | if (!_labelGraph.HasVertex(head)) throw new ArgumentException("Label does not exist", nameof(head)); |
| | 121 | |
|
| 0 | 122 | | this.Merge([tail, head]); |
| | 123 | |
|
| | 124 | | // var edgesAdded = false; |
| | 125 | | // if (!_labelGraph.HasEdge(tail, head)) |
| | 126 | | // { |
| | 127 | | // edgesAdded = true; |
| | 128 | | // _labelGraph.AddEdge(tail, head); |
| | 129 | | // } |
| | 130 | | // if (!_labelGraph.HasEdge(head, tail)) |
| | 131 | | // { |
| | 132 | | // edgesAdded = true; |
| | 133 | | // _labelGraph.AddEdge(head, tail); |
| | 134 | | // } |
| | 135 | | // if (!edgesAdded) return; |
| | 136 | | // |
| | 137 | | // this.FindLoops(head); |
| 0 | 138 | | } |
| | 139 | |
|
| | 140 | | /// <summary> |
| | 141 | | /// Gets the label for the given edge, if any. |
| | 142 | | /// </summary> |
| | 143 | | /// <param name="edge"></param> |
| | 144 | | /// <param name="label"></param> |
| | 145 | | /// <returns></returns> |
| | 146 | | public bool TryGet(EdgeId edge, out uint label) |
| 0 | 147 | | { |
| 0 | 148 | | return _labels.TryGetValue(edge, out label); |
| 0 | 149 | | } |
| | 150 | |
|
| | 151 | | /// <summary> |
| | 152 | | /// Gets the island label for the given edge and its size, if any. |
| | 153 | | /// </summary> |
| | 154 | | /// <param name="edge"></param> |
| | 155 | | /// <param name="island"></param> |
| | 156 | | /// <returns></returns> |
| | 157 | | public bool TryGetWithDetails(EdgeId edge, out (uint label, uint size, bool final) island) |
| 103 | 158 | | { |
| 103 | 159 | | island = (0, 0, false); |
| 135 | 160 | | if (!_labels.TryGetValue(edge, out var label)) return false; |
| | 161 | |
|
| 71 | 162 | | if (!_islands.TryGetValue(label, out var islandState)) |
| 0 | 163 | | throw new Exception("Island does not exist"); |
| | 164 | |
|
| 71 | 165 | | island = (label, islandState.size, final: islandState.final); |
| 71 | 166 | | return true; |
| 103 | 167 | | } |
| | 168 | |
|
| | 169 | | /// <summary> |
| | 170 | | /// Sets the given island as final, it cannot grow any further. |
| | 171 | | /// </summary> |
| | 172 | | /// <param name="label">The label of the island.</param> |
| | 173 | | public void SetAsFinal(uint label) |
| 8 | 174 | | { |
| 8 | 175 | | if (!_islands.TryGetValue(label, out var islandState)) |
| 0 | 176 | | throw new Exception("Island does not exist"); |
| | 177 | |
|
| 8 | 178 | | _islands[label] = (islandState.size, true); |
| 8 | 179 | | } |
| | 180 | |
|
| | 181 | | private bool FindLoops(uint label) |
| 14 | 182 | | { |
| 14 | 183 | | var pathTree = new PathTree(); |
| 14 | 184 | | var settled = new HashSet<uint>(); |
| 14 | 185 | | var queue = new Queue<uint>(); |
| 14 | 186 | | var loop = new HashSet<uint>(); // keeps all with a path back to label, initially only label. |
| | 187 | |
|
| 14 | 188 | | var loopFound = false; |
| 14 | 189 | | var enumerator = _labelGraph.GetEdgeEnumerator(); |
| 28 | 190 | | while (true) |
| 28 | 191 | | { |
| 28 | 192 | | if (!enumerator.MoveTo(label)) throw new Exception("Cannot search for loops on label that does not exist"); |
| | 193 | |
|
| 28 | 194 | | loop.Add(label); |
| 28 | 195 | | queue.Enqueue(pathTree.Add(label, uint.MaxValue)); |
| | 196 | |
|
| 71 | 197 | | while (queue.Count > 0) |
| 43 | 198 | | { |
| 43 | 199 | | var pointer = queue.Dequeue(); |
| 43 | 200 | | pathTree.Get(pointer, out var current, out var previous); |
| | 201 | |
|
| 43 | 202 | | if (!settled.Add(current)) continue; |
| 43 | 203 | | if (!enumerator.MoveTo(current)) continue; |
| | 204 | |
|
| 101 | 205 | | while (enumerator.MoveNext()) |
| 58 | 206 | | { |
| 58 | 207 | | var n = enumerator.Head; |
| 87 | 208 | | if (!enumerator.Forward) continue; |
| | 209 | |
|
| 29 | 210 | | if (loop.Contains(n)) |
| 14 | 211 | | { |
| | 212 | | // yay, a loop! |
| 14 | 213 | | loop.Add(current); |
| 29 | 214 | | while (previous != uint.MaxValue) |
| 15 | 215 | | { |
| 15 | 216 | | pathTree.Get(previous, out current, out previous); |
| 15 | 217 | | loop.Add(current); |
| 15 | 218 | | } |
| 14 | 219 | | } |
| | 220 | |
|
| 43 | 221 | | if (settled.Contains(n)) continue; |
| 15 | 222 | | queue.Enqueue(pathTree.Add(n, pointer)); |
| 15 | 223 | | } |
| 43 | 224 | | } |
| | 225 | |
|
| 42 | 226 | | if (loop.Count <= 1) return loopFound; |
| | 227 | |
|
| 14 | 228 | | label = this.Merge(loop); |
| 14 | 229 | | loopFound = true; |
| | 230 | |
|
| 14 | 231 | | loop.Clear(); |
| 14 | 232 | | pathTree.Clear(); |
| 14 | 233 | | settled.Clear(); |
| 14 | 234 | | queue.Clear(); |
| 14 | 235 | | } |
| 14 | 236 | | } |
| | 237 | |
|
| | 238 | | private uint Merge(HashSet<uint> labels) |
| 14 | 239 | | { |
| 19 | 240 | | while (true) |
| 19 | 241 | | { |
| | 242 | | // build a list of neighbours of the labels to be removed. |
| 19 | 243 | | var edgeEnumerator = _labelGraph.GetEdgeEnumerator(); |
| 19 | 244 | | var bestLabel = uint.MaxValue; |
| 19 | 245 | | var neighbours = new Dictionary<uint, (bool forward, bool backward)>(); |
| 135 | 246 | | foreach (var label in labels) |
| 39 | 247 | | { |
| 68 | 248 | | if (label < bestLabel) bestLabel = label; |
| 39 | 249 | | if (!edgeEnumerator.MoveTo(label)) continue; |
| | 250 | |
|
| 97 | 251 | | while (edgeEnumerator.MoveNext()) |
| 58 | 252 | | { |
| 58 | 253 | | var n = edgeEnumerator.Head; |
| 116 | 254 | | if (labels.Contains(n)) continue; |
| | 255 | |
|
| 0 | 256 | | (bool forward, bool backward) data = edgeEnumerator.Forward ? (true, false) : (false, true); |
| | 257 | |
|
| 0 | 258 | | if (neighbours.TryGetValue(n, out var existingData)) |
| 0 | 259 | | { |
| 0 | 260 | | data = (existingData.forward || data.forward, existingData.backward || data.backward); |
| 0 | 261 | | } |
| | 262 | |
|
| 0 | 263 | | neighbours[n] = data; |
| 0 | 264 | | } |
| 39 | 265 | | } |
| 19 | 266 | | if (bestLabel == uint.MaxValue) throw new Exception("Cannot merge empty loop"); |
| | 267 | |
|
| | 268 | | // add all edges to the neighbours again. |
| 57 | 269 | | foreach (var (neighbour, data) in neighbours) |
| 0 | 270 | | { |
| 0 | 271 | | if (data.forward) |
| 0 | 272 | | { |
| 0 | 273 | | if (!_labelGraph.HasEdge(bestLabel, neighbour)) _labelGraph.AddEdge(bestLabel, neighbour); |
| 0 | 274 | | } |
| | 275 | |
|
| 0 | 276 | | if (data.backward) |
| 0 | 277 | | { |
| 0 | 278 | | if (!_labelGraph.HasEdge(neighbour, bestLabel)) _labelGraph.AddEdge(neighbour, bestLabel); |
| 0 | 279 | | } |
| 0 | 280 | | } |
| | 281 | |
|
| 19 | 282 | | var bestData = _islands[bestLabel]; |
| 135 | 283 | | foreach (var label in labels) |
| 39 | 284 | | { |
| 58 | 285 | | if (label == bestLabel) continue; |
| 20 | 286 | | _labelGraph.RemoveVertex(label); |
| | 287 | |
|
| 20 | 288 | | var data = _islands[label]; |
| 31 | 289 | | if (bestData.size < uint.MaxValue) bestData.size += data.size; |
| 20 | 290 | | _islands.Remove(label); |
| 20 | 291 | | } |
| | 292 | |
|
| 19 | 293 | | _islands[bestLabel] = bestData; |
| | 294 | |
|
| 163 | 295 | | foreach (var k in _labels.Keys.ToArray()) |
| 53 | 296 | | { |
| 53 | 297 | | var val = _labels[k]; |
| 73 | 298 | | if (val == bestLabel) continue; |
| 36 | 299 | | if (!labels.Contains(val)) continue; |
| | 300 | |
|
| 30 | 301 | | _labels[k] = bestLabel; |
| 30 | 302 | | } |
| | 303 | |
|
| 28 | 304 | | if (bestLabel == NotAnIslandLabel) return bestLabel; |
| 10 | 305 | | if (_islands[bestLabel].size < _maxIslandSize) |
| 5 | 306 | | { |
| 5 | 307 | | return bestLabel; |
| | 308 | | } |
| | 309 | |
|
| 5 | 310 | | labels = [bestLabel, NotAnIslandLabel]; |
| 5 | 311 | | continue; |
| | 312 | | } |
| 14 | 313 | | } |
| | 314 | |
|
| | 315 | | public IEnumerator<(EdgeId edge, uint label)> GetEnumerator() |
| 0 | 316 | | { |
| 0 | 317 | | foreach (var (key, value) in _labels) |
| 0 | 318 | | { |
| 0 | 319 | | yield return (key, value); |
| 0 | 320 | | } |
| 0 | 321 | | } |
| | 322 | |
|
| | 323 | | IEnumerator IEnumerable.GetEnumerator() |
| 0 | 324 | | { |
| 0 | 325 | | return this.GetEnumerator(); |
| 0 | 326 | | } |
| | 327 | | } |