| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationCellAddressSet.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>Stores a persistent canonical set of exact navigation-cell addresses.</summary> |
| | | 11 | | internal sealed class NavigationCellAddressSet |
| | | 12 | | { |
| | | 13 | | private readonly PersistentStringMap<PersistentVoxelIndexMap<byte>> _maps; |
| | | 14 | | private readonly long _innerBytes; |
| | | 15 | | private readonly int _innerPages; |
| | | 16 | | |
| | 40821 | 17 | | private NavigationCellAddressSet( |
| | 40821 | 18 | | PersistentStringMap<PersistentVoxelIndexMap<byte>> maps, |
| | 40821 | 19 | | int count, |
| | 40821 | 20 | | long innerBytes, |
| | 40821 | 21 | | int innerPages) |
| | | 22 | | { |
| | 40821 | 23 | | _maps = maps; |
| | 40821 | 24 | | Count = count; |
| | 40821 | 25 | | _innerBytes = innerBytes; |
| | 40821 | 26 | | _innerPages = innerPages; |
| | 40821 | 27 | | } |
| | | 28 | | |
| | | 29 | | internal static NavigationCellAddressSet Empty { get; } = new( |
| | | 30 | | PersistentStringMap<PersistentVoxelIndexMap<byte>>.Empty, |
| | | 31 | | 0, |
| | | 32 | | 0, |
| | | 33 | | 0); |
| | | 34 | | |
| | | 35 | | internal int Count { get; } |
| | | 36 | | |
| | 395759 | 37 | | internal long RetainedBytes => checked(40L + _maps.RetainedBytes + _innerBytes); |
| | | 38 | | |
| | 396127 | 39 | | internal int PersistentPageCount => checked( |
| | 396127 | 40 | | 1 + _maps.PersistentNodeCount + _innerPages); |
| | | 41 | | |
| | | 42 | | internal bool Contains(NavigationCellAddress address) => |
| | 18631 | 43 | | _maps.TryGetValue(address.MapId, out PersistentVoxelIndexMap<byte> values) |
| | 18631 | 44 | | && values.TryGetValue(address.Index, out _); |
| | | 45 | | |
| | | 46 | | internal NavigationCellAddressSet Add(NavigationCellAddress address) |
| | | 47 | | { |
| | 45659 | 48 | | bool hadMap = _maps.TryGetValue( |
| | 45659 | 49 | | address.MapId, |
| | 45659 | 50 | | out PersistentVoxelIndexMap<byte> values); |
| | 45659 | 51 | | values ??= PersistentVoxelIndexMap<byte>.Empty; |
| | 45659 | 52 | | if (values.TryGetValue(address.Index, out _)) |
| | 4839 | 53 | | return this; |
| | 40820 | 54 | | long innerBytes = _innerBytes - (hadMap ? values.RetainedBytes : 0L); |
| | 40820 | 55 | | int innerPages = _innerPages - (hadMap ? values.PersistentNodeCount : 0); |
| | 40820 | 56 | | values = values.Set(address.Index, 1); |
| | 40820 | 57 | | return new NavigationCellAddressSet( |
| | 40820 | 58 | | _maps.Set(address.MapId, values), |
| | 40820 | 59 | | checked(Count + 1), |
| | 40820 | 60 | | checked(innerBytes + values.RetainedBytes), |
| | 40820 | 61 | | checked(innerPages + values.PersistentNodeCount)); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | internal NavigationCellAddress GetAt(int ordinal) |
| | | 65 | | { |
| | 76282 | 66 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 76282 | 67 | | (uint)ordinal >= (uint)Count, |
| | 76282 | 68 | | ordinal, |
| | 76282 | 69 | | nameof(ordinal)); |
| | 76282 | 70 | | int remaining = ordinal; |
| | 78261 | 71 | | for (int map = 0; ; map++) |
| | | 72 | | { |
| | 78261 | 73 | | PersistentVoxelIndexMap<byte> values = _maps.GetValueAt(map); |
| | 78261 | 74 | | if (remaining < values.Count) |
| | | 75 | | { |
| | 76282 | 76 | | return new NavigationCellAddress( |
| | 76282 | 77 | | _maps.GetKeyAt(map), |
| | 76282 | 78 | | values.GetKeyAt(remaining)); |
| | | 79 | | } |
| | 1979 | 80 | | remaining -= values.Count; |
| | | 81 | | } |
| | | 82 | | } |
| | | 83 | | } |