| | | 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 SwiftCollections; |
| | | 9 | | using System.Collections.Generic; |
| | | 10 | | |
| | | 11 | | namespace Gravitas; |
| | | 12 | | |
| | | 13 | | internal interface IIslandNodeState |
| | | 14 | | { |
| | | 15 | | int BodyKey { get; } |
| | | 16 | | |
| | | 17 | | int ParentIndex { get; set; } |
| | | 18 | | |
| | | 19 | | int RootKey { get; set; } |
| | | 20 | | |
| | | 21 | | bool IsAwakeForCollision { get; } |
| | | 22 | | |
| | | 23 | | void WakeFromCollision(); |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | internal sealed class IslandNodeKeyComparer<TNode> : IComparer<TNode> |
| | | 27 | | where TNode : struct, IIslandNodeState |
| | | 28 | | { |
| | | 29 | | public int Compare(TNode left, TNode right) => |
| | | 30 | | left.BodyKey.CompareTo(right.BodyKey); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | internal static class IslandGraphUtility |
| | | 34 | | { |
| | | 35 | | public static void SortAndDeduplicate<TNode>(SwiftList<TNode> nodes, IComparer<TNode> comparer) |
| | | 36 | | where TNode : struct, IIslandNodeState |
| | | 37 | | { |
| | 1124 | 38 | | if (nodes.Count == 0) |
| | 3 | 39 | | return; |
| | | 40 | | |
| | 1121 | 41 | | if (nodes.Count == 1) |
| | | 42 | | { |
| | 113 | 43 | | TNode singleNode = nodes[0]; |
| | 113 | 44 | | singleNode.ParentIndex = 0; |
| | 113 | 45 | | singleNode.RootKey = singleNode.BodyKey; |
| | 113 | 46 | | nodes[0] = singleNode; |
| | 113 | 47 | | return; |
| | | 48 | | } |
| | | 49 | | |
| | 1008 | 50 | | nodes.SortInPlace(comparer); |
| | | 51 | | |
| | 1008 | 52 | | int writeIndex = 0; |
| | 1008 | 53 | | int previousKey = -1; |
| | 25816 | 54 | | for (int readIndex = 0; readIndex < nodes.Count; readIndex++) |
| | | 55 | | { |
| | 11900 | 56 | | TNode node = nodes[readIndex]; |
| | 11900 | 57 | | if (node.BodyKey == previousKey) |
| | | 58 | | continue; |
| | | 59 | | |
| | 6217 | 60 | | node.ParentIndex = writeIndex; |
| | 6217 | 61 | | node.RootKey = node.BodyKey; |
| | 6217 | 62 | | nodes[writeIndex++] = node; |
| | 6217 | 63 | | previousKey = node.BodyKey; |
| | | 64 | | } |
| | | 65 | | |
| | 6691 | 66 | | while (nodes.Count > writeIndex) |
| | 5683 | 67 | | nodes.RemoveAt(nodes.Count - 1); |
| | 1008 | 68 | | } |
| | | 69 | | |
| | | 70 | | public static int Find<TNode>(SwiftList<TNode> nodes, int key) |
| | | 71 | | where TNode : struct, IIslandNodeState |
| | | 72 | | { |
| | 24022 | 73 | | int low = 0; |
| | 24022 | 74 | | int high = nodes.Count - 1; |
| | 60926 | 75 | | while (low <= high) |
| | | 76 | | { |
| | 60925 | 77 | | int mid = low + ((high - low) >> 1); |
| | 60925 | 78 | | int midKey = nodes[mid].BodyKey; |
| | 60925 | 79 | | if (midKey == key) |
| | 24021 | 80 | | return mid; |
| | | 81 | | |
| | 36904 | 82 | | if (midKey < key) |
| | 20181 | 83 | | low = mid + 1; |
| | | 84 | | else |
| | 16723 | 85 | | high = mid - 1; |
| | | 86 | | } |
| | | 87 | | |
| | 1 | 88 | | return -1; |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | public static void Union<TNode>(SwiftList<TNode> nodes, int nodeA, int nodeB) |
| | | 92 | | where TNode : struct, IIslandNodeState |
| | | 93 | | { |
| | 5580 | 94 | | int rootA = FindRoot(nodes, nodeA); |
| | 5580 | 95 | | int rootB = FindRoot(nodes, nodeB); |
| | 5580 | 96 | | if (rootA == rootB) |
| | 731 | 97 | | return; |
| | | 98 | | |
| | 4849 | 99 | | int keyA = nodes[rootA].BodyKey; |
| | 4849 | 100 | | int keyB = nodes[rootB].BodyKey; |
| | 4849 | 101 | | int parent = keyA <= keyB ? rootA : rootB; |
| | 4849 | 102 | | int child = parent == rootA ? rootB : rootA; |
| | | 103 | | |
| | 4849 | 104 | | TNode childNode = nodes[child]; |
| | 4849 | 105 | | childNode.ParentIndex = parent; |
| | 4849 | 106 | | childNode.RootKey = nodes[parent].BodyKey; |
| | 4849 | 107 | | nodes[child] = childNode; |
| | 4849 | 108 | | } |
| | | 109 | | |
| | | 110 | | public static int FindRoot<TNode>(SwiftList<TNode> nodes, int index) |
| | | 111 | | where TNode : struct, IIslandNodeState |
| | | 112 | | { |
| | 17490 | 113 | | int root = index; |
| | 26494 | 114 | | while (nodes[root].ParentIndex != root) |
| | 9004 | 115 | | root = nodes[root].ParentIndex; |
| | | 116 | | |
| | 26494 | 117 | | while (index != root) |
| | | 118 | | { |
| | 9004 | 119 | | TNode node = nodes[index]; |
| | 9004 | 120 | | int parent = node.ParentIndex; |
| | 9004 | 121 | | node.ParentIndex = root; |
| | 9004 | 122 | | node.RootKey = nodes[root].BodyKey; |
| | 9004 | 123 | | nodes[index] = node; |
| | 9004 | 124 | | index = parent; |
| | | 125 | | } |
| | | 126 | | |
| | 17490 | 127 | | return root; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | public static void CompressRoots<TNode>(SwiftList<TNode> nodes) |
| | | 131 | | where TNode : struct, IIslandNodeState |
| | | 132 | | { |
| | 14902 | 133 | | for (int i = 0; i < nodes.Count; i++) |
| | | 134 | | { |
| | 6330 | 135 | | int root = FindRoot(nodes, i); |
| | 6330 | 136 | | TNode node = nodes[i]; |
| | 6330 | 137 | | node.RootKey = nodes[root].BodyKey; |
| | 6330 | 138 | | nodes[i] = node; |
| | | 139 | | } |
| | 1121 | 140 | | } |
| | | 141 | | |
| | | 142 | | public static int ResolveConstraintRootKey<TNode>(SwiftList<TNode> nodes, int nodeA, int nodeB) |
| | | 143 | | where TNode : struct, IIslandNodeState |
| | | 144 | | { |
| | 6415 | 145 | | if (nodeA >= 0) |
| | 5976 | 146 | | return nodes[nodeA].RootKey; |
| | | 147 | | |
| | 439 | 148 | | return nodeB >= 0 ? nodes[nodeB].RootKey : -1; |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | public static bool WakeBodies<TNode>(SwiftList<TNode> nodes, int rootKey) |
| | | 152 | | where TNode : struct, IIslandNodeState |
| | | 153 | | { |
| | 1463 | 154 | | bool hasAwakeBody = false; |
| | 21176 | 155 | | for (int i = 0; i < nodes.Count; i++) |
| | | 156 | | { |
| | 10583 | 157 | | TNode node = nodes[i]; |
| | 10583 | 158 | | if (node.RootKey == rootKey && node.IsAwakeForCollision) |
| | | 159 | | { |
| | 1458 | 160 | | hasAwakeBody = true; |
| | 1458 | 161 | | break; |
| | | 162 | | } |
| | | 163 | | } |
| | | 164 | | |
| | 1463 | 165 | | if (!hasAwakeBody) |
| | 5 | 166 | | return false; |
| | | 167 | | |
| | 51936 | 168 | | for (int i = 0; i < nodes.Count; i++) |
| | | 169 | | { |
| | 24510 | 170 | | TNode node = nodes[i]; |
| | 24510 | 171 | | if (node.RootKey == rootKey) |
| | 6305 | 172 | | node.WakeFromCollision(); |
| | | 173 | | } |
| | | 174 | | |
| | 1458 | 175 | | return true; |
| | | 176 | | } |
| | | 177 | | } |