| | | 1 | | //======================================================================= |
| | | 2 | | // MaintenanceWorkMeter.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 | | namespace Trailblazer.Pathing; |
| | | 9 | | |
| | | 10 | | /// <summary>Debits every deterministic maintenance producer against one fixed-step budget.</summary> |
| | | 11 | | internal sealed class MaintenanceWorkMeter |
| | | 12 | | { |
| | | 13 | | private readonly MaintenanceWorkBudget _budget; |
| | | 14 | | private int _consumedEnvelopes; |
| | | 15 | | private int _baselineAddresses; |
| | | 16 | | private int _overlaySlots; |
| | | 17 | | private int _componentNodes; |
| | | 18 | | private int _seamCandidateProbes; |
| | | 19 | | private int _explicitEdges; |
| | | 20 | | private int _dependencyEntries; |
| | | 21 | | private int _surfaceComponentEdges; |
| | | 22 | | |
| | 7848 | 23 | | internal MaintenanceWorkMeter(MaintenanceWorkBudget budget) => _budget = budget; |
| | | 24 | | |
| | 20259 | 25 | | internal int ConsumedEnvelopes => _consumedEnvelopes; |
| | 29050 | 26 | | internal int BaselineAddresses => _baselineAddresses; |
| | 77 | 27 | | internal int OverlaySlots => _overlaySlots; |
| | 79 | 28 | | internal int ComponentNodes => _componentNodes; |
| | 39989 | 29 | | internal int SeamCandidateProbes => _seamCandidateProbes; |
| | 122 | 30 | | internal int ExplicitEdges => _explicitEdges; |
| | 1706 | 31 | | internal int DependencyEntries => _dependencyEntries; |
| | 1437656 | 32 | | internal int SurfaceComponentEdges => _surfaceComponentEdges; |
| | | 33 | | |
| | 20250 | 34 | | internal int RemainingEnvelopes => _budget.MaxConsumedEnvelopes - ConsumedEnvelopes; |
| | 29036 | 35 | | internal int RemainingBaselineAddresses => _budget.MaxBaselineAddresses - BaselineAddresses; |
| | 1 | 36 | | internal int RemainingOverlaySlots => _budget.MaxOverlaySlots - OverlaySlots; |
| | 1 | 37 | | internal int RemainingComponentNodes => _budget.MaxComponentNodes - ComponentNodes; |
| | | 38 | | internal int RemainingSeamCandidateProbes => |
| | 39858 | 39 | | _budget.MaxSeamCandidateProbes - SeamCandidateProbes; |
| | 1 | 40 | | internal int RemainingExplicitEdges => _budget.MaxExplicitEdges - ExplicitEdges; |
| | 1 | 41 | | internal int RemainingDependencyEntries => _budget.MaxDependencyEntries - DependencyEntries; |
| | | 42 | | internal int RemainingSurfaceComponentEdges => |
| | 1437620 | 43 | | _budget.MaxSurfaceComponentEdges - SurfaceComponentEdges; |
| | | 44 | | |
| | | 45 | | internal void Reset() |
| | | 46 | | { |
| | 22837 | 47 | | _consumedEnvelopes = 0; |
| | 22837 | 48 | | _baselineAddresses = 0; |
| | 22837 | 49 | | _overlaySlots = 0; |
| | 22837 | 50 | | _componentNodes = 0; |
| | 22837 | 51 | | _seamCandidateProbes = 0; |
| | 22837 | 52 | | _explicitEdges = 0; |
| | 22837 | 53 | | _dependencyEntries = 0; |
| | 22837 | 54 | | _surfaceComponentEdges = 0; |
| | 22837 | 55 | | } |
| | | 56 | | |
| | | 57 | | internal bool TryConsumeEnvelopes(int count) => |
| | 20268 | 58 | | TryConsume(ref _consumedEnvelopes, count, _budget.MaxConsumedEnvelopes); |
| | | 59 | | |
| | | 60 | | internal bool TryConsumeBaselineAddresses(int count) => |
| | 1185 | 61 | | TryConsume(ref _baselineAddresses, count, _budget.MaxBaselineAddresses); |
| | | 62 | | |
| | | 63 | | internal bool TryConsumeOverlaySlots(int count) => |
| | 114348 | 64 | | TryConsume(ref _overlaySlots, count, _budget.MaxOverlaySlots); |
| | | 65 | | |
| | | 66 | | internal bool TryConsumeComponentNodes(int count) => |
| | 130623 | 67 | | TryConsume(ref _componentNodes, count, _budget.MaxComponentNodes); |
| | | 68 | | |
| | | 69 | | internal bool TryConsumeSeamCandidateProbes(int count) => |
| | 39859 | 70 | | TryConsume(ref _seamCandidateProbes, count, _budget.MaxSeamCandidateProbes); |
| | | 71 | | |
| | | 72 | | internal bool TryConsumeExplicitEdges(int count) => |
| | 236811 | 73 | | TryConsume(ref _explicitEdges, count, _budget.MaxExplicitEdges); |
| | | 74 | | |
| | | 75 | | internal bool TryConsumeDependencyEntries(int count) => |
| | 355291 | 76 | | TryConsume(ref _dependencyEntries, count, _budget.MaxDependencyEntries); |
| | | 77 | | |
| | | 78 | | internal bool TryConsumeSurfaceComponentEdges(int count) => |
| | 1278945 | 79 | | TryConsume(ref _surfaceComponentEdges, count, _budget.MaxSurfaceComponentEdges); |
| | | 80 | | |
| | | 81 | | private static bool TryConsume(ref int consumed, int count, int maximum) |
| | | 82 | | { |
| | 2177330 | 83 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange(count < 0, count, nameof(count)); |
| | 2177329 | 84 | | if (count > maximum - consumed) |
| | 17627 | 85 | | return false; |
| | 2159702 | 86 | | consumed += count; |
| | 2159702 | 87 | | return true; |
| | | 88 | | } |
| | | 89 | | } |