| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationDependencyWorkspace.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 | | |
| | | 10 | | namespace Trailblazer.Pathing; |
| | | 11 | | |
| | | 12 | | /// <summary>Owns fixed page and component dependency accumulation.</summary> |
| | | 13 | | internal sealed class NavigationDependencyWorkspace |
| | | 14 | | { |
| | | 15 | | private readonly NavigationAddressStampSet _componentSet; |
| | | 16 | | private readonly NavigationPageStampSet _pageSet; |
| | | 17 | | |
| | 34632 | 18 | | internal NavigationDependencyWorkspace(int pageCapacity, int componentCapacity) |
| | | 19 | | { |
| | 34632 | 20 | | SwiftThrowHelper.ThrowIfNegative(pageCapacity, nameof(pageCapacity)); |
| | 34632 | 21 | | SwiftThrowHelper.ThrowIfNegative(componentCapacity, nameof(componentCapacity)); |
| | 34632 | 22 | | Pages = pageCapacity == 0 |
| | 34632 | 23 | | ? Array.Empty<GraphPageDependencyAddress>() |
| | 34632 | 24 | | : new GraphPageDependencyAddress[pageCapacity]; |
| | 34632 | 25 | | Components = componentCapacity == 0 |
| | 34632 | 26 | | ? Array.Empty<NavigationSurfaceComponentKey>() |
| | 34632 | 27 | | : new NavigationSurfaceComponentKey[componentCapacity]; |
| | 34632 | 28 | | _componentSet = new NavigationAddressStampSet(Math.Max(1, componentCapacity)); |
| | 34632 | 29 | | _pageSet = new NavigationPageStampSet(Math.Max(1, pageCapacity)); |
| | 34632 | 30 | | } |
| | | 31 | | |
| | | 32 | | internal GraphPageDependencyAddress[] Pages { get; } |
| | | 33 | | |
| | | 34 | | internal NavigationSurfaceComponentKey[] Components { get; } |
| | | 35 | | |
| | | 36 | | internal int PageCount { get; private set; } |
| | | 37 | | |
| | | 38 | | internal int ComponentCount { get; private set; } |
| | | 39 | | |
| | | 40 | | internal bool HasTransitionDependency { get; private set; } |
| | | 41 | | |
| | | 42 | | internal void Reset() |
| | | 43 | | { |
| | 8465 | 44 | | if (PageCount > 0) |
| | 2320 | 45 | | Array.Clear(Pages, 0, PageCount); |
| | 8465 | 46 | | if (ComponentCount > 0) |
| | 2048 | 47 | | Array.Clear(Components, 0, ComponentCount); |
| | 8465 | 48 | | PageCount = 0; |
| | 8465 | 49 | | ComponentCount = 0; |
| | 8465 | 50 | | HasTransitionDependency = false; |
| | 8465 | 51 | | _componentSet.Reset(); |
| | 8465 | 52 | | _pageSet.Reset(); |
| | 8465 | 53 | | } |
| | | 54 | | |
| | | 55 | | internal bool TryRecordComponent(NavigationSurfaceComponentKey component) |
| | | 56 | | { |
| | 13941 | 57 | | if (ComponentCount >= Components.Length) |
| | 1217 | 58 | | return _componentSet.Contains(component.Representative); |
| | 12724 | 59 | | if (!_componentSet.Add(component.Representative)) |
| | 9378 | 60 | | return true; |
| | 3346 | 61 | | Components[ComponentCount++] = component; |
| | 3346 | 62 | | return true; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | internal bool TryRecordPage(string mapId, int pageIndex) |
| | | 66 | | { |
| | 48959 | 67 | | if (PageCount >= Pages.Length) |
| | 1617 | 68 | | return _pageSet.Contains(mapId, pageIndex); |
| | 47342 | 69 | | if (!_pageSet.Add(mapId, pageIndex)) |
| | 43535 | 70 | | return true; |
| | 3807 | 71 | | Pages[PageCount++] = new GraphPageDependencyAddress(mapId, pageIndex); |
| | 3807 | 72 | | return true; |
| | | 73 | | } |
| | | 74 | | |
| | 255 | 75 | | internal void RecordTransitionDependency() => HasTransitionDependency = true; |
| | | 76 | | |
| | | 77 | | internal bool TryCountMissing( |
| | | 78 | | NavigationDependencyWorkspace source, |
| | | 79 | | NavigationWorkMeter meter, |
| | | 80 | | out int missingComponents, |
| | | 81 | | out int missingPages) |
| | | 82 | | { |
| | 35 | 83 | | SwiftThrowHelper.ThrowIfNull(source, nameof(source)); |
| | 35 | 84 | | SwiftThrowHelper.ThrowIfNull(meter, nameof(meter)); |
| | 35 | 85 | | missingComponents = 0; |
| | 35 | 86 | | missingPages = 0; |
| | 35 | 87 | | int probeCount = source.ComponentCount + source.PageCount; |
| | 35 | 88 | | if (!meter.TryConsumeLookupProbes(probeCount)) |
| | 2 | 89 | | return false; |
| | 134 | 90 | | for (int i = 0; i < source.ComponentCount; i++) |
| | | 91 | | { |
| | 34 | 92 | | if (!_componentSet.Contains(source.Components[i].Representative)) |
| | 5 | 93 | | missingComponents++; |
| | | 94 | | } |
| | 134 | 95 | | for (int i = 0; i < source.PageCount; i++) |
| | | 96 | | { |
| | 34 | 97 | | GraphPageDependencyAddress page = source.Pages[i]; |
| | 34 | 98 | | if (!_pageSet.Contains(page.MapId, page.PageIndex)) |
| | 5 | 99 | | missingPages++; |
| | | 100 | | } |
| | 33 | 101 | | return true; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | internal bool CanFit(int additionalComponents, int additionalPages) => |
| | 32 | 105 | | additionalComponents >= 0 |
| | 32 | 106 | | && additionalPages >= 0 |
| | 32 | 107 | | && additionalComponents <= Components.Length - ComponentCount |
| | 32 | 108 | | && additionalPages <= Pages.Length - PageCount; |
| | | 109 | | |
| | | 110 | | internal void CommitMerge(NavigationDependencyWorkspace source) |
| | | 111 | | { |
| | 28 | 112 | | SwiftThrowHelper.ThrowIfNull(source, nameof(source)); |
| | 110 | 113 | | for (int i = 0; i < source.ComponentCount; i++) |
| | 27 | 114 | | TryRecordComponent(source.Components[i]); |
| | 112 | 115 | | for (int i = 0; i < source.PageCount; i++) |
| | | 116 | | { |
| | 28 | 117 | | GraphPageDependencyAddress page = source.Pages[i]; |
| | 28 | 118 | | TryRecordPage(page.MapId, page.PageIndex); |
| | | 119 | | } |
| | 28 | 120 | | if (source.HasTransitionDependency) |
| | 1 | 121 | | HasTransitionDependency = true; |
| | 28 | 122 | | } |
| | | 123 | | } |