| | | 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 | | |
| | | 8 | | using System; |
| | | 9 | | using System.Runtime.CompilerServices; |
| | | 10 | | using SwiftCollections; |
| | | 11 | | |
| | | 12 | | namespace 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> |
| | | 22 | | public sealed 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> |
| | 131012 | 54 | | 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 => |
| | 6 | 60 | | (_firstPartition != null ? 1 : 0) |
| | 6 | 61 | | + (_secondPartition != null ? 1 : 0) |
| | 6 | 62 | | + (_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. |
| | 97 | 72 | | if (partitionType == null | partition == null) |
| | 2 | 73 | | return false; |
| | | 74 | | |
| | 95 | 75 | | if (_firstPartition == null) |
| | | 76 | | { |
| | 56 | 77 | | _firstPartitionType = partitionType; |
| | 56 | 78 | | _firstPartition = partition; |
| | 56 | 79 | | return true; |
| | | 80 | | } |
| | | 81 | | |
| | 39 | 82 | | if (_firstPartitionType == partitionType) |
| | 3 | 83 | | return false; |
| | | 84 | | |
| | 36 | 85 | | if (_secondPartition == null) |
| | | 86 | | { |
| | 25 | 87 | | _secondPartitionType = partitionType; |
| | 25 | 88 | | _secondPartition = partition; |
| | 25 | 89 | | return true; |
| | | 90 | | } |
| | | 91 | | |
| | 11 | 92 | | if (_secondPartitionType == partitionType) |
| | 1 | 93 | | return false; |
| | | 94 | | |
| | 10 | 95 | | 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 | | { |
| | 63 | 106 | | partition = null; |
| | | 107 | | |
| | 63 | 108 | | if (partitionType == null) |
| | 1 | 109 | | return false; |
| | | 110 | | |
| | 62 | 111 | | if (_firstPartitionType == partitionType) |
| | | 112 | | { |
| | 40 | 113 | | partition = _firstPartition; |
| | 40 | 114 | | _firstPartitionType = _secondPartitionType; |
| | 40 | 115 | | _firstPartition = _secondPartition; |
| | 40 | 116 | | ClearSecondPartition(); |
| | 40 | 117 | | MoveFirstOverflowPartitionToSecondSlot(); |
| | 40 | 118 | | return true; |
| | | 119 | | } |
| | | 120 | | |
| | 22 | 121 | | if (_secondPartitionType == partitionType) |
| | | 122 | | { |
| | 17 | 123 | | partition = _secondPartition; |
| | 17 | 124 | | ClearSecondPartition(); |
| | 17 | 125 | | MoveFirstOverflowPartitionToSecondSlot(); |
| | 17 | 126 | | return true; |
| | | 127 | | } |
| | | 128 | | |
| | 5 | 129 | | 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 | | { |
| | 1078 | 138 | | partition = null; |
| | | 139 | | |
| | 1078 | 140 | | if (partitionType == null) |
| | 1 | 141 | | return false; |
| | | 142 | | |
| | 1077 | 143 | | if (_firstPartitionType == partitionType) |
| | | 144 | | { |
| | 1052 | 145 | | partition = _firstPartition; |
| | 1052 | 146 | | return true; |
| | | 147 | | } |
| | | 148 | | |
| | 25 | 149 | | if (_secondPartitionType == partitionType) |
| | | 150 | | { |
| | 4 | 151 | | partition = _secondPartition; |
| | 4 | 152 | | return true; |
| | | 153 | | } |
| | | 154 | | |
| | 21 | 155 | | 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 | | { |
| | 1072 | 165 | | partition = default; |
| | | 166 | | |
| | 1072 | 167 | | if (!TryGet(typeof(T), out TPartitionBase? tempPartition) || tempPartition is not T typedPartition) |
| | 15 | 168 | | return false; |
| | | 169 | | |
| | 1057 | 170 | | partition = typedPartition; |
| | 1057 | 171 | | 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 | | { |
| | 1 | 180 | | 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 | | { |
| | 9 | 190 | | 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 | | { |
| | 15 | 199 | | _firstPartitionType = null; |
| | 15 | 200 | | _firstPartition = null; |
| | 15 | 201 | | ClearSecondPartition(); |
| | 15 | 202 | | _overflowPartitions?.Clear(); |
| | 5 | 203 | | } |
| | | 204 | | |
| | | 205 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 206 | | private void ClearSecondPartition() |
| | | 207 | | { |
| | 72 | 208 | | _secondPartitionType = null; |
| | 72 | 209 | | _secondPartition = null; |
| | 72 | 210 | | } |
| | | 211 | | |
| | | 212 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 213 | | private bool TryAddOverflowPartition(Type partitionType, TPartitionBase partition) |
| | | 214 | | { |
| | 10 | 215 | | if (FindOverflowPartitionIndex(partitionType) >= 0) |
| | 1 | 216 | | return false; |
| | | 217 | | |
| | 9 | 218 | | _overflowPartitions ??= new SwiftList<OverflowPartition>(4); |
| | 9 | 219 | | _overflowPartitions.Add(new OverflowPartition(partitionType, partition)); |
| | 9 | 220 | | return true; |
| | | 221 | | } |
| | | 222 | | |
| | | 223 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 224 | | private bool TryRemoveOverflowPartition(Type partitionType, out TPartitionBase? partition) |
| | | 225 | | { |
| | 5 | 226 | | int index = FindOverflowPartitionIndex(partitionType); |
| | 5 | 227 | | if (index < 0) |
| | | 228 | | { |
| | 3 | 229 | | partition = null; |
| | 3 | 230 | | return false; |
| | | 231 | | } |
| | | 232 | | |
| | 2 | 233 | | partition = _overflowPartitions!.InnerArray[index].Partition; |
| | 2 | 234 | | _overflowPartitions.RemoveAt(index); |
| | 2 | 235 | | return true; |
| | | 236 | | } |
| | | 237 | | |
| | | 238 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 239 | | private bool TryGetOverflowPartition(Type partitionType, out TPartitionBase? partition) |
| | | 240 | | { |
| | 21 | 241 | | int index = FindOverflowPartitionIndex(partitionType); |
| | 21 | 242 | | if (index < 0) |
| | | 243 | | { |
| | 19 | 244 | | partition = null; |
| | 19 | 245 | | return false; |
| | | 246 | | } |
| | | 247 | | |
| | 2 | 248 | | partition = _overflowPartitions!.InnerArray[index].Partition; |
| | 2 | 249 | | return true; |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 253 | | private int FindOverflowPartitionIndex(Type partitionType) |
| | | 254 | | { |
| | 36 | 255 | | if (_overflowPartitions == null) |
| | 19 | 256 | | return -1; |
| | | 257 | | |
| | 17 | 258 | | OverflowPartition[] partitions = _overflowPartitions.InnerArray; |
| | 46 | 259 | | for (int i = 0; i < _overflowPartitions.Count; i++) |
| | | 260 | | { |
| | 11 | 261 | | if (partitions[i].Type == partitionType) |
| | 5 | 262 | | return i; |
| | | 263 | | } |
| | | 264 | | |
| | 12 | 265 | | return -1; |
| | | 266 | | } |
| | | 267 | | |
| | | 268 | | private void MoveFirstOverflowPartitionToSecondSlot() |
| | | 269 | | { |
| | 57 | 270 | | if (_overflowPartitions == null || _overflowPartitions.Count == 0) |
| | 55 | 271 | | return; |
| | | 272 | | |
| | 2 | 273 | | OverflowPartition overflowPartition = _overflowPartitions[0]; |
| | 2 | 274 | | _overflowPartitions.RemoveAt(0); |
| | 2 | 275 | | _secondPartitionType = overflowPartition.Type; |
| | 2 | 276 | | _secondPartition = overflowPartition.Partition; |
| | 2 | 277 | | } |
| | | 278 | | |
| | | 279 | | /// <summary> |
| | | 280 | | /// Returns an allocation-free enumerator for the provider's current partitions. |
| | | 281 | | /// </summary> |
| | | 282 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 18 | 283 | | internal Enumerator GetEnumerator() => new(this); |
| | | 284 | | |
| | | 285 | | internal struct Enumerator |
| | | 286 | | { |
| | | 287 | | private readonly TPartitionBase? _firstPartition; |
| | | 288 | | private readonly TPartitionBase? _secondPartition; |
| | | 289 | | private readonly bool _hasOverflow; |
| | | 290 | | private SwiftList<OverflowPartition>.SwiftListEnumerator _overflowEnumerator; |
| | | 291 | | private int _inlineState; |
| | | 292 | | |
| | | 293 | | internal Enumerator(PartitionProvider<TPartitionBase> provider) |
| | | 294 | | { |
| | 18 | 295 | | _firstPartition = provider._firstPartition; |
| | 18 | 296 | | _secondPartition = provider._secondPartition; |
| | 18 | 297 | | _overflowEnumerator = provider._overflowPartitions != null |
| | 18 | 298 | | ? provider._overflowPartitions.GetEnumerator() |
| | 18 | 299 | | : default; |
| | 18 | 300 | | _hasOverflow = provider._overflowPartitions != null |
| | 18 | 301 | | && provider._overflowPartitions.Count > 0; |
| | 18 | 302 | | _inlineState = _firstPartition != null ? 0 : 2; |
| | 18 | 303 | | Current = default!; |
| | 18 | 304 | | } |
| | | 305 | | |
| | | 306 | | public TPartitionBase Current { get; private set; } |
| | | 307 | | |
| | | 308 | | public bool MoveNext() |
| | | 309 | | { |
| | 48 | 310 | | if (_inlineState == 0) |
| | | 311 | | { |
| | 17 | 312 | | Current = _firstPartition!; |
| | 17 | 313 | | _inlineState = 1; |
| | 17 | 314 | | return true; |
| | | 315 | | } |
| | | 316 | | |
| | 31 | 317 | | if (_inlineState == 1) |
| | | 318 | | { |
| | 17 | 319 | | _inlineState = 2; |
| | 17 | 320 | | if (_secondPartition != null) |
| | | 321 | | { |
| | 6 | 322 | | Current = _secondPartition; |
| | 6 | 323 | | return true; |
| | | 324 | | } |
| | | 325 | | } |
| | | 326 | | |
| | 25 | 327 | | if (!_hasOverflow || !_overflowEnumerator.MoveNext()) |
| | 18 | 328 | | return false; |
| | | 329 | | |
| | 7 | 330 | | Current = _overflowEnumerator.Current.Partition; |
| | 7 | 331 | | return true; |
| | | 332 | | } |
| | | 333 | | } |
| | | 334 | | |
| | | 335 | | private readonly struct OverflowPartition |
| | | 336 | | { |
| | | 337 | | internal OverflowPartition(Type type, TPartitionBase partition) |
| | | 338 | | { |
| | 9 | 339 | | Type = type; |
| | 9 | 340 | | Partition = partition; |
| | 9 | 341 | | } |
| | | 342 | | |
| | | 343 | | internal Type Type { get; } |
| | | 344 | | |
| | | 345 | | internal TPartitionBase Partition { get; } |
| | | 346 | | } |
| | | 347 | | } |