| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationSurfaceComponentIndex.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>Maps exact stable surface addresses to immutable weak components.</summary> |
| | | 11 | | internal sealed class NavigationSurfaceComponentIndex |
| | | 12 | | { |
| | | 13 | | private const long BaseRetainedBytes = 112L; |
| | | 14 | | |
| | | 15 | | private readonly NavigationMediumSlots< |
| | | 16 | | PersistentStringMap<PersistentVoxelIndexMap<NavigationSurfaceComponentKey>>> |
| | | 17 | | _membership; |
| | | 18 | | private readonly NavigationMediumSlots< |
| | | 19 | | PersistentStringMap<PersistentVoxelIndexMap<NavigationSurfaceComponent>>> |
| | | 20 | | _components; |
| | | 21 | | private readonly long _membershipMapBytes; |
| | | 22 | | private readonly long _componentMapBytes; |
| | | 23 | | private readonly long _componentValueBytes; |
| | | 24 | | private readonly int _membershipMapPages; |
| | | 25 | | private readonly int _componentMapPages; |
| | | 26 | | private readonly int _componentValuePages; |
| | | 27 | | |
| | 72152 | 28 | | private NavigationSurfaceComponentIndex( |
| | 72152 | 29 | | NavigationMediumSlots< |
| | 72152 | 30 | | PersistentStringMap<PersistentVoxelIndexMap<NavigationSurfaceComponentKey>>> |
| | 72152 | 31 | | membership, |
| | 72152 | 32 | | NavigationMediumSlots< |
| | 72152 | 33 | | PersistentStringMap<PersistentVoxelIndexMap<NavigationSurfaceComponent>>> |
| | 72152 | 34 | | components, |
| | 72152 | 35 | | long membershipMapBytes, |
| | 72152 | 36 | | long componentMapBytes, |
| | 72152 | 37 | | long componentValueBytes, |
| | 72152 | 38 | | int membershipMapPages, |
| | 72152 | 39 | | int componentMapPages, |
| | 72152 | 40 | | int componentValuePages) |
| | | 41 | | { |
| | 72152 | 42 | | _membership = membership; |
| | 72152 | 43 | | _components = components; |
| | 72152 | 44 | | _membershipMapBytes = membershipMapBytes; |
| | 72152 | 45 | | _componentMapBytes = componentMapBytes; |
| | 72152 | 46 | | _componentValueBytes = componentValueBytes; |
| | 72152 | 47 | | _membershipMapPages = membershipMapPages; |
| | 72152 | 48 | | _componentMapPages = componentMapPages; |
| | 72152 | 49 | | _componentValuePages = componentValuePages; |
| | 72152 | 50 | | } |
| | | 51 | | |
| | | 52 | | internal static NavigationSurfaceComponentIndex Empty { get; } = new( |
| | | 53 | | CreateEmptyRoots<NavigationSurfaceComponentKey>(), |
| | | 54 | | CreateEmptyRoots<NavigationSurfaceComponent>(), |
| | | 55 | | 0, |
| | | 56 | | 0, |
| | | 57 | | 0, |
| | | 58 | | 0, |
| | | 59 | | 0, |
| | | 60 | | 0); |
| | | 61 | | |
| | 3795 | 62 | | internal long RetainedBytes => checked( |
| | 3795 | 63 | | BaseRetainedBytes |
| | 3795 | 64 | | + GetRootBytes(_membership) |
| | 3795 | 65 | | + GetRootBytes(_components) |
| | 3795 | 66 | | + _membershipMapBytes |
| | 3795 | 67 | | + _componentMapBytes |
| | 3795 | 68 | | + _componentValueBytes); |
| | | 69 | | |
| | 3794 | 70 | | internal int PersistentPageCount => checked( |
| | 3794 | 71 | | GetRootObjectCount(_membership) |
| | 3794 | 72 | | + GetRootObjectCount(_components) |
| | 3794 | 73 | | + GetRootNodeCount(_membership) |
| | 3794 | 74 | | + GetRootNodeCount(_components) |
| | 3794 | 75 | | + _membershipMapPages |
| | 3794 | 76 | | + _componentMapPages |
| | 3794 | 77 | | + _componentValuePages); |
| | | 78 | | |
| | 17591 | 79 | | internal int PersistentMapNodeCount => checked( |
| | 17591 | 80 | | GetRootNodeCount(_membership) |
| | 17591 | 81 | | + GetRootNodeCount(_components) |
| | 17591 | 82 | | + _membershipMapPages |
| | 17591 | 83 | | + _componentMapPages); |
| | | 84 | | |
| | | 85 | | internal bool TryGet( |
| | | 86 | | NavigationCellAddress address, |
| | | 87 | | TraversalMedium medium, |
| | | 88 | | out NavigationSurfaceComponent component) |
| | | 89 | | { |
| | 3375870 | 90 | | if (!NavigationCell.IsKnownMedium(medium)) |
| | | 91 | | { |
| | 1 | 92 | | component = null!; |
| | 1 | 93 | | return false; |
| | | 94 | | } |
| | 3375869 | 95 | | _membership.TryGet(medium, out var membership); |
| | 3375869 | 96 | | if (membership.TryGetValue( |
| | 3375869 | 97 | | address.MapId, |
| | 3375869 | 98 | | out PersistentVoxelIndexMap<NavigationSurfaceComponentKey> membershipMap) |
| | 3375869 | 99 | | && membershipMap.TryGetValue(address.Index, out NavigationSurfaceComponentKey key) |
| | 3375869 | 100 | | && TryGet(key, out component)) |
| | | 101 | | { |
| | 3213191 | 102 | | return true; |
| | | 103 | | } |
| | 162678 | 104 | | component = null!; |
| | 162678 | 105 | | return false; |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | internal bool TryGet( |
| | | 109 | | NavigationSurfaceComponentKey key, |
| | | 110 | | out NavigationSurfaceComponent component) |
| | | 111 | | { |
| | 3249362 | 112 | | _components.TryGet(key.Medium, out var components); |
| | 3249362 | 113 | | if (components.TryGetValue( |
| | 3249362 | 114 | | key.Representative.MapId, |
| | 3249362 | 115 | | out PersistentVoxelIndexMap<NavigationSurfaceComponent> componentMap) |
| | 3249362 | 116 | | && componentMap.TryGetValue(key.Representative.Index, out component!)) |
| | | 117 | | { |
| | 3249337 | 118 | | return true; |
| | | 119 | | } |
| | 25 | 120 | | component = null!; |
| | 25 | 121 | | return false; |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | internal NavigationSurfaceComponentIndex AddComponentRecord( |
| | | 125 | | NavigationSurfaceComponent component, |
| | | 126 | | out int copiedPersistentNodes) |
| | | 127 | | { |
| | 1964 | 128 | | copiedPersistentNodes = 0; |
| | 1964 | 129 | | NavigationSurfaceComponentKey key = component.Key; |
| | 1964 | 130 | | _components.TryGet(key.Medium, out var components); |
| | 1964 | 131 | | bool hadComponentMap = components.TryGetValue( |
| | 1964 | 132 | | key.Representative.MapId, |
| | 1964 | 133 | | out PersistentVoxelIndexMap<NavigationSurfaceComponent> componentMap); |
| | 1964 | 134 | | componentMap ??= PersistentVoxelIndexMap<NavigationSurfaceComponent>.Empty; |
| | 1964 | 135 | | long componentMapBytes = _componentMapBytes |
| | 1964 | 136 | | - (hadComponentMap ? componentMap.RetainedBytes : 0L); |
| | 1964 | 137 | | int componentMapPages = _componentMapPages |
| | 1964 | 138 | | - (hadComponentMap ? componentMap.PersistentNodeCount : 0); |
| | 1964 | 139 | | bool replaced = componentMap.TryGetValue( |
| | 1964 | 140 | | key.Representative.Index, |
| | 1964 | 141 | | out NavigationSurfaceComponent prior); |
| | 1964 | 142 | | componentMap = componentMap.Set( |
| | 1964 | 143 | | key.Representative.Index, |
| | 1964 | 144 | | component, |
| | 1964 | 145 | | out int copied); |
| | 1964 | 146 | | copiedPersistentNodes = checked(copiedPersistentNodes + copied); |
| | 1964 | 147 | | componentMapBytes = checked(componentMapBytes + componentMap.RetainedBytes); |
| | 1964 | 148 | | componentMapPages = checked(componentMapPages + componentMap.PersistentNodeCount); |
| | 1964 | 149 | | components = components.Set(key.Representative.MapId, componentMap, out copied); |
| | 1964 | 150 | | copiedPersistentNodes = checked(copiedPersistentNodes + copied); |
| | | 151 | | |
| | 1964 | 152 | | return new NavigationSurfaceComponentIndex( |
| | 1964 | 153 | | _membership, |
| | 1964 | 154 | | _components.Set(key.Medium, components), |
| | 1964 | 155 | | _membershipMapBytes, |
| | 1964 | 156 | | componentMapBytes, |
| | 1964 | 157 | | checked(_componentValueBytes |
| | 1964 | 158 | | - (replaced ? prior.RetainedBytes : 0L) |
| | 1964 | 159 | | + component.RetainedBytes), |
| | 1964 | 160 | | _membershipMapPages, |
| | 1964 | 161 | | componentMapPages, |
| | 1964 | 162 | | checked(_componentValuePages |
| | 1964 | 163 | | - (replaced ? prior.PersistentPageCount : 0) |
| | 1964 | 164 | | + component.PersistentPageCount)); |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | internal NavigationSurfaceComponentIndex AddMembership( |
| | | 168 | | NavigationCellAddress address, |
| | | 169 | | NavigationSurfaceComponentKey key, |
| | | 170 | | out int copiedPersistentNodes) |
| | | 171 | | { |
| | 52971 | 172 | | copiedPersistentNodes = 0; |
| | 52971 | 173 | | _membership.TryGet(key.Medium, out var membership); |
| | 52971 | 174 | | bool hadMembershipMap = membership.TryGetValue( |
| | 52971 | 175 | | address.MapId, |
| | 52971 | 176 | | out PersistentVoxelIndexMap<NavigationSurfaceComponentKey> membershipMap); |
| | 52971 | 177 | | membershipMap ??= PersistentVoxelIndexMap<NavigationSurfaceComponentKey>.Empty; |
| | 52971 | 178 | | long membershipMapBytes = _membershipMapBytes |
| | 52971 | 179 | | - (hadMembershipMap ? membershipMap.RetainedBytes : 0L); |
| | 52971 | 180 | | int membershipMapPages = _membershipMapPages |
| | 52971 | 181 | | - (hadMembershipMap ? membershipMap.PersistentNodeCount : 0); |
| | 52971 | 182 | | membershipMap = membershipMap.Set(address.Index, key, out int copied); |
| | 52971 | 183 | | copiedPersistentNodes = checked(copiedPersistentNodes + copied); |
| | 52971 | 184 | | membershipMapBytes = checked(membershipMapBytes + membershipMap.RetainedBytes); |
| | 52971 | 185 | | membershipMapPages = checked(membershipMapPages + membershipMap.PersistentNodeCount); |
| | 52971 | 186 | | membership = membership.Set(address.MapId, membershipMap, out copied); |
| | 52971 | 187 | | copiedPersistentNodes = checked(copiedPersistentNodes + copied); |
| | 52971 | 188 | | return new NavigationSurfaceComponentIndex( |
| | 52971 | 189 | | _membership.Set(key.Medium, membership), |
| | 52971 | 190 | | _components, |
| | 52971 | 191 | | membershipMapBytes, |
| | 52971 | 192 | | _componentMapBytes, |
| | 52971 | 193 | | _componentValueBytes, |
| | 52971 | 194 | | membershipMapPages, |
| | 52971 | 195 | | _componentMapPages, |
| | 52971 | 196 | | _componentValuePages); |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | internal NavigationSurfaceComponentIndex RemoveComponentRecord( |
| | | 200 | | NavigationSurfaceComponent component, |
| | | 201 | | out int copiedPersistentNodes) |
| | | 202 | | { |
| | 204 | 203 | | copiedPersistentNodes = 0; |
| | 204 | 204 | | NavigationSurfaceComponentKey key = component.Key; |
| | 204 | 205 | | _components.TryGet(key.Medium, out var components); |
| | 204 | 206 | | if (!components.TryGetValue( |
| | 204 | 207 | | key.Representative.MapId, |
| | 204 | 208 | | out PersistentVoxelIndexMap<NavigationSurfaceComponent> componentMap) |
| | 204 | 209 | | || !componentMap.TryGetValue( |
| | 204 | 210 | | key.Representative.Index, |
| | 204 | 211 | | out NavigationSurfaceComponent existing)) |
| | | 212 | | { |
| | 1 | 213 | | return this; |
| | | 214 | | } |
| | 203 | 215 | | long componentMapBytes = _componentMapBytes - componentMap.RetainedBytes; |
| | 203 | 216 | | int componentMapPages = _componentMapPages - componentMap.PersistentNodeCount; |
| | 203 | 217 | | componentMap = componentMap.Remove(key.Representative.Index, out bool removed, out int copied); |
| | 203 | 218 | | copiedPersistentNodes = checked(copiedPersistentNodes + copied); |
| | | 219 | | System.Diagnostics.Debug.Assert(removed); |
| | 203 | 220 | | if (componentMap.Count == 0) |
| | | 221 | | { |
| | 154 | 222 | | components = components.Remove(key.Representative.MapId, out _, out copied); |
| | | 223 | | } |
| | | 224 | | else |
| | | 225 | | { |
| | 49 | 226 | | components = components.Set(key.Representative.MapId, componentMap, out copied); |
| | 49 | 227 | | componentMapBytes = checked(componentMapBytes + componentMap.RetainedBytes); |
| | 49 | 228 | | componentMapPages = checked(componentMapPages + componentMap.PersistentNodeCount); |
| | | 229 | | } |
| | 203 | 230 | | copiedPersistentNodes = checked(copiedPersistentNodes + copied); |
| | 203 | 231 | | return new NavigationSurfaceComponentIndex( |
| | 203 | 232 | | _membership, |
| | 203 | 233 | | _components.Set(key.Medium, components), |
| | 203 | 234 | | _membershipMapBytes, |
| | 203 | 235 | | componentMapBytes, |
| | 203 | 236 | | checked(_componentValueBytes - existing.RetainedBytes), |
| | 203 | 237 | | _membershipMapPages, |
| | 203 | 238 | | componentMapPages, |
| | 203 | 239 | | checked(_componentValuePages - existing.PersistentPageCount)); |
| | | 240 | | } |
| | | 241 | | |
| | | 242 | | internal NavigationSurfaceComponentIndex RemoveMembership( |
| | | 243 | | NavigationCellAddress address, |
| | | 244 | | NavigationSurfaceComponentKey expectedKey, |
| | | 245 | | out int copiedPersistentNodes) |
| | | 246 | | { |
| | 17015 | 247 | | copiedPersistentNodes = 0; |
| | 17015 | 248 | | _membership.TryGet(expectedKey.Medium, out var membership); |
| | 17015 | 249 | | if (!membership.TryGetValue( |
| | 17015 | 250 | | address.MapId, |
| | 17015 | 251 | | out PersistentVoxelIndexMap<NavigationSurfaceComponentKey> membershipMap) |
| | 17015 | 252 | | || !membershipMap.TryGetValue(address.Index, out NavigationSurfaceComponentKey key) |
| | 17015 | 253 | | || !key.Equals(expectedKey)) |
| | | 254 | | { |
| | 2 | 255 | | return this; |
| | | 256 | | } |
| | 17013 | 257 | | long membershipMapBytes = _membershipMapBytes - membershipMap.RetainedBytes; |
| | 17013 | 258 | | int membershipMapPages = _membershipMapPages - membershipMap.PersistentNodeCount; |
| | 17013 | 259 | | membershipMap = membershipMap.Remove(address.Index, out _, out int copied); |
| | 17013 | 260 | | copiedPersistentNodes = checked(copiedPersistentNodes + copied); |
| | 17013 | 261 | | if (membershipMap.Count == 0) |
| | | 262 | | { |
| | 196 | 263 | | membership = membership.Remove(address.MapId, out _, out copied); |
| | | 264 | | } |
| | | 265 | | else |
| | | 266 | | { |
| | 16817 | 267 | | membership = membership.Set(address.MapId, membershipMap, out copied); |
| | 16817 | 268 | | membershipMapBytes = checked(membershipMapBytes + membershipMap.RetainedBytes); |
| | 16817 | 269 | | membershipMapPages = checked(membershipMapPages + membershipMap.PersistentNodeCount); |
| | | 270 | | } |
| | 17013 | 271 | | copiedPersistentNodes = checked(copiedPersistentNodes + copied); |
| | 17013 | 272 | | return new NavigationSurfaceComponentIndex( |
| | 17013 | 273 | | _membership.Set(expectedKey.Medium, membership), |
| | 17013 | 274 | | _components, |
| | 17013 | 275 | | membershipMapBytes, |
| | 17013 | 276 | | _componentMapBytes, |
| | 17013 | 277 | | _componentValueBytes, |
| | 17013 | 278 | | membershipMapPages, |
| | 17013 | 279 | | _componentMapPages, |
| | 17013 | 280 | | _componentValuePages); |
| | | 281 | | } |
| | | 282 | | |
| | | 283 | | private static NavigationMediumSlots<PersistentStringMap<PersistentVoxelIndexMap<T>>> |
| | | 284 | | CreateEmptyRoots<T>() |
| | | 285 | | { |
| | 2 | 286 | | var roots = default( |
| | 2 | 287 | | NavigationMediumSlots<PersistentStringMap<PersistentVoxelIndexMap<T>>>); |
| | 2 | 288 | | for (TraversalMedium medium = TraversalMedium.Solid; |
| | 8 | 289 | | medium <= TraversalMedium.Liquid; |
| | 6 | 290 | | medium++) |
| | | 291 | | { |
| | 6 | 292 | | roots = roots.Set( |
| | 6 | 293 | | medium, |
| | 6 | 294 | | PersistentStringMap<PersistentVoxelIndexMap<T>>.Empty); |
| | | 295 | | } |
| | 2 | 296 | | return roots; |
| | | 297 | | } |
| | | 298 | | |
| | | 299 | | private static long GetRootBytes<T>( |
| | | 300 | | NavigationMediumSlots<PersistentStringMap<PersistentVoxelIndexMap<T>>> roots) |
| | | 301 | | { |
| | 7590 | 302 | | GetRoots(roots, out var solid, out var gas, out var liquid); |
| | 7590 | 303 | | return checked( |
| | 7590 | 304 | | solid.RetainedBytes |
| | 7590 | 305 | | + (ReferenceEquals(gas, solid) ? 0L : gas.RetainedBytes) |
| | 7590 | 306 | | + (ReferenceEquals(liquid, solid) || ReferenceEquals(liquid, gas) |
| | 7590 | 307 | | ? 0L |
| | 7590 | 308 | | : liquid.RetainedBytes)); |
| | | 309 | | } |
| | | 310 | | |
| | | 311 | | private static int GetRootNodeCount<T>( |
| | | 312 | | NavigationMediumSlots<PersistentStringMap<PersistentVoxelIndexMap<T>>> roots) |
| | | 313 | | { |
| | 42770 | 314 | | int total = 0; |
| | 42770 | 315 | | for (TraversalMedium medium = TraversalMedium.Solid; |
| | 171080 | 316 | | medium <= TraversalMedium.Liquid; |
| | 128310 | 317 | | medium++) |
| | | 318 | | { |
| | 128310 | 319 | | roots.TryGet(medium, out var root); |
| | 128310 | 320 | | total = checked(total + root.PersistentNodeCount); |
| | | 321 | | } |
| | 42770 | 322 | | return total; |
| | | 323 | | } |
| | | 324 | | |
| | | 325 | | private static int GetRootObjectCount<T>( |
| | | 326 | | NavigationMediumSlots<PersistentStringMap<PersistentVoxelIndexMap<T>>> roots) |
| | | 327 | | { |
| | 7588 | 328 | | GetRoots(roots, out var solid, out var gas, out var liquid); |
| | 7588 | 329 | | return 1 |
| | 7588 | 330 | | + (ReferenceEquals(gas, solid) ? 0 : 1) |
| | 7588 | 331 | | + (ReferenceEquals(liquid, solid) || ReferenceEquals(liquid, gas) ? 0 : 1); |
| | | 332 | | } |
| | | 333 | | |
| | | 334 | | private static void GetRoots<T>( |
| | | 335 | | NavigationMediumSlots<PersistentStringMap<PersistentVoxelIndexMap<T>>> roots, |
| | | 336 | | out PersistentStringMap<PersistentVoxelIndexMap<T>> solid, |
| | | 337 | | out PersistentStringMap<PersistentVoxelIndexMap<T>> gas, |
| | | 338 | | out PersistentStringMap<PersistentVoxelIndexMap<T>> liquid) |
| | | 339 | | { |
| | 15178 | 340 | | roots.TryGet(TraversalMedium.Solid, out solid!); |
| | 15178 | 341 | | roots.TryGet(TraversalMedium.Gas, out gas!); |
| | 15178 | 342 | | roots.TryGet(TraversalMedium.Liquid, out liquid!); |
| | 15178 | 343 | | } |
| | | 344 | | } |