| | | 1 | | //======================================================================= |
| | | 2 | | // GridCoveredAddressCursor.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 FixedMathSharp; |
| | | 10 | | using GridForge.Configuration; |
| | | 11 | | using GridForge.Spatial; |
| | | 12 | | using SwiftCollections.Utility; |
| | | 13 | | |
| | | 14 | | namespace GridForge.Grids.Topology; |
| | | 15 | | |
| | | 16 | | /// <summary>Describes the state of a resumable covered-address query.</summary> |
| | | 17 | | public enum GridCoveredAddressCursorStatus : byte |
| | | 18 | | { |
| | | 19 | | /// <summary>The bound revision or an eligible grid generation changed; discard the run.</summary> |
| | | 20 | | Stale = 0, |
| | | 21 | | |
| | | 22 | | /// <summary>More generation-input, lookup, address, or output work is required.</summary> |
| | | 23 | | More = 1, |
| | | 24 | | |
| | | 25 | | /// <summary>Every eligible covered topology address was examined.</summary> |
| | | 26 | | Complete = 2 |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary>Stores caller-owned bounded progress for a value-only covered-address query.</summary> |
| | | 30 | | /// <remarks> |
| | | 31 | | /// Construct with the maximum eligible generation count, begin through <see cref="GridWorld"/>, then |
| | | 32 | | /// supply the next canonical generation-input slice to each advance call until it is consumed. Once all |
| | | 33 | | /// declared generations are bound, pass an empty input span. The cursor retains no live grid or voxel. |
| | | 34 | | /// </remarks> |
| | | 35 | | public sealed class GridCoveredAddressCursor |
| | | 36 | | { |
| | | 37 | | private const long LogicalStateBytes = 512L; |
| | | 38 | | private const long ArrayHeaderBytes = 24L; |
| | | 39 | | private const long BoundGenerationBytes = 160L; |
| | | 40 | | |
| | | 41 | | internal readonly struct BoundGeneration |
| | | 42 | | { |
| | | 43 | | internal BoundGeneration( |
| | | 44 | | GridCoveredAddressGeneration generation, |
| | | 45 | | VoxelIndex minimum, |
| | | 46 | | VoxelIndex maximum, |
| | | 47 | | bool hasRange) |
| | | 48 | | { |
| | 50 | 49 | | Generation = generation; |
| | 50 | 50 | | Minimum = minimum; |
| | 50 | 51 | | Maximum = maximum; |
| | 50 | 52 | | HasRange = hasRange; |
| | 50 | 53 | | } |
| | | 54 | | |
| | | 55 | | internal GridCoveredAddressGeneration Generation { get; } |
| | | 56 | | internal VoxelIndex Minimum { get; } |
| | | 57 | | internal VoxelIndex Maximum { get; } |
| | | 58 | | internal bool HasRange { get; } |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | internal readonly BoundGeneration[] Generations; |
| | | 62 | | internal long WorldSpawnToken; |
| | | 63 | | internal uint WorldVersion; |
| | | 64 | | internal ulong WorldChangeSequence; |
| | | 65 | | internal Vector3d QueryMinimum; |
| | | 66 | | internal Vector3d QueryMaximum; |
| | | 67 | | internal GridConfigurationKey FilterConfigurationKey; |
| | | 68 | | internal GridCoveredAddressGeneration LastBoundGeneration; |
| | | 69 | | internal GridCoveredAddress PendingOutput; |
| | | 70 | | internal VoxelIndex CurrentAddress; |
| | | 71 | | internal int ExpectedGenerationCount; |
| | | 72 | | internal int BoundGenerationCount; |
| | | 73 | | internal int RangeGenerationCount; |
| | | 74 | | internal int GenerationOrdinal; |
| | | 75 | | internal bool HasConfigurationFilter; |
| | | 76 | | internal bool HasLastBoundGeneration; |
| | | 77 | | internal bool HasPendingOutput; |
| | | 78 | | internal bool HasCurrentAddress; |
| | | 79 | | internal GridCoveredAddressCursorStatus CurrentStatus; |
| | | 80 | | |
| | | 81 | | /// <summary>Initializes reusable storage for at most <paramref name="generationCapacity"/> eligible grids.</summary |
| | 24 | 82 | | public GridCoveredAddressCursor(int generationCapacity) |
| | | 83 | | { |
| | 24 | 84 | | SwiftThrowHelper.ThrowIfNegative(generationCapacity, nameof(generationCapacity)); |
| | 24 | 85 | | Generations = generationCapacity == 0 |
| | 24 | 86 | | ? Array.Empty<BoundGeneration>() |
| | 24 | 87 | | : new BoundGeneration[generationCapacity]; |
| | 24 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary>The maximum eligible generation count accepted by this cursor.</summary> |
| | 1 | 91 | | public int GenerationCapacity => Generations.Length; |
| | | 92 | | |
| | | 93 | | /// <summary>The deterministic logical bytes retained by this cursor and its owned generation storage.</summary> |
| | | 94 | | /// <remarks>Shared empty-array storage is not charged.</remarks> |
| | 4 | 95 | | public long RetainedBytes => checked( |
| | 4 | 96 | | LogicalStateBytes |
| | 4 | 97 | | + (Generations.Length == 0 |
| | 4 | 98 | | ? 0L |
| | 4 | 99 | | : ArrayHeaderBytes + ((long)Generations.Length * BoundGenerationBytes))); |
| | | 100 | | |
| | | 101 | | /// <summary>The current query state.</summary> |
| | 3 | 102 | | public GridCoveredAddressCursorStatus Status => CurrentStatus; |
| | | 103 | | |
| | | 104 | | /// <summary>The cumulative number of generation validation and spatial lookup probes.</summary> |
| | | 105 | | public ulong LookupProbeOrdinal { get; internal set; } |
| | | 106 | | |
| | | 107 | | /// <summary>The cumulative number of topology-address probes.</summary> |
| | | 108 | | public ulong AddressProbeOrdinal { get; internal set; } |
| | | 109 | | |
| | | 110 | | /// <summary>The cumulative number of covered addresses emitted.</summary> |
| | | 111 | | public ulong OutputOrdinal { get; internal set; } |
| | | 112 | | |
| | | 113 | | /// <summary>The exact committed world revision bound by this cursor.</summary> |
| | | 114 | | public GridCoveredAddressRunStamp RunStamp => |
| | 19 | 115 | | CurrentStatus == GridCoveredAddressCursorStatus.Stale |
| | 19 | 116 | | ? default |
| | 19 | 117 | | : new GridCoveredAddressRunStamp( |
| | 19 | 118 | | WorldSpawnToken, |
| | 19 | 119 | | WorldVersion, |
| | 19 | 120 | | WorldChangeSequence); |
| | | 121 | | |
| | | 122 | | internal bool Begin( |
| | | 123 | | long worldSpawnToken, |
| | | 124 | | uint worldVersion, |
| | | 125 | | ulong worldChangeSequence, |
| | | 126 | | Vector3d queryMinimum, |
| | | 127 | | Vector3d queryMaximum, |
| | | 128 | | int expectedGenerationCount, |
| | | 129 | | bool hasConfigurationFilter, |
| | | 130 | | GridConfigurationKey filterConfigurationKey) |
| | | 131 | | { |
| | 28 | 132 | | if (expectedGenerationCount < 0 || expectedGenerationCount > Generations.Length) |
| | | 133 | | { |
| | 2 | 134 | | MarkStale(); |
| | 2 | 135 | | return false; |
| | | 136 | | } |
| | | 137 | | |
| | 26 | 138 | | WorldSpawnToken = worldSpawnToken; |
| | 26 | 139 | | WorldVersion = worldVersion; |
| | 26 | 140 | | WorldChangeSequence = worldChangeSequence; |
| | 26 | 141 | | QueryMinimum = queryMinimum; |
| | 26 | 142 | | QueryMaximum = queryMaximum; |
| | 26 | 143 | | ExpectedGenerationCount = expectedGenerationCount; |
| | 26 | 144 | | BoundGenerationCount = 0; |
| | 26 | 145 | | RangeGenerationCount = 0; |
| | 26 | 146 | | GenerationOrdinal = 0; |
| | 26 | 147 | | HasConfigurationFilter = hasConfigurationFilter; |
| | 26 | 148 | | FilterConfigurationKey = filterConfigurationKey; |
| | 26 | 149 | | LastBoundGeneration = default; |
| | 26 | 150 | | PendingOutput = default; |
| | 26 | 151 | | CurrentAddress = default; |
| | 26 | 152 | | HasPendingOutput = false; |
| | 26 | 153 | | HasCurrentAddress = false; |
| | 26 | 154 | | HasLastBoundGeneration = false; |
| | 26 | 155 | | LookupProbeOrdinal = 0; |
| | 26 | 156 | | AddressProbeOrdinal = 0; |
| | 26 | 157 | | OutputOrdinal = 0; |
| | 26 | 158 | | CurrentStatus = expectedGenerationCount == 0 |
| | 26 | 159 | | ? GridCoveredAddressCursorStatus.Complete |
| | 26 | 160 | | : GridCoveredAddressCursorStatus.More; |
| | 26 | 161 | | return true; |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | internal GridCoveredAddressCursorStatus MarkStale() |
| | | 165 | | { |
| | 9 | 166 | | WorldSpawnToken = 0; |
| | 9 | 167 | | WorldVersion = 0; |
| | 9 | 168 | | WorldChangeSequence = 0; |
| | 9 | 169 | | QueryMinimum = default; |
| | 9 | 170 | | QueryMaximum = default; |
| | 9 | 171 | | FilterConfigurationKey = default; |
| | 9 | 172 | | LastBoundGeneration = default; |
| | 9 | 173 | | PendingOutput = default; |
| | 9 | 174 | | CurrentAddress = default; |
| | 9 | 175 | | ExpectedGenerationCount = 0; |
| | 9 | 176 | | BoundGenerationCount = 0; |
| | 9 | 177 | | RangeGenerationCount = 0; |
| | 9 | 178 | | GenerationOrdinal = 0; |
| | 9 | 179 | | HasConfigurationFilter = false; |
| | 9 | 180 | | HasLastBoundGeneration = false; |
| | 9 | 181 | | HasPendingOutput = false; |
| | 9 | 182 | | HasCurrentAddress = false; |
| | 9 | 183 | | LookupProbeOrdinal = 0; |
| | 9 | 184 | | AddressProbeOrdinal = 0; |
| | 9 | 185 | | OutputOrdinal = 0; |
| | 9 | 186 | | CurrentStatus = GridCoveredAddressCursorStatus.Stale; |
| | 9 | 187 | | return CurrentStatus; |
| | | 188 | | } |
| | | 189 | | } |