< 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: 28
Uncovered lines: 0
Coverable lines: 28
Total lines: 135
Line coverage: 100%
Branch coverage
100%
Covered branches: 30
Total branches: 30
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%66100%
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
 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 SwiftCollections;
 9using System;
 10using System.Collections.Generic;
 11using System.Linq;
 12using System.Runtime.CompilerServices;
 13
 14namespace GridForge.Spatial;
 15
 16/// <summary>
 17/// Provides efficient storage and retrieval of partitions keyed by their exact concrete <see cref="Type"/>.
 18/// </summary>
 19public sealed class PartitionProvider<TPartitionBase> where TPartitionBase : class
 20{
 21    /// <summary>
 22    /// Backing dictionary that stores partition instances keyed by their exact concrete type.
 23    /// </summary>
 24    private SwiftDictionary<Type, TPartitionBase>? _partitions;
 25
 26    /// <summary>
 27    /// Returns an enumerable of all partitions currently stored in the provider.
 28    /// </summary>
 1029    internal IEnumerable<TPartitionBase> Partitions => _partitions?.Values ?? Enumerable.Empty<TPartitionBase>();
 30
 31    /// <summary>
 32    /// Indicates whether the provider currently contains any partitions.
 33    /// Returns true if empty; otherwise, false.
 34    /// </summary>
 10470835    public bool IsEmpty => _partitions == null || _partitions.Count == 0;
 36
 37    /// <summary>
 38    /// Gets the current number of partitions stored in the provider.
 39    /// </summary>
 340    public int Count => _partitions?.Count ?? 0;
 41
 42    /// <summary>
 43    /// Attempts to add a partition to the provider with the specified type key.
 44    /// Returns true if the partition was added; false if a partition with the same type already exists.
 45    /// </summary>
 46    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 47    public bool TryAdd(Type partitionType, TPartitionBase partition)
 48    {
 28049        if (partitionType == null || partition == null)
 250            return false;
 51
 27852        _partitions ??= new SwiftDictionary<Type, TPartitionBase>();
 27853        return _partitions.Add(partitionType, partition);
 54    }
 55
 56    /// <summary>
 57    /// Attempts to remove a partition associated with the specified type.
 58    /// If successful, the removed partition is returned in the out parameter.
 59    /// Returns true if the partition was removed; otherwise, false.
 60    /// </summary>
 61    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 62    public bool TryRemove(Type partitionType, out TPartitionBase? partition)
 63    {
 27064        partition = null;
 65
 27066        if (partitionType == null || _partitions == null)
 367            return false;
 68
 26769        if (!_partitions.TryGetValue(partitionType, out partition))
 270            return false;
 71
 26572        _partitions.Remove(partitionType);
 73
 26574        return true;
 75    }
 76
 77    /// <summary>
 78    /// Attempts to retrieve a partition associated with the specified concrete type.
 79    /// </summary>
 80    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 81    public bool TryGet(Type partitionType, out TPartitionBase? partition)
 82    {
 2883        partition = null;
 84
 2885        if (partitionType == null || _partitions == null)
 886            return false;
 87
 2088        return _partitions.TryGetValue(partitionType, out partition);
 89    }
 90
 91    /// <summary>
 92    /// Attempts to retrieve a partition of the specified type.
 93    /// Returns true and sets the out parameter if the partition exists and is of the requested type; otherwise, returns
 94    /// </summary>
 95    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 96    public bool TryGet<T>(out T? partition) where T : TPartitionBase
 97    {
 2398        partition = default;
 99
 23100        if (!TryGet(typeof(T), out TPartitionBase? tempPartition) || tempPartition is not T typedPartition)
 13101            return false;
 102
 10103        partition = typedPartition;
 10104        return true;
 105    }
 106
 107    /// <summary>
 108    /// Determines whether the provider contains a partition associated with the specified concrete type.
 109    /// </summary>
 110    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 111    public bool Has(Type partitionType)
 112    {
 2113        return TryGet(partitionType, out _);
 114    }
 115
 116    /// <summary>
 117    /// Determines whether the provider contains a partition of the specified type.
 118    /// Returns true if such a partition exists; otherwise, false.
 119    /// </summary>
 120    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 121    public bool Has<T>() where T : TPartitionBase
 122    {
 6123        return TryGet<T>(out _);
 124    }
 125
 126    /// <summary>
 127    /// Removes all partitions from the provider, clearing its internal storage.
 128    /// </summary>
 129    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 130    public void Clear()
 131    {
 11132        _partitions?.Clear();
 11133        _partitions = null;
 11134    }
 135}