| | | 1 | | //======================================================================= |
| | | 2 | | // PersistentStringMap.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>Provides an ordinal string map whose updates copy only one balanced-tree path.</summary> |
| | | 13 | | internal sealed class PersistentStringMap<T> |
| | | 14 | | { |
| | | 15 | | private readonly Node? _root; |
| | | 16 | | |
| | | 17 | | internal static PersistentStringMap<T> Empty { get; } = new(null); |
| | | 18 | | |
| | 179155 | 19 | | private PersistentStringMap(Node? root) |
| | | 20 | | { |
| | 179155 | 21 | | _root = root; |
| | 179155 | 22 | | } |
| | | 23 | | |
| | 21603099 | 24 | | internal int Count => CountOf(_root); |
| | | 25 | | |
| | 3064233 | 26 | | internal int PersistentNodeCount => Count; |
| | | 27 | | |
| | 2939897 | 28 | | internal long RetainedBytes => checked(32L + ((long)Count * 64L)); |
| | | 29 | | |
| | 79635 | 30 | | internal bool ContainsKey(string key) => TryGetValue(key, out _); |
| | | 31 | | |
| | | 32 | | internal bool TryGetValue(string key, out T value) |
| | | 33 | | { |
| | 13566518 | 34 | | Node? node = _root; |
| | 21848015 | 35 | | while (node != null) |
| | | 36 | | { |
| | 19252877 | 37 | | int comparison = string.CompareOrdinal(key, node.Key); |
| | 19252877 | 38 | | if (comparison == 0) |
| | | 39 | | { |
| | 10971380 | 40 | | value = node.Value; |
| | 10971380 | 41 | | return true; |
| | | 42 | | } |
| | 8281497 | 43 | | node = comparison < 0 ? node.Left : node.Right; |
| | | 44 | | } |
| | 2595138 | 45 | | value = default!; |
| | 2595138 | 46 | | return false; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | internal PersistentStringMap<T> Set(string key, T value) => |
| | 75500 | 50 | | new(Set(_root, key, value)); |
| | | 51 | | |
| | | 52 | | internal PersistentStringMap<T> Set(string key, T value, out int copiedNodeCount) |
| | | 53 | | { |
| | 103072 | 54 | | copiedNodeCount = 0; |
| | 103072 | 55 | | return new PersistentStringMap<T>(Set(_root, key, value, ref copiedNodeCount)); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | internal PersistentStringMap<T> Remove(string key, out bool removed) |
| | | 59 | | { |
| | 78 | 60 | | Node? root = Remove(_root, key, out removed); |
| | 78 | 61 | | return removed ? new PersistentStringMap<T>(root) : this; |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | internal PersistentStringMap<T> Remove( |
| | | 65 | | string key, |
| | | 66 | | out bool removed, |
| | | 67 | | out int copiedNodeCount) |
| | | 68 | | { |
| | 505 | 69 | | copiedNodeCount = 0; |
| | 505 | 70 | | Node? root = Remove(_root, key, out removed, ref copiedNodeCount); |
| | 505 | 71 | | return removed ? new PersistentStringMap<T>(root) : this; |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | internal T GetValueAt(int ordinal) |
| | 6544190 | 75 | | => GetNodeAt(ordinal).Value; |
| | | 76 | | |
| | | 77 | | internal string GetKeyAt(int ordinal) |
| | 1746022 | 78 | | => GetNodeAt(ordinal).Key; |
| | | 79 | | |
| | | 80 | | private Node GetNodeAt(int ordinal) |
| | | 81 | | { |
| | 8290212 | 82 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 8290212 | 83 | | (uint)ordinal >= (uint)Count, |
| | 8290212 | 84 | | ordinal, |
| | 8290212 | 85 | | nameof(ordinal)); |
| | 8290212 | 86 | | Node node = _root!; |
| | 6171150 | 87 | | while (true) |
| | | 88 | | { |
| | 17173963 | 89 | | int leftCount = CountOf(node.Left); |
| | 17173963 | 90 | | if (ordinal < leftCount) |
| | 2712601 | 91 | | node = node.Left!; |
| | 14461362 | 92 | | else if (ordinal == leftCount) |
| | 8290212 | 93 | | return node; |
| | | 94 | | else |
| | | 95 | | { |
| | 6171150 | 96 | | ordinal -= leftCount + 1; |
| | 6171150 | 97 | | node = node.Right!; |
| | | 98 | | } |
| | | 99 | | } |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | private static Node Set(Node? node, string key, T value) |
| | | 103 | | { |
| | 155296 | 104 | | if (node == null) |
| | 20862 | 105 | | return new Node(key, value, null, null); |
| | 134434 | 106 | | int comparison = string.CompareOrdinal(key, node.Key); |
| | 134434 | 107 | | if (comparison == 0) |
| | 54638 | 108 | | return new Node(key, value, node.Left, node.Right); |
| | 79796 | 109 | | return comparison < 0 |
| | 79796 | 110 | | ? Balance(new Node(node.Key, node.Value, Set(node.Left, key, value), node.Right)) |
| | 79796 | 111 | | : Balance(new Node(node.Key, node.Value, node.Left, Set(node.Right, key, value))); |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | private static Node Set(Node? node, string key, T value, ref int copiedNodeCount) |
| | | 115 | | { |
| | 283335 | 116 | | if (node == null) |
| | 21112 | 117 | | return NewNode(key, value, null, null, ref copiedNodeCount); |
| | 262223 | 118 | | int comparison = string.CompareOrdinal(key, node.Key); |
| | 262223 | 119 | | if (comparison == 0) |
| | 81960 | 120 | | return NewNode(key, value, node.Left, node.Right, ref copiedNodeCount); |
| | 180263 | 121 | | return comparison < 0 |
| | 180263 | 122 | | ? Balance( |
| | 180263 | 123 | | NewNode( |
| | 180263 | 124 | | node.Key, |
| | 180263 | 125 | | node.Value, |
| | 180263 | 126 | | Set(node.Left, key, value, ref copiedNodeCount), |
| | 180263 | 127 | | node.Right, |
| | 180263 | 128 | | ref copiedNodeCount), |
| | 180263 | 129 | | ref copiedNodeCount) |
| | 180263 | 130 | | : Balance( |
| | 180263 | 131 | | NewNode( |
| | 180263 | 132 | | node.Key, |
| | 180263 | 133 | | node.Value, |
| | 180263 | 134 | | node.Left, |
| | 180263 | 135 | | Set(node.Right, key, value, ref copiedNodeCount), |
| | 180263 | 136 | | ref copiedNodeCount), |
| | 180263 | 137 | | ref copiedNodeCount); |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | private static Node? Remove(Node? node, string key, out bool removed) |
| | | 141 | | { |
| | 89 | 142 | | if (node == null) |
| | | 143 | | { |
| | 9 | 144 | | removed = false; |
| | 9 | 145 | | return null; |
| | | 146 | | } |
| | 80 | 147 | | int comparison = string.CompareOrdinal(key, node.Key); |
| | 80 | 148 | | if (comparison < 0) |
| | | 149 | | { |
| | 5 | 150 | | Node? left = Remove(node.Left, key, out removed); |
| | 5 | 151 | | return removed ? Balance(new Node(node.Key, node.Value, left, node.Right)) : node; |
| | | 152 | | } |
| | 75 | 153 | | if (comparison > 0) |
| | | 154 | | { |
| | 6 | 155 | | Node? right = Remove(node.Right, key, out removed); |
| | 6 | 156 | | return removed ? Balance(new Node(node.Key, node.Value, node.Left, right)) : node; |
| | | 157 | | } |
| | | 158 | | |
| | 69 | 159 | | removed = true; |
| | 69 | 160 | | if (node.Left == null) |
| | 67 | 161 | | return node.Right; |
| | 2 | 162 | | if (node.Right == null) |
| | 1 | 163 | | return node.Left; |
| | 1 | 164 | | Node successor = FindMinimum(node.Right); |
| | 1 | 165 | | Node? successorRight = RemoveMinimum(node.Right); |
| | 1 | 166 | | return Balance(new Node(successor.Key, successor.Value, node.Left, successorRight)); |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | private static Node? Remove( |
| | | 170 | | Node? node, |
| | | 171 | | string key, |
| | | 172 | | out bool removed, |
| | | 173 | | ref int copiedNodeCount) |
| | | 174 | | { |
| | 657 | 175 | | if (node == null) |
| | | 176 | | { |
| | 9 | 177 | | removed = false; |
| | 9 | 178 | | return null; |
| | | 179 | | } |
| | 648 | 180 | | int comparison = string.CompareOrdinal(key, node.Key); |
| | 648 | 181 | | if (comparison < 0) |
| | | 182 | | { |
| | 91 | 183 | | Node? left = Remove(node.Left, key, out removed, ref copiedNodeCount); |
| | 91 | 184 | | return removed |
| | 91 | 185 | | ? Balance( |
| | 91 | 186 | | NewNode(node.Key, node.Value, left, node.Right, ref copiedNodeCount), |
| | 91 | 187 | | ref copiedNodeCount) |
| | 91 | 188 | | : node; |
| | | 189 | | } |
| | 557 | 190 | | if (comparison > 0) |
| | | 191 | | { |
| | 61 | 192 | | Node? right = Remove(node.Right, key, out removed, ref copiedNodeCount); |
| | 61 | 193 | | return removed |
| | 61 | 194 | | ? Balance( |
| | 61 | 195 | | NewNode(node.Key, node.Value, node.Left, right, ref copiedNodeCount), |
| | 61 | 196 | | ref copiedNodeCount) |
| | 61 | 197 | | : node; |
| | | 198 | | } |
| | | 199 | | |
| | 496 | 200 | | removed = true; |
| | 496 | 201 | | if (node.Left == null) |
| | 454 | 202 | | return node.Right; |
| | 42 | 203 | | if (node.Right == null) |
| | 11 | 204 | | return node.Left; |
| | 31 | 205 | | Node successor = FindMinimum(node.Right); |
| | 31 | 206 | | Node? successorRight = RemoveMinimum(node.Right, ref copiedNodeCount); |
| | 31 | 207 | | return Balance( |
| | 31 | 208 | | NewNode(successor.Key, successor.Value, node.Left, successorRight, ref copiedNodeCount), |
| | 31 | 209 | | ref copiedNodeCount); |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | private static Node FindMinimum(Node node) |
| | | 213 | | { |
| | 45 | 214 | | while (node.Left != null) |
| | 13 | 215 | | node = node.Left; |
| | 32 | 216 | | return node; |
| | | 217 | | } |
| | | 218 | | |
| | 2 | 219 | | private static Node? RemoveMinimum(Node node) => node.Left == null |
| | 2 | 220 | | ? node.Right |
| | 2 | 221 | | : Balance(new Node(node.Key, node.Value, RemoveMinimum(node.Left), node.Right)); |
| | | 222 | | |
| | 43 | 223 | | private static Node? RemoveMinimum(Node node, ref int copiedNodeCount) => node.Left == null |
| | 43 | 224 | | ? node.Right |
| | 43 | 225 | | : Balance( |
| | 43 | 226 | | NewNode( |
| | 43 | 227 | | node.Key, |
| | 43 | 228 | | node.Value, |
| | 43 | 229 | | RemoveMinimum(node.Left, ref copiedNodeCount), |
| | 43 | 230 | | node.Right, |
| | 43 | 231 | | ref copiedNodeCount), |
| | 43 | 232 | | ref copiedNodeCount); |
| | | 233 | | |
| | | 234 | | private static Node Balance(Node node) |
| | | 235 | | { |
| | 79803 | 236 | | int balance = HeightOf(node.Left) - HeightOf(node.Right); |
| | 79803 | 237 | | if (balance > 1) |
| | | 238 | | { |
| | 45 | 239 | | if (HeightOf(node.Left!.Left) < HeightOf(node.Left.Right)) |
| | 19 | 240 | | node = new Node(node.Key, node.Value, RotateLeft(node.Left), node.Right); |
| | 45 | 241 | | return RotateRight(node); |
| | | 242 | | } |
| | 79758 | 243 | | if (balance < -1) |
| | | 244 | | { |
| | 8837 | 245 | | if (HeightOf(node.Right!.Right) < HeightOf(node.Right.Left)) |
| | 38 | 246 | | node = new Node(node.Key, node.Value, node.Left, RotateRight(node.Right)); |
| | 8837 | 247 | | return RotateLeft(node); |
| | | 248 | | } |
| | 70921 | 249 | | return node; |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | private static Node Balance(Node node, ref int copiedNodeCount) |
| | | 253 | | { |
| | 180452 | 254 | | int balance = HeightOf(node.Left) - HeightOf(node.Right); |
| | 180452 | 255 | | if (balance > 1) |
| | | 256 | | { |
| | 122 | 257 | | if (HeightOf(node.Left!.Left) < HeightOf(node.Left.Right)) |
| | | 258 | | { |
| | 68 | 259 | | node = NewNode( |
| | 68 | 260 | | node.Key, |
| | 68 | 261 | | node.Value, |
| | 68 | 262 | | RotateLeft(node.Left, ref copiedNodeCount), |
| | 68 | 263 | | node.Right, |
| | 68 | 264 | | ref copiedNodeCount); |
| | | 265 | | } |
| | 122 | 266 | | return RotateRight(node, ref copiedNodeCount); |
| | | 267 | | } |
| | 180330 | 268 | | if (balance < -1) |
| | | 269 | | { |
| | 13284 | 270 | | if (HeightOf(node.Right!.Right) < HeightOf(node.Right.Left)) |
| | | 271 | | { |
| | 18 | 272 | | node = NewNode( |
| | 18 | 273 | | node.Key, |
| | 18 | 274 | | node.Value, |
| | 18 | 275 | | node.Left, |
| | 18 | 276 | | RotateRight(node.Right, ref copiedNodeCount), |
| | 18 | 277 | | ref copiedNodeCount); |
| | | 278 | | } |
| | 13284 | 279 | | return RotateLeft(node, ref copiedNodeCount); |
| | | 280 | | } |
| | 167046 | 281 | | return node; |
| | | 282 | | } |
| | | 283 | | |
| | | 284 | | private static Node RotateLeft(Node node) |
| | | 285 | | { |
| | 8856 | 286 | | Node right = node.Right!; |
| | 8856 | 287 | | return new Node( |
| | 8856 | 288 | | right.Key, |
| | 8856 | 289 | | right.Value, |
| | 8856 | 290 | | new Node(node.Key, node.Value, node.Left, right.Left), |
| | 8856 | 291 | | right.Right); |
| | | 292 | | } |
| | | 293 | | |
| | | 294 | | private static Node RotateRight(Node node) |
| | | 295 | | { |
| | 83 | 296 | | Node left = node.Left!; |
| | 83 | 297 | | return new Node( |
| | 83 | 298 | | left.Key, |
| | 83 | 299 | | left.Value, |
| | 83 | 300 | | left.Left, |
| | 83 | 301 | | new Node(node.Key, node.Value, left.Right, node.Right)); |
| | | 302 | | } |
| | | 303 | | |
| | | 304 | | private static Node RotateLeft(Node node, ref int copiedNodeCount) |
| | | 305 | | { |
| | 13352 | 306 | | Node right = node.Right!; |
| | 13352 | 307 | | Node left = NewNode( |
| | 13352 | 308 | | node.Key, |
| | 13352 | 309 | | node.Value, |
| | 13352 | 310 | | node.Left, |
| | 13352 | 311 | | right.Left, |
| | 13352 | 312 | | ref copiedNodeCount); |
| | 13352 | 313 | | return NewNode(right.Key, right.Value, left, right.Right, ref copiedNodeCount); |
| | | 314 | | } |
| | | 315 | | |
| | | 316 | | private static Node RotateRight(Node node, ref int copiedNodeCount) |
| | | 317 | | { |
| | 140 | 318 | | Node left = node.Left!; |
| | 140 | 319 | | Node right = NewNode( |
| | 140 | 320 | | node.Key, |
| | 140 | 321 | | node.Value, |
| | 140 | 322 | | left.Right, |
| | 140 | 323 | | node.Right, |
| | 140 | 324 | | ref copiedNodeCount); |
| | 140 | 325 | | return NewNode(left.Key, left.Value, left.Left, right, ref copiedNodeCount); |
| | | 326 | | } |
| | | 327 | | |
| | | 328 | | private static Node NewNode( |
| | | 329 | | string key, |
| | | 330 | | T value, |
| | | 331 | | Node? left, |
| | | 332 | | Node? right, |
| | | 333 | | ref int copiedNodeCount) |
| | | 334 | | { |
| | 310594 | 335 | | copiedNodeCount = checked(copiedNodeCount + 1); |
| | 310594 | 336 | | return new Node(key, value, left, right); |
| | | 337 | | } |
| | | 338 | | |
| | 39744726 | 339 | | private static int CountOf(Node? node) => node?.Count ?? 0; |
| | 1532750 | 340 | | private static int HeightOf(Node? node) => node?.Height ?? 0; |
| | | 341 | | |
| | | 342 | | private sealed class Node |
| | | 343 | | { |
| | 483832 | 344 | | internal Node(string key, T value, Node? left, Node? right) |
| | | 345 | | { |
| | 483832 | 346 | | Key = key; |
| | 483832 | 347 | | Value = value; |
| | 483832 | 348 | | Left = left; |
| | 483832 | 349 | | Right = right; |
| | 483832 | 350 | | Height = Math.Max(HeightOf(left), HeightOf(right)) + 1; |
| | 483832 | 351 | | Count = CountOf(left) + CountOf(right) + 1; |
| | 483832 | 352 | | } |
| | | 353 | | |
| | | 354 | | internal string Key { get; } |
| | | 355 | | internal T Value { get; } |
| | | 356 | | internal Node? Left { get; } |
| | | 357 | | internal Node? Right { get; } |
| | | 358 | | internal int Height { get; } |
| | | 359 | | internal int Count { get; } |
| | | 360 | | } |
| | | 361 | | } |