| | | 1 | | //======================================================================= |
| | | 2 | | // ContactGroupKey.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2026-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | namespace Gravitas.CollisionHandling; |
| | | 9 | | |
| | | 10 | | /// <summary>Collision-free local provenance for one admitted surface region.</summary> |
| | | 11 | | internal readonly struct ContactGroupKey |
| | | 12 | | { |
| | | 13 | | internal ContactGroupKey(int namespaceA, int namespaceB, int surfaceA = 0, |
| | | 14 | | int surfaceB = 0, int region = 0, int sourceIdA = 0, int sourceIdB = 0, |
| | | 15 | | int sourceOrderA = 0, int sourceOrderB = 0) |
| | | 16 | | { |
| | 376368 | 17 | | NamespaceA = namespaceA; |
| | 376368 | 18 | | NamespaceB = namespaceB; |
| | 376368 | 19 | | SurfaceA = surfaceA; |
| | 376368 | 20 | | SurfaceB = surfaceB; |
| | 376368 | 21 | | Region = region; |
| | 376368 | 22 | | _orderedSourceA = ((ulong)(uint)sourceOrderA << 32) | (uint)sourceIdA; |
| | 376368 | 23 | | _orderedSourceB = ((ulong)(uint)sourceOrderB << 32) | (uint)sourceIdB; |
| | 376368 | 24 | | } |
| | | 25 | | |
| | | 26 | | internal readonly int NamespaceA, NamespaceB, SurfaceA, SurfaceB, Region; |
| | | 27 | | // Compound namespaces identify leaves. Supplier registry IDs plus one are |
| | | 28 | | // normalized to replay ordinals only when hashing; zero means no supplier. |
| | 278824 | 29 | | internal int SourceIdA => (int)(uint)_orderedSourceA; |
| | 276300 | 30 | | internal int SourceIdB => (int)(uint)_orderedSourceB; |
| | | 31 | | // Joined suppliers sort by live logical registration, independently of recycled |
| | | 32 | | // registry slots. Keep the actual ID as the low-word uniqueness/lookup key; |
| | | 33 | | // ordinary groups retain their existing order and two source comparisons. |
| | | 34 | | private readonly ulong _orderedSourceA, _orderedSourceB; |
| | | 35 | | |
| | | 36 | | internal int CompareTo(in ContactGroupKey other) |
| | | 37 | | { |
| | 2554719 | 38 | | int order = NamespaceA.CompareTo(other.NamespaceA); |
| | 2558489 | 39 | | if (order != 0) return order; |
| | 2550949 | 40 | | order = NamespaceB.CompareTo(other.NamespaceB); |
| | 2553131 | 41 | | if (order != 0) return order; |
| | 2548767 | 42 | | order = SurfaceA.CompareTo(other.SurfaceA); |
| | 2577373 | 43 | | if (order != 0) return order; |
| | 2520161 | 44 | | order = SurfaceB.CompareTo(other.SurfaceB); |
| | 2728573 | 45 | | if (order != 0) return order; |
| | 2311749 | 46 | | order = Region.CompareTo(other.Region); |
| | 2353379 | 47 | | if (order != 0) return order; |
| | 2270119 | 48 | | order = _orderedSourceA.CompareTo(other._orderedSourceA); |
| | 2270119 | 49 | | return order != 0 ? order : _orderedSourceB.CompareTo(other._orderedSourceB); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | internal ContactGroupKey Remap(int namespaceA, int namespaceB, bool reverse) => |
| | 254612 | 53 | | new(namespaceA, namespaceB, reverse ? SurfaceB : SurfaceA, |
| | 254612 | 54 | | reverse ? SurfaceA : SurfaceB, Region, |
| | 254612 | 55 | | reverse ? SourceIdB : SourceIdA, reverse ? SourceIdA : SourceIdB, |
| | 254612 | 56 | | (int)((reverse ? _orderedSourceB : _orderedSourceA) >> 32), |
| | 254612 | 57 | | (int)((reverse ? _orderedSourceA : _orderedSourceB) >> 32)); |
| | | 58 | | } |