| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationConnectionOwnerKeySet.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 explicit-connection owner keys.</summary> |
| | | 11 | | internal sealed class NavigationConnectionOwnerKeySet |
| | | 12 | | { |
| | | 13 | | private readonly PersistentStringMap<PersistentStringMap<bool>> _maps; |
| | | 14 | | private readonly long _innerBytes; |
| | | 15 | | private readonly int _innerPages; |
| | | 16 | | |
| | 4388 | 17 | | private NavigationConnectionOwnerKeySet( |
| | 4388 | 18 | | PersistentStringMap<PersistentStringMap<bool>> maps, |
| | 4388 | 19 | | int count, |
| | 4388 | 20 | | long innerBytes, |
| | 4388 | 21 | | int innerPages) |
| | | 22 | | { |
| | 4388 | 23 | | _maps = maps; |
| | 4388 | 24 | | Count = count; |
| | 4388 | 25 | | _innerBytes = innerBytes; |
| | 4388 | 26 | | _innerPages = innerPages; |
| | 4388 | 27 | | } |
| | | 28 | | |
| | | 29 | | internal static NavigationConnectionOwnerKeySet Empty { get; } = new( |
| | | 30 | | PersistentStringMap<PersistentStringMap<bool>>.Empty, |
| | | 31 | | 0, |
| | | 32 | | 0, |
| | | 33 | | 0); |
| | | 34 | | |
| | | 35 | | internal int Count { get; } |
| | | 36 | | |
| | 21935 | 37 | | internal long RetainedBytes => checked(40L + _maps.RetainedBytes + _innerBytes); |
| | | 38 | | |
| | 21935 | 39 | | internal int PersistentPageCount => checked( |
| | 21935 | 40 | | 1 + _maps.PersistentNodeCount + _innerPages); |
| | | 41 | | |
| | | 42 | | internal NavigationConnectionOwnerKeySet Add(NavigationConnectionOwnerKey owner) |
| | | 43 | | { |
| | 4395 | 44 | | bool hadMap = _maps.TryGetValue(owner.MapId, out PersistentStringMap<bool> values); |
| | 4395 | 45 | | values ??= PersistentStringMap<bool>.Empty; |
| | 4395 | 46 | | if (values.ContainsKey(owner.ConnectionId)) |
| | 8 | 47 | | return this; |
| | 4387 | 48 | | long innerBytes = _innerBytes - (hadMap ? values.RetainedBytes : 0L); |
| | 4387 | 49 | | int innerPages = _innerPages - (hadMap ? values.PersistentNodeCount : 0); |
| | 4387 | 50 | | values = values.Set(owner.ConnectionId, true); |
| | 4387 | 51 | | return new NavigationConnectionOwnerKeySet( |
| | 4387 | 52 | | _maps.Set(owner.MapId, values), |
| | 4387 | 53 | | checked(Count + 1), |
| | 4387 | 54 | | checked(innerBytes + values.RetainedBytes), |
| | 4387 | 55 | | checked(innerPages + values.PersistentNodeCount)); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | internal NavigationConnectionOwnerKey GetAt(int ordinal) |
| | | 59 | | { |
| | 17162 | 60 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 17162 | 61 | | (uint)ordinal >= (uint)Count, |
| | 17162 | 62 | | ordinal, |
| | 17162 | 63 | | nameof(ordinal)); |
| | 17162 | 64 | | int remaining = ordinal; |
| | 17524 | 65 | | for (int map = 0; ; map++) |
| | | 66 | | { |
| | 17524 | 67 | | PersistentStringMap<bool> values = _maps.GetValueAt(map); |
| | 17524 | 68 | | if (remaining < values.Count) |
| | | 69 | | { |
| | 17162 | 70 | | return new NavigationConnectionOwnerKey( |
| | 17162 | 71 | | _maps.GetKeyAt(map), |
| | 17162 | 72 | | values.GetKeyAt(remaining)); |
| | | 73 | | } |
| | 362 | 74 | | remaining -= values.Count; |
| | | 75 | | } |
| | | 76 | | } |
| | | 77 | | } |