< Summary

Line coverage
100%
Covered lines: 119
Uncovered lines: 0
Coverable lines: 119
Total lines: 363
Line coverage: 100%
Branch coverage
100%
Covered branches: 64
Total branches: 64
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/GridForge/GridForge/src/GridForge/Spatial/PartitionProvider.cs

#LineLine coverage
 1//=======================================================================
 2// PartitionProvider.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;
 9using System.Runtime.CompilerServices;
 10using SwiftCollections;
 11
 12namespace GridForge.Spatial;
 13
 14/// <summary>
 15/// Provides efficient storage and retrieval of partitions keyed by their exact concrete <see cref="Type"/>.
 16/// </summary>
 17/// <remarks>
 18/// The first two concrete types are stored inline. Additional types use provider-owned overflow
 19/// storage that is cleared and retained after compaction so a later promotion can reuse it.
 20/// Enumeration and compaction preserve registration order.
 21/// </remarks>
 22public sealed partial class PartitionProvider<TPartitionBase> where TPartitionBase : class
 23{
 24    /// <summary>
 25    /// The first inline partition used by the common one- and two-partition paths.
 26    /// </summary>
 27    private Type? _firstPartitionType;
 28
 29    /// <summary>
 30    /// The first inline partition used by the common one- and two-partition paths.
 31    /// </summary>
 32    private TPartitionBase? _firstPartition;
 33
 34    /// <summary>
 35    /// The second inline partition used by the common two-partition-per-voxel path.
 36    /// </summary>
 37    private Type? _secondPartitionType;
 38
 39    /// <summary>
 40    /// The second inline partition used by the common two-partition-per-voxel path.
 41    /// </summary>
 42    private TPartitionBase? _secondPartition;
 43
 44    /// <summary>
 45    /// Overflow storage used only when a voxel hosts more than two concrete partition types.
 46    /// The dictionary remains owned by the provider after compaction so later promotions can reuse it.
 47    /// </summary>
 48    private SwiftList<OverflowPartition>? _overflowPartitions;
 49
 50    /// <summary>
 51    /// Indicates whether the provider currently contains any partitions.
 52    /// Returns true if empty; otherwise, false.
 53    /// </summary>
 35561654    public bool IsEmpty => _firstPartition == null;
 55
 56    /// <summary>
 57    /// Gets the current number of partitions stored in the provider.
 58    /// </summary>
 59    public int Count =>
 660        (_firstPartition != null ? 1 : 0)
 661        + (_secondPartition != null ? 1 : 0)
 662        + (_overflowPartitions?.Count ?? 0);
 63
 64    /// <summary>
 65    /// Attempts to add a partition to the provider with the specified type key.
 66    /// Returns true if the partition was added; false if a partition with the same type already exists.
 67    /// </summary>
 68    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 69    public bool TryAdd(Type partitionType, TPartitionBase partition)
 70    {
 71        // Both checks are side-effect free; evaluating both avoids an extra hot-path branch.
 11572        if (partitionType == null | partition == null)
 273            return false;
 74
 11375        if (_firstPartition == null)
 76        {
 6677            _firstPartitionType = partitionType;
 6678            _firstPartition = partition;
 6679            return true;
 80        }
 81
 4782        if (_firstPartitionType == partitionType)
 383            return false;
 84
 4485        if (_secondPartition == null)
 86        {
 3087            _secondPartitionType = partitionType;
 3088            _secondPartition = partition;
 3089            return true;
 90        }
 91
 1492        if (_secondPartitionType == partitionType)
 193            return false;
 94
 1395        return TryAddOverflowPartition(partitionType!, partition!);
 96    }
 97
 98    /// <summary>
 99    /// Attempts to remove a partition associated with the specified type.
 100    /// If successful, the removed partition is returned in the out parameter.
 101    /// Returns true if the partition was removed; otherwise, false.
 102    /// </summary>
 103    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 104    public bool TryRemove(Type partitionType, out TPartitionBase? partition)
 105    {
 68106        partition = null;
 107
 68108        if (partitionType == null)
 1109            return false;
 110
 67111        if (_firstPartitionType == partitionType)
 112        {
 43113            partition = _firstPartition;
 43114            _firstPartitionType = _secondPartitionType;
 43115            _firstPartition = _secondPartition;
 43116            ClearSecondPartition();
 43117            MoveFirstOverflowPartitionToSecondSlot();
 43118            return true;
 119        }
 120
 24121        if (_secondPartitionType == partitionType)
 122        {
 18123            partition = _secondPartition;
 18124            ClearSecondPartition();
 18125            MoveFirstOverflowPartitionToSecondSlot();
 18126            return true;
 127        }
 128
 6129        return TryRemoveOverflowPartition(partitionType, out partition);
 130    }
 131
 132    /// <summary>
 133    /// Attempts to retrieve a partition associated with the specified concrete type.
 134    /// </summary>
 135    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 136    public bool TryGet(Type partitionType, out TPartitionBase? partition)
 137    {
 1167138        partition = null;
 139
 1167140        if (partitionType == null)
 1141            return false;
 142
 1166143        if (_firstPartitionType == partitionType)
 144        {
 1080145            partition = _firstPartition;
 1080146            return true;
 147        }
 148
 86149        if (_secondPartitionType == partitionType)
 150        {
 17151            partition = _secondPartition;
 17152            return true;
 153        }
 154
 69155        return TryGetOverflowPartition(partitionType, out partition);
 156    }
 157
 158    /// <summary>
 159    /// Attempts to retrieve a partition of the specified type.
 160    /// Returns true and sets the out parameter if the partition exists and is of the requested type; otherwise, returns
 161    /// </summary>
 162    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 163    public bool TryGet<T>(out T? partition) where T : TPartitionBase
 164    {
 9165        partition = default;
 166
 9167        if (!TryGet(typeof(T), out TPartitionBase? tempPartition) || tempPartition is not T typedPartition)
 1168            return false;
 169
 8170        partition = typedPartition;
 8171        return true;
 172    }
 173
 174    /// <summary>
 175    /// Determines whether the provider contains a partition associated with the specified concrete type.
 176    /// </summary>
 177    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 178    public bool Has(Type partitionType)
 179    {
 1180        return TryGet(partitionType, out _);
 181    }
 182
 183    /// <summary>
 184    /// Determines whether the provider contains a partition of the specified type.
 185    /// Returns true if such a partition exists; otherwise, false.
 186    /// </summary>
 187    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 188    public bool Has<T>() where T : TPartitionBase
 189    {
 1190        return TryGet<T>(out _);
 191    }
 192
 193    /// <summary>
 194    /// Removes all partitions from the provider, clearing its internal storage.
 195    /// </summary>
 196    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 197    public void Clear()
 198    {
 22199        _firstPartitionType = null;
 22200        _firstPartition = null;
 22201        ClearSecondPartition();
 22202        _overflowPartitions?.Clear();
 10203    }
 204
 205    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 206    private void ClearSecondPartition()
 207    {
 83208        _secondPartitionType = null;
 83209        _secondPartition = null;
 83210    }
 211
 212    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 213    private bool TryAddOverflowPartition(Type partitionType, TPartitionBase partition)
 214    {
 13215        if (FindOverflowPartitionIndex(partitionType) >= 0)
 1216            return false;
 217
 12218        _overflowPartitions ??= new SwiftList<OverflowPartition>(4);
 12219        _overflowPartitions.Add(new OverflowPartition(partitionType, partition));
 12220        return true;
 221    }
 222
 223    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 224    private bool TryRemoveOverflowPartition(Type partitionType, out TPartitionBase? partition)
 225    {
 6226        int index = FindOverflowPartitionIndex(partitionType);
 6227        if (index < 0)
 228        {
 3229            partition = null;
 3230            return false;
 231        }
 232
 3233        partition = _overflowPartitions!.InnerArray[index].Partition;
 3234        _overflowPartitions.RemoveAt(index);
 3235        return true;
 236    }
 237
 238    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 239    private bool TryGetOverflowPartition(Type partitionType, out TPartitionBase? partition)
 240    {
 69241        int index = FindOverflowPartitionIndex(partitionType);
 69242        if (index < 0)
 243        {
 57244            partition = null;
 57245            return false;
 246        }
 247
 12248        partition = _overflowPartitions!.InnerArray[index].Partition;
 12249        return true;
 250    }
 251
 252    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 253    private int FindOverflowPartitionIndex(Type partitionType)
 254    {
 88255        if (_overflowPartitions == null)
 23256            return -1;
 257
 65258        OverflowPartition[] partitions = _overflowPartitions.InnerArray;
 148259        for (int i = 0; i < _overflowPartitions.Count; i++)
 260        {
 25261            if (partitions[i].Type == partitionType)
 16262                return i;
 263        }
 264
 49265        return -1;
 266    }
 267
 268    private void MoveFirstOverflowPartitionToSecondSlot()
 269    {
 61270        if (_overflowPartitions == null || _overflowPartitions.Count == 0)
 59271            return;
 272
 2273        OverflowPartition overflowPartition = _overflowPartitions[0];
 2274        _overflowPartitions.RemoveAt(0);
 2275        _secondPartitionType = overflowPartition.Type;
 2276        _secondPartition = overflowPartition.Partition;
 2277    }
 278
 279    /// <summary>
 280    /// Returns an allocation-free enumerator for the provider's current partitions.
 281    /// </summary>
 282    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 25283    internal Enumerator GetEnumerator() => new(this);
 284
 285
 286
 287    private readonly struct OverflowPartition
 288    {
 289        internal OverflowPartition(Type type, TPartitionBase partition)
 290        {
 12291            Type = type;
 12292            Partition = partition;
 12293        }
 294
 295        internal Type Type { get; }
 296
 297        internal TPartitionBase Partition { get; }
 298    }
 299}

