| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | |
| | | 4 | | namespace Itinero.IO.Osm.Collections; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// A cache for node coordinates. |
| | | 8 | | /// </summary> |
| | | 9 | | internal sealed class UnsignedNodeIndex |
| | | 10 | | { |
| | | 11 | | // keeps all coordinates in a form [id, lat * 10.000.000, lon * 10.000.000] |
| | | 12 | | // assumes coordinates are added from a sorted source: TODO: make sure that the source read by the routerdb is sorte |
| | | 13 | | private int[] _data; |
| | | 14 | | private int[] _index; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Creates a new node coordinates cache. |
| | | 18 | | /// </summary> |
| | 0 | 19 | | public UnsignedNodeIndex() |
| | 0 | 20 | | { |
| | 0 | 21 | | _index = new int[1024 * 1024]; |
| | 0 | 22 | | _data = new int[0]; |
| | 0 | 23 | | } |
| | | 24 | | |
| | 0 | 25 | | private List<long>? _overflows = null; // overflows is null before sorting. |
| | 0 | 26 | | private long _idx = 0; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Adds a node id to the index. |
| | | 30 | | /// </summary> |
| | | 31 | | public void AddId(long id) |
| | 0 | 32 | | { |
| | | 33 | | int int1, int2; |
| | 0 | 34 | | long2doubleInt(id, out int1, out int2); |
| | | 35 | | |
| | 0 | 36 | | EnsureMinimumSize(ref _index, _idx + 2); |
| | 0 | 37 | | _index[(int)(_idx + 0)] = int1; |
| | 0 | 38 | | _index[(int)(_idx + 1)] = int2; |
| | 0 | 39 | | _idx += 2; |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | private static void long2doubleInt(long a, out int a1, out int a2) |
| | 0 | 43 | | { |
| | | 44 | | unchecked |
| | 0 | 45 | | { |
| | 0 | 46 | | a1 = (int)(a & uint.MaxValue); |
| | 0 | 47 | | a2 = (int)(a >> 32); |
| | 0 | 48 | | } |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | private static long doubleInt2long(int a1, int a2) |
| | 0 | 52 | | { |
| | | 53 | | unchecked |
| | 0 | 54 | | { |
| | 0 | 55 | | long b = a2; |
| | 0 | 56 | | b = b << 32; |
| | 0 | 57 | | b = b | (uint)a1; |
| | 0 | 58 | | return b; |
| | | 59 | | } |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Sorts and converts the index. |
| | | 64 | | /// </summary> |
| | | 65 | | public void SortAndConvertIndex() |
| | 0 | 66 | | { |
| | 0 | 67 | | _overflows = new List<long>(); |
| | 0 | 68 | | Array.Resize(ref _index, (int)_idx); |
| | | 69 | | |
| | 0 | 70 | | Logging.Logger.Log("NodeIndex", Logging.TraceEventType.Information, "Sorting node id's..."); |
| | 0 | 71 | | QuickSort.Sort((i) => |
| | 0 | 72 | | { |
| | 0 | 73 | | var int1 = _index[(int)(i * 2 + 0)]; |
| | 0 | 74 | | var int2 = _index[(int)(i * 2 + 1)]; |
| | 0 | 75 | | return doubleInt2long(int1, int2); |
| | 0 | 76 | | }, |
| | 0 | 77 | | (i, j) => |
| | 0 | 78 | | { |
| | 0 | 79 | | var int1 = _index[(int)(i * 2 + 0)]; |
| | 0 | 80 | | var int2 = _index[(int)(i * 2 + 1)]; |
| | 0 | 81 | | _index[(int)(i * 2 + 0)] = _index[(int)(j * 2 + 0)]; |
| | 0 | 82 | | _index[(int)(i * 2 + 1)] = _index[(int)(j * 2 + 1)]; |
| | 0 | 83 | | _index[(int)(j * 2 + 0)] = int1; |
| | 0 | 84 | | _index[(int)(j * 2 + 1)] = int2; |
| | 0 | 85 | | }, 0, _index.Length / 2 - 1); |
| | | 86 | | |
| | 0 | 87 | | for (long i = 0; i < _index.Length / 2; i++) |
| | 0 | 88 | | { |
| | 0 | 89 | | var int1 = _index[(int)(i * 2 + 0)]; |
| | 0 | 90 | | var int2 = _index[(int)(i * 2 + 1)]; |
| | 0 | 91 | | var id = doubleInt2long(int1, int2); |
| | | 92 | | |
| | 0 | 93 | | while (id >= (long)int.MaxValue * (long)(_overflows.Count + 1)) |
| | 0 | 94 | | { // nodes are overflowing again. |
| | 0 | 95 | | _overflows.Add(i); |
| | 0 | 96 | | } |
| | | 97 | | |
| | 0 | 98 | | _index[(int)i] = (int)(id - (long)int.MaxValue * (long)_overflows.Count); |
| | 0 | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | Array.Resize(ref _index, _index.Length / 2); |
| | 0 | 102 | | _idx = _index.Length; |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Gets the node id at the given index. |
| | | 107 | | /// </summary> |
| | | 108 | | public long this[long idx] |
| | | 109 | | { |
| | | 110 | | get |
| | 0 | 111 | | { |
| | 0 | 112 | | var int1 = _index[(int)(idx * 2 + 0)]; |
| | 0 | 113 | | var int2 = _index[(int)(idx * 2 + 1)]; |
| | 0 | 114 | | return doubleInt2long(int1, int2); |
| | 0 | 115 | | } |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Sets a vertex id for the given vertex. |
| | | 120 | | /// </summary> |
| | | 121 | | public void Set(long id, uint vertex) |
| | 0 | 122 | | { |
| | 0 | 123 | | var idx = this.TryGetIndex(id); |
| | | 124 | | |
| | 0 | 125 | | EnsureMinimumSize(ref _data, idx * 2 + 2, int.MaxValue); |
| | 0 | 126 | | _data[(int)(idx * 2 + 0)] = unchecked((int)vertex); |
| | 0 | 127 | | _data[(int)(idx * 2 + 1)] = int.MinValue; |
| | 0 | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Sets the coordinate for the given index. |
| | | 132 | | /// </summary> |
| | | 133 | | public void SetIndex(long idx, float latitude, float longitude) |
| | 0 | 134 | | { |
| | 0 | 135 | | var lat = (int)(latitude * 10000000); |
| | 0 | 136 | | var lon = (int)(longitude * 10000000); |
| | | 137 | | |
| | 0 | 138 | | EnsureMinimumSize(ref _data, idx * 2 + 2, int.MaxValue); |
| | | 139 | | |
| | 0 | 140 | | if (_data[(int)(idx * 2 + 1)] == int.MinValue) |
| | 0 | 141 | | { |
| | | 142 | | // this is already a core vertex, no need to overwrite this more valuable data. |
| | 0 | 143 | | return; |
| | | 144 | | } |
| | | 145 | | |
| | 0 | 146 | | _data[(int)(idx * 2 + 0)] = lat; |
| | 0 | 147 | | _data[(int)(idx * 2 + 1)] = lon; |
| | 0 | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Tries to get a core node and it's matching vertex. |
| | | 152 | | /// </summary> |
| | | 153 | | public bool TryGetCoreNode(long id, out uint vertex) |
| | 0 | 154 | | { |
| | 0 | 155 | | var idx = this.TryGetIndex(id); |
| | 0 | 156 | | if (idx == long.MaxValue) |
| | 0 | 157 | | { |
| | 0 | 158 | | vertex = uint.MaxValue; |
| | 0 | 159 | | return false; |
| | | 160 | | } |
| | | 161 | | |
| | 0 | 162 | | if (_data.Length <= idx * 2 + 0) |
| | 0 | 163 | | { |
| | 0 | 164 | | vertex = uint.MaxValue; |
| | 0 | 165 | | return false; |
| | | 166 | | } |
| | | 167 | | |
| | 0 | 168 | | vertex = unchecked((uint)_data[(int)(idx * 2 + 0)]); |
| | 0 | 169 | | return _data[(int)(idx * 2 + 1)] == int.MinValue; |
| | 0 | 170 | | } |
| | | 171 | | |
| | | 172 | | /// <summary> |
| | | 173 | | /// Returns true if the given id is a core node. |
| | | 174 | | /// </summary> |
| | | 175 | | public bool IsCoreNode(long id) |
| | 0 | 176 | | { |
| | 0 | 177 | | if (_previousIndex != long.MaxValue) |
| | 0 | 178 | | { |
| | 0 | 179 | | var tempId = this.GetId(_previousIndex + 1); |
| | 0 | 180 | | if (tempId == id) |
| | 0 | 181 | | { |
| | 0 | 182 | | if (this.IsCoreNodeAtIndex(_previousIndex + 1, id)) |
| | 0 | 183 | | { |
| | 0 | 184 | | return true; |
| | | 185 | | } |
| | | 186 | | |
| | 0 | 187 | | return false; |
| | | 188 | | } |
| | 0 | 189 | | } |
| | | 190 | | |
| | 0 | 191 | | var idx = this.TryGetIndex(id); |
| | 0 | 192 | | if (idx != long.MaxValue) |
| | 0 | 193 | | { |
| | 0 | 194 | | _previousIndex = idx; |
| | 0 | 195 | | if (this.IsCoreNodeAtIndex(idx, id)) |
| | 0 | 196 | | { |
| | 0 | 197 | | return true; |
| | | 198 | | } |
| | | 199 | | |
| | 0 | 200 | | return false; |
| | | 201 | | } |
| | | 202 | | |
| | 0 | 203 | | return false; |
| | 0 | 204 | | } |
| | | 205 | | |
| | | 206 | | |
| | | 207 | | /// <summary> |
| | | 208 | | /// Returns true if the given id is in this index. |
| | | 209 | | /// </summary> |
| | | 210 | | public bool HasId(long id) |
| | 0 | 211 | | { |
| | 0 | 212 | | if (_previousIndex != long.MaxValue) |
| | 0 | 213 | | { |
| | 0 | 214 | | var tempId = this.GetId(_previousIndex + 1); |
| | 0 | 215 | | if (tempId == id) |
| | 0 | 216 | | { |
| | 0 | 217 | | return true; |
| | | 218 | | } |
| | 0 | 219 | | } |
| | | 220 | | |
| | 0 | 221 | | var idx = this.TryGetIndex(id); |
| | 0 | 222 | | _previousIndex = idx; |
| | 0 | 223 | | return idx != long.MaxValue; |
| | 0 | 224 | | } |
| | | 225 | | |
| | | 226 | | /// <summary> |
| | | 227 | | /// Gets the coordinate for the given node. |
| | | 228 | | /// </summary> |
| | | 229 | | public bool TryGetValue(long id, out float latitude, out float longitude, out bool isCore) |
| | 0 | 230 | | { |
| | 0 | 231 | | var idx = this.TryGetIndex(id); |
| | 0 | 232 | | if (idx == long.MaxValue) |
| | 0 | 233 | | { |
| | 0 | 234 | | latitude = float.MaxValue; |
| | 0 | 235 | | longitude = float.MaxValue; |
| | 0 | 236 | | isCore = false; |
| | 0 | 237 | | return false; |
| | | 238 | | } |
| | | 239 | | |
| | 0 | 240 | | if (!this.GetLatLon(idx, out latitude, out longitude)) |
| | 0 | 241 | | { |
| | 0 | 242 | | latitude = float.MaxValue; |
| | 0 | 243 | | longitude = float.MaxValue; |
| | 0 | 244 | | isCore = false; |
| | 0 | 245 | | return false; |
| | | 246 | | } |
| | | 247 | | |
| | 0 | 248 | | isCore = this.IsCoreNodeAtIndex(idx, id); |
| | 0 | 249 | | return true; |
| | 0 | 250 | | } |
| | | 251 | | |
| | | 252 | | /// <summary> |
| | | 253 | | /// Gets all relevant info on the given node. |
| | | 254 | | /// </summary> |
| | | 255 | | public bool TryGetValue(long id, out float latitude, out float longitude, out bool isCore, out uint vertex, |
| | | 256 | | out long idx) |
| | 0 | 257 | | { |
| | 0 | 258 | | idx = this.TryGetIndex(id); |
| | 0 | 259 | | if (idx == long.MaxValue) |
| | 0 | 260 | | { // no relevant data here. |
| | 0 | 261 | | latitude = float.MaxValue; |
| | 0 | 262 | | longitude = float.MaxValue; |
| | 0 | 263 | | isCore = false; |
| | 0 | 264 | | vertex = uint.MaxValue; |
| | 0 | 265 | | return false; |
| | | 266 | | } |
| | 0 | 267 | | else if (_data.Length > idx * 2 + 1 && |
| | 0 | 268 | | _data[(int)(idx * 2 + 1)] == int.MinValue) |
| | 0 | 269 | | { // this is a core-vertex, no coordinates here anymore. |
| | 0 | 270 | | latitude = float.MaxValue; |
| | 0 | 271 | | longitude = float.MaxValue; |
| | 0 | 272 | | isCore = this.IsCoreNodeAtIndex(idx, id); |
| | 0 | 273 | | vertex = unchecked((uint)_data[(int)(idx * 2 + 0)]); |
| | 0 | 274 | | return true; |
| | | 275 | | } |
| | | 276 | | |
| | 0 | 277 | | if (this.GetLatLon(idx, out latitude, out longitude)) |
| | 0 | 278 | | { // no relevant data. |
| | 0 | 279 | | isCore = this.IsCoreNodeAtIndex(idx, id); |
| | 0 | 280 | | vertex = uint.MaxValue; |
| | 0 | 281 | | return true; |
| | | 282 | | } |
| | | 283 | | |
| | 0 | 284 | | latitude = float.MaxValue; |
| | 0 | 285 | | longitude = float.MaxValue; |
| | 0 | 286 | | isCore = false; |
| | 0 | 287 | | vertex = uint.MaxValue; |
| | 0 | 288 | | return false; |
| | 0 | 289 | | } |
| | | 290 | | |
| | | 291 | | /// <summary> |
| | | 292 | | /// Gets the coordinate for the given node. |
| | | 293 | | /// </summary> |
| | | 294 | | public bool TryGetValue(long id, out (double longitude, double latitude, float? e) coordinate, |
| | | 295 | | out bool isCore) |
| | 0 | 296 | | { |
| | 0 | 297 | | if (this.TryGetValue(id, out var latitude, out var longitude, out isCore)) |
| | 0 | 298 | | { |
| | 0 | 299 | | coordinate = (longitude, latitude, null); |
| | 0 | 300 | | return true; |
| | | 301 | | } |
| | | 302 | | |
| | 0 | 303 | | coordinate = (default, default, null); |
| | 0 | 304 | | return false; |
| | 0 | 305 | | } |
| | | 306 | | |
| | | 307 | | /// <summary> |
| | | 308 | | /// Returns true if a the given index there is an id that represents a core node. |
| | | 309 | | /// </summary> |
| | | 310 | | private bool IsCoreNodeAtIndex(long idx, long id) |
| | 0 | 311 | | { |
| | 0 | 312 | | if (idx > 0 && |
| | 0 | 313 | | this.GetId(idx - 1) == id) |
| | 0 | 314 | | { |
| | 0 | 315 | | return true; |
| | | 316 | | } |
| | | 317 | | |
| | 0 | 318 | | if (idx < _index.Length - 1 && |
| | 0 | 319 | | this.GetId(idx + 1) == id) |
| | 0 | 320 | | { |
| | 0 | 321 | | return true; |
| | | 322 | | } |
| | | 323 | | |
| | 0 | 324 | | return false; |
| | 0 | 325 | | } |
| | | 326 | | |
| | 0 | 327 | | private long _previousIndex = long.MaxValue; |
| | | 328 | | |
| | | 329 | | /// <summary> |
| | | 330 | | /// Gets the coordinate for the given node. |
| | | 331 | | /// </summary> |
| | | 332 | | public long TryGetIndex(long id) |
| | 0 | 333 | | { |
| | 0 | 334 | | if (_previousIndex != long.MaxValue && |
| | 0 | 335 | | _previousIndex + 1 < _idx) |
| | 0 | 336 | | { |
| | 0 | 337 | | var previousId = this.GetId(_previousIndex); |
| | 0 | 338 | | var offset = 1; |
| | 0 | 339 | | var nextId = this.GetId(_previousIndex + offset); |
| | 0 | 340 | | while (nextId == previousId && |
| | 0 | 341 | | _previousIndex + offset + 1 < _idx) |
| | 0 | 342 | | { |
| | 0 | 343 | | offset++; |
| | 0 | 344 | | nextId = this.GetId(_previousIndex + offset); |
| | 0 | 345 | | } |
| | | 346 | | |
| | 0 | 347 | | if (previousId < id && id < nextId) |
| | 0 | 348 | | { |
| | 0 | 349 | | return long.MaxValue; |
| | | 350 | | } |
| | | 351 | | |
| | 0 | 352 | | if (previousId == id) |
| | 0 | 353 | | { |
| | 0 | 354 | | return _previousIndex; |
| | | 355 | | } |
| | | 356 | | |
| | 0 | 357 | | if (nextId == id) |
| | 0 | 358 | | { |
| | 0 | 359 | | _previousIndex += offset; |
| | 0 | 360 | | return _previousIndex; |
| | | 361 | | } |
| | 0 | 362 | | } |
| | | 363 | | |
| | | 364 | | // do a binary search. |
| | 0 | 365 | | long bottom = 0; |
| | 0 | 366 | | var top = _idx - 1; |
| | 0 | 367 | | var bottomId = this.GetId(bottom); |
| | 0 | 368 | | if (id == bottomId) |
| | 0 | 369 | | { |
| | 0 | 370 | | _previousIndex = bottom; |
| | 0 | 371 | | return bottom; |
| | | 372 | | } |
| | | 373 | | |
| | 0 | 374 | | var topId = this.GetId(top); |
| | 0 | 375 | | if (id == topId) |
| | 0 | 376 | | { |
| | 0 | 377 | | while (top - 1 > 0 && |
| | 0 | 378 | | this.GetId(top - 1) == id) |
| | 0 | 379 | | { |
| | 0 | 380 | | top--; |
| | 0 | 381 | | } |
| | | 382 | | |
| | 0 | 383 | | _previousIndex = top; |
| | 0 | 384 | | return top; |
| | | 385 | | } |
| | | 386 | | |
| | 0 | 387 | | while (top - bottom > 1) |
| | 0 | 388 | | { |
| | 0 | 389 | | var middle = (top - bottom) / 2 + bottom; |
| | 0 | 390 | | var middleId = this.GetId(middle); |
| | 0 | 391 | | if (middleId == id) |
| | 0 | 392 | | { |
| | 0 | 393 | | while (middle - 1 > 0 && |
| | 0 | 394 | | this.GetId(middle - 1) == id) |
| | 0 | 395 | | { |
| | 0 | 396 | | middle--; |
| | 0 | 397 | | } |
| | | 398 | | |
| | 0 | 399 | | _previousIndex = middle; |
| | 0 | 400 | | return middle; |
| | | 401 | | } |
| | | 402 | | |
| | 0 | 403 | | if (middleId > id) |
| | 0 | 404 | | { |
| | 0 | 405 | | topId = middleId; |
| | 0 | 406 | | top = middle; |
| | 0 | 407 | | } |
| | | 408 | | else |
| | 0 | 409 | | { |
| | 0 | 410 | | bottomId = middleId; |
| | 0 | 411 | | bottom = middle; |
| | 0 | 412 | | } |
| | 0 | 413 | | } |
| | | 414 | | |
| | 0 | 415 | | return long.MaxValue; |
| | 0 | 416 | | } |
| | | 417 | | |
| | | 418 | | private long GetId(long index) |
| | 0 | 419 | | { |
| | 0 | 420 | | if (_index.Length == 0) |
| | 0 | 421 | | { |
| | 0 | 422 | | return long.MaxValue; |
| | | 423 | | } |
| | | 424 | | |
| | 0 | 425 | | var overflow = 0; |
| | 0 | 426 | | for (var i = 0; i < _overflows.Count; i++) |
| | 0 | 427 | | { |
| | 0 | 428 | | if (index >= _overflows[i]) |
| | 0 | 429 | | { |
| | 0 | 430 | | overflow = i + 1; |
| | 0 | 431 | | } |
| | | 432 | | else |
| | 0 | 433 | | { |
| | 0 | 434 | | break; |
| | | 435 | | } |
| | 0 | 436 | | } |
| | | 437 | | |
| | 0 | 438 | | return (long)_index[(int)index] + (long)(int.MaxValue * (long)overflow); |
| | 0 | 439 | | } |
| | | 440 | | |
| | | 441 | | private bool GetLatLon(long index, out float latitude, out float longitude) |
| | 0 | 442 | | { |
| | 0 | 443 | | index = index * 2; |
| | | 444 | | |
| | 0 | 445 | | var lat = _data[(int)(index + 0)]; |
| | 0 | 446 | | var lon = _data[(int)(index + 1)]; |
| | | 447 | | |
| | 0 | 448 | | if (lat == int.MaxValue && lon == int.MaxValue) |
| | 0 | 449 | | { |
| | 0 | 450 | | latitude = float.MaxValue; |
| | 0 | 451 | | longitude = float.MaxValue; |
| | 0 | 452 | | return false; |
| | | 453 | | } |
| | | 454 | | |
| | 0 | 455 | | latitude = (float)(lat / 10000000.0); |
| | 0 | 456 | | longitude = (float)(lon / 10000000.0); |
| | 0 | 457 | | return true; |
| | 0 | 458 | | } |
| | | 459 | | |
| | | 460 | | /// <summary> |
| | | 461 | | /// Returns the number of elements. |
| | | 462 | | /// </summary> |
| | 0 | 463 | | public long Count => _index.Length; |
| | | 464 | | |
| | | 465 | | private static void EnsureMinimumSize(ref int[] array, long position, long step = 16) |
| | 0 | 466 | | { |
| | 0 | 467 | | if (array.Length > position) return; |
| | 0 | 468 | | var newSize = array.Length + step; |
| | 0 | 469 | | while (newSize <= position) newSize += step; |
| | 0 | 470 | | Array.Resize(ref array, (int)newSize); |
| | 0 | 471 | | } |
| | | 472 | | } |