| | | 1 | | //======================================================================= |
| | | 2 | | // GridTraceIntervalScratch.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 SwiftCollections; |
| | | 9 | | |
| | | 10 | | namespace GridForge.Grids; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Owns reusable caller-side storage for allocation-free warmed interval traces. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// Instances retain capacity and are not thread-safe. Reentrant tracing or clearing an active instance |
| | | 17 | | /// throws before caller output is modified. Completed traces release temporary grid references. |
| | | 18 | | /// </remarks> |
| | | 19 | | public sealed class GridTraceIntervalScratch |
| | | 20 | | { |
| | | 21 | | private bool _isActive; |
| | | 22 | | internal SwiftList<ushort> CandidateGrids { get; } |
| | | 23 | | |
| | | 24 | | internal SwiftList<GridTraceAddressCandidate> AddressCandidates { get; } |
| | | 25 | | |
| | | 26 | | /// <summary>Creates trace scratch with optional expected grid and address counts.</summary> |
| | 146 | 27 | | public GridTraceIntervalScratch(int gridCapacity = 0, int addressCapacity = 0) |
| | | 28 | | { |
| | 146 | 29 | | ChronicleGuard.ThrowIfNegative(gridCapacity, nameof(gridCapacity)); |
| | 146 | 30 | | ChronicleGuard.ThrowIfNegative(addressCapacity, nameof(addressCapacity)); |
| | | 31 | | |
| | 146 | 32 | | CandidateGrids = new SwiftList<ushort>(gridCapacity); |
| | 146 | 33 | | AddressCandidates = new SwiftList<GridTraceAddressCandidate>(addressCapacity); |
| | 146 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary>Clears temporary values while retaining capacity.</summary> |
| | | 37 | | public void Clear() |
| | | 38 | | { |
| | 54 | 39 | | ThrowIfActive(); |
| | 53 | 40 | | ClearCore(); |
| | 53 | 41 | | } |
| | | 42 | | |
| | | 43 | | internal void Enter() |
| | | 44 | | { |
| | 287 | 45 | | ThrowIfActive(); |
| | 285 | 46 | | _isActive = true; |
| | 285 | 47 | | ClearCore(); |
| | 285 | 48 | | } |
| | | 49 | | |
| | | 50 | | internal void Exit() |
| | | 51 | | { |
| | 285 | 52 | | ClearCore(); |
| | 285 | 53 | | _isActive = false; |
| | 285 | 54 | | } |
| | | 55 | | |
| | 341 | 56 | | private void ThrowIfActive() => ChronicleGuard.ThrowIfTrue( |
| | 341 | 57 | | _isActive, nameof(GridTraceIntervalScratch), "Interval trace scratch is already in use."); |
| | | 58 | | |
| | | 59 | | private void ClearCore() |
| | | 60 | | { |
| | 623 | 61 | | CandidateGrids.Clear(); |
| | 623 | 62 | | AddressCandidates.Clear(); |
| | 623 | 63 | | } |
| | | 64 | | } |