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