| | | 1 | | //======================================================================= |
| | | 2 | | // PathQueryBatch.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 | | namespace Trailblazer.Pathing; |
| | | 9 | | |
| | | 10 | | /// <summary>Pairs one query with its caller-owned stable operation ordinal.</summary> |
| | | 11 | | internal readonly struct PathQueryBatchItem |
| | | 12 | | { |
| | | 13 | | internal const long LogicalRetainedBytes = 248L; |
| | | 14 | | |
| | | 15 | | internal PathQueryBatchItem(long stableOrdinal, PathQuery query) |
| | | 16 | | { |
| | | 17 | | StableOrdinal = stableOrdinal; |
| | | 18 | | Query = query; |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | internal long StableOrdinal { get; } |
| | | 22 | | |
| | | 23 | | internal PathQuery Query { get; } |
| | | 24 | | |
| | | 25 | | internal static long GetLogicalRetainedBytes(PathQuery query) |
| | | 26 | | { |
| | | 27 | | long retainedBytes = LogicalRetainedBytes; |
| | | 28 | | retainedBytes = AddStringPayload(retainedBytes, query.Start.MapId); |
| | | 29 | | retainedBytes = AddStringPayload(retainedBytes, query.End.MapId); |
| | | 30 | | return AddStringPayload(retainedBytes, query.AreaPolicy.PolicyId); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | private static long AddStringPayload(long bytes, string? value) => |
| | | 34 | | value == null |
| | | 35 | | ? bytes |
| | | 36 | | : bytes + ((long)value.Length * sizeof(char)); |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary>Describes a caller-owned prefix of immutable path queries.</summary> |
| | | 40 | | internal readonly struct PathQueryBatch |
| | | 41 | | { |
| | | 42 | | internal PathQueryBatch(PathQueryBatchItem[] items, int count) |
| | | 43 | | { |
| | 19 | 44 | | SwiftThrowHelper.ThrowIfNull(items, nameof(items)); |
| | 18 | 45 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 18 | 46 | | count < 0 || count > items.Length, |
| | 18 | 47 | | count, |
| | 18 | 48 | | nameof(count)); |
| | 16 | 49 | | Items = items; |
| | 16 | 50 | | Count = count; |
| | 16 | 51 | | } |
| | | 52 | | |
| | | 53 | | internal PathQueryBatchItem[] Items { get; } |
| | | 54 | | |
| | | 55 | | internal int Count { get; } |
| | | 56 | | |
| | | 57 | | internal long GetLogicalRetainedBytes() |
| | | 58 | | { |
| | 12 | 59 | | long retainedBytes = 0; |
| | 72 | 60 | | for (int i = 0; i < Count; i++) |
| | | 61 | | { |
| | 24 | 62 | | retainedBytes += PathQueryBatchItem.GetLogicalRetainedBytes( |
| | 24 | 63 | | Items[i].Query); |
| | | 64 | | } |
| | 12 | 65 | | return retainedBytes; |
| | | 66 | | } |
| | | 67 | | } |