| | | 1 | | using SwiftCollections; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | |
| | | 7 | | namespace GridForge.Spatial; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Provides efficient storage and retrieval of partitions keyed by their exact concrete <see cref="Type"/>. |
| | | 11 | | /// </summary> |
| | | 12 | | public 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> |
| | 7 | 22 | | 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> |
| | 78150 | 28 | | public bool IsEmpty => _partitions == null || _partitions.Count == 0; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Gets the current number of partitions stored in the provider. |
| | | 32 | | /// </summary> |
| | 3 | 33 | | 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 | | { |
| | 18 | 42 | | if (partitionType == null || partition == null) |
| | 2 | 43 | | return false; |
| | | 44 | | |
| | 16 | 45 | | _partitions ??= new SwiftDictionary<Type, TPartitionBase>(); |
| | 16 | 46 | | 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 | | { |
| | 10 | 57 | | partition = null; |
| | | 58 | | |
| | 10 | 59 | | if (partitionType == null || _partitions == null) |
| | 3 | 60 | | return false; |
| | | 61 | | |
| | 7 | 62 | | if (!_partitions.TryGetValue(partitionType, out partition)) |
| | 1 | 63 | | return false; |
| | | 64 | | |
| | 6 | 65 | | _partitions.Remove(partitionType); |
| | | 66 | | |
| | 6 | 67 | | if (_partitions.Count == 0) |
| | 4 | 68 | | _partitions = null; // Auto-clear empty |
| | | 69 | | |
| | 6 | 70 | | 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 | | { |
| | 26 | 79 | | partition = null; |
| | | 80 | | |
| | 26 | 81 | | if (partitionType == null || _partitions == null) |
| | 13 | 82 | | return false; |
| | | 83 | | |
| | 13 | 84 | | 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 | | { |
| | 21 | 94 | | partition = default; |
| | | 95 | | |
| | 21 | 96 | | if (!TryGet(typeof(T), out TPartitionBase? tempPartition) || tempPartition is not T typedPartition) |
| | 13 | 97 | | return false; |
| | | 98 | | |
| | 8 | 99 | | partition = typedPartition; |
| | 8 | 100 | | 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 | | { |
| | 2 | 109 | | 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 | | { |
| | 6 | 119 | | 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 | | { |
| | 8 | 128 | | _partitions?.Clear(); |
| | 8 | 129 | | _partitions = null; |
| | 8 | 130 | | } |
| | | 131 | | } |