| | | 1 | | //======================================================================= |
| | | 2 | | // PersistentIntMap.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 | | |
| | | 10 | | namespace Trailblazer.Pathing; |
| | | 11 | | |
| | | 12 | | /// <summary>Stores integer-addressed persistent pages with logarithmic path copying.</summary> |
| | | 13 | | internal sealed class PersistentIntMap<T> |
| | | 14 | | { |
| | | 15 | | private readonly Node? _root; |
| | | 16 | | |
| | | 17 | | internal static PersistentIntMap<T> Empty { get; } = new(null); |
| | | 18 | | |
| | 72790 | 19 | | private PersistentIntMap(Node? root) => _root = root; |
| | | 20 | | |
| | 82643 | 21 | | internal int Count => CountOf(_root); |
| | | 22 | | |
| | 29819 | 23 | | internal long RetainedBytes => checked(32L + ((long)Count * 72L)); |
| | | 24 | | |
| | 31658 | 25 | | internal int PersistentNodeCount => Count; |
| | | 26 | | |
| | | 27 | | internal bool TryGetValue(int key, out T value) |
| | | 28 | | { |
| | 15756073 | 29 | | Node? node = _root; |
| | 79471478 | 30 | | while (node != null) |
| | | 31 | | { |
| | 75572137 | 32 | | if (key == node.Key) |
| | | 33 | | { |
| | 11856732 | 34 | | value = node.Value; |
| | 11856732 | 35 | | return true; |
| | | 36 | | } |
| | 63715405 | 37 | | node = key < node.Key ? node.Left : node.Right; |
| | | 38 | | } |
| | 3899341 | 39 | | value = default!; |
| | 3899341 | 40 | | return false; |
| | | 41 | | } |
| | | 42 | | |
| | 2976 | 43 | | internal PersistentIntMap<T> Set(int key, T value) => new(Set(_root, key, value)); |
| | | 44 | | |
| | | 45 | | internal PersistentIntMap<T> Set(int key, T value, out int copiedNodeCount) |
| | | 46 | | { |
| | 33346 | 47 | | copiedNodeCount = 0; |
| | 33346 | 48 | | return new PersistentIntMap<T>(Set(_root, key, value, ref copiedNodeCount)); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | internal PersistentIntMap<T> Remove(int key) |
| | | 52 | | { |
| | 8 | 53 | | Node? root = Remove(_root, key, out bool removed); |
| | 8 | 54 | | return removed ? new PersistentIntMap<T>(root) : this; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | internal PersistentIntMap<T> Remove(int key, out int copiedNodeCount) |
| | | 58 | | { |
| | 62 | 59 | | copiedNodeCount = 0; |
| | 62 | 60 | | Node? root = Remove(_root, key, out bool removed, ref copiedNodeCount); |
| | 62 | 61 | | return removed ? new PersistentIntMap<T>(root) : this; |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | internal T GetValueAt(int ordinal) |
| | | 65 | | { |
| | 1113 | 66 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 1113 | 67 | | (uint)ordinal >= (uint)Count, |
| | 1113 | 68 | | ordinal, |
| | 1113 | 69 | | nameof(ordinal)); |
| | 1113 | 70 | | Node node = _root!; |
| | 403 | 71 | | while (true) |
| | | 72 | | { |
| | 1819 | 73 | | int leftCount = CountOf(node.Left); |
| | 1819 | 74 | | if (ordinal < leftCount) |
| | 303 | 75 | | node = node.Left!; |
| | 1516 | 76 | | else if (ordinal == leftCount) |
| | 1113 | 77 | | return node.Value; |
| | | 78 | | else |
| | | 79 | | { |
| | 403 | 80 | | ordinal -= leftCount + 1; |
| | 403 | 81 | | node = node.Right!; |
| | | 82 | | } |
| | | 83 | | } |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | private static Node Set(Node? node, int key, T value) |
| | | 87 | | { |
| | 4405 | 88 | | if (node == null) |
| | 2688 | 89 | | return new Node(key, value, null, null); |
| | 1717 | 90 | | if (key == node.Key) |
| | 288 | 91 | | return ReferenceEquals(value, node.Value) |
| | 288 | 92 | | ? node |
| | 288 | 93 | | : new Node(key, value, node.Left, node.Right); |
| | 1429 | 94 | | return key < node.Key |
| | 1429 | 95 | | ? Balance(new Node(node.Key, node.Value, Set(node.Left, key, value), node.Right)) |
| | 1429 | 96 | | : Balance(new Node(node.Key, node.Value, node.Left, Set(node.Right, key, value))); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | private static Node Set(Node? node, int key, T value, ref int copiedNodeCount) |
| | | 100 | | { |
| | 282084 | 101 | | if (node == null) |
| | 17122 | 102 | | return NewNode(key, value, null, null, ref copiedNodeCount); |
| | 264962 | 103 | | if (key == node.Key) |
| | 16224 | 104 | | return ReferenceEquals(value, node.Value) |
| | 16224 | 105 | | ? node |
| | 16224 | 106 | | : NewNode(key, value, node.Left, node.Right, ref copiedNodeCount); |
| | 248738 | 107 | | return key < node.Key |
| | 248738 | 108 | | ? Balance( |
| | 248738 | 109 | | NewNode( |
| | 248738 | 110 | | node.Key, |
| | 248738 | 111 | | node.Value, |
| | 248738 | 112 | | Set(node.Left, key, value, ref copiedNodeCount), |
| | 248738 | 113 | | node.Right, |
| | 248738 | 114 | | ref copiedNodeCount), |
| | 248738 | 115 | | ref copiedNodeCount) |
| | 248738 | 116 | | : Balance( |
| | 248738 | 117 | | NewNode( |
| | 248738 | 118 | | node.Key, |
| | 248738 | 119 | | node.Value, |
| | 248738 | 120 | | node.Left, |
| | 248738 | 121 | | Set(node.Right, key, value, ref copiedNodeCount), |
| | 248738 | 122 | | ref copiedNodeCount), |
| | 248738 | 123 | | ref copiedNodeCount); |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | private static Node? Remove(Node? node, int key, out bool removed) |
| | | 127 | | { |
| | 16 | 128 | | if (node == null) |
| | | 129 | | { |
| | 2 | 130 | | removed = false; |
| | 2 | 131 | | return null; |
| | | 132 | | } |
| | 14 | 133 | | if (key < node.Key) |
| | | 134 | | { |
| | 3 | 135 | | Node? left = Remove(node.Left, key, out removed); |
| | 3 | 136 | | return removed ? Balance(new Node(node.Key, node.Value, left, node.Right)) : node; |
| | | 137 | | } |
| | 11 | 138 | | if (key > node.Key) |
| | | 139 | | { |
| | 5 | 140 | | Node? right = Remove(node.Right, key, out removed); |
| | 5 | 141 | | return removed ? Balance(new Node(node.Key, node.Value, node.Left, right)) : node; |
| | | 142 | | } |
| | 6 | 143 | | removed = true; |
| | 6 | 144 | | if (node.Left == null) |
| | 4 | 145 | | return node.Right; |
| | 2 | 146 | | if (node.Right == null) |
| | 1 | 147 | | return node.Left; |
| | 1 | 148 | | Node successor = node.Right; |
| | 2 | 149 | | while (successor.Left != null) |
| | 1 | 150 | | successor = successor.Left; |
| | 1 | 151 | | return Balance(new Node( |
| | 1 | 152 | | successor.Key, |
| | 1 | 153 | | successor.Value, |
| | 1 | 154 | | node.Left, |
| | 1 | 155 | | RemoveMinimum(node.Right))); |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | private static Node? Remove( |
| | | 159 | | Node? node, |
| | | 160 | | int key, |
| | | 161 | | out bool removed, |
| | | 162 | | ref int copiedNodeCount) |
| | | 163 | | { |
| | 74 | 164 | | if (node == null) |
| | | 165 | | { |
| | 3 | 166 | | removed = false; |
| | 3 | 167 | | return null; |
| | | 168 | | } |
| | 71 | 169 | | if (key < node.Key) |
| | | 170 | | { |
| | 4 | 171 | | Node? left = Remove(node.Left, key, out removed, ref copiedNodeCount); |
| | 4 | 172 | | return removed |
| | 4 | 173 | | ? Balance( |
| | 4 | 174 | | NewNode(node.Key, node.Value, left, node.Right, ref copiedNodeCount), |
| | 4 | 175 | | ref copiedNodeCount) |
| | 4 | 176 | | : node; |
| | | 177 | | } |
| | 67 | 178 | | if (key > node.Key) |
| | | 179 | | { |
| | 8 | 180 | | Node? right = Remove(node.Right, key, out removed, ref copiedNodeCount); |
| | 8 | 181 | | return removed |
| | 8 | 182 | | ? Balance( |
| | 8 | 183 | | NewNode(node.Key, node.Value, node.Left, right, ref copiedNodeCount), |
| | 8 | 184 | | ref copiedNodeCount) |
| | 8 | 185 | | : node; |
| | | 186 | | } |
| | 59 | 187 | | removed = true; |
| | 59 | 188 | | if (node.Left == null) |
| | 57 | 189 | | return node.Right; |
| | 2 | 190 | | if (node.Right == null) |
| | 1 | 191 | | return node.Left; |
| | 1 | 192 | | Node successor = node.Right; |
| | 2 | 193 | | while (successor.Left != null) |
| | 1 | 194 | | successor = successor.Left; |
| | 1 | 195 | | return Balance( |
| | 1 | 196 | | NewNode( |
| | 1 | 197 | | successor.Key, |
| | 1 | 198 | | successor.Value, |
| | 1 | 199 | | node.Left, |
| | 1 | 200 | | RemoveMinimum(node.Right, ref copiedNodeCount), |
| | 1 | 201 | | ref copiedNodeCount), |
| | 1 | 202 | | ref copiedNodeCount); |
| | | 203 | | } |
| | | 204 | | |
| | 2 | 205 | | private static Node? RemoveMinimum(Node node) => node.Left == null |
| | 2 | 206 | | ? node.Right |
| | 2 | 207 | | : Balance(new Node(node.Key, node.Value, RemoveMinimum(node.Left), node.Right)); |
| | | 208 | | |
| | 2 | 209 | | private static Node? RemoveMinimum(Node node, ref int copiedNodeCount) => node.Left == null |
| | 2 | 210 | | ? node.Right |
| | 2 | 211 | | : Balance( |
| | 2 | 212 | | NewNode( |
| | 2 | 213 | | node.Key, |
| | 2 | 214 | | node.Value, |
| | 2 | 215 | | RemoveMinimum(node.Left, ref copiedNodeCount), |
| | 2 | 216 | | node.Right, |
| | 2 | 217 | | ref copiedNodeCount), |
| | 2 | 218 | | ref copiedNodeCount); |
| | | 219 | | |
| | | 220 | | private static Node Balance(Node node) |
| | | 221 | | { |
| | 1437 | 222 | | int balance = HeightOf(node.Left) - HeightOf(node.Right); |
| | 1437 | 223 | | if (balance > 1) |
| | | 224 | | { |
| | 2 | 225 | | if (HeightOf(node.Left!.Left) < HeightOf(node.Left.Right)) |
| | 1 | 226 | | node = new Node(node.Key, node.Value, RotateLeft(node.Left), node.Right); |
| | 2 | 227 | | return RotateRight(node); |
| | | 228 | | } |
| | 1435 | 229 | | if (balance < -1) |
| | | 230 | | { |
| | 298 | 231 | | if (HeightOf(node.Right!.Right) < HeightOf(node.Right.Left)) |
| | 3 | 232 | | node = new Node(node.Key, node.Value, node.Left, RotateRight(node.Right)); |
| | 298 | 233 | | return RotateLeft(node); |
| | | 234 | | } |
| | 1137 | 235 | | return node; |
| | | 236 | | } |
| | | 237 | | |
| | | 238 | | private static Node Balance(Node node, ref int copiedNodeCount) |
| | | 239 | | { |
| | 248747 | 240 | | int balance = HeightOf(node.Left) - HeightOf(node.Right); |
| | 248747 | 241 | | if (balance > 1) |
| | | 242 | | { |
| | 2 | 243 | | if (HeightOf(node.Left!.Left) < HeightOf(node.Left.Right)) |
| | | 244 | | { |
| | 1 | 245 | | node = NewNode( |
| | 1 | 246 | | node.Key, |
| | 1 | 247 | | node.Value, |
| | 1 | 248 | | RotateLeft(node.Left, ref copiedNodeCount), |
| | 1 | 249 | | node.Right, |
| | 1 | 250 | | ref copiedNodeCount); |
| | | 251 | | } |
| | 2 | 252 | | return RotateRight(node, ref copiedNodeCount); |
| | | 253 | | } |
| | 248745 | 254 | | if (balance < -1) |
| | | 255 | | { |
| | 16696 | 256 | | if (HeightOf(node.Right!.Right) < HeightOf(node.Right.Left)) |
| | | 257 | | { |
| | 1 | 258 | | node = NewNode( |
| | 1 | 259 | | node.Key, |
| | 1 | 260 | | node.Value, |
| | 1 | 261 | | node.Left, |
| | 1 | 262 | | RotateRight(node.Right, ref copiedNodeCount), |
| | 1 | 263 | | ref copiedNodeCount); |
| | | 264 | | } |
| | 16696 | 265 | | return RotateLeft(node, ref copiedNodeCount); |
| | | 266 | | } |
| | 232049 | 267 | | return node; |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | private static Node RotateLeft(Node node) |
| | | 271 | | { |
| | 299 | 272 | | Node right = node.Right!; |
| | 299 | 273 | | return new Node( |
| | 299 | 274 | | right.Key, |
| | 299 | 275 | | right.Value, |
| | 299 | 276 | | new Node(node.Key, node.Value, node.Left, right.Left), |
| | 299 | 277 | | right.Right); |
| | | 278 | | } |
| | | 279 | | |
| | | 280 | | private static Node RotateRight(Node node) |
| | | 281 | | { |
| | 5 | 282 | | Node left = node.Left!; |
| | 5 | 283 | | return new Node( |
| | 5 | 284 | | left.Key, |
| | 5 | 285 | | left.Value, |
| | 5 | 286 | | left.Left, |
| | 5 | 287 | | new Node(node.Key, node.Value, left.Right, node.Right)); |
| | | 288 | | } |
| | | 289 | | |
| | | 290 | | private static Node RotateLeft(Node node, ref int copiedNodeCount) |
| | | 291 | | { |
| | 16697 | 292 | | Node right = node.Right!; |
| | 16697 | 293 | | Node left = NewNode( |
| | 16697 | 294 | | node.Key, |
| | 16697 | 295 | | node.Value, |
| | 16697 | 296 | | node.Left, |
| | 16697 | 297 | | right.Left, |
| | 16697 | 298 | | ref copiedNodeCount); |
| | 16697 | 299 | | return NewNode(right.Key, right.Value, left, right.Right, ref copiedNodeCount); |
| | | 300 | | } |
| | | 301 | | |
| | | 302 | | private static Node RotateRight(Node node, ref int copiedNodeCount) |
| | | 303 | | { |
| | 3 | 304 | | Node left = node.Left!; |
| | 3 | 305 | | Node right = NewNode( |
| | 3 | 306 | | node.Key, |
| | 3 | 307 | | node.Value, |
| | 3 | 308 | | left.Right, |
| | 3 | 309 | | node.Right, |
| | 3 | 310 | | ref copiedNodeCount); |
| | 3 | 311 | | return NewNode(left.Key, left.Value, left.Left, right, ref copiedNodeCount); |
| | | 312 | | } |
| | | 313 | | |
| | | 314 | | private static Node NewNode( |
| | | 315 | | int key, |
| | | 316 | | T value, |
| | | 317 | | Node? left, |
| | | 318 | | Node? right, |
| | | 319 | | ref int copiedNodeCount) |
| | | 320 | | { |
| | 315494 | 321 | | copiedNodeCount = checked(copiedNodeCount + 1); |
| | 315494 | 322 | | return new Node(key, value, left, right); |
| | | 323 | | } |
| | | 324 | | |
| | 725498 | 325 | | private static int CountOf(Node? node) => node?.Count ?? 0; |
| | 1175400 | 326 | | private static int HeightOf(Node? node) => node?.Height ?? 0; |
| | | 327 | | |
| | | 328 | | private sealed class Node |
| | | 329 | | { |
| | 320518 | 330 | | internal Node(int key, T value, Node? left, Node? right) |
| | | 331 | | { |
| | 320518 | 332 | | Key = key; |
| | 320518 | 333 | | Value = value; |
| | 320518 | 334 | | Left = left; |
| | 320518 | 335 | | Right = right; |
| | 320518 | 336 | | Height = Math.Max(HeightOf(left), HeightOf(right)) + 1; |
| | 320518 | 337 | | Count = CountOf(left) + CountOf(right) + 1; |
| | 320518 | 338 | | } |
| | | 339 | | |
| | | 340 | | internal int Key { get; } |
| | | 341 | | internal T Value { get; } |
| | | 342 | | internal Node? Left { get; } |
| | | 343 | | internal Node? Right { get; } |
| | | 344 | | internal int Height { get; } |
| | | 345 | | internal int Count { get; } |
| | | 346 | | } |
| | | 347 | | } |