| | | 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) |
| | 38 | 98 | | { |
| | 44 | 99 | | if (tail == head) return false; |
| | | 100 | | |
| | 35 | 101 | | if (_labelGraph.HasEdge(tail, head)) return false; |
| | | 102 | | |
| | 29 | 103 | | var headHasEdge = _labelGraph.HasEdge(head); |
| | 29 | 104 | | _labelGraph.AddEdge(tail, head); |
| | | 105 | | |
| | 40 | 106 | | if (headHasEdge) return this.FindLoops(head); |
| | 18 | 107 | | return false; |
| | 38 | 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) |
| | 109 | 158 | | { |
| | 109 | 159 | | island = (0, 0, false); |
| | 141 | 160 | | if (!_labels.TryGetValue(edge, out var label)) return false; |
| | | 161 | | |
| | 77 | 162 | | if (!_islands.TryGetValue(label, out var islandState)) |
| | 0 | 163 | | throw new Exception("Island does not exist"); |
| | | 164 | | |
| | 77 | 165 | | island = (label, islandState.size, final: islandState.final); |
| | 77 | 166 | | return true; |
| | 109 | 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) |
| | 11 | 174 | | { |
| | 11 | 175 | | if (!_islands.TryGetValue(label, out var islandState)) |
| | 0 | 176 | | throw new Exception("Island does not exist"); |
| | | 177 | | |
| | 11 | 178 | | _islands[label] = (islandState.size, true); |
| | 11 | 179 | | } |
| | | 180 | | |
| | | 181 | | private bool FindLoops(uint label) |
| | 11 | 182 | | { |
| | 11 | 183 | | var pathTree = new PathTree(); |
| | 11 | 184 | | var settled = new HashSet<uint>(); |
| | 11 | 185 | | var queue = new Queue<uint>(); |
| | 11 | 186 | | var loop = new HashSet<uint>(); // keeps all with a path back to label, initially only label. |
| | | 187 | | |
| | 11 | 188 | | var loopFound = false; |
| | 11 | 189 | | var enumerator = _labelGraph.GetEdgeEnumerator(); |
| | 21 | 190 | | while (true) |
| | 21 | 191 | | { |
| | 21 | 192 | | if (!enumerator.MoveTo(label)) throw new Exception("Cannot search for loops on label that does not exist"); |
| | | 193 | | |
| | 21 | 194 | | loop.Add(label); |
| | 21 | 195 | | queue.Enqueue(pathTree.Add(label, uint.MaxValue)); |
| | | 196 | | |
| | 54 | 197 | | while (queue.Count > 0) |
| | 33 | 198 | | { |
| | 33 | 199 | | var pointer = queue.Dequeue(); |
| | 33 | 200 | | pathTree.Get(pointer, out var current, out var previous); |
| | | 201 | | |
| | 33 | 202 | | if (!settled.Add(current)) continue; |
| | 33 | 203 | | if (!enumerator.MoveTo(current)) continue; |
| | | 204 | | |
| | 79 | 205 | | while (enumerator.MoveNext()) |
| | 46 | 206 | | { |
| | 46 | 207 | | var n = enumerator.Head; |
| | 70 | 208 | | if (!enumerator.Forward) continue; |
| | | 209 | | |
| | 22 | 210 | | if (loop.Contains(n)) |
| | 10 | 211 | | { |
| | | 212 | | // yay, a loop! |
| | 10 | 213 | | loop.Add(current); |
| | 22 | 214 | | while (previous != uint.MaxValue) |
| | 12 | 215 | | { |
| | 12 | 216 | | pathTree.Get(previous, out current, out previous); |
| | 12 | 217 | | loop.Add(current); |
| | 12 | 218 | | } |
| | 10 | 219 | | } |
| | | 220 | | |
| | 32 | 221 | | if (settled.Contains(n)) continue; |
| | 12 | 222 | | queue.Enqueue(pathTree.Add(n, pointer)); |
| | 12 | 223 | | } |
| | 33 | 224 | | } |
| | | 225 | | |
| | 32 | 226 | | if (loop.Count <= 1) return loopFound; |
| | | 227 | | |
| | 10 | 228 | | label = this.Merge(loop); |
| | 10 | 229 | | loopFound = true; |
| | | 230 | | |
| | 10 | 231 | | loop.Clear(); |
| | 10 | 232 | | pathTree.Clear(); |
| | 10 | 233 | | settled.Clear(); |
| | 10 | 234 | | queue.Clear(); |
| | 10 | 235 | | } |
| | 11 | 236 | | } |
| | | 237 | | |
| | | 238 | | private uint Merge(HashSet<uint> labels) |
| | 10 | 239 | | { |
| | 15 | 240 | | while (true) |
| | 15 | 241 | | { |
| | | 242 | | // build a list of neighbours of the labels to be removed. |
| | 15 | 243 | | var edgeEnumerator = _labelGraph.GetEdgeEnumerator(); |
| | 15 | 244 | | var bestLabel = uint.MaxValue; |
| | 15 | 245 | | var neighbours = new Dictionary<uint, (bool forward, bool backward)>(); |
| | 109 | 246 | | foreach (var label in labels) |
| | 32 | 247 | | { |
| | 57 | 248 | | if (label < bestLabel) bestLabel = label; |
| | 32 | 249 | | if (!edgeEnumerator.MoveTo(label)) continue; |
| | | 250 | | |
| | 76 | 251 | | while (edgeEnumerator.MoveNext()) |
| | 44 | 252 | | { |
| | 44 | 253 | | var n = edgeEnumerator.Head; |
| | 88 | 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 | | } |
| | 32 | 265 | | } |
| | 15 | 266 | | if (bestLabel == uint.MaxValue) throw new Exception("Cannot merge empty loop"); |
| | | 267 | | |
| | | 268 | | // add all edges to the neighbours again. |
| | 45 | 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 | | |
| | 15 | 282 | | var bestData = _islands[bestLabel]; |
| | 109 | 283 | | foreach (var label in labels) |
| | 32 | 284 | | { |
| | 47 | 285 | | if (label == bestLabel) continue; |
| | 17 | 286 | | _labelGraph.RemoveVertex(label); |
| | | 287 | | |
| | 17 | 288 | | var data = _islands[label]; |
| | 28 | 289 | | if (bestData.size < uint.MaxValue) bestData.size += data.size; |
| | 17 | 290 | | _islands.Remove(label); |
| | 17 | 291 | | } |
| | | 292 | | |
| | 15 | 293 | | _islands[bestLabel] = bestData; |
| | | 294 | | |
| | 133 | 295 | | foreach (var k in _labels.Keys.ToArray()) |
| | 44 | 296 | | { |
| | 44 | 297 | | var val = _labels[k]; |
| | 58 | 298 | | if (val == bestLabel) continue; |
| | 33 | 299 | | if (!labels.Contains(val)) continue; |
| | | 300 | | |
| | 27 | 301 | | _labels[k] = bestLabel; |
| | 27 | 302 | | } |
| | | 303 | | |
| | 21 | 304 | | if (bestLabel == NotAnIslandLabel) return bestLabel; |
| | 9 | 305 | | if (_islands[bestLabel].size < _maxIslandSize) |
| | 4 | 306 | | { |
| | 4 | 307 | | return bestLabel; |
| | | 308 | | } |
| | | 309 | | |
| | 5 | 310 | | labels = [bestLabel, NotAnIslandLabel]; |
| | 5 | 311 | | continue; |
| | | 312 | | } |
| | 10 | 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 | | } |