/home/runner/work/GridForge/GridForge/src/GridForge/Spatial/PartitionProvider.Enumerator.cs

#LineLine coverage
 1//=======================================================================
 2// PartitionProvider.Enumerator.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 SwiftCollections;
 9
 10namespace GridForge.Spatial;
 11
 12public sealed partial class PartitionProvider<TPartitionBase>
 13    where TPartitionBase : class
 14{
 15    internal struct Enumerator
 16    {
 17        private readonly TPartitionBase? _firstPartition;
 18        private readonly TPartitionBase? _secondPartition;
 19        private readonly bool _hasOverflow;
 20        private SwiftList<OverflowPartition>.SwiftListEnumerator _overflowEnumerator;
 21        private int _inlineState;
 22
 23        internal Enumerator(PartitionProvider<TPartitionBase> provider)
 24        {
 2525            _firstPartition = provider._firstPartition;
 2526            _secondPartition = provider._secondPartition;
 2527            _overflowEnumerator = provider._overflowPartitions != null
 2528                ? provider._overflowPartitions.GetEnumerator()
 2529                : default;
 2530            _hasOverflow = provider._overflowPartitions != null
 2531                && provider._overflowPartitions.Count > 0;
 2532            _inlineState = _firstPartition != null ? 0 : 2;
 2533            Current = default!;
 2534        }
 35
 36        public TPartitionBase Current { get; private set; }
 37
 38        public bool MoveNext()
 39        {
 6840            if (_inlineState == 0)
 41            {
 2442                Current = _firstPartition!;
 2443                _inlineState = 1;
 2444                return true;
 45            }
 46
 4447            if (_inlineState == 1)
 48            {
 2449                _inlineState = 2;
 2450                if (_secondPartition != null)
 51                {
 1052                    Current = _secondPartition;
 1053                    return true;
 54                }
 55            }
 56
 3457            if (!_hasOverflow || !_overflowEnumerator.MoveNext())
 2558                return false;
 59
 960            Current = _overflowEnumerator.Current.Partition;
 961            return true;
 62        }
 63    }
 64}