< Summary

Information
Class: GridForge.Spatial.PartitionProvider<T>
Assembly: GridForge
File(s): /home/runner/work/GridForge/GridForge/src/GridForge/Spatial/PartitionProvider.cs
Line coverage
100%
Covered lines: 30
Uncovered lines: 0
Coverable lines: 30
Total lines: 131
Line coverage: 100%
Branch coverage
100%
Covered branches: 32
Total branches: 32
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Partitions()100%44100%
get_IsEmpty()100%22100%
get_Count()100%22100%
TryAdd(...)100%66100%
TryRemove(...)100%88100%
TryGet(...)100%44100%
TryGet(...)100%44100%
Has(...)100%11100%
Has()100%11100%
Clear()100%22100%

File(s)

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

#LineLine coverage
 1using SwiftCollections;
 2using System;
 3using System.Collections.Generic;
 4using System.Linq;
 5using System.Runtime.CompilerServices;
 6
 7namespace GridForge.Spatial;
 8
 9/// <summary>
 10/// Provides efficient storage and retrieval of partitions keyed by their exact concrete <see cref="Type"/>.
 11/// </summary>
 12public sealed class PartitionProvider<TPartitionBase> where TPartitionBase : class
 13{
 14    /// <summary>
 15    /// Backing dictionary that stores partition instances keyed by their exact concrete type.
 16    /// </summary>
 17    private SwiftDictionary<Type, TPartitionBase>? _partitions;
 18
 19    /// <summary>
 20    /// Returns an enumerable of all partitions currently stored in the provider.
 21    /// </summary>
 722    internal IEnumerable<TPartitionBase> Partitions => _partitions?.Values ?? Enumerable.Empty<TPartitionBase>();
 23
 24    /// <summary>
 25    /// Indicates whether the provider currently contains any partitions.
 26    /// Returns true if empty; otherwise, false.
 27    /// </summary>
 7815028    public bool IsEmpty => _partitions == null || _partitions.Count == 0;
 29
 30    /// <summary>
 31    /// Gets the current number of partitions stored in the provider.
 32    /// </summary>
 333    public int Count => _partitions?.Count ?? 0;
 34
 35    /// <summary>
 36    /// Attempts to add a partition to the provider with the specified type key.
 37    /// Returns true if the partition was added; false if a partition with the same type already exists.
 38    /// </summary>
 39    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 40    public bool TryAdd(Type partitionType, TPartitionBase partition)
 41    {
 1842        if (partitionType == null || partition == null)
 243            return false;
 44
 1645        _partitions ??= new SwiftDictionary<Type, TPartitionBase>();
 1646        return _partitions.Add(partitionType, partition);
 47    }
 48
 49    /// <summary>
 50    /// Attempts to remove a partition associated with the specified type.
 51    /// If successful, the removed partition is returned in the out parameter.
 52    /// Returns true if the partition was removed; otherwise, false.
 53    /// </summary>
 54    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 55    public bool TryRemove(Type partitionType, out TPartitionBase? partition)
 56    {
 1057        partition = null;
 58
 1059        if (partitionType == null || _partitions == null)
 360            return false;
 61
 762        if (!_partitions.TryGetValue(partitionType, out partition))
 163            return false;
 64
 665        _partitions.Remove(partitionType);
 66
 667        if (_partitions.Count == 0)
 468            _partitions = null; // Auto-clear empty
 69
 670        return true;
 71    }
 72
 73    /// <summary>
 74    /// Attempts to retrieve a partition associated with the specified concrete type.
 75    /// </summary>
 76    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 77    public bool TryGet(Type partitionType, out TPartitionBase? partition)
 78    {
 2679        partition = null;
 80
 2681        if (partitionType == null || _partitions == null)
 1382            return false;
 83
 1384        return _partitions.TryGetValue(partitionType, out partition);
 85    }
 86
 87    /// <summary>
 88    /// Attempts to retrieve a partition of the specified type.
 89    /// Returns true and sets the out parameter if the partition exists and is of the requested type; otherwise, returns
 90    /// </summary>
 91    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 92    public bool TryGet<T>(out T? partition) where T : TPartitionBase
 93    {
 2194        partition = default;
 95
 2196        if (!TryGet(typeof(T), out TPartitionBase? tempPartition) || tempPartition is not T typedPartition)
 1397            return false;
 98
 899        partition = typedPartition;
 8100        return true;
 101    }
 102
 103    /// <summary>
 104    /// Determines whether the provider contains a partition associated with the specified concrete type.
 105    /// </summary>
 106    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 107    public bool Has(Type partitionType)
 108    {
 2109        return TryGet(partitionType, out _);
 110    }
 111
 112    /// <summary>
 113    /// Determines whether the provider contains a partition of the specified type.
 114    /// Returns true if such a partition exists; otherwise, false.
 115    /// </summary>
 116    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 117    public bool Has<T>() where T : TPartitionBase
 118    {
 6119        return TryGet<T>(out _);
 120    }
 121
 122    /// <summary>
 123    /// Removes all partitions from the provider, clearing its internal storage.
 124    /// </summary>
 125    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 126    public void Clear()
 127    {
 8128        _partitions?.Clear();
 8129        _partitions = null;
 8130    }
 131}