< Summary

Information
Class: Trailblazer.Pathing.NavigationDependencyWorkspace
Assembly: Trailblazer
File(s): /home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Pathing/Search/NavigationDependencyWorkspace.cs
Line coverage
100%
Covered lines: 63
Uncovered lines: 0
Coverable lines: 63
Total lines: 123
Line coverage: 100%
Branch coverage
100%
Covered branches: 38
Total branches: 38
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%44100%
Reset()100%44100%
TryRecordComponent(...)100%44100%
TryRecordPage(...)100%44100%
RecordTransitionDependency()100%11100%
TryCountMissing(...)100%1010100%
CanFit(...)100%66100%
CommitMerge(...)100%66100%

File(s)

/home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Pathing/Search/NavigationDependencyWorkspace.cs

#LineLine coverage
 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
 8using System;
 9
 10namespace Trailblazer.Pathing;
 11
 12/// <summary>Owns fixed page and component dependency accumulation.</summary>
 13internal sealed class NavigationDependencyWorkspace
 14{
 15    private readonly NavigationAddressStampSet _componentSet;
 16    private readonly NavigationPageStampSet _pageSet;
 17
 3463218    internal NavigationDependencyWorkspace(int pageCapacity, int componentCapacity)
 19    {
 3463220        SwiftThrowHelper.ThrowIfNegative(pageCapacity, nameof(pageCapacity));
 3463221        SwiftThrowHelper.ThrowIfNegative(componentCapacity, nameof(componentCapacity));
 3463222        Pages = pageCapacity == 0
 3463223            ? Array.Empty<GraphPageDependencyAddress>()
 3463224            : new GraphPageDependencyAddress[pageCapacity];
 3463225        Components = componentCapacity == 0
 3463226            ? Array.Empty<NavigationSurfaceComponentKey>()
 3463227            : new NavigationSurfaceComponentKey[componentCapacity];
 3463228        _componentSet = new NavigationAddressStampSet(Math.Max(1, componentCapacity));
 3463229        _pageSet = new NavigationPageStampSet(Math.Max(1, pageCapacity));
 3463230    }
 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    {
 846544        if (PageCount > 0)
 232045            Array.Clear(Pages, 0, PageCount);
 846546        if (ComponentCount > 0)
 204847            Array.Clear(Components, 0, ComponentCount);
 846548        PageCount = 0;
 846549        ComponentCount = 0;
 846550        HasTransitionDependency = false;
 846551        _componentSet.Reset();
 846552        _pageSet.Reset();
 846553    }
 54
 55    internal bool TryRecordComponent(NavigationSurfaceComponentKey component)
 56    {
 1394157        if (ComponentCount >= Components.Length)
 121758            return _componentSet.Contains(component.Representative);
 1272459        if (!_componentSet.Add(component.Representative))
 937860            return true;
 334661        Components[ComponentCount++] = component;
 334662        return true;
 63    }
 64
 65    internal bool TryRecordPage(string mapId, int pageIndex)
 66    {
 4895967        if (PageCount >= Pages.Length)
 161768            return _pageSet.Contains(mapId, pageIndex);
 4734269        if (!_pageSet.Add(mapId, pageIndex))
 4353570            return true;
 380771        Pages[PageCount++] = new GraphPageDependencyAddress(mapId, pageIndex);
 380772        return true;
 73    }
 74
 25575    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    {
 3583        SwiftThrowHelper.ThrowIfNull(source, nameof(source));
 3584        SwiftThrowHelper.ThrowIfNull(meter, nameof(meter));
 3585        missingComponents = 0;
 3586        missingPages = 0;
 3587        int probeCount = source.ComponentCount + source.PageCount;
 3588        if (!meter.TryConsumeLookupProbes(probeCount))
 289            return false;
 13490        for (int i = 0; i < source.ComponentCount; i++)
 91        {
 3492            if (!_componentSet.Contains(source.Components[i].Representative))
 593                missingComponents++;
 94        }
 13495        for (int i = 0; i < source.PageCount; i++)
 96        {
 3497            GraphPageDependencyAddress page = source.Pages[i];
 3498            if (!_pageSet.Contains(page.MapId, page.PageIndex))
 599                missingPages++;
 100        }
 33101        return true;
 102    }
 103
 104    internal bool CanFit(int additionalComponents, int additionalPages) =>
 32105        additionalComponents >= 0
 32106        && additionalPages >= 0
 32107        && additionalComponents <= Components.Length - ComponentCount
 32108        && additionalPages <= Pages.Length - PageCount;
 109
 110    internal void CommitMerge(NavigationDependencyWorkspace source)
 111    {
 28112        SwiftThrowHelper.ThrowIfNull(source, nameof(source));
 110113        for (int i = 0; i < source.ComponentCount; i++)
 27114            TryRecordComponent(source.Components[i]);
 112115        for (int i = 0; i < source.PageCount; i++)
 116        {
 28117            GraphPageDependencyAddress page = source.Pages[i];
 28118            TryRecordPage(page.MapId, page.PageIndex);
 119        }
 28120        if (source.HasTransitionDependency)
 1121            HasTransitionDependency = true;
 28122    }
 123}