| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationExplicitConnectionIndex.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 GridForge.Spatial; |
| | | 10 | | using SwiftCollections; |
| | | 11 | | |
| | | 12 | | namespace Trailblazer.Pathing; |
| | | 13 | | |
| | | 14 | | /// <summary>Stores persistent explicit owners and their exact address incidence.</summary> |
| | | 15 | | internal sealed class NavigationExplicitConnectionIndex |
| | | 16 | | { |
| | 1 | 17 | | internal static readonly NavigationExplicitConnectionIndex Empty = new( |
| | 1 | 18 | | PersistentStringMap<PersistentStringMap<NavigationExplicitConnectionRecord>>.Empty, |
| | 1 | 19 | | PersistentStringMap<PersistentVoxelIndexMap<NavigationPagedSequence<NavigationConnectionOwnerKey>>>.Empty, |
| | 1 | 20 | | PersistentStringMap<PersistentVoxelIndexMap<NavigationPagedSequence<NavigationConnectionOwnerKey>>>.Empty, |
| | 1 | 21 | | ownerMapBytes: 0, |
| | 1 | 22 | | incidentMapBytes: 0, |
| | 1 | 23 | | endpointMapBytes: 0, |
| | 1 | 24 | | recordBytes: 0, |
| | 1 | 25 | | incidentArrayBytes: 0, |
| | 1 | 26 | | endpointArrayBytes: 0, |
| | 1 | 27 | | ownerMapPages: 0, |
| | 1 | 28 | | incidentMapPages: 0, |
| | 1 | 29 | | endpointMapPages: 0, |
| | 1 | 30 | | recordPages: 0, |
| | 1 | 31 | | incidentArrayPages: 0, |
| | 1 | 32 | | endpointArrayPages: 0); |
| | | 33 | | |
| | | 34 | | private readonly PersistentStringMap<PersistentStringMap<NavigationExplicitConnectionRecord>> _owners; |
| | | 35 | | private readonly PersistentStringMap<PersistentVoxelIndexMap<NavigationPagedSequence<NavigationConnectionOwnerKey>>> |
| | | 36 | | private readonly PersistentStringMap<PersistentVoxelIndexMap<NavigationPagedSequence<NavigationConnectionOwnerKey>>> |
| | | 37 | | private readonly long _ownerMapBytes; |
| | | 38 | | private readonly long _incidentMapBytes; |
| | | 39 | | private readonly long _endpointMapBytes; |
| | | 40 | | private readonly long _recordBytes; |
| | | 41 | | private readonly long _incidentArrayBytes; |
| | | 42 | | private readonly long _endpointArrayBytes; |
| | | 43 | | private readonly int _ownerMapPages; |
| | | 44 | | private readonly int _incidentMapPages; |
| | | 45 | | private readonly int _endpointMapPages; |
| | | 46 | | private readonly int _recordPages; |
| | | 47 | | private readonly int _incidentArrayPages; |
| | | 48 | | private readonly int _endpointArrayPages; |
| | | 49 | | |
| | 13726 | 50 | | private NavigationExplicitConnectionIndex( |
| | 13726 | 51 | | PersistentStringMap<PersistentStringMap<NavigationExplicitConnectionRecord>> owners, |
| | 13726 | 52 | | PersistentStringMap<PersistentVoxelIndexMap<NavigationPagedSequence<NavigationConnectionOwnerKey>>> incident, |
| | 13726 | 53 | | PersistentStringMap<PersistentVoxelIndexMap<NavigationPagedSequence<NavigationConnectionOwnerKey>>> endpoints, |
| | 13726 | 54 | | long ownerMapBytes, |
| | 13726 | 55 | | long incidentMapBytes, |
| | 13726 | 56 | | long endpointMapBytes, |
| | 13726 | 57 | | long recordBytes, |
| | 13726 | 58 | | long incidentArrayBytes, |
| | 13726 | 59 | | long endpointArrayBytes, |
| | 13726 | 60 | | int ownerMapPages, |
| | 13726 | 61 | | int incidentMapPages, |
| | 13726 | 62 | | int endpointMapPages, |
| | 13726 | 63 | | int recordPages, |
| | 13726 | 64 | | int incidentArrayPages, |
| | 13726 | 65 | | int endpointArrayPages) |
| | | 66 | | { |
| | 13726 | 67 | | _owners = owners; |
| | 13726 | 68 | | _incident = incident; |
| | 13726 | 69 | | _endpoints = endpoints; |
| | 13726 | 70 | | _ownerMapBytes = ownerMapBytes; |
| | 13726 | 71 | | _incidentMapBytes = incidentMapBytes; |
| | 13726 | 72 | | _endpointMapBytes = endpointMapBytes; |
| | 13726 | 73 | | _recordBytes = recordBytes; |
| | 13726 | 74 | | _incidentArrayBytes = incidentArrayBytes; |
| | 13726 | 75 | | _endpointArrayBytes = endpointArrayBytes; |
| | 13726 | 76 | | _ownerMapPages = ownerMapPages; |
| | 13726 | 77 | | _incidentMapPages = incidentMapPages; |
| | 13726 | 78 | | _endpointMapPages = endpointMapPages; |
| | 13726 | 79 | | _recordPages = recordPages; |
| | 13726 | 80 | | _incidentArrayPages = incidentArrayPages; |
| | 13726 | 81 | | _endpointArrayPages = endpointArrayPages; |
| | 13726 | 82 | | } |
| | | 83 | | |
| | 129662 | 84 | | internal long RetainedBytes => checked( |
| | 129662 | 85 | | 112L |
| | 129662 | 86 | | + _owners.RetainedBytes |
| | 129662 | 87 | | + _incident.RetainedBytes |
| | 129662 | 88 | | + _endpoints.RetainedBytes |
| | 129662 | 89 | | + _ownerMapBytes |
| | 129662 | 90 | | + _incidentMapBytes |
| | 129662 | 91 | | + _endpointMapBytes |
| | 129662 | 92 | | + _recordBytes |
| | 129662 | 93 | | + _incidentArrayBytes |
| | 129662 | 94 | | + _endpointArrayBytes); |
| | | 95 | | |
| | 130383 | 96 | | internal int PersistentPageCount => checked( |
| | 130383 | 97 | | 3 |
| | 130383 | 98 | | + _owners.PersistentNodeCount |
| | 130383 | 99 | | + _incident.PersistentNodeCount |
| | 130383 | 100 | | + _endpoints.PersistentNodeCount |
| | 130383 | 101 | | + _ownerMapPages |
| | 130383 | 102 | | + _incidentMapPages |
| | 130383 | 103 | | + _endpointMapPages |
| | 130383 | 104 | | + _recordPages |
| | 130383 | 105 | | + _incidentArrayPages |
| | 130383 | 106 | | + _endpointArrayPages); |
| | | 107 | | |
| | 125634 | 108 | | internal long PayloadRetainedBytes => checked( |
| | 125634 | 109 | | _recordBytes + _incidentArrayBytes + _endpointArrayBytes); |
| | | 110 | | |
| | 126370 | 111 | | internal int PayloadPersistentPageCount => checked( |
| | 126370 | 112 | | _recordPages + _incidentArrayPages + _endpointArrayPages); |
| | | 113 | | |
| | | 114 | | internal bool TryGet( |
| | | 115 | | NavigationConnectionOwnerKey owner, |
| | | 116 | | out NavigationExplicitConnectionRecord record) |
| | | 117 | | { |
| | 276046 | 118 | | if (_owners.TryGetValue( |
| | 276046 | 119 | | owner.MapId, |
| | 276046 | 120 | | out PersistentStringMap<NavigationExplicitConnectionRecord> map) |
| | 276046 | 121 | | && map.TryGetValue(owner.ConnectionId, out record!)) |
| | | 122 | | { |
| | 241443 | 123 | | return true; |
| | | 124 | | } |
| | 34603 | 125 | | record = null!; |
| | 34603 | 126 | | return false; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | internal int GetSourceOwnerCount(string mapId) => |
| | 1436 | 130 | | _owners.TryGetValue(mapId, out PersistentStringMap<NavigationExplicitConnectionRecord> map) |
| | 1436 | 131 | | ? map.Count |
| | 1436 | 132 | | : 0; |
| | | 133 | | |
| | | 134 | | internal NavigationExplicitConnectionRecord GetSourceOwnerAt(string mapId, int ordinal) |
| | | 135 | | { |
| | 93 | 136 | | _owners.TryGetValue(mapId, out PersistentStringMap<NavigationExplicitConnectionRecord> map); |
| | 93 | 137 | | return map.GetValueAt(ordinal); |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | internal int GetActiveIncidentEdgeCount(string mapId) |
| | | 141 | | { |
| | 14640 | 142 | | int count = 0; |
| | 14640 | 143 | | if (_owners.TryGetValue( |
| | 14640 | 144 | | mapId, |
| | 14640 | 145 | | out PersistentStringMap<NavigationExplicitConnectionRecord> owners)) |
| | | 146 | | { |
| | 836464 | 147 | | for (int i = 0; i < owners.Count; i++) |
| | | 148 | | { |
| | 417808 | 149 | | if (owners.GetValueAt(i).IsActive) |
| | 417807 | 150 | | count = checked(count + 1); |
| | | 151 | | } |
| | | 152 | | } |
| | 14640 | 153 | | if (!_endpoints.TryGetValue( |
| | 14640 | 154 | | mapId, |
| | 14640 | 155 | | out PersistentVoxelIndexMap<NavigationPagedSequence<NavigationConnectionOwnerKey>> addresses)) |
| | | 156 | | { |
| | 14208 | 157 | | return count; |
| | | 158 | | } |
| | 837328 | 159 | | for (int i = 0; i < addresses.Count; i++) |
| | | 160 | | { |
| | 418232 | 161 | | NavigationPagedSequence<NavigationConnectionOwnerKey>.Enumerator endpoints = |
| | 418232 | 162 | | addresses.GetValueAt(i).GetEnumerator(); |
| | 1253848 | 163 | | while (endpoints.MoveNext()) |
| | | 164 | | { |
| | 835616 | 165 | | NavigationConnectionOwnerKey owner = endpoints.Current; |
| | 835616 | 166 | | if (string.Equals(owner.MapId, mapId, StringComparison.Ordinal)) |
| | | 167 | | continue; |
| | 11 | 168 | | bool found = TryGet(owner, out NavigationExplicitConnectionRecord record); |
| | | 169 | | System.Diagnostics.Debug.Assert( |
| | | 170 | | found |
| | | 171 | | && string.Equals( |
| | | 172 | | record.Destination.MapId, |
| | | 173 | | mapId, |
| | | 174 | | StringComparison.Ordinal), |
| | | 175 | | "Published endpoint rows retain their exact owner and destination."); |
| | 11 | 176 | | if (!record.IsActive) |
| | | 177 | | continue; |
| | 10 | 178 | | count = checked(count + 1); |
| | | 179 | | } |
| | | 180 | | } |
| | 432 | 181 | | return count; |
| | | 182 | | } |
| | | 183 | | |
| | | 184 | | internal NavigationPagedSequence<NavigationConnectionOwnerKey>.Enumerator |
| | | 185 | | GetIncidentOwnerEnumerator(NavigationCellAddress address) => |
| | 17238 | 186 | | GetIncidentOwnerRow(address).GetEnumerator(); |
| | | 187 | | |
| | | 188 | | internal NavigationPagedSequence<NavigationConnectionOwnerKey> GetIncidentOwnerRow( |
| | | 189 | | NavigationCellAddress address) |
| | | 190 | | { |
| | 40228 | 191 | | if (_incident.TryGetValue( |
| | 40228 | 192 | | address.MapId, |
| | 40228 | 193 | | out PersistentVoxelIndexMap<NavigationPagedSequence<NavigationConnectionOwnerKey>> map) |
| | 40228 | 194 | | && map.TryGetValue( |
| | 40228 | 195 | | address.Index, |
| | 40228 | 196 | | out NavigationPagedSequence<NavigationConnectionOwnerKey> owners)) |
| | | 197 | | { |
| | 935 | 198 | | return owners; |
| | | 199 | | } |
| | 39293 | 200 | | return NavigationPagedSequence<NavigationConnectionOwnerKey>.Empty; |
| | | 201 | | } |
| | | 202 | | |
| | | 203 | | internal NavigationPagedSequence<NavigationConnectionOwnerKey> GetEndpointOwnerRow( |
| | | 204 | | NavigationCellAddress address) |
| | | 205 | | { |
| | 539008 | 206 | | if (_endpoints.TryGetValue( |
| | 539008 | 207 | | address.MapId, |
| | 539008 | 208 | | out PersistentVoxelIndexMap<NavigationPagedSequence<NavigationConnectionOwnerKey>> map) |
| | 539008 | 209 | | && map.TryGetValue( |
| | 539008 | 210 | | address.Index, |
| | 539008 | 211 | | out NavigationPagedSequence<NavigationConnectionOwnerKey> owners)) |
| | | 212 | | { |
| | 123904 | 213 | | return owners; |
| | | 214 | | } |
| | 415104 | 215 | | return NavigationPagedSequence<NavigationConnectionOwnerKey>.Empty; |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | internal int GetIncidentAddressCount(string mapId) => |
| | 1507 | 219 | | _incident.TryGetValue( |
| | 1507 | 220 | | mapId, |
| | 1507 | 221 | | out PersistentVoxelIndexMap<NavigationPagedSequence<NavigationConnectionOwnerKey>> addresses) |
| | 1507 | 222 | | ? addresses.Count |
| | 1507 | 223 | | : 0; |
| | | 224 | | |
| | | 225 | | internal NavigationCellAddress GetIncidentAddressAt(string mapId, int ordinal) |
| | | 226 | | { |
| | 134 | 227 | | _incident.TryGetValue( |
| | 134 | 228 | | mapId, |
| | 134 | 229 | | out PersistentVoxelIndexMap<NavigationPagedSequence<NavigationConnectionOwnerKey>> addresses); |
| | 134 | 230 | | return new NavigationCellAddress(mapId, addresses.GetKeyAt(ordinal)); |
| | | 231 | | } |
| | | 232 | | |
| | | 233 | | internal NavigationExplicitConnectionIndex SetOwner( |
| | | 234 | | NavigationExplicitConnectionRecord record, |
| | | 235 | | out int copiedNodes) |
| | | 236 | | { |
| | 4557 | 237 | | copiedNodes = 0; |
| | 4557 | 238 | | PersistentStringMap<NavigationExplicitConnectionRecord> ownerMap = |
| | 4557 | 239 | | _owners.TryGetValue( |
| | 4557 | 240 | | record.Owner.MapId, |
| | 4557 | 241 | | out PersistentStringMap<NavigationExplicitConnectionRecord> existing) |
| | 4557 | 242 | | ? existing |
| | 4557 | 243 | | : PersistentStringMap<NavigationExplicitConnectionRecord>.Empty; |
| | 4557 | 244 | | long ownerMapBytes = _ownerMapBytes - (existing?.RetainedBytes ?? 0); |
| | 4557 | 245 | | int ownerMapPages = _ownerMapPages - (existing?.PersistentNodeCount ?? 0); |
| | 4557 | 246 | | ownerMap.TryGetValue(record.Owner.ConnectionId, out NavigationExplicitConnectionRecord prior); |
| | 4557 | 247 | | ownerMap = ownerMap.Set(record.Owner.ConnectionId, record, out int copied); |
| | 4557 | 248 | | copiedNodes = checked(copiedNodes + copied); |
| | 4557 | 249 | | ownerMapBytes = checked(ownerMapBytes + ownerMap.RetainedBytes); |
| | 4557 | 250 | | ownerMapPages = checked(ownerMapPages + ownerMap.PersistentNodeCount); |
| | 4557 | 251 | | PersistentStringMap<PersistentStringMap<NavigationExplicitConnectionRecord>> owners = |
| | 4557 | 252 | | _owners.Set(record.Owner.MapId, ownerMap, out copied); |
| | 4557 | 253 | | copiedNodes = checked(copiedNodes + copied); |
| | 4557 | 254 | | return new NavigationExplicitConnectionIndex( |
| | 4557 | 255 | | owners, |
| | 4557 | 256 | | _incident, |
| | 4557 | 257 | | _endpoints, |
| | 4557 | 258 | | ownerMapBytes, |
| | 4557 | 259 | | _incidentMapBytes, |
| | 4557 | 260 | | _endpointMapBytes, |
| | 4557 | 261 | | checked(_recordBytes - (prior?.RetainedBytes ?? 0) + record.RetainedBytes), |
| | 4557 | 262 | | _incidentArrayBytes, |
| | 4557 | 263 | | _endpointArrayBytes, |
| | 4557 | 264 | | ownerMapPages, |
| | 4557 | 265 | | _incidentMapPages, |
| | 4557 | 266 | | _endpointMapPages, |
| | 4557 | 267 | | checked(_recordPages - (prior?.PersistentPageCount ?? 0) + record.PersistentPageCount), |
| | 4557 | 268 | | _incidentArrayPages, |
| | 4557 | 269 | | _endpointArrayPages); |
| | | 270 | | } |
| | | 271 | | |
| | | 272 | | internal NavigationExplicitConnectionIndex RemoveOwner( |
| | | 273 | | NavigationConnectionOwnerKey owner, |
| | | 274 | | out bool removed, |
| | | 275 | | out int copiedNodes) |
| | | 276 | | { |
| | 18 | 277 | | copiedNodes = 0; |
| | 18 | 278 | | if (!_owners.TryGetValue( |
| | 18 | 279 | | owner.MapId, |
| | 18 | 280 | | out PersistentStringMap<NavigationExplicitConnectionRecord> ownerMap) |
| | 18 | 281 | | || !ownerMap.TryGetValue(owner.ConnectionId, out NavigationExplicitConnectionRecord prior)) |
| | | 282 | | { |
| | 4 | 283 | | removed = false; |
| | 4 | 284 | | return this; |
| | | 285 | | } |
| | 14 | 286 | | removed = true; |
| | 14 | 287 | | long ownerMapBytes = _ownerMapBytes - ownerMap.RetainedBytes; |
| | 14 | 288 | | int ownerMapPages = _ownerMapPages - ownerMap.PersistentNodeCount; |
| | 14 | 289 | | ownerMap = ownerMap.Remove(owner.ConnectionId, out _, out int copied); |
| | 14 | 290 | | copiedNodes = checked(copiedNodes + copied); |
| | | 291 | | PersistentStringMap<PersistentStringMap<NavigationExplicitConnectionRecord>> owners; |
| | 14 | 292 | | if (ownerMap.Count == 0) |
| | 12 | 293 | | owners = _owners.Remove(owner.MapId, out _, out copied); |
| | | 294 | | else |
| | | 295 | | { |
| | 2 | 296 | | ownerMapBytes = checked(ownerMapBytes + ownerMap.RetainedBytes); |
| | 2 | 297 | | ownerMapPages = checked(ownerMapPages + ownerMap.PersistentNodeCount); |
| | 2 | 298 | | owners = _owners.Set(owner.MapId, ownerMap, out copied); |
| | | 299 | | } |
| | 14 | 300 | | copiedNodes = checked(copiedNodes + copied); |
| | 14 | 301 | | return new NavigationExplicitConnectionIndex( |
| | 14 | 302 | | owners, |
| | 14 | 303 | | _incident, |
| | 14 | 304 | | _endpoints, |
| | 14 | 305 | | ownerMapBytes, |
| | 14 | 306 | | _incidentMapBytes, |
| | 14 | 307 | | _endpointMapBytes, |
| | 14 | 308 | | checked(_recordBytes - prior.RetainedBytes), |
| | 14 | 309 | | _incidentArrayBytes, |
| | 14 | 310 | | _endpointArrayBytes, |
| | 14 | 311 | | ownerMapPages, |
| | 14 | 312 | | _incidentMapPages, |
| | 14 | 313 | | _endpointMapPages, |
| | 14 | 314 | | checked(_recordPages - prior.PersistentPageCount), |
| | 14 | 315 | | _incidentArrayPages, |
| | 14 | 316 | | _endpointArrayPages); |
| | | 317 | | } |
| | | 318 | | |
| | | 319 | | internal NavigationExplicitConnectionIndex SetIncidentRow( |
| | | 320 | | NavigationCellAddress address, |
| | | 321 | | NavigationPagedSequence<NavigationConnectionOwnerKey> prior, |
| | | 322 | | NavigationPagedSequence<NavigationConnectionOwnerKey> next, |
| | | 323 | | out int copiedNodes) |
| | | 324 | | { |
| | 4578 | 325 | | copiedNodes = 0; |
| | 4578 | 326 | | bool hadMap = _incident.TryGetValue( |
| | 4578 | 327 | | address.MapId, |
| | 4578 | 328 | | out PersistentVoxelIndexMap<NavigationPagedSequence<NavigationConnectionOwnerKey>> map); |
| | 4578 | 329 | | map ??= PersistentVoxelIndexMap<NavigationPagedSequence<NavigationConnectionOwnerKey>>.Empty; |
| | 4578 | 330 | | long incidentMapBytes = _incidentMapBytes - (hadMap ? map.RetainedBytes : 0); |
| | 4578 | 331 | | int incidentMapPages = _incidentMapPages - (hadMap ? map.PersistentNodeCount : 0); |
| | | 332 | | int copied; |
| | 4578 | 333 | | if (next.Count == 0) |
| | 46 | 334 | | map = map.Remove(address.Index, out _, out copied); |
| | | 335 | | else |
| | 4532 | 336 | | map = map.Set(address.Index, next, out copied); |
| | 4578 | 337 | | copiedNodes = checked(copiedNodes + copied); |
| | | 338 | | PersistentStringMap<PersistentVoxelIndexMap<NavigationPagedSequence<NavigationConnectionOwnerKey>>> incident; |
| | 4578 | 339 | | if (map.Count == 0) |
| | 37 | 340 | | incident = _incident.Remove(address.MapId, out _, out copied); |
| | | 341 | | else |
| | | 342 | | { |
| | 4541 | 343 | | incidentMapBytes = checked(incidentMapBytes + map.RetainedBytes); |
| | 4541 | 344 | | incidentMapPages = checked(incidentMapPages + map.PersistentNodeCount); |
| | 4541 | 345 | | incident = _incident.Set(address.MapId, map, out copied); |
| | | 346 | | } |
| | 4578 | 347 | | copiedNodes = checked(copiedNodes + copied); |
| | 4578 | 348 | | return new NavigationExplicitConnectionIndex( |
| | 4578 | 349 | | _owners, |
| | 4578 | 350 | | incident, |
| | 4578 | 351 | | _endpoints, |
| | 4578 | 352 | | _ownerMapBytes, |
| | 4578 | 353 | | incidentMapBytes, |
| | 4578 | 354 | | _endpointMapBytes, |
| | 4578 | 355 | | _recordBytes, |
| | 4578 | 356 | | checked( |
| | 4578 | 357 | | _incidentArrayBytes |
| | 4578 | 358 | | - prior.RetainedBytes |
| | 4578 | 359 | | + next.RetainedBytes), |
| | 4578 | 360 | | _endpointArrayBytes, |
| | 4578 | 361 | | _ownerMapPages, |
| | 4578 | 362 | | incidentMapPages, |
| | 4578 | 363 | | _endpointMapPages, |
| | 4578 | 364 | | _recordPages, |
| | 4578 | 365 | | checked( |
| | 4578 | 366 | | _incidentArrayPages |
| | 4578 | 367 | | - prior.PersistentPageCount |
| | 4578 | 368 | | + next.PersistentPageCount), |
| | 4578 | 369 | | _endpointArrayPages); |
| | | 370 | | } |
| | | 371 | | |
| | | 372 | | internal NavigationExplicitConnectionIndex SetEndpointRow( |
| | | 373 | | NavigationCellAddress address, |
| | | 374 | | NavigationPagedSequence<NavigationConnectionOwnerKey> prior, |
| | | 375 | | NavigationPagedSequence<NavigationConnectionOwnerKey> next, |
| | | 376 | | out int copiedNodes) |
| | | 377 | | { |
| | 4576 | 378 | | copiedNodes = 0; |
| | 4576 | 379 | | bool hadMap = _endpoints.TryGetValue( |
| | 4576 | 380 | | address.MapId, |
| | 4576 | 381 | | out PersistentVoxelIndexMap<NavigationPagedSequence<NavigationConnectionOwnerKey>> map); |
| | 4576 | 382 | | map ??= PersistentVoxelIndexMap<NavigationPagedSequence<NavigationConnectionOwnerKey>>.Empty; |
| | 4576 | 383 | | long endpointMapBytes = _endpointMapBytes - (hadMap ? map.RetainedBytes : 0); |
| | 4576 | 384 | | int endpointMapPages = _endpointMapPages - (hadMap ? map.PersistentNodeCount : 0); |
| | | 385 | | int copied; |
| | 4576 | 386 | | if (next.Count == 0) |
| | 27 | 387 | | map = map.Remove(address.Index, out _, out copied); |
| | | 388 | | else |
| | 4549 | 389 | | map = map.Set(address.Index, next, out copied); |
| | 4576 | 390 | | copiedNodes = checked(copiedNodes + copied); |
| | | 391 | | PersistentStringMap<PersistentVoxelIndexMap<NavigationPagedSequence<NavigationConnectionOwnerKey>>> endpoints; |
| | 4576 | 392 | | if (map.Count == 0) |
| | 18 | 393 | | endpoints = _endpoints.Remove(address.MapId, out _, out copied); |
| | | 394 | | else |
| | | 395 | | { |
| | 4558 | 396 | | endpointMapBytes = checked(endpointMapBytes + map.RetainedBytes); |
| | 4558 | 397 | | endpointMapPages = checked(endpointMapPages + map.PersistentNodeCount); |
| | 4558 | 398 | | endpoints = _endpoints.Set(address.MapId, map, out copied); |
| | | 399 | | } |
| | 4576 | 400 | | copiedNodes = checked(copiedNodes + copied); |
| | 4576 | 401 | | return new NavigationExplicitConnectionIndex( |
| | 4576 | 402 | | _owners, |
| | 4576 | 403 | | _incident, |
| | 4576 | 404 | | endpoints, |
| | 4576 | 405 | | _ownerMapBytes, |
| | 4576 | 406 | | _incidentMapBytes, |
| | 4576 | 407 | | endpointMapBytes, |
| | 4576 | 408 | | _recordBytes, |
| | 4576 | 409 | | _incidentArrayBytes, |
| | 4576 | 410 | | checked( |
| | 4576 | 411 | | _endpointArrayBytes |
| | 4576 | 412 | | - prior.RetainedBytes |
| | 4576 | 413 | | + next.RetainedBytes), |
| | 4576 | 414 | | _ownerMapPages, |
| | 4576 | 415 | | _incidentMapPages, |
| | 4576 | 416 | | endpointMapPages, |
| | 4576 | 417 | | _recordPages, |
| | 4576 | 418 | | _incidentArrayPages, |
| | 4576 | 419 | | checked( |
| | 4576 | 420 | | _endpointArrayPages |
| | 4576 | 421 | | - prior.PersistentPageCount |
| | 4576 | 422 | | + next.PersistentPageCount)); |
| | | 423 | | } |
| | | 424 | | |
| | | 425 | | private static int CompareOwners( |
| | | 426 | | NavigationConnectionOwnerKey left, |
| | | 427 | | NavigationConnectionOwnerKey right, |
| | | 428 | | PersistentStringMap<PersistentStringMap<NavigationExplicitConnectionRecord>> owners) |
| | | 429 | | { |
| | 37688 | 430 | | if (!TryGetRecord(owners, left, out NavigationExplicitConnectionRecord leftRecord) |
| | 37688 | 431 | | || !TryGetRecord(owners, right, out NavigationExplicitConnectionRecord rightRecord)) |
| | | 432 | | { |
| | 1 | 433 | | return left.CompareTo(right); |
| | | 434 | | } |
| | 37687 | 435 | | int comparison = leftRecord.Source.CompareTo(rightRecord.Source); |
| | 37687 | 436 | | if (comparison != 0) |
| | 37059 | 437 | | return comparison; |
| | 628 | 438 | | comparison = leftRecord.Destination.CompareTo(rightRecord.Destination); |
| | 628 | 439 | | if (comparison != 0) |
| | 10 | 440 | | return comparison; |
| | 618 | 441 | | comparison = string.CompareOrdinal(left.ConnectionId, right.ConnectionId); |
| | 618 | 442 | | return comparison; |
| | | 443 | | } |
| | | 444 | | |
| | | 445 | | internal int CompareOwners( |
| | | 446 | | NavigationConnectionOwnerKey left, |
| | 37688 | 447 | | NavigationConnectionOwnerKey right) => CompareOwners(left, right, _owners); |
| | | 448 | | |
| | | 449 | | private static bool TryGetRecord( |
| | | 450 | | PersistentStringMap<PersistentStringMap<NavigationExplicitConnectionRecord>> owners, |
| | | 451 | | NavigationConnectionOwnerKey owner, |
| | | 452 | | out NavigationExplicitConnectionRecord record) |
| | | 453 | | { |
| | 75375 | 454 | | if (owners.TryGetValue( |
| | 75375 | 455 | | owner.MapId, |
| | 75375 | 456 | | out PersistentStringMap<NavigationExplicitConnectionRecord> map) |
| | 75375 | 457 | | && map.TryGetValue(owner.ConnectionId, out record!)) |
| | | 458 | | { |
| | 75374 | 459 | | return true; |
| | | 460 | | } |
| | 1 | 461 | | record = null!; |
| | 1 | 462 | | return false; |
| | | 463 | | } |
| | | 464 | | |
| | | 465 | | } |