| | | 1 | | //======================================================================= |
| | | 2 | | // PersistentVoxelIndexMap.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 | | |
| | | 11 | | namespace Trailblazer.Pathing; |
| | | 12 | | |
| | | 13 | | /// <summary>Stores topology-local address values in an immutable balanced tree.</summary> |
| | | 14 | | internal sealed class PersistentVoxelIndexMap<T> |
| | | 15 | | { |
| | | 16 | | private readonly Node? _root; |
| | | 17 | | |
| | | 18 | | internal static PersistentVoxelIndexMap<T> Empty { get; } = new(null); |
| | | 19 | | |
| | 360956 | 20 | | private PersistentVoxelIndexMap(Node? root) => _root = root; |
| | | 21 | | |
| | 10485137 | 22 | | internal int Count => CountOf(_root); |
| | | 23 | | |
| | 306570 | 24 | | internal long RetainedBytes => checked(32L + ((long)Count * 64L)); |
| | | 25 | | |
| | 307947 | 26 | | internal int PersistentNodeCount => Count; |
| | | 27 | | |
| | | 28 | | internal bool TryGetValue(VoxelIndex key, out T value) |
| | | 29 | | { |
| | 10583696 | 30 | | Node? node = _root; |
| | 69159591 | 31 | | while (node != null) |
| | | 32 | | { |
| | 68581338 | 33 | | int comparison = key.CompareTo(node.Key); |
| | 68581338 | 34 | | if (comparison == 0) |
| | | 35 | | { |
| | 10005443 | 36 | | value = node.Value; |
| | 10005443 | 37 | | return true; |
| | | 38 | | } |
| | 58575895 | 39 | | node = comparison < 0 ? node.Left : node.Right; |
| | | 40 | | } |
| | 578253 | 41 | | value = default!; |
| | 578253 | 42 | | return false; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | internal PersistentVoxelIndexMap<T> Set(VoxelIndex key, T value) => |
| | 48212 | 46 | | new(Set(_root, key, value)); |
| | | 47 | | |
| | | 48 | | internal PersistentVoxelIndexMap<T> Set(VoxelIndex key, T value, out int copiedNodeCount) |
| | | 49 | | { |
| | 114855 | 50 | | copiedNodeCount = 0; |
| | 114855 | 51 | | return new PersistentVoxelIndexMap<T>(Set(_root, key, value, ref copiedNodeCount)); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | internal PersistentVoxelIndexMap<T> Remove(VoxelIndex key, out bool removed) |
| | | 55 | | { |
| | 57 | 56 | | Node? root = Remove(_root, key, out removed); |
| | 57 | 57 | | return removed ? new PersistentVoxelIndexMap<T>(root) : this; |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | internal PersistentVoxelIndexMap<T> Remove( |
| | | 61 | | VoxelIndex key, |
| | | 62 | | out bool removed, |
| | | 63 | | out int copiedNodeCount) |
| | | 64 | | { |
| | 17350 | 65 | | copiedNodeCount = 0; |
| | 17350 | 66 | | Node? root = Remove(_root, key, out removed, ref copiedNodeCount); |
| | 17350 | 67 | | return removed ? new PersistentVoxelIndexMap<T>(root) : this; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | internal T GetValueAt(int ordinal) |
| | | 71 | | { |
| | 2557307 | 72 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 2557307 | 73 | | (uint)ordinal >= (uint)Count, |
| | 2557307 | 74 | | ordinal, |
| | 2557307 | 75 | | nameof(ordinal)); |
| | 2557307 | 76 | | Node node = _root!; |
| | 12359041 | 77 | | while (true) |
| | | 78 | | { |
| | 27261486 | 79 | | int leftCount = CountOf(node.Left); |
| | 27261486 | 80 | | if (ordinal < leftCount) |
| | 12345138 | 81 | | node = node.Left!; |
| | 14916348 | 82 | | else if (ordinal == leftCount) |
| | 2557307 | 83 | | return node.Value; |
| | | 84 | | else |
| | | 85 | | { |
| | 12359041 | 86 | | ordinal -= leftCount + 1; |
| | 12359041 | 87 | | node = node.Right!; |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | internal VoxelIndex GetKeyAt(int ordinal) |
| | | 93 | | { |
| | 2264670 | 94 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 2264670 | 95 | | (uint)ordinal >= (uint)Count, |
| | 2264670 | 96 | | ordinal, |
| | 2264670 | 97 | | nameof(ordinal)); |
| | 2264670 | 98 | | Node node = _root!; |
| | 11189980 | 99 | | while (true) |
| | | 100 | | { |
| | 24640069 | 101 | | int leftCount = CountOf(node.Left); |
| | 24640069 | 102 | | if (ordinal < leftCount) |
| | 11185419 | 103 | | node = node.Left!; |
| | 13454650 | 104 | | else if (ordinal == leftCount) |
| | 2264670 | 105 | | return node.Key; |
| | | 106 | | else |
| | | 107 | | { |
| | 11189980 | 108 | | ordinal -= leftCount + 1; |
| | 11189980 | 109 | | node = node.Right!; |
| | | 110 | | } |
| | | 111 | | } |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | private static Node Set(Node? node, VoxelIndex key, T value) |
| | | 115 | | { |
| | 402081 | 116 | | if (node == null) |
| | 48146 | 117 | | return new Node(key, value, null, null); |
| | 353935 | 118 | | int comparison = key.CompareTo(node.Key); |
| | 353935 | 119 | | if (comparison == 0) |
| | 66 | 120 | | return new Node(key, value, node.Left, node.Right); |
| | 353869 | 121 | | return comparison < 0 |
| | 353869 | 122 | | ? Balance(new Node(node.Key, node.Value, Set(node.Left, key, value), node.Right)) |
| | 353869 | 123 | | : Balance(new Node(node.Key, node.Value, node.Left, Set(node.Right, key, value))); |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | private static Node Set( |
| | | 127 | | Node? node, |
| | | 128 | | VoxelIndex key, |
| | | 129 | | T value, |
| | | 130 | | ref int copiedNodeCount) |
| | | 131 | | { |
| | 1191308 | 132 | | if (node == null) |
| | 114624 | 133 | | return NewNode(key, value, null, null, ref copiedNodeCount); |
| | 1076684 | 134 | | int comparison = key.CompareTo(node.Key); |
| | 1076684 | 135 | | if (comparison == 0) |
| | 231 | 136 | | return NewNode(key, value, node.Left, node.Right, ref copiedNodeCount); |
| | 1076453 | 137 | | return comparison < 0 |
| | 1076453 | 138 | | ? Balance( |
| | 1076453 | 139 | | NewNode( |
| | 1076453 | 140 | | node.Key, |
| | 1076453 | 141 | | node.Value, |
| | 1076453 | 142 | | Set(node.Left, key, value, ref copiedNodeCount), |
| | 1076453 | 143 | | node.Right, |
| | 1076453 | 144 | | ref copiedNodeCount), |
| | 1076453 | 145 | | ref copiedNodeCount) |
| | 1076453 | 146 | | : Balance( |
| | 1076453 | 147 | | NewNode( |
| | 1076453 | 148 | | node.Key, |
| | 1076453 | 149 | | node.Value, |
| | 1076453 | 150 | | node.Left, |
| | 1076453 | 151 | | Set(node.Right, key, value, ref copiedNodeCount), |
| | 1076453 | 152 | | ref copiedNodeCount), |
| | 1076453 | 153 | | ref copiedNodeCount); |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | private static Node? Remove(Node? node, VoxelIndex key, out bool removed) |
| | | 157 | | { |
| | 106 | 158 | | if (node == null) |
| | | 159 | | { |
| | 2 | 160 | | removed = false; |
| | 2 | 161 | | return null; |
| | | 162 | | } |
| | 104 | 163 | | int comparison = key.CompareTo(node.Key); |
| | 104 | 164 | | if (comparison < 0) |
| | | 165 | | { |
| | 43 | 166 | | Node? left = Remove(node.Left, key, out removed); |
| | 43 | 167 | | return removed ? Balance(new Node(node.Key, node.Value, left, node.Right)) : node; |
| | | 168 | | } |
| | 61 | 169 | | if (comparison > 0) |
| | | 170 | | { |
| | 6 | 171 | | Node? right = Remove(node.Right, key, out removed); |
| | 6 | 172 | | return removed ? Balance(new Node(node.Key, node.Value, node.Left, right)) : node; |
| | | 173 | | } |
| | | 174 | | |
| | 55 | 175 | | removed = true; |
| | 55 | 176 | | if (node.Left == null) |
| | 51 | 177 | | return node.Right; |
| | 4 | 178 | | if (node.Right == null) |
| | 2 | 179 | | return node.Left; |
| | 2 | 180 | | Node successor = FindMinimum(node.Right); |
| | 2 | 181 | | Node? successorRight = RemoveMinimum(node.Right); |
| | 2 | 182 | | return Balance(new Node(successor.Key, successor.Value, node.Left, successorRight)); |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | private static Node? Remove( |
| | | 186 | | Node? node, |
| | | 187 | | VoxelIndex key, |
| | | 188 | | out bool removed, |
| | | 189 | | ref int copiedNodeCount) |
| | | 190 | | { |
| | 141458 | 191 | | if (node == null) |
| | | 192 | | { |
| | 2 | 193 | | removed = false; |
| | 2 | 194 | | return null; |
| | | 195 | | } |
| | 141456 | 196 | | int comparison = key.CompareTo(node.Key); |
| | 141456 | 197 | | if (comparison < 0) |
| | | 198 | | { |
| | 124002 | 199 | | Node? left = Remove(node.Left, key, out removed, ref copiedNodeCount); |
| | 124002 | 200 | | return removed |
| | 124002 | 201 | | ? Balance( |
| | 124002 | 202 | | NewNode(node.Key, node.Value, left, node.Right, ref copiedNodeCount), |
| | 124002 | 203 | | ref copiedNodeCount) |
| | 124002 | 204 | | : node; |
| | | 205 | | } |
| | 17454 | 206 | | if (comparison > 0) |
| | | 207 | | { |
| | 106 | 208 | | Node? right = Remove(node.Right, key, out removed, ref copiedNodeCount); |
| | 106 | 209 | | return removed |
| | 106 | 210 | | ? Balance( |
| | 106 | 211 | | NewNode(node.Key, node.Value, node.Left, right, ref copiedNodeCount), |
| | 106 | 212 | | ref copiedNodeCount) |
| | 106 | 213 | | : node; |
| | | 214 | | } |
| | | 215 | | |
| | 17348 | 216 | | removed = true; |
| | 17348 | 217 | | if (node.Left == null) |
| | 17331 | 218 | | return node.Right; |
| | 17 | 219 | | if (node.Right == null) |
| | 15 | 220 | | return node.Left; |
| | 2 | 221 | | Node successor = FindMinimum(node.Right); |
| | 2 | 222 | | Node? successorRight = RemoveMinimum(node.Right, ref copiedNodeCount); |
| | 2 | 223 | | return Balance( |
| | 2 | 224 | | NewNode(successor.Key, successor.Value, node.Left, successorRight, ref copiedNodeCount), |
| | 2 | 225 | | ref copiedNodeCount); |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | private static Node FindMinimum(Node node) |
| | | 229 | | { |
| | 6 | 230 | | while (node.Left != null) |
| | 2 | 231 | | node = node.Left; |
| | 4 | 232 | | return node; |
| | | 233 | | } |
| | | 234 | | |
| | 3 | 235 | | private static Node? RemoveMinimum(Node node) => node.Left == null |
| | 3 | 236 | | ? node.Right |
| | 3 | 237 | | : Balance(new Node(node.Key, node.Value, RemoveMinimum(node.Left), node.Right)); |
| | | 238 | | |
| | 3 | 239 | | private static Node? RemoveMinimum(Node node, ref int copiedNodeCount) => node.Left == null |
| | 3 | 240 | | ? node.Right |
| | 3 | 241 | | : Balance( |
| | 3 | 242 | | NewNode( |
| | 3 | 243 | | node.Key, |
| | 3 | 244 | | node.Value, |
| | 3 | 245 | | RemoveMinimum(node.Left, ref copiedNodeCount), |
| | 3 | 246 | | node.Right, |
| | 3 | 247 | | ref copiedNodeCount), |
| | 3 | 248 | | ref copiedNodeCount); |
| | | 249 | | |
| | | 250 | | private static Node Balance(Node node) |
| | | 251 | | { |
| | 353917 | 252 | | int balance = HeightOf(node.Left) - HeightOf(node.Right); |
| | 353917 | 253 | | if (balance > 1) |
| | | 254 | | { |
| | 362 | 255 | | if (HeightOf(node.Left!.Left) < HeightOf(node.Left.Right)) |
| | 361 | 256 | | node = new Node(node.Key, node.Value, RotateLeft(node.Left), node.Right); |
| | 362 | 257 | | return RotateRight(node); |
| | | 258 | | } |
| | 353555 | 259 | | if (balance < -1) |
| | | 260 | | { |
| | 41252 | 261 | | if (HeightOf(node.Right!.Right) < HeightOf(node.Right.Left)) |
| | 10 | 262 | | node = new Node(node.Key, node.Value, node.Left, RotateRight(node.Right)); |
| | 41252 | 263 | | return RotateLeft(node); |
| | | 264 | | } |
| | 312303 | 265 | | return node; |
| | | 266 | | } |
| | | 267 | | |
| | | 268 | | private static Node Balance(Node node, ref int copiedNodeCount) |
| | | 269 | | { |
| | 1200560 | 270 | | int balance = HeightOf(node.Left) - HeightOf(node.Right); |
| | 1200560 | 271 | | if (balance > 1) |
| | | 272 | | { |
| | 62 | 273 | | if (HeightOf(node.Left!.Left) < HeightOf(node.Left.Right)) |
| | | 274 | | { |
| | 61 | 275 | | node = NewNode( |
| | 61 | 276 | | node.Key, |
| | 61 | 277 | | node.Value, |
| | 61 | 278 | | RotateLeft(node.Left, ref copiedNodeCount), |
| | 61 | 279 | | node.Right, |
| | 61 | 280 | | ref copiedNodeCount); |
| | | 281 | | } |
| | 62 | 282 | | return RotateRight(node, ref copiedNodeCount); |
| | | 283 | | } |
| | 1200498 | 284 | | if (balance < -1) |
| | | 285 | | { |
| | 114703 | 286 | | if (HeightOf(node.Right!.Right) < HeightOf(node.Right.Left)) |
| | | 287 | | { |
| | 185 | 288 | | node = NewNode( |
| | 185 | 289 | | node.Key, |
| | 185 | 290 | | node.Value, |
| | 185 | 291 | | node.Left, |
| | 185 | 292 | | RotateRight(node.Right, ref copiedNodeCount), |
| | 185 | 293 | | ref copiedNodeCount); |
| | | 294 | | } |
| | 114703 | 295 | | return RotateLeft(node, ref copiedNodeCount); |
| | | 296 | | } |
| | 1085795 | 297 | | return node; |
| | | 298 | | } |
| | | 299 | | |
| | | 300 | | private static Node RotateLeft(Node node) |
| | | 301 | | { |
| | 41613 | 302 | | Node right = node.Right!; |
| | 41613 | 303 | | return new Node( |
| | 41613 | 304 | | right.Key, |
| | 41613 | 305 | | right.Value, |
| | 41613 | 306 | | new Node(node.Key, node.Value, node.Left, right.Left), |
| | 41613 | 307 | | right.Right); |
| | | 308 | | } |
| | | 309 | | |
| | | 310 | | private static Node RotateRight(Node node) |
| | | 311 | | { |
| | 372 | 312 | | Node left = node.Left!; |
| | 372 | 313 | | return new Node( |
| | 372 | 314 | | left.Key, |
| | 372 | 315 | | left.Value, |
| | 372 | 316 | | left.Left, |
| | 372 | 317 | | new Node(node.Key, node.Value, left.Right, node.Right)); |
| | | 318 | | } |
| | | 319 | | |
| | | 320 | | private static Node RotateLeft(Node node, ref int copiedNodeCount) |
| | | 321 | | { |
| | 114764 | 322 | | Node right = node.Right!; |
| | 114764 | 323 | | Node left = NewNode( |
| | 114764 | 324 | | node.Key, |
| | 114764 | 325 | | node.Value, |
| | 114764 | 326 | | node.Left, |
| | 114764 | 327 | | right.Left, |
| | 114764 | 328 | | ref copiedNodeCount); |
| | 114764 | 329 | | return NewNode(right.Key, right.Value, left, right.Right, ref copiedNodeCount); |
| | | 330 | | } |
| | | 331 | | |
| | | 332 | | private static Node RotateRight(Node node, ref int copiedNodeCount) |
| | | 333 | | { |
| | 247 | 334 | | Node left = node.Left!; |
| | 247 | 335 | | Node right = NewNode( |
| | 247 | 336 | | node.Key, |
| | 247 | 337 | | node.Value, |
| | 247 | 338 | | left.Right, |
| | 247 | 339 | | node.Right, |
| | 247 | 340 | | ref copiedNodeCount); |
| | 247 | 341 | | return NewNode(left.Key, left.Value, left.Left, right, ref copiedNodeCount); |
| | | 342 | | } |
| | | 343 | | |
| | | 344 | | private static Node NewNode( |
| | | 345 | | VoxelIndex key, |
| | | 346 | | T value, |
| | | 347 | | Node? left, |
| | | 348 | | Node? right, |
| | | 349 | | ref int copiedNodeCount) |
| | | 350 | | { |
| | 1545683 | 351 | | copiedNodeCount = checked(copiedNodeCount + 1); |
| | 1545683 | 352 | | return new Node(key, value, left, right); |
| | | 353 | | } |
| | | 354 | | |
| | 66450998 | 355 | | private static int CountOf(Node? node) => node?.Count ?? 0; |
| | 7486018 | 356 | | private static int HeightOf(Node? node) => node?.Height ?? 0; |
| | | 357 | | |
| | | 358 | | private sealed class Node |
| | | 359 | | { |
| | 2032153 | 360 | | internal Node(VoxelIndex key, T value, Node? left, Node? right) |
| | | 361 | | { |
| | 2032153 | 362 | | Key = key; |
| | 2032153 | 363 | | Value = value; |
| | 2032153 | 364 | | Left = left; |
| | 2032153 | 365 | | Right = right; |
| | 2032153 | 366 | | Height = Math.Max(HeightOf(left), HeightOf(right)) + 1; |
| | 2032153 | 367 | | Count = CountOf(left) + CountOf(right) + 1; |
| | 2032153 | 368 | | } |
| | | 369 | | |
| | | 370 | | internal VoxelIndex Key { get; } |
| | | 371 | | internal T Value { get; } |
| | | 372 | | internal Node? Left { get; } |
| | | 373 | | internal Node? Right { get; } |
| | | 374 | | internal int Height { get; } |
| | | 375 | | internal int Count { get; } |
| | | 376 | | } |
| | | 377 | | } |