| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationOverlayTransaction.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | using System.Collections.Generic; |
| | | 10 | | using System.Collections.ObjectModel; |
| | | 11 | | |
| | | 12 | | namespace Trailblazer.Pathing; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Defines the minimum atomic semantic-overlay publication unit across one or more maps. |
| | | 16 | | /// </summary> |
| | | 17 | | public sealed class NavigationOverlayTransaction |
| | | 18 | | { |
| | 1 | 19 | | private static readonly IComparer<NavigationMapOverlayDelta> MapComparer = new MapDeltaComparer(); |
| | | 20 | | private readonly NavigationMapOverlayDelta[] _maps; |
| | | 21 | | private readonly ReadOnlyCollection<NavigationMapOverlayDelta> _mapView; |
| | | 22 | | |
| | | 23 | | /// <summary>Initializes and canonically orders an immutable multi-map overlay transaction.</summary> |
| | 321 | 24 | | public NavigationOverlayTransaction(ReadOnlySpan<NavigationMapOverlayDelta> maps) |
| | | 25 | | { |
| | 321 | 26 | | SwiftThrowHelper.ThrowIfArgument( |
| | 321 | 27 | | maps.IsEmpty, |
| | 321 | 28 | | nameof(maps), |
| | 321 | 29 | | "An overlay transaction must contain at least one map delta."); |
| | | 30 | | |
| | 321 | 31 | | _maps = maps.ToArray(); |
| | 1428 | 32 | | for (int i = 0; i < _maps.Length; i++) |
| | 393 | 33 | | SwiftThrowHelper.ThrowIfNull(_maps[i], nameof(maps)); |
| | | 34 | | |
| | 321 | 35 | | Array.Sort(_maps, MapComparer); |
| | 321 | 36 | | long descriptorBytes = 32L; |
| | 1426 | 37 | | for (int i = 0; i < _maps.Length; i++) |
| | | 38 | | { |
| | 393 | 39 | | SwiftThrowHelper.ThrowIfArgument( |
| | 393 | 40 | | i > 0 && string.Equals(_maps[i - 1].MapId, _maps[i].MapId, StringComparison.Ordinal), |
| | 393 | 41 | | nameof(maps), |
| | 393 | 42 | | "Map overlay ids must be unique within one transaction."); |
| | 392 | 43 | | descriptorBytes = NavigationByteCount.SaturatingAdd( |
| | 392 | 44 | | descriptorBytes, |
| | 392 | 45 | | _maps[i].EstimatedDescriptorBytes); |
| | | 46 | | } |
| | | 47 | | |
| | 320 | 48 | | _mapView = Array.AsReadOnly(_maps); |
| | 320 | 49 | | EstimatedDescriptorBytes = descriptorBytes; |
| | 320 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary>Gets canonically ordered, unique per-map deltas.</summary> |
| | 5 | 53 | | public IReadOnlyList<NavigationMapOverlayDelta> Maps => _mapView; |
| | | 54 | | |
| | 35787 | 55 | | internal ReadOnlySpan<NavigationMapOverlayDelta> MapSpan => _maps; |
| | | 56 | | |
| | | 57 | | /// <summary>Gets a deterministic conservative byte count for submission admission.</summary> |
| | | 58 | | public long EstimatedDescriptorBytes { get; } |
| | | 59 | | |
| | | 60 | | private sealed class MapDeltaComparer : IComparer<NavigationMapOverlayDelta> |
| | | 61 | | { |
| | | 62 | | public int Compare(NavigationMapOverlayDelta? left, NavigationMapOverlayDelta? right) => |
| | 216 | 63 | | string.CompareOrdinal(left!.MapId, right!.MapId); |
| | | 64 | | } |
| | | 65 | | } |