< Summary

Information
Class: Trailblazer.Pathing.NavigationSurfaceComponentKeySet
Assembly: Trailblazer
File(s): /home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Pathing/Graph/NavigationSurfaceComponentKeySet.cs
Line coverage
100%
Covered lines: 83
Uncovered lines: 0
Coverable lines: 83
Total lines: 154
Line coverage: 100%
Branch coverage
100%
Covered branches: 34
Total branches: 34
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%11100%
get_RetainedBytes()100%11100%
get_PersistentPageCount()100%11100%
Contains(...)100%44100%
Add(...)100%88100%
Remove(...)100%1414100%
GetEnumerator()100%11100%
.ctor(...)100%11100%
get_Current()100%11100%
MoveNext()100%88100%

File(s)

/home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Pathing/Graph/NavigationSurfaceComponentKeySet.cs

#LineLine coverage
 1//=======================================================================
 2// NavigationSurfaceComponentKeySet.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
 8namespace Trailblazer.Pathing;
 9
 10/// <summary>Stores a persistent canonical set of exact address-medium keys.</summary>
 11internal sealed class NavigationSurfaceComponentKeySet
 12{
 13    private readonly PersistentStringMap<PersistentVoxelIndexMap<byte>> _maps;
 14    private readonly long _innerBytes;
 15    private readonly int _innerPages;
 16
 256717    private NavigationSurfaceComponentKeySet(
 256718        PersistentStringMap<PersistentVoxelIndexMap<byte>> maps,
 256719        int count,
 256720        long innerBytes,
 256721        int innerPages)
 22    {
 256723        _maps = maps;
 256724        Count = count;
 256725        _innerBytes = innerBytes;
 256726        _innerPages = innerPages;
 256727    }
 28
 29    internal static NavigationSurfaceComponentKeySet Empty { get; } = new(
 30        PersistentStringMap<PersistentVoxelIndexMap<byte>>.Empty,
 31        0,
 32        0,
 33        0);
 34
 35    internal int Count { get; }
 36
 30295037    internal long RetainedBytes => checked(40L + _maps.RetainedBytes + _innerBytes);
 38
 30356739    internal int PersistentPageCount => checked(
 30356740        1 + _maps.PersistentNodeCount + _innerPages);
 41
 42    internal bool Contains(NavigationSurfaceComponentKey key) =>
 203149443        _maps.TryGetValue(
 203149444            key.Representative.MapId,
 203149445            out PersistentVoxelIndexMap<byte> values)
 203149446        && values.TryGetValue(key.Representative.Index, out byte mask)
 203149447        && (mask & NavigationMediumSlots<byte>.GetBit(key.Medium)) != 0;
 48
 49    internal NavigationSurfaceComponentKeySet Add(NavigationSurfaceComponentKey key)
 50    {
 251651        string mapId = key.Representative.MapId;
 251652        bool hadMap = _maps.TryGetValue(
 251653            mapId,
 251654            out PersistentVoxelIndexMap<byte> values);
 251655        values ??= PersistentVoxelIndexMap<byte>.Empty;
 251656        byte bit = NavigationMediumSlots<byte>.GetBit(key.Medium);
 251657        values.TryGetValue(key.Representative.Index, out byte mask);
 251658        if ((mask & bit) != 0)
 359            return this;
 251360        long innerBytes = _innerBytes - (hadMap ? values.RetainedBytes : 0L);
 251361        int innerPages = _innerPages - (hadMap ? values.PersistentNodeCount : 0);
 251362        values = values.Set(key.Representative.Index, (byte)(mask | bit));
 251363        return new NavigationSurfaceComponentKeySet(
 251364            _maps.Set(mapId, values),
 251365            checked(Count + 1),
 251366            checked(innerBytes + values.RetainedBytes),
 251367            checked(innerPages + values.PersistentNodeCount));
 68    }
 69
 70    internal NavigationSurfaceComponentKeySet Remove(NavigationSurfaceComponentKey key)
 71    {
 5572        string mapId = key.Representative.MapId;
 5573        if (!_maps.TryGetValue(mapId, out PersistentVoxelIndexMap<byte> values)
 5574            || !values.TryGetValue(key.Representative.Index, out byte mask))
 75        {
 176            return this;
 77        }
 5478        byte bit = NavigationMediumSlots<byte>.GetBit(key.Medium);
 5479        if ((mask & bit) == 0)
 180            return this;
 5381        long innerBytes = _innerBytes - values.RetainedBytes;
 5382        int innerPages = _innerPages - values.PersistentNodeCount;
 5383        byte nextMask = (byte)(mask & ~bit);
 5384        values = nextMask == 0
 5385            ? values.Remove(key.Representative.Index, out _)
 5386            : values.Set(key.Representative.Index, nextMask);
 5387        PersistentStringMap<PersistentVoxelIndexMap<byte>> maps = values.Count == 0
 5388            ? _maps.Remove(mapId, out _)
 5389            : _maps.Set(mapId, values);
 5390        return new NavigationSurfaceComponentKeySet(
 5391            maps,
 5392            Count - 1,
 5393            checked(innerBytes + (values.Count == 0 ? 0L : values.RetainedBytes)),
 5394            checked(innerPages + (values.Count == 0 ? 0 : values.PersistentNodeCount)));
 95    }
 96
 279797    internal Enumerator GetEnumerator() => new(this);
 98
 99    internal struct Enumerator
 100    {
 101        private readonly NavigationSurfaceComponentKeySet _set;
 102        private int _map;
 103        private int _address;
 104        private TraversalMedium _medium;
 105        private TraversalMedium _currentMedium;
 106
 107        internal Enumerator(NavigationSurfaceComponentKeySet set)
 108        {
 2797109            _set = set;
 2797110            _map = 0;
 2797111            _address = 0;
 2797112            _medium = TraversalMedium.Solid;
 2797113            _currentMedium = TraversalMedium.Unknown;
 2797114        }
 115
 116        internal NavigationSurfaceComponentKey Current
 117        {
 118            get
 119            {
 2349120                PersistentVoxelIndexMap<byte> values = _set._maps.GetValueAt(_map);
 2349121                return new NavigationSurfaceComponentKey(
 2349122                    new NavigationCellAddress(
 2349123                        _set._maps.GetKeyAt(_map),
 2349124                        values.GetKeyAt(_address)),
 2349125                    _currentMedium);
 126            }
 127        }
 128
 129        internal bool MoveNext()
 130        {
 2396131            while (_map < _set._maps.Count)
 132            {
 2394133                PersistentVoxelIndexMap<byte> values = _set._maps.GetValueAt(_map);
 4263134                while (_address < values.Count)
 135                {
 4218136                    byte mask = values.GetValueAt(_address);
 8181137                    while (_medium <= TraversalMedium.Liquid)
 138                    {
 6312139                        TraversalMedium medium = _medium++;
 6312140                        if ((mask & NavigationMediumSlots<byte>.GetBit(medium)) == 0)
 141                            continue;
 2349142                        _currentMedium = medium;
 2349143                        return true;
 144                    }
 1869145                    _address++;
 1869146                    _medium = TraversalMedium.Solid;
 147                }
 45148                _map++;
 45149                _address = 0;
 150            }
 2151            return false;
 152        }
 153    }
 154}