| | | 1 | | //======================================================================= |
| | | 2 | | // PersistentGridConfigurationMap.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.Configuration; |
| | | 10 | | |
| | | 11 | | namespace Trailblazer.Pathing; |
| | | 12 | | |
| | | 13 | | /// <summary>Stores exact normalized grid-address values in an immutable balanced tree.</summary> |
| | | 14 | | internal sealed class PersistentGridConfigurationMap<T> |
| | | 15 | | { |
| | | 16 | | private readonly Node? _root; |
| | | 17 | | |
| | | 18 | | internal static PersistentGridConfigurationMap<T> Empty { get; } = new(null); |
| | | 19 | | |
| | 6896 | 20 | | private PersistentGridConfigurationMap(Node? root) => _root = root; |
| | | 21 | | |
| | 262843 | 22 | | internal int Count => CountOf(_root); |
| | | 23 | | |
| | 130094 | 24 | | internal long RetainedBytes => checked(32L + ((long)Count * 144L)); |
| | | 25 | | |
| | | 26 | | internal bool TryGetValue(GridConfigurationKey key, out T value) |
| | | 27 | | { |
| | 100665 | 28 | | Node? node = _root; |
| | 235228 | 29 | | while (node != null) |
| | | 30 | | { |
| | 231754 | 31 | | int comparison = Compare(key, node.Key); |
| | 231754 | 32 | | if (comparison == 0) |
| | | 33 | | { |
| | 97191 | 34 | | value = node.Value; |
| | 97191 | 35 | | return true; |
| | | 36 | | } |
| | 134563 | 37 | | node = comparison < 0 ? node.Left : node.Right; |
| | | 38 | | } |
| | 3474 | 39 | | value = default!; |
| | 3474 | 40 | | return false; |
| | | 41 | | } |
| | | 42 | | |
| | 973 | 43 | | internal T GetValueAt(int ordinal) => GetNodeAt(ordinal).Value; |
| | | 44 | | |
| | | 45 | | internal PersistentGridConfigurationMap<T> Set(GridConfigurationKey key, T value) => |
| | 689 | 46 | | new(Set(_root, key, value)); |
| | | 47 | | |
| | | 48 | | internal PersistentGridConfigurationMap<T> Set( |
| | | 49 | | GridConfigurationKey key, |
| | | 50 | | T value, |
| | | 51 | | out int copiedNodeCount) |
| | | 52 | | { |
| | 2711 | 53 | | copiedNodeCount = 0; |
| | 2711 | 54 | | return new PersistentGridConfigurationMap<T>( |
| | 2711 | 55 | | Set(_root, key, value, ref copiedNodeCount)); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | internal PersistentGridConfigurationMap<T> Remove(GridConfigurationKey key) => |
| | 7 | 59 | | new(Remove(_root, key, out _)); |
| | | 60 | | |
| | | 61 | | internal PersistentGridConfigurationMap<T> Remove( |
| | | 62 | | GridConfigurationKey key, |
| | | 63 | | out bool removed, |
| | | 64 | | out int copiedNodeCount) |
| | | 65 | | { |
| | 42 | 66 | | copiedNodeCount = 0; |
| | 42 | 67 | | Node? root = Remove(_root, key, out removed, ref copiedNodeCount); |
| | 42 | 68 | | return removed ? new PersistentGridConfigurationMap<T>(root) : this; |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | private static Node Set(Node? node, GridConfigurationKey key, T value) |
| | | 72 | | { |
| | 802 | 73 | | if (node == null) |
| | 688 | 74 | | return new Node(key, value, null, null); |
| | 114 | 75 | | int comparison = Compare(key, node.Key); |
| | 114 | 76 | | if (comparison == 0) |
| | 1 | 77 | | return new Node(key, value, node.Left, node.Right); |
| | 113 | 78 | | return comparison < 0 |
| | 113 | 79 | | ? Balance(new Node(node.Key, node.Value, Set(node.Left, key, value), node.Right)) |
| | 113 | 80 | | : Balance(new Node(node.Key, node.Value, node.Left, Set(node.Right, key, value))); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | private static Node Set( |
| | | 84 | | Node? node, |
| | | 85 | | GridConfigurationKey key, |
| | | 86 | | T value, |
| | | 87 | | ref int copiedNodeCount) |
| | | 88 | | { |
| | 5361 | 89 | | if (node == null) |
| | 2436 | 90 | | return NewNode(key, value, null, null, ref copiedNodeCount); |
| | 2925 | 91 | | int comparison = Compare(key, node.Key); |
| | 2925 | 92 | | if (comparison == 0) |
| | 275 | 93 | | return NewNode(key, value, node.Left, node.Right, ref copiedNodeCount); |
| | 2650 | 94 | | return comparison < 0 |
| | 2650 | 95 | | ? Balance( |
| | 2650 | 96 | | NewNode( |
| | 2650 | 97 | | node.Key, |
| | 2650 | 98 | | node.Value, |
| | 2650 | 99 | | Set(node.Left, key, value, ref copiedNodeCount), |
| | 2650 | 100 | | node.Right, |
| | 2650 | 101 | | ref copiedNodeCount), |
| | 2650 | 102 | | ref copiedNodeCount) |
| | 2650 | 103 | | : Balance( |
| | 2650 | 104 | | NewNode( |
| | 2650 | 105 | | node.Key, |
| | 2650 | 106 | | node.Value, |
| | 2650 | 107 | | node.Left, |
| | 2650 | 108 | | Set(node.Right, key, value, ref copiedNodeCount), |
| | 2650 | 109 | | ref copiedNodeCount), |
| | 2650 | 110 | | ref copiedNodeCount); |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | private static Node? Remove(Node? node, GridConfigurationKey key, out bool removed) |
| | | 114 | | { |
| | 18 | 115 | | if (node == null) |
| | | 116 | | { |
| | 2 | 117 | | removed = false; |
| | 2 | 118 | | return null; |
| | | 119 | | } |
| | 16 | 120 | | int comparison = Compare(key, node.Key); |
| | 16 | 121 | | if (comparison < 0) |
| | | 122 | | { |
| | 4 | 123 | | Node? left = Remove(node.Left, key, out removed); |
| | 4 | 124 | | return removed ? Balance(new Node(node.Key, node.Value, left, node.Right)) : node; |
| | | 125 | | } |
| | 12 | 126 | | if (comparison > 0) |
| | | 127 | | { |
| | 6 | 128 | | Node? right = Remove(node.Right, key, out removed); |
| | 6 | 129 | | return removed ? Balance(new Node(node.Key, node.Value, node.Left, right)) : node; |
| | | 130 | | } |
| | 6 | 131 | | removed = true; |
| | 6 | 132 | | if (node.Left == null) |
| | 4 | 133 | | return node.Right; |
| | 2 | 134 | | if (node.Right == null) |
| | 1 | 135 | | return node.Left; |
| | 1 | 136 | | Node successor = node.Right; |
| | 2 | 137 | | while (successor.Left != null) |
| | 1 | 138 | | successor = successor.Left; |
| | 1 | 139 | | Node? successorRight = Remove(node.Right, successor.Key, out _); |
| | 1 | 140 | | return Balance(new Node(successor.Key, successor.Value, node.Left, successorRight)); |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | private static Node? Remove( |
| | | 144 | | Node? node, |
| | | 145 | | GridConfigurationKey key, |
| | | 146 | | out bool removed, |
| | | 147 | | ref int copiedNodeCount) |
| | | 148 | | { |
| | 62 | 149 | | if (node == null) |
| | | 150 | | { |
| | 2 | 151 | | removed = false; |
| | 2 | 152 | | return null; |
| | | 153 | | } |
| | 60 | 154 | | int comparison = Compare(key, node.Key); |
| | 60 | 155 | | if (comparison < 0) |
| | | 156 | | { |
| | 5 | 157 | | Node? left = Remove(node.Left, key, out removed, ref copiedNodeCount); |
| | 5 | 158 | | return removed |
| | 5 | 159 | | ? Balance( |
| | 5 | 160 | | NewNode(node.Key, node.Value, left, node.Right, ref copiedNodeCount), |
| | 5 | 161 | | ref copiedNodeCount) |
| | 5 | 162 | | : node; |
| | | 163 | | } |
| | 55 | 164 | | if (comparison > 0) |
| | | 165 | | { |
| | 14 | 166 | | Node? right = Remove(node.Right, key, out removed, ref copiedNodeCount); |
| | 14 | 167 | | return removed |
| | 14 | 168 | | ? Balance( |
| | 14 | 169 | | NewNode(node.Key, node.Value, node.Left, right, ref copiedNodeCount), |
| | 14 | 170 | | ref copiedNodeCount) |
| | 14 | 171 | | : node; |
| | | 172 | | } |
| | 41 | 173 | | removed = true; |
| | 41 | 174 | | if (node.Left == null) |
| | 39 | 175 | | return node.Right; |
| | 2 | 176 | | if (node.Right == null) |
| | 1 | 177 | | return node.Left; |
| | 1 | 178 | | Node successor = node.Right; |
| | 2 | 179 | | while (successor.Left != null) |
| | 1 | 180 | | successor = successor.Left; |
| | 1 | 181 | | Node? successorRight = Remove(node.Right, successor.Key, out _, ref copiedNodeCount); |
| | 1 | 182 | | return Balance( |
| | 1 | 183 | | NewNode(successor.Key, successor.Value, node.Left, successorRight, ref copiedNodeCount), |
| | 1 | 184 | | ref copiedNodeCount); |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | private static int Compare(GridConfigurationKey left, GridConfigurationKey right) |
| | | 188 | | { |
| | 234869 | 189 | | int value = left.BoundsMin.X.CompareTo(right.BoundsMin.X); |
| | 369083 | 190 | | if (value != 0) return value; |
| | 100655 | 191 | | value = left.BoundsMin.Y.CompareTo(right.BoundsMin.Y); |
| | 102983 | 192 | | if (value != 0) return value; |
| | 98327 | 193 | | value = left.BoundsMin.Z.CompareTo(right.BoundsMin.Z); |
| | 98511 | 194 | | if (value != 0) return value; |
| | 98143 | 195 | | value = left.BoundsMax.X.CompareTo(right.BoundsMax.X); |
| | 98646 | 196 | | if (value != 0) return value; |
| | 97640 | 197 | | value = left.BoundsMax.Y.CompareTo(right.BoundsMax.Y); |
| | 97743 | 198 | | if (value != 0) return value; |
| | 97537 | 199 | | value = left.BoundsMax.Z.CompareTo(right.BoundsMax.Z); |
| | 97542 | 200 | | if (value != 0) return value; |
| | 97532 | 201 | | value = ((int)left.TopologyKind).CompareTo((int)right.TopologyKind); |
| | 97535 | 202 | | if (value != 0) return value; |
| | 97529 | 203 | | value = left.TopologyMetrics.CellRadius.CompareTo(right.TopologyMetrics.CellRadius); |
| | 97531 | 204 | | if (value != 0) return value; |
| | 97527 | 205 | | value = left.TopologyMetrics.CellWidth.CompareTo(right.TopologyMetrics.CellWidth); |
| | 97534 | 206 | | if (value != 0) return value; |
| | 97520 | 207 | | value = left.TopologyMetrics.LayerHeight.CompareTo(right.TopologyMetrics.LayerHeight); |
| | 97524 | 208 | | if (value != 0) return value; |
| | 97516 | 209 | | value = left.TopologyMetrics.CellLength.CompareTo(right.TopologyMetrics.CellLength); |
| | 97516 | 210 | | return value != 0 |
| | 97516 | 211 | | ? value |
| | 97516 | 212 | | : ((int)left.TopologyMetrics.HexOrientation).CompareTo( |
| | 97516 | 213 | | (int)right.TopologyMetrics.HexOrientation); |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | private static Node Balance(Node node) |
| | | 217 | | { |
| | 120 | 218 | | int balance = HeightOf(node.Left) - HeightOf(node.Right); |
| | 120 | 219 | | if (balance > 1) |
| | | 220 | | { |
| | 3 | 221 | | if (HeightOf(node.Left!.Left) < HeightOf(node.Left.Right)) |
| | 1 | 222 | | node = new Node(node.Key, node.Value, RotateLeft(node.Left), node.Right); |
| | 3 | 223 | | return RotateRight(node); |
| | | 224 | | } |
| | 117 | 225 | | if (balance < -1) |
| | | 226 | | { |
| | 20 | 227 | | if (HeightOf(node.Right!.Right) < HeightOf(node.Right.Left)) |
| | 5 | 228 | | node = new Node(node.Key, node.Value, node.Left, RotateRight(node.Right)); |
| | 20 | 229 | | return RotateLeft(node); |
| | | 230 | | } |
| | 97 | 231 | | return node; |
| | | 232 | | } |
| | | 233 | | |
| | | 234 | | private static Node Balance(Node node, ref int copiedNodeCount) |
| | | 235 | | { |
| | 2666 | 236 | | int balance = HeightOf(node.Left) - HeightOf(node.Right); |
| | 2666 | 237 | | if (balance > 1) |
| | | 238 | | { |
| | 47 | 239 | | if (HeightOf(node.Left!.Left) < HeightOf(node.Left.Right)) |
| | | 240 | | { |
| | 5 | 241 | | node = NewNode( |
| | 5 | 242 | | node.Key, |
| | 5 | 243 | | node.Value, |
| | 5 | 244 | | RotateLeft(node.Left, ref copiedNodeCount), |
| | 5 | 245 | | node.Right, |
| | 5 | 246 | | ref copiedNodeCount); |
| | | 247 | | } |
| | 47 | 248 | | return RotateRight(node, ref copiedNodeCount); |
| | | 249 | | } |
| | 2619 | 250 | | if (balance < -1) |
| | | 251 | | { |
| | 378 | 252 | | if (HeightOf(node.Right!.Right) < HeightOf(node.Right.Left)) |
| | | 253 | | { |
| | 3 | 254 | | node = NewNode( |
| | 3 | 255 | | node.Key, |
| | 3 | 256 | | node.Value, |
| | 3 | 257 | | node.Left, |
| | 3 | 258 | | RotateRight(node.Right, ref copiedNodeCount), |
| | 3 | 259 | | ref copiedNodeCount); |
| | | 260 | | } |
| | 378 | 261 | | return RotateLeft(node, ref copiedNodeCount); |
| | | 262 | | } |
| | 2241 | 263 | | return node; |
| | | 264 | | } |
| | | 265 | | |
| | | 266 | | private static Node RotateLeft(Node node) |
| | | 267 | | { |
| | 21 | 268 | | Node right = node.Right!; |
| | 21 | 269 | | return new Node(right.Key, right.Value, new Node(node.Key, node.Value, node.Left, right.Left), right.Right); |
| | | 270 | | } |
| | | 271 | | |
| | | 272 | | private static Node RotateRight(Node node) |
| | | 273 | | { |
| | 8 | 274 | | Node left = node.Left!; |
| | 8 | 275 | | return new Node(left.Key, left.Value, left.Left, new Node(node.Key, node.Value, left.Right, node.Right)); |
| | | 276 | | } |
| | | 277 | | |
| | | 278 | | private static Node RotateLeft(Node node, ref int copiedNodeCount) |
| | | 279 | | { |
| | 383 | 280 | | Node right = node.Right!; |
| | 383 | 281 | | Node left = NewNode( |
| | 383 | 282 | | node.Key, |
| | 383 | 283 | | node.Value, |
| | 383 | 284 | | node.Left, |
| | 383 | 285 | | right.Left, |
| | 383 | 286 | | ref copiedNodeCount); |
| | 383 | 287 | | return NewNode(right.Key, right.Value, left, right.Right, ref copiedNodeCount); |
| | | 288 | | } |
| | | 289 | | |
| | | 290 | | private static Node RotateRight(Node node, ref int copiedNodeCount) |
| | | 291 | | { |
| | 50 | 292 | | Node left = node.Left!; |
| | 50 | 293 | | Node right = NewNode( |
| | 50 | 294 | | node.Key, |
| | 50 | 295 | | node.Value, |
| | 50 | 296 | | left.Right, |
| | 50 | 297 | | node.Right, |
| | 50 | 298 | | ref copiedNodeCount); |
| | 50 | 299 | | return NewNode(left.Key, left.Value, left.Left, right, ref copiedNodeCount); |
| | | 300 | | } |
| | | 301 | | |
| | | 302 | | private static Node NewNode( |
| | | 303 | | GridConfigurationKey key, |
| | | 304 | | T value, |
| | | 305 | | Node? left, |
| | | 306 | | Node? right, |
| | | 307 | | ref int copiedNodeCount) |
| | | 308 | | { |
| | 6251 | 309 | | copiedNodeCount = checked(copiedNodeCount + 1); |
| | 6251 | 310 | | return new Node(key, value, left, right); |
| | | 311 | | } |
| | | 312 | | |
| | 278102 | 313 | | private static int CountOf(Node? node) => node?.Count ?? 0; |
| | 20716 | 314 | | private static int HeightOf(Node? node) => node?.Height ?? 0; |
| | | 315 | | |
| | | 316 | | private Node GetNodeAt(int ordinal) |
| | | 317 | | { |
| | 973 | 318 | | if ((uint)ordinal >= (uint)Count) |
| | 1 | 319 | | throw new ArgumentOutOfRangeException(nameof(ordinal)); |
| | 972 | 320 | | Node node = _root!; |
| | 24 | 321 | | while (true) |
| | | 322 | | { |
| | 1011 | 323 | | int leftCount = CountOf(node.Left); |
| | 1011 | 324 | | if (ordinal < leftCount) |
| | | 325 | | { |
| | 15 | 326 | | node = node.Left!; |
| | 15 | 327 | | continue; |
| | | 328 | | } |
| | 996 | 329 | | if (ordinal == leftCount) |
| | 972 | 330 | | return node; |
| | 24 | 331 | | ordinal -= leftCount + 1; |
| | 24 | 332 | | node = node.Right!; |
| | | 333 | | } |
| | | 334 | | } |
| | | 335 | | |
| | | 336 | | private sealed class Node |
| | | 337 | | { |
| | 7124 | 338 | | internal Node(GridConfigurationKey key, T value, Node? left, Node? right) |
| | | 339 | | { |
| | 7124 | 340 | | Key = key; |
| | 7124 | 341 | | Value = value; |
| | 7124 | 342 | | Left = left; |
| | 7124 | 343 | | Right = right; |
| | 7124 | 344 | | Height = Math.Max(HeightOf(left), HeightOf(right)) + 1; |
| | 7124 | 345 | | Count = CountOf(left) + CountOf(right) + 1; |
| | 7124 | 346 | | } |
| | | 347 | | |
| | | 348 | | internal GridConfigurationKey Key { get; } |
| | | 349 | | internal T Value { get; } |
| | | 350 | | internal Node? Left { get; } |
| | | 351 | | internal Node? Right { get; } |
| | | 352 | | internal int Height { get; } |
| | | 353 | | internal int Count { get; } |
| | | 354 | | } |
| | | 355 | | } |