Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SortedInt32KeyNode.cs
Go to the documentation of this file.
5
7
8[DebuggerDisplay("{_key} = {_value}")]
9internal sealed class SortedInt32KeyNode<TValue> : IBinaryTree
10{
12 public struct Enumerator : IEnumerator<KeyValuePair<int, TValue>>, IEnumerator, IDisposable, ISecurePooledObjectUser
13 {
15
16 private readonly int _poolUserId;
17
19
21
23
25 {
26 get
27 {
29 if (_current != null)
30 {
31 return _current.Value;
32 }
33 throw new InvalidOperationException();
34 }
35 }
36
37 int ISecurePooledObjectUser.PoolUserId => _poolUserId;
38
39 object IEnumerator.Current => Current;
40
42 {
43 Requires.NotNull(root, "root");
44 _root = root;
45 _current = null;
47 _stack = null;
48 if (!_root.IsEmpty)
49 {
50 if (!s_enumeratingStacks.TryTake(this, out _stack))
51 {
53 }
55 }
56 }
57
58 public void Dispose()
59 {
60 _root = null;
61 _current = null;
62 if (_stack != null && _stack.TryUse(ref this, out var value))
63 {
64 value.ClearFastWhenEmpty();
65 s_enumeratingStacks.TryAdd(this, _stack);
66 }
67 _stack = null;
68 }
69
70 public bool MoveNext()
71 {
73 if (_stack != null)
74 {
76 if (stack.Count > 0)
77 {
78 PushLeft((_current = stack.Pop().Value).Right);
79 return true;
80 }
81 }
82 _current = null;
83 return false;
84 }
85
86 public void Reset()
87 {
89 _current = null;
90 if (_stack != null)
91 {
93 stack.ClearFastWhenEmpty();
95 }
96 }
97
98 internal void ThrowIfDisposed()
99 {
100 if (_root == null || (_stack != null && !_stack.IsOwned(ref this)))
101 {
102 Requires.FailObjectDisposed(this);
103 }
104 }
105
107 {
108 Requires.NotNull(node, "node");
110 while (!node.IsEmpty)
111 {
113 node = node.Left;
114 }
115 }
116 }
117
119
120 private readonly int _key;
121
122 private readonly TValue _value;
123
124 private bool _frozen;
125
126 private byte _height;
127
129
131
132 public bool IsEmpty => _left == null;
133
134 public int Height => _height;
135
137
139
140 IBinaryTree? IBinaryTree.Left => _left;
141
142 IBinaryTree? IBinaryTree.Right => _right;
143
144 int IBinaryTree.Count
145 {
146 get
147 {
148 throw new NotSupportedException();
149 }
150 }
151
153
155 {
156 get
157 {
159 while (enumerator.MoveNext())
160 {
161 yield return enumerator.Current.Value;
162 }
163 }
164 }
165
167 {
168 _frozen = true;
169 }
170
171 private SortedInt32KeyNode(int key, TValue value, SortedInt32KeyNode<TValue> left, SortedInt32KeyNode<TValue> right, bool frozen = false)
172 {
173 Requires.NotNull(left, "left");
174 Requires.NotNull(right, "right");
175 _key = key;
176 _value = value;
177 _left = left;
178 _right = right;
179 _frozen = frozen;
180 _height = checked((byte)(1 + Math.Max(left._height, right._height)));
181 }
182
184 {
185 return new Enumerator(this);
186 }
187
193
195 {
197 }
198
199 internal TValue? GetValueOrDefault(int key)
200 {
202 while (true)
203 {
204 if (sortedInt32KeyNode.IsEmpty)
205 {
206 return default(TValue);
207 }
208 if (key == sortedInt32KeyNode._key)
209 {
210 break;
211 }
213 }
214 return sortedInt32KeyNode._value;
215 }
216
217 internal bool TryGetValue(int key, [MaybeNullWhen(false)] out TValue value)
218 {
220 while (true)
221 {
222 if (sortedInt32KeyNode.IsEmpty)
223 {
224 value = default(TValue);
225 return false;
226 }
227 if (key == sortedInt32KeyNode._key)
228 {
229 break;
230 }
232 }
233 value = sortedInt32KeyNode._value;
234 return true;
235 }
236
237 internal void Freeze(Action<KeyValuePair<int, TValue>>? freezeAction = null)
238 {
239 if (!_frozen)
240 {
242 _left.Freeze(freezeAction);
243 _right.Freeze(freezeAction);
244 _frozen = true;
245 }
246 }
247
249 {
250 Requires.NotNull(tree, "tree");
251 if (tree._right.IsEmpty)
252 {
253 return tree;
254 }
255 SortedInt32KeyNode<TValue> right = tree._right;
256 return right.Mutate(tree.Mutate(null, right._left));
257 }
258
260 {
261 Requires.NotNull(tree, "tree");
262 if (tree._left.IsEmpty)
263 {
264 return tree;
265 }
266 SortedInt32KeyNode<TValue> left = tree._left;
267 return left.Mutate(null, tree.Mutate(left._right));
268 }
269
271 {
272 Requires.NotNull(tree, "tree");
273 if (tree._right.IsEmpty)
274 {
275 return tree;
276 }
277 SortedInt32KeyNode<TValue> tree2 = tree.Mutate(null, RotateRight(tree._right));
278 return RotateLeft(tree2);
279 }
280
282 {
283 Requires.NotNull(tree, "tree");
284 if (tree._left.IsEmpty)
285 {
286 return tree;
287 }
289 return RotateRight(tree2);
290 }
291
293 {
294 Requires.NotNull(tree, "tree");
295 return tree._right._height - tree._left._height;
296 }
297
299 {
300 Requires.NotNull(tree, "tree");
301 return Balance(tree) >= 2;
302 }
303
305 {
306 Requires.NotNull(tree, "tree");
307 return Balance(tree) <= -2;
308 }
309
311 {
312 Requires.NotNull(tree, "tree");
313 if (IsRightHeavy(tree))
314 {
315 if (Balance(tree._right) >= 0)
316 {
317 return RotateLeft(tree);
318 }
319 return DoubleLeft(tree);
320 }
321 if (IsLeftHeavy(tree))
322 {
323 if (Balance(tree._left) <= 0)
324 {
325 return RotateRight(tree);
326 }
327 return DoubleRight(tree);
328 }
329 return tree;
330 }
331
333 {
334 replacedExistingValue = false;
335 if (IsEmpty)
336 {
337 mutated = true;
338 return new SortedInt32KeyNode<TValue>(key, value, this, this);
339 }
341 if (key > _key)
342 {
344 if (mutated)
345 {
346 sortedInt32KeyNode = Mutate(null, right);
347 }
348 }
349 else if (key < _key)
350 {
352 if (mutated)
353 {
355 }
356 }
357 else
358 {
359 if (valueComparer.Equals(_value, value))
360 {
361 mutated = false;
362 return this;
363 }
365 {
367 }
368 mutated = true;
371 }
372 if (!mutated)
373 {
374 return sortedInt32KeyNode;
375 }
377 }
378
380 {
381 if (IsEmpty)
382 {
383 mutated = false;
384 return this;
385 }
387 if (key == _key)
388 {
389 mutated = true;
390 if (_right.IsEmpty && _left.IsEmpty)
391 {
393 }
394 else if (_right.IsEmpty && !_left.IsEmpty)
395 {
397 }
398 else if (!_right.IsEmpty && _left.IsEmpty)
399 {
401 }
402 else
403 {
405 while (!sortedInt32KeyNode2._left.IsEmpty)
406 {
408 }
409 bool mutated2;
412 }
413 }
414 else if (key < _key)
415 {
417 if (mutated)
418 {
420 }
421 }
422 else
423 {
425 if (mutated)
426 {
428 }
429 }
430 if (!sortedInt32KeyNode.IsEmpty)
431 {
433 }
434 return sortedInt32KeyNode;
435 }
436
438 {
439 if (_frozen)
440 {
441 return new SortedInt32KeyNode<TValue>(_key, _value, left ?? _left, right ?? _right);
442 }
443 if (left != null)
444 {
445 _left = left;
446 }
447 if (right != null)
448 {
449 _right = right;
450 }
451 _height = checked((byte)(1 + Math.Max(_left._height, _right._height)));
452 return this;
453 }
454}
SortedInt32KeyNode(int key, TValue value, SortedInt32KeyNode< TValue > left, SortedInt32KeyNode< TValue > right, bool frozen=false)
SortedInt32KeyNode< TValue > RemoveRecursive(int key, out bool mutated)
static bool IsRightHeavy(SortedInt32KeyNode< TValue > tree)
static bool IsLeftHeavy(SortedInt32KeyNode< TValue > tree)
SortedInt32KeyNode< TValue > Mutate(SortedInt32KeyNode< TValue > left=null, SortedInt32KeyNode< TValue > right=null)
static SortedInt32KeyNode< TValue > MakeBalanced(SortedInt32KeyNode< TValue > tree)
void Freeze(Action< KeyValuePair< int, TValue > >? freezeAction=null)
SortedInt32KeyNode< TValue > Remove(int key, out bool mutated)
static SortedInt32KeyNode< TValue > DoubleRight(SortedInt32KeyNode< TValue > tree)
bool TryGetValue(int key, [MaybeNullWhen(false)] out TValue value)
static int Balance(SortedInt32KeyNode< TValue > tree)
SortedInt32KeyNode< TValue > SetItem(int key, TValue value, IEqualityComparer< TValue > valueComparer, out bool replacedExistingValue, out bool mutated)
static SortedInt32KeyNode< TValue > RotateLeft(SortedInt32KeyNode< TValue > tree)
static SortedInt32KeyNode< TValue > DoubleLeft(SortedInt32KeyNode< TValue > tree)
static readonly SortedInt32KeyNode< TValue > EmptyNode
SortedInt32KeyNode< TValue > SetOrAdd(int key, TValue value, IEqualityComparer< TValue > valueComparer, bool overwriteExistingValue, out bool replacedExistingValue, out bool mutated)
static SortedInt32KeyNode< TValue > RotateRight(SortedInt32KeyNode< TValue > tree)
static byte Max(byte val1, byte val2)
Definition Math.cs:738
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string DuplicateKey
Definition SR.cs:28
Definition SR.cs:7
SecurePooledObject< Stack< RefAsValueType< SortedInt32KeyNode< TValue > > > > _stack
static readonly SecureObjectPool< Stack< RefAsValueType< SortedInt32KeyNode< TValue > > >, Enumerator > s_enumeratingStacks