| | | 1 | | //======================================================================= |
| | | 2 | | // IslandGraphUtility.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 | | using System.Collections.Generic; |
| | | 9 | | using SwiftCollections; |
| | | 10 | | |
| | | 11 | | namespace Gravitas; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Builds and resolves collision island membership in deterministic body-key order. |
| | | 15 | | /// </summary> |
| | | 16 | | internal static class IslandGraphUtility |
| | | 17 | | { |
| | | 18 | | public static void SortAndDeduplicate<TNode>(SwiftList<TNode> nodes, IComparer<TNode> comparer) |
| | | 19 | | where TNode : struct, IIslandNodeState |
| | | 20 | | { |
| | 4897 | 21 | | if (nodes.Count == 0) |
| | 3 | 22 | | return; |
| | | 23 | | |
| | 4894 | 24 | | if (nodes.Count == 1) |
| | | 25 | | { |
| | 134 | 26 | | TNode singleNode = nodes[0]; |
| | 134 | 27 | | singleNode.ParentIndex = 0; |
| | 134 | 28 | | singleNode.RootKey = singleNode.BodyKey; |
| | 134 | 29 | | nodes[0] = singleNode; |
| | 134 | 30 | | return; |
| | | 31 | | } |
| | | 32 | | |
| | 4760 | 33 | | nodes.SortInPlace(comparer); |
| | | 34 | | |
| | 4760 | 35 | | int writeIndex = 0; |
| | 4760 | 36 | | int previousKey = -1; |
| | 80312 | 37 | | for (int readIndex = 0; readIndex < nodes.Count; readIndex++) |
| | | 38 | | { |
| | 35396 | 39 | | TNode node = nodes[readIndex]; |
| | 35396 | 40 | | if (node.BodyKey == previousKey) |
| | | 41 | | continue; |
| | | 42 | | |
| | 17209 | 43 | | node.ParentIndex = writeIndex; |
| | 17209 | 44 | | node.RootKey = node.BodyKey; |
| | 17209 | 45 | | nodes[writeIndex++] = node; |
| | 17209 | 46 | | previousKey = node.BodyKey; |
| | | 47 | | } |
| | | 48 | | |
| | 22947 | 49 | | while (nodes.Count > writeIndex) |
| | 18187 | 50 | | nodes.RemoveAt(nodes.Count - 1); |
| | 4760 | 51 | | } |
| | | 52 | | |
| | | 53 | | public static int Find<TNode>(SwiftList<TNode> nodes, int key) |
| | | 54 | | where TNode : struct, IIslandNodeState |
| | | 55 | | { |
| | 71056 | 56 | | int low = 0; |
| | 71056 | 57 | | int high = nodes.Count - 1; |
| | 154888 | 58 | | while (low <= high) |
| | | 59 | | { |
| | 154887 | 60 | | int mid = low + ((high - low) >> 1); |
| | 154887 | 61 | | int midKey = nodes[mid].BodyKey; |
| | 154887 | 62 | | if (midKey == key) |
| | 71055 | 63 | | return mid; |
| | | 64 | | |
| | 83832 | 65 | | if (midKey < key) |
| | 48835 | 66 | | low = mid + 1; |
| | | 67 | | else |
| | 34997 | 68 | | high = mid - 1; |
| | | 69 | | } |
| | | 70 | | |
| | 1 | 71 | | return -1; |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | public static void Union<TNode>(SwiftList<TNode> nodes, int nodeA, int nodeB) |
| | | 75 | | where TNode : struct, IIslandNodeState |
| | | 76 | | { |
| | 12137 | 77 | | int rootA = FindRoot(nodes, nodeA); |
| | 12137 | 78 | | int rootB = FindRoot(nodes, nodeB); |
| | 12137 | 79 | | if (rootA == rootB) |
| | 249 | 80 | | return; |
| | | 81 | | |
| | 11888 | 82 | | int keyA = nodes[rootA].BodyKey; |
| | 11888 | 83 | | int keyB = nodes[rootB].BodyKey; |
| | 11888 | 84 | | int parent = keyA <= keyB ? rootA : rootB; |
| | 11888 | 85 | | int child = parent == rootA ? rootB : rootA; |
| | | 86 | | |
| | 11888 | 87 | | TNode childNode = nodes[child]; |
| | 11888 | 88 | | childNode.ParentIndex = parent; |
| | 11888 | 89 | | childNode.RootKey = nodes[parent].BodyKey; |
| | 11888 | 90 | | nodes[child] = childNode; |
| | 11888 | 91 | | } |
| | | 92 | | |
| | | 93 | | public static int FindRoot<TNode>(SwiftList<TNode> nodes, int index) |
| | | 94 | | where TNode : struct, IIslandNodeState |
| | | 95 | | { |
| | 41617 | 96 | | int root = index; |
| | 59548 | 97 | | while (nodes[root].ParentIndex != root) |
| | 17931 | 98 | | root = nodes[root].ParentIndex; |
| | | 99 | | |
| | 59548 | 100 | | while (index != root) |
| | | 101 | | { |
| | 17931 | 102 | | TNode node = nodes[index]; |
| | 17931 | 103 | | int parent = node.ParentIndex; |
| | 17931 | 104 | | node.ParentIndex = root; |
| | 17931 | 105 | | node.RootKey = nodes[root].BodyKey; |
| | 17931 | 106 | | nodes[index] = node; |
| | 17931 | 107 | | index = parent; |
| | | 108 | | } |
| | | 109 | | |
| | 41617 | 110 | | return root; |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | public static void CompressRoots<TNode>(SwiftList<TNode> nodes) |
| | | 114 | | where TNode : struct, IIslandNodeState |
| | | 115 | | { |
| | 44474 | 116 | | for (int i = 0; i < nodes.Count; i++) |
| | | 117 | | { |
| | 17343 | 118 | | int root = FindRoot(nodes, i); |
| | 17343 | 119 | | TNode node = nodes[i]; |
| | 17343 | 120 | | node.RootKey = nodes[root].BodyKey; |
| | 17343 | 121 | | nodes[i] = node; |
| | | 122 | | } |
| | 4894 | 123 | | } |
| | | 124 | | |
| | | 125 | | public static int ResolveConstraintRootKey<TNode>(SwiftList<TNode> nodes, int nodeA, int nodeB) |
| | | 126 | | where TNode : struct, IIslandNodeState |
| | | 127 | | { |
| | 23377 | 128 | | if (nodeA >= 0) |
| | 15142 | 129 | | return nodes[nodeA].RootKey; |
| | | 130 | | |
| | 8235 | 131 | | return nodeB >= 0 ? nodes[nodeB].RootKey : -1; |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | public static bool WakeBodies<TNode>(SwiftList<TNode> nodes, int rootKey) |
| | | 135 | | where TNode : struct, IIslandNodeState |
| | | 136 | | { |
| | 5439 | 137 | | bool hasAwakeBody = false; |
| | 29828 | 138 | | for (int i = 0; i < nodes.Count; i++) |
| | | 139 | | { |
| | 14881 | 140 | | TNode node = nodes[i]; |
| | 14881 | 141 | | if (node.RootKey == rootKey && node.IsAwakeForCollision) |
| | | 142 | | { |
| | 5406 | 143 | | hasAwakeBody = true; |
| | 5406 | 144 | | break; |
| | | 145 | | } |
| | | 146 | | } |
| | | 147 | | |
| | 5439 | 148 | | if (!hasAwakeBody) |
| | 33 | 149 | | return false; |
| | | 150 | | |
| | 83052 | 151 | | for (int i = 0; i < nodes.Count; i++) |
| | | 152 | | { |
| | 36120 | 153 | | TNode node = nodes[i]; |
| | 36120 | 154 | | if (node.RootKey == rootKey) |
| | 17292 | 155 | | node.WakeFromCollision(); |
| | | 156 | | } |
| | | 157 | | |
| | 5406 | 158 | | return true; |
| | | 159 | | } |
| | | 160 | | } |